PluginProbe
Gutenify – Visual Site Builder Blocks & Starter Templates / 1.1.0
Gutenify – Visual Site Builder Blocks & Starter Templates v1.1.0
1.9.0 1.8.0 1.7.0 1.6.6 1.6.5 1.6.4 trunk 0.0.1 0.0.2 0.0.3 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 All 74 releases
gutenify / inc / class-rest.php

class-rest.php in Gutenify – Visual Site Builder Blocks & Starter Templates 1.1.0, at inc/class-rest.php

732 lines 19.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Rest API functions.
4 *
5 * @package Gutenify
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 require 'class-template-kits.php';
13
14 /**
15 * Class Gutenify_Rest
16 */
17 class Gutenify_Rest extends WP_REST_Controller {
18 /**
19 * Namespace.
20 *
21 * @var string
22 */
23 protected $namespace = 'gutenify/v';
24
25 /**
26 * Version.
27 *
28 * @var string
29 */
30 protected $version = '1';
31
32 /**
33 * Gutenify_Rest constructor.
34 */
35 public function __construct() {
36 add_action( 'rest_api_init', array( $this, 'register_routes' ) );
37 }
38
39 /**
40 * Register rest routes.
41 */
42 public function register_routes() {
43 $namespace = $this->namespace . $this->version;
44
45 // Get Templates.
46 register_rest_route(
47 $namespace,
48 '/get_templates/',
49 array(
50 'methods' => WP_REST_Server::READABLE,
51 'callback' => array( $this, 'get_templates' ),
52 'permission_callback' => '__return_true',
53 )
54 );
55
56 // Get template data.
57 register_rest_route(
58 $namespace,
59 '/get_template_data/',
60 array(
61 'methods' => WP_REST_Server::READABLE,
62 'callback' => array( $this, 'get_template_data' ),
63 'permission_callback' => '__return_true',
64 )
65 );
66
67 // Get settings data.
68 register_rest_route(
69 $namespace,
70 '/get_settings/',
71 array(
72 'methods' => WP_REST_Server::READABLE,
73 'callback' => array( $this, 'get_settings' ),
74 'permission_callback' => '__return_true',
75 )
76 );
77
78 // Update Settings.
79 register_rest_route(
80 $namespace,
81 '/update_settings/',
82 array(
83 'methods' => WP_REST_Server::EDITABLE,
84 'callback' => array( $this, 'update_settings' ),
85 'permission_callback' => array( $this, 'update_settings_permission' ),
86 )
87 );
88
89 // Get Templates.
90 register_rest_route(
91 $namespace,
92 '/get_template_kit/',
93 array(
94 'methods' => WP_REST_Server::READABLE,
95 'callback' => array( $this, 'get_template_kit' ),
96 'permission_callback' => '__return_true',
97 )
98 );
99
100 // Get Templates.
101 register_rest_route(
102 $namespace,
103 '/get_template_kit_data/',
104 array(
105 'methods' => WP_REST_Server::READABLE,
106 'callback' => array( $this, 'get_template_kit_data' ),
107 'permission_callback' => '__return_true',
108 )
109 );
110
111 // Add Kit.
112 register_rest_route(
113 $namespace,
114 '/create_kit/',
115 array(
116 'methods' => WP_REST_Server::EDITABLE,
117 'callback' => array( $this, 'create_kit' ),
118 'permission_callback' => array( $this, 'create_kit_permission' ),
119 )
120 );
121
122 // Get Templates.
123 register_rest_route(
124 $namespace,
125 '/get_site_options/',
126 array(
127 'methods' => WP_REST_Server::READABLE,
128 'callback' => array( $this, 'get_site_options' ),
129 'permission_callback' => '__return_true',
130 )
131 );
132
133 // Update Site Optoins
134 register_rest_route(
135 $namespace,
136 '/update_site_options/',
137 array(
138 'methods' => WP_REST_Server::EDITABLE,
139 'callback' => array( $this, 'update_site_options' ),
140 'permission_callback' => array( $this, 'update_settings_permission' ),
141 )
142 );
143
144 // Update Site Optoins
145 register_rest_route(
146 $namespace,
147 '/update_global_styles/',
148 array(
149 'methods' => WP_REST_Server::EDITABLE,
150 'callback' => array( $this, 'update_global_styles' ),
151 'permission_callback' => array( $this, 'update_settings_permission' ),
152 )
153 );
154 }
155
156 /**
157 * Get templates.
158 *
159 * @return mixed
160 */
161 public function get_templates( WP_REST_Request $request ) {
162 $api_site = defined( 'GUTENIFY_API_URL' ) ? GUTENIFY_API_URL : 'https://api.gutenify.com/';
163 $url = $api_site . 'wp-json/gutenify-library/v1/get_library/';
164 $templates = get_transient( 'gutenify_remote_templates', false );
165
166 $data = $request->get_params();
167
168 if ( ! empty( $data['reset'] ) && 'true' === $data['reset'] ) {
169 $templates = array();
170 }
171
172 /*
173 * Get remote templates.
174 */
175 if ( ! $templates ) {
176 $requested_templates = wp_remote_get(
177 add_query_arg(
178 array(
179 'gutenify_version' => '2.19.3',
180 'gutenify_pro' => false,
181 'gutenify_pro_version' => null,
182 ),
183 $url
184 )
185 );
186
187 if ( ! is_wp_error( $requested_templates ) ) {
188 $new_templates = wp_remote_retrieve_body( $requested_templates );
189 $new_templates = json_decode( $new_templates, true );
190
191 if ( $new_templates && isset( $new_templates['response'] ) && is_array( $new_templates['response'] ) ) {
192 $templates = $new_templates['response'];
193 set_transient( 'gutenify_remote_templates', $templates, DAY_IN_SECONDS );
194 }
195 }
196 }
197
198 // Remove Pro templates from array, cause for now there is no way to check if pro addon is activated.
199 if ( $templates ) {
200 foreach ( $templates as $k => $template ) {
201 $is_pro = false;
202
203 if ( isset( $template['types'] ) && is_array( $template['types'] ) ) {
204 foreach ( $template['types'] as $type ) {
205 $is_pro = $is_pro || 'pro' === $type['slug'];
206 }
207 }
208
209 if ( $is_pro ) {
210 unset( $templates[ $k ] );
211 }
212 }
213 }
214
215 /*
216 * Get user templates from db.
217 */
218
219 // Stupid hack.
220 // https://core.trac.wordpress.org/ticket/18408.
221 global $post;
222 $backup_global_post = $post;
223 $local_templates = array();
224
225 $local_templates_query = new WP_Query(
226 array(
227 'post_type' => 'gutenify_template',
228 'posts_per_page' => -1,
229 'showposts' => -1,
230 'paged' => -1,
231 )
232 );
233
234 while ( $local_templates_query->have_posts() ) {
235 $local_templates_query->the_post();
236 $db_template = get_post();
237
238 $categories = array();
239 $category_terms = get_the_terms( $db_template->ID, 'gutenify_template_category' );
240
241 if ( $category_terms ) {
242 foreach ( $category_terms as $cat ) {
243 $categories[] = array(
244 'slug' => $cat->slug,
245 'name' => $cat->name,
246 );
247 }
248 }
249
250 $image_id = get_post_thumbnail_id( $db_template->ID );
251 $image_data = wp_get_attachment_image_src( $image_id, 'large' );
252
253 $local_templates[] = array(
254 'id' => $db_template->ID,
255 'title' => $db_template->post_title,
256 'types' => array(
257 array(
258 'slug' => 'local',
259 ),
260 ),
261 'categories' => empty( $categories ) ? false : $categories,
262 'url' => get_post_permalink( $db_template->ID ),
263 'thumbnail' => isset( $image_data[0] ) ? $image_data[0] : false,
264 'thumbnail_width' => isset( $image_data[1] ) ? $image_data[1] : false,
265 'thumbnail_height' => isset( $image_data[2] ) ? $image_data[2] : false,
266 );
267 }
268
269 // Restore the global $post as it was before custom WP_Query.
270 // phpcs:ignore
271 $post = $backup_global_post;
272
273 /*
274 * Get theme templates.
275 */
276 $theme_templates = array();
277 $theme_templates_data = array();
278
279 foreach ( glob( get_template_directory() . '/gutenify/templates/*/content.php' ) as $template ) {
280 $template_path = dirname( $template );
281 $template_url = get_template_directory_uri() . str_replace( get_template_directory(), '', $template_path );
282 $slug = basename( $template_path );
283
284 $theme_templates_data[ $slug ] = array(
285 'slug' => $slug,
286 'path' => $template_path,
287 'url' => $template_url,
288 );
289 }
290
291 // get child theme templates.
292 if ( get_stylesheet_directory() !== get_template_directory() ) {
293 foreach ( glob( get_stylesheet_directory() . '/gutenify/templates/*/content.php' ) as $template ) {
294 $template_path = dirname( $template );
295 $template_url = get_stylesheet_directory_uri() . str_replace( get_stylesheet_directory(), '', $template_path );
296 $slug = basename( $template_path );
297
298 $theme_templates_data[ $slug ] = array(
299 'slug' => $slug,
300 'path' => $template_path,
301 'url' => $template_url,
302 );
303 }
304 }
305
306 // natural sort.
307 array_multisort( array_keys( $theme_templates_data ), SORT_NATURAL, $theme_templates_data );
308
309 foreach ( $theme_templates_data as $template_data ) {
310 $file_data = get_file_data(
311 $template_data['path'] . '/content.php',
312 array(
313 'name' => 'Name',
314 'category' => 'Category',
315 'source' => 'Source',
316 )
317 );
318
319 $thumbnail = false;
320 $thumbnail_width = false;
321 $thumbnail_height = false;
322
323 if ( file_exists( $template_data['path'] . '/thumbnail.png' ) ) {
324 $thumbnail = $template_data['url'] . '/thumbnail.png';
325
326 list($thumbnail_width, $thumbnail_height) = getimagesize( $thumbnail );
327 }
328
329 if ( file_exists( $template_data['path'] . '/thumbnail.jpg' ) ) {
330 $thumbnail = $template_data['url'] . '/thumbnail.jpg';
331 }
332
333 $theme_templates[] = array(
334 'id' => basename( $template_data['path'] ),
335 'title' => $file_data['name'],
336 'types' => array(
337 array(
338 'slug' => 'theme',
339 ),
340 ),
341 'categories' => isset( $file_data['category'] ) && $file_data['category'] ? array(
342 array(
343 'slug' => $file_data['category'],
344 'name' => $file_data['category'],
345 ),
346 ) : false,
347 'url' => false,
348 'thumbnail' => $thumbnail,
349 'thumbnail_width' => $thumbnail_width,
350 'thumbnail_height' => $thumbnail_height,
351 );
352 }
353
354 // merge all available templates.
355 $templates = ! empty( $templates ) ? $templates : array();
356 $templates = array_merge( $templates, $local_templates, $theme_templates );
357
358 if ( is_array( $templates ) ) {
359 return $this->success( $templates );
360 } else {
361 return $this->error( 'no_templates', __( 'Templates not found.', '@@text_domain' ) );
362 }
363 }
364
365 /**
366 * Get templates.
367 *
368 * @param WP_REST_Request $request request object.
369 *
370 * @return mixed
371 */
372 public function get_template_data( WP_REST_Request $request ) {
373 $api_site = defined( 'GUTENIFY_API_URL' ) ? GUTENIFY_API_URL : 'https://api.gutenify.com/';
374 $url = $api_site . 'wp-json/gutenify-library/v1/get_library_data/';
375 $id = $request->get_param( 'id' );
376 $type = $request->get_param( 'type' );
377 $template_data = false;
378
379 switch ( $type ) {
380 case 'remote':
381 $template_data = get_transient( 'gutenify_template_' . $type . '_' . $id, false );
382
383 if ( ! $template_data ) {
384 $requested_template_data = wp_remote_get(
385 add_query_arg(
386 apply_filters(
387 'gutenify_rest_template_data_url_args',
388 array(
389 'id' => $id,
390 'gutenify_version' => GUTENIFY_VERSION,
391 'site_url' => site_url( '/' ),
392 )
393 ),
394 $url
395 )
396 );
397
398 if ( ! is_wp_error( $requested_template_data ) ) {
399 $new_template_data = wp_remote_retrieve_body( $requested_template_data );
400 $new_template_data = json_decode( $new_template_data, true );
401
402 if ( $new_template_data && isset( $new_template_data['response'] ) && is_array( $new_template_data['response'] ) ) {
403 $template_data = $new_template_data['response'];
404 set_transient( 'gutenify_template_' . $type . '_' . $id, $template_data, DAY_IN_SECONDS );
405 }
406 }
407 }
408 break;
409 case 'local':
410 $post = get_post( $id );
411
412 if ( $post && 'gutenify_template' === $post->post_type ) {
413 $template_data = array(
414 'id' => $post->ID,
415 'title' => $post->post_title,
416 'content' => $post->post_content,
417 );
418 }
419
420 break;
421 case 'theme':
422 $template_content_file = get_stylesheet_directory() . '/gutenify/templates/' . $id . '/content.php';
423
424 if ( ! file_exists( $template_content_file ) ) {
425 $template_content_file = get_template_directory() . '/gutenify/templates/' . $id . '/content.php';
426 }
427
428 if ( file_exists( $template_content_file ) ) {
429 ob_start();
430 include $template_content_file;
431 $template_content = ob_get_clean();
432
433 if ( $template_content ) {
434 $template_data = get_file_data(
435 $template_content_file,
436 array(
437 'name' => 'Name',
438 )
439 );
440
441 $template_data = array(
442 'id' => $id,
443 'title' => $template_data['name'],
444 'content' => $template_content,
445 );
446 }
447 }
448 break;
449 }
450
451 if ( is_array( $template_data ) ) {
452 return $this->success( $template_data );
453 } else {
454 return $this->error( 'no_template_data', __( 'Template data not found.', '@@text_domain' ) );
455 }
456 }
457
458 /**
459 * Get settings.
460 *
461 * @return mixed
462 */
463 public function get_settings() {
464 $settings = gutenify_settings();
465
466 if ( is_array( $settings ) ) {
467 return $this->success( $settings );
468 } else {
469 return $this->error( 'no_settings', __( 'Settings data not found.', '@@text_domain' ) );
470 }
471 }
472
473 /**
474 * Get edit options permissions.
475 *
476 * @return bool
477 */
478 public function update_settings_permission() {
479 if ( ! current_user_can( 'manage_options' ) ) {
480 return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change options.', '@@text_domain' ) );
481 }
482 return true;
483 }
484
485 /**
486 * Update Settings.
487 *
488 * @param WP_REST_Request $request request object.
489 *
490 * @return mixed
491 */
492 public function update_settings( WP_REST_Request $request ) {
493 $new_settings = $request->get_param( 'settings' );
494
495 if ( is_array( $new_settings ) ) {
496 $current_settings = get_option( 'gutenify_settings', array() );
497 update_option( 'gutenify_settings', array_merge( $current_settings, $new_settings ) );
498 }
499
500 return $this->success( true );
501 }
502
503 /**
504 * Get templates.
505 *
506 * @return mixed
507 */
508 public function get_template_kit( WP_REST_Request $request ) {
509 $api_site = defined( 'GUTENIFY_API_URL' ) ? GUTENIFY_API_URL : 'https://api.gutenify.com/';
510 $url = $api_site . 'wp-json/gutenify-library/v1/get_kit_library/';
511 $templates = get_transient( 'gutenify_remote_template_kits', false );
512 $data = $request->get_params();
513
514 if ( ! empty( $data['reset'] ) && 'true' === $data['reset'] ) {
515 $templates = array();
516 }
517
518 /*
519 * Get remote templates.
520 */
521 if ( ! $templates ) {
522 $requested_templates = wp_remote_get(
523 add_query_arg(
524 array(
525 'gutenify_version' => '2.19.3',
526 'gutenify_pro' => false,
527 'gutenify_pro_version' => null,
528 ),
529 $url
530 )
531 );
532
533 if ( ! is_wp_error( $requested_templates ) ) {
534 $new_templates = wp_remote_retrieve_body( $requested_templates );
535 $new_templates = json_decode( $new_templates, true );
536
537 if ( $new_templates && isset( $new_templates['response'] ) && is_array( $new_templates['response'] ) ) {
538 $templates = $new_templates['response'];
539 set_transient( 'gutenify_remote_template_kits', $templates, DAY_IN_SECONDS );
540 }
541 }
542 }
543
544 // Remove Pro templates from array, cause for now there is no way to check if pro addon is activated.
545 if ( $templates ) {
546 foreach ( $templates as $k => $template ) {
547 $is_pro = false;
548
549 if ( isset( $template['types'] ) && is_array( $template['types'] ) ) {
550 foreach ( $template['types'] as $type ) {
551 $is_pro = $is_pro || 'pro' === $type['slug'];
552 }
553 }
554
555 if ( $is_pro ) {
556 unset( $templates[ $k ] );
557 }
558 }
559 }
560
561 if ( is_array( $templates ) ) {
562 return $this->success( $templates );
563 } else {
564 return $this->error( 'no_template_kits', __( 'Template kit not found.', '@@text_domain' ) );
565 }
566 }
567
568 public function get_template_kit_data( WP_REST_Request $request ) {
569 $api_site = defined( 'GUTENIFY_API_URL' ) ? GUTENIFY_API_URL : 'https://api.gutenify.com/';
570 $url = $api_site . 'wp-json/gutenify-library/v1/get_kit_library_data/';
571 $id = $request->get_param( 'id' );
572 $type = $request->get_param( 'type' );
573 $template_data = false;
574 switch ( $type ) {
575 case 'remote':
576 $template_data = get_transient( 'gutenify_template_kit_' . $type . '_' . $id, false );
577
578 if ( ! $template_data ) {
579 $requested_template_data = wp_remote_get(
580 add_query_arg(
581 apply_filters(
582 'gutenify_rest_template_data_url_args',
583 array(
584 'id' => $id,
585 'gutenify_version' => GUTENIFY_VERSION,
586 'gutenify_site' => site_url( '/' ),
587 )
588 ),
589 $url
590 )
591 );
592
593 if ( ! is_wp_error( $requested_template_data ) ) {
594 $new_template_data = wp_remote_retrieve_body( $requested_template_data );
595 $new_template_data = json_decode( $new_template_data, true );
596
597 if ( $new_template_data && isset( $new_template_data['response'] ) && is_array( $new_template_data['response'] ) ) {
598 $template_data = $new_template_data['response'];
599 set_transient( 'gutenify_template_' . $type . '_' . $id, $template_data, DAY_IN_SECONDS );
600 }
601 }
602 }
603 break;
604 case 'local':
605 $post = get_post( $id );
606
607 if ( $post && 'gutenify_template' === $post->post_type ) {
608 $template_data = array(
609 'id' => $post->ID,
610 'title' => $post->post_title,
611 'content' => $post->post_content,
612 );
613 }
614
615 break;
616 }
617
618 if ( is_array( $template_data ) ) {
619 return $this->success( $template_data );
620 } else {
621 return $this->error( 'no_template_data', __( 'Template data not found.', '@@text_domain' ) );
622 }
623 }
624
625 /**
626 * Get edit options permissions.
627 *
628 * @return bool
629 */
630 public function create_kit_permission() {
631 if ( ! current_user_can( 'manage_options' ) ) {
632 return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change options.', '@@text_domain' ) );
633 }
634 return true;
635 }
636
637 public function create_kit( WP_REST_Request $request ) {
638 $data = $request->get_params();
639 $gutenify_template_kits = new Gutenify_Template_Kits();
640 $response = $gutenify_template_kits->add_kit( $data );
641
642 if ( $response ) {
643 return $this->success( $response );
644 }
645 return $this->error( 'error_creating_kit', __( 'Error creating Kit', '@@text_domain' ) );
646 }
647
648 public function get_site_options() {
649 $options = get_option( 'gutenify_site_options' );
650 return $this->success( $options );
651 }
652
653 /**
654 * Update site options.
655 *
656 * @param WP_REST_Request $request request object.
657 *
658 * @return mixed
659 */
660 public function update_site_options( WP_REST_Request $request ) {
661 $options = $request->get_params();
662 $merged_options = array();
663 if ( is_array( $options ) ) {
664 $current_options = get_option( 'gutenify_site_options', array() );
665 if ( empty( $current_options ) ) {
666 $current_options = array();
667 }
668 $merged_options = array_merge( $current_options, $options );
669 update_option( 'gutenify_site_options', array_merge( $current_options, $options ) );
670 }
671
672 return $this->success( $merged_options );
673 }
674
675 /**
676 * Update site options.
677 *
678 * @param WP_REST_Request $request request object.
679 *
680 * @return mixed
681 */
682 public function update_global_styles( WP_REST_Request $request ) {
683 $options = (array) $request->get_params();
684
685 if ( isset( $options['css'] ) ) {
686 update_option( 'gutenify_global_style', $options['css'] );
687 }
688
689 if ( isset( $options['adminCss'] ) ) {
690 update_option( 'gutenify_admin_global_style', $options['adminCss'] );
691 }
692
693 return $this->success( $options );
694 }
695
696 /**
697 * Success rest.
698 *
699 * @param mixed $response response data.
700 * @return mixed
701 */
702 public function success( $response ) {
703 return new WP_REST_Response(
704 array(
705 'success' => true,
706 'response' => $response,
707 ),
708 200
709 );
710 }
711
712 /**
713 * Error rest.
714 *
715 * @param mixed $code error code.
716 * @param mixed $response response data.
717 * @return mixed
718 */
719 public function error( $code, $response ) {
720 return new WP_REST_Response(
721 array(
722 'error' => true,
723 'success' => false,
724 'error_code' => $code,
725 'response' => $response,
726 ),
727 401
728 );
729 }
730 }
731 new Gutenify_Rest();
732