From fc8e7be3ab36e22c7a79f8843457f5a24e2d85c2 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 18 Apr 2018 14:08:31 +0100 Subject: [PATCH] Only show notices on specific screens --- includes/admin/class-wc-admin-notices.php | 34 ++++++++++++++++------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/includes/admin/class-wc-admin-notices.php b/includes/admin/class-wc-admin-notices.php index cd0fd68991e..959bded7d3e 100644 --- a/includes/admin/class-wc-admin-notices.php +++ b/includes/admin/class-wc-admin-notices.php @@ -152,18 +152,32 @@ class WC_Admin_Notices { public static function add_notices() { $notices = self::get_notices(); - if ( ! empty( $notices ) ) { - wp_enqueue_style( 'woocommerce-activation', plugins_url( '/assets/css/activation.css', WC_PLUGIN_FILE ), array(), WC_VERSION ); + if ( empty( $notices ) ) { + return; + } - // Add RTL support. - wp_style_add_data( 'woocommerce-activation', 'rtl', 'replace' ); + $screen = get_current_screen(); + $screen_id = $screen ? $screen->id : ''; + $show_on_screens = array( + 'dashboard', + 'plugins', + ); - foreach ( $notices as $notice ) { - if ( ! empty( self::$core_notices[ $notice ] ) && apply_filters( 'woocommerce_show_admin_notice', true, $notice ) ) { - add_action( 'admin_notices', array( __CLASS__, self::$core_notices[ $notice ] ) ); - } else { - add_action( 'admin_notices', array( __CLASS__, 'output_custom_notices' ) ); - } + // Notices should only show on WooCommerce screens, the main dashboard, and on the plugins screen. + if ( ! in_array( $screen_id, wc_get_screen_ids(), true ) && ! in_array( $screen_id, $show_on_screens, true ) ) { + return; + } + + wp_enqueue_style( 'woocommerce-activation', plugins_url( '/assets/css/activation.css', WC_PLUGIN_FILE ), array(), WC_VERSION ); + + // Add RTL support. + wp_style_add_data( 'woocommerce-activation', 'rtl', 'replace' ); + + foreach ( $notices as $notice ) { + if ( ! empty( self::$core_notices[ $notice ] ) && apply_filters( 'woocommerce_show_admin_notice', true, $notice ) ) { + add_action( 'admin_notices', array( __CLASS__, self::$core_notices[ $notice ] ) ); + } else { + add_action( 'admin_notices', array( __CLASS__, 'output_custom_notices' ) ); } } }