PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.8.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.8.0
3.8.0 3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 All 112 releases
templately / modules / template-browsing / REST / TemplateTypes.php

TemplateTypes.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.8.0, at modules/template-browsing/REST/TemplateTypes.php

90 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Templately\Modules\TemplateBrowsing\REST;
3
4 use Templately\API\API;
5
6 use Templately\Utils\Database;
7 use WP_REST_Request;
8
9 /**
10 * TemplateTypes Class
11 *
12 * @since 2.0.0
13 * @package Templately\API
14 */
15 class TemplateTypes extends API {
16 private $endpoint = 'templateTypes';
17
18 /**
19 * Grouped template types, keyed block|page|pack.
20 *
21 * Untyped on purpose: a typed property is PHP 7.4+ and this tree must parse
22 * on the advertised 7.2 floor (`Requires PHP` in readme.txt).
23 *
24 * @var array
25 */
26 private $_items = [];
27
28 public function permission_check( WP_REST_Request $request ) {
29 $this->request = $request;
30 return true;
31 }
32
33 public function register_routes() {
34 $this->get( $this->endpoint, [ $this, 'get_types' ] );
35 }
36
37 public function get_types() {
38 /**
39 * Return if there is any cache data.
40 */
41 $_type = $this->get_param( 'itemType', 'page' );
42 $_type = rtrim( $_type, 's');
43
44 $types = Database::get_transient( $this->endpoint );
45 if( ! empty( $types ) && ! empty( $types[ $_type ] ) ){
46 return $this->success( $types[ $_type ] );
47 }
48
49 $id = $this->get_param( 'id', 0, 'intval' );
50 $funcArgs = [];
51
52 if ( $id > 0 ) {
53 $funcArgs['id'] = $id;
54 }
55
56 $query_params = 'item_template_types{id, name, slug}, page_template_types{id, name, slug}, pack_template_types{id, name, slug}';
57 $response = $this->http()->query( 'groupedTemplateTypes', $query_params, $funcArgs )->post();
58 /**
59 * Caching the response in transient.
60 */
61 if( ! is_wp_error( $response ) ) {
62 $this->_items = [
63 'block' => [],
64 'page' => [],
65 'pack' => [],
66 ];
67 if( is_array( $response ) ) {
68 array_walk( $response, function( $_item, $key ) {
69 switch( $key ) {
70 case 'item_template_types':
71 $this->_items['block'] = $_item;
72 break;
73 case 'page_template_types':
74 $this->_items['page'] = $_item;
75 break;
76 case 'pack_template_types':
77 $this->_items['pack'] = $_item;
78 break;
79 }
80 } );
81
82 Database::set_transient( $this->endpoint, $this->_items );
83 }
84
85 return $this->_items[ $_type ];
86 }
87
88 return $response;
89 }
90 }