/includes/admin/class-wc-admin-notices.php

  1. <?php 
  2. /** 
  3. * Display notices in admin 
  4. * 
  5. * @author WooThemes 
  6. * @category Admin 
  7. * @package WooCommerce/Admin 
  8. * @version 2.3.0 
  9. */ 
  10.  
  11. if ( ! defined( 'ABSPATH' ) ) { 
  12. exit; 
  13.  
  14. /** 
  15. * WC_Admin_Notices Class. 
  16. */ 
  17. class WC_Admin_Notices { 
  18.  
  19. /** 
  20. * Stores notices. 
  21. * @var array 
  22. */ 
  23. private static $notices = array(); 
  24.  
  25. /** 
  26. * Array of notices - name => callback. 
  27. * @var array 
  28. */ 
  29. private static $core_notices = array( 
  30. 'install' => 'install_notice',  
  31. 'update' => 'update_notice',  
  32. 'template_files' => 'template_file_check_notice',  
  33. 'theme_support' => 'theme_check_notice',  
  34. 'legacy_shipping' => 'legacy_shipping_notice',  
  35. 'no_shipping_methods' => 'no_shipping_methods_notice',  
  36. 'simplify_commerce' => 'simplify_commerce_notice',  
  37. ); 
  38.  
  39. /** 
  40. * Constructor. 
  41. */ 
  42. public static function init() { 
  43. self::$notices = get_option( 'woocommerce_admin_notices', array() ); 
  44.  
  45. add_action( 'switch_theme', array( __CLASS__, 'reset_admin_notices' ) ); 
  46. add_action( 'woocommerce_installed', array( __CLASS__, 'reset_admin_notices' ) ); 
  47. add_action( 'wp_loaded', array( __CLASS__, 'hide_notices' ) ); 
  48. add_action( 'shutdown', array( __CLASS__, 'store_notices' ) ); 
  49.  
  50. if ( current_user_can( 'manage_woocommerce' ) ) { 
  51. add_action( 'admin_print_styles', array( __CLASS__, 'add_notices' ) ); 
  52.  
  53. /** 
  54. * Store notices to DB 
  55. */ 
  56. public static function store_notices() { 
  57. update_option( 'woocommerce_admin_notices', self::get_notices() ); 
  58.  
  59. /** 
  60. * Get notices 
  61. * @return array 
  62. */ 
  63. public static function get_notices() { 
  64. return self::$notices; 
  65.  
  66. /** 
  67. * Remove all notices. 
  68. */ 
  69. public static function remove_all_notices() { 
  70. self::$notices = array(); 
  71.  
  72. /** 
  73. * Reset notices for themes when switched or a new version of WC is installed. 
  74. */ 
  75. public static function reset_admin_notices() { 
  76. if ( ! current_theme_supports( 'woocommerce' ) && ! in_array( get_option( 'template' ), wc_get_core_supported_themes() ) ) { 
  77. self::add_notice( 'theme_support' ); 
  78.  
  79. $simplify_options = get_option( 'woocommerce_simplify_commerce_settings', array() ); 
  80. $location = wc_get_base_location(); 
  81.  
  82. 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' ) ) ) ) { 
  83. WC_Admin_Notices::add_notice( 'simplify_commerce' ); 
  84.  
  85. self::add_notice( 'template_files' ); 
  86.  
  87. /** 
  88. * Show a notice. 
  89. * @param string $name 
  90. */ 
  91. public static function add_notice( $name ) { 
  92. self::$notices = array_unique( array_merge( self::get_notices(), array( $name ) ) ); 
  93.  
  94. /** 
  95. * Remove a notice from being displayed. 
  96. * @param string $name 
  97. */ 
  98. public static function remove_notice( $name ) { 
  99. self::$notices = array_diff( self::get_notices(), array( $name ) ); 
  100. delete_option( 'woocommerce_admin_notice_' . $name ); 
  101.  
  102. /** 
  103. * See if a notice is being shown. 
  104. * @param string $name 
  105. * @return boolean 
  106. */ 
  107. public static function has_notice( $name ) { 
  108. return in_array( $name, self::get_notices() ); 
  109.  
  110. /** 
  111. * Hide a notice if the GET variable is set. 
  112. */ 
  113. public static function hide_notices() { 
  114. if ( isset( $_GET['wc-hide-notice'] ) && isset( $_GET['_wc_notice_nonce'] ) ) { 
  115. if ( ! wp_verify_nonce( $_GET['_wc_notice_nonce'], 'woocommerce_hide_notices_nonce' ) ) { 
  116. wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) ); 
  117.  
  118. if ( ! current_user_can( 'manage_woocommerce' ) ) { 
  119. wp_die( __( 'Cheatin’ huh?', 'woocommerce' ) ); 
  120.  
  121. $hide_notice = sanitize_text_field( $_GET['wc-hide-notice'] ); 
  122. self::remove_notice( $hide_notice ); 
  123. do_action( 'woocommerce_hide_' . $hide_notice . '_notice' ); 
  124.  
  125. /** 
  126. * Add notices + styles if needed. 
  127. */ 
  128. public static function add_notices() { 
  129. $notices = self::get_notices(); 
  130.  
  131. if ( ! empty( $notices ) ) { 
  132. wp_enqueue_style( 'woocommerce-activation', plugins_url( '/assets/css/activation.css', WC_PLUGIN_FILE ) ); 
  133. foreach ( $notices as $notice ) { 
  134. if ( ! empty( self::$core_notices[ $notice ] ) && apply_filters( 'woocommerce_show_admin_notice', true, $notice ) ) { 
  135. add_action( 'admin_notices', array( __CLASS__, self::$core_notices[ $notice ] ) ); 
  136. } else { 
  137. add_action( 'admin_notices', array( __CLASS__, 'output_custom_notices' ) ); 
  138.  
  139. /** 
  140. * Add a custom notice. 
  141. * @param string $name 
  142. * @param string $notice_html 
  143. */ 
  144. public static function add_custom_notice( $name, $notice_html ) { 
  145. self::add_notice( $name ); 
  146. update_option( 'woocommerce_admin_notice_' . $name, wp_kses_post( $notice_html ) ); 
  147.  
  148. /** 
  149. * Output any stored custom notices. 
  150. */ 
  151. public static function output_custom_notices() { 
  152. $notices = self::get_notices(); 
  153.  
  154. if ( ! empty( $notices ) ) { 
  155. foreach ( $notices as $notice ) { 
  156. if ( empty( self::$core_notices[ $notice ] ) ) { 
  157. $notice_html = get_option( 'woocommerce_admin_notice_' . $notice ); 
  158.  
  159. if ( $notice_html ) { 
  160. include( 'views/html-notice-custom.php' ); 
  161.  
  162. /** 
  163. * If we need to update, include a message with the update button. 
  164. */ 
  165. public static function update_notice() { 
  166. if ( version_compare( get_option( 'woocommerce_db_version' ), WC_VERSION, '<' ) ) { 
  167. $updater = new WC_Background_Updater(); 
  168. if ( $updater->is_updating() || ! empty( $_GET['do_update_woocommerce'] ) ) { 
  169. include( 'views/html-notice-updating.php' ); 
  170. } else { 
  171. include( 'views/html-notice-update.php' ); 
  172. } else { 
  173. include( 'views/html-notice-updated.php' ); 
  174.  
  175. /** 
  176. * If we have just installed, show a message with the install pages button. 
  177. */ 
  178. public static function install_notice() { 
  179. include( 'views/html-notice-install.php' ); 
  180.  
  181. /** 
  182. * Show the Theme Check notice. 
  183. */ 
  184. public static function theme_check_notice() { 
  185. if ( ! current_theme_supports( 'woocommerce' ) && ! in_array( get_option( 'template' ), wc_get_core_supported_themes() ) ) { 
  186. include( 'views/html-notice-theme-support.php' ); 
  187. } else { 
  188. self::remove_notice( 'theme_support' ); 
  189.  
  190. /** 
  191. * Show a notice highlighting bad template files. 
  192. */ 
  193. public static function template_file_check_notice() { 
  194. $core_templates = WC_Admin_Status::scan_template_files( WC()->plugin_path() . '/templates' ); 
  195. $outdated = false; 
  196.  
  197. foreach ( $core_templates as $file ) { 
  198.  
  199. $theme_file = false; 
  200. if ( file_exists( get_stylesheet_directory() . '/' . $file ) ) { 
  201. $theme_file = get_stylesheet_directory() . '/' . $file; 
  202. } elseif ( file_exists( get_stylesheet_directory() . '/woocommerce/' . $file ) ) { 
  203. $theme_file = get_stylesheet_directory() . '/woocommerce/' . $file; 
  204. } elseif ( file_exists( get_template_directory() . '/' . $file ) ) { 
  205. $theme_file = get_template_directory() . '/' . $file; 
  206. } elseif ( file_exists( get_template_directory() . '/woocommerce/' . $file ) ) { 
  207. $theme_file = get_template_directory() . '/woocommerce/' . $file; 
  208.  
  209. if ( false !== $theme_file ) { 
  210. $core_version = WC_Admin_Status::get_file_version( WC()->plugin_path() . '/templates/' . $file ); 
  211. $theme_version = WC_Admin_Status::get_file_version( $theme_file ); 
  212.  
  213. if ( $core_version && $theme_version && version_compare( $theme_version, $core_version, '<' ) ) { 
  214. $outdated = true; 
  215. break; 
  216.  
  217. if ( $outdated ) { 
  218. include( 'views/html-notice-template-check.php' ); 
  219. } else { 
  220. self::remove_notice( 'template_files' ); 
  221.  
  222. /** 
  223. * Show a notice asking users to convert to shipping zones. 
  224. */ 
  225. public static function legacy_shipping_notice() { 
  226. $maybe_load_legacy_methods = array( 'flat_rate', 'free_shipping', 'international_delivery', 'local_delivery', 'local_pickup' ); 
  227. $enabled = false; 
  228.  
  229. foreach ( $maybe_load_legacy_methods as $method ) { 
  230. $options = get_option( 'woocommerce_' . $method . '_settings' ); 
  231. if ( $options && isset( $options['enabled'] ) && 'yes' === $options['enabled'] ) { 
  232. $enabled = true; 
  233.  
  234. if ( $enabled ) { 
  235. include( 'views/html-notice-legacy-shipping.php' ); 
  236. } else { 
  237. self::remove_notice( 'template_files' ); 
  238.  
  239. /** 
  240. * No shipping methods. 
  241. */ 
  242. public static function no_shipping_methods_notice() { 
  243. if ( wc_shipping_enabled() && ( empty( $_GET['page'] ) || empty( $_GET['tab'] ) || 'wc-settings' !== $_GET['page'] || 'shipping' !== $_GET['tab'] ) ) { 
  244. global $wpdb; 
  245.  
  246. $product_count = wp_count_posts( 'product' ); 
  247. $method_count = wc_get_shipping_method_count(); 
  248.  
  249. if ( $product_count->publish > 0 && 0 === $method_count ) { 
  250. include( 'views/html-notice-no-shipping-methods.php' ); 
  251.  
  252. if ( $method_count > 0 ) { 
  253. self::remove_notice( 'no_shipping_methods' ); 
  254.  
  255. /** 
  256. * Simplify Commerce is being removed from core. 
  257. */ 
  258. public static function simplify_commerce_notice() { 
  259. $location = wc_get_base_location(); 
  260.  
  261. if ( class_exists( 'WC_Gateway_Simplify_Commerce_Loader' ) || ! in_array( $location['country'], apply_filters( 'woocommerce_gateway_simplify_commerce_supported_countries', array( 'US', 'IE' ) ) ) ) { 
  262. self::remove_notice( 'simplify_commerce' ); 
  263. return; 
  264. if ( empty( $_GET['action'] ) ) { 
  265. include( 'views/html-notice-simplify-commerce.php' ); 
  266.  
  267. WC_Admin_Notices::init(); 
.