2013-07-24 16:01:36 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-11-03 13:53:50 +00:00
|
|
|
* Display notices in admin
|
2013-07-24 16:01:36 +00:00
|
|
|
*
|
2014-11-30 06:52:32 +00:00
|
|
|
* @author WooThemes
|
|
|
|
* @category Admin
|
|
|
|
* @package WooCommerce/Admin
|
2015-01-20 15:23:34 +00:00
|
|
|
* @version 2.3.0
|
2013-07-24 16:01:36 +00:00
|
|
|
*/
|
|
|
|
|
2014-09-20 19:52:30 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
2015-01-20 15:23:34 +00:00
|
|
|
exit;
|
2014-09-20 19:52:30 +00:00
|
|
|
}
|
2013-07-24 16:01:36 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* WC_Admin_Notices Class.
|
2013-07-24 16:01:36 +00:00
|
|
|
*/
|
|
|
|
class WC_Admin_Notices {
|
|
|
|
|
2016-06-06 17:20:10 +00:00
|
|
|
/**
|
|
|
|
* Stores notices.
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $notices = array();
|
|
|
|
|
2013-07-24 16:01:36 +00:00
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Array of notices - name => callback.
|
2015-01-20 15:23:34 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2016-05-11 13:22:06 +00:00
|
|
|
private static $core_notices = array(
|
2016-04-01 16:33:00 +00:00
|
|
|
'install' => 'install_notice',
|
|
|
|
'update' => 'update_notice',
|
|
|
|
'template_files' => 'template_file_check_notice',
|
|
|
|
'theme_support' => 'theme_check_notice',
|
|
|
|
'legacy_shipping' => 'legacy_shipping_notice',
|
|
|
|
'no_shipping_methods' => 'no_shipping_methods_notice',
|
2016-04-19 12:14:13 +00:00
|
|
|
'simplify_commerce' => 'simplify_commerce_notice',
|
2015-01-20 15:23:34 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Constructor.
|
2013-07-24 16:01:36 +00:00
|
|
|
*/
|
2016-05-11 13:22:06 +00:00
|
|
|
public static function init() {
|
|
|
|
self::$notices = get_option( 'woocommerce_admin_notices', array() );
|
|
|
|
|
|
|
|
add_action( 'switch_theme', array( __CLASS__, 'reset_admin_notices' ) );
|
|
|
|
add_action( 'woocommerce_installed', array( __CLASS__, 'reset_admin_notices' ) );
|
|
|
|
add_action( 'wp_loaded', array( __CLASS__, 'hide_notices' ) );
|
|
|
|
add_action( 'shutdown', array( __CLASS__, 'store_notices' ) );
|
2015-04-29 11:08:15 +00:00
|
|
|
|
|
|
|
if ( current_user_can( 'manage_woocommerce' ) ) {
|
2016-05-11 13:22:06 +00:00
|
|
|
add_action( 'admin_print_styles', array( __CLASS__, 'add_notices' ) );
|
2015-04-29 11:08:15 +00:00
|
|
|
}
|
2015-04-29 09:47:57 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 13:22:06 +00:00
|
|
|
/**
|
|
|
|
* Store notices to DB
|
|
|
|
*/
|
|
|
|
public static function store_notices() {
|
|
|
|
update_option( 'woocommerce_admin_notices', self::get_notices() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get notices
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function get_notices() {
|
|
|
|
return self::$notices;
|
|
|
|
}
|
|
|
|
|
2015-04-29 09:47:57 +00:00
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Remove all notices.
|
2015-04-29 09:47:57 +00:00
|
|
|
*/
|
|
|
|
public static function remove_all_notices() {
|
2016-05-11 13:22:06 +00:00
|
|
|
self::$notices = array();
|
2013-07-24 16:01:36 +00:00
|
|
|
}
|
|
|
|
|
2014-02-11 13:33:56 +00:00
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Reset notices for themes when switched or a new version of WC is installed.
|
2014-02-11 13:33:56 +00:00
|
|
|
*/
|
2016-05-11 13:22:06 +00:00
|
|
|
public static function reset_admin_notices() {
|
2015-01-20 15:23:34 +00:00
|
|
|
if ( ! current_theme_supports( 'woocommerce' ) && ! in_array( get_option( 'template' ), wc_get_core_supported_themes() ) ) {
|
2015-02-02 14:40:08 +00:00
|
|
|
self::add_notice( 'theme_support' );
|
2015-01-20 15:23:34 +00:00
|
|
|
}
|
2016-04-19 12:14:13 +00:00
|
|
|
|
|
|
|
$simplify_options = get_option( 'woocommerce_simplify_commerce_settings', array() );
|
2016-06-03 11:28:25 +00:00
|
|
|
$location = wc_get_base_location();
|
2016-04-19 12:14:13 +00:00
|
|
|
|
2016-06-03 11:28:25 +00:00
|
|
|
if ( ! class_exists( 'WC_Gateway_Simplify_Commerce_Loader' ) && ! empty( $simplify_options['enabled'] ) && 'yes' === $simplify_options['enabled'] && in_array( $location['country'], apply_filters( 'woocommerce_gateway_simplify_commerce_supported_countries', array( 'US', 'IE' ) ) ) ) {
|
2016-04-19 12:14:13 +00:00
|
|
|
WC_Admin_Notices::add_notice( 'simplify_commerce' );
|
|
|
|
}
|
|
|
|
|
2015-02-02 14:40:08 +00:00
|
|
|
self::add_notice( 'template_files' );
|
2014-02-11 13:33:56 +00:00
|
|
|
}
|
|
|
|
|
2013-07-24 16:01:36 +00:00
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Show a notice.
|
2016-04-01 16:30:04 +00:00
|
|
|
* @param string $name
|
2013-07-24 16:01:36 +00:00
|
|
|
*/
|
2015-01-20 15:23:34 +00:00
|
|
|
public static function add_notice( $name ) {
|
2016-05-11 13:22:06 +00:00
|
|
|
self::$notices = array_unique( array_merge( self::get_notices(), array( $name ) ) );
|
2015-01-20 15:23:34 +00:00
|
|
|
}
|
2013-07-24 16:01:36 +00:00
|
|
|
|
2015-01-20 15:23:34 +00:00
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Remove a notice from being displayed.
|
2015-01-20 15:23:34 +00:00
|
|
|
* @param string $name
|
|
|
|
*/
|
|
|
|
public static function remove_notice( $name ) {
|
2016-05-11 13:22:06 +00:00
|
|
|
self::$notices = array_diff( self::get_notices(), array( $name ) );
|
2016-04-01 16:30:04 +00:00
|
|
|
delete_option( 'woocommerce_admin_notice_' . $name );
|
2015-01-20 15:23:34 +00:00
|
|
|
}
|
2013-07-24 16:01:36 +00:00
|
|
|
|
2015-01-20 15:23:34 +00:00
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* See if a notice is being shown.
|
2015-01-20 15:23:34 +00:00
|
|
|
* @param string $name
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function has_notice( $name ) {
|
2016-05-11 13:22:06 +00:00
|
|
|
return in_array( $name, self::get_notices() );
|
2015-01-20 15:23:34 +00:00
|
|
|
}
|
2013-07-24 16:01:36 +00:00
|
|
|
|
2015-01-20 15:23:34 +00:00
|
|
|
/**
|
|
|
|
* Hide a notice if the GET variable is set.
|
|
|
|
*/
|
2016-05-11 13:22:06 +00:00
|
|
|
public static function hide_notices() {
|
2015-05-21 18:03:40 +00:00
|
|
|
if ( isset( $_GET['wc-hide-notice'] ) && isset( $_GET['_wc_notice_nonce'] ) ) {
|
|
|
|
if ( ! wp_verify_nonce( $_GET['_wc_notice_nonce'], 'woocommerce_hide_notices_nonce' ) ) {
|
2015-05-20 18:14:53 +00:00
|
|
|
wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! current_user_can( 'manage_woocommerce' ) ) {
|
|
|
|
wp_die( __( 'Cheatin’ huh?', 'woocommerce' ) );
|
|
|
|
}
|
|
|
|
|
2015-01-20 15:23:34 +00:00
|
|
|
$hide_notice = sanitize_text_field( $_GET['wc-hide-notice'] );
|
|
|
|
self::remove_notice( $hide_notice );
|
|
|
|
do_action( 'woocommerce_hide_' . $hide_notice . '_notice' );
|
2014-02-11 13:33:56 +00:00
|
|
|
}
|
2015-01-20 15:23:34 +00:00
|
|
|
}
|
2013-07-24 16:01:36 +00:00
|
|
|
|
2015-01-20 15:23:34 +00:00
|
|
|
/**
|
|
|
|
* Add notices + styles if needed.
|
|
|
|
*/
|
2016-05-11 13:22:06 +00:00
|
|
|
public static function add_notices() {
|
|
|
|
$notices = self::get_notices();
|
2014-06-04 16:01:38 +00:00
|
|
|
|
2016-06-06 16:24:31 +00:00
|
|
|
if ( ! empty( $notices ) ) {
|
2016-09-02 01:51:31 +00:00
|
|
|
wp_enqueue_style( 'woocommerce-activation', plugins_url( '/assets/css/activation.css', WC_PLUGIN_FILE ) );
|
2015-04-29 11:21:48 +00:00
|
|
|
foreach ( $notices as $notice ) {
|
2016-05-11 13:22:06 +00:00
|
|
|
if ( ! empty( self::$core_notices[ $notice ] ) && apply_filters( 'woocommerce_show_admin_notice', true, $notice ) ) {
|
|
|
|
add_action( 'admin_notices', array( __CLASS__, self::$core_notices[ $notice ] ) );
|
2016-04-01 16:30:04 +00:00
|
|
|
} else {
|
2016-05-11 13:22:06 +00:00
|
|
|
add_action( 'admin_notices', array( __CLASS__, 'output_custom_notices' ) );
|
2016-04-01 16:30:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a custom notice.
|
|
|
|
* @param string $name
|
|
|
|
* @param string $notice_html
|
|
|
|
*/
|
|
|
|
public static function add_custom_notice( $name, $notice_html ) {
|
|
|
|
self::add_notice( $name );
|
|
|
|
update_option( 'woocommerce_admin_notice_' . $name, wp_kses_post( $notice_html ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output any stored custom notices.
|
|
|
|
*/
|
2016-05-11 13:22:06 +00:00
|
|
|
public static function output_custom_notices() {
|
|
|
|
$notices = self::get_notices();
|
|
|
|
|
2016-06-06 16:24:31 +00:00
|
|
|
if ( ! empty( $notices ) ) {
|
2016-04-01 16:30:04 +00:00
|
|
|
foreach ( $notices as $notice ) {
|
2016-05-11 13:22:06 +00:00
|
|
|
if ( empty( self::$core_notices[ $notice ] ) ) {
|
2016-04-01 16:30:04 +00:00
|
|
|
$notice_html = get_option( 'woocommerce_admin_notice_' . $notice );
|
|
|
|
|
|
|
|
if ( $notice_html ) {
|
|
|
|
include( 'views/html-notice-custom.php' );
|
|
|
|
}
|
2015-04-29 11:21:48 +00:00
|
|
|
}
|
2015-04-29 09:47:57 +00:00
|
|
|
}
|
2014-06-04 16:01:38 +00:00
|
|
|
}
|
2015-01-20 15:23:34 +00:00
|
|
|
}
|
2015-01-14 12:17:03 +00:00
|
|
|
|
2015-01-20 15:23:34 +00:00
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* If we need to update, include a message with the update button.
|
2015-01-20 15:23:34 +00:00
|
|
|
*/
|
2016-05-11 13:22:06 +00:00
|
|
|
public static function update_notice() {
|
|
|
|
if ( version_compare( get_option( 'woocommerce_db_version' ), WC_VERSION, '<' ) ) {
|
2016-06-13 11:44:16 +00:00
|
|
|
$updater = new WC_Background_Updater();
|
2016-06-13 17:15:07 +00:00
|
|
|
if ( $updater->is_updating() || ! empty( $_GET['do_update_woocommerce'] ) ) {
|
2016-06-13 11:44:16 +00:00
|
|
|
include( 'views/html-notice-updating.php' );
|
2016-06-13 17:15:07 +00:00
|
|
|
} else {
|
|
|
|
include( 'views/html-notice-update.php' );
|
2016-06-13 11:44:16 +00:00
|
|
|
}
|
2016-05-11 13:22:06 +00:00
|
|
|
} else {
|
|
|
|
include( 'views/html-notice-updated.php' );
|
|
|
|
}
|
2016-05-11 11:43:35 +00:00
|
|
|
}
|
|
|
|
|
2013-07-24 16:01:36 +00:00
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* If we have just installed, show a message with the install pages button.
|
2013-07-24 16:01:36 +00:00
|
|
|
*/
|
2016-05-11 13:22:06 +00:00
|
|
|
public static function install_notice() {
|
2015-01-20 15:23:34 +00:00
|
|
|
include( 'views/html-notice-install.php' );
|
2013-07-24 16:01:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Show the Theme Check notice.
|
2013-07-24 16:01:36 +00:00
|
|
|
*/
|
2016-05-11 13:22:06 +00:00
|
|
|
public static function theme_check_notice() {
|
2015-05-22 16:52:18 +00:00
|
|
|
if ( ! current_theme_supports( 'woocommerce' ) && ! in_array( get_option( 'template' ), wc_get_core_supported_themes() ) ) {
|
2015-01-23 16:15:31 +00:00
|
|
|
include( 'views/html-notice-theme-support.php' );
|
2015-04-29 11:08:15 +00:00
|
|
|
} else {
|
|
|
|
self::remove_notice( 'theme_support' );
|
2015-01-23 16:15:31 +00:00
|
|
|
}
|
2013-07-24 16:01:36 +00:00
|
|
|
}
|
2014-02-11 13:33:56 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Show a notice highlighting bad template files.
|
2014-02-11 13:33:56 +00:00
|
|
|
*/
|
2016-05-11 13:22:06 +00:00
|
|
|
public static function template_file_check_notice() {
|
2014-06-04 10:16:19 +00:00
|
|
|
$core_templates = WC_Admin_Status::scan_template_files( WC()->plugin_path() . '/templates' );
|
2014-02-11 13:33:56 +00:00
|
|
|
$outdated = false;
|
|
|
|
|
|
|
|
foreach ( $core_templates as $file ) {
|
2015-04-01 13:33:56 +00:00
|
|
|
|
2014-02-11 13:33:56 +00:00
|
|
|
$theme_file = false;
|
|
|
|
if ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
|
|
|
|
$theme_file = get_stylesheet_directory() . '/' . $file;
|
|
|
|
} elseif ( file_exists( get_stylesheet_directory() . '/woocommerce/' . $file ) ) {
|
|
|
|
$theme_file = get_stylesheet_directory() . '/woocommerce/' . $file;
|
|
|
|
} elseif ( file_exists( get_template_directory() . '/' . $file ) ) {
|
|
|
|
$theme_file = get_template_directory() . '/' . $file;
|
2016-08-27 04:23:02 +00:00
|
|
|
} elseif ( file_exists( get_template_directory() . '/woocommerce/' . $file ) ) {
|
2014-02-11 13:33:56 +00:00
|
|
|
$theme_file = get_template_directory() . '/woocommerce/' . $file;
|
|
|
|
}
|
|
|
|
|
2016-09-07 22:32:24 +00:00
|
|
|
if ( false !== $theme_file ) {
|
2014-06-13 15:17:23 +00:00
|
|
|
$core_version = WC_Admin_Status::get_file_version( WC()->plugin_path() . '/templates/' . $file );
|
|
|
|
$theme_version = WC_Admin_Status::get_file_version( $theme_file );
|
2014-02-11 13:33:56 +00:00
|
|
|
|
|
|
|
if ( $core_version && $theme_version && version_compare( $theme_version, $core_version, '<' ) ) {
|
|
|
|
$outdated = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-11 15:13:47 +00:00
|
|
|
if ( $outdated ) {
|
|
|
|
include( 'views/html-notice-template-check.php' );
|
2015-01-20 15:23:34 +00:00
|
|
|
} else {
|
|
|
|
self::remove_notice( 'template_files' );
|
2014-02-11 15:13:47 +00:00
|
|
|
}
|
2014-02-11 13:33:56 +00:00
|
|
|
}
|
2016-03-15 15:58:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a notice asking users to convert to shipping zones.
|
|
|
|
*/
|
2016-05-11 13:22:06 +00:00
|
|
|
public static function legacy_shipping_notice() {
|
2016-03-15 15:58:00 +00:00
|
|
|
$maybe_load_legacy_methods = array( 'flat_rate', 'free_shipping', 'international_delivery', 'local_delivery', 'local_pickup' );
|
|
|
|
$enabled = false;
|
|
|
|
|
|
|
|
foreach ( $maybe_load_legacy_methods as $method ) {
|
|
|
|
$options = get_option( 'woocommerce_' . $method . '_settings' );
|
|
|
|
if ( $options && isset( $options['enabled'] ) && 'yes' === $options['enabled'] ) {
|
|
|
|
$enabled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $enabled ) {
|
|
|
|
include( 'views/html-notice-legacy-shipping.php' );
|
|
|
|
} else {
|
|
|
|
self::remove_notice( 'template_files' );
|
|
|
|
}
|
|
|
|
}
|
2016-03-31 17:54:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* No shipping methods.
|
|
|
|
*/
|
2016-05-11 13:22:06 +00:00
|
|
|
public static function no_shipping_methods_notice() {
|
2016-03-31 17:54:33 +00:00
|
|
|
if ( wc_shipping_enabled() && ( empty( $_GET['page'] ) || empty( $_GET['tab'] ) || 'wc-settings' !== $_GET['page'] || 'shipping' !== $_GET['tab'] ) ) {
|
|
|
|
$product_count = wp_count_posts( 'product' );
|
2016-05-25 11:05:23 +00:00
|
|
|
$method_count = wc_get_shipping_method_count();
|
2016-03-31 17:54:33 +00:00
|
|
|
|
|
|
|
if ( $product_count->publish > 0 && 0 === $method_count ) {
|
|
|
|
include( 'views/html-notice-no-shipping-methods.php' );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $method_count > 0 ) {
|
|
|
|
self::remove_notice( 'no_shipping_methods' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-19 12:14:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Simplify Commerce is being removed from core.
|
|
|
|
*/
|
2016-05-11 13:22:06 +00:00
|
|
|
public static function simplify_commerce_notice() {
|
2016-06-03 11:28:25 +00:00
|
|
|
$location = wc_get_base_location();
|
2016-06-06 16:24:31 +00:00
|
|
|
|
2016-06-03 11:28:25 +00:00
|
|
|
if ( class_exists( 'WC_Gateway_Simplify_Commerce_Loader' ) || ! in_array( $location['country'], apply_filters( 'woocommerce_gateway_simplify_commerce_supported_countries', array( 'US', 'IE' ) ) ) ) {
|
2016-04-19 12:14:13 +00:00
|
|
|
self::remove_notice( 'simplify_commerce' );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( empty( $_GET['action'] ) ) {
|
|
|
|
include( 'views/html-notice-simplify-commerce.php' );
|
|
|
|
}
|
|
|
|
}
|
2013-07-24 16:01:36 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 13:22:06 +00:00
|
|
|
WC_Admin_Notices::init();
|