| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\App\Modules\ImportExport\Runners\Import; |
| 4 |
|
| 5 |
use Elementor\Plugin; |
| 6 |
use Elementor\Core\Settings\Page\Manager as PageManager; |
| 7 |
use Elementor\App\Modules\ImportExport\Utils; |
| 8 |
use Elementor\Core\Experiments\Manager as ExperimentsManager; |
| 9 |
|
| 10 |
class Site_Settings extends Import_Runner_Base { |
| 11 |
|
| 12 |
/** |
| 13 |
* @var int |
| 14 |
*/ |
| 15 |
private $previous_kit_id; |
| 16 |
|
| 17 |
/** |
| 18 |
* @var int |
| 19 |
*/ |
| 20 |
private $active_kit_id; |
| 21 |
|
| 22 |
/** |
| 23 |
* @var int |
| 24 |
*/ |
| 25 |
private $imported_kit_id; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var string|null |
| 29 |
*/ |
| 30 |
private ?string $installed_theme = null; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var string|null |
| 34 |
*/ |
| 35 |
private ?string $activated_theme = null; |
| 36 |
|
| 37 |
/** |
| 38 |
* @var array|null |
| 39 |
*/ |
| 40 |
private ?array $previous_active_theme = null; |
| 41 |
|
| 42 |
/** |
| 43 |
* @var array |
| 44 |
*/ |
| 45 |
private $previous_experiments = []; |
| 46 |
|
| 47 |
/** |
| 48 |
* @var array |
| 49 |
*/ |
| 50 |
private $imported_experiments = []; |
| 51 |
|
| 52 |
public function get_theme_upgrader(): \Theme_Upgrader { |
| 53 |
if ( ! class_exists( '\Theme_Upgrader' ) ) { |
| 54 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 55 |
} |
| 56 |
|
| 57 |
if ( ! class_exists( '\WP_Ajax_Upgrader_Skin' ) ) { |
| 58 |
require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php'; |
| 59 |
} |
| 60 |
|
| 61 |
return new \Theme_Upgrader( new \WP_Ajax_Upgrader_Skin() ); |
| 62 |
} |
| 63 |
|
| 64 |
public static function get_name(): string { |
| 65 |
return 'site-settings'; |
| 66 |
} |
| 67 |
|
| 68 |
public function should_import( array $data ) { |
| 69 |
return ( |
| 70 |
isset( $data['include'] ) && |
| 71 |
in_array( 'settings', $data['include'], true ) && |
| 72 |
! empty( $data['site_settings']['settings'] ) |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
public function import( array $data, array $imported_data ) { |
| 77 |
$new_site_settings = $data['site_settings']['settings']; |
| 78 |
$title = $data['manifest']['title'] ?? 'Imported Kit'; |
| 79 |
|
| 80 |
$active_kit = Plugin::$instance->kits_manager->get_active_kit(); |
| 81 |
|
| 82 |
$this->active_kit_id = (int) $active_kit->get_id(); |
| 83 |
$this->previous_kit_id = (int) Plugin::$instance->kits_manager->get_previous_id(); |
| 84 |
|
| 85 |
$result = []; |
| 86 |
|
| 87 |
$old_settings = $active_kit->get_meta( PageManager::META_KEY ); |
| 88 |
|
| 89 |
if ( ! $old_settings ) { |
| 90 |
$old_settings = []; |
| 91 |
} |
| 92 |
|
| 93 |
if ( ! empty( $old_settings['custom_colors'] ) ) { |
| 94 |
$new_site_settings['custom_colors'] = array_merge( $old_settings['custom_colors'], $new_site_settings['custom_colors'] ); |
| 95 |
} |
| 96 |
|
| 97 |
if ( ! empty( $old_settings['custom_typography'] ) ) { |
| 98 |
$new_site_settings['custom_typography'] = array_merge( $old_settings['custom_typography'], $new_site_settings['custom_typography'] ); |
| 99 |
} |
| 100 |
|
| 101 |
if ( ! empty( $new_site_settings['space_between_widgets'] ) ) { |
| 102 |
$new_site_settings['space_between_widgets'] = Utils::update_space_between_widgets_values( $new_site_settings['space_between_widgets'] ); |
| 103 |
} |
| 104 |
|
| 105 |
$new_site_settings = array_replace_recursive( $old_settings, $new_site_settings ); |
| 106 |
|
| 107 |
$new_kit = Plugin::$instance->kits_manager->create_new_kit( $title, $new_site_settings ); |
| 108 |
|
| 109 |
$this->imported_kit_id = (int) $new_kit; |
| 110 |
|
| 111 |
$result['site-settings'] = (bool) $new_kit; |
| 112 |
|
| 113 |
$import_theme_result = $this->import_theme( $data ); |
| 114 |
|
| 115 |
if ( ! empty( $import_theme_result ) ) { |
| 116 |
$result['theme'] = $import_theme_result; |
| 117 |
} |
| 118 |
|
| 119 |
$this->import_experiments( $data ); |
| 120 |
|
| 121 |
if ( ! empty( $this->imported_experiments ) ) { |
| 122 |
$result['experiments'] = $this->imported_experiments; |
| 123 |
} |
| 124 |
|
| 125 |
return $result; |
| 126 |
} |
| 127 |
|
| 128 |
protected function install_theme( $slug, $version ) { |
| 129 |
$download_url = "https://downloads.wordpress.org/theme/{$slug}.{$version}.zip"; |
| 130 |
|
| 131 |
return $this->get_theme_upgrader()->install( $download_url ); |
| 132 |
} |
| 133 |
|
| 134 |
protected function activate_theme( $slug ) { |
| 135 |
switch_theme( $slug ); |
| 136 |
} |
| 137 |
|
| 138 |
public function import_theme( array $data ) { |
| 139 |
if ( empty( $data['site_settings']['theme'] ) ) { |
| 140 |
return null; |
| 141 |
} |
| 142 |
|
| 143 |
$theme = $data['site_settings']['theme']; |
| 144 |
$theme_slug = $theme['slug']; |
| 145 |
$theme_name = $theme['name']; |
| 146 |
|
| 147 |
$current_theme = wp_get_theme(); |
| 148 |
$this->previous_active_theme = []; |
| 149 |
$this->previous_active_theme['slug'] = $current_theme->get_stylesheet(); |
| 150 |
$this->previous_active_theme['version'] = $current_theme->get( 'Version' ); |
| 151 |
|
| 152 |
if ( $current_theme->get_stylesheet() === $theme_slug ) { |
| 153 |
$result['succeed'][ $theme_slug ] = sprintf( |
| 154 |
/* translators: %s: Theme name. */ |
| 155 |
__( 'Theme: %1$s is already used', 'elementor' ), |
| 156 |
$theme_name |
| 157 |
); |
| 158 |
return $result; |
| 159 |
} |
| 160 |
|
| 161 |
try { |
| 162 |
if ( wp_get_theme( $theme_slug )->exists() ) { |
| 163 |
$this->activate_theme( $theme_slug ); |
| 164 |
$this->activated_theme = $theme_slug; |
| 165 |
$result['succeed'][ $theme_slug ] = sprintf( |
| 166 |
/* translators: %s: Theme name. */ |
| 167 |
__( 'Theme: %1$s has already been installed and activated', 'elementor' ), |
| 168 |
$theme_name |
| 169 |
); |
| 170 |
return $result; |
| 171 |
} |
| 172 |
|
| 173 |
$import = $this->install_theme( $theme_slug, $theme['version'] ); |
| 174 |
|
| 175 |
if ( is_wp_error( $import ) ) { |
| 176 |
$result['failed'][ $theme_slug ] = sprintf( |
| 177 |
/* translators: %s: Theme name. */ |
| 178 |
__( 'Failed to install theme: %1$s', 'elementor' ), |
| 179 |
$theme_name |
| 180 |
); |
| 181 |
return $result; |
| 182 |
} |
| 183 |
|
| 184 |
$result['succeed'][ $theme_slug ] = sprintf( |
| 185 |
/* translators: %s: Theme name. */ |
| 186 |
__( 'Theme: %1$s has been successfully installed', 'elementor' ), |
| 187 |
$theme_name |
| 188 |
); |
| 189 |
$this->installed_theme = $theme_slug; |
| 190 |
$this->activate_theme( $theme_slug ); |
| 191 |
} catch ( \Exception $error ) { |
| 192 |
$result['failed'][ $theme_slug ] = $error->getMessage(); |
| 193 |
} |
| 194 |
|
| 195 |
return $result; |
| 196 |
} |
| 197 |
|
| 198 |
private function import_experiments( array $data ) { |
| 199 |
if ( empty( $data['site_settings']['experiments'] ) ) { |
| 200 |
return null; |
| 201 |
} |
| 202 |
|
| 203 |
$experiments_data = $data['site_settings']['experiments']; |
| 204 |
$experiments_manager = Plugin::$instance->experiments; |
| 205 |
$current_features = $experiments_manager->get_features(); |
| 206 |
|
| 207 |
$this->save_previous_experiments_state( $current_features ); |
| 208 |
|
| 209 |
foreach ( $experiments_data as $feature_name => $feature_data ) { |
| 210 |
if ( ! isset( $current_features[ $feature_name ] ) ) { |
| 211 |
continue; |
| 212 |
} |
| 213 |
|
| 214 |
$current_feature = $current_features[ $feature_name ]; |
| 215 |
|
| 216 |
$current_feature_state = $current_feature['state']; |
| 217 |
$new_state = $feature_data['state']; |
| 218 |
|
| 219 |
if ( $current_feature_state === $new_state ) { |
| 220 |
continue; |
| 221 |
} |
| 222 |
|
| 223 |
if ( ! in_array( $new_state, [ ExperimentsManager::STATE_DEFAULT, ExperimentsManager::STATE_ACTIVE, ExperimentsManager::STATE_ACTIVE ], true ) ) { |
| 224 |
continue; |
| 225 |
} |
| 226 |
|
| 227 |
$option_key = $experiments_manager->get_feature_option_key( $feature_name ); |
| 228 |
|
| 229 |
if ( 'default' === $new_state ) { |
| 230 |
delete_option( $option_key ); |
| 231 |
} else { |
| 232 |
update_option( $option_key, $new_state ); |
| 233 |
} |
| 234 |
|
| 235 |
$this->imported_experiments[ $feature_name ] = $feature_data; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
private function save_previous_experiments_state( array $current_features ) { |
| 240 |
$experiments_manager = Plugin::$instance->experiments; |
| 241 |
|
| 242 |
foreach ( $current_features as $feature_name => $feature ) { |
| 243 |
if ( ! $feature['mutable'] ) { |
| 244 |
continue; |
| 245 |
} |
| 246 |
|
| 247 |
$option_key = $experiments_manager->get_feature_option_key( $feature_name ); |
| 248 |
$saved_state = get_option( $option_key ); |
| 249 |
|
| 250 |
$this->previous_experiments[ $feature_name ] = [ |
| 251 |
'name' => $feature_name, |
| 252 |
'title' => $feature['title'], |
| 253 |
'state' => empty( $saved_state ) ? 'default' : $saved_state, |
| 254 |
'default' => $feature['default'], |
| 255 |
'release_status' => $feature['release_status'], |
| 256 |
]; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
public function get_import_session_metadata(): array { |
| 261 |
return [ |
| 262 |
'previous_kit_id' => $this->previous_kit_id, |
| 263 |
'active_kit_id' => $this->active_kit_id, |
| 264 |
'imported_kit_id' => $this->imported_kit_id, |
| 265 |
'installed_theme' => $this->installed_theme, |
| 266 |
'activated_theme' => $this->activated_theme, |
| 267 |
'previous_active_theme' => $this->previous_active_theme, |
| 268 |
'previous_experiments' => $this->previous_experiments, |
| 269 |
'imported_experiments' => $this->imported_experiments, |
| 270 |
]; |
| 271 |
} |
| 272 |
} |
| 273 |
|