← All changes
|
vendor/codeinwp/themeisle-sdk/src/Modules/Rollback.php
+9
-50
3.10.13
→
3.4.7
View file →
| @@ -12,9 +12,8 @@ | ||
| 12 | 12 | namespace ThemeisleSDK\Modules; |
| 13 | 13 | |
| 14 | 14 | // Exit if accessed directly. |
| 15 | 15 | use ThemeisleSDK\Common\Abstract_Module; |
| 16 | -use ThemeisleSDK\Loader; | |
| 17 | 16 | use ThemeisleSDK\Product; |
| 18 | 17 | |
| 19 | 18 | if ( ! defined( 'ABSPATH' ) ) { |
| 20 | 19 | exit; |
| @@ -98,9 +97,9 @@ | ||
| 98 | 97 | * @return array Array of versions. |
| 99 | 98 | */ |
| 100 | 99 | private function get_api_versions() { |
| 101 | 100 | |
| 102 | - $cache_key = $this->product->get_cache_key(); | |
| 101 | + $cache_key = $this->product->get_key() . '_' . preg_replace( '/[^0-9a-zA-Z ]/m', '', $this->product->get_version() ) . 'versions'; | |
| 103 | 102 | $cache_versions = get_transient( $cache_key ); |
| 104 | 103 | if ( false === $cache_versions ) { |
| 105 | 104 | $versions = $this->get_remote_versions(); |
| 106 | 105 | set_transient( $cache_key, $versions, 5 * DAY_IN_SECONDS ); |
| @@ -120,11 +119,9 @@ | ||
| 120 | 119 | $url = $this->get_versions_api_url(); |
| 121 | 120 | if ( empty( $url ) ) { |
| 122 | 121 | return []; |
| 123 | 122 | } |
| 124 | - $response = function_exists( 'wp_remote_get_wp_remote_get' ) | |
| 125 | - ? wp_remote_get_wp_remote_get( $url ) | |
| 126 | - : wp_remote_get( $url ); //phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get | |
| 123 | + $response = wp_remote_get( $url ); | |
| 127 | 124 | if ( is_wp_error( $response ) ) { |
| 128 | 125 | return array(); |
| 129 | 126 | } |
| 130 | 127 | $response = wp_remote_retrieve_body( $response ); |
| @@ -184,9 +181,9 @@ | ||
| 184 | 181 | $version = $this->get_rollback(); |
| 185 | 182 | if ( empty( $version ) ) { |
| 186 | 183 | return $links; |
| 187 | 184 | } |
| 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>'; | |
| 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>'; | |
| 189 | 186 | |
| 190 | 187 | return $links; |
| 191 | 188 | } |
| 192 | 189 | |
| @@ -193,9 +190,9 @@ | ||
| 193 | 190 | /** |
| 194 | 191 | * Start the rollback operation. |
| 195 | 192 | */ |
| 196 | 193 | public function start_rollback() { |
| 197 | - if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], $this->product->get_key() . '_rollback' ) ) { //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 194 | + if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], $this->product->get_key() . '_rollback' ) ) { | |
| 198 | 195 | wp_nonce_ays( '' ); |
| 199 | 196 | } |
| 200 | 197 | |
| 201 | 198 | if ( $this->product->is_plugin() ) { |
| @@ -230,14 +227,11 @@ | ||
| 230 | 227 | set_site_transient( 'update_plugins', $plugin_transient ); |
| 231 | 228 | |
| 232 | 229 | $transient = get_transient( $this->product->get_key() . '_warning_rollback' ); |
| 233 | 230 | |
| 234 | - // Style fix for the api link that gets outside the content. | |
| 235 | - echo '<style>body#error-page{word-break:break-word;}</style>'; | |
| 236 | - | |
| 237 | 231 | if ( false === $transient ) { |
| 238 | 232 | set_transient( $this->product->get_key() . '_warning_rollback', 'in progress', 30 ); |
| 239 | - require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; | |
| 233 | + require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); | |
| 240 | 234 | $title = sprintf( apply_filters( $this->product->get_key() . '_rollback_message', 'Rolling back %s to v%s' ), $this->product->get_name(), $version ); |
| 241 | 235 | $plugin = $plugin_folder . '/' . $plugin_file; |
| 242 | 236 | $nonce = 'upgrade-plugin_' . $plugin; |
| 243 | 237 | $url = 'update.php?action=upgrade-plugin&plugin=' . urlencode( $plugin ); |
| @@ -246,9 +240,9 @@ | ||
| 246 | 240 | $upgrader->upgrade( $plugin ); |
| 247 | 241 | delete_transient( $this->product->get_key() . '_warning_rollback' ); |
| 248 | 242 | wp_die( |
| 249 | 243 | '', |
| 250 | - esc_attr( $title ), | |
| 244 | + $title, | |
| 251 | 245 | array( |
| 252 | 246 | 'response' => 200, |
| 253 | 247 | ) |
| 254 | 248 | ); |
| @@ -273,41 +267,22 @@ | ||
| 273 | 267 | set_site_transient( 'update_themes', $transient ); |
| 274 | 268 | |
| 275 | 269 | $transient = get_transient( $this->product->get_key() . '_warning_rollback' ); |
| 276 | 270 | |
| 277 | - // Style fix for the api link that gets outside the content. | |
| 278 | - echo '<style>body#error-page{word-break:break-word;}</style>'; | |
| 279 | - | |
| 280 | 271 | if ( false === $transient ) { |
| 281 | 272 | set_transient( $this->product->get_key() . '_warning_rollback', 'in progress', 30 ); |
| 282 | - require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; | |
| 273 | + require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); | |
| 283 | 274 | $title = sprintf( apply_filters( $this->product->get_key() . '_rollback_message', 'Rolling back %s to v%s' ), $this->product->get_name(), $version ); |
| 284 | 275 | $theme = $folder . '/style.css'; |
| 285 | 276 | $nonce = 'upgrade-theme_' . $theme; |
| 286 | 277 | $url = 'update.php?action=upgrade-theme&theme=' . urlencode( $theme ); |
| 287 | 278 | |
| 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 | - | |
| 304 | 279 | $upgrader = new \Theme_Upgrader( new \Theme_Upgrader_Skin( compact( 'title', 'nonce', 'url', 'theme' ) ) ); |
| 305 | 280 | $upgrader->upgrade( $theme ); |
| 306 | 281 | delete_transient( $this->product->get_key() . '_warning_rollback' ); |
| 307 | 282 | wp_die( |
| 308 | 283 | '', |
| 309 | - esc_attr( $title ), | |
| 284 | + $title, | |
| 310 | 285 | array( |
| 311 | 286 | 'response' => 200, |
| 312 | 287 | ) |
| 313 | 288 | ); |
| @@ -359,9 +334,9 @@ | ||
| 359 | 334 | * |
| 360 | 335 | * @return bool Which version is greater? |
| 361 | 336 | */ |
| 362 | 337 | public function sort_rollback_array( $a, $b ) { |
| 363 | - return version_compare( $b['version'], $a['version'] ); | |
| 338 | + return version_compare( $a['version'], $b['version'], '<' ) > 0; | |
| 364 | 339 | } |
| 365 | 340 | |
| 366 | 341 | /** |
| 367 | 342 | * Load module logic. |
| @@ -391,27 +366,11 @@ | ||
| 391 | 366 | ); |
| 392 | 367 | } |
| 393 | 368 | |
| 394 | 369 | /** |
| 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 | - /** | |
| 407 | 370 | * 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. |
| 408 | 371 | */ |
| 409 | 372 | public function add_hooks() { |
| 410 | 373 | add_action( 'admin_post_' . $this->product->get_key() . '_rollback', array( $this, 'start_rollback' ) ); |
| 411 | 374 | 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 ); | |
| 416 | 375 | } |
| 417 | 376 | } |