PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.2.0
Kubio AI Page Builder v2.2.0
2.8.3 2.8.2 2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / lib / api / colibri.php
kubio / lib / api Last commit date
prompts 2 years ago colibri.php 2 years ago contact-form.php 2 years ago entity.php 4 years ago get-body-class.php 4 years ago get-classic-page-template.php 4 years ago get-page-query.php 4 years ago get-page-title.php 4 years ago get-post-content.php 4 years ago get-post-styles.php 4 years ago index.php 2 years ago kubio-cloud.php 2 years ago page-templates.php 4 years ago save-template-parts-filter.php 2 years ago subscribe-form.php 4 years ago typekit.php 4 years ago update-settings-flags.php 2 years ago
colibri.php
403 lines
1 <?php
2
3 use IlluminateAgnostic\Arr\Support\Arr;
4 use Kubio\Flags;
5 use Kubio\Core\Utils;
6
7 add_action(
8 'rest_api_init',
9 function () {
10 $namespace = 'kubio/v1';
11
12 register_rest_route(
13 $namespace,
14 '/enable-theme',
15 array(
16 'methods' => 'GET',
17 'callback' => 'kubio_enable_theme',
18 'permission_callback' => function () {
19 return current_user_can( 'edit_theme_options' );
20 },
21 )
22 );
23
24 register_rest_route(
25 $namespace,
26 '/colibri-data-export',
27 array(
28 'methods' => 'GET',
29 'callback' => 'kubio_colibri_data_export',
30 'permission_callback' => function () {
31 return current_user_can( 'edit_theme_options' );
32 },
33 )
34 );
35
36 register_rest_route(
37 $namespace,
38 '/colibri-data-export-done',
39 array(
40 'methods' => 'GET',
41 'callback' => 'kubio_colibri_data_export_done',
42 'permission_callback' => function () {
43 return current_user_can( 'edit_theme_options' );
44 },
45 )
46 );
47
48 register_rest_route(
49 $namespace,
50 '/snippet',
51 array(
52 'methods' => 'POST',
53 'callback' => 'kubio_save_block_snippet',
54 'permission_callback' => function () {
55 return current_user_can( 'edit_theme_options' );
56 },
57 )
58 );
59
60 register_rest_route(
61 $namespace,
62 '/snippet/update',
63 array(
64 'methods' => 'POST',
65 'callback' => 'kubio_update_block_snippet',
66 'permission_callback' => function () {
67 return current_user_can( 'edit_theme_options' );
68 },
69 )
70 );
71 }
72 );
73
74
75 /**
76 * API callback for saving a snippet; It creates a zip archive with the html template, the screenshot and the assets.
77 * Also sends the zip archive to Kubio cloud.
78 *
79 * @param WP_REST_Request $request
80 * @return void
81 */
82 function kubio_save_block_snippet( WP_REST_Request $request ) {
83 if ( ! class_exists( 'ZipArchive' ) ) {
84 wp_send_json_error( __( 'Zip Export not supported.' ) );
85 }
86
87 $name = $request->get_param( 'name' );
88 $category = $request->get_param( 'category' );
89 $snippet = $request->get_param( 'snippet' );
90 $screenshot = $request->get_param( 'screenshot' );
91 $global_data = $request->get_param( 'globalData' );
92 $zip_path = get_temp_dir() . $name . '.zip';
93
94 $screenshot_ext = explode( ';base64,', $screenshot );
95 $screenshot_ext = explode( 'data:image/', $screenshot_ext[0] );
96
97 if ( empty( $screenshot_ext[1] ) ) {
98 wp_send_json_error( __( 'Bad screenshot.' ) );
99 }
100
101 // allow only letters, numbers, underscore, dashe and space characters.
102 if ( preg_match( '/[^A-Za-z0-9_ \-]/', $name ) || strlen( $name ) > 100 ) {
103 wp_send_json_error( __( 'Bad name.' ) );
104 }
105
106 $zip = kubio_create_snippet_zip( $zip_path, $snippet, $screenshot, $global_data );
107
108 $apiKey = Flags::get( 'kubio_cloud_api_key' );
109
110 $request_url = add_query_arg(
111 array(
112 'filename' => $name,
113 'category_id' => $category,
114 'block' => $snippet[0],
115 ),
116 Utils::getSnippetsURL( '/save' )
117 );
118
119 $response = wp_remote_post(
120 $request_url,
121 array(
122 'headers' => array(
123 'X-Authorization' => $apiKey,
124 'accept' => 'application/json',
125 'content-type' => 'application/binary',
126 ),
127 'body' => file_get_contents( $zip_path ),
128 )
129 );
130
131 $response_code = wp_remote_retrieve_response_code( $response );
132
133 if ( 200 !== $response_code ) {
134 $errorMessage = wp_remote_retrieve_body( $response );
135 wp_send_json_error( $errorMessage );
136 }
137
138 $body = wp_remote_retrieve_body( $response );
139
140 wp_send_json_success( json_decode( $body ) );
141 }
142
143 function kubio_update_block_snippet( WP_REST_Request $request ) {
144 if ( ! class_exists( 'ZipArchive' ) ) {
145 wp_send_json_error( __( 'Zip Export not supported.' ) );
146 }
147
148 $id = $request->get_param( 'id' );
149 $name = $request->get_param( 'name' );
150 $category = $request->get_param( 'category' );
151 $snippet = $request->get_param( 'snippet' );
152 $screenshot = $request->get_param( 'screenshot' );
153 $global_data = $request->get_param( 'globalData' );
154
155 $zip_path = get_temp_dir() . $name . '.zip';
156
157 if ( empty( $id ) ) {
158 wp_send_json_error( __( 'No id.' ) );
159 }
160
161 // allow only letters, numbers, underscore, dashe and space characters.
162 if ( preg_match( '/[^A-Za-z0-9_ \-]/', $name ) || strlen( $name ) > 100 ) {
163 wp_send_json_error( __( 'Bad name.' ) );
164 }
165
166 $zip = kubio_create_snippet_zip( $zip_path, $snippet, $screenshot, $global_data );
167
168 $apiKey = Flags::get( 'kubio_cloud_api_key' );
169
170 $args = array(
171 'filename' => $name,
172 'category_id' => $category,
173 'block' => $snippet[0],
174
175 );
176
177 $request_url = add_query_arg(
178 $args,
179 Utils::getSnippetsURL( '/' . $id . '/update' )
180 );
181
182 $response = wp_remote_post(
183 $request_url,
184 array(
185 'headers' => array(
186 'X-Authorization' => $apiKey,
187 'accept' => 'application/json',
188 'content-type' => 'application/binary',
189 ),
190 'body' => file_get_contents( $zip_path ),
191 )
192 );
193
194 $response_code = wp_remote_retrieve_response_code( $response );
195
196 if ( 200 !== $response_code ) {
197 $errorMessage = wp_remote_retrieve_body();
198 wp_send_json_error( $errorMessage );
199 }
200
201 $body = wp_remote_retrieve_body( $response );
202
203 wp_send_json_success( json_decode( $body ) );
204 }
205
206 /**
207 * This function creates a temporary zip file for the given snippet.
208 *
209 * @param $path
210 * @param $snippet
211 * @param $screenshot_data
212 * @return false|ZipArchive
213 */
214 function kubio_create_snippet_zip( $path, $snippet, $screenshot_data, $global_data ) {
215 // create the zip archive and start adding.
216 $zip = new ZipArchive();
217 if ( true !== $zip->open( $path, ZipArchive::CREATE | ZipArchive::OVERWRITE ) ) {
218 wp_send_json_error( __( 'Unable to open export file (archive) for writing.' ) );
219 }
220
221 // handle the screenshot data and add it to the zip
222 if ( preg_match( '/^data:image\/(\w+);base64,/', $screenshot_data, $screenshot_type ) ) {
223 $screenshot_data = substr( $screenshot_data, strpos( $screenshot_data, ',' ) + 1 );
224 $screenshot_type = strtolower( $screenshot_type[1] ); // jpg, png, gif
225 if ( ! in_array( $screenshot_type, array( 'jpg', 'jpeg', 'gif', 'png' ) ) ) {
226 return false;
227 }
228
229 $screenshot_data = str_replace( ' ', '+', $screenshot_data );
230 $screenshot_data = base64_decode( $screenshot_data );
231
232 $zip->addFromString( 'screenshot.' . $screenshot_type, $screenshot_data );
233 }
234
235 $zip->addFromString( 'global.json', json_encode( $global_data, JSON_PRETTY_PRINT ) );
236
237 // from now on we search assets from the template, and we save them in the archive.
238 //$zip->addEmptyDir( 'assets' );
239
240 // search for media files by urls.
241 $upload_settings = wp_get_upload_dir();
242 $pattern = '#' . str_replace( '/', '\/', $upload_settings['baseurl'] ) . '(.*?)"#';
243 $pattern = str_replace( ':', '\:', $pattern );
244 preg_match_all( $pattern, json_encode( $snippet, JSON_UNESCAPED_SLASHES ), $matches );
245
246 $uploaded_files = array();
247
248 if ( ! empty( $matches ) ) {
249 $uploaded_files = $matches[1];
250 }
251
252 // search media files by id.
253 $uploaded_files = kubio_parse_blocks_for_media_id_atts_and_stack( $snippet, $uploaded_files );
254
255 foreach ( $uploaded_files as $uploaded_file ) {
256 $file_path = $upload_settings['basedir'] . $uploaded_file;
257
258 if ( file_exists( $file_path ) ) {
259 $file = file_get_contents( $file_path );
260 $new_path = wp_normalize_path( 'assets' . $uploaded_file );
261 $zip->addFromString( $new_path, $file );
262 }
263 }
264
265 // at last, save the snippet template and close the zip archive.
266 $parsed = str_replace( $upload_settings['baseurl'], '{{snippet_url}}', json_encode( $snippet, JSON_UNESCAPED_SLASHES ) );
267 $zip->addFromString( 'template.json', $parsed );
268
269 $zip->close();
270 return $zip;
271 }
272
273 /**
274 * This function searches for attributes that hold media files and adds them to the $uploaded_files stack.
275 *
276 * @param $block
277 * @param $uploaded_files
278 * @return mixed
279 */
280 function kubio_parse_blocks_for_media_id_atts_and_stack( $block, $uploaded_files = false ) {
281 // $block[0] is the block name.
282 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block[0] );
283
284 if ( ! empty( $block_type->supports['kubio'] ) ) {
285 $assets_importer_map = Arr::get( $block_type->supports['kubio'], 'assetsURLImporterMap', array() );
286
287 if ( ! empty( $assets_importer_map ) ) {
288 $id_attr = '';
289 foreach ( $assets_importer_map as $url_attr => $import_map ) {
290 $id_attr = Arr::get( $import_map, 'assetIdToAttr' );
291 }
292
293 // $block[1] holds the attributes
294 $id = $block[1][ $id_attr ];
295
296 if ( ! empty( $id ) ) {
297 $url = wp_get_attachment_url( $id );
298 $parsed = parse_url( $url );
299 $new_path = str_replace( '/wp-content/uploads', '', $parsed['path'] );
300 if ( ! in_array( $new_path, $uploaded_files ) ) {
301 $uploaded_files[] = $new_path;
302 }
303 }
304 }
305 }
306
307 // at $block['2'] there should be an array of blocks as innerBlocks.
308 if ( ! empty( $block['2'] ) ) {
309 $uploaded_files = kubio_parse_blocks_for_media_id_atts_and_stack( $block['2'][0], $uploaded_files );
310 }
311
312 return $uploaded_files;
313 }
314
315 function kubio_get_installed_theme() {
316 $themes = wp_get_themes();
317 $kubio_themes = array( 'kubio', 'elevate-wp' );
318 $intersect = array_values( array_intersect( array_keys( $themes ), $kubio_themes ) );
319 $theme = Arr::get( $intersect, 0, null );
320 return $theme;
321 }
322
323 function kubio_enable_theme( WP_REST_Request $data ) {
324 switch_theme( $data['name'] );
325 return wp_send_json( array( 'switched' => $data['name'] ) );
326 }
327
328 function kubio_colibri_data_export_done() {
329
330 // copy menus location from colibri to kubio;
331 $colibri_options = get_option( 'theme_mods_colibri-wp', array() );
332 $kubio_options = get_option( 'theme_mods_kubio', array() );
333
334 $colibri_locations = Arr::get( $colibri_options, 'nav_menu_locations', array() );
335 $kubio_locations = Arr::get( $kubio_options, 'nav_menu_locations', array() );
336
337 $locations_map = array(
338 'header-menu' => 'header-menu',
339 'footer-menu' => 'footer-menu',
340 'footer-menu-1' => 'footer-menu-secondary',
341 'header-menu-1' => 'header-menu-secondary',
342 );
343
344 foreach ( $colibri_locations as $location => $menu ) {
345 $location = Arr::get( $locations_map, $location, $location );
346 $kubio_locations[ $location ] = $menu;
347 }
348
349 Arr::set( $kubio_options, 'nav_menu_locations', $kubio_locations );
350
351 if ( $theme = kubio_get_installed_theme() ) {
352 update_option( "theme_mods_{$theme}", $kubio_options );
353 switch_theme( $theme );
354
355 flush_rewrite_rules();
356 update_option( 'theme_switched', false );
357 }
358
359 wp_send_json_success();
360 }
361
362 function kubio_colibri_data_export() {
363
364 $theme = kubio_get_installed_theme();
365
366 if ( $theme ) {
367 ob_clean();
368
369 // templates are created with colibri-wp theme on kubio activation //
370 foreach ( get_block_templates( array(), 'wp_template' ) as $template ) {
371 wp_set_post_terms( $template->wp_id, $theme, 'wp_theme' );
372 }
373
374 echo wp_json_encode(
375 \ExtendBuilder\export_colibri_data(
376 array( 'exclude_generated' => true ),
377 false
378 )
379 );
380 switch_theme( $theme );
381 } else {
382 wp_send_json_error(
383 array(
384 'error' => 'kubio-not-installed',
385 )
386 );
387 }
388 }
389
390 add_filter(
391 'colibri_page_builder/get_partial_details',
392 function ( $data ) {
393 $post = get_post( $data['id'] );
394 $new_data = array(
395 'title' => $post->post_title,
396 'page_template' => $post->page_template,
397 'parent' => $post->post_parent,
398 );
399 return array_merge( $data, $new_data );
400 },
401 100
402 );
403