$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' => '', 'img_src' => '', 'message' => '', 'ctas' => [ 'confirm' => [ 'link' => '#', 'text' => '', ], 'cancel' => [ 'link' => '#', 'text' => '', ], ], 'type' => 'success', ]; $notification_details = wp_parse_args( $notification_details, $default ); global $pagenow; $type = in_array( $notification_details['type'], [ 'success', 'info', 'warning', 'error' ], true ) ? $notification_details['type'] : 'success'; $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 = '