WC_Admin_Notices

WC_Admin_Notices Class.

Defined (1)

The class is defined in the following location(s).

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