| @@ -6,12 +6,20 @@ | ||
| 6 | 6 | use FL\Assistant\Helpers\CustomizerOptionHelper; |
| 7 | 7 | use FL\Assistant\Clients\Cloud\CloudClient; |
| 8 | 8 | use FL\Assistant\Helpers\MediaPathHelper; |
| 9 | 9 | use FL\Assistant\Helpers\ScreenshotHelper; |
| 10 | +use FL\Assistant\Helpers\AstraSettingsHelper; | |
| 10 | 11 | |
| 11 | 12 | class CustomizerService { |
| 12 | 13 | |
| 13 | 14 | /** |
| 15 | + * @var array | |
| 16 | + */ | |
| 17 | + private $helpers = [ | |
| 18 | + 'astra' => AstraSettingsHelper::class, | |
| 19 | + ]; | |
| 20 | + | |
| 21 | + /** | |
| 14 | 22 | * @param int $library_id |
| 15 | 23 | * @return array |
| 16 | 24 | */ |
| 17 | 25 | public function export_to_cloud( $library_id ) { |
| @@ -21,9 +29,9 @@ | ||
| 21 | 29 | |
| 22 | 30 | return $client->libraries->create_item( |
| 23 | 31 | $library_id, |
| 24 | 32 | [ |
| 25 | - 'name' => sprintf( _x( '%s Settings', '%s theme name', 'fl-assistant' ), $theme->Name ), | |
| 33 | + 'name' => sprintf( _x( '%s Settings', '%s theme name', 'assistant' ), $theme->Name ), | |
| 26 | 34 | 'type' => 'theme_settings', |
| 27 | 35 | 'data' => $data, |
| 28 | 36 | 'media' => [ |
| 29 | 37 | 'attachments' => MediaPathHelper::get_image_paths_from_data( $data ) |
| @@ -40,17 +48,19 @@ | ||
| 40 | 48 | global $wp_customize; |
| 41 | 49 | |
| 42 | 50 | $theme_service = new ThemeService(); |
| 43 | 51 | $theme = $theme_service->get_current_theme_data(); |
| 52 | + $template = get_template(); | |
| 44 | 53 | |
| 45 | - $data = array( | |
| 46 | - 'mods' => array(), | |
| 47 | - 'options' => array(), | |
| 54 | + $data = [ | |
| 55 | + 'theme' => $theme, | |
| 56 | + 'mods' => [], | |
| 57 | + 'options' => [], | |
| 58 | + 'custom' => [], | |
| 48 | 59 | 'css' => '', |
| 49 | - 'theme' => $theme, | |
| 50 | - ); | |
| 60 | + ]; | |
| 51 | 61 | |
| 52 | - $ignore = array( | |
| 62 | + $ignore = [ | |
| 53 | 63 | 'blogname', |
| 54 | 64 | 'blogdescription', |
| 55 | 65 | 'show_on_front', |
| 56 | 66 | 'page_on_front', |
| @@ -59,9 +69,9 @@ | ||
| 59 | 69 | 'nav_menus_created_posts', |
| 60 | 70 | 'sidebars_widgets', |
| 61 | 71 | 'site_icon', |
| 62 | 72 | 'custom_css_post_id', |
| 63 | - ); | |
| 73 | + ]; | |
| 64 | 74 | |
| 65 | 75 | $mods = get_theme_mods(); |
| 66 | 76 | $settings = $wp_customize->settings(); |
| 67 | 77 | |
| @@ -86,8 +96,13 @@ | ||
| 86 | 96 | if ( function_exists( 'wp_get_custom_css_post' ) ) { |
| 87 | 97 | $data['css'] = wp_get_custom_css(); |
| 88 | 98 | } |
| 89 | 99 | |
| 100 | + if ( isset( $this->helpers[ $template ] ) ) { | |
| 101 | + $helper = new $this->helpers[ $template ]; | |
| 102 | + $data['custom'] = json_encode( $helper->export() ); | |
| 103 | + } | |
| 104 | + | |
| 90 | 105 | return $data; |
| 91 | 106 | } |
| 92 | 107 | |
| 93 | 108 | /** |
| @@ -141,13 +156,13 @@ | ||
| 141 | 156 | */ |
| 142 | 157 | public function can_import_settings( $data ) { |
| 143 | 158 | $theme = $data->theme; |
| 144 | 159 | $slug = $theme->slug; |
| 145 | - $parentSlug = $theme->parent ? $theme->parent->slug : null; | |
| 160 | + $parent_slug = $theme->parent ? $theme->parent->slug : null; | |
| 146 | 161 | $stylesheet = get_stylesheet(); |
| 147 | 162 | $template = get_template(); |
| 148 | 163 | |
| 149 | - if ( $slug !== $stylesheet && $slug !== $template && $parentSlug !== $stylesheet ) { | |
| 164 | + if ( ( $theme->parent && $parent_slug !== $template ) || ( $slug !== $stylesheet && $parent_slug !== $stylesheet ) ) { | |
| 150 | 165 | return new \WP_Error( 'import', __( 'Import failed! These settings are not for the current theme.' ) ); |
| 151 | 166 | } |
| 152 | 167 | |
| 153 | 168 | return true; |
| @@ -159,8 +174,9 @@ | ||
| 159 | 174 | */ |
| 160 | 175 | public function import_settings( $data ) { |
| 161 | 176 | global $wp_customize; |
| 162 | 177 | |
| 178 | + $template = get_template(); | |
| 163 | 179 | $can_import = $this->can_import_settings( $data ); |
| 164 | 180 | |
| 165 | 181 | if ( is_wp_error( $can_import ) ) { |
| 166 | 182 | return $can_import; |
| @@ -181,10 +197,20 @@ | ||
| 181 | 197 | |
| 182 | 198 | do_action( 'customize_save', $wp_customize ); |
| 183 | 199 | |
| 184 | 200 | foreach ( $data->mods as $key => $val ) { |
| 201 | + | |
| 202 | + if( is_object( $val ) ) { | |
| 203 | + $val = json_decode( json_encode( $val ), true ); | |
| 204 | + } | |
| 185 | 205 | do_action( 'customize_save_' . $key, $wp_customize ); |
| 186 | 206 | set_theme_mod( $key, $val ); |
| 207 | + } | |
| 208 | + | |
| 209 | + if ( isset( $this->helpers[ $template ] ) ) { | |
| 210 | + $helper = new $this->helpers[ $template ]; | |
| 211 | + $settings = json_decode( $data->custom, 1 ); | |
| 212 | + $helper->import( $settings ); | |
| 187 | 213 | } |
| 188 | 214 | |
| 189 | 215 | do_action( 'customize_save_after', $wp_customize ); |
| 190 | 216 | |