From 17ee7adff412736af5bbd89f6cb3b8344a172cb6 Mon Sep 17 00:00:00 2001 From: donnchawp Date: Thu, 20 Jun 2024 08:24:43 +0000 Subject: [PATCH] Super Cache: add an admin notice to encourage migration to Jetpack Boost (#37933) * Add an admin notice to encourage migration to Jetpack Boost * changelog * Boost age check only applies if Boost is installed. * Add boost error and spinner to admin notice * Rename banner error to migration error. * Update animations so they work on any migration button * Place JS in a function to avoid polluting global variables * Add a MINIMUM_BOOST_VERSION cosntant for that Boost version * Fixes for static analysis checks * Updated phan baseline * Put it in an anon function * Update the button handling code. Yes, that's a terrible commit message, but it's a huge overhaul of the JavaScript, thank you Nauris. * Remove this class, it's not needed any more * Dismiss the admin notice if Jetpack Boost is activated by migration * Move the supercache settings page check into the notice. Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/9593905304 Upstream-Ref: Automattic/jetpack@191638562fe81f2e94173a1b2836d555a7b569b6 --- CHANGELOG.md | 1 + inc/boost.php | 171 ++++++++++++++++++++++++++++++ js/admin.js | 240 +++++++++++++++++++++++------------------- styling/dashboard.css | 11 ++ wp-cache.php | 11 +- 5 files changed, 321 insertions(+), 113 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 667bc779..55b17437 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 This is an alpha version! The changes listed here are not final. ### Added +- Super Cache: add an admin notice to encourage migration to Jetpack Boost - Super Cache: modify boost install code so it can be used by multiple buttons - Super Cache: notify Boost of migration to that plugin diff --git a/inc/boost.php b/inc/boost.php index ea3bf37e..ae000a82 100644 --- a/inc/boost.php +++ b/inc/boost.php @@ -1,4 +1,14 @@ +
+

+ +

+

+ +

+ +

+

+ + + + +

+
+ =' ); + } else { + return true; // don't care if Boost is not installed + } } \ No newline at end of file diff --git a/js/admin.js b/js/admin.js index 7d1715a1..f6621f22 100644 --- a/js/admin.js +++ b/js/admin.js @@ -1,122 +1,146 @@ -jQuery( document ).ready( () => { - // Don't run on versions of WordPress too old for the block editor and the translation methods it brings. - // All the install / activate options are plain links with meaningful destinations anyway. - if ( ! window.wp || ! window.wp.i18n ) { - return; - } +/** + * Handle the buttons for the Boost migration. + * @param {jQuery} $ - jQuery + */ +( $ => { + $( document ).ready( function () { + // Don't run on versions of WordPress too old for the block editor and the translation methods it brings. + // All the install / activate options are plain links with meaningful destinations anyway. + if ( ! window.wp || ! window.wp.i18n ) { + return; + } - const { __, sprintf } = window.wp.i18n; - const ajaxurl = window.ajaxurl; - const wpscAdmin = window.wpscAdmin; + const { __, sprintf } = window.wp.i18n; + const ajaxurl = window.ajaxurl; + const wpscAdmin = window.wpscAdmin; - const link = jQuery( '.wpsc-install-action-button' ); - const label = link.find( 'label' ); - const spinner = link.find( '.spinner' ); + const setupBoostButton = $target => { + if ( ! $target.hasClass( 'wpsc-boost-migration-button' ) ) { + // eslint-disable-next-line no-console + console.warn( 'Unexpected button clicked for Boost migration.' ); + return; + } + const $label = $target.find( 'label' ); + const $spinner = $target.find( '.spinner' ); + const $errorMessage = $target.prev( '.wpsc-boost-migration-error' ); + const source = $target.attr( 'data-source' ); + const originalText = $label.text(); - // Dismiss Boost banner. - jQuery( '.wpsc-boost-dismiss' ).on( 'click', function () { - jQuery( '.wpsc-boost-banner' ).fadeOut( 'slow' ); + // Helper function to show an error. + const showError = err => { + reset(); - jQuery.post( ajaxurl, { - action: 'wpsc-hide-boost-banner', - nonce: wpscAdmin.boostDismissNonce, - } ); - } ); + $errorMessage + .text( + err || + __( 'An error occurred while trying to activate Jetpack Boost', 'wp-super-cache' ) + ) + .show(); + }; + // Helper function to show Boost Banner work in progress. + const showBusy = action => { + $target.attr( 'disabled', true ); + $label.text( action ); + $spinner.addClass( 'is-active' ).show(); + }; - // One-click install for Boost. - jQuery( '.wpsc-install-boost-button' ).on( 'click', event => { - const source = jQuery( event.currentTarget ).attr( 'data-source' ); - event.preventDefault(); - showBoostBannerBusy( __( 'Installing…', 'wp-super-cache' ) ); + // Helper function to reset Boost Banner button. + const reset = () => { + $target.attr( 'disabled', false ); + $label.text( originalText ); + $spinner.removeClass( 'is-active' ).hide(); + }; - jQuery - .post( ajaxurl, { - action: 'wpsc_install_plugin', - _ajax_nonce: wpscAdmin.boostInstallNonce, - slug: 'jetpack-boost', - } ) - .done( response => { - if ( response.success ) { - activateBoost( source ); - } else { - showBoostBannerError( response.data ); - } - } ) - .fail( response => { - showBoostBannerError( - sprintf( - /* translators: %d is an HTTP error code */ - __( 'Failed to install Jetpack Boost: HTTP %d error received', 'wp-super-cache' ), - response.status - ) - ); - } ); - } ); + // Activate Jetpack Boost. + const activateBoost = () => { + showBusy( __( 'Activating…', 'wp-super-cache' ) ); - // Handle activate button click. - jQuery( '.wpsc-activate-boost-button' ).on( 'click', event => { - const source = jQuery( event.currentTarget ).attr( 'data-source' ); - event.preventDefault(); - activateBoost( source ); - } ); + $.post( ajaxurl, { + action: 'wpsc_activate_boost', + _ajax_nonce: wpscAdmin.boostActivateNonce, + source: source, + } ) + .done( response => { + if ( response.success ) { + $label.text( 'Success! Sending you to Jetpack Boost...' ); + $spinner.hide(); + window.location.href = 'admin.php?page=jetpack-boost'; + } else { + showError( response.data ); + } + } ) + .fail( response => { + showError( + sprintf( + /* translators: %d is an HTTP error code */ + __( 'Failed to activate Jetpack Boost: HTTP %d error received', 'wp-super-cache' ), + response.status + ) + ); + } ); + }; - // Helper function to show Boost Banner work in progress. - const showBoostBannerBusy = action => { - link.attr( 'disabled', true ); - label.text( action ); - spinner.addClass( 'is-active' ).show(); - }; + const installBoost = () => { + showBusy( __( 'Installing…', 'wp-super-cache' ) ); + $.post( ajaxurl, { + action: 'wpsc_install_plugin', + _ajax_nonce: wpscAdmin.boostInstallNonce, + slug: 'jetpack-boost', + } ) + .done( response => { + if ( response.success ) { + activateBoost(); + } else { + showError( response.data ); + } + } ) + .fail( response => { + showError( + sprintf( + /* translators: %d is an HTTP error code */ + __( 'Failed to install Jetpack Boost: HTTP %d error received', 'wp-super-cache' ), + response.status + ) + ); + } ); + }; - // Helper function to reset Boost Banner button. - const resetBoostBannerButton = () => { - link.attr( 'disabled', false ); - jQuery( '#wpsc-activate-boost-button' ) - .find( 'label' ) - .text( __( 'Activate Jetpack Boost', 'wp-super-cache' ) ); - jQuery( '#wpsc-install-boost-button' ) - .find( 'label' ) - .text( __( 'Install Jetpack Boost', 'wp-super-cache' ) ); - spinner.removeClass( 'is-active' ).hide(); - }; + return { + installBoost, + activateBoost, + }; + }; - // Helper function to show an error. - const showBoostBannerError = err => { - resetBoostBannerButton(); + // One-click install for Boost. + $( '.wpsc-install-boost-button' ).on( 'click', event => { + event.preventDefault(); + const boostActivation = setupBoostButton( $( event.currentTarget ) ); + boostActivation.installBoost(); + } ); - jQuery( '#wpsc-boost-banner-error' ) - .text( - err || __( 'An error occurred while trying to activate Jetpack Boost', 'wp-super-cache' ) - ) - .show(); - }; + // Handle activate button click. + $( '.wpsc-activate-boost-button' ).on( 'click', event => { + event.preventDefault(); + const boostActivation = setupBoostButton( $( event.currentTarget ) ); + boostActivation.activateBoost(); + } ); - // Activate Jetpack Boost. - const activateBoost = source => { - showBoostBannerBusy( __( 'Activating…', 'wp-super-cache' ) ); + // Dismiss Boost banner. + $( '.wpsc-boost-dismiss' ).on( 'click', () => { + $( '.wpsc-boost-banner' ).fadeOut( 'slow' ); + $.post( ajaxurl, { + action: 'wpsc-hide-boost-banner', + nonce: wpscAdmin.boostDismissNonce, + } ); + } ); - jQuery - .post( ajaxurl, { - action: 'wpsc_activate_boost', - _ajax_nonce: wpscAdmin.boostActivateNonce, - source: source, - } ) - .done( response => { - if ( response.success ) { - label.text( 'Success! Sending you to Jetpack Boost...' ); - spinner.hide(); - window.location.href = 'admin.php?page=jetpack-boost'; - } else { - showBoostBannerError( response.data ); - } - } ) - .fail( response => { - showBoostBannerError( - sprintf( - /* translators: %d is an HTTP error code */ - __( 'Failed to activate Jetpack Boost: HTTP %d error received', 'wp-super-cache' ), - response.status - ) - ); + // Dismiss admin notice + $( '.boost-notice' ).on( 'click', '.notice-dismiss', event => { + event.preventDefault(); + $.post( ajaxurl, { + action: 'wpsc_dismiss_boost_notice', + _ajax_nonce: wpscAdmin.boostNoticeDismissNonce, } ); - }; -} ); + } ); + } ); +} )( jQuery ); diff --git a/styling/dashboard.css b/styling/dashboard.css index 6c108591..406f4380 100644 --- a/styling/dashboard.css +++ b/styling/dashboard.css @@ -173,3 +173,14 @@ width: 190px; height: auto; } + +#wpsc-notice-boost-migrate { + width: 80%; + margin: 0 auto; + margin-top: 10px; + + a.button.button-primary { + background-color: var(--jp-black); + color: var(--jp-white); + } +} diff --git a/wp-cache.php b/wp-cache.php index ee74b811..23dcce0a 100644 --- a/wp-cache.php +++ b/wp-cache.php @@ -319,9 +319,10 @@ function wp_super_cache_admin_enqueue_scripts( $hook ) { 'wp-super-cache-admin', 'wpscAdmin', array( - 'boostDismissNonce' => wp_create_nonce( 'wpsc_dismiss_boost_banner' ), - 'boostInstallNonce' => wp_create_nonce( 'updates' ), - 'boostActivateNonce' => wp_create_nonce( 'activate-boost' ), + 'boostNoticeDismissNonce' => wp_create_nonce( 'wpsc_dismiss_boost_notice' ), + 'boostDismissNonce' => wp_create_nonce( 'wpsc_dismiss_boost_banner' ), + 'boostInstallNonce' => wp_create_nonce( 'updates' ), + 'boostActivateNonce' => wp_create_nonce( 'activate-boost' ), ) ); } @@ -422,9 +423,9 @@ function wpsc_jetpack_boost_install_banner() { ?>

- + - +