← All changes
|
vendor/codeinwp/themeisle-sdk/src/Modules/Rollback.php
+51
-10
3.1.3
→
3.11.10
View file →
| @@ -12,8 +12,9 @@ | ||
| 12 | 12 | namespace ThemeisleSDK\Modules; |
| 13 | 13 | |
| 14 | 14 | // Exit if accessed directly. |
| 15 | 15 | use ThemeisleSDK\Common\Abstract_Module; |
| 16 | +use ThemeisleSDK\Loader; | |
| 16 | 17 | use ThemeisleSDK\Product; |
| 17 | 18 | |
| 18 | 19 | if ( ! defined( 'ABSPATH' ) ) { |
| 19 | 20 | exit; |
| @@ -97,9 +98,9 @@ | ||
| 97 | 98 | * @return array Array of versions. |
| 98 | 99 | */ |
| 99 | 100 | private function get_api_versions() { |
| 100 | 101 | |
| 101 | - $cache_key = $this->product->get_key() . '_' . preg_replace( '/[^0-9a-zA-Z ]/m', '', $this->product->get_version() ) . 'versions'; | |
| 102 | + $cache_key = $this->product->get_cache_key(); | |
| 102 | 103 | $cache_versions = get_transient( $cache_key ); |
| 103 | 104 | if ( false === $cache_versions ) { |
| 104 | 105 | $versions = $this->get_remote_versions(); |
| 105 | 106 | set_transient( $cache_key, $versions, 5 * DAY_IN_SECONDS ); |
| @@ -119,9 +120,11 @@ | ||
| 119 | 120 | $url = $this->get_versions_api_url(); |
| 120 | 121 | if ( empty( $url ) ) { |
| 121 | 122 | return []; |
| 122 | 123 | } |
| 123 | - $response = wp_remote_get( $url ); | |
| 124 | + $response = function_exists( 'vip_safe_wp_remote_get' ) | |
| 125 | + ? vip_safe_wp_remote_get( $url ) | |
| 126 | + : wp_remote_get( $url ); //phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get | |
| 124 | 127 | if ( is_wp_error( $response ) ) { |
| 125 | 128 | return array(); |
| 126 | 129 | } |
| 127 | 130 | $response = wp_remote_retrieve_body( $response ); |
| @@ -166,9 +169,9 @@ | ||
| 166 | 169 | if ( $this->product->requires_license() && strlen( $license ) < 10 ) { |
| 167 | 170 | return ''; |
| 168 | 171 | } |
| 169 | 172 | |
| 170 | - return sprintf( '%s?edd_action=get_versions&name=%s&url=%s&license=%s', $this->product->get_store_url(), urlencode( $this->product->get_name() ), urlencode( get_site_url() ), $license ); | |
| 173 | + return sprintf( '%slicense/versions/%s/%s/%s/%s', Product::API_URL, rawurlencode( $this->product->get_name() ), $license, urlencode( get_site_url() ), $this->product->get_version() ); | |
| 171 | 174 | } |
| 172 | 175 | |
| 173 | 176 | /** |
| 174 | 177 | * Show the rollback links in the plugin page. |
| @@ -181,9 +184,9 @@ | ||
| 181 | 184 | $version = $this->get_rollback(); |
| 182 | 185 | if ( empty( $version ) ) { |
| 183 | 186 | return $links; |
| 184 | 187 | } |
| 185 | - $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>'; | |
| 188 | + $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', Loader::$labels['rollback']['cta'] ), $version['version'] ) . '</a>'; | |
| 186 | 189 | |
| 187 | 190 | return $links; |
| 188 | 191 | } |
| 189 | 192 | |
| @@ -190,9 +193,9 @@ | ||
| 190 | 193 | /** |
| 191 | 194 | * Start the rollback operation. |
| 192 | 195 | */ |
| 193 | 196 | public function start_rollback() { |
| 194 | - if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], $this->product->get_key() . '_rollback' ) ) { | |
| 197 | + if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], $this->product->get_key() . '_rollback' ) ) { //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 195 | 198 | wp_nonce_ays( '' ); |
| 196 | 199 | } |
| 197 | 200 | |
| 198 | 201 | if ( $this->product->is_plugin() ) { |
| @@ -227,11 +230,14 @@ | ||
| 227 | 230 | set_site_transient( 'update_plugins', $plugin_transient ); |
| 228 | 231 | |
| 229 | 232 | $transient = get_transient( $this->product->get_key() . '_warning_rollback' ); |
| 230 | 233 | |
| 234 | + // Style fix for the api link that gets outside the content. | |
| 235 | + echo '<style>body#error-page{word-break:break-word;}</style>'; | |
| 236 | + | |
| 231 | 237 | if ( false === $transient ) { |
| 232 | 238 | set_transient( $this->product->get_key() . '_warning_rollback', 'in progress', 30 ); |
| 233 | - require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); | |
| 239 | + require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; | |
| 234 | 240 | $title = sprintf( apply_filters( $this->product->get_key() . '_rollback_message', 'Rolling back %s to v%s' ), $this->product->get_name(), $version ); |
| 235 | 241 | $plugin = $plugin_folder . '/' . $plugin_file; |
| 236 | 242 | $nonce = 'upgrade-plugin_' . $plugin; |
| 237 | 243 | $url = 'update.php?action=upgrade-plugin&plugin=' . urlencode( $plugin ); |
| @@ -240,9 +246,9 @@ | ||
| 240 | 246 | $upgrader->upgrade( $plugin ); |
| 241 | 247 | delete_transient( $this->product->get_key() . '_warning_rollback' ); |
| 242 | 248 | wp_die( |
| 243 | 249 | '', |
| 244 | - $title, | |
| 250 | + esc_attr( $title ), | |
| 245 | 251 | array( |
| 246 | 252 | 'response' => 200, |
| 247 | 253 | ) |
| 248 | 254 | ); |
| @@ -267,22 +273,41 @@ | ||
| 267 | 273 | set_site_transient( 'update_themes', $transient ); |
| 268 | 274 | |
| 269 | 275 | $transient = get_transient( $this->product->get_key() . '_warning_rollback' ); |
| 270 | 276 | |
| 277 | + // Style fix for the api link that gets outside the content. | |
| 278 | + echo '<style>body#error-page{word-break:break-word;}</style>'; | |
| 279 | + | |
| 271 | 280 | if ( false === $transient ) { |
| 272 | 281 | set_transient( $this->product->get_key() . '_warning_rollback', 'in progress', 30 ); |
| 273 | - require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); | |
| 282 | + require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; | |
| 274 | 283 | $title = sprintf( apply_filters( $this->product->get_key() . '_rollback_message', 'Rolling back %s to v%s' ), $this->product->get_name(), $version ); |
| 275 | 284 | $theme = $folder . '/style.css'; |
| 276 | 285 | $nonce = 'upgrade-theme_' . $theme; |
| 277 | 286 | $url = 'update.php?action=upgrade-theme&theme=' . urlencode( $theme ); |
| 278 | 287 | |
| 288 | + /** | |
| 289 | + * The rollback will attach a temporary theme for the rollback to the transient. | |
| 290 | + * However, when executing the upgrade for the attached theme we need to change the slug to the original theme slug. | |
| 291 | + * This is because it will use the slug to create a temp folder for the theme used during the upgrade. | |
| 292 | + */ | |
| 293 | + add_filter( | |
| 294 | + 'upgrader_package_options', | |
| 295 | + function ( $options ) use ( $folder, $theme ) { | |
| 296 | + if ( isset( $options['hook_extra']['theme'] ) && $options['hook_extra']['theme'] === $theme && isset( $options['hook_extra']['temp_backup']['slug'] ) ) { | |
| 297 | + $options['hook_extra']['temp_backup']['slug'] = $folder; | |
| 298 | + } | |
| 299 | + | |
| 300 | + return $options; | |
| 301 | + } | |
| 302 | + ); | |
| 303 | + | |
| 279 | 304 | $upgrader = new \Theme_Upgrader( new \Theme_Upgrader_Skin( compact( 'title', 'nonce', 'url', 'theme' ) ) ); |
| 280 | 305 | $upgrader->upgrade( $theme ); |
| 281 | 306 | delete_transient( $this->product->get_key() . '_warning_rollback' ); |
| 282 | 307 | wp_die( |
| 283 | 308 | '', |
| 284 | - $title, | |
| 309 | + esc_attr( $title ), | |
| 285 | 310 | array( |
| 286 | 311 | 'response' => 200, |
| 287 | 312 | ) |
| 288 | 313 | ); |
| @@ -334,9 +359,9 @@ | ||
| 334 | 359 | * |
| 335 | 360 | * @return bool Which version is greater? |
| 336 | 361 | */ |
| 337 | 362 | public function sort_rollback_array( $a, $b ) { |
| 338 | - return version_compare( $a['version'], $b['version'], '<' ) > 0; | |
| 363 | + return version_compare( $b['version'], $a['version'] ); | |
| 339 | 364 | } |
| 340 | 365 | |
| 341 | 366 | /** |
| 342 | 367 | * Load module logic. |
| @@ -366,11 +391,27 @@ | ||
| 366 | 391 | ); |
| 367 | 392 | } |
| 368 | 393 | |
| 369 | 394 | /** |
| 395 | + * Fires after the option has been updated. | |
| 396 | + * | |
| 397 | + * @param mixed $old_value The old option value. | |
| 398 | + * @param mixed $value The new option value. | |
| 399 | + * @param string $option Option name. | |
| 400 | + */ | |
| 401 | + public function update_active_plugins_action( $old_value, $value, $option ) { | |
| 402 | + delete_site_transient( 'update_plugins' ); | |
| 403 | + wp_cache_delete( 'plugins', 'plugins' ); | |
| 404 | + } | |
| 405 | + | |
| 406 | + /** | |
| 370 | 407 | * 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. |
| 371 | 408 | */ |
| 372 | 409 | public function add_hooks() { |
| 373 | 410 | add_action( 'admin_post_' . $this->product->get_key() . '_rollback', array( $this, 'start_rollback' ) ); |
| 374 | 411 | add_action( 'admin_footer', array( $this, 'add_footer' ) ); |
| 412 | + | |
| 413 | + // This hook will be invoked after the plugin activation. | |
| 414 | + // We use this to force an update of the cache so that Update is present immediate after a rollback. | |
| 415 | + add_action( 'update_option_active_plugins', array( $this, 'update_active_plugins_action' ), 10, 3 ); | |
| 375 | 416 | } |
| 376 | 417 | } |