Optimise the admin notices class.

@claudiosmweb
This commit is contained in:
Mike Jolley 2015-01-20 15:23:34 +00:00
parent 91de767442
commit 2d8f021cc7
11 changed files with 137 additions and 116 deletions

View File

@ -5,26 +5,41 @@
* @author WooThemes * @author WooThemes
* @category Admin * @category Admin
* @package WooCommerce/Admin * @package WooCommerce/Admin
* @version 2.1.0 * @version 2.3.0
*/ */
if ( ! defined( 'ABSPATH' ) ) { if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly exit;
} }
if ( ! class_exists( 'WC_Admin_Notices' ) ) :
/** /**
* WC_Admin_Notices Class * WC_Admin_Notices Class
*/ */
class WC_Admin_Notices { 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() { public function __construct() {
add_action( 'switch_theme', array( $this, 'reset_admin_notices' ) ); add_action( 'switch_theme', array( $this, 'reset_admin_notices' ) );
add_action( 'woocommerce_updated', 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' ) ); add_action( 'admin_print_styles', array( $this, 'add_notices' ) );
} }
@ -32,72 +47,108 @@ class WC_Admin_Notices {
* Reset notices for themes when switched or a new version of WC is installed * Reset notices for themes when switched or a new version of WC is installed
*/ */
public function reset_admin_notices() { 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. * Add notices + styles if needed.
*/ */
public function add_notices() { 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() ); $notices = get_option( 'woocommerce_admin_notices', array() );
if ( ! empty( $_GET['hide_theme_support_notice'] ) ) { foreach ( $notices as $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 ) ); wp_enqueue_style( 'woocommerce-activation', plugins_url( '/assets/css/activation.css', WC_PLUGIN_FILE ) );
add_action( 'admin_notices', array( $this, 'theme_check_notice' ) ); add_action( 'admin_notices', array( $this, $this->notices[ $notice ] ) );
}
}
if ( in_array( 'template_files', $notices ) ) {
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' ) );
} }
} }
/** /**
* Show the install notices * If we need to update, include a message with the update button
*/ */
public function install_notice() { public function update_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' ); 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 ) { * If we have just installed, show a message with the install pages button
*/
public function install_notice() {
include( 'views/html-notice-install.php' ); include( 'views/html-notice-install.php' );
} }
}
/** /**
* Show the Theme Check notice * Show the Theme Check notice
@ -121,10 +172,6 @@ class WC_Admin_Notices {
* Show a notice highlighting bad template files * Show a notice highlighting bad template files
*/ */
public function template_file_check_notice() { 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' ); $core_templates = WC_Admin_Status::scan_template_files( WC()->plugin_path() . '/templates' );
$outdated = false; $outdated = false;
@ -153,6 +200,8 @@ class WC_Admin_Notices {
if ( $outdated ) { if ( $outdated ) {
include( 'views/html-notice-template-check.php' ); include( 'views/html-notice-template-check.php' );
} else {
self::remove_notice( 'template_files' );
} }
} }
@ -192,6 +241,4 @@ class WC_Admin_Notices {
} }
} }
endif; new WC_Admin_Notices();
return new WC_Admin_Notices();

View File

@ -133,14 +133,6 @@ class WC_Admin_Status {
echo '<div class="updated"><p>' . __( 'Tax rates successfully deleted', 'woocommerce' ) . '</p></div>'; echo '<div class="updated"><p>' . __( 'Tax rates successfully deleted', 'woocommerce' ) . '</p></div>';
break; 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 '<div class="updated"><p>' . __( 'Translation update message hidden successfully!', 'woocommerce' ) . '</p></div>';
break;
default : default :
$action = esc_attr( $_GET['action'] ); $action = esc_attr( $_GET['action'] );
if ( isset( $tools[ $action ]['callback'] ) ) { if ( isset( $tools[ $action ]['callback'] ) ) {

View File

@ -426,7 +426,7 @@ class WC_Admin_Welcome {
delete_transient( '_wc_activation_redirect' ); delete_transient( '_wc_activation_redirect' );
// Bail if we are waiting to install or update via the interface update/install links // 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; return;
} }

View File

@ -43,21 +43,21 @@ class WC_Admin {
// Classes we only need during non-ajax requests // Classes we only need during non-ajax requests
if ( ! is_ajax() ) { if ( ! is_ajax() ) {
include( 'class-wc-admin-menus.php' ); include_once( 'class-wc-admin-menus.php' );
include( 'class-wc-admin-welcome.php' ); include_once( 'class-wc-admin-welcome.php' );
include( 'class-wc-admin-notices.php' ); include_once( 'class-wc-admin-notices.php' );
include( 'class-wc-admin-assets.php' ); include_once( 'class-wc-admin-assets.php' );
include( 'class-wc-admin-webhooks.php' ); include_once( 'class-wc-admin-webhooks.php' );
// Help // Help
if ( apply_filters( 'woocommerce_enable_admin_help_tab', true ) ) { if ( apply_filters( 'woocommerce_enable_admin_help_tab', true ) ) {
include( 'class-wc-admin-help.php' ); include_once( 'class-wc-admin-help.php' );
} }
} }
// Importers // Importers
if ( defined( 'WP_LOAD_IMPORTERS' ) ) { if ( defined( 'WP_LOAD_IMPORTERS' ) ) {
include( 'class-wc-admin-importers.php' ); include_once( 'class-wc-admin-importers.php' );
} }
} }

View File

@ -19,5 +19,5 @@ if ( current_user_can( 'install_plugins' ) ) {
<div id="message" class="updated woocommerce-message wc-connect"> <div id="message" class="updated woocommerce-message wc-connect">
<p><?php _e( '<strong>The Frontend Styles options is deprecated</strong> &#8211; 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' ); ?></p> <p><?php _e( '<strong>The Frontend Styles options is deprecated</strong> &#8211; 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' ); ?></p>
<p class="submit"><a href="<?php echo esc_url( $url ); ?>" class="wc-update-now button-primary"><?php _e( 'Install the new WooCommerce Colors plugin', 'woocommerce' ); ?></a> <a class="skip button" href="<?php echo add_query_arg( 'hide_frontend_colors_notice', 'true' ); ?>"><?php _e( 'Hide this notice', 'woocommerce' ); ?></a></p> <p class="submit"><a href="<?php echo esc_url( $url ); ?>" class="wc-update-now button-primary"><?php _e( 'Install the new WooCommerce Colors plugin', 'woocommerce' ); ?></a> <a class="skip button" href="<?php echo add_query_arg( 'wc-hide-notice', 'frontend_colors' ); ?>"><?php _e( 'Hide this notice', 'woocommerce' ); ?></a></p>
</div> </div>

View File

@ -11,5 +11,5 @@ if ( ! defined( 'ABSPATH' ) ) {
<div id="message" class="updated woocommerce-message wc-connect"> <div id="message" class="updated woocommerce-message wc-connect">
<p><?php _e( '<strong>Welcome to WooCommerce</strong> &#8211; You\'re almost ready to start selling :)', 'woocommerce' ); ?></p> <p><?php _e( '<strong>Welcome to WooCommerce</strong> &#8211; You\'re almost ready to start selling :)', 'woocommerce' ); ?></p>
<p class="submit"><a href="<?php echo add_query_arg( 'install_woocommerce_pages', 'true', admin_url( 'admin.php?page=wc-settings' ) ); ?>" class="button-primary"><?php _e( 'Install WooCommerce Pages', 'woocommerce' ); ?></a> <a class="skip button" href="<?php echo add_query_arg( 'skip_install_woocommerce_pages', 'true', admin_url( 'admin.php?page=wc-settings' ) ); ?>"><?php _e( 'Skip setup', 'woocommerce' ); ?></a></p> <p class="submit"><a href="<?php echo add_query_arg( 'install_woocommerce_pages', 'true', admin_url( 'admin.php?page=wc-settings' ) ); ?>" class="button-primary"><?php _e( 'Install WooCommerce Pages', 'woocommerce' ); ?></a> <a class="skip button" href="<?php echo esc_url( add_query_arg( 'wc-hide-notice', 'install' ) ); ?>"><?php _e( 'Skip setup', 'woocommerce' ); ?></a></p>
</div> </div>

View File

@ -11,5 +11,5 @@ if ( ! defined( 'ABSPATH' ) ) {
<div id="message" class="updated woocommerce-message wc-connect"> <div id="message" class="updated woocommerce-message wc-connect">
<p><?php _e( '<strong>Your theme has bundled outdated copies of WooCommerce template files</strong> &#8211; 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' ); ?></p> <p><?php _e( '<strong>Your theme has bundled outdated copies of WooCommerce template files</strong> &#8211; 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' ); ?></p>
<p class="submit"><a class="button-primary" href="<?php echo esc_url( admin_url( 'admin.php?page=wc-status' ) ); ?>"><?php _e( 'System Status', 'woocommerce' ); ?></a> <a class="skip button-primary" href="<?php echo esc_url( add_query_arg( 'hide_template_files_notice', 'true' ) ); ?>"><?php _e( 'Hide this notice', 'woocommerce' ); ?></a></p> <p class="submit"><a class="button-primary" href="<?php echo esc_url( admin_url( 'admin.php?page=wc-status' ) ); ?>"><?php _e( 'System Status', 'woocommerce' ); ?></a> <a class="skip button-primary" href="<?php echo esc_url( add_query_arg( 'wc-hide-notice', 'template_files' ) ); ?>"><?php _e( 'Hide this notice', 'woocommerce' ); ?></a></p>
</div> </div>

View File

@ -14,6 +14,6 @@ if ( ! defined( 'ABSPATH' ) ) {
<p class="submit"> <p class="submit">
<a href="http://woothemes.com/storefront" class="button-primary" target="_blank"><?php _e( 'Find out more about Storefront', 'woocommerce' ); ?></a> <a href="http://woothemes.com/storefront" class="button-primary" target="_blank"><?php _e( 'Find out more about Storefront', 'woocommerce' ); ?></a>
<a href="<?php echo esc_url( apply_filters( 'woocommerce_docs_url', 'http://docs.woothemes.com/document/third-party-custom-theme-compatibility/', 'theme-compatibility' ) ); ?>" class="button"><?php _e( 'Theme integration guide', 'woocommerce' ); ?></a> <a href="<?php echo esc_url( apply_filters( 'woocommerce_docs_url', 'http://docs.woothemes.com/document/third-party-custom-theme-compatibility/', 'theme-compatibility' ) ); ?>" class="button"><?php _e( 'Theme integration guide', 'woocommerce' ); ?></a>
<a class="skip button" href="<?php echo esc_url( add_query_arg( 'hide_theme_support_notice', 'true' ) ); ?>"><?php _e( 'Hide this notice', 'woocommerce' ); ?></a> <a class="skip button" href="<?php echo esc_url( add_query_arg( 'wc-hide-notice', 'theme_support' ) ); ?>"><?php _e( 'Hide this notice', 'woocommerce' ); ?></a>
</p> </p>
</div> </div>

View File

@ -6,11 +6,6 @@
if ( ! defined( 'ABSPATH' ) ) { if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly exit; // Exit if accessed directly
} }
if ( isset( $_GET['action'] ) && 'hide_translation_upgrade' == $_GET['action'] ) {
return;
}
?> ?>
<div id="message" class="updated woocommerce-message wc-connect"> <div id="message" class="updated woocommerce-message wc-connect">
@ -23,6 +18,6 @@ if ( isset( $_GET['action'] ) && 'hide_translation_upgrade' == $_GET['action'] )
<a href="<?php echo wp_nonce_url( add_query_arg( array( 'action' => 'do-translation-upgrade' ), admin_url( 'update-core.php' ) ), 'upgrade-translations' ); ?>" class="button-primary"><?php _e( 'Update Translation', 'woocommerce' ); ?></a> <a href="<?php echo wp_nonce_url( add_query_arg( array( 'action' => 'do-translation-upgrade' ), admin_url( 'update-core.php' ) ), 'upgrade-translations' ); ?>" class="button-primary"><?php _e( 'Update Translation', 'woocommerce' ); ?></a>
<a href="<?php echo wp_nonce_url( admin_url( 'admin.php?page=wc-status&tab=tools&action=translation_upgrade' ), 'debug_action' ); ?>" class="button-primary"><?php _e( 'Force Update Translation', 'woocommerce' ); ?></a> <a href="<?php echo wp_nonce_url( admin_url( 'admin.php?page=wc-status&tab=tools&action=translation_upgrade' ), 'debug_action' ); ?>" class="button-primary"><?php _e( 'Force Update Translation', 'woocommerce' ); ?></a>
<?php endif; ?> <?php endif; ?>
<a href="<?php echo wp_nonce_url( admin_url( 'admin.php?page=wc-status&tab=tools&action=hide_translation_upgrade' ), 'debug_action' ); ?>" class="button"><?php _e( 'Hide This Message', 'woocommerce' ); ?></a> <a href="<?php echo esc_url( add_query_arg( 'wc-hide-notice', 'translation_upgrade' ) ); ?>" class="button"><?php _e( 'Hide This Message', 'woocommerce' ); ?></a>
</p> </p>
</div> </div>

View File

@ -51,23 +51,14 @@ class WC_Install {
self::create_pages(); self::create_pages();
// We no longer need to install pages // We no longer need to install pages
delete_option( '_wc_needs_pages' ); WC_Admin_Notices::remove_notice( 'install' );
delete_transient( '_wc_activation_redirect' );
// What's new redirect // What's new redirect
wp_redirect( admin_url( 'index.php?page=wc-about&wc-installed=true' ) ); if ( ! WC_Admin_Notices::has_notice( 'update' ) ) {
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' ); delete_transient( '_wc_activation_redirect' );
wp_redirect( admin_url( 'index.php?page=wc-about&wc-updated=true' ) );
// What's new redirect
wp_redirect( admin_url( 'index.php?page=wc-about' ) );
exit; exit;
}
// Update button // Update button
} elseif ( ! empty( $_GET['do_update_woocommerce'] ) ) { } elseif ( ! empty( $_GET['do_update_woocommerce'] ) ) {
@ -75,15 +66,16 @@ class WC_Install {
self::update(); self::update();
// Update complete // Update complete
delete_option( '_wc_needs_pages' ); WC_Admin_Notices::remove_notice( 'update' );
delete_option( '_wc_needs_update' );
delete_transient( '_wc_activation_redirect' );
// What's new redirect // What's new redirect
if ( ! WC_Admin_Notices::has_notice( 'install' ) ) {
delete_transient( '_wc_activation_redirect' );
wp_redirect( admin_url( 'index.php?page=wc-about&wc-updated=true' ) ); wp_redirect( admin_url( 'index.php?page=wc-about&wc-updated=true' ) );
exit; exit;
} }
} }
}
/** /**
* Install WC * Install WC
@ -110,7 +102,7 @@ class WC_Install {
$current_db_version = get_option( 'woocommerce_db_version', null ); $current_db_version = get_option( 'woocommerce_db_version', null );
if ( version_compare( $current_db_version, '2.2.0', '<' ) && null !== $current_db_version ) { 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 { } else {
update_option( 'woocommerce_db_version', WC()->version ); update_option( 'woocommerce_db_version', WC()->version );
} }
@ -120,7 +112,7 @@ class WC_Install {
// Check if pages are needed // Check if pages are needed
if ( wc_get_page_id( 'shop' ) < 1 ) { if ( wc_get_page_id( 'shop' ) < 1 ) {
update_option( '_wc_needs_pages', 1 ); WC_Admin_Notices::add_notice( 'install' );
} }
// Flush rules after install // Flush rules after install

View File

@ -94,12 +94,7 @@ class WC_Language_Pack_Upgrader {
* @return void * @return void
*/ */
public function configure_woocommerce_upgrade_notice() { public function configure_woocommerce_upgrade_notice() {
$notices = get_option( 'woocommerce_admin_notices', array() ); WC_Admin_Notices::add_notice( 'translation_upgrade' );
if ( false === array_search( 'translation_upgrade', $notices ) ) {
$notices[] = 'translation_upgrade';
update_option( 'woocommerce_admin_notices', $notices );
}
} }
/** /**