| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\App\Modules\ImportExportCustomization\Runners\Import; |
| 4 |
|
| 5 |
use Elementor\Plugin; |
| 6 |
use Elementor\Core\Settings\Page\Manager as PageManager; |
| 7 |
use Elementor\App\Modules\ImportExportCustomization\Utils; |
| 8 |
use Elementor\Core\Experiments\Manager as ExperimentsManager; |
| 9 |
|
| 10 |
class Site_Settings extends Import_Runner_Base { |
| 11 |
|
| 12 |
const ALLOWED_SETTINGS = [ |
| 13 |
'theme', |
| 14 |
'globalColors', |
| 15 |
'globalFonts', |
| 16 |
'themeStyleSettings', |
| 17 |
'generalSettings', |
| 18 |
'experiments', |
| 19 |
'customCode', |
| 20 |
'customIcons', |
| 21 |
'customFonts', |
| 22 |
]; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var int |
| 26 |
*/ |
| 27 |
private $previous_kit_id; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var int |
| 31 |
*/ |
| 32 |
private $active_kit_id; |
| 33 |
|
| 34 |
/** |
| 35 |
* @var int |
| 36 |
*/ |
| 37 |
private $imported_kit_id; |
| 38 |
|
| 39 |
/** |
| 40 |
* @var string|null |
| 41 |
*/ |
| 42 |
private ?string $installed_theme = null; |
| 43 |
|
| 44 |
/** |
| 45 |
* @var string|null |
| 46 |
*/ |
| 47 |
private ?string $activated_theme = null; |
| 48 |
|
| 49 |
/** |
| 50 |
* @var array|null |
| 51 |
*/ |
| 52 |
private ?array $previous_active_theme = null; |
| 53 |
|
| 54 |
/** |
| 55 |
* @var array |
| 56 |
*/ |
| 57 |
private $previous_experiments = []; |
| 58 |
|
| 59 |
/** |
| 60 |
* @var array |
| 61 |
*/ |
| 62 |
private $imported_experiments = []; |
| 63 |
|
| 64 |
public function get_theme_upgrader(): \Theme_Upgrader { |
| 65 |
if ( ! function_exists( 'request_filesystem_credentials' ) ) { |
| 66 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 67 |
} |
| 68 |
|
| 69 |
if ( ! class_exists( '\Theme_Upgrader' ) ) { |
| 70 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 71 |
} |
| 72 |
|
| 73 |
if ( ! class_exists( '\WP_Ajax_Upgrader_Skin' ) ) { |
| 74 |
require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php'; |
| 75 |
} |
| 76 |
|
| 77 |
return new \Theme_Upgrader( new \WP_Ajax_Upgrader_Skin() ); |
| 78 |
} |
| 79 |
|
| 80 |
public static function get_name(): string { |
| 81 |
return 'site-settings'; |
| 82 |
} |
| 83 |
|
| 84 |
public function should_import( array $data ) { |
| 85 |
return ( |
| 86 |
isset( $data['include'] ) && |
| 87 |
in_array( 'settings', $data['include'], true ) && |
| 88 |
! empty( $data['site_settings']['settings'] ) |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
public function import( array $data, array $imported_data ) { |
| 93 |
$customization = $data['customization']['settings'] ?? null; |
| 94 |
|
| 95 |
if ( $customization ) { |
| 96 |
return $this->import_with_customization( $data, $imported_data, $customization ); |
| 97 |
} |
| 98 |
|
| 99 |
return $this->import_with_manifest( $data, $imported_data ); |
| 100 |
} |
| 101 |
|
| 102 |
private function import_with_customization( array $data, array $imported_data, array $customization ) { |
| 103 |
$result = apply_filters( 'elementor/import-export-customization/import/site-settings/customization', null, $data, $imported_data, $customization, $this ); |
| 104 |
|
| 105 |
if ( is_array( $result ) ) { |
| 106 |
return $result; |
| 107 |
} |
| 108 |
|
| 109 |
return $this->import_with_manifest( $data, $imported_data, ! empty( $customization['theme'] ) ); |
| 110 |
} |
| 111 |
|
| 112 |
private function import_with_manifest( array $data, array $imported_data, $include_theme = true ) { |
| 113 |
$new_site_settings = $data['site_settings']['settings']; |
| 114 |
$title = $data['manifest']['title'] ?? 'Imported Kit'; |
| 115 |
$manifest_settings = $data['manifest']['site-settings'] ?? []; |
| 116 |
|
| 117 |
$active_kit = Plugin::$instance->kits_manager->get_active_kit(); |
| 118 |
|
| 119 |
$this->active_kit_id = (int) $active_kit->get_id(); |
| 120 |
$this->previous_kit_id = (int) Plugin::$instance->kits_manager->get_previous_id(); |
| 121 |
|
| 122 |
$result = []; |
| 123 |
|
| 124 |
$old_settings = $active_kit->get_meta( PageManager::META_KEY ); |
| 125 |
|
| 126 |
if ( ! $old_settings ) { |
| 127 |
$old_settings = []; |
| 128 |
} |
| 129 |
|
| 130 |
$new_site_settings = $this->filter_settings_by_manifest( $new_site_settings, $manifest_settings ); |
| 131 |
|
| 132 |
if ( ( $manifest_settings['globalColors'] ?? false ) && ! empty( $old_settings['custom_colors'] ) && ! empty( $new_site_settings['custom_colors'] ) ) { |
| 133 |
$new_site_settings['custom_colors'] = array_merge( $old_settings['custom_colors'], $new_site_settings['custom_colors'] ); |
| 134 |
} |
| 135 |
|
| 136 |
if ( ( $manifest_settings['globalFonts'] ?? false ) && ! empty( $old_settings['custom_typography'] ) && ! empty( $new_site_settings['custom_typography'] ) ) { |
| 137 |
$new_site_settings['custom_typography'] = array_merge( $old_settings['custom_typography'], $new_site_settings['custom_typography'] ); |
| 138 |
} |
| 139 |
|
| 140 |
if ( ( $manifest_settings['generalSettings'] ?? false ) && ! empty( $new_site_settings['space_between_widgets'] ) ) { |
| 141 |
$new_site_settings['space_between_widgets'] = Utils::update_space_between_widgets_values( $new_site_settings['space_between_widgets'] ); |
| 142 |
} |
| 143 |
|
| 144 |
$new_site_settings = array_replace_recursive( $old_settings, $new_site_settings ); |
| 145 |
|
| 146 |
$new_kit = Plugin::$instance->kits_manager->create_new_kit( $title, $new_site_settings ); |
| 147 |
|
| 148 |
$this->imported_kit_id = (int) $new_kit; |
| 149 |
|
| 150 |
$result['site-settings']['imported_kit_id'] = $this->imported_kit_id; |
| 151 |
|
| 152 |
foreach ( $new_site_settings as $key => $value ) { |
| 153 |
$result['site-settings'][ $key ] = $value; |
| 154 |
} |
| 155 |
|
| 156 |
if ( ( $manifest_settings['theme'] ?? false ) && $include_theme ) { |
| 157 |
$import_theme_result = $this->import_theme( $data ); |
| 158 |
|
| 159 |
if ( ! empty( $import_theme_result ) ) { |
| 160 |
$result['theme'] = $import_theme_result; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
if ( $manifest_settings['experiments'] ?? false ) { |
| 165 |
$this->import_experiments( $data ); |
| 166 |
|
| 167 |
if ( ! empty( $this->imported_experiments ) ) { |
| 168 |
$result['experiments'] = $this->imported_experiments; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
return $result; |
| 173 |
} |
| 174 |
|
| 175 |
private function filter_settings_by_manifest( array $settings, array $manifest_settings ): array { |
| 176 |
foreach ( self::ALLOWED_SETTINGS as $setting_key ) { |
| 177 |
if ( ! ( $manifest_settings[ $setting_key ] ?? false ) ) { |
| 178 |
$settings = $this->remove_setting_by_key( $settings, $setting_key ); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
return $settings; |
| 183 |
} |
| 184 |
|
| 185 |
private function remove_setting_by_key( array $settings, string $setting_key ): array { |
| 186 |
switch ( $setting_key ) { |
| 187 |
case 'globalColors': |
| 188 |
$settings = $this->remove_global_colors( $settings ); |
| 189 |
break; |
| 190 |
case 'globalFonts': |
| 191 |
$settings = $this->remove_global_fonts( $settings ); |
| 192 |
break; |
| 193 |
case 'themeStyleSettings': |
| 194 |
$settings = $this->remove_theme_style( $settings ); |
| 195 |
break; |
| 196 |
case 'generalSettings': |
| 197 |
$settings = $this->remove_other_settings( $settings ); |
| 198 |
break; |
| 199 |
} |
| 200 |
|
| 201 |
return $settings; |
| 202 |
} |
| 203 |
|
| 204 |
private function remove_global_colors( array $settings ): array { |
| 205 |
$color_keys = [ 'system_colors', 'custom_colors' ]; |
| 206 |
|
| 207 |
foreach ( $color_keys as $key ) { |
| 208 |
if ( isset( $settings[ $key ] ) ) { |
| 209 |
unset( $settings[ $key ] ); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
return $settings; |
| 214 |
} |
| 215 |
|
| 216 |
private function remove_global_fonts( array $settings ): array { |
| 217 |
$typography_keys = [ 'system_typography', 'custom_typography', 'default_generic_fonts' ]; |
| 218 |
|
| 219 |
foreach ( $typography_keys as $key ) { |
| 220 |
if ( isset( $settings[ $key ] ) ) { |
| 221 |
unset( $settings[ $key ] ); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
return $settings; |
| 226 |
} |
| 227 |
|
| 228 |
private function remove_theme_style( array $settings ): array { |
| 229 |
$theme_style_patterns = [ |
| 230 |
'/^body_/', |
| 231 |
'/^h[1-6]_/', |
| 232 |
'/^button_/', |
| 233 |
'/^link_/', |
| 234 |
'/^form_field_/', |
| 235 |
]; |
| 236 |
|
| 237 |
foreach ( $settings as $key => $value ) { |
| 238 |
foreach ( $theme_style_patterns as $pattern ) { |
| 239 |
if ( preg_match( $pattern, $key ) ) { |
| 240 |
unset( $settings[ $key ] ); |
| 241 |
break; |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
return $settings; |
| 247 |
} |
| 248 |
|
| 249 |
private function remove_other_settings( array $settings ): array { |
| 250 |
$settings_keys = [ |
| 251 |
'template', |
| 252 |
'container_width', |
| 253 |
'container_padding', |
| 254 |
'space_between_widgets', |
| 255 |
'viewport_md', |
| 256 |
'viewport_lg', |
| 257 |
'page_title_selector', |
| 258 |
'activeItemIndex', |
| 259 |
]; |
| 260 |
|
| 261 |
foreach ( $settings_keys as $key ) { |
| 262 |
if ( isset( $settings[ $key ] ) ) { |
| 263 |
unset( $settings[ $key ] ); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
return $settings; |
| 268 |
} |
| 269 |
|
| 270 |
protected function install_theme( $slug, $version ) { |
| 271 |
$download_url = "https://downloads.wordpress.org/theme/{$slug}.{$version}.zip"; |
| 272 |
|
| 273 |
return $this->get_theme_upgrader()->install( $download_url ); |
| 274 |
} |
| 275 |
|
| 276 |
protected function activate_theme( $slug ) { |
| 277 |
switch_theme( $slug ); |
| 278 |
} |
| 279 |
|
| 280 |
public function import_theme( array $data ) { |
| 281 |
if ( empty( $data['site_settings']['theme'] ) ) { |
| 282 |
return null; |
| 283 |
} |
| 284 |
|
| 285 |
if ( ! function_exists( 'wp_get_theme' ) ) { |
| 286 |
require_once ABSPATH . 'wp-admin/includes/theme.php'; |
| 287 |
} |
| 288 |
|
| 289 |
$theme = $data['site_settings']['theme']; |
| 290 |
$theme_slug = $theme['slug']; |
| 291 |
$theme_name = $theme['name']; |
| 292 |
|
| 293 |
$current_theme = wp_get_theme(); |
| 294 |
$this->previous_active_theme = []; |
| 295 |
$this->previous_active_theme['slug'] = $current_theme->get_stylesheet(); |
| 296 |
$this->previous_active_theme['version'] = $current_theme->get( 'Version' ); |
| 297 |
|
| 298 |
if ( $current_theme->get_stylesheet() === $theme_slug ) { |
| 299 |
$result['succeed'][ $theme_slug ] = sprintf( |
| 300 |
/* translators: %s: Theme name. */ |
| 301 |
__( 'Theme: %s is already used', 'elementor' ), |
| 302 |
$theme_name |
| 303 |
); |
| 304 |
return $result; |
| 305 |
} |
| 306 |
|
| 307 |
try { |
| 308 |
if ( wp_get_theme( $theme_slug )->exists() ) { |
| 309 |
$this->activate_theme( $theme_slug ); |
| 310 |
$this->activated_theme = $theme_slug; |
| 311 |
$result['succeed'][ $theme_slug ] = sprintf( |
| 312 |
/* translators: %s: Theme name. */ |
| 313 |
__( 'Theme: %s has already been installed and activated', 'elementor' ), |
| 314 |
$theme_name |
| 315 |
); |
| 316 |
return $result; |
| 317 |
} |
| 318 |
|
| 319 |
$import = $this->install_theme( $theme_slug, $theme['version'] ); |
| 320 |
|
| 321 |
if ( is_wp_error( $import ) ) { |
| 322 |
$result['failed'][ $theme_slug ] = sprintf( |
| 323 |
/* translators: %s: Theme name. */ |
| 324 |
__( 'Failed to install theme: %s', 'elementor' ), |
| 325 |
$theme_name |
| 326 |
); |
| 327 |
return $result; |
| 328 |
} |
| 329 |
|
| 330 |
$result['succeed'][ $theme_slug ] = sprintf( |
| 331 |
/* translators: %s: Theme name. */ |
| 332 |
__( 'Theme: %s has been successfully installed', 'elementor' ), |
| 333 |
$theme_name |
| 334 |
); |
| 335 |
$this->installed_theme = $theme_slug; |
| 336 |
$this->activate_theme( $theme_slug ); |
| 337 |
} catch ( \Exception $error ) { |
| 338 |
$result['failed'][ $theme_slug ] = $error->getMessage(); |
| 339 |
} |
| 340 |
|
| 341 |
return $result; |
| 342 |
} |
| 343 |
|
| 344 |
public function import_experiments( array $data ) { |
| 345 |
if ( empty( $data['site_settings']['experiments'] ) ) { |
| 346 |
return null; |
| 347 |
} |
| 348 |
|
| 349 |
$experiments_data = $data['site_settings']['experiments']; |
| 350 |
$experiments_manager = Plugin::$instance->experiments; |
| 351 |
$current_features = $experiments_manager->get_features(); |
| 352 |
|
| 353 |
$this->save_previous_experiments_state( $current_features ); |
| 354 |
|
| 355 |
foreach ( $experiments_data as $feature_name => $feature_data ) { |
| 356 |
if ( ! isset( $current_features[ $feature_name ] ) ) { |
| 357 |
continue; |
| 358 |
} |
| 359 |
|
| 360 |
$current_feature = $current_features[ $feature_name ]; |
| 361 |
|
| 362 |
$current_feature_state = $current_feature['state']; |
| 363 |
$new_state = $feature_data['state']; |
| 364 |
|
| 365 |
if ( $current_feature_state === $new_state ) { |
| 366 |
continue; |
| 367 |
} |
| 368 |
|
| 369 |
if ( ! in_array( $new_state, [ ExperimentsManager::STATE_DEFAULT, ExperimentsManager::STATE_ACTIVE, ExperimentsManager::STATE_ACTIVE ], true ) ) { |
| 370 |
continue; |
| 371 |
} |
| 372 |
|
| 373 |
$option_key = $experiments_manager->get_feature_option_key( $feature_name ); |
| 374 |
|
| 375 |
if ( 'default' === $new_state ) { |
| 376 |
delete_option( $option_key ); |
| 377 |
} else { |
| 378 |
update_option( $option_key, $new_state ); |
| 379 |
} |
| 380 |
|
| 381 |
$this->imported_experiments[ $feature_name ] = $feature_data; |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
private function save_previous_experiments_state( array $current_features ) { |
| 386 |
$experiments_manager = Plugin::$instance->experiments; |
| 387 |
|
| 388 |
foreach ( $current_features as $feature_name => $feature ) { |
| 389 |
if ( ! $feature['mutable'] ) { |
| 390 |
continue; |
| 391 |
} |
| 392 |
|
| 393 |
$option_key = $experiments_manager->get_feature_option_key( $feature_name ); |
| 394 |
$saved_state = get_option( $option_key ); |
| 395 |
|
| 396 |
$this->previous_experiments[ $feature_name ] = [ |
| 397 |
'name' => $feature_name, |
| 398 |
'title' => $feature['title'], |
| 399 |
'state' => empty( $saved_state ) ? 'default' : $saved_state, |
| 400 |
'default' => $feature['default'], |
| 401 |
'release_status' => $feature['release_status'], |
| 402 |
]; |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
public function get_import_session_metadata(): array { |
| 407 |
return [ |
| 408 |
'previous_kit_id' => $this->previous_kit_id, |
| 409 |
'active_kit_id' => $this->active_kit_id, |
| 410 |
'imported_kit_id' => $this->imported_kit_id, |
| 411 |
'installed_theme' => $this->installed_theme, |
| 412 |
'activated_theme' => $this->activated_theme, |
| 413 |
'previous_active_theme' => $this->previous_active_theme, |
| 414 |
'previous_experiments' => $this->previous_experiments, |
| 415 |
'imported_experiments' => $this->imported_experiments, |
| 416 |
]; |
| 417 |
} |
| 418 |
} |
| 419 |
|