hooks(); } /** * Use this function to get the option name to allow * inheritance for the New_User class * * @return string */ public function option_name() { return self::OPTION_NAME; } /** * Use this function to get the source URL to allow * inheritance for the New_User class * * @return string */ public function source_url() { return self::SOURCE_URL; } /** * Register hooks. * * @since 2.6/5.9 */ public function hooks() { add_action( 'admin_enqueue_scripts', array( $this, 'enqueues' ) ); add_action( 'sby_admin_notices', array( $this, 'output' ) ); // on cron. Once a week? add_action( 'sby_notification_update', array( $this, 'update' ) ); add_action( 'wp_ajax_sby_dashboard_notification_dismiss', array( $this, 'dismiss' ) ); } /** * Check if user has access and is enabled. * * @since 2.6/5.9 * * @return bool */ public function has_access() { $access = false; if ( current_user_can( 'manage_youtube_feed_options' ) || current_user_can( 'manage_options' ) ) { $access = true; } return apply_filters( 'sby_admin_notifications_has_access', $access ); } /** * Get option value. * * @since 2.6/5.9 * * @param bool $cache Reference property cache if available. * * @return array */ public function get_option( $cache = true ) { if ( $this->option && $cache ) { return $this->option; } $option = get_option( $this->option_name(), array() ); $this->option = array( 'update' => ! empty( $option['update'] ) ? $option['update'] : 0, 'events' => ! empty( $option['events'] ) ? $option['events'] : array(), 'feed' => ! empty( $option['feed'] ) ? $option['feed'] : array(), 'dismissed' => ! empty( $option['dismissed'] ) ? $option['dismissed'] : array(), ); return $this->option; } /** * Fetch notifications from feed. * * @since 2.6/5.9 * * @return array */ public function fetch_feed() { $res = wp_remote_get( $this->source_url() ); if ( is_wp_error( $res ) ) { return array(); } $body = wp_remote_retrieve_body( $res ); if ( empty( $body ) ) { return array(); } return $this->verify( json_decode( $body, true ) ); } /** * Verify notification data before it is saved. * * @since 2.6/5.9 * * @param array $notifications Array of notifications items to verify. * * @return array */ public function verify( $notifications ) { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh $data = array(); if ( ! is_array( $notifications ) || empty( $notifications ) ) { return $data; } $option = $this->get_option(); foreach ( $notifications as $notification ) { // Ignore if not a targeted plugin if ( ! empty( $notification['plugin'] ) && is_array( $notification['plugin'] ) && ! in_array( self::PLUGIN, $notification['plugin'], true ) ) { continue; } // Ignore if max wp version detected if ( ! empty( $notification['maxwpver'] ) && version_compare( get_bloginfo( 'version' ), $notification['maxwpver'], '>' ) ) { continue; } // Ignore if max version has been reached if ( ! empty( $notification['maxver'] ) && version_compare( $notification['maxver'], SBYVER ) < 0 ) { continue; } // Ignore if min version has not been reached if ( ! empty( $notification['minver'] ) && version_compare( $notification['minver'], SBYVER ) > 0 ) { continue; } // Ignore if a specific sby_status is empty or false if ( ! empty( $notification['statuscheck'] ) ) { $status_key = sanitize_key( $notification['statuscheck'] ); $sby_statuses_option = get_option( 'sby_statuses', array() ); if ( empty( $sby_statuses_option[ $status_key ] ) ) { continue; } } // The message and license should never be empty, if they are, ignore. if ( empty( $notification['content'] ) || empty( $notification['type'] ) ) { continue; } // Ignore if license type does not match. $license = sby_is_pro_version() ? 'pro' : 'free'; if ( ! in_array( $license, $notification['type'], true ) ) { continue; } // Ignore if expired. if ( ! empty( $notification['end'] ) && time() > strtotime( $notification['end'] ) ) { continue; } // Ignore if notification has already been dismissed. if ( ! empty( $option['dismissed'] ) && in_array( $notification['id'], $option['dismissed'] ) ) { // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict continue; } // Ignore message "9". if ( in_array( (int)$notification['id'], array( 9 ) ) ) { // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict continue; } // TODO: Ignore if notification existed before installing SBY. // Prevents bombarding the user with notifications after activation. $activated = false; if ( ! empty( $activated ) && ! empty( $notification['start'] ) && $activated > strtotime( $notification['start'] ) ) { continue; } $data[] = $notification; } return $data; } /** * Verify saved notification data for active notifications. * * @since 2.6/5.9 * * @param array $notifications Array of notifications items to verify. * * @return array */ public function verify_active( $notifications ) { if ( ! is_array( $notifications ) || empty( $notifications ) ) { return array(); } // Remove notfications that are not active. foreach ( $notifications as $key => $notification ) { if ( ( ! empty( $notification['start'] ) && sby_get_current_time() < strtotime( $notification['start'] ) ) || ( ! empty( $notification['end'] ) && sby_get_current_time() > strtotime( $notification['end'] ) ) ) { unset( $notifications[ $key ] ); } if ( empty( $notification['recent_install_override'] ) && $this->recently_installed() ) { unset( $notifications[ $key ] ); } // Ignore if max version has been reached if ( ! empty( $notification['maxver'] ) && version_compare( $notification['maxver'], SBYVER ) < 0 ) { unset( $notifications[ $key ] ); } // Ignore if max wp version detected if ( ! empty( $notification['maxwpver'] ) && version_compare( get_bloginfo( 'version' ), $notification['maxwpver'], '>' ) ) { unset( $notifications[ $key ] ); } // Ignore if min version has not been reached if ( ! empty( $notification['minver'] ) && version_compare( $notification['minver'], SBYVER ) > 0 ) { unset( $notifications[ $key ] ); } // Ignore if a specific sby_status is empty or false if ( ! empty( $notification['statuscheck'] ) ) { $status_key = sanitize_key( $notification['statuscheck'] ); $sby_statuses_option = get_option( 'sby_statuses', array() ); if ( empty( $sby_statuses_option[ $status_key ] ) ) { unset( $notifications[ $key ] ); } } } return $notifications; } /** * @return bool * * @since 1.4.5/1.4.2 */ public function recently_installed() { $sby_statuses_option = get_option( 'sby_statuses', array() ); if ( ! isset( $sby_statuses_option['first_install'] ) ) { return false; } // Plugin was installed less than a week ago if ( (int) $sby_statuses_option['first_install'] > time() - WEEK_IN_SECONDS ) { return true; } return false; } /** * Get notification data. * * @since 2.6/5.9 * * @return array */ public function get() { if ( ! $this->has_access() ) { return array(); } $option = $this->get_option(); // Update notifications using async task. if ( empty( $option['update'] ) || sby_get_current_time() > $option['update'] + DAY_IN_SECONDS ) { $this->update(); } $events = ! empty( $option['events'] ) ? $this->verify_active( $option['events'] ) : array(); $feed = ! empty( $option['feed'] ) ? $this->verify_active( $option['feed'] ) : array(); // If there is a new user notification, add it to the beginning of the notification list $sby_newuser = new SBY_New_User(); $newuser_notifications = $sby_newuser->get(); if ( ! empty( $newuser_notifications ) ) { return $newuser_notifications; } return array_merge( $events, $feed ); } /** * Get notification count. * * @since 2.6/5.9 * * @return int */ public function get_count() { return count( $this->get() ); } /** * Add a manual notification event. * * @since 2.6/5.9 * * @param array $notification Notification data. */ public function add( $notification ) { if ( empty( $notification['id'] ) ) { return; } $option = $this->get_option(); if ( in_array( $notification['id'], $option['dismissed'] ) ) { // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict return; } foreach ( $option['events'] as $item ) { if ( $item['id'] === $notification['id'] ) { return; } } $notification = $this->verify( array( $notification ) ); update_option( 'sby_notifications', array( 'update' => $option['update'], 'feed' => $option['feed'], 'events' => array_merge( $notification, $option['events'] ), 'dismissed' => $option['dismissed'], ) ); } /** * Update notification data from feed. * * @since 2.6/5.9 */ public function update() { $feed = $this->fetch_feed(); $option = $this->get_option(); update_option( 'sby_notifications', array( 'update' => sby_get_current_time(), 'feed' => $feed, 'events' => $option['events'], 'dismissed' => $option['dismissed'], ) ); } /** * Admin area Form Overview enqueues. * * @since 2.6/5.9 */ public function enqueues() { if ( ! $this->has_access() ) { return; } $notifications = $this->get(); if ( empty( $notifications ) ) { return; } $min = ''; wp_enqueue_style( 'sby-admin-notifications', SBY_PLUGIN_URL . "css/admin-notifications{$min}.css", array(), SBYVER ); wp_enqueue_script( 'sby-admin-notifications', SBY_PLUGIN_URL . "js/admin-notifications{$min}.js", array( 'jquery' ), SBYVER, true ); wp_localize_script( 'sby-admin-notifications', 'sby_admin', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'sby-admin' ), ) ); } /** * Fields from the remote source contain placeholders to allow * some messages to be used for multiple plugins. * * @param $content string * @param $notification array * * @return string * * @since 2.6/5.9 */ public function replace_merge_fields( $content, $notification ) { $merge_fields = array( '{plugin}' => 'YouTube Feeds', '{amount}' => isset( $notification['amount'] ) ? $notification['amount'] : '', '{platform}' => 'YouTube', '{lowerplatform}' => 'youtube', '{review-url}' => 'https://wordpress.org/support/plugin/feeds-for-youtube/reviews/', '{slug}' => 'feeds-for-youtube', '{campaign}' => sby_utm_campaign() ); if ( sby_is_pro_version() ) { $merge_fields['{campaign}'] = 'youtube-pro'; $merge_fields['{plugin}'] = 'YouTube Feeds Pro'; } foreach ( $merge_fields as $find => $replace ) { $content = str_replace( $find, $replace, $content ); } return $content; } /** * Output notifications on YouTube Feeds admin area. * * @since 2.6/5.9 */ public function output() { // if we are one single feed page then return if ( isset( $_GET['feed_id'] ) ) { return; } $notifications = $this->get(); if ( empty( $notifications ) ) { return; } $is_review_notice = ! empty( $notifications[0] ) && ! empty( $notifications[0]['id'] ) && $notifications[0]['id'] === 'review'; if ( ! $is_review_notice && ! empty( $_GET['feed_id'] ) ) { return; } $notifications_html = ''; $current_class = ' current'; $content_allowed_tags = array( 'em' => array(), 'strong' => array(), 'span' => array( 'style' => array(), ), 'a' => array( 'href' => array(), 'target' => array(), 'rel' => array(), ), ); foreach ( $notifications as $notification ) { $type = $notification['id']; // Buttons HTML. $buttons_html = ''; if ( ! empty( $notification['btns'] ) && is_array( $notification['btns'] ) ) { foreach ( $notification['btns'] as $btn_type => $btn ) { if ( $type == 'review' || $type == 'discount' ) { $btn_class = $btn_type === 'primary' ? 'sby-btn-blue' : 'sby-btn-grey'; } else { $btn_class = $btn_type === 'primary' ? 'sby-btn-orange' : 'sby-btn-grey'; } if ( is_array( $btn['url'] ) ) { $args = array(); foreach ( $btn['url'] as $key => $value ) { $args[ sanitize_key( $key ) ] = sanitize_key( $value ); } $btn['url'] = add_query_arg( $args ); } if ( ! empty( $btn['attr'] ) ) { $btn['target'] = '_blank'; } if ( empty( $btn['class'] ) ) { $btn['class'] = ''; } $buttons_html .= sprintf( '%5$s', ! empty( $btn['url'] ) ? esc_url( $this->replace_merge_fields( str_replace( 'sbi_', 'sby_', $btn['url'] ), $notification ) ) : '', esc_attr( $btn['class'] ), esc_attr( $btn_class ), ! empty( $btn['target'] ) && $btn['target'] === '_blank' ? ' target="_blank" rel="noopener noreferrer"' : '', ! empty( $btn['text'] ) ? sanitize_text_field( $btn['text'] ) : '' ); } $buttons_html = ! empty( $buttons_html ) ? '
' . $buttons_html . '
' : ''; } if ( empty( $notification['image'] ) ) { $image_html = '
'; $image_html .= ''; $image_html .= '
'; } else { if ( $notification['image'] === 'balloon' ) { $image_html = sprintf( '
notice', SBY_PLUGIN_URL . 'img/balloon.svg' ); } else if ( $notification['id'] === 'review' || $notification['id'] === 'discount' ) { $image_html = sprintf( '
notice', SBY_PLUGIN_URL . 'img/' . sanitize_text_field( str_replace( array( 'sbi', '.png' ), array( 'sby', '.svg' ), $notification['image'] ) ) ); } else { $image_html = '
'; $img_src = SBY_PLUGIN_URL . 'img/' . sanitize_text_field( $notification['image'] ); $image_html .= 'notice'; if ( isset( $notification['image_overlay'] ) ) { $image_html .= '
'. esc_html( str_replace( '%', '%%', $notification['image_overlay'] ) ).'
'; } } $image_html .= '
'; } // Check if it's review notice then show step #1 if ( $type == 'review' ) { $step1_img = SBY_PLUGIN_URL . 'img/' . sanitize_text_field( str_replace( array( 'sbi', 'png' ), array( 'sby', 'svg' ), $notification['image'] ) ); $step1_img_html = sprintf('
notice
', $step1_img); $review_consent = get_option( 'sby_review_consent' ); $sby_open_feedback_url = 'https://smashballoon.com/feedback/?plugin=' . sby_utm_campaign(); // step #1 for the review notice if ( ! $review_consent ) { $step1_btns = sprintf( '', __( 'Yes', 'feeds-for-youtube' ) ); $step1_btns .= sprintf( '%s', $sby_open_feedback_url, __( 'No', 'feeds-for-youtube' ) ); $notifications_html .= sprintf( '
' . $step1_img_html . '

%1$s

%2$s
', __( 'Are you enjoying the YouTube Feeds Plugin?', 'feeds-for-youtube' ), $step1_btns, ! empty( $notification['id'] ) ? esc_attr( sanitize_text_field( $notification['id'] ) ) : 0 ); } } $review_consent = get_option( 'sby_review_consent' ); $review_step2_style = ''; if ( $type == 'review' && ! $review_consent ) { $review_step2_style = 'style="display: none;"'; } // Build the notification HTML for review notice if ( $type == 'review' ) { $notifications_html .= sprintf( '
' . $image_html . '

%1$s

%2$s

%3$s
', __( 'Glad to hear you are enjoying it. Would you consider leaving a positive review?', 'feeds-for-youtube' ), __( 'It really helps to support the plugin and help others to discover it too!', 'feeds-for-youtube' ), $buttons_html, ! empty( $notification['id'] ) ? esc_attr( sanitize_text_field( $notification['id'] ) ) : 0, $current_class, ( $notification['id'] == 'review' && ! empty( $review_step2_style ) ) ? $review_step2_style : '', ( $type == 'review' ) ? 'rn_step_2' : '' ); } else if ( $type == 'discount' ) { // Notification HTML for other notices $notifications_html .= sprintf( '
' . $image_html . '

%1$s

%2$s

%3$s
', ! empty( $notification['title'] ) ? $this->get_notice_title( $notification ) : '', ! empty( $notification['content'] ) ? $this->get_notice_content( $notification, $content_allowed_tags ) : '', $buttons_html, ! empty( $notification['id'] ) ? esc_attr( sanitize_text_field( $notification['id'] ) ) : 0, $current_class, ( $notification['id'] == 'review' && ! empty( $review_step2_style ) ) ? $review_step2_style : '' ); } else { // Notification HTML for other notices $notifications_html .= sprintf( '
' . $image_html . '

%1$s

%2$s

%3$s
', ! empty( $notification['title'] ) ? $this->replace_merge_fields( sanitize_text_field( $notification['title'] ), $notification ) : '', ! empty( $notification['content'] ) ? wp_kses( $this->replace_merge_fields( $notification['content'], $notification ), $content_allowed_tags ) : '', $buttons_html, ! empty( $notification['id'] ) ? esc_attr( sanitize_text_field( $notification['id'] ) ) : 0, $current_class, ( $notification['id'] == 'review' && ! empty( $review_step2_style ) ) ? $review_step2_style : '' ); } // Only first notification is current. $current_class = ''; } $close_href = add_query_arg( array( 'sby_dismiss' => $type ) ); $type_class = ''; if ( $type === 'review' || $type == 'discount' ) { $type_class = $type === 'review' ? 'sby_review_notice' : 'sby_discount_notice'; } ?>
> 1 ) : ?>
replace_merge_fields( $notification['title'], $notification ); } return $title; } /** * SBY Get Notice Content depending on the notice type * * @since 2.0 * * @param array $notification * @param array $content_allowed_tags * * @return string $content */ public function get_notice_content( $notification, $content_allowed_tags ) { $type = $notification['id']; $content = ''; // Notice content depending on notice type if ( $type == 'review' ) { $content = __( 'It really helps to support the plugin and help others to discover it too!', 'feeds-for-youtube' ); } else if ( $type == 'discount' ) { $content = __( 'We don’t run promotions very often, but for a limited time we’re offering 60% Off our Pro version to all users of our free YouTube Feeds.', 'feeds-for-youtube' ); } else { if ( ! empty( $notification['content'] ) ) { $content = wp_kses( $this->replace_merge_fields( $notification['content'], $notification ), $content_allowed_tags ); } } return $content; } /** * Dismiss notification via AJAX. If it's a new user message, also dismiss it * on all admin pages. * * @since 2.6/5.9 */ public function dismiss() { // Run a security check. check_ajax_referer( 'sby-admin', 'nonce' ); // Check for access and required param. if ( ! $this->has_access() || empty( $_POST['id'] ) ) { wp_send_json_error(); } $id = sanitize_text_field( wp_unslash( $_POST['id'] ) ); if ( $id === 'review' ) { $sby_statuses_option = get_option( 'sby_statuses', array() ); update_option( 'sby_rating_notice', 'dismissed', false ); $sby_statuses_option['rating_notice_dismissed'] = sby_get_current_time(); update_option( 'sby_statuses', $sby_statuses_option, false ); } elseif ( $id === 'discount' ) { update_user_meta( get_current_user_id(), 'sby_ignore_new_user_sale_notice', 'always' ); $current_month_number = (int)date('n', sby_get_current_time() ); $not_early_in_the_year = ($current_month_number > 5); if ( $not_early_in_the_year ) { update_user_meta( get_current_user_id(), 'sby_ignore_bfcm_sale_notice', date( 'Y', sby_get_current_time() ) ); } } $option = $this->get_option(); $type = is_numeric( $id ) ? 'feed' : 'events'; $option['dismissed'][] = $id; $option['dismissed'] = array_unique( $option['dismissed'] ); // Remove notification. if ( is_array( $option[ $type ] ) && ! empty( $option[ $type ] ) ) { foreach ( $option[ $type ] as $key => $notification ) { if ( $notification['id'] == $id ) { // phpcs:ignore WordPress.PHP.StrictComparisons unset( $option[ $type ][ $key ] ); break; } } } update_option( 'sby_notifications', $option ); wp_send_json_success(); } public function output_return() { ob_start(); $this->output(); return ob_get_clean(); } }