$notification_details['id'], 'display_at' => time(), ] ); } if ( empty( $notification_details ) ) { return; } $notification_html = self::get_notification_html( $notification_details ); do_action( $notification_details['id'] . '_before_render' ); echo $notification_html; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, already escaped internally. do_action( $notification_details['id'] . '_after_render' ); self::render_snippets(); } /** * Get last notification details. * * @return array Last notification details. */ private static function get_last_notification() { $notification = self::get_notifications_metadata(); return isset( $notification['last_notification'] ) ? $notification['last_notification'] : []; } /** * Get notification center details. * * @return array Notification center details. */ private static function get_notifications_metadata() { $data = get_option( 'themeisle_sdk_notifications', [ 'last_notification' => [], 'last_notification_active' => 0, ] ); return $data; } /** * Check if the notification is still possible. * * @param array $notification Notification to check. * * @return array Either is still active or not. */ private static function get_notification_details( $notification ) { $notifications = array_filter( self::$notifications, function ( $value ) use ( $notification ) { if ( isset( $value['id'] ) && isset( $notification['id'] ) && $value['id'] === $notification['id'] ) { return true; } return false; } ); return ! empty( $notifications ) ? reset( $notifications ) : []; } /** * Check if the notification is expired. * * @param array $notification Notification to check. * * @return bool Either the notification is due. */ private static function is_notification_expired( $notification ) { if ( ! isset( $notification['display_at'] ) ) { return true; } $notifications = array_filter( self::$notifications, function ( $value ) use ( $notification ) { if ( isset( $value['id'] ) && isset( $notification['id'] ) && $value['id'] === $notification['id'] ) { return true; } return false; } ); if ( empty( $notifications ) ) { return true; } $notification_definition = reset( $notifications ); $when_to_expire = isset( $notification_definition['expires_at'] ) ? $notification_definition['expires_at'] : ( isset( $notification_definition['expires'] ) ? ( $notification['display_at'] + $notification_definition['expires'] ) : ( $notification['display_at'] + self::MAX_TIME_TO_LIVE * DAY_IN_SECONDS ) ); return ( $when_to_expire - time() ) < 0; } /** * Set last notification details. */ private static function set_last_active_notification_timestamp() { $metadata = self::get_notifications_metadata(); $metadata['last_notification_active'] = time(); update_option( 'themeisle_sdk_notifications', $metadata ); } /** * Return notification to show. * * @return array Notification data. */ public static function get_random_notification() { if ( ( time() - self::get_last_active_notification_timestamp() ) < self::TIME_BETWEEN_NOTIFICATIONS * DAY_IN_SECONDS ) { return []; } $notifications = self::$notifications; $notifications = array_filter( $notifications, function ( $value ) { if ( isset( $value['sticky'] ) && true === $value['sticky'] ) { return true; } return false; } ); // No priority notifications, use all. if ( empty( $notifications ) ) { $notifications = self::$notifications; } if ( empty( $notifications ) ) { return []; } $notifications = array_values( $notifications ); return $notifications[ array_rand( $notifications, 1 ) ]; } /** * Get last notification details. * * @return int Last notification details. */ private static function get_last_active_notification_timestamp() { $notification = self::get_notifications_metadata(); return isset( $notification['last_notification_active'] ) ? $notification['last_notification_active'] : 0; } /** * Get last notification details. * * @param array $notification Notification data. */ private static function set_active_notification( $notification ) { $metadata = self::get_notifications_metadata(); $metadata['last_notification'] = $notification; update_option( 'themeisle_sdk_notifications', $metadata ); } /** * Get notification html. * * @param array $notification_details Notification details. * * @return string Html for notice. */ public static function get_notification_html( $notification_details ) { $default = [ 'id' => '', 'heading' => '', 'message' => '', 'ctas' => [ 'confirm' => [ 'link' => '#', 'text' => '', ], 'cancel' => [ 'link' => '#', 'text' => '', ], ], ]; $notification_details = wp_parse_args( $notification_details, $default ); global $pagenow; $notification_details['ctas']['cancel']['link'] = wp_nonce_url( add_query_arg( [ 'nid' => $notification_details['id'] ], admin_url( $pagenow ) ), $notification_details['id'], 'tsdk_dismiss_nonce' ); $notification_html = '
'; if ( ! empty( $notification_details['heading'] ) ) { $notification_html .= sprintf( '

%s

', wp_kses_post( $notification_details['heading'] ) ); } if ( ! empty( $notification_details['message'] ) ) { $notification_html .= wp_kses_post( $notification_details['message'] ); } $notification_html .= '
'; if ( ! empty( $notification_details['ctas']['confirm']['text'] ) ) { $notification_html .= sprintf( '%s', esc_url( $notification_details['ctas']['confirm']['link'] ), esc_attr( $notification_details['id'] . '_confirm' ), wp_kses_post( $notification_details['ctas']['confirm']['text'] ) ); } if ( ! empty( $notification_details['ctas']['cancel']['text'] ) ) { $notification_html .= sprintf( '%s', esc_url( $notification_details['ctas']['cancel']['link'] ), esc_attr( $notification_details['id'] ) . '_cancel', wp_kses_post( $notification_details['ctas']['cancel']['text'] ) ); } $notification_html .= '
'; $notification_html .= '
'; $notification_html .= '
'; return $notification_html; } /** * Adds js snippet for hiding the notice. */ public static function render_snippets() { ?> is_from_partner( $product ) ) { return false; } if ( ! current_user_can( 'manage_options' ) ) { return false; } if ( ( time() - $product->get_install_time() ) < ( self::MIN_INSTALL_TIME * HOUR_IN_SECONDS ) ) { return false; } return true; } /** * Setup notifications queue. */ public static function setup_notifications() { $notifications = apply_filters( 'themeisle_sdk_registered_notifications', [] ); $notifications = array_filter( $notifications, function ( $value ) { if ( ! isset( $value['id'] ) ) { return false; } if ( get_option( $value['id'], '' ) !== '' ) { return false; } return apply_filters( $value['id'] . '_should_show', true ); } ); self::$notifications = $notifications; } /** * Load the module logic. * * @param Product $product Product to load the module for. * * @return Notification Module instance. */ public function load( $product ) { if ( apply_filters( 'themeisle_sdk_hide_notifications', false ) ) { return; } $this->product = $product; $notifications = apply_filters( 'themeisle_sdk_registered_notifications', [] ); $notifications = array_filter( $notifications, function ( $value ) { if ( ! isset( $value['id'] ) ) { return false; } if ( get_option( $value['id'], '' ) !== '' ) { return false; } return apply_filters( $value['id'] . '_should_show', true ); } ); self::$notifications = $notifications; add_action( 'admin_notices', array( __CLASS__, 'show_notification' ) ); add_action( 'wp_ajax_themeisle_sdk_dismiss_notice', array( __CLASS__, 'dismiss' ) ); add_action( 'admin_head', array( __CLASS__, 'dismiss_get' ) ); add_action( 'admin_head', array( __CLASS__, 'setup_notifications' ) ); return $this; } }