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

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