| @@ -1,380 +1,193 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace FL\Assistant\Services; | |
| 4 | - | |
| 5 | -use FL\Assistant\Services\ThemeService; | |
| 6 | -use FL\Assistant\Helpers\CustomizerOptionHelper; | |
| 7 | -use FL\Assistant\Clients\Cloud\CloudClient; | |
| 8 | -use FL\Assistant\Helpers\MediaPathHelper; | |
| 9 | -use FL\Assistant\Helpers\ScreenshotHelper; | |
| 10 | -use FL\Assistant\Helpers\AstraSettingsHelper; | |
| 11 | - | |
| 12 | -class CustomizerService { | |
| 13 | - | |
| 14 | - /** | |
| 15 | - * @var array | |
| 16 | - */ | |
| 17 | - private $helpers = [ | |
| 18 | - 'astra' => AstraSettingsHelper::class, | |
| 19 | - ]; | |
| 20 | - | |
| 21 | - /** | |
| 22 | - * @param int $library_id | |
| 23 | - * @return array | |
| 24 | - */ | |
| 25 | - public function export_to_cloud( $library_id, $subtype, $options ) { | |
| 26 | - $client = new CloudClient; | |
| 27 | - | |
| 28 | - if ( $subtype === 'theme_settings' ) { | |
| 29 | - $theme = wp_get_theme(); | |
| 30 | - $data = $this->export_theme_settings(); | |
| 31 | - $name = sprintf( _x( '%s Settings', '%s theme name', 'assistant' ), $theme->Name ); | |
| 32 | - } elseif ( $subtype === 'bb_settings' ) { | |
| 33 | - if ( ! class_exists( '\FLBuilderModel') ) { | |
| 34 | - return rest_ensure_response( [ 'error' => __( 'Unable to load BeaverBuilder settings exporter.' ) ] ); | |
| 35 | - } | |
| 36 | - | |
| 37 | - if ( ! current_user_can( 'manage_options' ) ) { | |
| 38 | - return rest_ensure_response( [ 'error' => __( 'You do not have permission to export BeaverBuilder settings.' ) ] ); | |
| 39 | - } | |
| 40 | - | |
| 41 | - $possible_options = [ | |
| 42 | - 'admin_settings', | |
| 43 | - 'global_settings', | |
| 44 | - 'global_styles', | |
| 45 | - 'global_colors', | |
| 46 | - ]; | |
| 47 | - $settings = []; | |
| 48 | - $settings['builder_global_settings'] = \FLBuilderModel::get_global_settings(); | |
| 49 | - $settings['admin_settings'] = []; | |
| 50 | - $all_settings = true; | |
| 51 | - $selected_settings = []; | |
| 52 | - | |
| 53 | - foreach ( \FLBuilderAdminSettings::registered_settings() as $setting ) { | |
| 54 | - $settings['admin_settings'][ $setting ] = get_option( $setting ); | |
| 55 | - } | |
| 56 | - | |
| 57 | - if ( class_exists( 'FLBuilderGlobalStyles' ) ) { | |
| 58 | - $globals = \FLBuilderGlobalStyles::get_settings( false ); | |
| 59 | - $settings['global_colors'] = $globals->colors; | |
| 60 | - unset( $globals->colors ); | |
| 61 | - $settings['global_styles'] = $globals; | |
| 62 | - } | |
| 63 | - | |
| 64 | - foreach ( $possible_options as $option ) { | |
| 65 | - if ( ! in_array( $option, $options ) ) { | |
| 66 | - $all_settings = false; | |
| 67 | - } | |
| 68 | - } | |
| 69 | - | |
| 70 | - if ( in_array( 'admin_settings', $options ) ) { | |
| 71 | - $selected_settings[] = 'Admin'; | |
| 72 | - } else { | |
| 73 | - unset( $settings['admin_settings'] ); | |
| 74 | - } | |
| 75 | - | |
| 76 | - if ( in_array( 'global_settings', $options ) ) { | |
| 77 | - $selected_settings[] = 'Global Settings'; | |
| 78 | - } else { | |
| 79 | - unset( $settings['builder_global_settings'] ); | |
| 80 | - } | |
| 81 | - | |
| 82 | - if ( in_array( 'global_styles', $options ) ) { | |
| 83 | - $selected_settings[] = 'Styles'; | |
| 84 | - } else { | |
| 85 | - unset( $settings['global_styles'] ); | |
| 86 | - } | |
| 87 | - | |
| 88 | - if ( in_array( 'global_colors', $options ) ) { | |
| 89 | - $selected_settings[] = 'Colors'; | |
| 90 | - } else { | |
| 91 | - unset( $settings['global_colors'] ); | |
| 92 | - } | |
| 93 | - | |
| 94 | - if ( isset( $settings['global_colors'] ) && isset ( $globals->prefix ) ) { | |
| 95 | - $settings['global_colors_prefix'] = $globals->prefix; | |
| 96 | - } | |
| 97 | - | |
| 98 | - if ( $all_settings ) { | |
| 99 | - $name = 'All Settings'; | |
| 100 | - } else { | |
| 101 | - $name = implode( ', ', $selected_settings ); | |
| 102 | - } | |
| 103 | - | |
| 104 | - $data = json_encode( $settings ); | |
| 105 | - } | |
| 106 | - | |
| 107 | - $screenshot = isset( $_POST['screenshot'] ) ? ScreenShotHelper::get_for_post_request( home_url(), false ) : null; | |
| 108 | - | |
| 109 | - return $client->libraries->create_item( | |
| 110 | - $library_id, | |
| 111 | - [ | |
| 112 | - 'name' => $name, | |
| 113 | - 'type' => 'settings', | |
| 114 | - 'subtype' => $subtype, | |
| 115 | - 'data' => $data, | |
| 116 | - 'media' => [ | |
| 117 | - 'attachments' => is_array( $data ) ? MediaPathHelper::get_image_paths_from_data( $data ) : MediaPathHelper::get_image_paths_from_string( $data ) | |
| 118 | - ], | |
| 119 | - 'screenshot' => $screenshot, | |
| 120 | - ] | |
| 121 | - ); | |
| 122 | - } | |
| 123 | - | |
| 124 | - /** | |
| 125 | - * @return array | |
| 126 | - */ | |
| 127 | - public function export_theme_settings() { | |
| 128 | - global $wp_customize; | |
| 129 | - | |
| 130 | - $theme_service = new ThemeService(); | |
| 131 | - $theme = $theme_service->get_current_theme_data(); | |
| 132 | - $template = get_template(); | |
| 133 | - | |
| 134 | - $data = [ | |
| 135 | - 'theme' => $theme, | |
| 136 | - 'mods' => [], | |
| 137 | - 'options' => [], | |
| 138 | - 'custom' => [], | |
| 139 | - 'css' => '', | |
| 140 | - ]; | |
| 141 | - | |
| 142 | - $ignore = [ | |
| 143 | - 'blogname', | |
| 144 | - 'blogdescription', | |
| 145 | - 'show_on_front', | |
| 146 | - 'page_on_front', | |
| 147 | - 'page_for_posts', | |
| 148 | - 'nav_menu_locations', | |
| 149 | - 'nav_menus_created_posts', | |
| 150 | - 'sidebars_widgets', | |
| 151 | - 'site_icon', | |
| 152 | - 'custom_css_post_id', | |
| 153 | - ]; | |
| 154 | - | |
| 155 | - $mods = get_theme_mods(); | |
| 156 | - $settings = $wp_customize->settings(); | |
| 157 | - | |
| 158 | - foreach ( $mods as $key => $value ) { | |
| 159 | - if ( in_array( $key, $ignore ) ) { | |
| 160 | - continue; | |
| 161 | - } | |
| 162 | - $data['mods'][ $key ] = $value; | |
| 163 | - } | |
| 164 | - | |
| 165 | - foreach ( $settings as $key => $setting ) { | |
| 166 | - if ( 'option' === $setting->type ) { | |
| 167 | - if ( 0 === strpos( $key, 'widget_' ) || 0 === strpos( $key, 'sidebars_' ) ) { | |
| 168 | - continue; | |
| 169 | - } elseif ( in_array( $key, $ignore ) ) { | |
| 170 | - continue; | |
| 171 | - } | |
| 172 | - $data['options'][ $key ] = $setting->value(); | |
| 173 | - } | |
| 174 | - } | |
| 175 | - | |
| 176 | - if ( function_exists( 'wp_get_custom_css_post' ) ) { | |
| 177 | - $data['css'] = wp_get_custom_css(); | |
| 178 | - } | |
| 179 | - | |
| 180 | - if ( isset( $this->helpers[ $template ] ) ) { | |
| 181 | - $helper = new $this->helpers[ $template ]; | |
| 182 | - $data['custom'] = json_encode( $helper->export() ); | |
| 183 | - } | |
| 184 | - | |
| 185 | - return $data; | |
| 186 | - } | |
| 187 | - | |
| 188 | - /** | |
| 189 | - * @param int $item_id | |
| 190 | - * @return array | |
| 191 | - */ | |
| 192 | - public function import_from_cloud( $item_id ) { | |
| 193 | - $client = new CloudClient; | |
| 194 | - $item = $client->libraries->get_item( $item_id ); | |
| 195 | - | |
| 196 | - if ($item->subtype === 'theme_settings') { | |
| 197 | - return $this->import_theme_settings_from_cloud( $item ); | |
| 198 | - } elseif ( $item->subtype === 'bb_settings' ) { | |
| 199 | - // Convert settings back into array | |
| 200 | - $item->data = json_decode(json_encode($item->data), true); | |
| 201 | - return $this->import_bb_settings( $item ); | |
| 202 | - } else { | |
| 203 | - return rest_ensure_response( [ 'error' => __( 'Unsupported settings type.' ) ] ); | |
| 204 | - } | |
| 205 | - } | |
| 206 | - | |
| 207 | - public function import_theme_settings_from_cloud( $item ) { | |
| 208 | - if ( ! is_object( $item ) || empty( $item->data ) ) { | |
| 209 | - return rest_ensure_response( [ 'error' => __( 'Missing item data.' ) ] ); | |
| 210 | - } | |
| 211 | - | |
| 212 | - $can_import = $this->can_import_settings( $item->data ); | |
| 213 | - | |
| 214 | - if ( is_wp_error( $can_import ) ) { | |
| 215 | - return rest_ensure_response( [ | |
| 216 | - 'error' => $can_import->get_error_message() | |
| 217 | - ] ); | |
| 218 | - } | |
| 219 | - | |
| 220 | - $data = $this->import_media_from_library( $item ); | |
| 221 | - $this->import_theme_settings( $data ); | |
| 222 | - | |
| 223 | - return rest_ensure_response( [ | |
| 224 | - 'success' => true | |
| 225 | - ] ); | |
| 226 | - } | |
| 227 | - | |
| 228 | - public function import_bb_settings( $item ) { | |
| 229 | - $data = $item->data; | |
| 230 | - | |
| 231 | - if ( !class_exists( '\FLBuilderModel') ) { | |
| 232 | - return rest_ensure_response( [ 'error' => __( 'Unable to load BeaverBuilder settings importer.' ) ] ); | |
| 233 | - } | |
| 234 | - | |
| 235 | - if ( ! current_user_can( 'manage_options' ) ) { | |
| 236 | - return rest_ensure_response( [ 'error' => __( 'You do not have permission to export BeaverBuilder settings.' ) ] ); | |
| 237 | - } | |
| 238 | - | |
| 239 | - if ( isset( $data['builder_global_settings'] ) ) { | |
| 240 | - update_option( '_fl_builder_settings', $data['builder_global_settings'], true ); | |
| 241 | - } | |
| 242 | - | |
| 243 | - // loop through admin settings | |
| 244 | - if ( isset( $data['admin_settings'] ) ) { | |
| 245 | - $settings = $data['admin_settings']; | |
| 246 | - | |
| 247 | - foreach ( $settings as $key => $setting ) { | |
| 248 | - update_option( $key, $setting, true ); | |
| 249 | - } | |
| 250 | - } | |
| 251 | - | |
| 252 | - $globals = get_option( '_fl_builder_styles' ); | |
| 253 | - if ( isset( $data['global_styles'] ) ) { | |
| 254 | - $backup_colors = isset( $globals->colors ) ? $globals->colors : array(); | |
| 255 | - $new_settings = (object) $data['global_styles']; | |
| 256 | - $new_settings->colors = $backup_colors; | |
| 257 | - $globals = $new_settings; | |
| 258 | - \FLBuilderUtils::update_option( '_fl_builder_styles', $globals, true ); | |
| 259 | - } | |
| 260 | - | |
| 261 | - // global styles/colors... | |
| 262 | - if ( isset( $data['global_colors'] ) ) { | |
| 263 | - // get current settings and swap out colours | |
| 264 | - $globals = $globals ? $globals : \FLBuilderGlobalStyles::get_settings( false ); | |
| 265 | - $current = $globals->colors; | |
| 266 | - | |
| 267 | - $new = array_merge( (array) $current, (array) $data['global_colors'] ); | |
| 268 | - | |
| 269 | - // filter out duplicates | |
| 270 | - $serialized = array_map( 'serialize', $new ); | |
| 271 | - $unique = array_unique( $serialized ); | |
| 272 | - $globals->colors = array_intersect_key( $new, $unique ); | |
| 273 | - | |
| 274 | - foreach ( $globals->colors as $k => $color ) { | |
| 275 | - if ( empty( $color ) || ! $color['color'] ) { | |
| 276 | - unset( $globals->colors[ $k ] ); | |
| 277 | - } | |
| 278 | - } | |
| 279 | - | |
| 280 | - if ( isset( $data->global_colors_prefix ) ) { | |
| 281 | - $globals->prefix = $data->global_colors_prefix; | |
| 282 | - } | |
| 283 | - | |
| 284 | - // Fixes global colors not starting with 0 key | |
| 285 | - $globals->colors = array_values( $globals->colors ); | |
| 286 | - | |
| 287 | - \FLBuilderUtils::update_option( '_fl_builder_styles', $globals, true ); | |
| 288 | - } | |
| 289 | - | |
| 290 | - \FLBuilderModel::delete_asset_cache_for_all_posts(); | |
| 291 | - | |
| 292 | - return rest_ensure_response( [ | |
| 293 | - 'success' => true | |
| 294 | - ] ); | |
| 295 | - } | |
| 296 | - | |
| 297 | - public function import_media_from_library( $item ) { | |
| 298 | - $service = new MediaLibraryService(); | |
| 299 | - $data = $item->data; | |
| 300 | - $media = $item->media; | |
| 301 | - $imported = []; | |
| 302 | - | |
| 303 | - if ( isset( $media->attachments ) ) { | |
| 304 | - foreach ( $media->attachments as $attachment ) { | |
| 305 | - $response = $service->import_cloud_media( $attachment ); | |
| 306 | - $imported[ $attachment->file_name ] = wp_get_attachment_metadata( $response['id'] ); | |
| 307 | - $imported[ $attachment->file_name ]['id'] = $response['id']; | |
| 308 | - } | |
| 309 | - } | |
| 310 | - | |
| 311 | - return MediaPathHelper::replace_imported_attachment_urls_in_data( $data, $imported ); | |
| 312 | - } | |
| 313 | - | |
| 314 | - /** | |
| 315 | - * @param array $data | |
| 316 | - * @return bool|WP_Error | |
| 317 | - */ | |
| 318 | - public function can_import_settings( $data ) { | |
| 319 | - $theme = $data->theme; | |
| 320 | - $slug = $theme->slug; | |
| 321 | - $parent_slug = $theme->parent ? $theme->parent->slug : null; | |
| 322 | - $stylesheet = get_stylesheet(); | |
| 323 | - $template = get_template(); | |
| 324 | - | |
| 325 | - if ( ( $theme->parent && $parent_slug !== $template ) || ( $slug !== $stylesheet && $parent_slug !== $stylesheet ) ) { | |
| 326 | - return new \WP_Error( 'import', __( 'Import failed! These settings are not for the current theme.' ) ); | |
| 327 | - } | |
| 328 | - | |
| 329 | - return true; | |
| 330 | - } | |
| 331 | - | |
| 332 | - /** | |
| 333 | - * @param array $data | |
| 334 | - * @return bool|WP_Error | |
| 335 | - */ | |
| 336 | - public function import_theme_settings( $data ) { | |
| 337 | - global $wp_customize; | |
| 338 | - | |
| 339 | - $template = get_template(); | |
| 340 | - $can_import = $this->can_import_settings( $data ); | |
| 341 | - | |
| 342 | - if ( is_wp_error( $can_import ) ) { | |
| 343 | - return $can_import; | |
| 344 | - } | |
| 345 | - | |
| 346 | - foreach ( $data->options as $option_key => $option_value ) { | |
| 347 | - $option = new CustomizerOptionHelper( $wp_customize, $option_key, array( | |
| 348 | - 'default' => '', | |
| 349 | - 'type' => 'option', | |
| 350 | - 'capability' => 'edit_theme_options' | |
| 351 | - ) ); | |
| 352 | - $option->import( $option_value ); | |
| 353 | - } | |
| 354 | - | |
| 355 | - if( function_exists( 'wp_update_custom_css_post' ) && $data->css ) { | |
| 356 | - wp_update_custom_css_post( $data->css ); | |
| 357 | - } | |
| 358 | - | |
| 359 | - do_action( 'customize_save', $wp_customize ); | |
| 360 | - | |
| 361 | - foreach ( $data->mods as $key => $val ) { | |
| 362 | - | |
| 363 | - if( is_object( $val ) ) { | |
| 364 | - $val = json_decode( json_encode( $val ), true ); | |
| 365 | - } | |
| 366 | - do_action( 'customize_save_' . $key, $wp_customize ); | |
| 367 | - set_theme_mod( $key, $val ); | |
| 368 | - } | |
| 369 | - | |
| 370 | - if ( isset( $this->helpers[ $template ] ) ) { | |
| 371 | - $helper = new $this->helpers[ $template ]; | |
| 372 | - $settings = json_decode( $data->custom, 1 ); | |
| 373 | - $helper->import( $settings ); | |
| 374 | - } | |
| 375 | - | |
| 376 | - do_action( 'customize_save_after', $wp_customize ); | |
| 377 | - | |
| 378 | - return true; | |
| 379 | - } | |
| 380 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace FL\Assistant\Services; | |
| 4 | + | |
| 5 | +use FL\Assistant\Services\ThemeService; | |
| 6 | +use FL\Assistant\Helpers\CustomizerOptionHelper; | |
| 7 | +use FL\Assistant\Clients\Cloud\CloudClient; | |
| 8 | +use FL\Assistant\Helpers\MediaPathHelper; | |
| 9 | +use FL\Assistant\Helpers\ScreenshotHelper; | |
| 10 | + | |
| 11 | +class CustomizerService { | |
| 12 | + | |
| 13 | + /** | |
| 14 | + * @param int $library_id | |
| 15 | + * @return array | |
| 16 | + */ | |
| 17 | + public function export_to_cloud( $library_id ) { | |
| 18 | + $client = new CloudClient; | |
| 19 | + $data = $this->export_settings(); | |
| 20 | + $theme = wp_get_theme(); | |
| 21 | + | |
| 22 | + return $client->libraries->create_item( | |
| 23 | + $library_id, | |
| 24 | + [ | |
| 25 | + 'name' => sprintf( _x( '%s Settings', '%s theme name', 'fl-assistant' ), $theme->Name ), | |
| 26 | + 'type' => 'theme_settings', | |
| 27 | + 'data' => $data, | |
| 28 | + 'media' => [ | |
| 29 | + 'attachments' => MediaPathHelper::get_image_paths_from_data( $data ) | |
| 30 | + ], | |
| 31 | + 'screenshot' => ScreenshotHelper::get_for_post_request( home_url(), false ), | |
| 32 | + ] | |
| 33 | + ); | |
| 34 | + } | |
| 35 | + | |
| 36 | + /** | |
| 37 | + * @return array | |
| 38 | + */ | |
| 39 | + public function export_settings() { | |
| 40 | + global $wp_customize; | |
| 41 | + | |
| 42 | + $theme_service = new ThemeService(); | |
| 43 | + $theme = $theme_service->get_current_theme_data(); | |
| 44 | + | |
| 45 | + $data = array( | |
| 46 | + 'mods' => array(), | |
| 47 | + 'options' => array(), | |
| 48 | + 'css' => '', | |
| 49 | + 'theme' => $theme, | |
| 50 | + ); | |
| 51 | + | |
| 52 | + $ignore = array( | |
| 53 | + 'blogname', | |
| 54 | + 'blogdescription', | |
| 55 | + 'show_on_front', | |
| 56 | + 'page_on_front', | |
| 57 | + 'page_for_posts', | |
| 58 | + 'nav_menu_locations', | |
| 59 | + 'nav_menus_created_posts', | |
| 60 | + 'sidebars_widgets', | |
| 61 | + 'site_icon', | |
| 62 | + 'custom_css_post_id', | |
| 63 | + ); | |
| 64 | + | |
| 65 | + $mods = get_theme_mods(); | |
| 66 | + $settings = $wp_customize->settings(); | |
| 67 | + | |
| 68 | + foreach ( $mods as $key => $value ) { | |
| 69 | + if ( in_array( $key, $ignore ) ) { | |
| 70 | + continue; | |
| 71 | + } | |
| 72 | + $data['mods'][ $key ] = $value; | |
| 73 | + } | |
| 74 | + | |
| 75 | + foreach ( $settings as $key => $setting ) { | |
| 76 | + if ( 'option' === $setting->type ) { | |
| 77 | + if ( 0 === strpos( $key, 'widget_' ) || 0 === strpos( $key, 'sidebars_' ) ) { | |
| 78 | + continue; | |
| 79 | + } elseif ( in_array( $key, $ignore ) ) { | |
| 80 | + continue; | |
| 81 | + } | |
| 82 | + $data['options'][ $key ] = $setting->value(); | |
| 83 | + } | |
| 84 | + } | |
| 85 | + | |
| 86 | + if ( function_exists( 'wp_get_custom_css_post' ) ) { | |
| 87 | + $data['css'] = wp_get_custom_css(); | |
| 88 | + } | |
| 89 | + | |
| 90 | + return $data; | |
| 91 | + } | |
| 92 | + | |
| 93 | + /** | |
| 94 | + * @param int $item_id | |
| 95 | + * @return array | |
| 96 | + */ | |
| 97 | + public function import_from_cloud( $item_id ) { | |
| 98 | + $client = new CloudClient; | |
| 99 | + $item = $client->libraries->get_item( $item_id ); | |
| 100 | + | |
| 101 | + if ( ! is_object( $item ) || empty( $item->data ) ) { | |
| 102 | + return rest_ensure_response( [ 'error' => __( 'Missing item data.' ) ] ); | |
| 103 | + } | |
| 104 | + | |
| 105 | + $can_import = $this->can_import_settings( $item->data ); | |
| 106 | + | |
| 107 | + if ( is_wp_error( $can_import ) ) { | |
| 108 | + return rest_ensure_response( [ | |
| 109 | + 'error' => $can_import->get_error_message() | |
| 110 | + ] ); | |
| 111 | + } | |
| 112 | + | |
| 113 | + $data = $this->import_media_from_library( $item ); | |
| 114 | + $this->import_settings( $data ); | |
| 115 | + | |
| 116 | + return rest_ensure_response( [ | |
| 117 | + 'success' => true | |
| 118 | + ] ); | |
| 119 | + } | |
| 120 | + | |
| 121 | + public function import_media_from_library( $item ) { | |
| 122 | + $service = new MediaLibraryService(); | |
| 123 | + $data = $item->data; | |
| 124 | + $media = $item->media; | |
| 125 | + $imported = []; | |
| 126 | + | |
| 127 | + if ( isset( $media->attachments ) ) { | |
| 128 | + foreach ( $media->attachments as $attachment ) { | |
| 129 | + $response = $service->import_cloud_media( $attachment ); | |
| 130 | + $imported[ $attachment->file_name ] = wp_get_attachment_metadata( $response['id'] ); | |
| 131 | + $imported[ $attachment->file_name ]['id'] = $response['id']; | |
| 132 | + } | |
| 133 | + } | |
| 134 | + | |
| 135 | + return MediaPathHelper::replace_imported_attachment_urls_in_data( $data, $imported ); | |
| 136 | + } | |
| 137 | + | |
| 138 | + /** | |
| 139 | + * @param array $data | |
| 140 | + * @return bool|WP_Error | |
| 141 | + */ | |
| 142 | + public function can_import_settings( $data ) { | |
| 143 | + $theme = $data->theme; | |
| 144 | + $slug = $theme->slug; | |
| 145 | + $parent_slug = $theme->parent ? $theme->parent->slug : null; | |
| 146 | + $stylesheet = get_stylesheet(); | |
| 147 | + $template = get_template(); | |
| 148 | + | |
| 149 | + if ( ( $theme->parent && $parent_slug !== $template ) || ( $slug !== $stylesheet && $parent_slug !== $stylesheet ) ) { | |
| 150 | + return new \WP_Error( 'import', __( 'Import failed! These settings are not for the current theme.' ) ); | |
| 151 | + } | |
| 152 | + | |
| 153 | + return true; | |
| 154 | + } | |
| 155 | + | |
| 156 | + /** | |
| 157 | + * @param array $data | |
| 158 | + * @return bool|WP_Error | |
| 159 | + */ | |
| 160 | + public function import_settings( $data ) { | |
| 161 | + global $wp_customize; | |
| 162 | + | |
| 163 | + $can_import = $this->can_import_settings( $data ); | |
| 164 | + | |
| 165 | + if ( is_wp_error( $can_import ) ) { | |
| 166 | + return $can_import; | |
| 167 | + } | |
| 168 | + | |
| 169 | + foreach ( $data->options as $option_key => $option_value ) { | |
| 170 | + $option = new CustomizerOptionHelper( $wp_customize, $option_key, array( | |
| 171 | + 'default' => '', | |
| 172 | + 'type' => 'option', | |
| 173 | + 'capability' => 'edit_theme_options' | |
| 174 | + ) ); | |
| 175 | + $option->import( $option_value ); | |
| 176 | + } | |
| 177 | + | |
| 178 | + if( function_exists( 'wp_update_custom_css_post' ) && $data->css ) { | |
| 179 | + wp_update_custom_css_post( $data->css ); | |
| 180 | + } | |
| 181 | + | |
| 182 | + do_action( 'customize_save', $wp_customize ); | |
| 183 | + | |
| 184 | + foreach ( $data->mods as $key => $val ) { | |
| 185 | + do_action( 'customize_save_' . $key, $wp_customize ); | |
| 186 | + set_theme_mod( $key, $val ); | |
| 187 | + } | |
| 188 | + | |
| 189 | + do_action( 'customize_save_after', $wp_customize ); | |
| 190 | + | |
| 191 | + return true; | |
| 192 | + } | |
| 193 | +} | |