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0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / admin / class-wc-admin-notices.php
woocommerce / includes / admin Last commit date
helper 1 year ago importers 1 year ago list-tables 1 year ago marketplace-suggestions 2 years ago meta-boxes 1 year ago notes 2 years ago plugin-updates 2 years ago reports 1 year ago settings 1 year ago views 1 year ago class-wc-admin-addons.php 1 year ago class-wc-admin-api-keys-table-list.php 2 years ago class-wc-admin-api-keys.php 2 years ago class-wc-admin-assets.php 1 year ago class-wc-admin-attributes.php 3 years ago class-wc-admin-brands.php 1 year ago class-wc-admin-customize.php 5 years ago class-wc-admin-dashboard-setup.php 2 years ago class-wc-admin-dashboard.php 1 year ago class-wc-admin-duplicate-product.php 1 year ago class-wc-admin-exporters.php 3 years ago class-wc-admin-help.php 2 years ago class-wc-admin-importers.php 1 year ago class-wc-admin-log-table-list.php 2 years ago class-wc-admin-marketplace-promotions.php 1 year ago class-wc-admin-menus.php 1 year ago class-wc-admin-meta-boxes.php 2 years ago class-wc-admin-notices.php 1 year ago class-wc-admin-permalink-settings.php 5 years ago class-wc-admin-pointers.php 3 years ago class-wc-admin-post-types.php 1 year ago class-wc-admin-profile.php 1 year ago class-wc-admin-reports.php 1 year ago class-wc-admin-settings.php 1 year ago class-wc-admin-setup-wizard.php 2 years ago class-wc-admin-status.php 2 years ago class-wc-admin-taxonomies.php 3 years ago class-wc-admin-upload-downloadable-product.php 2 years ago class-wc-admin-webhooks-table-list.php 1 year ago class-wc-admin-webhooks.php 1 year ago class-wc-admin.php 1 year ago wc-admin-functions.php 1 year ago wc-meta-box-functions.php 1 year ago woocommerce-legacy-reports.php 1 year ago
class-wc-admin-notices.php
740 lines
1 <?php
2 /**
3 * Display notices in admin
4 *
5 * @package WooCommerce\Admin
6 * @version 3.4.0
7 */
8
9 use Automattic\Jetpack\Constants;
10 use Automattic\WooCommerce\Internal\Utilities\Users;
11 use Automattic\WooCommerce\Internal\Utilities\WebhookUtil;
12 use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;
13
14 defined( 'ABSPATH' ) || exit;
15
16 /**
17 * WC_Admin_Notices Class.
18 */
19 class WC_Admin_Notices {
20
21 /**
22 * Local notices cache.
23 *
24 * DON'T manipulate this field directly!
25 * Always use get_notices and set_notices instead.
26 *
27 * @var array
28 */
29 private static $notices = array();
30
31 /**
32 * Array of notices - name => callback.
33 *
34 * @var array
35 */
36 private static $core_notices = array(
37 'update' => 'update_notice',
38 'template_files' => 'template_file_check_notice',
39 'legacy_shipping' => 'legacy_shipping_notice',
40 'no_shipping_methods' => 'no_shipping_methods_notice',
41 'regenerating_thumbnails' => 'regenerating_thumbnails_notice',
42 'regenerating_lookup_table' => 'regenerating_lookup_table_notice',
43 'no_secure_connection' => 'secure_connection_notice',
44 'maxmind_license_key' => 'maxmind_missing_license_key_notice',
45 'redirect_download_method' => 'redirect_download_method_notice',
46 'uploads_directory_is_unprotected' => 'uploads_directory_is_unprotected_notice',
47 'base_tables_missing' => 'base_tables_missing_notice',
48 'download_directories_sync_complete' => 'download_directories_sync_complete',
49 );
50
51 /**
52 * Stores a flag indicating if the code is running in a multisite setup.
53 *
54 * @var bool
55 */
56 private static bool $is_multisite;
57
58 /**
59 * Initializes the class.
60 */
61 public static function init() {
62 self::$is_multisite = is_multisite();
63 self::set_notices( get_option( 'woocommerce_admin_notices', array() ) );
64
65 add_action( 'switch_theme', array( __CLASS__, 'reset_admin_notices' ) );
66 add_action( 'woocommerce_installed', array( __CLASS__, 'reset_admin_notices' ) );
67 add_action( 'wp_loaded', array( __CLASS__, 'add_redirect_download_method_notice' ) );
68 add_action( 'admin_init', array( __CLASS__, 'hide_notices' ), 20 );
69 add_action( 'admin_init', array( __CLASS__, 'maybe_remove_legacy_api_removal_notice' ), 20 );
70
71 // @TODO: This prevents Action Scheduler async jobs from storing empty list of notices during WC installation.
72 // That could lead to OBW not starting and 'Run setup wizard' notice not appearing in WP admin, which we want
73 // to avoid.
74 if ( ! WC_Install::is_new_install() || ! wc_is_running_from_async_action_scheduler() ) {
75 add_action( 'shutdown', array( __CLASS__, 'store_notices' ) );
76 }
77
78 if ( current_user_can( 'manage_woocommerce' ) ) {
79 add_action( 'admin_print_styles', array( __CLASS__, 'add_notices' ) );
80 }
81 }
82
83 /**
84 * Parses query to create nonces when available.
85 *
86 * @deprecated 5.4.0
87 * @param object $response The WP_REST_Response we're working with.
88 * @return object $response The prepared WP_REST_Response object.
89 */
90 public static function prepare_note_with_nonce( $response ) {
91 wc_deprecated_function( __CLASS__ . '::' . __FUNCTION__, '5.4.0' );
92
93 return $response;
94 }
95
96 /**
97 * Store the locally cached notices to DB.
98 */
99 public static function store_notices() {
100 update_option( 'woocommerce_admin_notices', self::get_notices() );
101 }
102
103 /**
104 * Get the value of the locally cached notices array for the current site.
105 *
106 * @return array
107 */
108 public static function get_notices() {
109 if ( ! self::$is_multisite ) {
110 return self::$notices;
111 }
112
113 $blog_id = get_current_blog_id();
114 $notices = self::$notices[ $blog_id ] ?? null;
115 if ( ! is_null( $notices ) ) {
116 return $notices;
117 }
118
119 self::$notices[ $blog_id ] = get_option( 'woocommerce_admin_notices', array() );
120 return self::$notices[ $blog_id ];
121 }
122
123 /**
124 * Set the locally cached notices array for the current site.
125 *
126 * @param array $notices New value for the locally cached notices array.
127 */
128 private static function set_notices( array $notices ) {
129 if ( self::$is_multisite ) {
130 self::$notices[ get_current_blog_id() ] = $notices;
131 } else {
132 self::$notices = $notices;
133 }
134 }
135
136 /**
137 * Remove all notices from the locally cached notices array.
138 */
139 public static function remove_all_notices() {
140 self::set_notices( array() );
141 }
142
143 /**
144 * Reset notices for themes when switched or a new version of WC is installed.
145 */
146 public static function reset_admin_notices() {
147 if ( ! self::is_ssl() ) {
148 self::add_notice( 'no_secure_connection' );
149 }
150 if ( ! self::is_uploads_directory_protected() ) {
151 self::add_notice( 'uploads_directory_is_unprotected' );
152 }
153 self::add_notice( 'template_files' );
154 self::add_min_version_notice();
155 self::add_maxmind_missing_license_key_notice();
156 self::maybe_add_legacy_api_removal_notice();
157 }
158
159 /**
160 * Add an admin notice about unsupported webhooks with Legacy API payload if at least one of these exist
161 * and the Legacy REST API plugin is not installed.
162 */
163 private static function maybe_add_legacy_api_removal_notice() {
164 if ( wc_get_container()->get( WebhookUtil::class )->get_legacy_webhooks_count() > 0 && ! WC()->legacy_rest_api_is_available() ) {
165 self::add_custom_notice(
166 'legacy_webhooks_unsupported_in_woo_90',
167 sprintf(
168 '%s%s',
169 sprintf(
170 '<h4>%s</h4>',
171 esc_html__( 'WooCommerce webhooks that use the Legacy REST API are unsupported', 'woocommerce' )
172 ),
173 sprintf(
174 // translators: Placeholders are URLs.
175 wpautop( __( '⚠️ The WooCommerce Legacy REST API has been removed from WooCommerce, this will cause <a href="%1$s">webhooks on this site that are configured to use the Legacy REST API</a> to stop working. <a target="_blank" href="%2$s">A separate WooCommerce extension is available</a> to allow these webhooks to keep using the Legacy REST API without interruption. You can also edit these webhooks to use the current REST API version to generate the payload instead. <b><a target="_blank" href="%3$s">Learn more about this change.</a></b>', 'woocommerce' ) ),
176 admin_url( 'admin.php?page=wc-settings&tab=advanced&section=webhooks&legacy=true' ),
177 'https://wordpress.org/plugins/woocommerce-legacy-rest-api/',
178 'https://developer.woocommerce.com/2023/10/03/the-legacy-rest-api-will-move-to-a-dedicated-extension-in-woocommerce-9-0/'
179 )
180 )
181 );
182 }
183 }
184
185 /**
186 * Remove the admin notice about the unsupported webhooks if the Legacy REST API plugin is installed.
187 *
188 * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
189 */
190 public static function maybe_remove_legacy_api_removal_notice() {
191 if ( self::has_notice( 'legacy_webhooks_unsupported_in_woo_90' ) && ( WC()->legacy_rest_api_is_available() || 0 === wc_get_container()->get( WebhookUtil::class )->get_legacy_webhooks_count() ) ) {
192 self::remove_notice( 'legacy_webhooks_unsupported_in_woo_90' );
193 }
194 }
195
196 /**
197 * Show a notice.
198 *
199 * @param string $name Notice name.
200 * @param bool $force_save Force saving inside this method instead of at the 'shutdown'.
201 */
202 public static function add_notice( $name, $force_save = false ) {
203 self::set_notices( array_unique( array_merge( self::get_notices(), array( $name ) ) ) );
204
205 if ( $force_save ) {
206 // Adding early save to prevent more race conditions with notices.
207 self::store_notices();
208 }
209 }
210
211 /**
212 * Remove a notice from being displayed.
213 *
214 * @param string $name Notice name.
215 * @param bool $force_save Force saving inside this method instead of at the 'shutdown'.
216 */
217 public static function remove_notice( $name, $force_save = false ) {
218 self::set_notices( array_diff( self::get_notices(), array( $name ) ) );
219 delete_option( 'woocommerce_admin_notice_' . $name );
220
221 if ( $force_save ) {
222 // Adding early save to prevent more race conditions with notices.
223 self::store_notices();
224 }
225 }
226
227 /**
228 * Remove a given set of notices.
229 *
230 * An array of notice names or a regular expression string can be passed, in the later case
231 * all the notices whose name matches the regular expression will be removed.
232 *
233 * @param array|string $names_array_or_regex An array of notice names, or a string representing a regular expression.
234 * @param bool $force_save Force saving inside this method instead of at the 'shutdown'.
235 * @return void
236 */
237 public static function remove_notices( $names_array_or_regex, $force_save = false ) {
238 if ( ! is_array( $names_array_or_regex ) ) {
239 $names_array_or_regex = array_filter( self::get_notices(), fn( $notice_name ) => 1 === preg_match( $names_array_or_regex, $notice_name ) );
240 }
241 self::set_notices( array_diff( self::get_notices(), $names_array_or_regex ) );
242
243 if ( $force_save ) {
244 // Adding early save to prevent more race conditions with notices.
245 self::store_notices();
246 }
247 }
248
249 /**
250 * See if a notice is being shown.
251 *
252 * @param string $name Notice name.
253 *
254 * @return boolean
255 */
256 public static function has_notice( $name ) {
257 return in_array( $name, self::get_notices(), true );
258 }
259
260 /**
261 * Hide a notice if the GET variable is set.
262 */
263 public static function hide_notices() {
264 if ( isset( $_GET['wc-hide-notice'] ) && isset( $_GET['_wc_notice_nonce'] ) ) {
265 if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_GET['_wc_notice_nonce'] ) ), 'woocommerce_hide_notices_nonce' ) ) {
266 wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
267 }
268
269 $notice_name = sanitize_text_field( wp_unslash( $_GET['wc-hide-notice'] ) );
270
271 /**
272 * Filter the capability required to dismiss a given notice.
273 *
274 * @since 6.7.0
275 *
276 * @param string $default_capability The default required capability.
277 * @param string $notice_name The notice name.
278 */
279 $required_capability = apply_filters( 'woocommerce_dismiss_admin_notice_capability', 'manage_woocommerce', $notice_name );
280
281 if ( ! current_user_can( $required_capability ) ) {
282 wp_die( esc_html__( 'You don&#8217;t have permission to do this.', 'woocommerce' ) );
283 }
284
285 self::hide_notice( $notice_name );
286 }
287 }
288
289 /**
290 * Hide a single notice.
291 *
292 * @param string $name Notice name.
293 */
294 private static function hide_notice( $name ) {
295 self::remove_notice( $name );
296
297 update_user_meta( get_current_user_id(), 'dismissed_' . $name . '_notice', true );
298
299 do_action( 'woocommerce_hide_' . $name . '_notice' );
300 }
301
302 /**
303 * Check if a given user has dismissed a given admin notice.
304 *
305 * @since 8.5.0
306 *
307 * @param string $name The name of the admin notice to check.
308 * @param int|null $user_id User id, or null for the current user.
309 * @return bool True if the user has dismissed the notice.
310 */
311 public static function user_has_dismissed_notice( string $name, ?int $user_id = null ): bool {
312 return (bool) get_user_meta( $user_id ?? get_current_user_id(), "dismissed_{$name}_notice", true );
313 }
314
315 /**
316 * Add notices + styles if needed.
317 */
318 public static function add_notices() {
319 $notices = self::get_notices();
320
321 if ( empty( $notices ) ) {
322 return;
323 }
324
325 require_once WC_ABSPATH . 'includes/admin/wc-admin-functions.php';
326
327 $screen = get_current_screen();
328 $screen_id = $screen ? $screen->id : '';
329 $show_on_screens = array(
330 'dashboard',
331 'plugins',
332 );
333
334 // Notices should only show on WooCommerce screens, the main dashboard, and on the plugins screen.
335 if ( ! in_array( $screen_id, wc_get_screen_ids(), true ) && ! in_array( $screen_id, $show_on_screens, true ) ) {
336 return;
337 }
338
339 wp_enqueue_style( 'woocommerce-activation', plugins_url( '/assets/css/activation.css', WC_PLUGIN_FILE ), array(), Constants::get_constant( 'WC_VERSION' ) );
340
341 // Add RTL support.
342 wp_style_add_data( 'woocommerce-activation', 'rtl', 'replace' );
343
344 foreach ( $notices as $notice ) {
345 if ( ! empty( self::$core_notices[ $notice ] ) && apply_filters( 'woocommerce_show_admin_notice', true, $notice ) ) {
346 add_action( 'admin_notices', array( __CLASS__, self::$core_notices[ $notice ] ) );
347 } else {
348 add_action( 'admin_notices', array( __CLASS__, 'output_custom_notices' ) );
349 }
350 }
351 }
352
353 /**
354 * Add a custom notice.
355 *
356 * @param string $name Notice name.
357 * @param string $notice_html Notice HTML.
358 */
359 public static function add_custom_notice( $name, $notice_html ) {
360 self::add_notice( $name );
361 update_option( 'woocommerce_admin_notice_' . $name, wp_kses_post( $notice_html ) );
362 }
363
364 /**
365 * Output any stored custom notices.
366 */
367 public static function output_custom_notices() {
368 $notices = self::get_notices();
369
370 if ( ! empty( $notices ) ) {
371 foreach ( $notices as $notice ) {
372 if ( empty( self::$core_notices[ $notice ] ) ) {
373 $notice_html = get_option( 'woocommerce_admin_notice_' . $notice );
374
375 if ( $notice_html ) {
376 include __DIR__ . '/views/html-notice-custom.php';
377 }
378 }
379 }
380 }
381 }
382
383 /**
384 * If we need to update the database, include a message with the DB update button.
385 */
386 public static function update_notice() {
387 $screen = get_current_screen();
388 $screen_id = $screen ? $screen->id : '';
389 if ( WC()->is_wc_admin_active() && in_array( $screen_id, wc_get_screen_ids(), true ) ) {
390 return;
391 }
392
393 if ( WC_Install::needs_db_update() ) {
394 $next_scheduled_date = WC()->queue()->get_next( 'woocommerce_run_update_callback', null, 'woocommerce-db-updates' );
395
396 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
397 if ( $next_scheduled_date || ! empty( $_GET['do_update_woocommerce'] ) ) {
398 include __DIR__ . '/views/html-notice-updating.php';
399 } else {
400 include __DIR__ . '/views/html-notice-update.php';
401 }
402 } else {
403 include __DIR__ . '/views/html-notice-updated.php';
404 }
405 }
406
407 /**
408 * If we have just installed, show a message with the install pages button.
409 *
410 * @deprecated 4.6.0
411 */
412 public static function install_notice() {
413 _deprecated_function( __CLASS__ . '::' . __FUNCTION__, '4.6.0', esc_html__( 'Onboarding is maintained in WooCommerce Admin.', 'woocommerce' ) );
414 }
415
416 /**
417 * Show a notice highlighting bad template files.
418 */
419 public static function template_file_check_notice() {
420 $core_templates = WC_Admin_Status::scan_template_files( WC()->plugin_path() . '/templates' );
421 $outdated = false;
422
423 foreach ( $core_templates as $file ) {
424
425 $theme_file = false;
426 if ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
427 $theme_file = get_stylesheet_directory() . '/' . $file;
428 } elseif ( file_exists( get_stylesheet_directory() . '/' . WC()->template_path() . $file ) ) {
429 $theme_file = get_stylesheet_directory() . '/' . WC()->template_path() . $file;
430 } elseif ( file_exists( get_template_directory() . '/' . $file ) ) {
431 $theme_file = get_template_directory() . '/' . $file;
432 } elseif ( file_exists( get_template_directory() . '/' . WC()->template_path() . $file ) ) {
433 $theme_file = get_template_directory() . '/' . WC()->template_path() . $file;
434 }
435
436 if ( false !== $theme_file ) {
437 $core_version = WC_Admin_Status::get_file_version( WC()->plugin_path() . '/templates/' . $file );
438 $theme_version = WC_Admin_Status::get_file_version( $theme_file );
439
440 if ( $core_version && $theme_version && version_compare( $theme_version, $core_version, '<' ) ) {
441 $outdated = true;
442 break;
443 }
444 }
445 }
446
447 if ( $outdated ) {
448 include __DIR__ . '/views/html-notice-template-check.php';
449 } else {
450 self::remove_notice( 'template_files' );
451 }
452 }
453
454 /**
455 * Show a notice asking users to convert to shipping zones.
456 *
457 * @todo remove in 4.0.0
458 */
459 public static function legacy_shipping_notice() {
460 $maybe_load_legacy_methods = array( 'flat_rate', 'free_shipping', 'international_delivery', 'local_delivery', 'local_pickup' );
461 $enabled = false;
462
463 foreach ( $maybe_load_legacy_methods as $method ) {
464 $options = get_option( 'woocommerce_' . $method . '_settings' );
465 if ( $options && isset( $options['enabled'] ) && 'yes' === $options['enabled'] ) {
466 $enabled = true;
467 }
468 }
469
470 if ( $enabled ) {
471 include __DIR__ . '/views/html-notice-legacy-shipping.php';
472 } else {
473 self::remove_notice( 'template_files' );
474 }
475 }
476
477 /**
478 * No shipping methods.
479 */
480 public static function no_shipping_methods_notice() {
481 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
482 if ( wc_shipping_enabled() && ( empty( $_GET['page'] ) || empty( $_GET['tab'] ) || 'wc-settings' !== $_GET['page'] || 'shipping' !== $_GET['tab'] ) ) {
483 $product_count = wp_count_posts( 'product' );
484 $method_count = wc_get_shipping_method_count();
485
486 if ( $product_count->publish > 0 && 0 === $method_count ) {
487 include __DIR__ . '/views/html-notice-no-shipping-methods.php';
488 }
489
490 if ( $method_count > 0 ) {
491 self::remove_notice( 'no_shipping_methods' );
492 }
493 }
494 }
495
496 /**
497 * Notice shown when regenerating thumbnails background process is running.
498 */
499 public static function regenerating_thumbnails_notice() {
500 include __DIR__ . '/views/html-notice-regenerating-thumbnails.php';
501 }
502
503 /**
504 * Notice about secure connection.
505 */
506 public static function secure_connection_notice() {
507 if ( self::is_ssl() || get_user_meta( get_current_user_id(), 'dismissed_no_secure_connection_notice', true ) ) {
508 return;
509 }
510
511 include __DIR__ . '/views/html-notice-secure-connection.php';
512 }
513
514 /**
515 * Notice shown when regenerating thumbnails background process is running.
516 *
517 * @since 3.6.0
518 */
519 public static function regenerating_lookup_table_notice() {
520 // See if this is still relevant.
521 if ( ! wc_update_product_lookup_tables_is_running() ) {
522 self::remove_notice( 'regenerating_lookup_table' );
523 return;
524 }
525
526 include __DIR__ . '/views/html-notice-regenerating-lookup-table.php';
527 }
528
529 /**
530 * Add notice about minimum PHP and WordPress requirement.
531 *
532 * @since 3.6.5
533 */
534 public static function add_min_version_notice() {
535 if ( version_compare( phpversion(), WC_NOTICE_MIN_PHP_VERSION, '<' ) || version_compare( get_bloginfo( 'version' ), WC_NOTICE_MIN_WP_VERSION, '<' ) ) {
536 self::add_notice( WC_PHP_MIN_REQUIREMENTS_NOTICE );
537 }
538 }
539
540 /**
541 * Notice about WordPress and PHP minimum requirements.
542 *
543 * @deprecated 8.2.0 WordPress and PHP minimum requirements notices are no longer shown.
544 *
545 * @since 3.6.5
546 * @return void
547 */
548 public static function wp_php_min_requirements_notice() {
549 }
550
551 /**
552 * Add MaxMind missing license key notice.
553 *
554 * @since 3.9.0
555 */
556 public static function add_maxmind_missing_license_key_notice() {
557 $default_address = get_option( 'woocommerce_default_customer_address' );
558
559 if ( ! in_array( $default_address, array( 'geolocation', 'geolocation_ajax' ), true ) ) {
560 return;
561 }
562
563 $integration_options = get_option( 'woocommerce_maxmind_geolocation_settings' );
564 if ( empty( $integration_options['license_key'] ) ) {
565 self::add_notice( 'maxmind_license_key' );
566
567 }
568 }
569
570 /**
571 * Add notice about Redirect-only download method, nudging user to switch to a different method instead.
572 */
573 public static function add_redirect_download_method_notice() {
574 if ( 'redirect' === get_option( 'woocommerce_file_download_method' ) ) {
575 self::add_notice( 'redirect_download_method' );
576 } else {
577 self::remove_notice( 'redirect_download_method' );
578 }
579 }
580
581 /**
582 * Notice about the completion of the product downloads sync, with further advice for the site operator.
583 */
584 public static function download_directories_sync_complete() {
585 $notice_dismissed = apply_filters(
586 'woocommerce_hide_download_directories_sync_complete',
587 get_user_meta( get_current_user_id(), 'download_directories_sync_complete', true )
588 );
589
590 if ( $notice_dismissed ) {
591 self::remove_notice( 'download_directories_sync_complete' );
592 }
593
594 if ( Users::is_site_administrator() ) {
595 include __DIR__ . '/views/html-notice-download-dir-sync-complete.php';
596 }
597 }
598
599 /**
600 * Display MaxMind missing license key notice.
601 *
602 * @since 3.9.0
603 */
604 public static function maxmind_missing_license_key_notice() {
605 $user_dismissed_notice = get_user_meta( get_current_user_id(), 'dismissed_maxmind_license_key_notice', true );
606 $filter_dismissed_notice = ! apply_filters( 'woocommerce_maxmind_geolocation_display_notices', true );
607
608 if ( $user_dismissed_notice || $filter_dismissed_notice ) {
609 self::remove_notice( 'maxmind_license_key' );
610 return;
611 }
612
613 include __DIR__ . '/views/html-notice-maxmind-license-key.php';
614 }
615
616 /**
617 * Notice about Redirect-Only download method.
618 *
619 * @since 4.0
620 */
621 public static function redirect_download_method_notice() {
622 if ( apply_filters( 'woocommerce_hide_redirect_method_nag', get_user_meta( get_current_user_id(), 'dismissed_redirect_download_method_notice', true ) ) ) {
623 self::remove_notice( 'redirect_download_method' );
624 return;
625 }
626
627 include __DIR__ . '/views/html-notice-redirect-only-download.php';
628 }
629
630 /**
631 * Notice about uploads directory begin unprotected.
632 *
633 * @since 4.2.0
634 */
635 public static function uploads_directory_is_unprotected_notice() {
636 if ( get_user_meta( get_current_user_id(), 'dismissed_uploads_directory_is_unprotected_notice', true ) || self::is_uploads_directory_protected() ) {
637 self::remove_notice( 'uploads_directory_is_unprotected' );
638 return;
639 }
640
641 include __DIR__ . '/views/html-notice-uploads-directory-is-unprotected.php';
642 }
643
644 /**
645 * Notice about base tables missing.
646 */
647 public static function base_tables_missing_notice() {
648 $notice_dismissed = apply_filters(
649 'woocommerce_hide_base_tables_missing_nag',
650 get_user_meta( get_current_user_id(), 'dismissed_base_tables_missing_notice', true )
651 );
652 if ( $notice_dismissed ) {
653 self::remove_notice( 'base_tables_missing' );
654 }
655
656 include __DIR__ . '/views/html-notice-base-table-missing.php';
657 }
658
659 /**
660 * Determine if the store is running SSL.
661 *
662 * @return bool Flag SSL enabled.
663 * @since 3.5.1
664 */
665 protected static function is_ssl() {
666 $shop_page = wc_get_page_permalink( 'shop' );
667
668 return ( is_ssl() && 'https' === substr( $shop_page, 0, 5 ) );
669 }
670
671 /**
672 * Wrapper for is_plugin_active.
673 *
674 * @param string $plugin Plugin to check.
675 * @return boolean
676 */
677 protected static function is_plugin_active( $plugin ) {
678 if ( ! function_exists( 'is_plugin_active' ) ) {
679 include_once ABSPATH . 'wp-admin/includes/plugin.php';
680 }
681 return is_plugin_active( $plugin );
682 }
683
684 /**
685 * Simplify Commerce is no longer in core.
686 *
687 * @deprecated 3.6.0 No longer shown.
688 */
689 public static function simplify_commerce_notice() {
690 wc_deprecated_function( 'WC_Admin_Notices::simplify_commerce_notice', '3.6.0' );
691 }
692
693 /**
694 * Show the Theme Check notice.
695 *
696 * @deprecated 3.3.0 No longer shown.
697 */
698 public static function theme_check_notice() {
699 wc_deprecated_function( 'WC_Admin_Notices::theme_check_notice', '3.3.0' );
700 }
701
702 /**
703 * Check if uploads directory is protected.
704 *
705 * @since 4.2.0
706 * @return bool
707 */
708 protected static function is_uploads_directory_protected() {
709 $cache_key = '_woocommerce_upload_directory_status';
710 $status = get_transient( $cache_key );
711
712 // Check for cache.
713 if ( false !== $status ) {
714 return 'protected' === $status;
715 }
716
717 // Get only data from the uploads directory.
718 $uploads = wp_get_upload_dir();
719
720 // Check for the "uploads/woocommerce_uploads" directory.
721 $response = wp_safe_remote_get(
722 esc_url_raw( $uploads['baseurl'] . '/woocommerce_uploads/' ),
723 array(
724 'redirection' => 0,
725 )
726 );
727 $response_code = intval( wp_remote_retrieve_response_code( $response ) );
728 $response_content = wp_remote_retrieve_body( $response );
729
730 // Check if returns 200 with empty content in case can open an index.html file,
731 // and check for non-200 codes in case the directory is protected.
732 $is_protected = ( 200 === $response_code && empty( $response_content ) ) || ( 200 !== $response_code );
733 set_transient( $cache_key, $is_protected ? 'protected' : 'unprotected', 1 * DAY_IN_SECONDS );
734
735 return $is_protected;
736 }
737 }
738
739 WC_Admin_Notices::init();
740