PluginProbe
Assistant – Every Day Productivity Apps / 1.4.8
Assistant – Every Day Productivity Apps v1.4.8
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
assistant / backend / src / Services / CustomizerService.php

CustomizerService.php in Assistant – Every Day Productivity Apps 1.4.8, at backend/src/Services/CustomizerService.php

220 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
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 */
47 public function export_settings() {
48 global $wp_customize;
49
50 $theme_service = new ThemeService();
51 $theme = $theme_service->get_current_theme_data();
52 $template = get_template();
53
54 $data = [
55 'theme' => $theme,
56 'mods' => [],
57 'options' => [],
58 'custom' => [],
59 'css' => '',
60 ];
61
62 $ignore = [
63 'blogname',
64 'blogdescription',
65 'show_on_front',
66 'page_on_front',
67 'page_for_posts',
68 'nav_menu_locations',
69 'nav_menus_created_posts',
70 'sidebars_widgets',
71 'site_icon',
72 'custom_css_post_id',
73 ];
74
75 $mods = get_theme_mods();
76 $settings = $wp_customize->settings();
77
78 foreach ( $mods as $key => $value ) {
79 if ( in_array( $key, $ignore ) ) {
80 continue;
81 }
82 $data['mods'][ $key ] = $value;
83 }
84
85 foreach ( $settings as $key => $setting ) {
86 if ( 'option' === $setting->type ) {
87 if ( 0 === strpos( $key, 'widget_' ) || 0 === strpos( $key, 'sidebars_' ) ) {
88 continue;
89 } elseif ( in_array( $key, $ignore ) ) {
90 continue;
91 }
92 $data['options'][ $key ] = $setting->value();
93 }
94 }
95
96 if ( function_exists( 'wp_get_custom_css_post' ) ) {
97 $data['css'] = wp_get_custom_css();
98 }
99
100 if ( isset( $this->helpers[ $template ] ) ) {
101 $helper = new $this->helpers[ $template ];
102 $data['custom'] = json_encode( $helper->export() );
103 }
104
105 return $data;
106 }
107
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 /**
154 * @param array $data
155 * @return bool|WP_Error
156 */
157 public function can_import_settings( $data ) {
158 $theme = $data->theme;
159 $slug = $theme->slug;
160 $parent_slug = $theme->parent ? $theme->parent->slug : null;
161 $stylesheet = get_stylesheet();
162 $template = get_template();
163
164 if ( ( $theme->parent && $parent_slug !== $template ) || ( $slug !== $stylesheet && $parent_slug !== $stylesheet ) ) {
165 return new \WP_Error( 'import', __( 'Import failed! These settings are not for the current theme.' ) );
166 }
167
168 return true;
169 }
170
171 /**
172 * @param array $data
173 * @return bool|WP_Error
174 */
175 public function import_settings( $data ) {
176 global $wp_customize;
177
178 $template = get_template();
179 $can_import = $this->can_import_settings( $data );
180
181 if ( is_wp_error( $can_import ) ) {
182 return $can_import;
183 }
184
185 foreach ( $data->options as $option_key => $option_value ) {
186 $option = new CustomizerOptionHelper( $wp_customize, $option_key, array(
187 'default' => '',
188 'type' => 'option',
189 'capability' => 'edit_theme_options'
190 ) );
191 $option->import( $option_value );
192 }
193
194 if( function_exists( 'wp_update_custom_css_post' ) && $data->css ) {
195 wp_update_custom_css_post( $data->css );
196 }
197
198 do_action( 'customize_save', $wp_customize );
199
200 foreach ( $data->mods as $key => $val ) {
201
202 if( is_object( $val ) ) {
203 $val = json_decode( json_encode( $val ), true );
204 }
205 do_action( 'customize_save_' . $key, $wp_customize );
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 );
213 }
214
215 do_action( 'customize_save_after', $wp_customize );
216
217 return true;
218 }
219 }
220