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

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