Skip to content

Commit

Permalink
Super Cache: use "activate/install boost button" on the banner in oth…
Browse files Browse the repository at this point in the history
…er areas (#37824)

* Modify banner install code so it supports more buttons

* changelog

* Remove admin notice from here as it's in #37840

* Updated versions

* Remove dummy function required for testing

* Remove secondary button function. Different PR for that.

* Activate the plugin, not the source!

* Fix the path to the source

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/9545379479

Upstream-Ref: Automattic/jetpack@cd21d87
  • Loading branch information
donnchawp authored and matticbot committed Jun 17, 2024
1 parent 0036cee commit c156ff5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: modify boost install code so it can be used by multiple buttons
- Super Cache: notify Boost of migration to that plugin

### Changed
Expand Down
11 changes: 11 additions & 0 deletions 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 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 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

0 comments on commit c156ff5

Please sign in to comment.