PluginProbe
Gutenify – Visual Site Builder Blocks & Starter Templates / 1.0.3
Gutenify – Visual Site Builder Blocks & Starter Templates v1.0.3
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.0.3, at inc/class-rest.php

731 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 = array_merge( $templates, $local_templates, $theme_templates );
356
357 if ( is_array( $templates ) ) {
358 return $this->success( $templates );
359 } else {
360 return $this->error( 'no_templates', __( 'Templates not found.', '@@text_domain' ) );
361 }
362 }
363
364 /**
365 * Get templates.
366 *
367 * @param WP_REST_Request $request request object.
368 *
369 * @return mixed
370 */
371 public function get_template_data( WP_REST_Request $request ) {
372 $api_site = defined( 'GUTENIFY_API_URL' ) ? GUTENIFY_API_URL : 'https://api.gutenify.com/';
373 $url = $api_site . 'wp-json/gutenify-library/v1/get_library_data/';
374 $id = $request->get_param( 'id' );
375 $type = $request->get_param( 'type' );
376 $template_data = false;
377
378 switch ( $type ) {
379 case 'remote':
380 $template_data = get_transient( 'gutenify_template_' . $type . '_' . $id, false );
381
382 if ( ! $template_data ) {
383 $requested_template_data = wp_remote_get(
384 add_query_arg(
385 apply_filters(
386 'gutenify_rest_template_data_url_args',
387 array(
388 'id' => $id,
389 'gutenify_version' => GUTENIFY_VERSION,
390 'gutenify_site' => site_url( '/' ),
391 )
392 ),
393 $url
394 )
395 );
396
397 if ( ! is_wp_error( $requested_template_data ) ) {
398 $new_template_data = wp_remote_retrieve_body( $requested_template_data );
399 $new_template_data = json_decode( $new_template_data, true );
400
401 if ( $new_template_data && isset( $new_template_data['response'] ) && is_array( $new_template_data['response'] ) ) {
402 $template_data = $new_template_data['response'];
403 set_transient( 'gutenify_template_' . $type . '_' . $id, $template_data, DAY_IN_SECONDS );
404 }
405 }
406 }
407 break;
408 case 'local':
409 $post = get_post( $id );
410
411 if ( $post && 'gutenify_template' === $post->post_type ) {
412 $template_data = array(
413 'id' => $post->ID,
414 'title' => $post->post_title,
415 'content' => $post->post_content,
416 );
417 }
418
419 break;
420 case 'theme':
421 $template_content_file = get_stylesheet_directory() . '/gutenify/templates/' . $id . '/content.php';
422
423 if ( ! file_exists( $template_content_file ) ) {
424 $template_content_file = get_template_directory() . '/gutenify/templates/' . $id . '/content.php';
425 }
426
427 if ( file_exists( $template_content_file ) ) {
428 ob_start();
429 include $template_content_file;
430 $template_content = ob_get_clean();
431
432 if ( $template_content ) {
433 $template_data = get_file_data(
434 $template_content_file,
435 array(
436 'name' => 'Name',
437 )
438 );
439
440 $template_data = array(
441 'id' => $id,
442 'title' => $template_data['name'],
443 'content' => $template_content,
444 );
445 }
446 }
447 break;
448 }
449
450 if ( is_array( $template_data ) ) {
451 return $this->success( $template_data );
452 } else {
453 return $this->error( 'no_template_data', __( 'Template data not found.', '@@text_domain' ) );
454 }
455 }
456
457 /**
458 * Get settings.
459 *
460 * @return mixed
461 */
462 public function get_settings() {
463 $settings = gutenify_settings();
464
465 if ( is_array( $settings ) ) {
466 return $this->success( $settings );
467 } else {
468 return $this->error( 'no_settings', __( 'Settings data not found.', '@@text_domain' ) );
469 }
470 }
471
472 /**
473 * Get edit options permissions.
474 *
475 * @return bool
476 */
477 public function update_settings_permission() {
478 if ( ! current_user_can( 'manage_options' ) ) {
479 return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change options.', '@@text_domain' ) );
480 }
481 return true;
482 }
483
484 /**
485 * Update Settings.
486 *
487 * @param WP_REST_Request $request request object.
488 *
489 * @return mixed
490 */
491 public function update_settings( WP_REST_Request $request ) {
492 $new_settings = $request->get_param( 'settings' );
493
494 if ( is_array( $new_settings ) ) {
495 $current_settings = get_option( 'gutenify_settings', array() );
496 update_option( 'gutenify_settings', array_merge( $current_settings, $new_settings ) );
497 }
498
499 return $this->success( true );
500 }
501
502 /**
503 * Get templates.
504 *
505 * @return mixed
506 */
507 public function get_template_kit( WP_REST_Request $request ) {
508 $api_site = defined( 'GUTENIFY_API_URL' ) ? GUTENIFY_API_URL : 'https://api.gutenify.com/';
509 $url = $api_site . 'wp-json/gutenify-library/v1/get_kit_library/';
510 $templates = get_transient( 'gutenify_remote_template_kits', false );
511 $data = $request->get_params();
512
513 if ( ! empty( $data['reset'] ) && 'true' === $data['reset'] ) {
514 $templates = array();
515 }
516
517 /*
518 * Get remote templates.
519 */
520 if ( ! $templates ) {
521 $requested_templates = wp_remote_get(
522 add_query_arg(
523 array(
524 'gutenify_version' => '2.19.3',
525 'gutenify_pro' => false,
526 'gutenify_pro_version' => null,
527 ),
528 $url
529 )
530 );
531
532 if ( ! is_wp_error( $requested_templates ) ) {
533 $new_templates = wp_remote_retrieve_body( $requested_templates );
534 $new_templates = json_decode( $new_templates, true );
535
536 if ( $new_templates && isset( $new_templates['response'] ) && is_array( $new_templates['response'] ) ) {
537 $templates = $new_templates['response'];
538 set_transient( 'gutenify_remote_template_kits', $templates, DAY_IN_SECONDS );
539 }
540 }
541 }
542
543 // Remove Pro templates from array, cause for now there is no way to check if pro addon is activated.
544 if ( $templates ) {
545 foreach ( $templates as $k => $template ) {
546 $is_pro = false;
547
548 if ( isset( $template['types'] ) && is_array( $template['types'] ) ) {
549 foreach ( $template['types'] as $type ) {
550 $is_pro = $is_pro || 'pro' === $type['slug'];
551 }
552 }
553
554 if ( $is_pro ) {
555 unset( $templates[ $k ] );
556 }
557 }
558 }
559
560 if ( is_array( $templates ) ) {
561 return $this->success( $templates );
562 } else {
563 return $this->error( 'no_template_kits', __( 'Template kit not found.', '@@text_domain' ) );
564 }
565 }
566
567 public function get_template_kit_data( WP_REST_Request $request ) {
568 $api_site = defined( 'GUTENIFY_API_URL' ) ? GUTENIFY_API_URL : 'https://api.gutenify.com/';
569 $url = $api_site . 'wp-json/gutenify-library/v1/get_kit_library_data/';
570 $id = $request->get_param( 'id' );
571 $type = $request->get_param( 'type' );
572 $template_data = false;
573 switch ( $type ) {
574 case 'remote':
575 $template_data = get_transient( 'gutenify_template_kit_' . $type . '_' . $id, false );
576
577 if ( ! $template_data ) {
578 $requested_template_data = wp_remote_get(
579 add_query_arg(
580 apply_filters(
581 'gutenify_rest_template_data_url_args',
582 array(
583 'id' => $id,
584 'gutenify_version' => GUTENIFY_VERSION,
585 'gutenify_site' => site_url( '/' ),
586 )
587 ),
588 $url
589 )
590 );
591
592 if ( ! is_wp_error( $requested_template_data ) ) {
593 $new_template_data = wp_remote_retrieve_body( $requested_template_data );
594 $new_template_data = json_decode( $new_template_data, true );
595
596 if ( $new_template_data && isset( $new_template_data['response'] ) && is_array( $new_template_data['response'] ) ) {
597 $template_data = $new_template_data['response'];
598 set_transient( 'gutenify_template_' . $type . '_' . $id, $template_data, DAY_IN_SECONDS );
599 }
600 }
601 }
602 break;
603 case 'local':
604 $post = get_post( $id );
605
606 if ( $post && 'gutenify_template' === $post->post_type ) {
607 $template_data = array(
608 'id' => $post->ID,
609 'title' => $post->post_title,
610 'content' => $post->post_content,
611 );
612 }
613
614 break;
615 }
616
617 if ( is_array( $template_data ) ) {
618 return $this->success( $template_data );
619 } else {
620 return $this->error( 'no_template_data', __( 'Template data not found.', '@@text_domain' ) );
621 }
622 }
623
624 /**
625 * Get edit options permissions.
626 *
627 * @return bool
628 */
629 public function create_kit_permission() {
630 if ( ! current_user_can( 'manage_options' ) ) {
631 return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change options.', '@@text_domain' ) );
632 }
633 return true;
634 }
635
636 public function create_kit( WP_REST_Request $request ) {
637 $data = $request->get_params();
638 $gutenify_template_kits = new Gutenify_Template_Kits();
639 $response = $gutenify_template_kits->add_kit( $data );
640
641 if ( $response ) {
642 return $this->success( $response );
643 }
644 return $this->error( 'error_creating_kit', __( 'Error creating Kit', '@@text_domain' ) );
645 }
646
647 public function get_site_options() {
648 $options = get_option( 'gutenify_site_options' );
649 return $this->success( $options );
650 }
651
652 /**
653 * Update site options.
654 *
655 * @param WP_REST_Request $request request object.
656 *
657 * @return mixed
658 */
659 public function update_site_options( WP_REST_Request $request ) {
660 $options = $request->get_params();
661 $merged_options = array();
662 if ( is_array( $options ) ) {
663 $current_options = get_option( 'gutenify_site_options', array() );
664 if ( empty( $current_options ) ) {
665 $current_options = array();
666 }
667 $merged_options = array_merge( $current_options, $options ) ;
668 update_option( 'gutenify_site_options', array_merge( $current_options, $options ) );
669 }
670
671 return $this->success( $merged_options );
672 }
673
674 /**
675 * Update site options.
676 *
677 * @param WP_REST_Request $request request object.
678 *
679 * @return mixed
680 */
681 public function update_global_styles( WP_REST_Request $request ) {
682 $options = (array) $request->get_params();
683
684 if ( isset( $options['css'] ) ) {
685 update_option( 'gutenify_global_style', $options['css'] );
686 }
687
688 if ( isset( $options['adminCss'] ) ) {
689 update_option( 'gutenify_admin_global_style', $options['adminCss'] );
690 }
691
692 return $this->success( $options );
693 }
694
695 /**
696 * Success rest.
697 *
698 * @param mixed $response response data.
699 * @return mixed
700 */
701 public function success( $response ) {
702 return new WP_REST_Response(
703 array(
704 'success' => true,
705 'response' => $response,
706 ),
707 200
708 );
709 }
710
711 /**
712 * Error rest.
713 *
714 * @param mixed $code error code.
715 * @param mixed $response response data.
716 * @return mixed
717 */
718 public function error( $code, $response ) {
719 return new WP_REST_Response(
720 array(
721 'error' => true,
722 'success' => false,
723 'error_code' => $code,
724 'response' => $response,
725 ),
726 401
727 );
728 }
729 }
730 new Gutenify_Rest();
731