| 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 ) { |
| 26 |
$client = new CloudClient; |
| 27 |
$data = $this->export_settings(); |
| 28 |
$theme = wp_get_theme(); |
| 29 |
$screenshot = isset( $_POST['screenshot'] ) ? ScreenShotHelper::get_for_post_request( home_url(), false ) : null; |
| 30 |
|
| 31 |
return $client->libraries->create_item( |
| 32 |
$library_id, |
| 33 |
[ |
| 34 |
'name' => sprintf( _x( '%s Settings', '%s theme name', 'assistant' ), $theme->Name ), |
| 35 |
'type' => 'theme_settings', |
| 36 |
'data' => $data, |
| 37 |
'media' => [ |
| 38 |
'attachments' => MediaPathHelper::get_image_paths_from_data( $data ) |
| 39 |
], |
| 40 |
'screenshot' => $screenshot, |
| 41 |
] |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* @return array |
| 47 |
*/ |
| 48 |
public function export_settings() { |
| 49 |
global $wp_customize; |
| 50 |
|
| 51 |
$theme_service = new ThemeService(); |
| 52 |
$theme = $theme_service->get_current_theme_data(); |
| 53 |
$template = get_template(); |
| 54 |
|
| 55 |
$data = [ |
| 56 |
'theme' => $theme, |
| 57 |
'mods' => [], |
| 58 |
'options' => [], |
| 59 |
'custom' => [], |
| 60 |
'css' => '', |
| 61 |
]; |
| 62 |
|
| 63 |
$ignore = [ |
| 64 |
'blogname', |
| 65 |
'blogdescription', |
| 66 |
'show_on_front', |
| 67 |
'page_on_front', |
| 68 |
'page_for_posts', |
| 69 |
'nav_menu_locations', |
| 70 |
'nav_menus_created_posts', |
| 71 |
'sidebars_widgets', |
| 72 |
'site_icon', |
| 73 |
'custom_css_post_id', |
| 74 |
]; |
| 75 |
|
| 76 |
$mods = get_theme_mods(); |
| 77 |
$settings = $wp_customize->settings(); |
| 78 |
|
| 79 |
foreach ( $mods as $key => $value ) { |
| 80 |
if ( in_array( $key, $ignore ) ) { |
| 81 |
continue; |
| 82 |
} |
| 83 |
$data['mods'][ $key ] = $value; |
| 84 |
} |
| 85 |
|
| 86 |
foreach ( $settings as $key => $setting ) { |
| 87 |
if ( 'option' === $setting->type ) { |
| 88 |
if ( 0 === strpos( $key, 'widget_' ) || 0 === strpos( $key, 'sidebars_' ) ) { |
| 89 |
continue; |
| 90 |
} elseif ( in_array( $key, $ignore ) ) { |
| 91 |
continue; |
| 92 |
} |
| 93 |
$data['options'][ $key ] = $setting->value(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if ( function_exists( 'wp_get_custom_css_post' ) ) { |
| 98 |
$data['css'] = wp_get_custom_css(); |
| 99 |
} |
| 100 |
|
| 101 |
if ( isset( $this->helpers[ $template ] ) ) { |
| 102 |
$helper = new $this->helpers[ $template ]; |
| 103 |
$data['custom'] = json_encode( $helper->export() ); |
| 104 |
} |
| 105 |
|
| 106 |
return $data; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* @param int $item_id |
| 111 |
* @return array |
| 112 |
*/ |
| 113 |
public function import_from_cloud( $item_id ) { |
| 114 |
$client = new CloudClient; |
| 115 |
$item = $client->libraries->get_item( $item_id ); |
| 116 |
|
| 117 |
if ( ! is_object( $item ) || empty( $item->data ) ) { |
| 118 |
return rest_ensure_response( [ 'error' => __( 'Missing item data.' ) ] ); |
| 119 |
} |
| 120 |
|
| 121 |
$can_import = $this->can_import_settings( $item->data ); |
| 122 |
|
| 123 |
if ( is_wp_error( $can_import ) ) { |
| 124 |
return rest_ensure_response( [ |
| 125 |
'error' => $can_import->get_error_message() |
| 126 |
] ); |
| 127 |
} |
| 128 |
|
| 129 |
$data = $this->import_media_from_library( $item ); |
| 130 |
$this->import_settings( $data ); |
| 131 |
|
| 132 |
return rest_ensure_response( [ |
| 133 |
'success' => true |
| 134 |
] ); |
| 135 |
} |
| 136 |
|
| 137 |
public function import_media_from_library( $item ) { |
| 138 |
$service = new MediaLibraryService(); |
| 139 |
$data = $item->data; |
| 140 |
$media = $item->media; |
| 141 |
$imported = []; |
| 142 |
|
| 143 |
if ( isset( $media->attachments ) ) { |
| 144 |
foreach ( $media->attachments as $attachment ) { |
| 145 |
$response = $service->import_cloud_media( $attachment ); |
| 146 |
$imported[ $attachment->file_name ] = wp_get_attachment_metadata( $response['id'] ); |
| 147 |
$imported[ $attachment->file_name ]['id'] = $response['id']; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
return MediaPathHelper::replace_imported_attachment_urls_in_data( $data, $imported ); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* @param array $data |
| 156 |
* @return bool|WP_Error |
| 157 |
*/ |
| 158 |
public function can_import_settings( $data ) { |
| 159 |
$theme = $data->theme; |
| 160 |
$slug = $theme->slug; |
| 161 |
$parent_slug = $theme->parent ? $theme->parent->slug : null; |
| 162 |
$stylesheet = get_stylesheet(); |
| 163 |
$template = get_template(); |
| 164 |
|
| 165 |
if ( ( $theme->parent && $parent_slug !== $template ) || ( $slug !== $stylesheet && $parent_slug !== $stylesheet ) ) { |
| 166 |
return new \WP_Error( 'import', __( 'Import failed! These settings are not for the current theme.' ) ); |
| 167 |
} |
| 168 |
|
| 169 |
return true; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* @param array $data |
| 174 |
* @return bool|WP_Error |
| 175 |
*/ |
| 176 |
public function import_settings( $data ) { |
| 177 |
global $wp_customize; |
| 178 |
|
| 179 |
$template = get_template(); |
| 180 |
$can_import = $this->can_import_settings( $data ); |
| 181 |
|
| 182 |
if ( is_wp_error( $can_import ) ) { |
| 183 |
return $can_import; |
| 184 |
} |
| 185 |
|
| 186 |
foreach ( $data->options as $option_key => $option_value ) { |
| 187 |
$option = new CustomizerOptionHelper( $wp_customize, $option_key, array( |
| 188 |
'default' => '', |
| 189 |
'type' => 'option', |
| 190 |
'capability' => 'edit_theme_options' |
| 191 |
) ); |
| 192 |
$option->import( $option_value ); |
| 193 |
} |
| 194 |
|
| 195 |
if( function_exists( 'wp_update_custom_css_post' ) && $data->css ) { |
| 196 |
wp_update_custom_css_post( $data->css ); |
| 197 |
} |
| 198 |
|
| 199 |
do_action( 'customize_save', $wp_customize ); |
| 200 |
|
| 201 |
foreach ( $data->mods as $key => $val ) { |
| 202 |
|
| 203 |
if( is_object( $val ) ) { |
| 204 |
$val = json_decode( json_encode( $val ), true ); |
| 205 |
} |
| 206 |
do_action( 'customize_save_' . $key, $wp_customize ); |
| 207 |
set_theme_mod( $key, $val ); |
| 208 |
} |
| 209 |
|
| 210 |
if ( isset( $this->helpers[ $template ] ) ) { |
| 211 |
$helper = new $this->helpers[ $template ]; |
| 212 |
$settings = json_decode( $data->custom, 1 ); |
| 213 |
$helper->import( $settings ); |
| 214 |
} |
| 215 |
|
| 216 |
do_action( 'customize_save_after', $wp_customize ); |
| 217 |
|
| 218 |
return true; |
| 219 |
} |
| 220 |
} |
| 221 |
|