Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Super Cache: use "activate/install boost button" on the banner in other areas #37824

Merged
merged 10 commits into from
Jun 17, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Super Cache: modify boost install code so it can be used by multiple buttons
11 changes: 11 additions & 0 deletions projects/plugins/super-cache/inc/boost.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<?php

/**
* Get shared configuration for each migration button.
*/
function wpsc_get_boost_migration_config() {
return array(
'install_url' => wp_nonce_url( admin_url( 'update.php?action=install-plugin&plugin=jetpack-boost' ), 'install-plugin_jetpack-boost' ),
'activate_url' => admin_url( 'plugins.php' ),
'is_installed' => wpsc_is_boost_installed(),
);
}

/**
* Add a notice to the settings page if the Jetpack Boost cache module is detected.
* The notice contains instructions on how to disable the Boost Cache module.
Expand Down
13 changes: 8 additions & 5 deletions projects/plugins/super-cache/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ jQuery( document ).ready( () => {
} );

// One-click install for Boost.
jQuery( '#wpsc-install-boost-button' ).on( 'click', event => {
jQuery( '.wpsc-install-boost-button' ).on( 'click', event => {
const source = jQuery( event.currentTarget ).attr( 'data-source' );
event.preventDefault();
showBoostBannerBusy( __( 'Installing…', 'wp-super-cache' ) );

Expand All @@ -36,7 +37,7 @@ jQuery( document ).ready( () => {
} )
.done( response => {
if ( response.success ) {
activateBoost();
activateBoost( source );
} else {
showBoostBannerError( response.data );
}
Expand All @@ -53,9 +54,10 @@ jQuery( document ).ready( () => {
} );

// Handle activate button click.
jQuery( '#wpsc-activate-boost-button' ).on( 'click', event => {
jQuery( '.wpsc-activate-boost-button' ).on( 'click', event => {
const source = jQuery( event.currentTarget ).attr( 'data-source' );
event.preventDefault();
activateBoost();
activateBoost( source );
} );

// Helper function to show Boost Banner work in progress.
Expand Down Expand Up @@ -89,13 +91,14 @@ jQuery( document ).ready( () => {
};

// Activate Jetpack Boost.
const activateBoost = () => {
const activateBoost = source => {
showBoostBannerBusy( __( 'Activating…', 'wp-super-cache' ) );

jQuery
.post( ajaxurl, {
action: 'wpsc_activate_boost',
_ajax_nonce: wpscAdmin.boostActivateNonce,
source: source,
} )
.done( response => {
if ( response.success ) {
Expand Down
22 changes: 13 additions & 9 deletions projects/plugins/super-cache/wp-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,18 @@ function wpsc_hide_boost_banner() {
function wpsc_ajax_activate_boost() {
check_ajax_referer( 'activate-boost' );

if ( ! isset( $_POST['source'] ) ) {
wp_send_json_error( 'no source specified' );
}

$source = sanitize_text_field( wp_unslash( $_POST['source'] ) );
$result = activate_plugin( 'jetpack-boost/jetpack-boost.php' );
if ( is_wp_error( $result ) ) {
wp_send_json_error( $result->get_error_message() );
}

wpsc_notify_migration_to_boost( $source );

wp_send_json_success();
}
add_action( 'wp_ajax_wpsc_activate_boost', 'wpsc_ajax_activate_boost' );
Expand All @@ -390,12 +397,10 @@ function wpsc_jetpack_boost_install_banner() {
return;
}

$install_url = wp_nonce_url( admin_url( 'update.php?action=install-plugin&plugin=jetpack-boost' ), 'install-plugin_jetpack-boost' );
$activate_url = admin_url( 'plugins.php' );
$is_installed = wpsc_is_boost_installed();
$button_url = $is_installed ? $activate_url : $install_url;
$button_label = $is_installed ? __( 'Activate Jetpack Boost', 'wp-super-cache' ) : __( 'Install Jetpack Boost', 'wp-super-cache' );
$button_id = $is_installed ? 'wpsc-activate-boost-button' : 'wpsc-install-boost-button';
$config = wpsc_get_boost_migration_config();
$button_url = $config['is_installed'] ? $config['activate_url'] : $config['install_url'];
$button_label = $config['is_installed'] ? __( 'Activate Jetpack Boost', 'wp-super-cache' ) : __( 'Install Jetpack Boost', 'wp-super-cache' );
$button_class = $config['is_installed'] ? 'wpsc-activate-boost-button' : 'wpsc-install-boost-button';
$plugin_url = plugin_dir_url( __FILE__ );

?>
Expand All @@ -419,7 +424,7 @@ function wpsc_jetpack_boost_install_banner() {

<div id="wpsc-boost-banner-error" style="display:none; color:red; margin-bottom: 20px;"></div>

<a href="<?php echo esc_url( $button_url ); ?>" class="button button-primary wpsc-install-action-button" id="<?php echo esc_attr( $button_id ); ?>">
<a data-source='banner' href="<?php echo esc_url( $button_url ); ?>" class="button button-primary wpsc-install-action-button <?php echo esc_attr( $button_class ); ?>">
<div class="spinner" style="display:none; margin-top: 8px"></div>
<label><?php echo esc_html( $button_label ); ?></label>
</a>
Expand Down Expand Up @@ -2078,7 +2083,6 @@ function wpsc_config_file_notices() {
echo '<div class="error"><p><strong>' . $msg . '</strong></p></div>';
}
add_action( 'admin_notices', 'wpsc_config_file_notices' );

function wpsc_dismiss_indexhtml_warning() {
check_ajax_referer( "wpsc-index-dismiss" );
update_site_option( 'wp_super_cache_index_detected', 3 );
Expand Down Expand Up @@ -2799,7 +2803,7 @@ function wp_cache_clean_legacy_files( $dir, $file_prefix ) {
@unlink( $dir . 'meta/' . str_replace( '.html', '.meta', $file ) );
} else {
$meta = json_decode( wp_cache_get_legacy_cache( $dir . 'meta/' . $file ), true );
if ( $curr_blog_id && $curr_blog_id !== (int)$meta['blog_id'] ) {
if ( $curr_blog_id && $curr_blog_id !== (int) $meta['blog_id'] ) {
continue;
}
@unlink( $dir . $file);
Expand Down