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 {
|
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Array of notices - name => callback.
|
2015-01-20 15:23:34 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2015-04-29 11:21:48 +00:00
|
|
|
private $core_notices = array(
|
2016-03-15 15:58: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',
|
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
|
|
|
*/
|
|
|
|
public function __construct() {
|
2014-02-11 13:33:56 +00:00
|
|
|
add_action( 'switch_theme', array( $this, 'reset_admin_notices' ) );
|
2015-01-23 16:36:52 +00:00
|
|
|
add_action( 'woocommerce_installed', array( $this, 'reset_admin_notices' ) );
|
2015-01-20 15:23:34 +00:00
|
|
|
add_action( 'wp_loaded', array( $this, 'hide_notices' ) );
|
2015-04-29 11:08:15 +00:00
|
|
|
|
|
|
|
if ( current_user_can( 'manage_woocommerce' ) ) {
|
|
|
|
add_action( 'admin_print_styles', array( $this, 'add_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() {
|
|
|
|
delete_option( 'woocommerce_admin_notices' );
|
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
|
|
|
*/
|
|
|
|
public 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
|
|
|
}
|
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.
|
2015-01-20 15:23:34 +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 ) {
|
|
|
|
$notices = array_unique( array_merge( get_option( 'woocommerce_admin_notices', array() ), array( $name ) ) );
|
|
|
|
update_option( 'woocommerce_admin_notices', $notices );
|
|
|
|
}
|
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 ) {
|
|
|
|
$notices = array_diff( get_option( 'woocommerce_admin_notices', array() ), array( $name ) );
|
|
|
|
update_option( 'woocommerce_admin_notices', $notices );
|
|
|
|
}
|
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 ) {
|
|
|
|
return in_array( $name, get_option( 'woocommerce_admin_notices', array() ) );
|
|
|
|
}
|
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.
|
|
|
|
*/
|
|
|
|
public 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.
|
|
|
|
*/
|
|
|
|
public function add_notices() {
|
|
|
|
$notices = get_option( 'woocommerce_admin_notices', array() );
|
2014-06-04 16:01:38 +00:00
|
|
|
|
2015-04-29 11:21:48 +00:00
|
|
|
if ( $notices ) {
|
2014-08-28 22:18:26 +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 ) {
|
|
|
|
if ( ! empty( $this->core_notices[ $notice ] ) && apply_filters( 'woocommerce_show_admin_notice', true, $notice ) ) {
|
|
|
|
add_action( 'admin_notices', array( $this, $this->core_notices[ $notice ] ) );
|
|
|
|
}
|
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
|
|
|
*/
|
|
|
|
public function update_notice() {
|
|
|
|
include( 'views/html-notice-update.php' );
|
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
|
|
|
*/
|
2014-02-11 13:33:56 +00:00
|
|
|
public 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
|
|
|
*/
|
2014-02-11 13:33:56 +00:00
|
|
|
public 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
|
|
|
*/
|
|
|
|
public 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;
|
|
|
|
} elseif( file_exists( get_template_directory() . '/woocommerce/' . $file ) ) {
|
|
|
|
$theme_file = get_template_directory() . '/woocommerce/' . $file;
|
|
|
|
}
|
|
|
|
|
2015-04-01 13:33:56 +00:00
|
|
|
if ( $theme_file !== false ) {
|
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.
|
|
|
|
*/
|
|
|
|
public function legacy_shipping_notice() {
|
|
|
|
$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' );
|
|
|
|
}
|
|
|
|
}
|
2013-07-24 16:01:36 +00:00
|
|
|
}
|
|
|
|
|
2015-01-20 15:23:34 +00:00
|
|
|
new WC_Admin_Notices();
|