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

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