warning( sprintf( /* Translators: %1$s: notice type ('success' or 'error'), %2$s: notice text. */ 'Admin notices can only be added if a user is currently logged in. Attempted (%1$s) notice: "%2$s"', $notice_type, $message ), array( 'backtrace' => true, 'user_id' => $user_id, ) ); return; } $notices = get_transient( '_wcs_admin_notices_' . $user_id ); if ( ! is_array( $notices ) ) { $notices = array(); } $notices[ $notice_type ][] = array( 'message' => $message, 'screen_id' => $screen_id, ); set_transient( '_wcs_admin_notices_' . $user_id, $notices, HOUR_IN_SECONDS ); } /** * Display any admin notices added with wcs_add_admin_notice(). * * @see wcs_add_admin_notice() * @since 1.0.0 Migrated from WooCommerce Subscriptions v2.0. * @since 7.2.0 Supports contextual awareness of the user and screen. * * @param bool $clear If the message queue should be cleared after rendering the message(s). Defaults to true. * * @return void */ function wcs_display_admin_notices( $clear = true ) { $user_id = get_current_user_id(); $notices = get_transient( '_wcs_admin_notices_' . $user_id ); if ( ! is_array( $notices ) || empty( $notices ) ) { return; } /** * Normalizes, sanitizes and outputs the provided notices. * * @param array &$notices The notice data. * @param string $class The CSS notice class to be applied (typically 'updated' or 'error'). * * @return void */ $handle_notices = static function ( &$notices, $class ) { $notice_output = array(); $screen_id = false; foreach ( $notices as $index => $notice ) { // Ensure the notice data now has the expected shape. If it does not, remove it. if ( ! is_array( $notice ) || ! isset( $notice['message'] ) || ! array_key_exists( 'screen_id', $notice ) ) { unset( $notices[ $index ] ); continue; } // We only need to determine the current screen ID once. if ( false === $screen_id ) { $screen = get_current_screen(); $screen_id = $screen instanceof WP_Screen ? $screen->id : ''; } // Should the notice display in the current screen context? if ( is_string( $notice['screen_id'] ) && $screen_id !== $notice['screen_id'] ) { continue; } $notice_output[] = $notice['message']; unset( $notices[ $index ] ); } // $notice_output may be empty if some notices were withheld, due to not matching the screen context. if ( ! empty( $notice_output ) ) { echo '
' . wp_kses_post( implode( "
\n", $notice_output ) ) . '