PluginProbe
Assistant – Every Day Productivity Apps / 0.7.1
Assistant – Every Day Productivity Apps v0.7.1
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 0.7.1, at backend/src/Services/CustomizerService.php

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