PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 1.3.3
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v1.3.3
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 / core / api / class-api.php

class-api.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 1.3.3, at core/api/class-api.php

1,769 lines 62.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;
4
5 use Elementor\Plugin as ElementorPlugin;
6 use Elementor\TemplateLibrary\Source_Local;
7
8 class API {
9 /**
10 * Instance of API
11 * @var API
12 */
13 protected static $_instance = null;
14 /**
15 * Templately API $url
16 * @var string
17 */
18 protected static $url = 'https://app.templately.com/api/plugin';
19 /**
20 * API Trigger Time;
21 * @var integer
22 */
23 protected $trigger_time = null;
24
25 /**
26 * Get the instance of API
27 * @return void
28 */
29 public static function get_instance() {
30 if ( static::$_instance === null ) {
31 static::$_instance = new static;
32 }
33
34 return static::$_instance;
35 }
36
37 private static $query = null;
38
39 /**
40 * Will Invoked When The API Instanciated.
41 */
42 public function __construct() {
43 self::$query = Query::instance();
44 Loader::add_action( 'wp_ajax_get_templately', $this, 'get_templately' );
45 }
46 /**
47 * Checking the template eligibility for import.
48 */
49 public function is_eligible_for_import( $template, $types = [ 'page', 'section' ] ){
50 if( ! \is_plugin_active( 'elementor-pro/elementor-pro.php' ) &&
51 isset( $template['type'] ) &&
52 ! in_array( $template['type'], $types, true ) ) {
53 Helper::send_error( [
54 'message' => 'You have to install/activate <a href="https://wpdeveloper.com/go/elementor" target="_blank">Elementor PRO</a> to import ' . ucwords($template['type']) . ' Blocks.',
55 'type' => 'html'
56 ] );
57 }
58 }
59 /**
60 * Get API Key
61 * @return void
62 */
63 public function get_api() {
64 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
65
66 return ! empty( $api_key ) || is_string( $api_key ) ? $api_key : null;
67 }
68
69 /**
70 * AJAX actions
71 * @return void
72 */
73 public function get_templately() {
74 RequirementInstaller::raise_limits();
75
76 if ( empty( $_POST ) || ! check_admin_referer( 'templately_nonce', 'nonce' ) ) {
77 Helper::send_error( __( 'Something went wrong. Nonce Issue.', 'templately' ) );
78 }
79 $content_type = isset( $_POST['content_type'] ) ? \strtolower( sanitize_text_field( $_POST['content_type'] ) ) : false;
80 if ( ! $content_type ) {
81 Helper::send_error( __( 'Something went wrong.', 'templately' ) );
82 }
83
84 $this->trigger_time = DB::get_option( '_templately_trigger', false );
85
86 switch ( $content_type ) {
87 case 'install_requirements' :
88 $data = $this->install_requirements();
89 break;
90 case 'platform_exists' :
91 $data = $this->platform_exists();
92 break;
93 case 'verification_done' :
94 $data = $this->verification_done();
95 break;
96 case 'get_dependencies' :
97 $data = $this->get_dependencies();
98 break;
99 case 'get_templates' :
100 $data = $this->get_templates();
101 break;
102 case 'blocks' :
103 $data = $this->get_items();
104 break;
105 case 'pages' :
106 $data = $this->get_items( 'pages' );
107 break;
108 case 'packs' :
109 $data = $this->get_items( 'packs' );
110 break;
111 case 'search' :
112 $data = $this->getItemsOrPacks();
113 break;
114 case 'check_dependency' :
115 $data = $this->check_dependency();
116 break;
117 case 'template_content' :
118 $origin = isset( $_POST['source'] ) ? sanitize_text_field( $_POST['source'] ) : 'remote';
119 $data = $this->insert_template( $origin );
120 break;
121 case 'save_template' :
122 $args = $this->ready_args( $_POST );
123 $data = $this->save_template( $args );
124 break;
125 case 'push_to_cloud' :
126 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : null;
127 $file_type = isset( $_POST['file_type'] ) ? sanitize_text_field( $_POST['file_type'] ) : 'elementor';
128 $data = $this->push_to_cloud( $id, $file_type );
129 break;
130 case 'remove_from_cloud' :
131 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : null;
132 $data = $this->remove_from_cloud( $id );
133 break;
134 case 'delete_template' :
135 $data = $this->delete_template();
136 break;
137 case 'clouds_sync' :
138 $data = $this->clouds_sync();
139 break;
140 case 'template_export' :
141 $data = $this->template_export();
142 break;
143 case 'get_item' :
144 $data = $this->get_item();
145 break;
146 case 'get_tags':
147 $data = $this->get_tags();
148 break;
149 case 'set_favourite' :
150 $data = $this->set_favourite();
151 break;
152 case 'set_unfavourite' :
153 $data = $this->set_favourite( true );
154 break;
155 case 'import_to_library' :
156 $data = $this->import_to_library();
157 break;
158 case 'create_page' :
159 $data = $this->create_page();
160 break;
161 case 'get_workspace' :
162 $data = $this->get_workspace();
163 break;
164 case 'edit_workspace' :
165 $data = $this->create_or_edit_workspace( true );
166 break;
167 case 'add_files_to_workspace' :
168 $data = $this->add_files_to_workspace( true );
169 break;
170 case 'delete_workspace' :
171 $data = $this->delete_workspace();
172 break;
173 case 'create_workspace' :
174 $data = $this->create_or_edit_workspace();
175 break;
176 case 'workspace_details' :
177 $data = $this->workspace_details();
178 break;
179 case 'copy_or_move' :
180 $data = $this->copy_or_move();
181 break;
182 case 'cloud_usage_limit' :
183 $data = $this->cloud_usage_limit();
184 break;
185 case 'create_user' :
186 $data = $this->create_user();
187 break;
188 case 'login_user' :
189 $data = $this->login_user();
190 break;
191 case 'logout' :
192 $data = $this->logout();
193 break;
194 }
195 // send success message to api.
196 Helper::send_success( $data );
197 }
198 /**
199 * Collect IP from request.
200 */
201 private static function get_ip() {
202 $ip = '127.0.0.1'; // Local IP
203 if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
204 $ip = $_SERVER['HTTP_CLIENT_IP'];
205 } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
206 $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
207 } else {
208 $ip = $_SERVER['REMOTE_ADDR'];
209 }
210
211 return $ip;
212 }
213
214 /**
215 * This method will responbile for making ready arguments for Save Template
216 *
217 * @param array $posted_data
218 *
219 * @return void
220 */
221 protected function ready_args( $posted_data = array () ) {
222 if ( empty( $posted_data ) ) {
223 return [];
224 }
225
226 $args = [];
227 $args['content'] = isset( $posted_data['content'] ) ? wp_unslash( $posted_data['content'] ) : [];
228 $args['post_id'] = intval( $posted_data['post_id'] );
229 $args['file_type'] = $posted_data['file_type'];
230 $args['source'] = $posted_data['source'];
231 $args['type'] = $posted_data['type'];
232 if ( isset( $posted_data['workspaceActivity'] ) ) {
233 $args['workspaceActivity'] = \json_decode( wp_unslash( $posted_data['workspaceActivity'] ), true );
234 }
235 if ( isset( $posted_data['thumbnail'] ) && ! empty( $posted_data['thumbnail'] ) ) {
236 $args['thumbnail'] = $posted_data['thumbnail'];
237 }
238 $args['title'] = wp_strip_all_tags( $posted_data['title'] );
239
240 return $args;
241 }
242
243 public function save_template( $args = array () ) {
244
245 if ( empty( $args ) ) {
246 // $error_message = $data->get_error_message();
247 Helper::send_error( __( "Something Went Wrong", 'templately' ) );
248 }
249 if ( empty( $args['content'] ) ) {
250 Helper::send_error( __( "There is nothing to save in your template.", 'templately' ) );
251 }
252 if ( ! isset( $args['post_id'] ) ) {
253 Helper::send_error( __( "No ID found to save your template.", 'templately' ) );
254 }
255
256 if ( isset( $args['workspaceActivity'] ) ) {
257 DB::update_user_specific_login_meta( '_templately_cloud_last_activity', $args['workspaceActivity'] );
258 }
259
260 $saved_template = ElementorPlugin::$instance->templates_manager->save_template( $args );
261 $connected = DB::get_user_specific_login_meta( '_templately_connected' );
262 if ( $connected ) {
263 if ( is_array( $saved_template ) && isset( $saved_template['template_id'] ) ) {
264 $pushed_template = $this->push_to_cloud( $saved_template['template_id'], $args['file_type'], $args );
265 }
266
267 return $pushed_template;
268 }
269
270 return $saved_template;
271 }
272 public static function get_items_count(){
273 $templately_item_counts = DB::get_transient( 'templately_item_counts' );
274 if( $templately_item_counts !== false ) {
275 return $templately_item_counts;
276 }
277
278 $defaults = [
279 'elementor' => [
280 'items' => [
281 'total' => '1469',
282 'starter' => '964',
283 'pro' => '415',
284 ],
285 'blocks' => [
286 'total' => '517',
287 'starter' => '358',
288 'pro' => '159',
289 ],
290 'pages' => [
291 'total' => '803',
292 'starter' => '506',
293 'pro' => '297',
294 ],
295 'packs' => [
296 'total' => '149',
297 'starter' => '100',
298 'pro' => '49',
299 ]
300 ],
301 'gutenberg' => [
302 'blocks' => [
303 'total' => '3',
304 'starter' => '3',
305 'pro' => '0',
306 ],
307 'pages' => [
308 'total' => '3',
309 'starter' => '3',
310 'pro' => '0',
311 ],
312 'packs' => [
313 'total' => '3',
314 'starter' => '3',
315 'pro' => '0',
316 ],
317 ]
318 ];
319
320 $data = self::$query->get( '{ getCounts{ key, value } }' );
321 if ( is_wp_error( $data ) ) {
322 DB::set_transient('templately_item_counts', $defaults );
323 return $defaults;
324 }
325 if( isset( $data['data']['getCounts'] ) ) {
326 $new_array = [
327 'items' => [],
328 'blocks' => [],
329 'pages' => [],
330 'packs' => [],
331 ];
332
333 $_new_data = [];
334
335 array_walk( $data['data']['getCounts'], function( $item ) use ( &$new_array, &$_new_data ) {
336 if( in_array( $item['key'] , ['elementor', 'gutenberg'], true ) ) {
337 $values = json_decode($item['value'], true);
338
339 array_walk( $values, function( $_item ) use ( &$new_array ) {
340 $new_key = explode('-', $_item['key']);
341 if( count( $new_key ) === 2 ) {
342 if( isset( $new_array[ $new_key[1] ] )) {
343 $new_array[ $new_key[1] ] = array_merge( $new_array[ $new_key[1] ], [ $new_key[0] => $_item['value'] ] );
344 $temp_array = $new_array[ $new_key[1] ];
345 unset( $temp_array['total'] );
346 $new_array[ $new_key[1] ]['total'] = array_sum( $temp_array );
347 }
348 }
349 });
350
351 $_new_data[ $item['key'] ] = $new_array;
352 }
353 });
354
355 DB::set_transient('templately_item_counts', $_new_data);
356 return $_new_data;
357 }
358 }
359 /**
360 * Get all categories
361 * @return void
362 */
363 public static function get_categories() {
364 $categories = DB::get_transient( 'templately_categories' );
365 if ( $categories !== false ) {
366 return $categories;
367 }
368 $query = '{ groupedCategories { item_categories { id, name, parent }, page_categories{ id, name, parent }, pack_categories{ id, name, parent } } }';
369 $data = self::$query->get( $query );
370 if ( is_wp_error( $data ) ) {
371 return [];
372 }
373 $parentChildListedCategories = $newCategories = $new_category_list = [];
374 if ( ! empty( $data['data']['groupedCategories'] ) ) {
375 foreach ( $data['data']['groupedCategories'] as $singleCatKey => $singleCategories ) {
376 array_walk( $singleCategories, function ( $value, $key ) use ( &$parentChildListedCategories ) {
377 if ( isset( $value['id'] ) && is_null( $value['parent'] ) ) {
378 $parentChildListedCategories[ $value['id'] ] = $value;
379 }
380 } );
381 array_walk( $singleCategories, function ( $value, $key ) use ( &$parentChildListedCategories ) {
382 if ( isset( $value['id'] ) && ! is_null( $value['parent'] ) ) {
383 if ( isset( $parentChildListedCategories[ $value['parent'] ] ) ) {
384 $parentChildListedCategories[ $value['parent'] ]['child'][] = $value;
385 } else {
386 $parentChildListedCategories[ $value['id'] ] = $value;
387 }
388 }
389 } );
390
391 $newCategories[ $singleCatKey ] = $parentChildListedCategories;
392 $parentChildListedCategories = [];
393 }
394
395 foreach ( $newCategories as $k => $v ) {
396 $new_category_list[ $k ] = [];
397 foreach ( $v as $kk => $vv ) {
398 if ( $kk === 'id' || $kk === 'parent' ) {
399 $new_category_list[ $k ][] = intval( $vv );
400 } else {
401 $new_category_list[ $k ][] = $vv;
402 }
403 }
404 }
405 }
406
407 DB::set_transient( 'templately_categories', $new_category_list );
408
409 return $new_category_list;
410 }
411
412 public function get_dependencies() {
413 $dependencies = DB::get_transient( 'templately_dependencies' );
414 if ( $dependencies !== false ) {
415 return $dependencies;
416 }
417 $query = '{ dependencies{ id, name, icon, is_pro, platform } }';
418 $data = self::$query->get( $query );
419 if ( is_wp_error( $data ) ) {
420 Helper::send_error( $data->get_error_code() . ': ' . $data->get_error_message() );
421 }
422 if ( isset( $data['errors'], $data['errors'][0], $data['errors'][0]['message'] ) ) {
423 Helper::send_error( $data['errors'][0]['message'] );
424 }
425 if ( isset( $data['data'], $data['data']['dependencies'] ) ) {
426 DB::set_transient( 'templately_dependencies', $data['data']['dependencies'] );
427
428 return $data['data']['dependencies'];
429 }
430
431 Helper::send_error( __( 'Something went wrong with fetching dependencies data.', 'templately' ) );
432 }
433
434 /**
435 * Get all items for every type
436 *
437 * @param string $type
438 *
439 * @return void
440 */
441 private function get_items( $type = 'items' ) {
442 $pagenum = isset( $_POST['pagenum'] ) ? intval( $_POST['pagenum'] ) : 1;
443 $platform = isset( $_POST['platform'] ) ? strtolower( $_POST['platform'] ) : 'elementor';
444 $per_page = isset( $_POST['per_page'] ) ? intval( $_POST['per_page'] ) : 48;
445 $package_type = isset( $_POST['plan_type'] ) ? trim( strtolower( $_POST['plan_type'] ) ) : false;
446 $platform = $platform === 'templately' ? 'elementor' : $platform;
447 $category_id = isset( $_POST['category_id'] ) ? intval( $_POST['category_id'] ) : false;
448 $dependencies = isset( $_POST['dependencies'] ) ? trim( $_POST['dependencies'] ) : false;
449 $tag_id = isset( $_POST['tag_id'] ) ? json_encode( trim( $_POST['tag_id'] ) ) : false;
450
451
452 switch ( $package_type ) {
453 case "all":
454 $plan_type = 1;
455 break;
456 case "starter":
457 $plan_type = 2;
458 break;
459 case "pro":
460 $plan_type = 3;
461 break;
462 default:
463 $plan_type = 1;
464 break;
465 }
466
467 $query = sprintf(
468 '{ %s( %s page: %s, per_page: %s, platform: "%s", %s%s%s ) { total_page, current_page, data { id, name, rating, type, slug, favourite_count, %s thumbnail, price, author{ display_name, name, joined }, category{ id, name } } } }',
469 $type,
470 ( $category_id ) ? 'category_id: ' . $category_id . ',' : '',
471 $pagenum,
472 $per_page,
473 esc_attr( $platform ),
474 ( $plan_type === 1 || $plan_type === false ) ? '' : "plan_type: $plan_type",
475 ( $dependencies == false || $dependencies == '' ) ? '' : 'dependencies: "'. $dependencies .'"',
476 ( $tag_id === false ) ? '' : "tag_id: $tag_id",
477 ( $type === 'packs' ) ? '' : 'dependencies{ id, name, slug, icon, is_pro, link, plugin_file, plugin_original_slug },'
478 );
479
480 $data = self::$query->get( $query );
481 if ( is_wp_error( $data ) ) {
482 Helper::send_error( $data->get_error_code() . ': ' . $data->get_error_message() );
483 }
484
485 if ( isset( $data['errors'], $data['errors'][0], $data['errors'][0]['message'] ) ) {
486 Helper::send_error( $data['errors'][0]['message'] );
487 }
488
489 if ( isset( $data['data'], $data['data'][ $type ] ) ) {
490 return $data['data'][ $type ];
491 }
492 }
493
494 /**
495 * Get Items or Packs for Search
496 *
497 * @param string $type
498 *
499 * @return void
500 */
501 private function getItemsOrPacks() {
502 $pagenum = isset( $_POST['pagenum'] ) ? intval( $_POST['pagenum'] ) : 1;
503 $platform = isset( $_POST['platform'] ) ? strtolower( $_POST['platform'] ) : 'elementor';
504 $platform = $platform === 'templately' ? 'elementor' : $platform;
505 $search = isset( $_POST['search'] ) ? strtolower( $_POST['search'] ) : '';
506
507 $category_id = isset( $_POST['category_id'] ) ? intval( $_POST['category_id'] ) : false;
508 $dependencies = isset( $_POST['dependencies'] ) ? json_encode( trim( $_POST['dependencies'] ) ) : false;
509 $tag_id = isset( $_POST['tag_id'] ) ? json_encode( trim( $_POST['tag_id'] ) ) : false;
510 $types = isset( $_POST['query_type'] ) ? trim( $_POST['query_type'] ) : 'block';
511
512 $query = sprintf(
513 '{ getItemsAndPacks( page: %s, search: "%s", per_page: 48, platform: "%s", query_type: "%s", %s%s%s ) { data { id, name, rating, type, slug, favourite_count, thumbnail, price, author{ display_name, name, joined }, category{ id, name } } } }',
514 $pagenum,
515 $search,
516 esc_attr( $platform ),
517 $types,
518 $category_id !== false && $category_id !== 'all' ? "category_id: $category_id," : '',
519 $dependencies !== false ? "dependencies: $dependencies," : '',
520 ! empty( $tag_id ) ? "tag_id: $tag_id" : ''
521 );
522
523 $data = self::$query->get( $query );
524 if ( is_wp_error( $data ) ) {
525 Helper::send_error( $data->get_error_code() . ': ' . $data->get_error_message() );
526 }
527 if ( isset( $data['data'], $data['data']['getItemsAndPacks'] ) ) {
528 $this->data = [];
529 if( isset( $data['data']['getItemsAndPacks']['data'] ) && ! empty( $data['data']['getItemsAndPacks']['data'] ) ) {
530 array_walk( $data['data']['getItemsAndPacks']['data'], function( $item ) {
531 $item['id'] = intval(\str_replace( " {$item['type']}", '', $item['id']));
532 $this->data[] = $item;
533 });
534 }
535 return $this->data;
536 }
537 }
538
539 /**
540 * Get template content
541 *
542 * @param string $origin
543 * @param integer $id
544 *
545 * @return void
546 */
547 public static function get_template_content( $args, $import = false ) {
548 if ( isset( $args['item_id'] ) && ! empty( $args['item_id'] ) ) {
549 $id = $args['item_id'];
550 }
551 if ( isset( $args['origin'] ) && ! empty( $args['origin'] ) ) {
552 $origin = $args['origin'];
553 } else {
554 $origin = 'remote';
555 }
556
557 if ( isset( $args['file_type'] ) && ! empty( $args['file_type'] ) ) {
558 $item_type = $args['file_type'];
559 }
560
561 if ( is_null( $id ) ) {
562 Helper::send_error( __( 'Something went wrong.', 'templately' ) );
563 }
564 /**
565 * Item will come from remote server.
566 */
567 if ( $origin === 'remote' || $origin === 'cloud' ) {
568 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
569 if ( ! $api_key ) {
570 Helper::send_error( __( 'Activate Your Account!', 'templately' ) );
571 }
572 $query = '{ itemContent( id: ' . $id . ', api_key: "' . $api_key . '" ){ status, message, data } }';
573 if ( $origin === 'cloud' ) {
574 $query = '{ myCloudInsert( file_id: ' . $id . ', api_key: "' . $api_key . '", file_type: "' . $item_type . '" ) }';
575 }
576 $data = self::$query->get( $query );
577 if ( is_wp_error( $data ) ) {
578 Helper::send_error( $data->get_error_code() . ': ' . $data->get_error_message() );
579 }
580 if ( $origin === 'remote' ) {
581 if ( isset( $data['data'], $data['data']['itemContent'], $data['data']['itemContent']['status'] ) ) {
582 if ( $data['data']['itemContent']['status'] == 'error' ) {
583 Helper::send_error( $data['data']['itemContent']['message'] );
584 }
585 if ( $data['data']['itemContent']['status'] == 'required-pro-acc' ) {
586 Helper::send_error( array (
587 'errorCode' => 'required_pro',
588 'message' => $data['data']['itemContent']['message'],
589 ) );
590 }
591 }
592
593 if ( isset( $data['data'], $data['data']['itemContent'], $data['data']['itemContent']['data'] ) ) {
594 $data = json_decode( $data['data']['itemContent']['data'], true );
595 if ( isset( $args['is_editor'] ) && $args['is_editor'] == true ) {
596 return $data;
597 } else {
598 // Helper::send_error( array(
599 // 'errorCode' => 'required_elementor_pro',
600 // 'message' => __( "Type: {$data['type']} needs Elementor Pro to import.", 'templately' ),
601 // ) );
602 }
603
604 return $data;
605 }
606 }
607 if ( $origin === 'cloud' ) {
608 $data = json_decode( $data['data']['myCloudInsert'], true );
609
610 return $data;
611 }
612 if ( $import ) {
613 return null;
614 }
615 Helper::send_error( __( 'Something went wrong!', 'templately' ) );
616 }
617 /**
618 * Item will come from local WP Installation.
619 */
620 if ( $origin === 'local' ) {
621 $data = self::$query->getFromLibrary( $id );
622 if ( isset( $data['content'] ) ) {
623 return $data;
624 }
625 }
626 Helper::send_error( __( 'Something went wrong!', 'templately' ) );
627 }
628
629 /**
630 * This Method is responsible for Inserting Template
631 *
632 * @param string $origin
633 *
634 * @return void
635 */
636 public function insert_template( $origin = 'remote' ) {
637 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : null;
638 if ( is_null( $id ) ) {
639 Helper::send_error( __( 'Templates ID Not Found.', 'templately' ) );
640 }
641 $is_editor = isset( $_POST['is_editor'] ) ? $_POST['is_editor'] === "true" : null;
642 $default_args = array (
643 'editor_post_id' => false,
644 'item_id' => $id,
645 'origin' => $origin
646 );
647 if ( ! is_null( $is_editor ) ) {
648 $default_args['is_editor'] = $is_editor;
649 }
650 if ( isset( $_POST['file_type'] ) && ! empty( $_POST['file_type'] ) ) {
651 $default_args['file_type'] = sanitize_text_field( $_POST['file_type'] );
652 }
653 if ( isset( $default_args['file_type'] ) && $default_args['file_type'] === 'gutenberg' ) {
654 $template = self::get_template_content( $default_args );
655 if( is_string( $template )) {
656 $template = \json_decode( \wp_unslash( $template ), true );
657 }
658 }
659 if ( isset( $default_args['file_type'] ) && $default_args['file_type'] === 'elementor' ) {
660 $importer = new Importer;
661 $template = $importer->get_data( $default_args );
662 }
663
664 return isset( $template['content'] ) ? $template : Helper::send_error( __( 'There is no Template Content to be inserted.', 'templately' ) );
665 }
666
667 private function cloud_push( $name, $data, $item_type = 'elementor', $args = array () ) {
668 if ( empty( $name ) ) {
669 Helper::send_error( __( "Name can't be empty.", 'templately' ) );
670 }
671 if ( $item_type === 'elementor' ) {
672 if ( empty( $data ) || ! is_array( $data ) ) {
673 Helper::send_error( __( "Data has to be an array.", 'templately' ) );
674 }
675 if ( ! isset( $data['content'] ) || ( isset( $data['content'] ) && empty( $data['content'] ) ) ) {
676 Helper::send_error( __( "There is no data to push.", 'templately' ) );
677 }
678 }
679
680 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
681
682 if ( empty( $api_key ) ) {
683 Helper::send_error( __( "API Key is not found, try to re-login.", 'templately' ) );
684 }
685 // if( $item_type === 'elementor' ) {
686 // }
687 $data = wp_slash( \json_encode(
688 $data, JSON_UNESCAPED_LINE_TERMINATORS | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE
689 ) );
690
691 $response = self::$query->push( $name, $data, $api_key, $item_type, $args );
692 if ( is_wp_error( $response ) ) {
693 Helper::send_error( $response->get_error_code() . ': ' . $response->get_error_message() );
694 }
695 if ( isset( $response['errors'] ) ) {
696 Helper::send_error( $response['errors'][0]['message'] );
697 }
698
699 $cloud_item = json_decode( $response['data']['pushToMyCloud']['data'] );
700 if ( isset( $cloud_item->myCloud_item ) ) {
701 $cloud_map = DB::get_option( '_templately_cloud_map', [] );
702 $id = $args['id'];
703
704 if ( ! isset( $cloud_map[ $id ] ) ) {
705 $cloud_map[] = array (
706 'template_id' => $id,
707 'cloud_item_id' => intval( $cloud_item->myCloud_item ),
708 );
709 }
710 DB::update_option( '_templately_cloud_map', $cloud_map );
711 }
712
713 return $response['data']['pushToMyCloud'];
714 }
715
716 /**
717 * Push to Cloud
718 *
719 * @param integer $id
720 *
721 * @return void
722 */
723 private function push_to_cloud( $id = null, $item_type = 'elementor', $args = [] ) {
724 if ( isset( $_POST['workspace_id'] ) ) {
725 $args['workspace_id'] = intval( $_POST['workspace_id'] );
726 }
727
728 if ( $item_type === 'elementor' ) {
729 if ( $id === null ) {
730 Helper::send_error( __( 'ID not found to push the item in cloud.', 'templately' ) );
731 }
732 $data = self::$query->getFromLibrary( $id );
733
734 if ( ! empty( $data ) && isset( $data['content'] ) ) {
735 $name = \get_the_title( $id );
736 $args['id'] = $id;
737
738 return $this->cloud_push( $name, $data, $item_type, $args );
739 }
740 }
741
742 if ( $item_type === 'gutenberg' ) {
743 $name = isset( $_POST['title'] ) && ! empty( $_POST['title'] ) ? sanitize_text_field( $_POST['title'] ) : '';
744 $data = isset( $_POST['data'] ) && ! empty( $_POST['data'] ) ? $_POST['data'] : '';
745
746 return $this->cloud_push( $name, $data, 'gutenberg', $args );
747 }
748
749 Helper::send_error( __( 'Something went wrong', 'templately' ) );
750 }
751
752 /**
753 * This method is responsible for Removing item from Cloud
754 *
755 * @param integer $id
756 *
757 * @return void
758 */
759 private function remove_from_cloud( $id = null ) {
760 if ( is_null( $id ) ) {
761 Helper::send_error( __( 'ID not found to remove Template from Cloud.', 'templately' ) );
762 }
763 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
764 if ( ! $api_key ) {
765 Helper::send_error( __( 'You have no rights to delete file from Cloud. You have to logged in first', 'templately' ) );
766 }
767
768 $from = isset( $_POST['remove_from'] ) && ! empty( $_POST['remove_from'] ) ? trim( $_POST['remove_from'] ) : 'cloud';
769 $response = self::$query->remove_cloud_item( $id, $api_key, $from );
770
771 if ( is_wp_error( $response ) ) {
772 Helper::send_error( $response->get_error_code() . ': ' . $response->get_error_message() );
773 }
774
775 if ( $from === 'cloud' ) {
776 $response = $response['data']['removeFromMyCloud'];
777 } else {
778 $response = $response['data']['deleteWorkspaceFile'];
779 }
780
781 if ( isset( $response['status'] ) && $response['status'] != 'success' ) {
782 Helper::send_error( __( 'Something went wrong.', 'templately' ) );
783 }
784
785 if ( $from === 'cloud' ) {
786 $cloud_map = DB::get_option( '_templately_cloud_map', [] );
787 if ( ! empty( $cloud_map ) ) {
788 $new_cloud_map = array_filter( $cloud_map, function ( $value ) use ( $id ) {
789 return $value['cloud_item_id'] != $id;
790 } );
791 DB::update_option( '_templately_cloud_map', array_values( $new_cloud_map ) );
792 }
793
794 $users_data = DB::get_user_specific_login_meta( '_templately_connect_data', [] );
795 if ( array_key_exists( 'clouds', $users_data ) && array_key_exists( 'files', $users_data['clouds'] ) ) {
796 $new_users_data = array_filter( $users_data['clouds']['files'], function ( $value ) use ( $id ) {
797 return $value['id'] != $id;
798 } );
799 $users_data['clouds']['files'] = array_values( $new_users_data );
800 DB::update_user_specific_login_meta( '_templately_connect_data', $users_data );
801 }
802
803 return $id;
804 }
805
806 return $response;
807 }
808
809 /**
810 * Template Export
811 *
812 * @param integer $id
813 *
814 * @return void
815 */
816 public function template_export( $id = null ) {
817 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : null;
818 if ( $id === null ) {
819 Helper::send_error( __( 'Something went wrong.', 'templately' ) );
820 }
821
822 return ElementorPlugin::$instance->templates_manager->export_template( array (
823 'source' => 'local',
824 'template_id' => $id
825 ) );
826 }
827
828 /**
829 * Check Dependency for Items
830 * @return void
831 */
832 protected function check_dependency() {
833 $dependencies = json_decode( wp_unslash( $_POST['dependency'] ) );
834 $not_active_plugin_list = [];
835 $all_plugins = Helper::get_plugins();
836 $platform = isset( $_POST['platform'] ) ? trim( $_POST['platform'] ) : 'elementor';
837 if ( ! \is_plugin_active( 'elementor/elementor.php' ) && $platform === 'elementor' ) {
838 $elementor_plugin = new \stdClass();
839 $elementor_plugin->name = __( 'Elementor', 'templately' );
840 $elementor_plugin->plugin_file = 'elementor/elementor.php';
841 $elementor_plugin->plugin_original_slug = 'elementor';
842 $elementor_plugin->slug = 'elementor';
843 $elementor_plugin->is_pro = false;
844 $not_active_plugin_list[] = $elementor_plugin;
845 }
846
847 if ( ! empty( $dependencies ) && is_array( $dependencies ) ) {
848 foreach ( $dependencies as $dependency ) {
849 if ( is_null( $dependency->plugin_file ) ) {
850 continue;
851 }
852 if ( ! \is_plugin_active( $dependency->plugin_file ) ) {
853 if ( $dependency->is_pro ) {
854 if ( isset( $all_plugins[ $dependency->plugin_file ] ) ) {
855 unset( $dependency->is_pro );
856 $dependency->message = __( 'You have the plugin installed.', 'templately' );
857 $not_active_plugin_list[] = $dependency;
858 } else {
859 $not_active_plugin_list[] = $dependency;
860 }
861 } else {
862 $not_active_plugin_list[] = $dependency;
863 }
864 }
865 }
866 }
867 Helper::send_success( array (
868 'dependency' => $not_active_plugin_list
869 ) );
870 }
871
872 /**
873 * This Method is responsible for Get Item from Server.
874 * @return void
875 */
876 protected function get_item() {
877 $slug = isset( $_POST['slug'] ) ? trim( sanitize_text_field( $_POST['slug'] ) ) : false;
878 if ( ! $slug ) {
879 Helper::send_error( __( 'Something went wrong.', 'templately' ) );
880 }
881 $type = isset( $_POST['type'] ) ? trim( sanitize_text_field( $_POST['type'] ) ) : 'block';
882
883 $query = '';
884 if ( $type === 'block' ) {
885 $query = '{ items( slug: "' . $slug . '" ) { data { slug, rating, type, downloads, dependencies{ id, icon, name, slug, is_pro, link, plugin_file, plugin_original_slug }, live_url, name, id, retina_ready, documentation_ready, features, favourite_count, thumbnail, price, pack{ name, slug }, author{ display_name, name, profile_photo, joined }, tags{ name, id }, category{ name, id } } } }';
886 }
887 if ( $type === 'pack' ) {
888 $query = '{
889 packs( slug: "' . $slug . '" ) {
890 data { name, id, slug, rating, downloads, favourite_count, thumbnail, price, author{ display_name, name, profile_photo, joined }, tags{ name, id }, category{ name, id },
891 items {
892 slug, type, live_url, name, downloads, rating, id, dependencies{ id, icon, name, slug, is_pro, link, plugin_file, plugin_original_slug }, retina_ready, documentation_ready, features, favourite_count, thumbnail, price, pack{ name, slug }, author{ display_name, name, profile_photo, joined }, tags{ name, id }, category{ name, id } } }
893 }
894 }';
895 }
896
897 if ( $type === 'page' ) {
898 $query = '{ pages( slug: "' . $slug . '" ) { data { slug, type, rating, downloads, dependencies{ id, icon, name, slug, is_pro, link, plugin_file, plugin_original_slug }, live_url, name, id, retina_ready, documentation_ready, features, favourite_count, thumbnail, price, pack{ name, slug }, author{ display_name, name, joined, profile_photo }, tags{ name, id }, category{ name, id } } } }';
899 }
900
901 $data = self::$query->get( $query );
902 if ( \is_wp_error( $data ) ) {
903 Helper::send_error( $data->get_error_code() . ': ' . $data->get_error_message() );
904 }
905 if ( $type === 'block' ) {
906 if ( isset( $data['data'], $data['data']['items'] ) ) {
907 return $data['data']['items']['data'];
908 }
909 }
910 if ( $type === 'page' ) {
911 if ( isset( $data['data'], $data['data']['pages'] ) ) {
912 return $data['data']['pages']['data'];
913 }
914 }
915 if ( $type === 'pack' ) {
916 if ( isset( $data['data'], $data['data']['packs'] ) ) {
917 return $data['data']['packs']['data'];
918 }
919 }
920 Helper::send_error( __( 'Something went wrong!', 'templately' ) );
921 }
922
923 /**
924 * This Method is responsible for Get Items tag from Server.
925 * @return void
926 */
927 protected function get_tags() {
928 $tags = DB::get_transient( 'templately_tags' );
929 if ( $tags !== false ) {
930 return $tags;
931 }
932
933 $query = '{ tags { id, name } }';
934 $data = self::$query->get( $query );
935
936 if ( \is_wp_error( $data ) ) {
937 Helper::send_error( $data->get_error_code() . ': ' . $data->get_error_message() );
938 }
939
940 if ( isset( $data['data'], $data['data']['tags'] ) ) {
941 DB::set_transient( 'templately_tags', $data['data']['tags'] );
942
943 return $data['data']['tags'];
944 }
945
946 Helper::send_error( __( 'Something went wrong with fetching dependencies data.', 'templately' ) );
947 }
948
949
950 /**
951 * This Methods Helps to Import Template in Library
952 * @return void
953 */
954 public function import_to_library() {
955 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : false;
956 if ( ! $id ) {
957 Helper::send_error( __( 'Something went wrong!', 'templately' ) );
958 }
959 $title = isset( $_POST['title'] ) ? trim( sanitize_text_field( $_POST['title'] ) ) : '';
960
961 $importer = new Importer;
962 $template = $importer->get_data( array (
963 'editor_post_id' => false,
964 'item_id' => $id
965 ) );
966 /**
967 * Checking the template eligibility for import.
968 */
969 $this->is_eligible_for_import( $template );
970
971 $imported_template = $importer->import_templately( $template );
972 if ( is_wp_error( $imported_template ) ) {
973 Helper::send_error( 'Error Code: ' . $imported_template->get_error_code() . ', Message: ' . $imported_template->get_error_message() );
974 }
975 if ( ! is_wp_error( $imported_template ) && is_array( $imported_template ) ) {
976 return array (
977 'post_id' => $imported_template['template_id'],
978 'edit_link' => get_edit_post_link( $imported_template['template_id'], 'internal' ),
979 'elementor_edit_link' => ElementorPlugin::$instance->documents->get( $imported_template['template_id'] )->get_edit_url(),
980 'visit' => $imported_template['url']
981 );
982 }
983 Helper::send_error( __( 'Something went wrong with Import!', 'templately' ) );
984 }
985
986 /**
987 * This Method is Responsible for Create A Page From a Template.
988 * also its used to Import Template
989 *
990 * @param boolean $is_import
991 *
992 * @return void
993 */
994 public function create_page() {
995 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : false;
996 if ( ! $id ) {
997 Helper::send_error( __( 'Something went wrong!', 'templately' ) );
998 }
999 $title = isset( $_POST['title'] ) ? trim( sanitize_text_field( $_POST['title'] ) ) : '';
1000 $platform = isset( $_POST['platform'] ) ? trim( $_POST['platform'] ) : 'elementor';
1001 if ( $platform === 'elementor' ) {
1002 $importer = new Importer;
1003 $template_data = $importer->get_data( array (
1004 'editor_post_id' => false,
1005 'item_id' => $id
1006 ) );
1007 if ( ! empty( $title ) ) {
1008 $template_data['page_title'] = $title;
1009 }
1010 /**
1011 * Checking the template eligibility for import.
1012 */
1013 $this->is_eligible_for_import( $template_data );
1014 $inserted_ID = $importer->create_page( $template_data );
1015 }
1016
1017 if ( $platform === 'gutenberg' ) {
1018 $inserted_ID = self::get_template_content( array (
1019 'origin' => 'remote',
1020 'item_id' => $id,
1021 ) );
1022
1023 if ( isset( $inserted_ID['content'] ) ) {
1024 $inserted_ID = wp_insert_post( array (
1025 'post_status' => 'draft',
1026 'post_type' => 'page',
1027 'post_title' => $title,
1028 'post_content' => $inserted_ID['content'],
1029 ) );
1030 }
1031 }
1032
1033 if ( is_wp_error( $inserted_ID ) ) {
1034 Helper::send_error( 'Error Code: ' . $inserted_ID->get_error_code() . ', Message: ' . $inserted_ID->get_error_message() );
1035 }
1036
1037 if ( $inserted_ID && ! is_wp_error( $inserted_ID ) ) {
1038 return array (
1039 'post_id' => $inserted_ID,
1040 'edit_link' => get_edit_post_link( $inserted_ID, 'internal' ),
1041 'elementor_edit_link' => $platform === 'elementor' ? ElementorPlugin::$instance->documents->get( $inserted_ID )->get_edit_url() : false,
1042 'visit' => get_permalink( $inserted_ID )
1043 );
1044 }
1045
1046 Helper::send_error( __( 'Something went wrong!', 'templately' ) );
1047 }
1048
1049 /**
1050 * This Method is responsible for Check User Exists or Not and Make them Logged in to Server
1051 * to collect profiles data and the API Key.
1052 *
1053 * @param boolean $login
1054 *
1055 * @return void
1056 */
1057 public function login_user() {
1058 $errors = [];
1059
1060 $global_signin = isset( $_POST['global_signin'] ) ? filter_var( $_POST['global_signin'], FILTER_VALIDATE_BOOLEAN) : false;
1061 if( $global_signin ) {
1062 Login::set_user_login_choice( get_current_user_id(), $global_signin );
1063 } else {
1064 Login::set_link_my_account();
1065 }
1066
1067 $withApi = isset( $_POST['withApi'] ) ? trim( $_POST['withApi'] ) === 'true' : false;
1068
1069 if ( $withApi ) {
1070 $api_key = sanitize_text_field( trim( $_POST['api_key'] ) );
1071 if ( empty( $api_key ) ) {
1072 $errors['api_key'] = __( 'API Key Field Cannot be Empty.', 'templately' );
1073 }
1074 $query = \sprintf(
1075 'mutation{ connectWithApiKey( api_key : "%s", site_url : "%s", ip: "%s" ){ status, message, user { id, first_name, last_name, name, profile_photo, is_verified, api_key, my_cloud { limit, usages, name, last_pushed, files{ data { id, name, thumbnail, medium_thumbnail, preview }, current_page, total_page }}, email, favourites { id, type }, joined, plan, plan_expire_at }}}',
1076 $api_key,
1077 \home_url( '/' ),
1078 self::get_ip()
1079 );
1080 } else {
1081 $email = sanitize_text_field( trim( $_POST['email'] ) );
1082 if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
1083 $errors['email'] = __( 'Make sure you have given a valid email address.', 'templately' );
1084 }
1085 $password = sanitize_text_field( trim( $_POST['password'] ) );
1086 if ( empty( $password ) ) {
1087 $errors['password'] = __( 'Password Field Cannot be Empty.', 'templately' );
1088 }
1089 $query = \sprintf(
1090 'mutation{ connect( email : "%s", password : "%s", site_url : "%s", ip: "%s" ){ status, message, user { id, first_name, last_name, name, plan, plan_expire_at, profile_photo, is_verified, api_key, email, favourites { id, type }, joined, my_cloud { limit, usages, name, last_pushed, files{ data { id, name }, current_page, total_page }}}}}',
1091 $email,
1092 $password,
1093 \home_url( '/' ),
1094 self::get_ip()
1095 );
1096 }
1097
1098
1099 $api_response = self::$query->get( $query );
1100
1101 if ( is_wp_error( $api_response ) ) {
1102 Helper::send_error( $api_response->get_error_code() . ': ' . $api_response->get_error_message() );
1103 }
1104 if ( isset( $api_response['errors'] ) ) {
1105 print_r($api_response['errors']);
1106 Helper::send_error( __( 'Something went wrong. Maybe Invalid Credentials.', 'templately' ) );
1107 }
1108 if ( $withApi ) {
1109 $api_response = $api_response['data']['connectWithApiKey'];
1110 if ( isset( $api_response['status'] ) && $api_response['status'] !== 'success' ) {
1111 Helper::send_error( $api_response['message'] );
1112 }
1113
1114 return $this->user_data( $api_response );
1115 } else {
1116 $api_response = $api_response['data']['connect'];
1117 if ( isset( $api_response['status'] ) && $api_response['status'] !== 'success' ) {
1118 Helper::send_error( $api_response['message'] );
1119 }
1120
1121 return $this->user_data( $api_response );
1122 }
1123 Helper::send_error( __( 'Something went wrong', 'templately' ) );
1124 }
1125
1126 public function create_user() {
1127 $errors = [];
1128 $first_name = sanitize_text_field( trim( $_POST['first_name'] ) );
1129 $last_name = sanitize_text_field( trim( $_POST['last_name'] ) );
1130
1131 if( Login::already_signedin_globally() ) {
1132 Login::set_link_my_account();
1133 } else {
1134 Login::set_user_login_choice( get_current_user_id(), true );
1135 }
1136
1137 if ( empty( $first_name ) ) {
1138 $errors['first_name'] = __( 'First Name Can\'t be Empty.', 'templately' );
1139 }
1140 if ( empty( $last_name ) ) {
1141 $errors['last_name'] = __( 'Last Name Can\'t be Empty.', 'templately' );
1142 }
1143 $email = sanitize_text_field( trim( $_POST['email'] ) );
1144 if ( empty( $email ) ) {
1145 $errors['email'] = __( 'Email Field Can\'t be Empty.', 'templately' );
1146 }
1147 if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
1148 $errors['email'] = __( 'Make sure you have given a valid email address.', 'templately' );
1149 }
1150 $password = sanitize_text_field( trim( $_POST['password'] ) );
1151 if ( empty( $password ) ) {
1152 $errors['password'] = __( 'Password Field Can\'t be Empty.', 'templately' );
1153 }
1154 if ( ! empty( $errors ) ) {
1155 Helper::send_error( $errors );
1156 }
1157
1158 $query = \sprintf(
1159 'mutation{ createUser( first_name : "%s", last_name : "%s", email : "%s", password : "%s", site_url : "%s", ip: "%s" ){ status, message, user { id, name, first_name, last_name, plan, plan_expire_at, profile_photo, api_key, email, joined, is_verified}}}',
1160 $first_name,
1161 $last_name,
1162 $email,
1163 $password,
1164 \home_url( '/' ),
1165 self::get_ip()
1166 );
1167 $api_response = self::$query->get( $query );
1168 if ( is_wp_error( $api_response ) ) {
1169 Helper::send_error( $api_response->get_error_code() . ': ' . $api_response->get_error_message() );
1170 }
1171
1172 if ( isset( $api_response['errors'] ) ) {
1173 if ( isset( $api_response['errors'][0]['extensions'] ) && isset( $api_response['errors'][0]['extensions']['validation'] ) ) {
1174 Helper::send_error( $api_response['errors'][0]['extensions']['validation'] );
1175 }
1176 Helper::send_error( 'Something went wrong.' );
1177 }
1178
1179 $api_response = $api_response['data']['createUser'];
1180 if ( isset( $api_response['status'] ) && $api_response['status'] !== 'success' ) {
1181 Helper::send_error( $api_response['message'] );
1182 }
1183
1184 return $this->user_data( $api_response, true );
1185 }
1186
1187 /**
1188 * Helper Function for checkNVerify methods
1189 *
1190 * @param array $api_response
1191 *
1192 * @return void
1193 */
1194 protected function user_data( $api_response, $create_user = false, $raw = false ) {
1195 if ( isset( $api_response ) && ! is_null( $api_response ) ) {
1196 $user_data = $api_response;
1197 $userState = [];
1198 if ( isset( $user_data['user'], $user_data['user']['favourites'] ) ) {
1199
1200 $favourites_count = count( $user_data['user']['favourites'] );
1201
1202 $favourites = [
1203 'item' => [],
1204 'pack' => []
1205 ];
1206 array_walk( $user_data['user']['favourites'], function ( $value ) use ( &$favourites ) {
1207 if ( ! is_null( $value ) ) {
1208 if ( ! in_array( $value['id'], isset( $favourites[ $value['type'] ] ) ? $favourites[ $value['type'] ] : [] ) ) {
1209 $favourites[ $value['type'] ][] = $value['id'];
1210 }
1211 }
1212 } );
1213 $userState['favourites'] = $favourites;
1214 $userState['favourites_items'] = $favourites_count;
1215 }
1216
1217 if ( isset( $user_data['user'], $user_data['user']['id'] ) ) {
1218 $userState['id'] = intval( $user_data['user']['id'] );
1219 }
1220 if ( isset( $user_data['user'], $user_data['user']['first_name'] ) ) {
1221 $userState['first_name'] = sanitize_text_field( $user_data['user']['first_name'] );
1222 }
1223 if ( isset( $user_data['user'], $user_data['user']['profile_photo'] ) ) {
1224 $userState['profile_photo'] = sanitize_text_field( $user_data['user']['profile_photo'] );
1225 }
1226 if ( isset( $user_data['user'], $user_data['user']['last_name'] ) ) {
1227 $userState['last_name'] = sanitize_text_field( $user_data['user']['last_name'] );
1228 }
1229 if ( isset( $user_data['user'], $user_data['user']['name'] ) ) {
1230 $userState['name'] = sanitize_text_field( $user_data['user']['name'] );
1231 }
1232 if ( isset( $user_data['user'], $user_data['user']['email'] ) ) {
1233 $userState['email'] = sanitize_text_field( $user_data['user']['email'] );
1234 }
1235 if ( isset( $user_data['user'], $user_data['user']['plan'] ) ) {
1236 $userState['plan'] = sanitize_text_field( $user_data['user']['plan'] );
1237 }
1238 if ( isset( $user_data['user'], $user_data['user']['plan_expire_at'] ) ) {
1239 $userState['plan_expire_at'] = sanitize_text_field( $user_data['user']['plan_expire_at'] );
1240 }
1241 if ( isset( $user_data['user'], $user_data['user']['joined'] ) ) {
1242 $userState['joined'] = sanitize_text_field( $user_data['user']['joined'] );
1243 }
1244 if ( isset( $user_data['user'], $user_data['user']['is_verified'] ) ) {
1245 $userState['is_verified'] = sanitize_text_field( $user_data['user']['is_verified'] );
1246 }
1247 if ( isset( $user_data['status'] ) ) {
1248 $userState['status'] = sanitize_text_field( $user_data['status'] );
1249 }
1250 $api_key = '';
1251 if ( isset( $user_data['user'], $user_data['user']['api_key'] ) ) {
1252 $api_key = sanitize_text_field( $user_data['user']['api_key'] );
1253 }
1254 if ( isset( $user_data['user'], $user_data['user']['my_cloud'] ) ) {
1255 $user_clouds['limit'] = $user_data['user']['my_cloud']['limit'];
1256 $user_clouds['usages'] = $user_data['user']['my_cloud']['usages'];
1257 $user_clouds['name'] = $user_data['user']['my_cloud']['name'];
1258 $user_cloud_files = [];
1259
1260 if ( isset( $user_data['user']['my_cloud']['last_pushed'] ) ) {
1261 DB::update_user_specific_login_meta( '_templately_cloud_last_activity', $user_data['user']['my_cloud']['last_pushed'] );
1262 }
1263
1264 array_walk( $user_data['user']['my_cloud']['files'], function ( $value ) use ( &$user_cloud_files ) {
1265 //TODO: mycloud
1266 } );
1267 $user_clouds['files'] = $user_cloud_files;
1268 $userState['clouds'] = $user_clouds;
1269 }
1270 if ( empty( $api_key ) ) {
1271 Helper::send_error( __( 'API Key is not found', 'templately' ) );
1272 }
1273
1274 if ( $create_user ) {
1275 $userState['is_verified'] = false;
1276 $userState['is_global_login'] = Login::get_global_signed_in_by_user( get_current_user_id() );
1277 }
1278
1279 DB::update_user_specific_login_meta( '_templately_api_key', $api_key );
1280 DB::update_user_specific_login_meta( '_templately_connected', true );
1281 DB::update_user_specific_login_meta( '_templately_connect_data', $userState );
1282
1283 if ( $raw ) {
1284 return $userState;
1285 }
1286 Helper::send_success( array (
1287 'message' => __( 'Successfully Connected', 'templately' ),
1288 'admin' => $userState
1289 ) );
1290 }
1291 }
1292
1293 /**
1294 * This method delete templates from library.
1295 * @return void
1296 */
1297 protected function delete_template() {
1298 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : false;
1299 if ( ! $id ) {
1300 Helper::send_error( __( 'Template ID is not found to delete', 'templately' ) );
1301 }
1302 // $type = isset( $_POST['type'] ) ? trim( $_POST['type'] ) : 'template';
1303 \wp_delete_post( $id );
1304 $cloud_map = DB::get_option( '_templately_cloud_map', [] );
1305 $new_cloud_map = [];
1306 array_walk( $cloud_map, function ( $cloud ) use ( &$cloud_map, $id, &$new_cloud_map ) {
1307 if ( $cloud['template_id'] !== $id ) {
1308 $new_cloud_map[] = $cloud;
1309 }
1310 } );
1311 DB::update_option( '_templately_cloud_map', $new_cloud_map );
1312 Helper::send_success( __( 'Successfully Deleted.', 'templately' ) );
1313 }
1314
1315 /**
1316 * This method helps to sync user profile data to current one.
1317 * @return void
1318 */
1319 protected function clouds_sync() {
1320 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
1321
1322 if ( ! empty( $api_key ) ) {
1323 $file_type = isset( $_POST['fileType'] ) ? trim( $_POST['fileType'] ) : 'elementor';
1324 $search = isset( $_POST['search'] ) ? trim( $_POST['search'] ) : '';
1325 $per_page = isset( $_POST['per_page'] ) ? intval( $_POST['per_page'] ) : 12;
1326 $pagenum = isset( $_POST['page'] ) ? intval( $_POST['page'] ) : 1;
1327
1328 $query = \sprintf(
1329 '{ myCloud( api_key: "%s", cloud_files_page: %s, cloud_files_per_page: %s, file_type: "%s"%s ) { limit, usages, name, last_pushed, files { data{ id, name, thumbnail, medium_thumbnail, preview, file_type, created_at }, current_page, total_page } } }',
1330 $api_key,
1331 $pagenum,
1332 $per_page,
1333 $file_type,
1334 ! empty( $search ) ? ', files_search: "' . $search . '"' : ''
1335 );
1336 $data = self::$query->get( $query );
1337 if ( is_wp_error( $data ) ) {
1338 Helper::send_error( $data->get_error_code() . ': ' . $data->get_error_message() );
1339 }
1340
1341 if ( isset( $data['data'], $data['data']['myCloud'] ) ) {
1342 $user_data = DB::get_user_specific_login_meta( '_templately_connect_data', [] );
1343 $user_data['clouds'] = $data['data']['myCloud'];
1344 DB::update_user_specific_login_meta( '_templately_connect_data', $user_data );
1345 if ( isset( $data['data']['myCloud']['last_pushed'] ) ) {
1346 DB::update_user_specific_login_meta( '_templately_cloud_last_activity', $data['data']['myCloud']['last_pushed'] );
1347 }
1348
1349 return $data['data']['myCloud']['files'];
1350 } else {
1351 // TODO: in future will be handled.
1352 Helper::send_success( __( 'You have no items in your cloud.', 'templately' ) );
1353 }
1354 }
1355 Helper::send_error( __( 'You have to logged in first via your admin dashboard.', 'templately' ) );
1356 }
1357
1358 /**
1359 * This Method is responsible for Disconnect user.
1360 * @return void
1361 */
1362 protected function logout() {
1363 DB::delete_option( '_templately_trigger' );
1364 DB::delete_transient( 'templately_dependencies' );
1365 DB::delete_option( '_templately_connect_email' );
1366
1367 DB::delete_user_specific_login_meta( '_templately_connected' );
1368 DB::delete_user_specific_login_meta( '_templately_api_key' );
1369 DB::delete_user_specific_login_meta( '_templately_connect_data' );
1370 DB::delete_user_specific_login_meta( '_templately_cloud_last_activity' );
1371
1372 if( Login::user_can_logout() ) {
1373 DB::delete_option( '_templately_user_login_choice' );
1374 }else {
1375 Login::delete_link_my_account();
1376 }
1377
1378 DB::delete_transient( 'templately_categories' );
1379 DB::delete_option( '_templately_cloud_map' );
1380 Helper::send_success( __( 'Logged Out', 'templately' ) );
1381 }
1382
1383 /**
1384 * Verification Done AJAX Call.
1385 */
1386 protected function verification_done() {
1387 $data = DB::get_user_specific_login_meta( '_templately_connect_data', [] );
1388 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
1389 $query = \sprintf('{ isVerifiedUser( api_key: "%s" ) }', $api_key);
1390 $response = self::$query->get( $query );
1391
1392 if( is_wp_error($response) ){
1393 Helper::send_error( $response->get_error_code() . ': ' . $response->get_error_message() );
1394 }
1395
1396 $data['is_verified'] = false;
1397 if( isset($response['data'], $response['data']['isVerifiedUser']) && boolval($response['data']['isVerifiedUser']) ) {
1398 $data['is_verified'] = true;
1399 }
1400
1401 return DB::update_user_specific_login_meta( '_templately_connect_data', $data );
1402 }
1403
1404 protected function install_requirements() {
1405 $requirements = isset( $_POST['requirements'] ) ? \json_decode( \wp_unslash( $_POST['requirements'] ), true ) : false;
1406 return RequirementInstaller::get_instance()->install_plugin( $requirements );
1407 die;
1408 }
1409
1410 /**
1411 * Platform Exists or Not
1412 *
1413 * @return void
1414 */
1415 protected function platform_exists() {
1416 $platform = isset( $_POST['platform'] ) ? trim( sanitize_text_field( $_POST['platform'] ) ) : 'elementor';
1417 if ( $platform === 'gutenberg' ) {
1418 $wp_version = get_bloginfo( 'version' );
1419 if ( version_compare( $wp_version, '5.0.0', '>=' ) ) {
1420 return true;
1421 } else {
1422 if ( \is_plugin_active( 'gutenberg/gutenberg.php' ) ) {
1423 return true;
1424 } else {
1425 return false;
1426 }
1427 }
1428 }
1429
1430 if ( $platform === 'elementor' ) {
1431 if ( \is_plugin_active( 'elementor/elementor.php' ) ) {
1432 return true;
1433 } else {
1434 return false;
1435 }
1436 }
1437
1438 return false;
1439 }
1440
1441 protected function set_favourite( $unfav = false ) {
1442 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
1443 if ( ! $api_key ) {
1444 Helper::send_error( __( 'Something went wrong. Please, try logged in again.', 'templately' ) );
1445 }
1446 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : false;
1447 $type = isset( $_POST['type'] ) ? trim( sanitize_text_field( $_POST['type'] ) ) : false;
1448 if ( $id === false || $type === false ) {
1449 Helper::send_error( __( 'Item ID or Item Type is not found.', 'templately' ) );
1450 }
1451
1452 $type = $type === 'block' || $type === 'page' ? 'item' : 'pack';
1453 $query = 'mutation { favourite( type_id: ' . $id . ', api_key: "' . $api_key . '", type: "' . $type . '" ){ status, message, data } }';
1454 if ( $unfav ) {
1455 $query = 'mutation { unFavourite( type_id: ' . $id . ', api_key: "' . $api_key . '", type: "' . $type . '" ) }';
1456 }
1457 $response = self::$query->get( $query );
1458 if ( is_wp_error( $response ) ) {
1459 Helper::send_error( $response->get_error_code() . ': ' . $response->get_error_message() );
1460 }
1461 if ( isset( $response['data']['favourite'] ) ) {
1462 $response = $response['data']['favourite'];
1463 if ( isset( $response['status'] ) && $response['status'] != 'success' ) {
1464 Helper::send_error( $response['message'] );
1465 }
1466
1467 $user_data = DB::get_user_specific_login_meta( '_templately_connect_data', [] );
1468 if ( ! empty( $user_data ) && array_key_exists( 'favourites', $user_data ) ) {
1469 if ( isset( $user_data['favourites'][ $type ] ) ) {
1470 if ( ! in_array( $id, $user_data['favourites'][ $type ] ) ) {
1471 $user_data['favourites'][ $type ][] = $id;
1472 }
1473 } else {
1474 $user_data['favourites'][ $type ] = [];
1475 $user_data['favourites'][ $type ][] = $id;
1476 }
1477 } else {
1478 $user_data['favourites'] = [];
1479 $user_data['favourites'][ $type ] = [];
1480 $user_data['favourites'][ $type ][] = $id;
1481 }
1482
1483 DB::update_user_specific_login_meta( '_templately_connect_data', $user_data );
1484
1485 return $user_data;
1486 }
1487 if ( $unfav && isset( $response['data']['unFavourite'] ) && $response['data']['unFavourite'] ) {
1488 $user_data = DB::get_user_specific_login_meta( '_templately_connect_data', [] );
1489 if ( $unfav && isset( $user_data['favourites'][ $type ] ) && is_array( $user_data['favourites'][ $type ] ) ) {
1490 $favourites = array_filter( $user_data['favourites'][ $type ], function ( $value ) use ( $id ) {
1491 return $value != $id;
1492 } );
1493 $user_data['favourites'][ $type ] = array_values( $favourites );
1494 DB::update_user_specific_login_meta( '_templately_connect_data', $user_data );
1495 }
1496
1497 return $user_data;
1498 }
1499 Helper::send_error( __( 'Something went wrong.', 'templately' ) );
1500 }
1501
1502 protected function get_workspace() {
1503 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
1504 if ( empty( $api_key ) ) {
1505 Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1506 }
1507
1508 $shared = isset( $_POST['shared'] ) ? boolval( $_POST['shared'] ) : false;
1509 $pagenum = isset( $_POST['pagenum'] ) ? intval( $_POST['pagenum'] ) : 1;
1510 $file_type = isset( $_POST['file_type'] ) ? trim( $_POST['file_type'] ) : 'elementor';
1511 $per_page = isset( $_POST['per_page'] ) ? intval( $_POST['per_page'] ) : 11;
1512
1513 $query = sprintf(
1514 '{ %s ( page: %s, per_page: %s, api_key: "%s", file_type: "%s" ){ data{ id, name, slug %s }, total_page, current_page } }',
1515 $shared ? 'sharedWorkspaces' : 'workspaces',
1516 $pagenum,
1517 $per_page,
1518 $api_key,
1519 $file_type,
1520 $pagenum !== - 1 ? ', sharedWith{ name, email, profile_photo, id }, owner{ id, name, joined, display_name }, pending_invitations' : ''
1521 );
1522 $response = self::$query->get( $query );
1523 if ( is_wp_error( $response ) ) {
1524 Helper::send_error( $response->get_error_code() . ': ' . $response->get_error_message() );
1525 }
1526
1527 if ( isset( $response['data'], $response['data']['workspaces'] ) && ! $shared ) {
1528 return $response['data']['workspaces'];
1529 }
1530
1531 if ( isset( $response['data'], $response['data']['sharedWorkspaces'] ) && $shared ) {
1532 return $response['data']['sharedWorkspaces'];
1533 }
1534
1535 Helper::send_error( __( 'Something went wrong loading WorkSpace. Try again or contact with Templately Support.', 'templately' ) );
1536 }
1537
1538 protected function create_workspace() {
1539 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
1540 if ( empty( $api_key ) ) {
1541 Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1542 }
1543 $title = isset( $_POST['title'] ) ? trim( $_POST['title'] ) : false;
1544 if ( $title === false ) {
1545 Helper::send_error( __( 'You have to give title at least.', 'templately' ) );
1546 }
1547 $share_with = isset( $_POST['share_with'] ) ? trim( $_POST['share_with'] ) : '';
1548
1549 $query = sprintf(
1550 'mutation { createWorkspace ( api_key: "%s", name: "%s", share_with: "%s" ){ status, message, workspace{ id, name, slug, sharedWith{ id, name, profile_photo }, owner{ id, name, profile_photo }, pending_invitations } } }',
1551 $api_key,
1552 $title,
1553 $share_with
1554 );
1555 $response = self::$query->get( $query );
1556 if ( is_wp_error( $response ) ) {
1557 Helper::send_error( $response->get_error_code() . ': ' . $response->get_error_message() );
1558 }
1559
1560 if ( isset( $response['data'], $response['data']['createWorkspace'] ) ) {
1561 return $response['data']['createWorkspace'];
1562 }
1563 Helper::send_error( __( 'Something went wrong regarding create WorkSpace. Try again or contact with Templately Support.', 'templately' ) );
1564 }
1565
1566 protected function create_or_edit_workspace( $edit = false ) {
1567 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
1568 if ( empty( $api_key ) ) {
1569 Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1570 }
1571 $title = isset( $_POST['title'] ) ? trim( $_POST['title'] ) : false;
1572 if ( $title === false ) {
1573 Helper::send_error( __( 'You have to give title at least.', 'templately' ) );
1574 }
1575 $share_with = isset( $_POST['share_with'] ) ? trim( $_POST['share_with'] ) : '';
1576
1577 $query = sprintf(
1578 'mutation { createWorkspace ( api_key: "%s", name: "%s", share_with: "%s" ){ status, message, workspace{ id, name, slug, sharedWith{ id, name, profile_photo }, owner{ id, name, profile_photo }, pending_invitations } } }',
1579 $api_key,
1580 $title,
1581 \wp_slash( \json_encode( \explode( ',', $share_with ) ) )
1582 );
1583 $action = 'create';
1584 if ( $edit ) {
1585 $action = 'edit';
1586 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : false;
1587 $platform = ( isset( $_POST['platform'] ) && ! empty( $_POST['platform'] ) ) ? trim( $_POST['platform'] ) : 'elementor';
1588 if ( $id === false ) {
1589 Helper::send_error( __( 'Workspace not found.', 'templately' ) );
1590 }
1591 $remove = ( isset( $_POST['remove'] ) && ! empty( $_POST['remove'] ) ) ? trim( $_POST['remove'] ) : '';
1592 $query = sprintf(
1593 'mutation { editWorkspace ( api_key: "%s", name: "%s", file_type: "%s", id: %s %s %s ){ status, workspace{ id, name, slug, sharedWith{ id, name, profile_photo }, files{ data{ id, my_cloud_id, name, file_type, owner{ id, name, profile_photo }, created_at, last_modified }, current_page, total_page }, owner{ id, name, profile_photo }, pending_invitations } } }',
1594 $api_key,
1595 $title,
1596 $platform,
1597 $id,
1598 ! empty( $share_with ) ? 'share_with: "' . \wp_slash( \json_encode( \explode( ',', $share_with ) ) ) . '"' : '',
1599 ! empty( $remove ) ? ', remove: "' . $remove . '"' : ''
1600 );
1601 }
1602 $response = self::$query->get( $query );
1603 if ( is_wp_error( $response ) ) {
1604 Helper::send_error( $response->get_error_code() . ': ' . $response->get_error_message() );
1605 }
1606
1607 if ( $edit === false && isset( $response['data'], $response['data']['createWorkspace'] ) ) {
1608 return $response['data']['createWorkspace'];
1609 }
1610 if ( $edit && isset( $response['data'], $response['data']['editWorkspace'] ) ) {
1611 return $response['data']['editWorkspace'];
1612 }
1613 Helper::send_error( __( "Something went wrong regarding $action WorkSpace. Try again or contact with Templately Support.", 'templately' ) );
1614 }
1615
1616 protected function delete_workspace() {
1617 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
1618 if ( empty( $api_key ) ) {
1619 Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1620 }
1621 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : false;
1622 if ( $id === false ) {
1623 Helper::send_error( __( 'Workspace not found.', 'templately' ) );
1624 }
1625 $withFiles = isset( $_POST['withFiles'] ) ? boolval( intval( $_POST['withFiles'] ) ) : false;
1626
1627 $query = sprintf(
1628 'mutation { deleteWorkspace ( id: %s, api_key: "%s" %s ){ status } }',
1629 $id,
1630 $api_key,
1631 $withFiles ? ', delete_files: true' : ''
1632 );
1633 $response = self::$query->get( $query );
1634 if ( is_wp_error( $response ) ) {
1635 Helper::send_error( $response->get_error_code() . ': ' . $response->get_error_message() );
1636 }
1637 if ( isset( $response['data'], $response['data']['deleteWorkspace'], $response['data']['deleteWorkspace']['status'] ) ) {
1638 return $response['data']['deleteWorkspace']['status'];
1639 }
1640 Helper::send_error( __( 'Something went wrong regarding delete WorkSpace. Try again or contact with Templately Support.', 'templately' ) );
1641 }
1642
1643 protected function copy_or_move() {
1644 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
1645 if ( empty( $api_key ) ) {
1646 Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1647 }
1648 $action = isset( $_POST['trigger_action'] ) ? trim( $_POST['trigger_action'] ) : 'copy';
1649 $cloud_file_id = isset( $_POST['cloud_file_id'] ) ? intval( $_POST['cloud_file_id'] ) : false;
1650 if ( $cloud_file_id === false ) {
1651 Helper::send_error( __( 'No Cloud Item is selected to' . $action . '.', 'templately' ) );
1652 }
1653 $workspace_id = isset( $_POST['workspace_id'] ) ? intval( $_POST['workspace_id'] ) : false;
1654 if ( $workspace_id === false ) {
1655 Helper::send_error( __( 'No WorkSpace is selected, where the item will ' . $action . ' to.', 'templately' ) );
1656 }
1657 $query = sprintf(
1658 'mutation { copyOrMoveToWorkspace ( api_key: "%s", my_cloud_file_id: %s, workspace_id: %s, action: "%s" ){ status, message } }',
1659 $api_key,
1660 $cloud_file_id,
1661 $workspace_id,
1662 $action
1663 );
1664 $response = self::$query->get( $query );
1665 if ( is_wp_error( $response ) ) {
1666 Helper::send_error( $response->get_error_code() . ': ' . $response->get_error_message() );
1667 }
1668
1669 if ( isset( $response['data'], $response['data']['copyOrMoveToWorkspace'], $response['data']['copyOrMoveToWorkspace']['status'] ) ) {
1670 return $response['data']['copyOrMoveToWorkspace']['status'];
1671 }
1672 Helper::send_error( __( "Something went wrong regarding $action to WorkSpace. Try again or contact with Templately Support.", 'templately' ) );
1673 }
1674
1675 protected function workspace_details() {
1676 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
1677 if ( empty( $api_key ) ) {
1678 Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1679 }
1680 $slug = isset( $_POST['slug'] ) ? trim( $_POST['slug'] ) : false;
1681 if ( $slug === false ) {
1682 Helper::send_error( __( 'Slug can\'t be empty.', 'templately' ) );
1683 }
1684
1685 $file_type = isset( $_POST['file_type'] ) ? trim( $_POST['file_type'] ) : 'elementor';
1686 $search = isset( $_POST['search'] ) ? trim( $_POST['search'] ) : '';
1687 $files_per_page = isset( $_POST['per_page'] ) ? intval( $_POST['per_page'] ) : 10;
1688 $files_page = isset( $_POST['page'] ) ? intval( $_POST['page'] ) : 1;
1689
1690 $query = sprintf(
1691 '{ workspaceDetails ( api_key: "%s", slug: "%s", files_page: %s, files_per_page: %s, file_type: "%s" %s ){ id, name, slug, files{ data { id, name, my_cloud_id, file_type, owner{ id, name, profile_photo }, created_at, last_modified }, current_page, total_page }, sharedWith{ name, id, profile_photo }, owner{ id, name, profile_photo }, pending_invitations } }',
1692 $api_key,
1693 $slug,
1694 $files_page,
1695 $files_per_page,
1696 $file_type,
1697 ! empty( $search ) ? ', files_search: "' . $search . '"' : ''
1698 );
1699 $response = self::$query->get( $query );
1700 if ( is_wp_error( $response ) ) {
1701 Helper::send_error( $response->get_error_code() . ': ' . $response->get_error_message() );
1702 }
1703 if ( isset( $response['data'], $response['data']['workspaceDetails'] ) ) {
1704 return $response['data']['workspaceDetails'];
1705 } else {
1706 if ( isset( $response['data'] ) && ! isset( $response['data']['workspaceDetails'] ) ) {
1707 return null;
1708 }
1709 }
1710 Helper::send_error( __( "Something went wrong regarding loading your WorkSpace. Try again or contact with Templately Support.", 'templately' ) );
1711 }
1712
1713 protected function cloud_usage_limit() {
1714 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
1715 if ( empty( $api_key ) ) {
1716 Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1717 }
1718
1719 $query = sprintf(
1720 '{ myCloudUsage( api_key: "%s" ){ data, status } }',
1721 $api_key
1722 );
1723 $response = self::$query->get( $query );
1724 if ( is_wp_error( $response ) ) {
1725 Helper::send_error( $response->get_error_code() . ': ' . $response->get_error_message() );
1726 }
1727 if ( isset( $response['data'], $response['data']['myCloudUsage'] ) ) {
1728 return $response['data']['myCloudUsage'];
1729 }
1730 Helper::send_error( __( "Something went wrong regarding collecting your usage limit. Try again or contact with Templately Support.", 'templately' ) );
1731 }
1732
1733 protected function add_files_to_workspace() {
1734 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
1735 if ( empty( $api_key ) ) {
1736 Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1737 }
1738
1739 $workspace_id = isset( $_POST['workspace_id'] ) ? intval( $_POST['workspace_id'] ) : false;
1740 if ( $workspace_id === false ) {
1741 Helper::send_error( __( 'Workspace Not Found.', 'templately' ) );
1742 }
1743 $files = isset( $_POST['files'] ) ? trim( $_POST['files'] ) : false;
1744 if ( $files === false ) {
1745 Helper::send_error( __( 'Workspace Not Found.', 'templately' ) );
1746 }
1747 $query = sprintf(
1748 'mutation { addFileToWorkspace( api_key: "%s", workspace_id: %d, files: %s ){ data, status } }',
1749 $api_key,
1750 $workspace_id,
1751 \json_encode( $files )
1752 );
1753 $response = self::$query->get( $query );
1754 if ( is_wp_error( $response ) ) {
1755 Helper::send_error( $response->get_error_code() . ': ' . $response->get_error_message() );
1756 }
1757 if ( isset( $response['data'], $response['data']['addFileToWorkspace'] ) ) {
1758 return $response['data']['addFileToWorkspace'];
1759 }
1760 Helper::send_error( __( "Something went wrong regarding adding files to your Workspace. Try again or contact with Templately Support.", 'templately' ) );
1761 }
1762
1763 public function get_templates() {
1764 return Elementor::templates( false, [
1765 'page' => ( isset( $_POST['page'] ) ? intval( $_POST['page'] ) : 1 )
1766 ] );
1767 }
1768 }
1769