PluginProbe
Assistant – Every Day Productivity Apps / 1.4.7
Assistant – Every Day Productivity Apps v1.4.7
1.5.5 trunk 0.1.0 0.2.0 0.2.1 0.2.2 0.3.0 0.3.1 0.4 0.4.1 0.4.2 0.5.0 0.5.1 0.6.0 0.7.0 0.7.0.2 0.7.0.3 0.7.0.4 0.7.0.5 0.7.0.6 0.7.0.7 0.7.0.8 0.7.0.9 0.7.1 1.0 All 71 releases
← All changes | backend/src/Services/CustomizerService.php +116 -41 0.7.0.21.4.7 View file →
@@ -3,27 +3,64 @@
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 */
25 + public function export_to_cloud( $library_id ) {
26 + $client = new CloudClient;
27 + $data = $this->export_settings();
28 + $theme = wp_get_theme();
29 +
30 + return $client->libraries->create_item(
31 + $library_id,
32 + [
33 + 'name' => sprintf( _x( '%s Settings', '%s theme name', 'assistant' ), $theme->Name ),
34 + 'type' => 'theme_settings',
35 + 'data' => $data,
36 + 'media' => [
37 + 'attachments' => MediaPathHelper::get_image_paths_from_data( $data )
38 + ],
39 + 'screenshot' => ScreenshotHelper::get_for_post_request( home_url(), false ),
40 + ]
41 + );
42 + }
43 +
44 + /**
45 + * @return array
46 + */
13 47 public function export_settings() {
14 - $wp_customize = $this->init_customizer();
48 + global $wp_customize;
49 +
15 50 $theme_service = new ThemeService();
16 51 $theme = $theme_service->get_current_theme_data();
52 + $template = get_template();
17 53
18 - $data = array(
19 - 'mods' => array(),
20 - 'options' => array(),
54 + $data = [
55 + 'theme' => $theme,
56 + 'mods' => [],
57 + 'options' => [],
58 + 'custom' => [],
21 59 'css' => '',
22 - 'theme' => $theme,
23 - );
60 + ];
24 61
25 - $ignore = array(
62 + $ignore = [
26 63 'blogname',
27 64 'blogdescription',
28 65 'show_on_front',
29 66 'page_on_front',
@@ -32,9 +69,9 @@
32 69 'nav_menus_created_posts',
33 70 'sidebars_widgets',
34 71 'site_icon',
35 72 'custom_css_post_id',
36 - );
73 + ];
37 74
38 75 $mods = get_theme_mods();
39 76 $settings = $wp_customize->settings();
40 77
@@ -59,23 +96,73 @@
59 96 if ( function_exists( 'wp_get_custom_css_post' ) ) {
60 97 $data['css'] = wp_get_custom_css();
61 98 }
62 99
100 + if ( isset( $this->helpers[ $template ] ) ) {
101 + $helper = new $this->helpers[ $template ];
102 + $data['custom'] = json_encode( $helper->export() );
103 + }
104 +
63 105 return $data;
64 106 }
65 107
66 108 /**
109 + * @param int $item_id
110 + * @return array
111 + */
112 + public function import_from_cloud( $item_id ) {
113 + $client = new CloudClient;
114 + $item = $client->libraries->get_item( $item_id );
115 +
116 + if ( ! is_object( $item ) || empty( $item->data ) ) {
117 + return rest_ensure_response( [ 'error' => __( 'Missing item data.' ) ] );
118 + }
119 +
120 + $can_import = $this->can_import_settings( $item->data );
121 +
122 + if ( is_wp_error( $can_import ) ) {
123 + return rest_ensure_response( [
124 + 'error' => $can_import->get_error_message()
125 + ] );
126 + }
127 +
128 + $data = $this->import_media_from_library( $item );
129 + $this->import_settings( $data );
130 +
131 + return rest_ensure_response( [
132 + 'success' => true
133 + ] );
134 + }
135 +
136 + public function import_media_from_library( $item ) {
137 + $service = new MediaLibraryService();
138 + $data = $item->data;
139 + $media = $item->media;
140 + $imported = [];
141 +
142 + if ( isset( $media->attachments ) ) {
143 + foreach ( $media->attachments as $attachment ) {
144 + $response = $service->import_cloud_media( $attachment );
145 + $imported[ $attachment->file_name ] = wp_get_attachment_metadata( $response['id'] );
146 + $imported[ $attachment->file_name ]['id'] = $response['id'];
147 + }
148 + }
149 +
150 + return MediaPathHelper::replace_imported_attachment_urls_in_data( $data, $imported );
151 + }
152 +
153 + /**
67 154 * @param array $data
68 155 * @return bool|WP_Error
69 156 */
70 157 public function can_import_settings( $data ) {
71 - $theme = $data['theme'];
72 - $slug = $theme['slug'];
73 - $parentSlug = $theme['parent'] ? $theme['parent']['slug'] : null;
158 + $theme = $data->theme;
159 + $slug = $theme->slug;
160 + $parent_slug = $theme->parent ? $theme->parent->slug : null;
74 161 $stylesheet = get_stylesheet();
75 162 $template = get_template();
76 163
77 - if ( $slug !== $stylesheet && $slug !== $template && $parentSlug !== $stylesheet ) {
164 + if ( ( $theme->parent && $parent_slug !== $template ) || ( $slug !== $stylesheet && $parent_slug !== $stylesheet ) ) {
78 165 return new \WP_Error( 'import', __( 'Import failed! These settings are not for the current theme.' ) );
79 166 }
80 167
81 168 return true;
@@ -85,9 +172,11 @@
85 172 * @param array $data
86 173 * @return bool|WP_Error
87 174 */
88 175 public function import_settings( $data ) {
89 - $wp_customize = $this->init_customizer();
176 + global $wp_customize;
177 +
178 + $template = get_template();
90 179 $can_import = $this->can_import_settings( $data );
91 180
92 181 if ( is_wp_error( $can_import ) ) {
93 182 return $can_import;
@@ -92,9 +181,9 @@
92 181 if ( is_wp_error( $can_import ) ) {
93 182 return $can_import;
94 183 }
95 184
96 - foreach ( $data['options'] as $option_key => $option_value ) {
185 + foreach ( $data->options as $option_key => $option_value ) {
97 186 $option = new CustomizerOptionHelper( $wp_customize, $option_key, array(
98 187 'default' => '',
99 188 'type' => 'option',
100 189 'capability' => 'edit_theme_options'
@@ -101,44 +190,30 @@
101 190 ) );
102 191 $option->import( $option_value );
103 192 }
104 193
105 - if( function_exists( 'wp_update_custom_css_post' ) && $data['css'] ) {
106 - wp_update_custom_css_post( $data['css'] );
194 + if( function_exists( 'wp_update_custom_css_post' ) && $data->css ) {
195 + wp_update_custom_css_post( $data->css );
107 196 }
108 197
109 198 do_action( 'customize_save', $wp_customize );
110 199
111 - foreach ( $data['mods'] as $key => $val ) {
200 + foreach ( $data->mods as $key => $val ) {
201 +
202 + if( is_object( $val ) ) {
203 + $val = json_decode( json_encode( $val ), true );
204 + }
112 205 do_action( 'customize_save_' . $key, $wp_customize );
113 206 set_theme_mod( $key, $val );
114 207 }
115 208
209 + if ( isset( $this->helpers[ $template ] ) ) {
210 + $helper = new $this->helpers[ $template ];
211 + $settings = json_decode( $data->custom, 1 );
212 + $helper->import( $settings );
213 + }
214 +
116 215 do_action( 'customize_save_after', $wp_customize );
117 216
118 217 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 218 }
144 219 }