| 1 |
<?php |
| 2 |
/** |
| 3 |
* The rollback class for ThemeIsle SDK |
| 4 |
* |
| 5 |
* @package ThemeIsleSDK |
| 6 |
* @subpackage Rollback |
| 7 |
* @copyright Copyright (c) 2017, Marius Cristea |
| 8 |
* @license http://opensource.org/licenses/gpl-3.0.php GNU Public License |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
// Exit if accessed directly. |
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
if ( ! class_exists( 'ThemeIsle_SDK_Rollback' ) ) : |
| 16 |
/** |
| 17 |
* Rollback for ThemeIsle SDK. |
| 18 |
*/ |
| 19 |
class ThemeIsle_SDK_Rollback { |
| 20 |
|
| 21 |
/** |
| 22 |
* @var ThemeIsle_SDK_Product $product Themeisle Product. |
| 23 |
*/ |
| 24 |
protected $product; |
| 25 |
|
| 26 |
|
| 27 |
/** |
| 28 |
* ThemeIsle_SDK_Rollback constructor. |
| 29 |
* |
| 30 |
* @param ThemeIsle_SDK_Product $product_object Product Object. |
| 31 |
*/ |
| 32 |
public function __construct( $product_object ) { |
| 33 |
if ( $product_object instanceof ThemeIsle_SDK_Product ) { |
| 34 |
$this->product = $product_object; |
| 35 |
} |
| 36 |
if ( $this->product->can_rollback() ) { |
| 37 |
$this->show_link(); |
| 38 |
$this->add_hooks(); |
| 39 |
|
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Add js scripts for themes rollback. |
| 45 |
*/ |
| 46 |
public function add_footer() { |
| 47 |
$screen = get_current_screen(); |
| 48 |
if ( ! isset( $screen->parent_file ) ) { |
| 49 |
return; |
| 50 |
} |
| 51 |
if ( $screen->parent_file !== 'themes.php' ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
if ( $this->product->get_type() === 'plugin' ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
$version = $this->product->get_rollback(); |
| 59 |
?> |
| 60 |
<script type="text/javascript"> |
| 61 |
jQuery(document).ready(function ($) { |
| 62 |
setInterval(checkTheme, 500); |
| 63 |
|
| 64 |
function checkTheme() { |
| 65 |
var theme = '<?php echo esc_attr( $this->product->get_slug() ); ?>-action'; |
| 66 |
|
| 67 |
if (jQuery('#' + theme).length > 0) { |
| 68 |
if (jQuery('.theme-overlay.active').is(':visible')) { |
| 69 |
if (jQuery('#' + theme + '-rollback').length === 0) { |
| 70 |
jQuery('.theme-actions .active-theme').prepend('<a class="button" style="float:left" id="' + theme + '-rollback" href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin-post.php?action=' . $this->product->get_key() . '_rollback' ), $this->product->get_key() . '_rollback' ) ); ?>">Rollback to v<?php echo esc_attr( $version['version'] ); ?></a>') |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
} |
| 75 |
} |
| 76 |
}) |
| 77 |
|
| 78 |
</script> |
| 79 |
<?php |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Set the rollback hook. Strangely, this does not work if placed in the ThemeIsle_SDK_Rollback class, so it is being called from there instead. |
| 85 |
*/ |
| 86 |
public function add_hooks() { |
| 87 |
add_action( 'admin_post_' . $this->product->get_key() . '_rollback', array( $this, 'start_rollback' ) ); |
| 88 |
add_action( 'admin_footer', array( $this, 'add_footer' ) ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* If product can be rolled back, show the link to rollback. |
| 93 |
*/ |
| 94 |
private function show_link() { |
| 95 |
add_filter( |
| 96 |
'plugin_action_links_' . plugin_basename( $this->product->get_basefile() ), array( |
| 97 |
$this, |
| 98 |
'add_rollback_link', |
| 99 |
) |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Show the rollback links in the plugin page. |
| 105 |
* |
| 106 |
* @return array The links. |
| 107 |
*/ |
| 108 |
public function add_rollback_link( $links ) { |
| 109 |
$version = $this->product->get_rollback(); |
| 110 |
$links[] = '<a href="' . wp_nonce_url( admin_url( 'admin-post.php?action=' . $this->product->get_key() . '_rollback' ), $this->product->get_key() . '_rollback' ) . '">' . sprintf( apply_filters( $this->product->get_key() . '_rollback_label', 'Rollback to v%s' ), $version['version'] ) . '</a>'; |
| 111 |
|
| 112 |
return $links; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Start the rollback operation. |
| 117 |
*/ |
| 118 |
public function start_rollback() { |
| 119 |
if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], $this->product->get_key() . '_rollback' ) ) { |
| 120 |
wp_nonce_ays( '' ); |
| 121 |
} |
| 122 |
|
| 123 |
if ( $this->product->get_type() === 'plugin' ) { |
| 124 |
$this->start_rollback_plugin(); |
| 125 |
} elseif ( $this->product->get_type() === 'theme' ) { |
| 126 |
$this->start_rollback_theme(); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Alter links and remove duplicate customize message. |
| 132 |
* |
| 133 |
* @param array $links Array of old links. |
| 134 |
* |
| 135 |
* @return mixed Array of links. |
| 136 |
*/ |
| 137 |
public function alter_links_theme_upgrade( $links ) { |
| 138 |
if ( isset( $links['preview'] ) ) { |
| 139 |
$links['preview'] = str_replace( '<span aria-hidden="true">Customize</span>', '', $links['preview'] ); |
| 140 |
} |
| 141 |
|
| 142 |
return $links; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Start the rollback operation for the theme. |
| 147 |
*/ |
| 148 |
private function start_rollback_theme() { |
| 149 |
add_filter( 'update_theme_complete_actions', array( $this, 'alter_links_theme_upgrade' ) ); |
| 150 |
$rollback = $this->product->get_rollback(); |
| 151 |
$transient = get_site_transient( 'update_themes' ); |
| 152 |
$folder = $this->product->get_slug(); |
| 153 |
$version = $rollback['version']; |
| 154 |
$temp_array = array( |
| 155 |
'new_version' => $version, |
| 156 |
'package' => $rollback['url'], |
| 157 |
); |
| 158 |
|
| 159 |
$transient->response[ $folder . '/style.css' ] = $temp_array; |
| 160 |
set_site_transient( 'update_themes', $transient ); |
| 161 |
|
| 162 |
$transient = get_transient( $this->product->get_key() . '_warning_rollback' ); |
| 163 |
|
| 164 |
if ( false === $transient ) { |
| 165 |
set_transient( $this->product->get_key() . '_warning_rollback', 'in progress', 30 ); |
| 166 |
require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); |
| 167 |
$title = sprintf( apply_filters( $this->product->get_key() . '_rollback_message', 'Rolling back %s to v%s' ), $this->product->get_name(), $version ); |
| 168 |
$theme = $folder . '/style.css'; |
| 169 |
$nonce = 'upgrade-theme_' . $theme; |
| 170 |
$url = 'update.php?action=upgrade-theme&theme=' . urlencode( $theme ); |
| 171 |
|
| 172 |
$upgrader = new Theme_Upgrader( new Theme_Upgrader_Skin( compact( 'title', 'nonce', 'url', 'theme' ) ) ); |
| 173 |
$upgrader->upgrade( $theme ); |
| 174 |
delete_transient( $this->product->get_key() . '_warning_rollback' ); |
| 175 |
wp_die( |
| 176 |
'', $title, array( |
| 177 |
'response' => 200, |
| 178 |
) |
| 179 |
); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Start the rollback operation for the plugin. |
| 185 |
*/ |
| 186 |
private function start_rollback_plugin() { |
| 187 |
$rollback = $this->product->get_rollback(); |
| 188 |
$plugin_transient = get_site_transient( 'update_plugins' ); |
| 189 |
$plugin_folder = $this->product->get_slug(); |
| 190 |
$plugin_file = $this->product->get_file(); |
| 191 |
$version = $rollback['version']; |
| 192 |
$temp_array = array( |
| 193 |
'slug' => $plugin_folder, |
| 194 |
'new_version' => $version, |
| 195 |
'package' => $rollback['url'], |
| 196 |
); |
| 197 |
|
| 198 |
$temp_object = (object) $temp_array; |
| 199 |
$plugin_transient->response[ $plugin_folder . '/' . $plugin_file ] = $temp_object; |
| 200 |
set_site_transient( 'update_plugins', $plugin_transient ); |
| 201 |
|
| 202 |
$transient = get_transient( $this->product->get_key() . '_warning_rollback' ); |
| 203 |
|
| 204 |
if ( false === $transient ) { |
| 205 |
set_transient( $this->product->get_key() . '_warning_rollback', 'in progress', 30 ); |
| 206 |
require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); |
| 207 |
$title = sprintf( apply_filters( $this->product->get_key() . '_rollback_message', 'Rolling back %s to v%s' ), $this->product->get_name(), $version ); |
| 208 |
$plugin = $plugin_folder . '/' . $plugin_file; |
| 209 |
$nonce = 'upgrade-plugin_' . $plugin; |
| 210 |
$url = 'update.php?action=upgrade-plugin&plugin=' . urlencode( $plugin ); |
| 211 |
$upgrader_skin = new Plugin_Upgrader_Skin( compact( 'title', 'nonce', 'url', 'plugin' ) ); |
| 212 |
$upgrader = new Plugin_Upgrader( $upgrader_skin ); |
| 213 |
$upgrader->upgrade( $plugin ); |
| 214 |
delete_transient( $this->product->get_key() . '_warning_rollback' ); |
| 215 |
wp_die( |
| 216 |
'', $title, array( |
| 217 |
'response' => 200, |
| 218 |
) |
| 219 |
); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
endif; |
| 224 |
|