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

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