Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 122 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
ThemeVariationsController | |
0.00% |
0 / 122 |
|
0.00% |
0 / 9 |
306 | |
0.00% |
0 / 1 |
register_routes | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
2 | |||
get_pattern_args | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
set_pattern_args | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
get_update_diy_global_style_variation_args | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
2 | |||
translate | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
get_style_variations | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
30 | |||
get_theme_variations | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
12 | |||
set_theme_variation | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
update_diy_global_style_variation | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | namespace NewfoldLabs\WP\Module\Onboarding\RestApi\Themes; |
3 | |
4 | use NewfoldLabs\WP\Module\Onboarding\Permissions; |
5 | use NewfoldLabs\WP\Module\Onboarding\Data\Themes; |
6 | use NewfoldLabs\WP\Module\Onboarding\Data\Services\GlobalStylesService; |
7 | |
8 | /** |
9 | * Class ThemeVariationsController |
10 | */ |
11 | class ThemeVariationsController extends \WP_REST_Controller { |
12 | |
13 | /** |
14 | * The namespace of this controller's route. |
15 | * |
16 | * @var string |
17 | */ |
18 | protected $namespace = 'newfold-onboarding/v1'; |
19 | |
20 | /** |
21 | * The base of this controller's route. |
22 | * |
23 | * @var string |
24 | */ |
25 | protected $rest_base = '/themes'; |
26 | |
27 | |
28 | /** |
29 | * The extended base of this controller's route. |
30 | * |
31 | * @var string |
32 | */ |
33 | protected $rest_extended_base = '/variations'; |
34 | |
35 | /** |
36 | * Registers routes for ThemeVariationsController |
37 | */ |
38 | public function register_routes() { |
39 | register_rest_route( |
40 | $this->namespace, |
41 | $this->rest_base . $this->rest_extended_base, |
42 | array( |
43 | array( |
44 | 'methods' => \WP_REST_Server::READABLE, |
45 | 'args' => $this->get_pattern_args(), |
46 | 'callback' => array( $this, 'get_theme_variations' ), |
47 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
48 | ), |
49 | array( |
50 | 'methods' => \WP_REST_Server::EDITABLE, |
51 | 'args' => $this->set_pattern_args(), |
52 | 'callback' => array( $this, 'set_theme_variation' ), |
53 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
54 | ), |
55 | ) |
56 | ); |
57 | |
58 | register_rest_route( |
59 | $this->namespace, |
60 | $this->rest_base . $this->rest_extended_base . '/update', |
61 | array( |
62 | array( |
63 | 'methods' => \WP_REST_Server::EDITABLE, |
64 | 'args' => $this->get_update_diy_global_style_variation_args(), |
65 | 'callback' => array( $this, 'update_diy_global_style_variation' ), |
66 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
67 | ), |
68 | ) |
69 | ); |
70 | } |
71 | |
72 | /** |
73 | * Get the patterns arguments. |
74 | * Retrieves the original numerous variations if true, else sends the recently saved theme settings in the DB. |
75 | * |
76 | * @return array |
77 | */ |
78 | public function get_pattern_args() { |
79 | return array( |
80 | 'variations' => array( |
81 | 'type' => 'boolean', |
82 | 'default' => false, |
83 | ), |
84 | ); |
85 | } |
86 | |
87 | /** |
88 | * Set the pattern arguments to the latest modified Global Style that is to be saved in the DB. |
89 | * |
90 | * @return array |
91 | */ |
92 | public function set_pattern_args() { |
93 | return array( |
94 | 'title' => array( |
95 | 'type' => 'string', |
96 | 'required' => true, |
97 | ), |
98 | 'settings' => array( |
99 | 'type' => 'object', |
100 | 'required' => true, |
101 | ), |
102 | ); |
103 | } |
104 | |
105 | /** |
106 | * Gets the arguments for updating DIY global style variations. |
107 | * |
108 | * @return array An array of argument definitions for the REST API request. |
109 | */ |
110 | public function get_update_diy_global_style_variation_args() { |
111 | return array( |
112 | 'id' => array( |
113 | 'description' => __( 'The id of a template', 'wp-module-onboarding' ), |
114 | 'type' => 'integer', |
115 | 'default' => GlobalStylesService::get_active_custom_global_styles_post_id(), |
116 | ), |
117 | 'styles' => array( |
118 | 'description' => __( 'The custom styles', 'wp-module-onboarding' ), |
119 | 'default' => array(), |
120 | 'required' => false, |
121 | ), |
122 | 'settings' => array( |
123 | 'description' => __( 'The custom settings', 'wp-module-onboarding' ), |
124 | 'default' => array(), |
125 | 'required' => false, |
126 | ), |
127 | ); |
128 | } |
129 | |
130 | /** |
131 | * Translate decoded json file. |
132 | * |
133 | * @param string $theme_json Theme content. |
134 | * @param string $domain Domain type. |
135 | * @return string |
136 | */ |
137 | private static function translate( $theme_json, $domain = 'default' ) { |
138 | $i18n_schema = wp_json_file_decode( __DIR__ . '/theme-i18n.json' ); |
139 | |
140 | return translate_settings_using_i18n_schema( $i18n_schema, $theme_json, $domain ); |
141 | } |
142 | |
143 | /** |
144 | * Translate the json decoded HTML Files and retrieve all the Theme Style Variations. |
145 | * |
146 | * @return array |
147 | */ |
148 | private static function get_style_variations() { |
149 | $variations = array(); |
150 | $base_directory = get_stylesheet_directory() . '/styles'; |
151 | if ( is_dir( $base_directory ) ) { |
152 | $nested_files = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator( $base_directory ) ); |
153 | $nested_html_files = iterator_to_array( new \RegexIterator( $nested_files, '/^.+\.json$/i', \RecursiveRegexIterator::GET_MATCH ) ); |
154 | ksort( $nested_html_files ); |
155 | foreach ( $nested_html_files as $path => $file ) { |
156 | $decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) ); |
157 | if ( is_array( $decoded_file ) ) { |
158 | $translated = self::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) ); |
159 | $variation = ( new \WP_Theme_JSON( $translated ) )->get_data(); |
160 | if ( empty( $variation['title'] ) ) { |
161 | $variation['title'] = basename( $path, '.json' ); |
162 | } |
163 | $variations[] = $variation; |
164 | } |
165 | } |
166 | } |
167 | return $variations; |
168 | } |
169 | |
170 | /** |
171 | * Retrieves the active themes variations. |
172 | * |
173 | * @param \WP_REST_Request $request Request model. |
174 | * @return array|\WP_Error |
175 | */ |
176 | public function get_theme_variations( \WP_REST_Request $request ) { |
177 | |
178 | $default = $request->get_param( 'variations' ); |
179 | |
180 | // If there exists an old Custom Theme then return that |
181 | if ( false === $default && false !== Themes::get_selected_diy_theme_settings() ) { |
182 | return array( |
183 | Themes::get_selected_diy_theme_settings(), |
184 | ); |
185 | } |
186 | |
187 | $active_variation = \WP_Theme_JSON_Resolver::get_theme_data()->get_data(); |
188 | $active_variation_global_style = array( |
189 | 'id' => 0, |
190 | 'title' => 'Default', |
191 | 'version' => $active_variation['version'], |
192 | 'settings' => $active_variation['settings'], |
193 | 'styles' => $active_variation['styles'], |
194 | ); |
195 | |
196 | return array_merge( |
197 | array( $active_variation_global_style ), |
198 | self::get_style_variations() |
199 | ); |
200 | } |
201 | |
202 | /** |
203 | * Saves the custom active theme variations. |
204 | * |
205 | * @param \WP_REST_Request $request Request model. |
206 | * @return \WP_REST_Response|\WP_Error |
207 | */ |
208 | public function set_theme_variation( \WP_REST_Request $request ) { |
209 | // The theme data with the new Colors and Fonts |
210 | $theme_data = json_decode( $request->get_body(), true ); |
211 | |
212 | if ( $theme_data ) { |
213 | |
214 | // Save the new Theme style into the db |
215 | Themes::set_diy_theme_settings( $theme_data ); |
216 | |
217 | return new \WP_REST_Response( |
218 | $theme_data, |
219 | 200 |
220 | ); |
221 | } |
222 | |
223 | return new \WP_Error( |
224 | 500, |
225 | 'Missing important parameters', |
226 | 'Settings parameter is found to be missing' |
227 | ); |
228 | } |
229 | |
230 | /** |
231 | * Handles the update DIY global style variation request. |
232 | * |
233 | * @param \WP_REST_Request $request The REST API request object containing the parameters. |
234 | * |
235 | * @return \WP_REST_Response|\WP_Error Returns a WP_REST_Response on success, or a WP_Error on failure. |
236 | */ |
237 | public function update_diy_global_style_variation( \WP_REST_Request $request ) { |
238 | $request_data = $request->get_params(); |
239 | $id = $request_data['id']; |
240 | $styles = $request_data['styles']; |
241 | $settings = $request_data['settings']; |
242 | |
243 | $status = GlobalStylesService::update_diy_global_style_variation( $id, $styles, $settings ); |
244 | |
245 | if ( is_wp_error( $status ) ) { |
246 | return $status; |
247 | } |
248 | |
249 | return new \WP_REST_Response( |
250 | array( |
251 | 'message' => __( 'Global style customization created.', 'wp-module-onboarding' ), |
252 | ), |
253 | 201 |
254 | ); |
255 | } |
256 | } |