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

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