| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\App\Modules\ImportExport\Runners\Revert; |
| 4 |
|
| 5 |
use Elementor\Plugin; |
| 6 |
use Elementor\Core\Experiments\Manager as ExperimentsManager; |
| 7 |
|
| 8 |
class Site_Settings extends Revert_Runner_Base { |
| 9 |
|
| 10 |
public static function get_name(): string { |
| 11 |
return 'site-settings'; |
| 12 |
} |
| 13 |
|
| 14 |
public function should_revert( array $data ): bool { |
| 15 |
return ( |
| 16 |
isset( $data['runners'] ) && |
| 17 |
array_key_exists( static::get_name(), $data['runners'] ) |
| 18 |
); |
| 19 |
} |
| 20 |
|
| 21 |
public function revert( array $data ) { |
| 22 |
Plugin::$instance->kits_manager->revert( |
| 23 |
$data['runners'][ static::get_name() ]['imported_kit_id'], |
| 24 |
$data['runners'][ static::get_name() ]['active_kit_id'], |
| 25 |
$data['runners'][ static::get_name() ]['previous_kit_id'] |
| 26 |
); |
| 27 |
|
| 28 |
$this->revert_theme( $data ); |
| 29 |
$this->revert_experiments( $data ); |
| 30 |
} |
| 31 |
|
| 32 |
public function get_theme_upgrader(): \Theme_Upgrader { |
| 33 |
if ( ! class_exists( '\Theme_Upgrader' ) ) { |
| 34 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 35 |
} |
| 36 |
|
| 37 |
if ( ! class_exists( '\WP_Ajax_Upgrader_Skin' ) ) { |
| 38 |
require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php'; |
| 39 |
} |
| 40 |
|
| 41 |
return new \Theme_Upgrader( new \WP_Ajax_Upgrader_Skin() ); |
| 42 |
} |
| 43 |
|
| 44 |
protected function revert_theme( $data ) { |
| 45 |
$installed_theme = $data['runners'][ static::get_name() ]['installed_theme']; |
| 46 |
$activated_theme = $data['runners'][ static::get_name() ]['activated_theme']; |
| 47 |
$previous_active_theme = $data['runners'][ static::get_name() ]['previous_active_theme']; |
| 48 |
|
| 49 |
if ( empty( $installed_theme ) && empty( $activated_theme ) ) { |
| 50 |
// no need to remove a theme as it was used before import |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
if ( ! empty( $activated_theme ) ) { |
| 55 |
$previous_theme = wp_get_theme( $previous_active_theme['slug'] ); |
| 56 |
|
| 57 |
// no need to remove imported theme as it existed before import |
| 58 |
$this->activate_previous_theme( $previous_active_theme ); |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
if ( ! empty( $installed_theme ) ) { |
| 63 |
$this->activate_previous_theme( $previous_active_theme ); |
| 64 |
$this->delete_theme( $installed_theme ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
protected function should_delete_theme( $theme_slug ): bool { |
| 69 |
$current_theme = wp_get_theme(); |
| 70 |
|
| 71 |
return $theme_slug !== $current_theme->get_stylesheet() && wp_get_theme( $theme_slug )->exists(); |
| 72 |
} |
| 73 |
|
| 74 |
protected function delete_theme( $theme_slug ): bool { |
| 75 |
return delete_theme( $theme_slug ); |
| 76 |
} |
| 77 |
|
| 78 |
protected function activate_previous_theme( $previous_active_theme ) { |
| 79 |
if ( ! $previous_active_theme ) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
$theme = wp_get_theme( $previous_active_theme['slug'] ); |
| 84 |
|
| 85 |
if ( $theme->exists() ) { |
| 86 |
switch_theme( $theme->get_stylesheet() ); |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
$download_url = "https://downloads.wordpress.org/theme/{$previous_active_theme['slug']}.{$previous_active_theme['version']}.zip"; |
| 91 |
$install = $this->get_theme_upgrader()->install( $download_url ); |
| 92 |
|
| 93 |
if ( ! $install || is_wp_error( $install ) ) { |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
switch_theme( $previous_active_theme['slug'] ); |
| 98 |
} |
| 99 |
|
| 100 |
protected function revert_experiments( array $data ) { |
| 101 |
$runner_data = $data['runners'][ static::get_name() ]; |
| 102 |
$previous_experiments = $runner_data['previous_experiments'] ?? []; |
| 103 |
|
| 104 |
if ( empty( $previous_experiments ) ) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
$experiments_manager = Plugin::$instance->experiments; |
| 109 |
$current_features = $experiments_manager->get_features(); |
| 110 |
|
| 111 |
foreach ( $previous_experiments as $feature_name => $feature_data ) { |
| 112 |
if ( ! isset( $current_features[ $feature_name ] ) ) { |
| 113 |
continue; |
| 114 |
} |
| 115 |
|
| 116 |
if ( ! array_key_exists( $feature_name, $previous_experiments ) ) { |
| 117 |
continue; |
| 118 |
} |
| 119 |
|
| 120 |
$option_key = $experiments_manager->get_feature_option_key( $feature_name ); |
| 121 |
$previous_state = $feature_data['state']; |
| 122 |
|
| 123 |
if ( ExperimentsManager::STATE_DEFAULT === $previous_state ) { |
| 124 |
delete_option( $option_key ); |
| 125 |
} else { |
| 126 |
update_option( $option_key, $previous_state ); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|