class-wc-admin-notices.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. /**
  3. * Display notices in admin
  4. *
  5. * @package WooCommerce\Admin
  6. * @version 3.4.0
  7. */
  8. defined( 'ABSPATH' ) || exit;
  9. /**
  10. * WC_Admin_Notices Class.
  11. */
  12. class WC_Admin_Notices {
  13. /**
  14. * Stores notices.
  15. *
  16. * @var array
  17. */
  18. private static $notices = array();
  19. /**
  20. * Array of notices - name => callback.
  21. *
  22. * @var array
  23. */
  24. private static $core_notices = array(
  25. 'install' => 'install_notice',
  26. 'update' => 'update_notice',
  27. 'template_files' => 'template_file_check_notice',
  28. 'legacy_shipping' => 'legacy_shipping_notice',
  29. 'no_shipping_methods' => 'no_shipping_methods_notice',
  30. 'simplify_commerce' => 'simplify_commerce_notice',
  31. 'regenerating_thumbnails' => 'regenerating_thumbnails_notice',
  32. 'no_secure_connection' => 'secure_connection_notice',
  33. 'wootenberg' => 'wootenberg_feature_plugin_notice',
  34. );
  35. /**
  36. * Constructor.
  37. */
  38. public static function init() {
  39. self::$notices = get_option( 'woocommerce_admin_notices', array() );
  40. add_action( 'switch_theme', array( __CLASS__, 'reset_admin_notices' ) );
  41. add_action( 'woocommerce_installed', array( __CLASS__, 'reset_admin_notices' ) );
  42. add_action( 'wp_loaded', array( __CLASS__, 'hide_notices' ) );
  43. add_action( 'shutdown', array( __CLASS__, 'store_notices' ) );
  44. if ( current_user_can( 'manage_woocommerce' ) ) {
  45. add_action( 'admin_print_styles', array( __CLASS__, 'add_notices' ) );
  46. add_action( 'activate_gutenberg/gutenberg.php', array( __CLASS__, 'add_wootenberg_feature_plugin_notice_on_gutenberg_activate' ) );
  47. }
  48. }
  49. /**
  50. * Store notices to DB
  51. */
  52. public static function store_notices() {
  53. update_option( 'woocommerce_admin_notices', self::get_notices() );
  54. }
  55. /**
  56. * Get notices
  57. *
  58. * @return array
  59. */
  60. public static function get_notices() {
  61. return self::$notices;
  62. }
  63. /**
  64. * Remove all notices.
  65. */
  66. public static function remove_all_notices() {
  67. self::$notices = array();
  68. }
  69. /**
  70. * Reset notices for themes when switched or a new version of WC is installed.
  71. */
  72. public static function reset_admin_notices() {
  73. $simplify_options = get_option( 'woocommerce_simplify_commerce_settings', array() );
  74. $location = wc_get_base_location();
  75. $shop_page = 0 < wc_get_page_id( 'shop' ) ? get_permalink( wc_get_page_id( 'shop' ) ) : get_home_url();
  76. 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' ) ), true ) ) {
  77. WC_Admin_Notices::add_notice( 'simplify_commerce' );
  78. }
  79. if ( ! is_ssl() || 'https' !== substr( $shop_page, 0, 5 ) ) {
  80. WC_Admin_Notices::add_notice( 'no_secure_connection' );
  81. }
  82. WC_Admin_Notices::add_wootenberg_feature_plugin_notice();
  83. self::add_notice( 'template_files' );
  84. }
  85. /**
  86. * Show a notice.
  87. *
  88. * @param string $name Notice name.
  89. */
  90. public static function add_notice( $name ) {
  91. self::$notices = array_unique( array_merge( self::get_notices(), array( $name ) ) );
  92. }
  93. /**
  94. * Remove a notice from being displayed.
  95. *
  96. * @param string $name Notice 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. *
  105. * @param string $name Notice name.
  106. * @return boolean
  107. */
  108. public static function has_notice( $name ) {
  109. return in_array( $name, self::get_notices(), true );
  110. }
  111. /**
  112. * Hide a notice if the GET variable is set.
  113. */
  114. public static function hide_notices() {
  115. if ( isset( $_GET['wc-hide-notice'] ) && isset( $_GET['_wc_notice_nonce'] ) ) { // WPCS: input var ok, CSRF ok.
  116. if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_GET['_wc_notice_nonce'] ) ), 'woocommerce_hide_notices_nonce' ) ) { // WPCS: input var ok, CSRF ok.
  117. wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
  118. }
  119. if ( ! current_user_can( 'manage_woocommerce' ) ) {
  120. wp_die( esc_html__( 'You don&#8217;t have permission to do this.', 'woocommerce' ) );
  121. }
  122. $hide_notice = sanitize_text_field( wp_unslash( $_GET['wc-hide-notice'] ) ); // WPCS: input var ok, CSRF ok.
  123. self::remove_notice( $hide_notice );
  124. update_user_meta( get_current_user_id(), 'dismissed_' . $hide_notice . '_notice', true );
  125. do_action( 'woocommerce_hide_' . $hide_notice . '_notice' );
  126. }
  127. }
  128. /**
  129. * Add notices + styles if needed.
  130. */
  131. public static function add_notices() {
  132. $notices = self::get_notices();
  133. if ( empty( $notices ) ) {
  134. return;
  135. }
  136. $screen = get_current_screen();
  137. $screen_id = $screen ? $screen->id : '';
  138. $show_on_screens = array(
  139. 'dashboard',
  140. 'plugins',
  141. );
  142. // Notices should only show on WooCommerce screens, the main dashboard, and on the plugins screen.
  143. if ( ! in_array( $screen_id, wc_get_screen_ids(), true ) && ! in_array( $screen_id, $show_on_screens, true ) ) {
  144. return;
  145. }
  146. wp_enqueue_style( 'woocommerce-activation', plugins_url( '/assets/css/activation.css', WC_PLUGIN_FILE ), array(), WC_VERSION );
  147. // Add RTL support.
  148. wp_style_add_data( 'woocommerce-activation', 'rtl', 'replace' );
  149. foreach ( $notices as $notice ) {
  150. if ( ! empty( self::$core_notices[ $notice ] ) && apply_filters( 'woocommerce_show_admin_notice', true, $notice ) ) {
  151. add_action( 'admin_notices', array( __CLASS__, self::$core_notices[ $notice ] ) );
  152. } else {
  153. add_action( 'admin_notices', array( __CLASS__, 'output_custom_notices' ) );
  154. }
  155. }
  156. }
  157. /**
  158. * Add a custom notice.
  159. *
  160. * @param string $name Notice name.
  161. * @param string $notice_html Notice HTML.
  162. */
  163. public static function add_custom_notice( $name, $notice_html ) {
  164. self::add_notice( $name );
  165. update_option( 'woocommerce_admin_notice_' . $name, wp_kses_post( $notice_html ) );
  166. }
  167. /**
  168. * Output any stored custom notices.
  169. */
  170. public static function output_custom_notices() {
  171. $notices = self::get_notices();
  172. if ( ! empty( $notices ) ) {
  173. foreach ( $notices as $notice ) {
  174. if ( empty( self::$core_notices[ $notice ] ) ) {
  175. $notice_html = get_option( 'woocommerce_admin_notice_' . $notice );
  176. if ( $notice_html ) {
  177. include dirname( __FILE__ ) . '/views/html-notice-custom.php';
  178. }
  179. }
  180. }
  181. }
  182. }
  183. /**
  184. * If we need to update, include a message with the update button.
  185. */
  186. public static function update_notice() {
  187. if ( version_compare( get_option( 'woocommerce_db_version' ), WC_VERSION, '<' ) ) {
  188. $updater = new WC_Background_Updater();
  189. if ( $updater->is_updating() || ! empty( $_GET['do_update_woocommerce'] ) ) { // WPCS: input var ok, CSRF ok.
  190. include dirname( __FILE__ ) . '/views/html-notice-updating.php';
  191. } else {
  192. include dirname( __FILE__ ) . '/views/html-notice-update.php';
  193. }
  194. } else {
  195. include dirname( __FILE__ ) . '/views/html-notice-updated.php';
  196. }
  197. }
  198. /**
  199. * If we have just installed, show a message with the install pages button.
  200. */
  201. public static function install_notice() {
  202. include dirname( __FILE__ ) . '/views/html-notice-install.php';
  203. }
  204. /**
  205. * Show the Theme Check notice.
  206. *
  207. * @todo Remove this next major release.
  208. */
  209. public static function theme_check_notice() {
  210. wc_deprecated_function( 'WC_Admin_Notices::theme_check_notice', '3.3.0' );
  211. if ( ! current_theme_supports( 'woocommerce' ) ) {
  212. include dirname( __FILE__ ) . '/views/html-notice-theme-support.php';
  213. }
  214. }
  215. /**
  216. * Show a notice highlighting bad template files.
  217. */
  218. public static function template_file_check_notice() {
  219. $core_templates = WC_Admin_Status::scan_template_files( WC()->plugin_path() . '/templates' );
  220. $outdated = false;
  221. foreach ( $core_templates as $file ) {
  222. $theme_file = false;
  223. if ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
  224. $theme_file = get_stylesheet_directory() . '/' . $file;
  225. } elseif ( file_exists( get_stylesheet_directory() . '/' . WC()->template_path() . $file ) ) {
  226. $theme_file = get_stylesheet_directory() . '/' . WC()->template_path() . $file;
  227. } elseif ( file_exists( get_template_directory() . '/' . $file ) ) {
  228. $theme_file = get_template_directory() . '/' . $file;
  229. } elseif ( file_exists( get_template_directory() . '/' . WC()->template_path() . $file ) ) {
  230. $theme_file = get_template_directory() . '/' . WC()->template_path() . $file;
  231. }
  232. if ( false !== $theme_file ) {
  233. $core_version = WC_Admin_Status::get_file_version( WC()->plugin_path() . '/templates/' . $file );
  234. $theme_version = WC_Admin_Status::get_file_version( $theme_file );
  235. if ( $core_version && $theme_version && version_compare( $theme_version, $core_version, '<' ) ) {
  236. $outdated = true;
  237. break;
  238. }
  239. }
  240. }
  241. if ( $outdated ) {
  242. include dirname( __FILE__ ) . '/views/html-notice-template-check.php';
  243. } else {
  244. self::remove_notice( 'template_files' );
  245. }
  246. }
  247. /**
  248. * Show a notice asking users to convert to shipping zones.
  249. */
  250. public static function legacy_shipping_notice() {
  251. $maybe_load_legacy_methods = array( 'flat_rate', 'free_shipping', 'international_delivery', 'local_delivery', 'local_pickup' );
  252. $enabled = false;
  253. foreach ( $maybe_load_legacy_methods as $method ) {
  254. $options = get_option( 'woocommerce_' . $method . '_settings' );
  255. if ( $options && isset( $options['enabled'] ) && 'yes' === $options['enabled'] ) {
  256. $enabled = true;
  257. }
  258. }
  259. if ( $enabled ) {
  260. include dirname( __FILE__ ) . '/views/html-notice-legacy-shipping.php';
  261. } else {
  262. self::remove_notice( 'template_files' );
  263. }
  264. }
  265. /**
  266. * No shipping methods.
  267. */
  268. public static function no_shipping_methods_notice() {
  269. if ( wc_shipping_enabled() && ( empty( $_GET['page'] ) || empty( $_GET['tab'] ) || 'wc-settings' !== $_GET['page'] || 'shipping' !== $_GET['tab'] ) ) { // WPCS: input var ok, CSRF ok.
  270. $product_count = wp_count_posts( 'product' );
  271. $method_count = wc_get_shipping_method_count();
  272. if ( $product_count->publish > 0 && 0 === $method_count ) {
  273. include dirname( __FILE__ ) . '/views/html-notice-no-shipping-methods.php';
  274. }
  275. if ( $method_count > 0 ) {
  276. self::remove_notice( 'no_shipping_methods' );
  277. }
  278. }
  279. }
  280. /**
  281. * Simplify Commerce is being removed from core.
  282. */
  283. public static function simplify_commerce_notice() {
  284. $location = wc_get_base_location();
  285. if ( class_exists( 'WC_Gateway_Simplify_Commerce_Loader' ) || ! in_array( $location['country'], apply_filters( 'woocommerce_gateway_simplify_commerce_supported_countries', array( 'US', 'IE' ) ), true ) ) {
  286. self::remove_notice( 'simplify_commerce' );
  287. return;
  288. }
  289. if ( empty( $_GET['action'] ) ) { // WPCS: input var ok, CSRF ok.
  290. include dirname( __FILE__ ) . '/views/html-notice-simplify-commerce.php';
  291. }
  292. }
  293. /**
  294. * Notice shown when regenerating thumbnails background process is running.
  295. */
  296. public static function regenerating_thumbnails_notice() {
  297. include dirname( __FILE__ ) . '/views/html-notice-regenerating-thumbnails.php';
  298. }
  299. /**
  300. * Notice about secure connection.
  301. */
  302. public static function secure_connection_notice() {
  303. if ( get_user_meta( get_current_user_id(), 'dismissed_no_secure_connection_notice', true ) ) {
  304. return;
  305. }
  306. include dirname( __FILE__ ) . '/views/html-notice-secure-connection.php';
  307. }
  308. /**
  309. * If Gutenberg is active, tell people about the Products block feature plugin.
  310. *
  311. * @since 3.4.3
  312. * @todo Remove this notice and associated code once the feature plugin has been merged into core.
  313. */
  314. public static function add_wootenberg_feature_plugin_notice() {
  315. if ( is_plugin_active( 'gutenberg/gutenberg.php' ) && ! is_plugin_active( 'woo-gutenberg-products-block/woocommerce-gutenberg-products-block.php' ) ) {
  316. self::add_notice( 'wootenberg' );
  317. }
  318. }
  319. /**
  320. * Tell people about the Products block feature plugin when they activate Gutenberg.
  321. *
  322. * @since 3.4.3
  323. * @todo Remove this notice and associated code once the feature plugin has been merged into core.
  324. */
  325. public static function add_wootenberg_feature_plugin_notice_on_gutenberg_activate() {
  326. if ( ! is_plugin_active( 'woo-gutenberg-products-block/woocommerce-gutenberg-products-block.php' ) ) {
  327. self::add_notice( 'wootenberg' );
  328. }
  329. }
  330. /**
  331. * Notice about trying the Products block.
  332. */
  333. public static function wootenberg_feature_plugin_notice() {
  334. if ( get_user_meta( get_current_user_id(), 'dismissed_wootenberg_notice', true ) || is_plugin_active( 'woo-gutenberg-products-block/woocommerce-gutenberg-products-block.php' ) ) {
  335. self::remove_notice( 'wootenberg' );
  336. return;
  337. }
  338. include dirname( __FILE__ ) . '/views/html-notice-wootenberg.php';
  339. }
  340. }
  341. WC_Admin_Notices::init();