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

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