PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 2.2.10
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v2.2.10
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 3.1.10 All 111 releases
templately / includes / API / Items.php

Items.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 2.2.10, at includes/API/Items.php

408 lines 10.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\API;
4
5 use WP_REST_Request;
6 use Templately\Utils\Database;
7
8 use function json_decode;
9
10 class Items extends API {
11 public function permission_check( WP_REST_Request $request ) {
12 $this->request = $request;
13
14 $_route = $request->get_route();
15 if( $_route === '/templately/v1/items/favourite' ) {
16 return parent::permission_check( $request );
17 }
18
19 return true;
20 }
21
22 public function register_routes() {
23 $this->get( 'items', [ $this, 'get_items' ], [
24 'type' => [
25 'default' => 'items',
26 'required' => false,
27 // 'validate_callback' => function( $param, $request, $key ){
28 // return in_array( $param, [ 'items', 'sections', 'blocks', 'packs', 'pages' ], true );
29 // }
30 ],
31 'platform' => [
32 'default' => 'elementor',
33 'required' => false
34 ],
35 'per_page' => [
36 'default' => 40,
37 'required' => false
38 ],
39 ] );
40
41 $this->get( 'items/(?P<slug>[a-zA-Z0-9-]+)', [ $this, 'get_item' ], [
42 'slug' => [
43 'required' => true,
44 ],
45 'type' => [
46 'default' => 'block'
47 ]
48 ] );
49
50 $this->get( 'items/search/(?P<keyword>[a-zA-Z0-9-]+)', [ $this, 'get_search_results' ], [
51 'keyword' => [
52 'required' => true,
53 ],
54 'platform' => [
55 'default' => 'elementor',
56 ],
57 'query_type' => [
58 'default' => 'block',
59 ],
60 ] );
61
62 $this->post( 'items/favourite', [ $this, 'set_favourite' ] );
63 $this->get( 'items-count', [ $this, 'get_counts' ] );
64 $this->get( 'featured-items', [ $this, 'featured_items' ] );
65 }
66
67 /**
68 * @param string $type
69 *
70 * @return string
71 */
72 public function get_query_types( $type = 'blocks' ) {
73 $item_types = ( $type === 'blocks' || $type === 'sections' ) ? 'items' : trim( $type );
74 return in_array( $item_types, [ 'items', 'pages', 'packs' ], true ) ? $item_types : false;
75 }
76
77 public function get_items( WP_REST_Request $request ) {
78 $_type = $this->get_param( 'type', 'blocks' );
79 $type = $this->get_query_types( $_type );
80 $plan = $this->get_param( 'plan', 'all' );
81 $plan_type = $this->get_plan( $plan );
82 $platform = $this->get_param( 'platform', 'elementor' );
83 $search = $this->get_param( 'search' );
84 $page = $this->get_param( 'page', 1, 'intval' );
85 $per_page = $this->get_param( 'per_page', 40, 'intval' );
86 $template_type_id = $this->get_param( 'template_type_id', 0, 'intval' );
87 $category_id = $this->get_param( 'category_id', 0, 'intval' );
88
89 $dependencies = $request->get_param( 'dependencies' );
90 $tags = $request->get_param( 'tags' );
91
92 $funcArgs = [
93 'page' => $page,
94 'per_page' => $per_page,
95 'platform' => $platform,
96 ];
97
98 if ( $plan_type > 1 ) {
99 $funcArgs['plan_type'] = $plan_type;
100 }
101
102 if ( $category_id ) {
103 $funcArgs['category_id'] = $category_id;
104 }
105 if ( $template_type_id > 0 ) {
106 $funcArgs['template_type_id'] = $template_type_id;
107 }
108 if ( ! empty( $dependencies['include'] ) || ! empty( $dependencies['exclude'] ) ) {
109 $funcArgs['dependencies'] = wp_slash( json_encode( $dependencies ) );
110 }
111 if ( ! empty( $tags ) ) {
112 $funcArgs['tag_id'] = wp_slash( json_encode( $tags ) );
113 }
114
115 if ( ! empty( $search ) ) {
116 $funcArgs['search'] = $search;
117 }
118
119 $query = 'total_page, current_page, data { id, name, price, rating, downloads, type, template_type{ slug }, slug, favourite_count, thumbnail, thumbnail2, thumbnail3 }';
120 if( $type !== 'packs' ) {
121 $query = 'total_page, current_page, data { id, name, price, rating, downloads, type, template_type{ slug }, slug, favourite_count, dependencies{ id, name, icon, plugin_file, plugin_original_slug, is_pro, link }, thumbnail }';
122 }
123
124 if( $type === false ) {
125 return $this->error( 'invalid_type_call', __( 'Invalid Type Call', 'templately' ) );
126 }
127
128 return $this->http()->query(
129 $type,
130 $query,
131 $funcArgs
132 )->post();
133 }
134
135 public function get_item() {
136 $slug = $this->get_param( 'slug' );
137 $_type = $this->get_param( 'type', 'blocks' );
138 $type = $this->get_query_types( $_type );
139
140 if ( empty( $slug ) ) {
141 return $this->error(
142 'invalid_item_slug',
143 __( 'Items slug cannot be empty.', 'templately' ),
144 'items/:slug',
145 '400'
146 );
147 }
148
149 if( $type === false ) {
150 return $this->error( 'invalid_type_call', __( 'Invalid Type Call', 'templately' ) );
151 }
152
153 $items_params = 'id, name, rating, type, description, slug, price, features, favourite_count, thumbnail, downloads, categories{ id, name, slug }, dependencies{ id, name, icon, plugin_file, plugin_original_slug, is_pro, link }, tags{ name, id }, categories{ name, id }, screenshots{ url }, banner, pack{ id, name, slug, items{ id, price, name, type, slug, thumbnail } }, live_url, template_type{ id, name, slug }';
154 $params = 'data { ' . $items_params . ' }';
155
156 if ( $type == 'packs' ) {
157 $params = 'data { id, name, rating, type, slug, live_url, price, features, favourite_count, thumbnail, downloads, categories{ id, name, slug }, items { ' . $items_params . ' } }';
158 }
159
160 $response = $this->http()->query( $type, $params, [ 'slug' => $slug ] )->post();
161
162 if ( is_wp_error( $response ) ) {
163 return $response;
164 }
165
166 if( empty( $response['data'] ) ) {
167 return $this->error(
168 'invalid_response',
169 __('Item data not found', 'templately'),
170 '/items\/' . $slug,
171 '404'
172 );
173 }
174
175 return current( $response['data'] );
176 }
177
178 public function get_search_results( WP_REST_Request $request ) {
179 $keyword = $request->get_param( 'keyword' );
180 $platform = $request->get_param( 'platform' );
181 $query_type = $request->get_param( 'query_type' );
182
183 if ( empty( $keyword ) ) {
184 return $this->error(
185 'invalid_search_keyword',
186 __( 'Search keyword cannot be empty.', 'templately' ),
187 'items/search/:keyword',
188 '400'
189 );
190 }
191
192 $funcArgs = [
193 'search' => $keyword,
194 'platform' => $platform,
195 'query_type' => $query_type
196 ];
197
198 $response = $this->http()->query(
199 'getItemsAndPacks',
200 'total_page, current_page, data { id, name, rating, type, slug, favourite_count, thumbnail }',
201 $funcArgs
202 )->post();
203
204 if( is_wp_error( $response ) ){
205 return $response;
206 }
207
208 $modified_response = [
209 'data' => []
210 ];
211
212 if ( ! empty( $response['data'] ) ) {
213 $modified_response['data'] = array_map( function( $item ){
214 $item['id'] = (int) $item['id'];
215 return $item;
216 }, $response['data'] );
217 unset( $response['data'] );
218 }
219
220 return array_merge( $response, $modified_response );
221 }
222
223 public function set_favourite(){
224 $id = $this->get_param( 'id', 0, 'intval' );
225 $type = $this->get_param( 'itemType', 'block' );
226 $action = $this->get_param( 'action', 'do' );
227
228 $query = 'status, message, data';
229 if( $action === 'undo' ) {
230 $query = '';
231 }
232
233 $funcArgs = [
234 'api_key' => $this->api_key,
235 'type_id' => $id,
236 'type' => $type,
237 ];
238
239 $response = $this->http()->mutation(
240 $action === 'undo' ? 'unFavourite' : 'favourite',
241 $query,
242 $funcArgs
243 )->post();
244
245 if( is_wp_error( $response ) ) {
246 return $response;
247 }
248
249 $_response = [];
250
251 $_data = $response;
252 if( $action == 'do' ) {
253 if( ! empty( $_data['data'] ) && $_data['status'] === 'success' ) {
254 $_data['data'] = $_temp_data = json_decode( $_data['data'] );
255 $_temp_data = [ $_temp_data ];
256
257 $_favourites = $this->utils('options')->get('favourites');
258 $_favourites = $this->utils('helper')->normalizeFavourites( $_temp_data, $_favourites );
259 $this->utils('options')->set('favourites', $_favourites);
260
261 $_response['status'] = 'success';
262 $_response['data'] = $_favourites;
263 }
264 }
265
266 if( $action == 'undo' ) {
267 $_temp_data = [
268 'id' => $id,
269 'type' => $type == 'block' || $type == 'page' ? 'item' : 'pack'
270 ];
271
272 if( $response == 1 ) {
273 $_favourites = $this->utils('options')->get('favourites');
274 $_favourites = $this->utils('helper')->normalizeFavourites( $_temp_data, $_favourites, true );
275 $this->utils('options')->set('favourites', $_favourites);
276
277 $_response['status'] = 'success';
278 $_response['data'] = $_favourites;
279 } else {
280 $_response['status'] = 'error';
281 $_response['message'] = __( 'Unfavourite Action Failed: Something went wrong.', 'templately' );
282 }
283 }
284
285 return $_response;
286 }
287
288 public function get_counts(){
289 $defaults = Database::get_transient( 'counts' );
290 if( $defaults ) {
291 return $defaults;
292 }
293
294 $defaults = [
295 'elementor' => [
296 'items' => [
297 'total' => '1469',
298 'starter' => '964',
299 'pro' => '415',
300 ],
301 'blocks' => [
302 'total' => '517',
303 'starter' => '358',
304 'pro' => '159',
305 ],
306 'pages' => [
307 'total' => '803',
308 'starter' => '506',
309 'pro' => '297',
310 ],
311 'packs' => [
312 'total' => '149',
313 'starter' => '100',
314 'pro' => '49',
315 ]
316 ],
317 'gutenberg' => [
318 'blocks' => [
319 'total' => '3',
320 'starter' => '3',
321 'pro' => '0',
322 ],
323 'pages' => [
324 'total' => '3',
325 'starter' => '3',
326 'pro' => '0',
327 ],
328 'packs' => [
329 'total' => '3',
330 'starter' => '3',
331 'pro' => '0',
332 ],
333 ]
334 ];
335
336 $response = $this->http()->query(
337 'getCounts',
338 'key, value'
339 )->post();
340
341 if ( is_wp_error( $response ) ) {
342 return $defaults;
343 }
344
345 $new_array = [
346 'items' => [],
347 'blocks' => [],
348 'pages' => [],
349 'packs' => [],
350 ];
351 $_new_data = [];
352
353 array_walk( $response, function( $item ) use ( &$new_array, &$_new_data ) {
354 if( in_array( $item['key'] , ['elementor', 'gutenberg'], true ) ) {
355 $values = json_decode($item['value'], true);
356
357 array_walk( $values, function( $_item ) use ( &$new_array ) {
358 $new_key = explode('-', $_item['key']);
359 if( count( $new_key ) === 2 ) {
360 if( isset( $new_array[ $new_key[1] ] )) {
361 $new_array[ $new_key[1] ] = array_merge( $new_array[ $new_key[1] ], [ $new_key[0] => $_item['value'] ] );
362 $temp_array = $new_array[ $new_key[1] ];
363 unset( $temp_array['total'] );
364 $new_array[ $new_key[1] ]['total'] = array_sum( $temp_array );
365 }
366 }
367 });
368
369 $_new_data[ $item['key'] ] = $new_array;
370 }
371 });
372
373
374 Database::set_transient( 'counts', $_new_data );
375
376 return $_new_data;
377 }
378
379 /**
380 * Get Featured Item List
381 * @param WP_REST_Request $request
382 * @return mixed
383 */
384 public function featured_items(WP_REST_Request $request){
385 $defaults = Database::get_transient( 'featuredItems' );
386 if( $defaults ) {
387 return $defaults;
388 }
389
390 $platform = $request->get_param( 'platform' );
391 $funcArgs = [
392 // 'page' => 1,
393 // 'per_page' => 10,
394 'platform' => $platform,
395 ];
396 $response = $this->http()->query(
397 'featuredItems',
398 'data{ id, name, slug, price, type, thumbnail }',
399 $funcArgs
400 )->post();
401
402 if( ! is_wp_error( $response ) ) {
403 Database::set_transient( 'featuredItems', $response );
404 }
405
406 return $response;
407 }
408 }