| @@ -3,27 +3,144 @@ | ||
| 3 | 3 | namespace FL\Assistant\Services; |
| 4 | 4 | |
| 5 | 5 | use FL\Assistant\Services\ThemeService; |
| 6 | 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; | |
| 7 | 11 | |
| 8 | 12 | class CustomizerService { |
| 9 | 13 | |
| 10 | 14 | /** |
| 15 | + * @var array | |
| 16 | + */ | |
| 17 | + private $helpers = [ | |
| 18 | + 'astra' => AstraSettingsHelper::class, | |
| 19 | + ]; | |
| 20 | + | |
| 21 | + /** | |
| 22 | + * @param int $library_id | |
| 11 | 23 | * @return array |
| 12 | 24 | */ |
| 13 | - public function export_settings() { | |
| 14 | - $wp_customize = $this->init_customizer(); | |
| 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 | + | |
| 15 | 130 | $theme_service = new ThemeService(); |
| 16 | 131 | $theme = $theme_service->get_current_theme_data(); |
| 132 | + $template = get_template(); | |
| 17 | 133 | |
| 18 | - $data = array( | |
| 19 | - 'mods' => array(), | |
| 20 | - 'options' => array(), | |
| 134 | + $data = [ | |
| 135 | + 'theme' => $theme, | |
| 136 | + 'mods' => [], | |
| 137 | + 'options' => [], | |
| 138 | + 'custom' => [], | |
| 21 | 139 | 'css' => '', |
| 22 | - 'theme' => $theme, | |
| 23 | - ); | |
| 140 | + ]; | |
| 24 | 141 | |
| 25 | - $ignore = array( | |
| 142 | + $ignore = [ | |
| 26 | 143 | 'blogname', |
| 27 | 144 | 'blogdescription', |
| 28 | 145 | 'show_on_front', |
| 29 | 146 | 'page_on_front', |
| @@ -32,9 +149,9 @@ | ||
| 32 | 149 | 'nav_menus_created_posts', |
| 33 | 150 | 'sidebars_widgets', |
| 34 | 151 | 'site_icon', |
| 35 | 152 | 'custom_css_post_id', |
| 36 | - ); | |
| 153 | + ]; | |
| 37 | 154 | |
| 38 | 155 | $mods = get_theme_mods(); |
| 39 | 156 | $settings = $wp_customize->settings(); |
| 40 | 157 | |
| @@ -59,23 +176,154 @@ | ||
| 59 | 176 | if ( function_exists( 'wp_get_custom_css_post' ) ) { |
| 60 | 177 | $data['css'] = wp_get_custom_css(); |
| 61 | 178 | } |
| 62 | 179 | |
| 180 | + if ( isset( $this->helpers[ $template ] ) ) { | |
| 181 | + $helper = new $this->helpers[ $template ]; | |
| 182 | + $data['custom'] = json_encode( $helper->export() ); | |
| 183 | + } | |
| 184 | + | |
| 63 | 185 | return $data; |
| 64 | 186 | } |
| 65 | 187 | |
| 66 | 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 | + /** | |
| 67 | 315 | * @param array $data |
| 68 | 316 | * @return bool|WP_Error |
| 69 | 317 | */ |
| 70 | 318 | public function can_import_settings( $data ) { |
| 71 | - $theme = $data['theme']; | |
| 72 | - $slug = $theme['slug']; | |
| 73 | - $parentSlug = $theme['parent'] ? $theme['parent']['slug'] : null; | |
| 319 | + $theme = $data->theme; | |
| 320 | + $slug = $theme->slug; | |
| 321 | + $parent_slug = $theme->parent ? $theme->parent->slug : null; | |
| 74 | 322 | $stylesheet = get_stylesheet(); |
| 75 | 323 | $template = get_template(); |
| 76 | 324 | |
| 77 | - if ( $slug !== $stylesheet && $slug !== $template && $parentSlug !== $stylesheet ) { | |
| 325 | + if ( ( $theme->parent && $parent_slug !== $template ) || ( $slug !== $stylesheet && $parent_slug !== $stylesheet ) ) { | |
| 78 | 326 | return new \WP_Error( 'import', __( 'Import failed! These settings are not for the current theme.' ) ); |
| 79 | 327 | } |
| 80 | 328 | |
| 81 | 329 | return true; |
| @@ -84,10 +332,12 @@ | ||
| 84 | 332 | /** |
| 85 | 333 | * @param array $data |
| 86 | 334 | * @return bool|WP_Error |
| 87 | 335 | */ |
| 88 | - public function import_settings( $data ) { | |
| 89 | - $wp_customize = $this->init_customizer(); | |
| 336 | + public function import_theme_settings( $data ) { | |
| 337 | + global $wp_customize; | |
| 338 | + | |
| 339 | + $template = get_template(); | |
| 90 | 340 | $can_import = $this->can_import_settings( $data ); |
| 91 | 341 | |
| 92 | 342 | if ( is_wp_error( $can_import ) ) { |
| 93 | 343 | return $can_import; |
| @@ -92,9 +342,9 @@ | ||
| 92 | 342 | if ( is_wp_error( $can_import ) ) { |
| 93 | 343 | return $can_import; |
| 94 | 344 | } |
| 95 | 345 | |
| 96 | - foreach ( $data['options'] as $option_key => $option_value ) { | |
| 346 | + foreach ( $data->options as $option_key => $option_value ) { | |
| 97 | 347 | $option = new CustomizerOptionHelper( $wp_customize, $option_key, array( |
| 98 | 348 | 'default' => '', |
| 99 | 349 | 'type' => 'option', |
| 100 | 350 | 'capability' => 'edit_theme_options' |
| @@ -101,46 +351,30 @@ | ||
| 101 | 351 | ) ); |
| 102 | 352 | $option->import( $option_value ); |
| 103 | 353 | } |
| 104 | 354 | |
| 105 | - if( function_exists( 'wp_update_custom_css_post' ) && $data['css'] ) { | |
| 106 | - wp_update_custom_css_post( $data['css'] ); | |
| 355 | + if( function_exists( 'wp_update_custom_css_post' ) && $data->css ) { | |
| 356 | + wp_update_custom_css_post( $data->css ); | |
| 107 | 357 | } |
| 108 | 358 | |
| 109 | 359 | do_action( 'customize_save', $wp_customize ); |
| 110 | 360 | |
| 111 | - foreach ( $data['mods'] as $key => $val ) { | |
| 361 | + foreach ( $data->mods as $key => $val ) { | |
| 362 | + | |
| 363 | + if( is_object( $val ) ) { | |
| 364 | + $val = json_decode( json_encode( $val ), true ); | |
| 365 | + } | |
| 112 | 366 | do_action( 'customize_save_' . $key, $wp_customize ); |
| 113 | 367 | set_theme_mod( $key, $val ); |
| 114 | 368 | } |
| 115 | 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 | + | |
| 116 | 376 | do_action( 'customize_save_after', $wp_customize ); |
| 117 | 377 | |
| 118 | 378 | return true; |
| 119 | - } | |
| 120 | - | |
| 121 | - /** | |
| 122 | - * Dynamically initialize the customizer. Inspired by the core | |
| 123 | - * function _wp_customize_publish_changeset. | |
| 124 | - * | |
| 125 | - * @return object | |
| 126 | - */ | |
| 127 | - public function init_customizer() { | |
| 128 | - global $wp_customize; | |
| 129 | - | |
| 130 | - if ( ! class_exists( 'WP_Customize_Manager' ) ) { | |
| 131 | - require_once ABSPATH . WPINC . '/class-wp-customize-manager.php'; | |
| 132 | - } | |
| 133 | - if ( ! $wp_customize ) { | |
| 134 | - $wp_customize = new \WP_Customize_Manager(); | |
| 135 | - } | |
| 136 | - if ( ! did_action( 'customize_register' ) ) { | |
| 137 | - remove_action( 'customize_register', array( $wp_customize, 'register_controls' ) ); | |
| 138 | - $wp_customize->register_controls(); | |
| 139 | - $wp_customize->start_previewing_theme(); | |
| 140 | - do_action( 'customize_register', $wp_customize ); | |
| 141 | - $wp_customize->stop_previewing_theme(); | |
| 142 | - } | |
| 143 | - | |
| 144 | - return $wp_customize; | |
| 145 | 379 | } |
| 146 | 380 | } |