| @@ -10,8 +10,10 @@ | ||
| 10 | 10 | use function PoweredCache\Utils\can_configure_htaccess; |
| 11 | 11 | use function PoweredCache\Utils\can_configure_object_cache; |
| 12 | 12 | use function PoweredCache\Utils\can_control_all_settings; |
| 13 | 13 | use function PoweredCache\Utils\get_object_cache_dropins; |
| 14 | +use function PoweredCache\Utils\is_premium; | |
| 15 | +use const PoweredCache\Constants\PURGE_CACHE_PLUGIN_NOTICE_TRANSIENT; | |
| 14 | 16 | |
| 15 | 17 | /** |
| 16 | 18 | * Default setup routine |
| 17 | 19 | * |
| @@ -18,18 +20,27 @@ | ||
| 18 | 20 | * @return void |
| 19 | 21 | */ |
| 20 | 22 | function setup() { |
| 21 | 23 | if ( POWERED_CACHE_IS_NETWORK ) { |
| 22 | - add_action( 'network_admin_notices', __NAMESPACE__ . '\\maybe_display_plugin_compatability_notices' ); | |
| 24 | + add_action( 'network_admin_notices', __NAMESPACE__ . '\\maybe_display_plugin_compatibility_notices' ); | |
| 23 | 25 | add_action( 'network_admin_notices', __NAMESPACE__ . '\\maybe_display_advanced_cache_notices' ); |
| 24 | 26 | add_action( 'network_admin_notices', __NAMESPACE__ . '\\maybe_display_object_cache_notices' ); |
| 25 | 27 | add_action( 'network_admin_notices', __NAMESPACE__ . '\\maybe_display_htaccess_notice' ); |
| 28 | + add_action( 'network_admin_notices', __NAMESPACE__ . '\\maybe_display_purge_cache_plugin_notice' ); | |
| 29 | + add_action( 'network_admin_notices', __NAMESPACE__ . '\\maybe_display_dev_mode_notice' ); | |
| 26 | 30 | } else { |
| 27 | - add_action( 'admin_notices', __NAMESPACE__ . '\\maybe_display_plugin_compatability_notices' ); | |
| 31 | + add_action( 'admin_notices', __NAMESPACE__ . '\\maybe_display_plugin_compatibility_notices' ); | |
| 28 | 32 | add_action( 'admin_notices', __NAMESPACE__ . '\\maybe_display_advanced_cache_notices' ); |
| 29 | 33 | add_action( 'admin_notices', __NAMESPACE__ . '\\maybe_display_object_cache_notices' ); |
| 30 | 34 | add_action( 'admin_notices', __NAMESPACE__ . '\\maybe_display_htaccess_notice' ); |
| 35 | + add_action( 'admin_notices', __NAMESPACE__ . '\\maybe_display_purge_cache_plugin_notice' ); | |
| 36 | + add_action( 'admin_notices', __NAMESPACE__ . '\\maybe_display_dev_mode_notice' ); | |
| 31 | 37 | } |
| 38 | + | |
| 39 | + add_action( 'activated_plugin', __NAMESPACE__ . '\\observe_plugin_changes', 10, 2 ); | |
| 40 | + add_action( 'deactivated_plugin', __NAMESPACE__ . '\\observe_plugin_changes', 10, 2 ); | |
| 41 | + add_action( 'admin_post_powered_cache_dismiss_notice', __NAMESPACE__ . '\\dismiss_notice' ); | |
| 42 | + add_action( 'wp_ajax_powered_cache_dismiss_notice_ajax', __NAMESPACE__ . '\\dismiss_notice_ajax' ); | |
| 32 | 43 | } |
| 33 | 44 | |
| 34 | 45 | /** |
| 35 | 46 | * Display incompatible plugins |
| @@ -35,9 +46,11 @@ | ||
| 35 | 46 | * Display incompatible plugins |
| 36 | 47 | * |
| 37 | 48 | * @since 1.0 |
| 38 | 49 | */ |
| 39 | -function maybe_display_plugin_compatability_notices() { | |
| 50 | +function maybe_display_plugin_compatibility_notices() { | |
| 51 | + $settings = \PoweredCache\Utils\get_settings(); | |
| 52 | + | |
| 40 | 53 | $plugins = array( |
| 41 | 54 | 'hummingbird-performance' => 'hummingbird-performance/wp-hummingbird.php', |
| 42 | 55 | 'wp-rocket' => 'wp-rocket/wp-rocket.php', |
| 43 | 56 | 'w3-total-cache' => 'w3-total-cache/w3-total-cache.php', |
| @@ -52,10 +65,20 @@ | ||
| 52 | 65 | 'gzip-ninja-speed-compression' => 'gzip-ninja-speed-compression/gzip-ninja-speed.php', |
| 53 | 66 | 'speed-booster-pack' => 'speed-booster-pack/speed-booster-pack.php', |
| 54 | 67 | 'wp-performance-score-booster' => 'wp-performance-score-booster/wp-performance-score-booster.php', |
| 55 | 68 | 'check-and-enable-gzip-compression' => 'check-and-enable-gzip-compression/richards-toolbox.php', |
| 69 | + 'swift-performance-lite' => 'swift-performance-lite/performance.php', | |
| 70 | + 'swift-performance' => 'swift-performance/performance.php', | |
| 71 | + 'litespeed-cache' => 'litespeed-cache/litespeed-cache.php', | |
| 72 | + 'wp-optimize' => 'wp-optimize/wp-optimize.php', | |
| 56 | 73 | ); |
| 57 | 74 | |
| 75 | + if ( $settings['prefetch_links'] && is_premium() ) { | |
| 76 | + $plugins['quicklink'] = 'quicklink/quicklink.php'; | |
| 77 | + $plugins['flying-pages'] = 'flying-pages/flying-pages.php'; | |
| 78 | + $plugins['instant-page'] = 'instant-page/instantpage.php'; | |
| 79 | + } | |
| 80 | + | |
| 58 | 81 | $callback = POWERED_CACHE_IS_NETWORK ? 'is_plugin_active_for_network' : 'is_plugin_active'; |
| 59 | 82 | |
| 60 | 83 | $plugins = array_filter( $plugins, $callback ); |
| 61 | 84 | |
| @@ -65,14 +88,14 @@ | ||
| 65 | 88 | ?> |
| 66 | 89 | |
| 67 | 90 | <?php if ( current_user_can( 'activate_plugins' ) ) : ?> |
| 68 | 91 | <div class="error"> |
| 69 | - <p><?php esc_html_e( 'The following plugins are not compatible with Powered Cache and may cause unintended results:', 'powered-cache' ); ?></p> | |
| 92 | + <p><?php esc_html_e( 'The following plugins are not compatible with Powered Cache and may cause unintended results:', 'powered-cache' ); ?></p> | |
| 70 | 93 | <ul class="incompatible-plugin-list"> |
| 71 | 94 | <?php |
| 72 | 95 | foreach ( $plugins as $plugin ) { |
| 73 | 96 | $plugin_data = get_plugin_data( WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $plugin ); |
| 74 | - echo '<li>' . esc_attr( $plugin_data['Name'] ) . '</span> <a href="' . esc_url_raw( wp_nonce_url( admin_url( 'admin-post.php?action=deactivate_plugin&plugin=' . rawurlencode( $plugin ) ), 'deactivate_plugin' ) ) . '" class="button-secondary">' . esc_html__( 'Deactivate', 'powered-cache' ) . '</a></li>'; | |
| 97 | + echo '<li>' . esc_attr( $plugin_data['Name'] ) . '</span> <a href="' . esc_url_raw( wp_nonce_url( admin_url( 'admin-post.php?action=deactivate_plugin&plugin=' . rawurlencode( $plugin ) ), 'deactivate_plugin' ) ) . '" class="button-secondary">' . esc_html__( 'Deactivate', 'powered-cache' ) . '</a></li>'; // phpcs:ignore WordPressVIPMinimum.Security.ProperEscapingFunction.notAttrEscAttr | |
| 75 | 98 | } |
| 76 | 99 | ?> |
| 77 | 100 | </ul> |
| 78 | 101 | </div> |
| @@ -297,6 +320,204 @@ | ||
| 297 | 320 | <?php echo wp_kses_post( $message ); ?> |
| 298 | 321 | </p> |
| 299 | 322 | </div> |
| 300 | 323 | |
| 324 | + <?php | |
| 325 | +} | |
| 326 | + | |
| 327 | +/** | |
| 328 | + * Observe new plugin activation/deactivation | |
| 329 | + * | |
| 330 | + * @param string $plugin file | |
| 331 | + * @param boolean $network_wide Whether plugin de/activated network wide or not | |
| 332 | + * | |
| 333 | + * @return void | |
| 334 | + * @since 3.2 | |
| 335 | + */ | |
| 336 | +function observe_plugin_changes( $plugin, $network_wide ) { | |
| 337 | + if ( false !== stripos( $plugin, 'powered-cache' ) ) { | |
| 338 | + return; | |
| 339 | + } | |
| 340 | + | |
| 341 | + if ( $network_wide ) { | |
| 342 | + set_site_transient( PURGE_CACHE_PLUGIN_NOTICE_TRANSIENT, '1' ); | |
| 343 | + | |
| 344 | + return; | |
| 345 | + } | |
| 346 | + | |
| 347 | + set_transient( PURGE_CACHE_PLUGIN_NOTICE_TRANSIENT, '1' ); | |
| 348 | +} | |
| 349 | + | |
| 350 | +/** | |
| 351 | + * Display cache purging notice upon a new plugin activated/deactivated | |
| 352 | + * | |
| 353 | + * @return void | |
| 354 | + * @since 3.2 | |
| 355 | + */ | |
| 356 | +function maybe_display_purge_cache_plugin_notice() { | |
| 357 | + $has_notice = false; | |
| 358 | + | |
| 359 | + if ( POWERED_CACHE_IS_NETWORK && current_user_can( 'manage_network' ) ) { | |
| 360 | + $has_notice = get_site_transient( PURGE_CACHE_PLUGIN_NOTICE_TRANSIENT ); | |
| 361 | + $purge_url = wp_nonce_url( admin_url( 'admin-post.php?action=powered_cache_purge_page_cache_network' ), 'powered_cache_purge_page_cache_network' ); | |
| 362 | + } elseif ( current_user_can( 'activate_plugins' ) ) { | |
| 363 | + $has_notice = get_transient( PURGE_CACHE_PLUGIN_NOTICE_TRANSIENT ); | |
| 364 | + $purge_url = wp_nonce_url( admin_url( 'admin-post.php?action=powered_cache_purge_all_cache' ), 'powered_cache_purge_all_cache' ); | |
| 365 | + } | |
| 366 | + | |
| 367 | + if ( $has_notice ) { | |
| 368 | + $message = __( '<strong>Powered Cache:</strong> One or more plugins have been activated or deactivated; consider clearing the cache if these changes impact your site\'s front end.', 'powered-cache' ); | |
| 369 | + $dismiss_nonce = wp_create_nonce( 'powered_cache_dismiss_notice_ajax' ); | |
| 370 | + ?> | |
| 371 | + <div class="notice notice-warning is-dismissible powered-cache-dismissible-notice" data-notice-id="<?php echo esc_attr( PURGE_CACHE_PLUGIN_NOTICE_TRANSIENT ); ?>" data-nonce="<?php echo esc_attr( $dismiss_nonce ); ?>"> | |
| 372 | + <p> | |
| 373 | + <?php echo wp_kses_post( $message ); ?> | |
| 374 | + </p> | |
| 375 | + <p> | |
| 376 | + <a href="<?php echo esc_url_raw( $purge_url ); ?>" class="button-primary"> | |
| 377 | + <?php esc_html_e( 'Purge Cache', 'powered-cache' ); ?> | |
| 378 | + </a> | |
| 379 | + <a href="<?php echo esc_url_raw( wp_nonce_url( admin_url( 'admin-post.php?action=powered_cache_dismiss_notice¬ice=' . PURGE_CACHE_PLUGIN_NOTICE_TRANSIENT ), 'powered_cache_dismiss_notice' ) ); ?>" class="button-secondary powered-cache-dismiss-button"> | |
| 380 | + <?php esc_html_e( 'Dismiss this notice', 'powered-cache' ); ?> | |
| 381 | + </a> | |
| 382 | + </p> | |
| 383 | + </div> | |
| 384 | + <script type="text/javascript"> | |
| 385 | + (function() { | |
| 386 | + document.addEventListener('click', function(e) { | |
| 387 | + var button = e.target.closest('.notice-dismiss, .powered-cache-dismiss-button'); | |
| 388 | + if (!button) { | |
| 389 | + return; | |
| 390 | + } | |
| 391 | + | |
| 392 | + var notice = button.closest('.powered-cache-dismissible-notice'); | |
| 393 | + if (!notice) { | |
| 394 | + return; | |
| 395 | + } | |
| 396 | + | |
| 397 | + e.preventDefault(); | |
| 398 | + | |
| 399 | + var noticeId = notice.getAttribute('data-notice-id'); | |
| 400 | + var nonce = notice.getAttribute('data-nonce'); | |
| 401 | + | |
| 402 | + if (button.classList.contains('powered-cache-dismiss-button')) { | |
| 403 | + e.preventDefault(); | |
| 404 | + notice.style.opacity = '1'; | |
| 405 | + notice.style.transition = 'opacity 0.3s'; | |
| 406 | + notice.style.opacity = '0'; | |
| 407 | + | |
| 408 | + setTimeout(function() { | |
| 409 | + if (notice && notice.parentNode) { | |
| 410 | + notice.parentNode.removeChild(notice); | |
| 411 | + } | |
| 412 | + }, 300); | |
| 413 | + } | |
| 414 | + | |
| 415 | + var xhr = new XMLHttpRequest(); | |
| 416 | + xhr.open('POST', '<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>', true); | |
| 417 | + xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); | |
| 418 | + xhr.onload = function () { | |
| 419 | + if (xhr.status !== 200) { | |
| 420 | + console.error('Dismiss failed'); | |
| 421 | + } | |
| 422 | + }; | |
| 423 | + | |
| 424 | + xhr.send( | |
| 425 | + 'action=powered_cache_dismiss_notice_ajax' | |
| 426 | + + '¬ice=' + encodeURIComponent(noticeId) | |
| 427 | + + '&nonce=' + encodeURIComponent(nonce) | |
| 428 | + ); | |
| 429 | + }); | |
| 430 | + })(); | |
| 431 | + </script> | |
| 432 | + <?php | |
| 433 | + } | |
| 434 | +} | |
| 435 | + | |
| 436 | +/** | |
| 437 | + * Dismis given notice | |
| 438 | + * | |
| 439 | + * @return void | |
| 440 | + * @since 3.2 | |
| 441 | + */ | |
| 442 | +function dismiss_notice() { | |
| 443 | + $nonce = isset( $_GET['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) : ''; | |
| 444 | + if ( ! wp_verify_nonce( $nonce, 'powered_cache_dismiss_notice' ) ) { | |
| 445 | + wp_nonce_ays( '' ); | |
| 446 | + } | |
| 447 | + | |
| 448 | + if ( current_user_can( 'manage_options' ) && ! empty( $_GET['notice'] ) ) { | |
| 449 | + $notice = sanitize_text_field( wp_unslash( $_GET['notice'] ) ); | |
| 450 | + | |
| 451 | + if ( POWERED_CACHE_IS_NETWORK ) { | |
| 452 | + delete_site_transient( $notice ); | |
| 453 | + } else { | |
| 454 | + delete_transient( $notice ); | |
| 455 | + } | |
| 456 | + } | |
| 457 | + | |
| 458 | + wp_safe_redirect( esc_url_raw( wp_get_referer() ) ); | |
| 459 | + exit; | |
| 460 | +} | |
| 461 | + | |
| 462 | +/** | |
| 463 | + * Dismiss notice via AJAX | |
| 464 | + * | |
| 465 | + * @return void | |
| 466 | + * @since 3.7 | |
| 467 | + */ | |
| 468 | +function dismiss_notice_ajax() { | |
| 469 | + check_ajax_referer( 'powered_cache_dismiss_notice_ajax', 'nonce' ); | |
| 470 | + | |
| 471 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 472 | + wp_send_json_error( array( 'message' => esc_html__( 'Permission denied', 'powered-cache' ) ) ); | |
| 473 | + } | |
| 474 | + | |
| 475 | + if ( empty( $_POST['notice'] ) ) { | |
| 476 | + wp_send_json_error( array( 'message' => esc_html__( 'Notice ID missing', 'powered-cache' ) ) ); | |
| 477 | + } | |
| 478 | + | |
| 479 | + $notice = sanitize_text_field( wp_unslash( $_POST['notice'] ) ); | |
| 480 | + | |
| 481 | + if ( POWERED_CACHE_IS_NETWORK ) { | |
| 482 | + delete_site_transient( $notice ); | |
| 483 | + } else { | |
| 484 | + delete_transient( $notice ); | |
| 485 | + } | |
| 486 | + | |
| 487 | + wp_send_json_success( array( 'message' => esc_html__( 'Notice dismissed', 'powered-cache' ) ) ); | |
| 488 | +} | |
| 489 | + | |
| 490 | +/** | |
| 491 | + * Display a notice when development mode is active | |
| 492 | + * | |
| 493 | + * @return void | |
| 494 | + * @since 3.6 | |
| 495 | + * @related \PoweredCache\DevMode | |
| 496 | + */ | |
| 497 | +function maybe_display_dev_mode_notice() { | |
| 498 | + if ( ! \PoweredCache\Utils\is_dev_mode_active() ) { | |
| 499 | + return; | |
| 500 | + } | |
| 501 | + | |
| 502 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 503 | + return; | |
| 504 | + } | |
| 505 | + | |
| 506 | + // Only display on Powered Cache settings page | |
| 507 | + $current_screen = get_current_screen(); | |
| 508 | + | |
| 509 | + if ( | |
| 510 | + ! $current_screen | |
| 511 | + || ( ! isset( $current_screen->id ) || false === strpos( $current_screen->id, 'powered-cache' ) ) | |
| 512 | + ) { | |
| 513 | + return; | |
| 514 | + } | |
| 515 | + | |
| 516 | + ?> | |
| 517 | + <div class="notice notice-warning is-dismissible"> | |
| 518 | + <p><strong><?php esc_html_e( 'Development Mode is active.', 'powered-cache' ); ?></strong> | |
| 519 | + <?php esc_html_e( 'Caching and optimizations are currently disabled. Don’t forget to disable it when done.', 'powered-cache' ); ?> | |
| 520 | + </p> | |
| 521 | + </div> | |
| 301 | 522 | <?php |
| 302 | 523 | } |