This commit is contained in:
Mike Jolley 2018-06-25 12:43:47 +01:00
commit c67e46e8cf
3 changed files with 132 additions and 5 deletions

View File

@ -0,0 +1,73 @@
<?php
/**
* WooCommerce Beta Tester Plugin Upgrader
*
* Class that extends the WP Core Plugin_Upgrader found in core to do version switch.
*
* @package WC_Beta_Tester
*/
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'Plugin_Upgrader' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
}
/**
* Class WC_Beta_Tester_Plugin_Upgrader
*/
class WC_Beta_Tester_Plugin_Upgrader extends Plugin_Upgrader {
/**
* Switch plugin version.
*
* @param string $plugin Plugin we're switching.
* @param array $args Args.
*
* @return array|bool|\WP_Error
*/
public function switch_version( $plugin, $args = array() ) {
$defaults = array(
'clear_update_cache' => true,
);
$parsed_args = wp_parse_args( $args, $defaults );
$this->init();
$this->upgrade_strings();
$plugin_version = $this->skin->options['version'];
$download_url = WC_Beta_Tester::instance()->get_download_url( $plugin_version );
add_filter( 'upgrader_pre_install', array( $this, 'deactivate_plugin_before_upgrade' ), 10, 2 );
add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ), 10, 4 );
$this->run( array(
'package' => $download_url,
'destination' => WP_PLUGIN_DIR,
'clear_destination' => true,
'clear_working' => true,
'hook_extra' => array(
'plugin' => $plugin,
'type' => 'plugin',
'action' => 'update',
),
) );
// Cleanup our hooks, in case something else does a upgrade on this connection.
remove_filter( 'upgrader_pre_install', array( $this, 'deactivate_plugin_before_upgrade' ) );
remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ) );
if ( ! $this->result || is_wp_error( $this->result ) ) {
return $this->result;
}
// Force refresh of plugin update information.
wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
return true;
}
}

View File

@ -24,6 +24,51 @@ class WC_Beta_Tester_Version_Picker {
*/
public function __construct() {
add_action( 'admin_menu', array( $this, 'add_to_menus' ) );
add_action( 'admin_init', array( $this, 'handle_version_switch' ) );
}
/**
* Handler for the version switch button.
*/
public function handle_version_switch() {
if ( ! isset( $_GET['wcbt_switch_to_version'], $_GET['_wpnonce'] ) ) { // WPCS: Input var ok.
return;
}
if ( ! wp_verify_nonce( wp_unslash( $_GET['_wpnonce'] ), 'wcbt_switch_version_nonce' ) ) { // WPCS: Input var ok, sanitization ok.
wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'woocommerce-beta-tester' ) );
}
$version = isset( $_GET['wcbt_switch_to_version'] ) ? sanitize_text_field( wp_unslash( $_GET['wcbt_switch_to_version'] ) ) : ''; // WPCS: Input var ok, sanitization ok.
if ( empty( $version ) ) {
return;
}
include dirname( __FILE__ ) . '/class-wc-beta-tester-plugin-upgrader.php';
$plugin_name = 'woocommerce';
$plugin = 'woocommerce/woocommerce.php';
$skin_args = array(
'type' => 'web',
'url' => 'tools.php?page=wc-beta-tester-version-picker',
'title' => 'Version switch result',
'plugin' => $plugin_name,
'version' => $version,
'nonce' => wp_unslash( $_GET['_wpnonce'] ), // WPCS: Input var ok, sanitization ok.
);
$skin = new Automatic_Upgrader_Skin( $skin_args );
$upgrader = new WC_Beta_Tester_Plugin_Upgrader( $skin );
$result = $upgrader->switch_version( $plugin );
if ( $result ) {
activate_plugin( $plugin, '', is_network_admin(), true );
wp_redirect( admin_url( 'tools.php?page=wc-beta-tester-version-picker&switched=' . rawurlencode( $version ) ) );
} else {
// TODO: fail more gracefully.
print_r( $skin->get_upgrade_messages() );
}
}
/**
@ -55,9 +100,15 @@ class WC_Beta_Tester_Version_Picker {
}
usort( $tags, 'version_compare' );
$tags = array_reverse( $tags );
$tags = array_reverse( $tags );
$versions_html = '';
$versions_html = '<ul class="wcbt-version-list">';
if ( ! empty( $_GET['switched'] ) ) { // WPCS: input var ok, CSRF ok.
/* translators: %s: WooCoomerce version */
$versions_html .= '<h2>' . sprintf( esc_html__( 'Successfully switched version to %s.', 'woocommerce-beta-tester' ), esc_html( sanitize_text_field( wp_unslash( $_GET['switched'] ) ) ) ) . '</h2>'; // WPCS: input var ok, CSRF ok.
}
$versions_html .= '<ul class="wcbt-version-list">';
$plugin_data = WC_Beta_Tester::instance()->get_plugin_data();
$this->current_version = $plugin_data['Version'];
@ -91,11 +142,12 @@ class WC_Beta_Tester_Version_Picker {
$settings = WC_Beta_Tester::get_settings();
$channel = $settings->channel;
?>
<div class="wrap">
<div class="wcbt-content-wrap">
<h1><?php esc_html_e( 'Available WooCommerce Releases', 'woocommerce-beta-tester' ); ?></h1>
<form name="wcbt-select-version" class="wcbt-select-version-form" action="<?php echo esc_attr( admin_url( '/index.php' ) ); ?>">
<form name="wcbt-select-version" class="wcbt-select-version-form" action="<?php echo esc_attr( admin_url( '/tools.php' ) ); ?>">
<div class="wcbt-versions-wrap">
<?php echo $this->get_versions_html( $channel ); // WPCS: XSS ok. ?>
</div>
@ -103,6 +155,8 @@ class WC_Beta_Tester_Version_Picker {
<a href="#wcbt-modal-version-switch-confirm" class="button-primary" id="wcbt-modal-version-switch-confirm"><?php esc_html_e( 'Switch version', 'woocommerce-beta-tester' ); ?></a>
</div>
<?php wp_nonce_field( 'wcbt_switch_version_nonce' ); ?>
<input type="hidden" name="noheader" value="1">
<input type="hidden" name="page" value="wc-beta-tester-version-picker">
<script type="text/template" id="tmpl-wcbt-version-switch-confirm">
<div class="wc-backbone-modal wc-backbone-modal-beta-tester-version-info">
@ -140,7 +194,7 @@ class WC_Beta_Tester_Version_Picker {
<p class="wcbt-notice"><?php esc_html_e( 'Notice: We strongly recommend you perform the test on a staging site and create a complete backup of your WordPress files and database prior to performing a version switch. We are not responsible for any misuse, deletions, white screens, fatal errors, or any other issue arising from using this plugin.', 'woocommerce-beta-tester' ); ?></p>
</article>
<footer>
<input type="submit" value="<?php esc_attr_e( 'Switch version', 'woocommerce-beta-tester' ); ?>" class="button-primary wcbt-go" id="wcbt-submit-version-switch" />&nbsp;
<input type="submit" value="<?php esc_attr_e( 'Switch version', 'woocommerce-beta-tester' ); ?>" class="button-primary wcbt-go" id="wcbt-submit-version-switch"/>&nbsp;
<a href="#" class="modal-close modal-close-link"><?php esc_attr_e( 'Cancel', 'woocommerce-beta-tester' ); ?></a>
</footer>
</section>

View File

@ -19,7 +19,7 @@ defined( 'ABSPATH' ) || exit;
<?php if ( ! is_plugin_active( 'woocommerce/woocommerce.php' ) && current_user_can( 'activate_plugin', 'woocommerce/woocommerce.php' ) ) : ?>
<p>
<a href="<?php echo esc_url( wp_nonce_url( self_admin_url( 'plugins.php?action=activate&plugin=woocommerce/woocommerce.php&plugin_status=active' ), 'activate-plugin_woocommerce/woocommerce.php' ) ); ?>" class="button button-primary"><?php esc_html_e( 'Active WooCommerce', 'woocommerce-beta-tester' ); ?></a>
<a href="<?php echo esc_url( wp_nonce_url( self_admin_url( 'plugins.php?action=activate&plugin=woocommerce/woocommerce.php&plugin_status=active' ), 'activate-plugin_woocommerce/woocommerce.php' ) ); ?>" class="button button-primary"><?php esc_html_e( 'Activate WooCommerce', 'woocommerce-beta-tester' ); ?></a>
<?php if ( current_user_can( 'deactivate_plugin', 'woocommerce-beta-tester/woocommerce-beta-tester.php' ) ) : ?>
<a href="<?php echo esc_url( wp_nonce_url( 'plugins.php?action=deactivate&plugin=woocommerce-beta-tester/woocommerce-beta-tester.php&plugin_status=inactive', 'deactivate-plugin_woocommerce-beta-tester/woocommerce-beta-tester.php' ) ); ?>" class="button button-secondary"><?php esc_html_e( 'Turn off Beta Tester plugin', 'woocommerce-beta-tester' ); ?></a>
<?php endif; ?>