PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.7.4
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.7.4
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! 3.7.4, at includes/API/Items.php

647 lines 18.0 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' || $_route === '/templately/v1/items/rating' ) {
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 'sort_by' => [
40 'default' => 'latest',
41 'required' => false,
42 'validate_callback' => function( $param, $request, $key ){
43 return empty( $param ) || in_array( $param, [ 'latest', 'rating', 'download' ], true );
44 }
45 ],
46 ] );
47
48 // only number
49 $this->get( 'items/(?P<id>[0-9]+)', [ $this, 'get_item_by_id' ], [
50 'id' => [
51 'required' => true,
52 ],
53 'type' => [
54 'default' => 'block'
55 ]
56 ] );
57
58 // with slug
59 $this->get( 'items/(?P<slug>[a-zA-Z0-9-]+)', [ $this, 'get_item' ], [
60 'slug' => [
61 'required' => true,
62 ],
63 'type' => [
64 'default' => 'block'
65 ]
66 ] );
67
68 $this->get( 'items/search/(?P<keyword>[a-zA-Z0-9-]+)', [ $this, 'get_search_results' ], [
69 'keyword' => [
70 'required' => true,
71 ],
72 'platform' => [
73 'default' => 'elementor',
74 ],
75 'query_type' => [
76 'default' => 'block',
77 ],
78 ] );
79
80 $this->post( 'items/favourite', [ $this, 'set_favourite' ] );
81 $this->post( 'items/rating', [ $this, 'set_rating' ] );
82 $this->get( 'items-count', [ $this, 'get_counts' ] );
83 $this->get( 'featured-items', [ $this, 'featured_items' ] );
84 $this->get( 'trending-items', [ $this, 'trending_items' ] );
85 $this->get( 'related-items', [ $this, 'related_items' ]);
86 }
87
88 /**
89 * @param string $type
90 *
91 * @return string
92 */
93 public function get_query_types( $type = 'blocks' ) {
94 $item_types = ( $type === 'blocks' || $type === 'sections' ) ? 'items' : trim( $type );
95 return in_array( $item_types, [ 'items', 'pages', 'packs' ], true ) ? $item_types : false;
96 }
97
98 public function get_items( WP_REST_Request $request ) {
99 $_type = $this->get_param( 'type', 'blocks' );
100 $type = $this->get_query_types( $_type );
101 $plan = $this->get_param( 'plan', 'all' );
102 $plan_type = $this->get_plan( $plan );
103 $platform = $this->get_param( 'platform', 'elementor' );
104 $search = $this->get_param( 'search' );
105 $include_search = $this->get_param( 'include_search' );
106 $page = $this->get_param( 'page', 1, 'intval' );
107 $per_page = $this->get_param( 'per_page', 40, 'intval' );
108 $template_type_id = $this->get_param( 'template_type_id', 0, 'intval' );
109 $categories_raw = $this->get_param( 'categories' );
110 $sort_by = $this->get_param( 'sort_by', 'latest' );
111
112 $dependencies = $request->get_param( 'dependencies' );
113 $tags = $request->get_param( 'tags' );
114
115 $funcArgs = [
116 'page' => $page,
117 'per_page' => $per_page,
118 'platform' => $platform,
119 'sort_by' => $sort_by,
120 ];
121
122 if ( $plan_type > 1 ) {
123 $funcArgs['plan_type'] = $plan_type;
124 }
125
126 if ( ! empty( $categories_raw ) ) {
127 $decoded = json_decode( $categories_raw, true );
128 if ( is_array( $decoded ) && ! empty( $decoded ) ) {
129 $funcArgs['categories'] = wp_slash( json_encode( array_values( array_filter( array_map( 'intval', $decoded ) ) ) ) );
130 }
131 }
132 if ( $template_type_id > 0 ) {
133 $funcArgs['template_type_id'] = $template_type_id;
134 }
135 if ( ! empty( $dependencies['include'] ) || ! empty( $dependencies['exclude'] ) ) {
136 $funcArgs['dependencies'] = wp_slash( json_encode( $dependencies ) );
137 }
138 if ( ! empty( $tags ) ) {
139 $funcArgs['tag_id'] = wp_slash( json_encode( $tags ) );
140 }
141
142 if ( ! empty( $search ) ) {
143 $funcArgs['search'] = $search;
144 }
145
146 if ( ! empty( $include_search ) ) {
147 $funcArgs['include_search'] = $include_search;
148 }
149
150 $query = 'total_page, current_page, data { id, fullsite_import, name, price, rating, downloads, type, template_type{ slug }, slug, favourite_count, thumbnail, thumbnail2, thumbnail3, badges }';
151 if( $type !== 'packs' ) {
152 $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, pack { id, has_settings }, badges }';
153 }
154
155 if( $type === false ) {
156 return $this->error( 'invalid_type_call', __( 'Invalid Type Call', 'templately' ) );
157 }
158
159 $transient_key = "_templately_items_" . md5(json_encode([$type, $query, $funcArgs]));
160 $response = false;
161
162 if ( defined( 'TEMPLATELY_ITEMS_TRANSIENT' ) && constant( 'TEMPLATELY_ITEMS_TRANSIENT' ) ) {
163 $response = Database::get_transient( $transient_key );
164 }
165
166 if ( empty( $response ) ) {
167 $response = $this->http()->query( $type, $query, $funcArgs )->post();
168
169 if ( ! empty( $response ) && ! is_wp_error( $response ) && defined( 'TEMPLATELY_ITEMS_TRANSIENT' ) && constant( 'TEMPLATELY_ITEMS_TRANSIENT' ) ) {
170 Database::set_transient( $transient_key, $response );
171 }
172 }
173
174 return $response;
175 }
176
177 public function get_item() {
178 $slug = $this->get_param( 'slug' );
179 $_type = $this->get_param( 'type', 'blocks' );
180 $type = $this->get_query_types( $_type );
181
182 if ( empty( $slug ) ) {
183 return $this->error(
184 'invalid_item_slug',
185 __( 'Items slug cannot be empty.', 'templately' ),
186 'items/:slug',
187 '400'
188 );
189 }
190
191 if( $type === false ) {
192 return $this->error( 'invalid_type_call', __( 'Invalid Type Call', 'templately' ) );
193 }
194
195 $items_params = 'id, name, rating, type, description, slug, price, features, favourite_count, is_favourite, is_reviewed, 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, published_at, updated_at, is_trending, badges, pack{ id, name, slug, items{ id, price, name, type, slug, thumbnail, badges }, has_settings }, live_url, template_type{ id, name, slug }';
196 $params = 'data { ' . $items_params . ', variations { name, slug, type, platform } }';
197
198 if ( $type == 'packs' ) {
199 $params = 'data { id, fullsite_import, ai_compatible, has_settings, has_attachments, name, rating, type, slug, live_url, price, features, favourite_count, is_favourite, is_reviewed, thumbnail, description, tags{ name, id }, banner, published_at, updated_at, is_trending, badges, template_type{ id, name, slug } downloads, categories{ id, name, slug }, items { ' . $items_params . ' }, variations { name, slug, type, platform } }';
200 }
201
202 $args = [
203 'slug' => $slug,
204 ];
205 if (!empty($this->api_key)) {
206 $args['api_key'] = $this->api_key;
207 }
208
209 $transient_key = "_templately_items_" . md5(json_encode([$type, $params, $args]));
210 $response = false;
211
212 if ( defined( 'TEMPLATELY_ITEMS_TRANSIENT' ) && constant( 'TEMPLATELY_ITEMS_TRANSIENT' ) ) {
213 $response = Database::get_transient( $transient_key );
214 }
215
216 if ( empty( $response ) ) {
217 $response = $this->http()->query( $type, $params, $args )->post();
218
219 if ( ! empty( $response ) && ! is_wp_error( $response ) && defined( 'TEMPLATELY_ITEMS_TRANSIENT' ) && constant( 'TEMPLATELY_ITEMS_TRANSIENT' ) ) {
220 Database::set_transient( $transient_key, $response );
221 }
222 }
223
224 if ( is_wp_error( $response ) ) {
225 return $response;
226 }
227
228 if( empty( $response['data'] ) ) {
229 return $this->error(
230 'invalid_response',
231 __('Item data not found', 'templately'),
232 '/items\/' . $slug,
233 '404'
234 );
235 }
236
237 return current( $response['data'] );
238 }
239
240 public function get_item_by_id() {
241 $id = (int) $this->get_param( 'id' );
242 $_type = $this->get_param( 'type', 'blocks' );
243 $type = $this->get_query_types( $_type );
244
245 if ( empty( $id ) ) {
246 return $this->error(
247 'invalid_item_slug',
248 __( 'Items id cannot be empty.', 'templately' ),
249 'items/:id',
250 '400'
251 );
252 }
253
254 if( $type === false ) {
255 return $this->error( 'invalid_type_call', __( 'Invalid Type Call', 'templately' ) );
256 }
257
258 $items_params = 'id, name, rating, type, description, slug, price, features, favourite_count, is_favourite, is_reviewed, 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, published_at, updated_at, is_trending, badges, pack{ id, name, slug, items{ id, price, name, type, slug, thumbnail, badges }, has_settings }, live_url, template_type{ id, name, slug }';
259 $params = 'data { ' . $items_params . ', variations { name, slug, type, platform } }';
260
261 if ( $type == 'packs' ) {
262 $params = 'data { id, fullsite_import, ai_compatible, has_settings, has_attachments, name, rating, type, slug, live_url, price, features, favourite_count, is_favourite, is_reviewed, thumbnail, description, tags{ name, id }, banner, published_at, updated_at, is_trending, badges, template_type{ id, name, slug } downloads, categories{ id, name, slug }, items { ' . $items_params . ' }, variations { name, slug, type, platform } }';
263 }
264
265 $args = [
266 'id' => $id,
267 ];
268 if (!empty($this->api_key)) {
269 $args['api_key'] = $this->api_key;
270 }
271
272 $response = $this->http()->query( $type, $params, $args )->post();
273
274 if ( is_wp_error( $response ) ) {
275 return $response;
276 }
277
278 if( empty( $response['data'] ) ) {
279 return $this->error(
280 'invalid_response',
281 __('Item data not found', 'templately'),
282 '/items\/' . $id,
283 '404'
284 );
285 }
286
287 return current( $response['data'] );
288 }
289
290 public function get_search_results( WP_REST_Request $request ) {
291 $keyword = $request->get_param( 'keyword' );
292 $platform = $request->get_param( 'platform' );
293 $query_type = $request->get_param( 'query_type' );
294
295 if ( empty( $keyword ) ) {
296 return $this->error(
297 'invalid_search_keyword',
298 __( 'Search keyword cannot be empty.', 'templately' ),
299 'items/search/:keyword',
300 '400'
301 );
302 }
303
304 $funcArgs = [
305 'search' => $keyword,
306 'platform' => $platform,
307 'query_type' => $query_type
308 ];
309
310 $response = $this->http()->query(
311 'getItemsAndPacks',
312 'total_page, current_page, data { id, name, rating, type, slug, favourite_count, thumbnail }',
313 $funcArgs
314 )->post();
315
316 if( is_wp_error( $response ) ){
317 return $response;
318 }
319
320 $modified_response = [
321 'data' => []
322 ];
323
324 if ( ! empty( $response['data'] ) ) {
325 $modified_response['data'] = array_map( function( $item ){
326 $item['id'] = (int) $item['id'];
327 return $item;
328 }, $response['data'] );
329 unset( $response['data'] );
330 }
331
332 return array_merge( $response, $modified_response );
333 }
334
335 public function set_favourite(){
336 $id = $this->get_param( 'id', 0, 'intval' );
337 $type = $this->get_param( 'itemType', 'block' );
338 $action = $this->get_param( 'action', 'do' );
339
340 $query = 'status, message, data';
341 if( $action === 'undo' ) {
342 $query = '';
343 }
344
345 $funcArgs = [
346 'api_key' => $this->api_key,
347 'type_id' => $id,
348 'type' => $type,
349 ];
350
351 $response = $this->http()->mutation(
352 $action === 'undo' ? 'unFavourite' : 'favourite',
353 $query,
354 $funcArgs
355 )->post();
356
357 if( is_wp_error( $response ) ) {
358 return $response;
359 }
360
361 $_response = [];
362
363 $_data = $response;
364 if( $action == 'do' ) {
365 if( ! empty( $_data['data'] ) && $_data['status'] === 'success' ) {
366 $_data['data'] = $_temp_data = json_decode( $_data['data'] );
367 $_temp_data = [ $_temp_data ];
368
369 $_favourites = $this->utils('options')->get('favourites');
370 $_favourites = $this->utils('helper')->normalizeFavourites( $_temp_data, $_favourites );
371 $this->utils('options')->set('favourites', $_favourites);
372
373 $_response['status'] = 'success';
374 $_response['data'] = $_favourites;
375 }
376 }
377
378 if( $action == 'undo' ) {
379 $_temp_data = [
380 'id' => $id,
381 'type' => $type == 'block' || $type == 'page' ? 'item' : 'pack'
382 ];
383
384 if( $response == 1 || $response === false ) {
385 $_favourites = $this->utils('options')->get('favourites');
386 $_favourites = $this->utils('helper')->normalizeFavourites( $_temp_data, $_favourites, true );
387 $this->utils('options')->set('favourites', $_favourites);
388
389 $_response['status'] = 'success';
390 $_response['data'] = $_favourites;
391 } else {
392 $_response['status'] = 'error';
393 $_response['message'] = __( 'Unfavourite Action Failed: Something went wrong.', 'templately' );
394 }
395 }
396
397 return $_response;
398 }
399
400 public function set_rating(){
401 $type_id = $this->get_param( 'type_id', 0, 'intval' );
402 $itemType = $this->get_param( 'itemType', 'pack' );
403 $rating = $this->get_param( 'rating', 5, 'intval' );
404
405 $query = 'status, message, data';
406
407 $funcArgs = [
408 'api_key' => $this->api_key,
409 'type_id' => $type_id,
410 'type' => $itemType,
411 'rating' => $rating,
412 ];
413
414 $response = $this->http()->mutation(
415 'review',
416 $query,
417 $funcArgs
418 )->post();
419
420 if( is_wp_error( $response ) ) {
421 return $response;
422 }
423
424 if( empty( $response['data'] ) ) {
425 return $this->error(
426 'invalid_response',
427 __('Something went wrong.', 'templately'),
428 '/items\/rating',
429 '404'
430 );
431 }
432
433 if( ! empty( $response['data'] ) && $response['status'] === 'success' ) {
434 $response['data'] = json_decode( $response['data'], true );
435
436 $_ratings = $this->utils('options')->get('reviews', []);
437 // $_reviews = $this->utils( 'helper' )->normalizeReviews( $response['user']['reviews'] );
438 $_ratings[ $itemType ][ $type_id ] = $rating;
439 $this->utils('options')->set('reviews', $_ratings);
440
441 $_ratings[ $itemType ][ "avg_$type_id" ] = (float) $response['data']['avg_rating'];
442 $response['data']['reviews'] = $_ratings;
443 }
444
445 return $response;
446 }
447
448 public function get_counts(){
449 $defaults = Database::get_transient( 'counts' );
450 if( $defaults ) {
451 return $defaults;
452 }
453
454 $defaults = [
455 'elementor' => [
456 'items' => [
457 'total' => '1469',
458 'starter' => '964',
459 'pro' => '415',
460 ],
461 'blocks' => [
462 'total' => '517',
463 'starter' => '358',
464 'pro' => '159',
465 ],
466 'pages' => [
467 'total' => '803',
468 'starter' => '506',
469 'pro' => '297',
470 ],
471 'packs' => [
472 'total' => '149',
473 'starter' => '100',
474 'pro' => '49',
475 ]
476 ],
477 'gutenberg' => [
478 'blocks' => [
479 'total' => '3',
480 'starter' => '3',
481 'pro' => '0',
482 ],
483 'pages' => [
484 'total' => '3',
485 'starter' => '3',
486 'pro' => '0',
487 ],
488 'packs' => [
489 'total' => '3',
490 'starter' => '3',
491 'pro' => '0',
492 ],
493 ]
494 ];
495
496 $response = $this->http()->query(
497 'getCounts',
498 'key, value'
499 )->post();
500
501 if ( is_wp_error( $response ) ) {
502 return $defaults;
503 }
504
505 $new_array = [
506 'items' => [],
507 'blocks' => [],
508 'pages' => [],
509 'packs' => [],
510 ];
511 $_new_data = [];
512
513 array_walk( $response, function( $item ) use ( &$new_array, &$_new_data ) {
514 if( in_array( $item['key'] , ['elementor', 'gutenberg'], true ) ) {
515 $values = json_decode($item['value'], true);
516
517 array_walk( $values, function( $_item ) use ( &$new_array ) {
518 $new_key = explode('-', $_item['key']);
519 if( count( $new_key ) === 2 ) {
520 if( isset( $new_array[ $new_key[1] ] )) {
521 $new_array[ $new_key[1] ] = array_merge( $new_array[ $new_key[1] ], [ $new_key[0] => $_item['value'] ] );
522 $temp_array = $new_array[ $new_key[1] ];
523 unset( $temp_array['total'] );
524 $new_array[ $new_key[1] ]['total'] = array_sum( $temp_array );
525 }
526 }
527 });
528
529 $_new_data[ $item['key'] ] = $new_array;
530 }
531 });
532
533
534 Database::set_transient( 'counts', $_new_data );
535
536 return $_new_data;
537 }
538
539 /**
540 * Get Featured Item List
541 * @param WP_REST_Request $request
542 * @return mixed
543 */
544 public function featured_items(WP_REST_Request $request){
545 $platform = $request->get_param( 'platform' );
546 $defaults = Database::get_transient( "featuredItems_{$platform}" );
547 if( $defaults ) {
548 return $defaults;
549 }
550
551 $funcArgs = [
552 // 'page' => 1,
553 // 'per_page' => 10,
554 'platform' => $platform,
555 ];
556 $response = $this->http()->query(
557 'featuredItems',
558 'data{ id, name, slug, price, type, thumbnail, badges }',
559 $funcArgs
560 )->post();
561
562 if( ! is_wp_error( $response ) ) {
563 Database::set_transient( 'featuredItems', $response );
564 }
565
566 return $response;
567 }
568
569 /**
570 * Get Trending Item List
571 * @param WP_REST_Request $request
572 * @return mixed
573 */
574 public function trending_items(WP_REST_Request $request){
575 $platform = $request->get_param( 'platform' );
576 $defaults = Database::get_transient( "trendingItems_{$platform}" );
577 if( $defaults ) {
578 return $defaults;
579 }
580
581 $funcArgs = [
582 // 'page' => 1,
583 // 'per_page' => 10,
584 'platform' => $platform,
585 ];
586 $response = $this->http()->query(
587 'trendingItems',
588 'data{ id, name, slug, price, type, thumbnail, badges }',
589 $funcArgs
590 )->post();
591
592 if( ! is_wp_error( $response ) ) {
593 Database::set_transient( 'trendingItems', $response );
594 }
595
596 return $response;
597 }
598
599 /**
600 * Get Related Item List
601 * @param WP_REST_Request $request
602 * @return mixed
603 */
604 public function related_items(WP_REST_Request $request){
605 $item_id = (int) $request->get_param( 'item_id' );
606 $type = $request->get_param( '_type' );
607
608 if ( empty( $item_id ) ) {
609 return $this->error(
610 'invalid_item_id',
611 __( 'Item ID cannot be empty.', 'templately' ),
612 'related-items',
613 '400'
614 );
615 }
616
617 if ( empty( $type ) ) {
618 return $this->error(
619 'invalid_type',
620 __( 'Type cannot be empty.', 'templately' ),
621 'related-items',
622 '400'
623 );
624 }
625
626 $funcArgs = [
627 'id' => $item_id,
628 'type' => $type,
629 'limit' => 4, // Limit to 3 related items
630 ];
631
632 $response = $this->http()->query(
633 'relatedItems',
634 'id, name, slug, price, type, thumbnail, badges',
635 $funcArgs
636 )->post();
637
638 if( is_wp_error( $response ) ) {
639 return $response;
640 }
641
642 return [
643 'status' => 'success',
644 'data' => $response,
645 ];
646 }
647 }