| @@ -6,30 +6,118 @@ | ||
| 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 | - public function export_to_cloud( $library_id ) { | |
| 25 | + public function export_to_cloud( $library_id, $subtype, $options ) { | |
| 18 | 26 | $client = new CloudClient; |
| 19 | - $data = $this->export_settings(); | |
| 20 | - $theme = wp_get_theme(); | |
| 21 | 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 | + | |
| 22 | 109 | return $client->libraries->create_item( |
| 23 | 110 | $library_id, |
| 24 | 111 | [ |
| 25 | - 'name' => sprintf( _x( '%s Settings', '%s theme name', 'fl-assistant' ), $theme->Name ), | |
| 26 | - 'type' => 'theme_settings', | |
| 112 | + 'name' => $name, | |
| 113 | + 'type' => 'settings', | |
| 114 | + 'subtype' => $subtype, | |
| 27 | 115 | 'data' => $data, |
| 28 | 116 | 'media' => [ |
| 29 | - 'attachments' => MediaPathHelper::get_image_paths_from_data( $data ) | |
| 117 | + 'attachments' => is_array( $data ) ? MediaPathHelper::get_image_paths_from_data( $data ) : MediaPathHelper::get_image_paths_from_string( $data ) | |
| 30 | 118 | ], |
| 31 | - 'screenshot' => ScreenshotHelper::get_for_post_request( home_url(), false ), | |
| 119 | + 'screenshot' => $screenshot, | |
| 32 | 120 | ] |
| 33 | 121 | ); |
| 34 | 122 | } |
| 35 | 123 | |
| @@ -35,22 +123,24 @@ | ||
| 35 | 123 | |
| 36 | 124 | /** |
| 37 | 125 | * @return array |
| 38 | 126 | */ |
| 39 | - public function export_settings() { | |
| 127 | + public function export_theme_settings() { | |
| 40 | 128 | global $wp_customize; |
| 41 | 129 | |
| 42 | 130 | $theme_service = new ThemeService(); |
| 43 | 131 | $theme = $theme_service->get_current_theme_data(); |
| 132 | + $template = get_template(); | |
| 44 | 133 | |
| 45 | - $data = array( | |
| 46 | - 'mods' => array(), | |
| 47 | - 'options' => array(), | |
| 134 | + $data = [ | |
| 135 | + 'theme' => $theme, | |
| 136 | + 'mods' => [], | |
| 137 | + 'options' => [], | |
| 138 | + 'custom' => [], | |
| 48 | 139 | 'css' => '', |
| 49 | - 'theme' => $theme, | |
| 50 | - ); | |
| 140 | + ]; | |
| 51 | 141 | |
| 52 | - $ignore = array( | |
| 142 | + $ignore = [ | |
| 53 | 143 | 'blogname', |
| 54 | 144 | 'blogdescription', |
| 55 | 145 | 'show_on_front', |
| 56 | 146 | 'page_on_front', |
| @@ -59,9 +149,9 @@ | ||
| 59 | 149 | 'nav_menus_created_posts', |
| 60 | 150 | 'sidebars_widgets', |
| 61 | 151 | 'site_icon', |
| 62 | 152 | 'custom_css_post_id', |
| 63 | - ); | |
| 153 | + ]; | |
| 64 | 154 | |
| 65 | 155 | $mods = get_theme_mods(); |
| 66 | 156 | $settings = $wp_customize->settings(); |
| 67 | 157 | |
| @@ -86,8 +176,13 @@ | ||
| 86 | 176 | if ( function_exists( 'wp_get_custom_css_post' ) ) { |
| 87 | 177 | $data['css'] = wp_get_custom_css(); |
| 88 | 178 | } |
| 89 | 179 | |
| 180 | + if ( isset( $this->helpers[ $template ] ) ) { | |
| 181 | + $helper = new $this->helpers[ $template ]; | |
| 182 | + $data['custom'] = json_encode( $helper->export() ); | |
| 183 | + } | |
| 184 | + | |
| 90 | 185 | return $data; |
| 91 | 186 | } |
| 92 | 187 | |
| 93 | 188 | /** |
| @@ -97,8 +192,20 @@ | ||
| 97 | 192 | public function import_from_cloud( $item_id ) { |
| 98 | 193 | $client = new CloudClient; |
| 99 | 194 | $item = $client->libraries->get_item( $item_id ); |
| 100 | 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 ) { | |
| 101 | 208 | if ( ! is_object( $item ) || empty( $item->data ) ) { |
| 102 | 209 | return rest_ensure_response( [ 'error' => __( 'Missing item data.' ) ] ); |
| 103 | 210 | } |
| 104 | 211 | |
| @@ -110,9 +217,9 @@ | ||
| 110 | 217 | ] ); |
| 111 | 218 | } |
| 112 | 219 | |
| 113 | 220 | $data = $this->import_media_from_library( $item ); |
| 114 | - $this->import_settings( $data ); | |
| 221 | + $this->import_theme_settings( $data ); | |
| 115 | 222 | |
| 116 | 223 | return rest_ensure_response( [ |
| 117 | 224 | 'success' => true |
| 118 | 225 | ] ); |
| @@ -117,8 +224,77 @@ | ||
| 117 | 224 | 'success' => true |
| 118 | 225 | ] ); |
| 119 | 226 | } |
| 120 | 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 | + | |
| 121 | 297 | public function import_media_from_library( $item ) { |
| 122 | 298 | $service = new MediaLibraryService(); |
| 123 | 299 | $data = $item->data; |
| 124 | 300 | $media = $item->media; |
| @@ -141,13 +317,13 @@ | ||
| 141 | 317 | */ |
| 142 | 318 | public function can_import_settings( $data ) { |
| 143 | 319 | $theme = $data->theme; |
| 144 | 320 | $slug = $theme->slug; |
| 145 | - $parentSlug = $theme->parent ? $theme->parent->slug : null; | |
| 321 | + $parent_slug = $theme->parent ? $theme->parent->slug : null; | |
| 146 | 322 | $stylesheet = get_stylesheet(); |
| 147 | 323 | $template = get_template(); |
| 148 | 324 | |
| 149 | - if ( $slug !== $stylesheet && $slug !== $template && $parentSlug !== $stylesheet ) { | |
| 325 | + if ( ( $theme->parent && $parent_slug !== $template ) || ( $slug !== $stylesheet && $parent_slug !== $stylesheet ) ) { | |
| 150 | 326 | return new \WP_Error( 'import', __( 'Import failed! These settings are not for the current theme.' ) ); |
| 151 | 327 | } |
| 152 | 328 | |
| 153 | 329 | return true; |
| @@ -156,11 +332,12 @@ | ||
| 156 | 332 | /** |
| 157 | 333 | * @param array $data |
| 158 | 334 | * @return bool|WP_Error |
| 159 | 335 | */ |
| 160 | - public function import_settings( $data ) { | |
| 336 | + public function import_theme_settings( $data ) { | |
| 161 | 337 | global $wp_customize; |
| 162 | 338 | |
| 339 | + $template = get_template(); | |
| 163 | 340 | $can_import = $this->can_import_settings( $data ); |
| 164 | 341 | |
| 165 | 342 | if ( is_wp_error( $can_import ) ) { |
| 166 | 343 | return $can_import; |
| @@ -181,10 +358,20 @@ | ||
| 181 | 358 | |
| 182 | 359 | do_action( 'customize_save', $wp_customize ); |
| 183 | 360 | |
| 184 | 361 | foreach ( $data->mods as $key => $val ) { |
| 362 | + | |
| 363 | + if( is_object( $val ) ) { | |
| 364 | + $val = json_decode( json_encode( $val ), true ); | |
| 365 | + } | |
| 185 | 366 | do_action( 'customize_save_' . $key, $wp_customize ); |
| 186 | 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 ); | |
| 187 | 374 | } |
| 188 | 375 | |
| 189 | 376 | do_action( 'customize_save_after', $wp_customize ); |
| 190 | 377 | |