| @@ -3,16 +3,43 @@ | ||
| 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; | |
| 7 | 10 | |
| 8 | 11 | class CustomizerService { |
| 9 | 12 | |
| 10 | 13 | /** |
| 14 | + * @param int $library_id | |
| 11 | 15 | * @return array |
| 12 | 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 | + */ | |
| 13 | 39 | public function export_settings() { |
| 14 | - $wp_customize = $this->init_customizer(); | |
| 40 | + global $wp_customize; | |
| 41 | + | |
| 15 | 42 | $theme_service = new ThemeService(); |
| 16 | 43 | $theme = $theme_service->get_current_theme_data(); |
| 17 | 44 | |
| 18 | 45 | $data = array( |
| @@ -63,19 +90,64 @@ | ||
| 63 | 90 | return $data; |
| 64 | 91 | } |
| 65 | 92 | |
| 66 | 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 | + /** | |
| 67 | 139 | * @param array $data |
| 68 | 140 | * @return bool|WP_Error |
| 69 | 141 | */ |
| 70 | 142 | public function can_import_settings( $data ) { |
| 71 | - $theme = $data['theme']; | |
| 72 | - $slug = $theme['slug']; | |
| 73 | - $parentSlug = $theme['parent'] ? $theme['parent']['slug'] : null; | |
| 143 | + $theme = $data->theme; | |
| 144 | + $slug = $theme->slug; | |
| 145 | + $parent_slug = $theme->parent ? $theme->parent->slug : null; | |
| 74 | 146 | $stylesheet = get_stylesheet(); |
| 75 | 147 | $template = get_template(); |
| 76 | 148 | |
| 77 | - if ( $slug !== $stylesheet && $slug !== $template && $parentSlug !== $stylesheet ) { | |
| 149 | + if ( ( $theme->parent && $parent_slug !== $template ) || $slug !== $stylesheet ) { | |
| 78 | 150 | return new \WP_Error( 'import', __( 'Import failed! These settings are not for the current theme.' ) ); |
| 79 | 151 | } |
| 80 | 152 | |
| 81 | 153 | return true; |
| @@ -85,9 +157,10 @@ | ||
| 85 | 157 | * @param array $data |
| 86 | 158 | * @return bool|WP_Error |
| 87 | 159 | */ |
| 88 | 160 | public function import_settings( $data ) { |
| 89 | - $wp_customize = $this->init_customizer(); | |
| 161 | + global $wp_customize; | |
| 162 | + | |
| 90 | 163 | $can_import = $this->can_import_settings( $data ); |
| 91 | 164 | |
| 92 | 165 | if ( is_wp_error( $can_import ) ) { |
| 93 | 166 | return $can_import; |
| @@ -92,9 +165,9 @@ | ||
| 92 | 165 | if ( is_wp_error( $can_import ) ) { |
| 93 | 166 | return $can_import; |
| 94 | 167 | } |
| 95 | 168 | |
| 96 | - foreach ( $data['options'] as $option_key => $option_value ) { | |
| 169 | + foreach ( $data->options as $option_key => $option_value ) { | |
| 97 | 170 | $option = new CustomizerOptionHelper( $wp_customize, $option_key, array( |
| 98 | 171 | 'default' => '', |
| 99 | 172 | 'type' => 'option', |
| 100 | 173 | 'capability' => 'edit_theme_options' |
| @@ -101,15 +174,15 @@ | ||
| 101 | 174 | ) ); |
| 102 | 175 | $option->import( $option_value ); |
| 103 | 176 | } |
| 104 | 177 | |
| 105 | - if( function_exists( 'wp_update_custom_css_post' ) && $data['css'] ) { | |
| 106 | - wp_update_custom_css_post( $data['css'] ); | |
| 178 | + if( function_exists( 'wp_update_custom_css_post' ) && $data->css ) { | |
| 179 | + wp_update_custom_css_post( $data->css ); | |
| 107 | 180 | } |
| 108 | 181 | |
| 109 | 182 | do_action( 'customize_save', $wp_customize ); |
| 110 | 183 | |
| 111 | - foreach ( $data['mods'] as $key => $val ) { | |
| 184 | + foreach ( $data->mods as $key => $val ) { | |
| 112 | 185 | do_action( 'customize_save_' . $key, $wp_customize ); |
| 113 | 186 | set_theme_mod( $key, $val ); |
| 114 | 187 | } |
| 115 | 188 | |
| @@ -115,30 +188,6 @@ | ||
| 115 | 188 | |
| 116 | 189 | do_action( 'customize_save_after', $wp_customize ); |
| 117 | 190 | |
| 118 | 191 | 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 | - protected 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 | - do_action( 'customize_register', $wp_customize ); | |
| 140 | - } | |
| 141 | - | |
| 142 | - return $wp_customize; | |
| 143 | 192 | } |
| 144 | 193 | } |