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

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