From 2d8f021cc7a4fba4ca13cc826f35946eaa73365f Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 20 Jan 2015 15:23:34 +0000 Subject: [PATCH] Optimise the admin notices class. @claudiosmweb --- includes/admin/class-wc-admin-notices.php | 171 +++++++++++------- includes/admin/class-wc-admin-status.php | 8 - includes/admin/class-wc-admin-welcome.php | 2 +- includes/admin/class-wc-admin.php | 14 +- .../views/html-notice-frontend-colors.php | 2 +- includes/admin/views/html-notice-install.php | 2 +- .../views/html-notice-template-check.php | 2 +- .../admin/views/html-notice-theme-support.php | 2 +- .../views/html-notice-translation-upgrade.php | 7 +- includes/class-wc-install.php | 36 ++-- includes/class-wc-language-pack-upgrader.php | 7 +- 11 files changed, 137 insertions(+), 116 deletions(-) diff --git a/includes/admin/class-wc-admin-notices.php b/includes/admin/class-wc-admin-notices.php index 35ab027c975..2ddc2fbe447 100644 --- a/includes/admin/class-wc-admin-notices.php +++ b/includes/admin/class-wc-admin-notices.php @@ -5,26 +5,41 @@ * @author WooThemes * @category Admin * @package WooCommerce/Admin - * @version 2.1.0 + * @version 2.3.0 */ if ( ! defined( 'ABSPATH' ) ) { - exit; // Exit if accessed directly + exit; } -if ( ! class_exists( 'WC_Admin_Notices' ) ) : - /** * WC_Admin_Notices Class */ class WC_Admin_Notices { /** - * Hook in tabs. + * Array of notices - name => callback + * @var array + */ + private $notices = array( + 'install' => 'install_notice', + 'update' => 'update_notice', + 'template_files' => 'template_file_check_notice', + 'frontend_colors' => 'frontend_colors_notice', + 'theme_support' => 'theme_check_notice', + 'translation_upgrade' => 'translation_upgrade_notice' + ); + + /** + * Constructor */ public function __construct() { add_action( 'switch_theme', array( $this, 'reset_admin_notices' ) ); add_action( 'woocommerce_updated', array( $this, 'reset_admin_notices' ) ); + add_action( 'wp_loaded', array( $this, 'hide_notices' ) ); + add_action( 'woocommerce_hide_frontend_colors_notice', array( $this, 'hide_frontend_colors_notice' ) ); + add_action( 'woocommerce_hide_install_notice', array( $this, 'hide_install_notice' ) ); + add_action( 'woocommerce_hide_translation_upgrade_notice', array( $this, 'hide_translation_upgrade_notice' ) ); add_action( 'admin_print_styles', array( $this, 'add_notices' ) ); } @@ -32,71 +47,107 @@ class WC_Admin_Notices { * Reset notices for themes when switched or a new version of WC is installed */ public function reset_admin_notices() { - update_option( 'woocommerce_admin_notices', array( 'template_files', 'theme_support' ) ); + $show_notices = array( 'template_files' ); + + if ( $this->has_frontend_colors() ) { + $show_notices[] = 'frontend_colors'; + } + + if ( ! current_theme_supports( 'woocommerce' ) && ! in_array( get_option( 'template' ), wc_get_core_supported_themes() ) ) { + $show_notices[] = 'theme_support'; + } + + update_option( 'woocommerce_admin_notices', $show_notices ); + } + + /** + * Show a notice + * @param string $name + */ + public static function add_notice( $name ) { + $notices = array_unique( array_merge( get_option( 'woocommerce_admin_notices', array() ), array( $name ) ) ); + update_option( 'woocommerce_admin_notices', $notices ); + } + + /** + * Remove a notice from being displayed + * @param string $name + */ + public static function remove_notice( $name ) { + $notices = array_diff( get_option( 'woocommerce_admin_notices', array() ), array( $name ) ); + update_option( 'woocommerce_admin_notices', $notices ); + } + + /** + * See if a notice is being shown + * @param string $name + * @return boolean + */ + public static function has_notice( $name ) { + return in_array( $name, get_option( 'woocommerce_admin_notices', array() ) ); + } + + /** + * Hide a notice if the GET variable is set. + */ + public function hide_notices() { + if ( isset( $_GET['wc-hide-notice'] ) ) { + $hide_notice = sanitize_text_field( $_GET['wc-hide-notice'] ); + self::remove_notice( $hide_notice ); + do_action( 'woocommerce_hide_' . $hide_notice . '_notice' ); + } + } + + /** + * When install is hidden, trigger a redirect + */ + public function hide_install_notice() { + // What's new redirect + if ( ! self::has_notice( 'update' ) ) { + delete_transient( '_wc_activation_redirect' ); + wp_redirect( admin_url( 'index.php?page=wc-about&wc-updated=true' ) ); + exit; + } + } + + /** + * Delete colors option + */ + public function hide_frontend_colors_notice() { + delete_option( 'woocommerce_frontend_css_colors' ); + } + + /** + * Hide translation upgrade message + */ + public function hide_translation_upgrade_notice() { + update_option( 'woocommerce_language_pack_version', array( WC_VERSION , get_locale() ) ); } /** * Add notices + styles if needed. */ public function add_notices() { - if ( get_option( '_wc_needs_update' ) == 1 || get_option( '_wc_needs_pages' ) == 1 ) { - wp_enqueue_style( 'woocommerce-activation', plugins_url( '/assets/css/activation.css', WC_PLUGIN_FILE ) ); - add_action( 'admin_notices', array( $this, 'install_notice' ) ); - } - $notices = get_option( 'woocommerce_admin_notices', array() ); - if ( ! empty( $_GET['hide_theme_support_notice'] ) ) { - $notices = array_diff( $notices, array( 'theme_support' ) ); - update_option( 'woocommerce_admin_notices', $notices ); - } - - if ( ! empty( $_GET['hide_template_files_notice'] ) ) { - $notices = array_diff( $notices, array( 'template_files' ) ); - update_option( 'woocommerce_admin_notices', $notices ); - } - - if ( ! empty( $_GET['hide_frontend_colors_notice'] ) ) { - delete_option( 'woocommerce_frontend_css_colors' ); - } - - if ( in_array( 'theme_support', $notices ) && ! current_theme_supports( 'woocommerce' ) ) { - $template = get_option( 'template' ); - - if ( ! in_array( $template, wc_get_core_supported_themes() ) ) { - wp_enqueue_style( 'woocommerce-activation', plugins_url( '/assets/css/activation.css', WC_PLUGIN_FILE ) ); - add_action( 'admin_notices', array( $this, 'theme_check_notice' ) ); - } - } - - if ( in_array( 'template_files', $notices ) ) { + foreach ( $notices as $notice ) { wp_enqueue_style( 'woocommerce-activation', plugins_url( '/assets/css/activation.css', WC_PLUGIN_FILE ) ); - add_action( 'admin_notices', array( $this, 'template_file_check_notice' ) ); - } - - if ( in_array( 'translation_upgrade', $notices ) ) { - wp_enqueue_style( 'woocommerce-activation', plugins_url( '/assets/css/activation.css', WC_PLUGIN_FILE ) ); - add_action( 'admin_notices', array( $this, 'translation_upgrade_notice' ) ); - } - - if ( $this->has_frontend_colors() ) { - add_action( 'admin_notices', array( $this, 'frontend_colors_notice' ) ); + add_action( 'admin_notices', array( $this, $this->notices[ $notice ] ) ); } } /** - * Show the install notices + * If we need to update, include a message with the update button + */ + public function update_notice() { + include( 'views/html-notice-update.php' ); + } + + /** + * If we have just installed, show a message with the install pages button */ public function install_notice() { - // If we need to update, include a message with the update button - if ( get_option( '_wc_needs_update' ) == 1 ) { - include( 'views/html-notice-update.php' ); - } - - // If we have just installed, show a message with the install pages button - elseif ( get_option( '_wc_needs_pages' ) == 1 ) { - include( 'views/html-notice-install.php' ); - } + include( 'views/html-notice-install.php' ); } /** @@ -121,10 +172,6 @@ class WC_Admin_Notices { * Show a notice highlighting bad template files */ public function template_file_check_notice() { - if ( isset( $_GET['page'] ) && 'wc-status' == $_GET['page'] ) { - return; - } - $core_templates = WC_Admin_Status::scan_template_files( WC()->plugin_path() . '/templates' ); $outdated = false; @@ -153,6 +200,8 @@ class WC_Admin_Notices { if ( $outdated ) { include( 'views/html-notice-template-check.php' ); + } else { + self::remove_notice( 'template_files' ); } } @@ -192,6 +241,4 @@ class WC_Admin_Notices { } } -endif; - -return new WC_Admin_Notices(); +new WC_Admin_Notices(); diff --git a/includes/admin/class-wc-admin-status.php b/includes/admin/class-wc-admin-status.php index d50083ccca2..6e852d85384 100644 --- a/includes/admin/class-wc-admin-status.php +++ b/includes/admin/class-wc-admin-status.php @@ -133,14 +133,6 @@ class WC_Admin_Status { echo '

' . __( 'Tax rates successfully deleted', 'woocommerce' ) . '

'; break; - case 'hide_translation_upgrade' : - update_option( 'woocommerce_language_pack_version', array( WC_VERSION , get_locale() ) ); - $notices = get_option( 'woocommerce_admin_notices', array() ); - $notices = array_diff( $notices, array( 'translation_upgrade' ) ); - update_option( 'woocommerce_admin_notices', $notices ); - - echo '

' . __( 'Translation update message hidden successfully!', 'woocommerce' ) . '

'; - break; default : $action = esc_attr( $_GET['action'] ); if ( isset( $tools[ $action ]['callback'] ) ) { diff --git a/includes/admin/class-wc-admin-welcome.php b/includes/admin/class-wc-admin-welcome.php index 327c356bacf..09e8179a1cb 100644 --- a/includes/admin/class-wc-admin-welcome.php +++ b/includes/admin/class-wc-admin-welcome.php @@ -426,7 +426,7 @@ class WC_Admin_Welcome { delete_transient( '_wc_activation_redirect' ); // Bail if we are waiting to install or update via the interface update/install links - if ( get_option( '_wc_needs_update' ) == 1 || get_option( '_wc_needs_pages' ) == 1 ) { + if ( WC_Admin_Notices::has_notice( 'install' ) || WC_Admin_Notices::has_notice( 'update' ) ) { return; } diff --git a/includes/admin/class-wc-admin.php b/includes/admin/class-wc-admin.php index 13c6c46a768..f679a92c2d9 100644 --- a/includes/admin/class-wc-admin.php +++ b/includes/admin/class-wc-admin.php @@ -43,21 +43,21 @@ class WC_Admin { // Classes we only need during non-ajax requests if ( ! is_ajax() ) { - include( 'class-wc-admin-menus.php' ); - include( 'class-wc-admin-welcome.php' ); - include( 'class-wc-admin-notices.php' ); - include( 'class-wc-admin-assets.php' ); - include( 'class-wc-admin-webhooks.php' ); + include_once( 'class-wc-admin-menus.php' ); + include_once( 'class-wc-admin-welcome.php' ); + include_once( 'class-wc-admin-notices.php' ); + include_once( 'class-wc-admin-assets.php' ); + include_once( 'class-wc-admin-webhooks.php' ); // Help if ( apply_filters( 'woocommerce_enable_admin_help_tab', true ) ) { - include( 'class-wc-admin-help.php' ); + include_once( 'class-wc-admin-help.php' ); } } // Importers if ( defined( 'WP_LOAD_IMPORTERS' ) ) { - include( 'class-wc-admin-importers.php' ); + include_once( 'class-wc-admin-importers.php' ); } } diff --git a/includes/admin/views/html-notice-frontend-colors.php b/includes/admin/views/html-notice-frontend-colors.php index f4bab6c935a..796c05d2e4d 100644 --- a/includes/admin/views/html-notice-frontend-colors.php +++ b/includes/admin/views/html-notice-frontend-colors.php @@ -19,5 +19,5 @@ if ( current_user_can( 'install_plugins' ) ) {

The Frontend Styles options is deprecated – It is recommended that you install the replacement WooCommerce Colors plugin from WordPress.org If you want to continue editing the colors of your store.', 'woocommerce' ); ?>

-

+

diff --git a/includes/admin/views/html-notice-install.php b/includes/admin/views/html-notice-install.php index 99849ceb5df..472d9b6f1ac 100644 --- a/includes/admin/views/html-notice-install.php +++ b/includes/admin/views/html-notice-install.php @@ -11,5 +11,5 @@ if ( ! defined( 'ABSPATH' ) ) {

Welcome to WooCommerce – You\'re almost ready to start selling :)', 'woocommerce' ); ?>

-

+

\ No newline at end of file diff --git a/includes/admin/views/html-notice-template-check.php b/includes/admin/views/html-notice-template-check.php index bb2378928dc..5612888b388 100644 --- a/includes/admin/views/html-notice-template-check.php +++ b/includes/admin/views/html-notice-template-check.php @@ -11,5 +11,5 @@ if ( ! defined( 'ABSPATH' ) ) {

Your theme has bundled outdated copies of WooCommerce template files – if you encounter functionality issues on the frontend this could be the reason. Ensure you update or remove them (in general we recommend only bundling the template files you actually need to customize). See the system report for full details.', 'woocommerce' ); ?>

-

+

diff --git a/includes/admin/views/html-notice-theme-support.php b/includes/admin/views/html-notice-theme-support.php index f1595f3e41a..e0d1cb1323e 100644 --- a/includes/admin/views/html-notice-theme-support.php +++ b/includes/admin/views/html-notice-theme-support.php @@ -14,6 +14,6 @@ if ( ! defined( 'ABSPATH' ) ) {

- +

diff --git a/includes/admin/views/html-notice-translation-upgrade.php b/includes/admin/views/html-notice-translation-upgrade.php index 789dd9508a5..18db243ac00 100644 --- a/includes/admin/views/html-notice-translation-upgrade.php +++ b/includes/admin/views/html-notice-translation-upgrade.php @@ -6,11 +6,6 @@ if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly } - -if ( isset( $_GET['action'] ) && 'hide_translation_upgrade' == $_GET['action'] ) { - return; -} - ?>
@@ -23,6 +18,6 @@ if ( isset( $_GET['action'] ) && 'hide_translation_upgrade' == $_GET['action'] ) - +

diff --git a/includes/class-wc-install.php b/includes/class-wc-install.php index 04f0a0702f0..0af6b2bc2cb 100644 --- a/includes/class-wc-install.php +++ b/includes/class-wc-install.php @@ -51,23 +51,14 @@ class WC_Install { self::create_pages(); // We no longer need to install pages - delete_option( '_wc_needs_pages' ); - delete_transient( '_wc_activation_redirect' ); + WC_Admin_Notices::remove_notice( 'install' ); // What's new redirect - wp_redirect( admin_url( 'index.php?page=wc-about&wc-installed=true' ) ); - exit; - - // Skip button - } elseif ( ! empty( $_GET['skip_install_woocommerce_pages'] ) ) { - - // We no longer need to install pages - delete_option( '_wc_needs_pages' ); - delete_transient( '_wc_activation_redirect' ); - - // What's new redirect - wp_redirect( admin_url( 'index.php?page=wc-about' ) ); - exit; + if ( ! WC_Admin_Notices::has_notice( 'update' ) ) { + delete_transient( '_wc_activation_redirect' ); + wp_redirect( admin_url( 'index.php?page=wc-about&wc-updated=true' ) ); + exit; + } // Update button } elseif ( ! empty( $_GET['do_update_woocommerce'] ) ) { @@ -75,13 +66,14 @@ class WC_Install { self::update(); // Update complete - delete_option( '_wc_needs_pages' ); - delete_option( '_wc_needs_update' ); - delete_transient( '_wc_activation_redirect' ); + WC_Admin_Notices::remove_notice( 'update' ); // What's new redirect - wp_redirect( admin_url( 'index.php?page=wc-about&wc-updated=true' ) ); - exit; + if ( ! WC_Admin_Notices::has_notice( 'install' ) ) { + delete_transient( '_wc_activation_redirect' ); + wp_redirect( admin_url( 'index.php?page=wc-about&wc-updated=true' ) ); + exit; + } } } @@ -110,7 +102,7 @@ class WC_Install { $current_db_version = get_option( 'woocommerce_db_version', null ); if ( version_compare( $current_db_version, '2.2.0', '<' ) && null !== $current_db_version ) { - update_option( '_wc_needs_update', 1 ); + WC_Admin_Notices::add_notice( 'update' ); } else { update_option( 'woocommerce_db_version', WC()->version ); } @@ -120,7 +112,7 @@ class WC_Install { // Check if pages are needed if ( wc_get_page_id( 'shop' ) < 1 ) { - update_option( '_wc_needs_pages', 1 ); + WC_Admin_Notices::add_notice( 'install' ); } // Flush rules after install diff --git a/includes/class-wc-language-pack-upgrader.php b/includes/class-wc-language-pack-upgrader.php index 8c1df7e7255..d8fb7a824d3 100644 --- a/includes/class-wc-language-pack-upgrader.php +++ b/includes/class-wc-language-pack-upgrader.php @@ -94,12 +94,7 @@ class WC_Language_Pack_Upgrader { * @return void */ public function configure_woocommerce_upgrade_notice() { - $notices = get_option( 'woocommerce_admin_notices', array() ); - if ( false === array_search( 'translation_upgrade', $notices ) ) { - $notices[] = 'translation_upgrade'; - - update_option( 'woocommerce_admin_notices', $notices ); - } + WC_Admin_Notices::add_notice( 'translation_upgrade' ); } /**