| 1 |
<?php |
| 2 |
namespace Elementor\App\Modules\ImportExportCustomization\Runners\Export; |
| 3 |
|
| 4 |
use Elementor\Modules\AtomicWidgets\Module as Atomic_Widgets_Module; |
| 5 |
use Elementor\Modules\GlobalClasses\Module as Global_Classes_Module; |
| 6 |
use Elementor\Modules\Variables\Module as Variables_Module; |
| 7 |
use Elementor\Plugin; |
| 8 |
|
| 9 |
class Site_Settings extends Export_Runner_Base { |
| 10 |
const ALLOWED_SETTINGS = [ |
| 11 |
'theme', |
| 12 |
'globalColors', |
| 13 |
'globalFonts', |
| 14 |
'themeStyleSettings', |
| 15 |
'generalSettings', |
| 16 |
'experiments', |
| 17 |
'customCode', |
| 18 |
'customIcons', |
| 19 |
'customFonts', |
| 20 |
'classes', |
| 21 |
'variables', |
| 22 |
]; |
| 23 |
|
| 24 |
public static function get_name(): string { |
| 25 |
return 'site-settings'; |
| 26 |
} |
| 27 |
|
| 28 |
public function should_export( array $data ) { |
| 29 |
return ( |
| 30 |
isset( $data['include'] ) && |
| 31 |
in_array( 'settings', $data['include'], true ) |
| 32 |
); |
| 33 |
} |
| 34 |
|
| 35 |
public function export( array $data ) { |
| 36 |
$customization = $data['customization']['settings'] ?? null; |
| 37 |
if ( $customization ) { |
| 38 |
return $this->export_customization( $data, $customization ); |
| 39 |
} |
| 40 |
|
| 41 |
return $this->export_all( $data ); |
| 42 |
} |
| 43 |
|
| 44 |
private function export_all( $data, $include_theme = true ) { |
| 45 |
$kit = Plugin::$instance->kits_manager->get_active_kit(); |
| 46 |
$kit_data = $kit->get_export_data(); |
| 47 |
|
| 48 |
$excluded_kit_settings_keys = [ |
| 49 |
'site_name', |
| 50 |
'site_description', |
| 51 |
'site_logo', |
| 52 |
'site_favicon', |
| 53 |
]; |
| 54 |
|
| 55 |
foreach ( $excluded_kit_settings_keys as $setting_key ) { |
| 56 |
unset( $kit_data['settings'][ $setting_key ] ); |
| 57 |
} |
| 58 |
|
| 59 |
if ( $include_theme ) { |
| 60 |
$theme_data = $this->export_theme(); |
| 61 |
|
| 62 |
if ( $theme_data ) { |
| 63 |
$kit_data['theme'] = $theme_data; |
| 64 |
$manifest_data['theme'] = $theme_data; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
$experiments_data = $this->export_experiments(); |
| 69 |
|
| 70 |
if ( $experiments_data ) { |
| 71 |
$kit_data['experiments'] = $experiments_data; |
| 72 |
$manifest_data['experiments'] = array_keys( $experiments_data ); |
| 73 |
} |
| 74 |
|
| 75 |
$manifest_data['site-settings'] = array_fill_keys( self::ALLOWED_SETTINGS, true ); |
| 76 |
|
| 77 |
if ( ! $include_theme ) { |
| 78 |
$manifest_data['site-settings']['theme'] = false; |
| 79 |
} |
| 80 |
|
| 81 |
if ( $this->is_classes_feature_active() ) { |
| 82 |
$manifest_data['site-settings']['classesCount'] = $this->get_classes_count(); |
| 83 |
} else { |
| 84 |
unset( $manifest_data['site-settings']['classes'] ); |
| 85 |
} |
| 86 |
|
| 87 |
if ( $this->is_variables_feature_active() ) { |
| 88 |
$manifest_data['site-settings']['variablesCount'] = $this->get_variables_count(); |
| 89 |
} else { |
| 90 |
unset( $manifest_data['site-settings']['variables'] ); |
| 91 |
} |
| 92 |
|
| 93 |
return [ |
| 94 |
'files' => [ |
| 95 |
'path' => 'site-settings', |
| 96 |
'data' => $kit_data, |
| 97 |
], |
| 98 |
'manifest' => [ |
| 99 |
$manifest_data, |
| 100 |
], |
| 101 |
]; |
| 102 |
} |
| 103 |
|
| 104 |
public function get_classes_count(): int { |
| 105 |
$classes_repository = \Elementor\Modules\GlobalClasses\Global_Classes_Repository::make(); |
| 106 |
$classes_data = $classes_repository->all()->get(); |
| 107 |
|
| 108 |
return count( $classes_data['items'] ?? [] ); |
| 109 |
} |
| 110 |
|
| 111 |
public function get_variables_count(): int { |
| 112 |
$kit = Plugin::$instance->kits_manager->get_active_kit(); |
| 113 |
$variables_repository = new \Elementor\Modules\Variables\Storage\Variables_Repository( $kit ); |
| 114 |
$collection = $variables_repository->load(); |
| 115 |
$count = 0; |
| 116 |
|
| 117 |
foreach ( $collection->all() as $variable ) { |
| 118 |
if ( ! $variable->is_deleted() ) { |
| 119 |
$count++; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
return $count; |
| 124 |
} |
| 125 |
|
| 126 |
public function is_classes_feature_active(): bool { |
| 127 |
return Plugin::$instance->experiments->is_feature_active( Global_Classes_Module::NAME ) |
| 128 |
&& Plugin::$instance->experiments->is_feature_active( Atomic_Widgets_Module::EXPERIMENT_NAME ); |
| 129 |
} |
| 130 |
|
| 131 |
public function is_variables_feature_active(): bool { |
| 132 |
return Plugin::$instance->experiments->is_feature_active( Variables_Module::EXPERIMENT_NAME ) |
| 133 |
&& Plugin::$instance->experiments->is_feature_active( Atomic_Widgets_Module::EXPERIMENT_NAME ); |
| 134 |
} |
| 135 |
|
| 136 |
private function export_customization( $data, $customization ) { |
| 137 |
$result = apply_filters( 'elementor/import-export-customization/export/site-settings/customization', null, $data, $customization, $this ); |
| 138 |
|
| 139 |
if ( is_array( $result ) ) { |
| 140 |
return $result; |
| 141 |
} |
| 142 |
|
| 143 |
$export_result = $this->export_all( $data, ! empty( $customization['theme'] ) ); |
| 144 |
|
| 145 |
if ( $this->is_classes_feature_active() ) { |
| 146 |
$include_classes = $customization['classes'] ?? false; |
| 147 |
$export_result['manifest'][0]['site-settings']['classes'] = (bool) $include_classes; |
| 148 |
|
| 149 |
if ( ! $include_classes ) { |
| 150 |
$export_result['manifest'][0]['site-settings']['classesCount'] = 0; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
if ( $this->is_variables_feature_active() ) { |
| 155 |
$include_variables = $customization['variables'] ?? false; |
| 156 |
$export_result['manifest'][0]['site-settings']['variables'] = (bool) $include_variables; |
| 157 |
|
| 158 |
if ( ! $include_variables ) { |
| 159 |
$export_result['manifest'][0]['site-settings']['variablesCount'] = 0; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
return $export_result; |
| 164 |
} |
| 165 |
|
| 166 |
public function export_theme() { |
| 167 |
$theme = wp_get_theme(); |
| 168 |
|
| 169 |
if ( empty( $theme ) || empty( $theme->get( 'ThemeURI' ) ) ) { |
| 170 |
return null; |
| 171 |
} |
| 172 |
|
| 173 |
$theme_data['name'] = $theme->get( 'Name' ); |
| 174 |
$theme_data['theme_uri'] = $theme->get( 'ThemeURI' ); |
| 175 |
$theme_data['version'] = $theme->get( 'Version' ); |
| 176 |
$theme_data['slug'] = $theme->get_stylesheet(); |
| 177 |
|
| 178 |
return $theme_data; |
| 179 |
} |
| 180 |
|
| 181 |
public function export_experiments() { |
| 182 |
$features = Plugin::$instance->experiments->get_features(); |
| 183 |
|
| 184 |
if ( empty( $features ) ) { |
| 185 |
return null; |
| 186 |
} |
| 187 |
|
| 188 |
$experiments_data = []; |
| 189 |
|
| 190 |
foreach ( $features as $feature_name => $feature ) { |
| 191 |
$experiments_data[ $feature_name ] = [ |
| 192 |
'name' => $feature_name, |
| 193 |
'title' => $feature['title'], |
| 194 |
'state' => $feature['state'], |
| 195 |
'default' => $feature['default'], |
| 196 |
'release_status' => $feature['release_status'], |
| 197 |
]; |
| 198 |
} |
| 199 |
|
| 200 |
return empty( $experiments_data ) ? null : $experiments_data; |
| 201 |
} |
| 202 |
} |
| 203 |
|