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

1,661 lines 59.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately;
4
5 use Elementor\Plugin as ElementorPlugin;
6 use Elementor\TemplateLibrary\Source_Local;
7 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 $this->data = [];
423 if( isset( $data['data']['getItemsAndPacks']['data'] ) && ! empty( $data['data']['getItemsAndPacks']['data'] ) ) {
424 array_walk( $data['data']['getItemsAndPacks']['data'], function( $item ) {
425 $item['id'] = intval(\str_replace( " {$item['type']}", '', $item['id']));
426 $this->data[] = $item;
427 });
428 }
429 return $this->data;
430 }
431 }
432
433 /**
434 * Get template content
435 *
436 * @param string $origin
437 * @param integer $id
438 *
439 * @return void
440 */
441 public static function get_template_content( $args, $import = false ) {
442 if ( isset( $args['item_id'] ) && ! empty( $args['item_id'] ) ) {
443 $id = $args['item_id'];
444 }
445 if ( isset( $args['origin'] ) && ! empty( $args['origin'] ) ) {
446 $origin = $args['origin'];
447 } else {
448 $origin = 'remote';
449 }
450
451 if ( isset( $args['file_type'] ) && ! empty( $args['file_type'] ) ) {
452 $item_type = $args['file_type'];
453 }
454
455 if ( is_null( $id ) ) {
456 Helper::send_error( __( 'Something went wrong.', 'templately' ) );
457 }
458 /**
459 * Item will come from remote server.
460 */
461 if ( $origin === 'remote' || $origin === 'cloud' ) {
462 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
463 if ( ! $api_key ) {
464 Helper::send_error( __( 'Activate Your Account!', 'templately' ) );
465 }
466 $query = '{ itemContent( id: ' . $id . ', api_key: "' . $api_key . '" ){ status, message, data } }';
467 if ( $origin === 'cloud' ) {
468 $query = '{ myCloudInsert( file_id: ' . $id . ', api_key: "' . $api_key . '", file_type: "' . $item_type . '" ) }';
469 }
470 $data = Query::get( $query );
471 if ( is_wp_error( $data ) ) {
472 Helper::send_error( $data->get_error_code() . ': ' . $data->get_error_message() );
473 }
474 if ( $origin === 'remote' ) {
475 if ( isset( $data['data'], $data['data']['itemContent'], $data['data']['itemContent']['status'] ) ) {
476 if ( $data['data']['itemContent']['status'] == 'error' ) {
477 Helper::send_error( $data['data']['itemContent']['message'] );
478 }
479 if ( $data['data']['itemContent']['status'] == 'required-pro-acc' ) {
480 Helper::send_error( array (
481 'errorCode' => 'required_pro',
482 'message' => $data['data']['itemContent']['message'],
483 ) );
484 }
485 }
486
487 if ( isset( $data['data'], $data['data']['itemContent'], $data['data']['itemContent']['data'] ) ) {
488 $data = json_decode( $data['data']['itemContent']['data'], true );
489 if ( isset( $args['is_editor'] ) && $args['is_editor'] == true ) {
490 return $data;
491 } else {
492 // Helper::send_error( array(
493 // 'errorCode' => 'required_elementor_pro',
494 // 'message' => __( "Type: {$data['type']} needs Elementor Pro to import.", 'templately' ),
495 // ) );
496 }
497
498 return $data;
499 }
500 }
501 if ( $origin === 'cloud' ) {
502 $data = json_decode( $data['data']['myCloudInsert'], true );
503
504 return $data;
505 }
506 if ( $import ) {
507 return null;
508 }
509 Helper::send_error( __( 'Something went wrong!', 'templately' ) );
510 }
511 /**
512 * Item will come from local WP Installation.
513 */
514 if ( $origin === 'local' ) {
515 $data = Query::getFromLibrary( $id );
516 if ( isset( $data['content'] ) ) {
517 return $data;
518 }
519 }
520 Helper::send_error( __( 'Something went wrong!', 'templately' ) );
521 }
522
523 /**
524 * This Method is responsible for Inserting Template
525 *
526 * @param string $origin
527 *
528 * @return void
529 */
530 public function insert_template( $origin = 'remote' ) {
531 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : null;
532 if ( is_null( $id ) ) {
533 Helper::send_error( __( 'Templates ID Not Found.', 'templately' ) );
534 }
535 $is_editor = isset( $_POST['is_editor'] ) ? $_POST['is_editor'] === "true" : null;
536 $default_args = array (
537 'editor_post_id' => false,
538 'item_id' => $id,
539 'origin' => $origin
540 );
541 if ( ! is_null( $is_editor ) ) {
542 $default_args['is_editor'] = $is_editor;
543 }
544 if ( isset( $_POST['file_type'] ) && ! empty( $_POST['file_type'] ) ) {
545 $default_args['file_type'] = sanitize_text_field( $_POST['file_type'] );
546 }
547 if ( isset( $default_args['file_type'] ) && $default_args['file_type'] === 'gutenberg' ) {
548 $template = self::get_template_content( $default_args );
549 }
550 if ( isset( $default_args['file_type'] ) && $default_args['file_type'] === 'elementor' ) {
551 $importer = new Importer;
552 $template = $importer->get_data( $default_args );
553 }
554
555 return isset( $template['content'] ) ? $template : Helper::send_error( __( 'There is no Template Content to be inserted.', 'templately' ) );
556 }
557
558 private function cloud_push( $name, $data, $item_type = 'elementor', $args = array () ) {
559 if ( empty( $name ) ) {
560 Helper::send_error( __( "Name can't be empty.", 'templately' ) );
561 }
562 if ( $item_type === 'elementor' ) {
563 if ( empty( $data ) || ! is_array( $data ) ) {
564 Helper::send_error( __( "Data has to be an array.", 'templately' ) );
565 }
566 if ( ! isset( $data['content'] ) || ( isset( $data['content'] ) && empty( $data['content'] ) ) ) {
567 Helper::send_error( __( "There is no data to insert.", 'templately' ) );
568 }
569 }
570
571 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
572
573 if ( empty( $api_key ) ) {
574 Helper::send_error( __( "API Key is not found, try to re-login.", 'templately' ) );
575 }
576 // if( $item_type === 'elementor' ) {
577 // }
578 $data = wp_slash( \json_encode(
579 $data, JSON_UNESCAPED_LINE_TERMINATORS | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE
580 ) );
581
582 $response = Query::push( $name, $data, $api_key, $item_type, $args );
583 if ( is_wp_error( $response ) ) {
584 Helper::send_error( $response->get_error_code() . ': ' . $response->get_error_message() );
585 }
586 if ( isset( $response['errors'] ) ) {
587 Helper::send_error( $response['errors'][0]['message'] );
588 }
589
590 $cloud_item = json_decode( $response['data']['pushToMyCloud']['data'] );
591 if ( isset( $cloud_item->myCloud_item ) ) {
592 $cloud_map = DB::get_option( '_templately_cloud_map', [] );
593 $id = $args['id'];
594
595 if ( ! isset( $cloud_map[ $id ] ) ) {
596 $cloud_map[] = array (
597 'template_id' => $id,
598 'cloud_item_id' => intval( $cloud_item->myCloud_item ),
599 );
600 }
601 DB::update_option( '_templately_cloud_map', $cloud_map );
602 }
603
604 return $response['data']['pushToMyCloud'];
605 }
606
607 /**
608 * Push to Cloud
609 *
610 * @param integer $id
611 *
612 * @return void
613 */
614 private function push_to_cloud( $id = null, $item_type = 'elementor', $args = [] ) {
615 if ( isset( $_POST['workspace_id'] ) ) {
616 $args['workspace_id'] = intval( $_POST['workspace_id'] );
617 }
618
619 if ( $item_type === 'elementor' ) {
620 if ( $id === null ) {
621 Helper::send_error( __( 'ID not found to push the item in cloud.', 'templately' ) );
622 }
623 $data = Query::getFromLibrary( $id );
624 if ( ! empty( $data ) && isset( $data['content'] ) && ! empty( $data['content'] ) ) {
625 $name = \get_the_title( $id );
626 $args['id'] = $id;
627
628 return $this->cloud_push( $name, $data, $item_type, $args );
629 }
630 }
631
632 if ( $item_type === 'gutenberg' ) {
633 $name = isset( $_POST['title'] ) && ! empty( $_POST['title'] ) ? sanitize_text_field( $_POST['title'] ) : '';
634 $data = isset( $_POST['data'] ) && ! empty( $_POST['data'] ) ? $_POST['data'] : '';
635
636 return $this->cloud_push( $name, $data, 'gutenberg', $args );
637 }
638
639 Helper::send_error( __( 'Something went wrong', 'templately' ) );
640 }
641
642 /**
643 * This method is responsible for Removing item from Cloud
644 *
645 * @param integer $id
646 *
647 * @return void
648 */
649 private function remove_from_cloud( $id = null ) {
650 if ( is_null( $id ) ) {
651 Helper::send_error( __( 'ID not found to remove Template from Cloud.', 'templately' ) );
652 }
653 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
654 if ( ! $api_key ) {
655 Helper::send_error( __( 'You have no rights to delete file from Cloud. You have to logged in first', 'templately' ) );
656 }
657
658 $from = isset( $_POST['remove_from'] ) && ! empty( $_POST['remove_from'] ) ? trim( $_POST['remove_from'] ) : 'cloud';
659 $response = Query::remove_cloud_item( $id, $api_key, $from );
660
661 if ( is_wp_error( $response ) ) {
662 Helper::send_error( $response->get_error_code() . ': ' . $response->get_error_message() );
663 }
664
665 if ( $from === 'cloud' ) {
666 $response = $response['data']['removeFromMyCloud'];
667 } else {
668 $response = $response['data']['deleteWorkspaceFile'];
669 }
670
671 if ( isset( $response['status'] ) && $response['status'] != 'success' ) {
672 Helper::send_error( __( 'Something went wrong.', 'templately' ) );
673 }
674
675 if ( $from === 'cloud' ) {
676 $cloud_map = DB::get_option( '_templately_cloud_map', [] );
677 if ( ! empty( $cloud_map ) ) {
678 $new_cloud_map = array_filter( $cloud_map, function ( $value ) use ( $id ) {
679 return $value['cloud_item_id'] != $id;
680 } );
681 DB::update_option( '_templately_cloud_map', array_values( $new_cloud_map ) );
682 }
683
684 $users_data = DB::get_user_specific_login_meta( '_templately_connect_data', [] );
685 if ( array_key_exists( 'clouds', $users_data ) && array_key_exists( 'files', $users_data['clouds'] ) ) {
686 $new_users_data = array_filter( $users_data['clouds']['files'], function ( $value ) use ( $id ) {
687 return $value['id'] != $id;
688 } );
689 $users_data['clouds']['files'] = array_values( $new_users_data );
690 DB::update_user_specific_login_meta( '_templately_connect_data', $users_data );
691 }
692
693 return $id;
694 }
695
696 return $response;
697 }
698
699 /**
700 * Template Export
701 *
702 * @param integer $id
703 *
704 * @return void
705 */
706 public function template_export( $id = null ) {
707 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : null;
708 if ( $id === null ) {
709 Helper::send_error( __( 'Something went wrong.', 'templately' ) );
710 }
711
712 return ElementorPlugin::$instance->templates_manager->export_template( array (
713 'source' => 'local',
714 'template_id' => $id
715 ) );
716 }
717
718 /**
719 * Check Dependency for Items
720 * @return void
721 */
722 protected function check_dependency() {
723 $dependencies = json_decode( wp_unslash( $_POST['dependency'] ) );
724 $not_active_plugin_list = [];
725 $all_plugins = Helper::get_plugins();
726 if ( ! \is_plugin_active( 'elementor/elementor.php' ) ) {
727 $elementor_plugin = new \stdClass();
728 $elementor_plugin->name = __( 'Elementor', 'templately' );
729 $elementor_plugin->plugin_file = 'elementor/elementor.php';
730 $elementor_plugin->slug = 'elementor';
731 $elementor_plugin->is_pro = false;
732 $not_active_plugin_list[] = $elementor_plugin;
733 }
734
735 if ( ! empty( $dependencies ) && is_array( $dependencies ) ) {
736 foreach ( $dependencies as $dependency ) {
737 if ( is_null( $dependency->plugin_file ) ) {
738 continue;
739 }
740 if ( ! \is_plugin_active( $dependency->plugin_file ) ) {
741 if ( $dependency->is_pro ) {
742 if ( isset( $all_plugins[ $dependency->plugin_file ] ) ) {
743 unset( $dependency->is_pro );
744 $dependency->message = __( 'You have the plugin installed.', 'templately' );
745 $not_active_plugin_list[] = $dependency;
746 } else {
747 $not_active_plugin_list[] = $dependency;
748 }
749 } else {
750 $not_active_plugin_list[] = $dependency;
751 }
752 }
753 }
754 }
755 Helper::send_success( array (
756 'dependency' => $not_active_plugin_list
757 ) );
758 }
759
760 /**
761 * This Method is responsible for Get Item from Server.
762 * @return void
763 */
764 protected function get_item() {
765 $slug = isset( $_POST['slug'] ) ? trim( sanitize_text_field( $_POST['slug'] ) ) : false;
766 if ( ! $slug ) {
767 Helper::send_error( __( 'Something went wrong.', 'templately' ) );
768 }
769 $type = isset( $_POST['type'] ) ? trim( sanitize_text_field( $_POST['type'] ) ) : 'block';
770
771 $query = '';
772 if ( $type === 'block' ) {
773 $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 } } } }';
774 }
775 if ( $type === 'pack' ) {
776 $query = '{
777 packs( slug: "' . $slug . '" ) {
778 data { name, id, slug, rating, downloads, favourite_count, thumbnail, price, author{ display_name, name, profile_photo, joined }, tags{ name }, category{ name },
779 items {
780 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, author{ display_name, name, profile_photo, joined }, tags{ name }, category{ name, id } } }
781 }
782 }';
783 }
784
785 if ( $type === 'page' ) {
786 $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 } } } }';
787 }
788
789 $data = Query::get( $query );
790 if ( \is_wp_error( $data ) ) {
791 Helper::send_error( $data->get_error_code() . ': ' . $data->get_error_message() );
792 }
793 if ( $type === 'block' ) {
794 if ( isset( $data['data'], $data['data']['items'] ) ) {
795 return $data['data']['items']['data'];
796 }
797 }
798 if ( $type === 'page' ) {
799 if ( isset( $data['data'], $data['data']['pages'] ) ) {
800 return $data['data']['pages']['data'];
801 }
802 }
803 if ( $type === 'pack' ) {
804 if ( isset( $data['data'], $data['data']['packs'] ) ) {
805 return $data['data']['packs']['data'];
806 }
807 }
808 Helper::send_error( __( 'Something went wrong!', 'templately' ) );
809 }
810
811
812 /**
813 * This Method is responsible for Get Items tag from Server.
814 * @return void
815 */
816 protected function get_tags() {
817 $tags = DB::get_option( '_templately_tags', false );
818 if ( $tags !== false ) {
819 return $tags;
820 }
821
822 $query = '{ tags { id, name } }';
823 $data = Query::get( $query );
824
825 if ( \is_wp_error( $data ) ) {
826 Helper::send_error( $data->get_error_code() . ': ' . $data->get_error_message() );
827 }
828
829 if ( isset( $data['data'], $data['data']['tags'] ) ) {
830 DB::update_option( '_templately_tags', $data['data']['tags'] );
831
832 return $data['data']['tags'];
833 }
834
835 Helper::send_error( __( 'Something went wrong with fetching dependencies data.', 'templately' ) );
836 }
837
838
839 /**
840 * This Methods Helps to Import Template in Library
841 * @return void
842 */
843 public function import_to_library() {
844 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : false;
845 if ( ! $id ) {
846 Helper::send_error( __( 'Something went wrong!', 'templately' ) );
847 }
848 $title = isset( $_POST['title'] ) ? trim( sanitize_text_field( $_POST['title'] ) ) : '';
849
850 $importer = new Importer;
851 $template = $importer->get_data( array (
852 'editor_post_id' => false,
853 'item_id' => $id
854 ) );
855 $imported_template = $importer->import_templately( $template );
856 if ( is_wp_error( $imported_template ) ) {
857 Helper::send_error( 'Error Code: ' . $imported_template->get_error_code() . ', Message: ' . $imported_template->get_error_message() );
858 }
859 if ( ! is_wp_error( $imported_template ) && is_array( $imported_template ) ) {
860 return array (
861 'post_id' => $imported_template['template_id'],
862 'edit_link' => get_edit_post_link( $imported_template['template_id'], 'internal' ),
863 'elementor_edit_link' => ElementorPlugin::$instance->documents->get( $imported_template['template_id'] )->get_edit_url(),
864 'visit' => $imported_template['url']
865 );
866 }
867 Helper::send_error( __( 'Something went wrong with Import!', 'templately' ) );
868 }
869
870 /**
871 * This Method is Responsible for Create A Page From a Template.
872 * also its used to Import Template
873 *
874 * @param boolean $is_import
875 *
876 * @return void
877 */
878 public function create_page() {
879 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : false;
880 if ( ! $id ) {
881 Helper::send_error( __( 'Something went wrong!', 'templately' ) );
882 }
883 $title = isset( $_POST['title'] ) ? trim( sanitize_text_field( $_POST['title'] ) ) : '';
884 $platform = isset( $_POST['platform'] ) ? trim( $_POST['platform'] ) : 'elementor';
885 if ( $platform === 'elementor' ) {
886 $importer = new Importer;
887 $template_data = $importer->get_data( array (
888 'editor_post_id' => false,
889 'item_id' => $id
890 ) );
891 if ( ! empty( $title ) ) {
892 $template_data['page_title'] = $title;
893 }
894 $inserted_ID = $importer->create_page( $template_data );
895 }
896
897 if ( $platform === 'gutenberg' ) {
898 $inserted_ID = self::get_template_content( array (
899 'origin' => 'remote',
900 'item_id' => $id,
901 ) );
902
903 if ( isset( $inserted_ID['content'] ) ) {
904 $inserted_ID = wp_insert_post( array (
905 'post_status' => 'draft',
906 'post_type' => 'page',
907 'post_title' => $title,
908 'post_content' => $inserted_ID['content'],
909 ) );
910 }
911 }
912
913 if ( is_wp_error( $inserted_ID ) ) {
914 Helper::send_error( 'Error Code: ' . $inserted_ID->get_error_code() . ', Message: ' . $inserted_ID->get_error_message() );
915 }
916
917 if ( $inserted_ID && ! is_wp_error( $inserted_ID ) ) {
918 return array (
919 'post_id' => $inserted_ID,
920 'edit_link' => get_edit_post_link( $inserted_ID, 'internal' ),
921 'elementor_edit_link' => $platform === 'elementor' ? ElementorPlugin::$instance->documents->get( $inserted_ID )->get_edit_url() : false,
922 'visit' => get_permalink( $inserted_ID )
923 );
924 }
925
926 Helper::send_error( __( 'Something went wrong!', 'templately' ) );
927 }
928
929 /**
930 * This Method is responsible for Check User Exists or Not and Make them Logged in to Server
931 * to collect profiles data and the API Key.
932 *
933 * @param boolean $login
934 *
935 * @return void
936 */
937 public function login_user() {
938 $errors = [];
939
940 $global_signin = filter_var( $_POST['global_signin'], FILTER_VALIDATE_BOOLEAN);
941
942 if( ! $global_signin ) {
943 Login::set_link_my_account();
944 }else {
945 Login::set_user_login_choice( get_current_user_id(), $global_signin );
946 }
947
948 $withApi = isset( $_POST['withApi'] ) ? trim( $_POST['withApi'] ) === 'true' : false;
949
950 if ( $withApi ) {
951 $api_key = sanitize_text_field( trim( $_POST['api_key'] ) );
952 if ( empty( $api_key ) ) {
953 $errors['api_key'] = __( 'API Key Field Cannot be Empty.', 'templately' );
954 }
955 $query = \sprintf(
956 '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 }}}',
957 $api_key,
958 \home_url( '/' ),
959 self::get_ip()
960 );
961 } else {
962 $email = sanitize_text_field( trim( $_POST['email'] ) );
963 if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
964 $errors['email'] = __( 'Make sure you have given a valid email address.', 'templately' );
965 }
966 $password = sanitize_text_field( trim( $_POST['password'] ) );
967 if ( empty( $password ) ) {
968 $errors['password'] = __( 'Password Field Cannot be Empty.', 'templately' );
969 }
970 $query = \sprintf(
971 '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 }}}}}',
972 $email,
973 $password,
974 \home_url( '/' ),
975 self::get_ip()
976 );
977 }
978
979
980 $api_response = Query::get( $query );
981
982 if ( is_wp_error( $api_response ) ) {
983 Helper::send_error( $api_response->get_error_code() . ': ' . $api_response->get_error_message() );
984 }
985 if ( isset( $api_response['errors'] ) ) {
986 Helper::send_error( __( 'Something went wrong. Maybe Invalid Credentials.', 'templately' ) );
987 }
988 if ( $withApi ) {
989 $api_response = $api_response['data']['connectWithApiKey'];
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 } else {
996 $api_response = $api_response['data']['connect'];
997 if ( isset( $api_response['status'] ) && $api_response['status'] !== 'success' ) {
998 Helper::send_error( $api_response['message'] );
999 }
1000
1001 return $this->user_data( $api_response );
1002 }
1003 Helper::send_error( __( 'Something went wrong', 'templately' ) );
1004 }
1005
1006 public function create_user() {
1007 $errors = [];
1008 $first_name = sanitize_text_field( trim( $_POST['first_name'] ) );
1009 $last_name = sanitize_text_field( trim( $_POST['last_name'] ) );
1010 if ( empty( $first_name ) ) {
1011 $errors['first_name'] = __( 'First Name Can\'t be Empty.', 'templately' );
1012 }
1013 if ( empty( $last_name ) ) {
1014 $errors['last_name'] = __( 'Last Name Can\'t be Empty.', 'templately' );
1015 }
1016 $email = sanitize_text_field( trim( $_POST['email'] ) );
1017 if ( empty( $email ) ) {
1018 $errors['email'] = __( 'Email Field Can\'t be Empty.', 'templately' );
1019 }
1020 if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
1021 $errors['email'] = __( 'Make sure you have given a valid email address.', 'templately' );
1022 }
1023 $password = sanitize_text_field( trim( $_POST['password'] ) );
1024 if ( empty( $password ) ) {
1025 $errors['password'] = __( 'Password Field Can\'t be Empty.', 'templately' );
1026 }
1027 if ( ! empty( $errors ) ) {
1028 Helper::send_error( $errors );
1029 }
1030
1031 $query = \sprintf(
1032 '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}}}',
1033 $first_name,
1034 $last_name,
1035 $email,
1036 $password,
1037 \home_url( '/' ),
1038 self::get_ip()
1039 );
1040 $api_response = Query::get( $query );
1041 if ( is_wp_error( $api_response ) ) {
1042 Helper::send_error( $api_response->get_error_code() . ': ' . $api_response->get_error_message() );
1043 }
1044
1045 if ( isset( $api_response['errors'] ) ) {
1046 if ( isset( $api_response['errors'][0]['extensions'] ) && isset( $api_response['errors'][0]['extensions']['validation'] ) ) {
1047 Helper::send_error( $api_response['errors'][0]['extensions']['validation'] );
1048 }
1049 Helper::send_error( 'Something went wrong.' );
1050 }
1051
1052 $api_response = $api_response['data']['createUser'];
1053 if ( isset( $api_response['status'] ) && $api_response['status'] !== 'success' ) {
1054 Helper::send_error( $api_response['message'] );
1055 }
1056
1057 return $this->user_data( $api_response, true );
1058 }
1059
1060 /**
1061 * Helper Function for checkNVerify methods
1062 *
1063 * @param array $api_response
1064 *
1065 * @return void
1066 */
1067 protected function user_data( $api_response, $create_user = false, $raw = false ) {
1068 if ( isset( $api_response ) && ! is_null( $api_response ) ) {
1069 $user_data = $api_response;
1070 $userState = [];
1071 if ( isset( $user_data['user'], $user_data['user']['favourites'] ) ) {
1072 $favourites = [
1073 'item' => [],
1074 'pack' => []
1075 ];
1076 array_walk( $user_data['user']['favourites'], function ( $value ) use ( &$favourites ) {
1077 if ( ! is_null( $value ) ) {
1078 if ( ! in_array( $value['id'], isset( $favourites[ $value['type'] ] ) ? $favourites[ $value['type'] ] : [] ) ) {
1079 $favourites[ $value['type'] ][] = $value['id'];
1080 }
1081 }
1082 } );
1083 $userState['favourites'] = $favourites;
1084 }
1085
1086 if ( isset( $user_data['user'], $user_data['user']['id'] ) ) {
1087 $userState['id'] = intval( $user_data['user']['id'] );
1088 }
1089 if ( isset( $user_data['user'], $user_data['user']['first_name'] ) ) {
1090 $userState['first_name'] = sanitize_text_field( $user_data['user']['first_name'] );
1091 }
1092 if ( isset( $user_data['user'], $user_data['user']['profile_photo'] ) ) {
1093 $userState['profile_photo'] = sanitize_text_field( $user_data['user']['profile_photo'] );
1094 }
1095 if ( isset( $user_data['user'], $user_data['user']['last_name'] ) ) {
1096 $userState['last_name'] = sanitize_text_field( $user_data['user']['last_name'] );
1097 }
1098 if ( isset( $user_data['user'], $user_data['user']['name'] ) ) {
1099 $userState['name'] = sanitize_text_field( $user_data['user']['name'] );
1100 }
1101 if ( isset( $user_data['user'], $user_data['user']['email'] ) ) {
1102 $userState['email'] = sanitize_text_field( $user_data['user']['email'] );
1103 }
1104 if ( isset( $user_data['user'], $user_data['user']['plan'] ) ) {
1105 $userState['plan'] = sanitize_text_field( $user_data['user']['plan'] );
1106 }
1107 if ( isset( $user_data['user'], $user_data['user']['plan_expire_at'] ) ) {
1108 $userState['plan_expire_at'] = sanitize_text_field( $user_data['user']['plan_expire_at'] );
1109 }
1110 if ( isset( $user_data['user'], $user_data['user']['joined'] ) ) {
1111 $userState['joined'] = sanitize_text_field( $user_data['user']['joined'] );
1112 }
1113 if ( isset( $user_data['status'] ) ) {
1114 $userState['status'] = sanitize_text_field( $user_data['status'] );
1115 }
1116 $api_key = '';
1117 if ( isset( $user_data['user'], $user_data['user']['api_key'] ) ) {
1118 $api_key = sanitize_text_field( $user_data['user']['api_key'] );
1119 }
1120 if ( isset( $user_data['user'], $user_data['user']['my_cloud'] ) ) {
1121 $user_clouds['limit'] = $user_data['user']['my_cloud']['limit'];
1122 $user_clouds['usages'] = $user_data['user']['my_cloud']['usages'];
1123 $user_clouds['name'] = $user_data['user']['my_cloud']['name'];
1124 $user_cloud_files = [];
1125
1126 if ( isset( $user_data['user']['my_cloud']['last_pushed'] ) ) {
1127 DB::update_user_specific_login_meta( '_templately_cloud_last_activity', $user_data['user']['my_cloud']['last_pushed'] );
1128 }
1129
1130 array_walk( $user_data['user']['my_cloud']['files'], function ( $value ) use ( &$user_cloud_files ) {
1131 //TODO: mycloud
1132 } );
1133 $user_clouds['files'] = $user_cloud_files;
1134 $userState['clouds'] = $user_clouds;
1135 }
1136 if ( empty( $api_key ) ) {
1137 Helper::send_error( __( 'API Key is not found', 'templately' ) );
1138 }
1139
1140 if ( $create_user ) {
1141 $userState['is_verified'] = false;
1142 }
1143
1144 DB::update_user_specific_login_meta( '_templately_api_key', $api_key );
1145 DB::update_user_specific_login_meta( '_templately_connected', true );
1146 DB::update_user_specific_login_meta( '_templately_connect_data', $userState );
1147
1148 if ( $raw ) {
1149 return $userState;
1150 }
1151 Helper::send_success( array (
1152 'message' => __( 'Successfully Connected', 'templately' ),
1153 'admin' => $userState
1154 ) );
1155 }
1156 }
1157
1158 /**
1159 * This method delete templates from library.
1160 * @return void
1161 */
1162 protected function delete_template() {
1163 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : false;
1164 if ( ! $id ) {
1165 Helper::send_error( __( 'Template ID is not found to delete', 'templately' ) );
1166 }
1167 // $type = isset( $_POST['type'] ) ? trim( $_POST['type'] ) : 'template';
1168 \wp_delete_post( $id );
1169 $cloud_map = DB::get_option( '_templately_cloud_map', [] );
1170 $new_cloud_map = [];
1171 array_walk( $cloud_map, function ( $cloud ) use ( &$cloud_map, $id, &$new_cloud_map ) {
1172 if ( $cloud['template_id'] !== $id ) {
1173 $new_cloud_map[] = $cloud;
1174 }
1175 } );
1176 DB::update_option( '_templately_cloud_map', $new_cloud_map );
1177 Helper::send_success( __( 'Successfully Deleted.', 'templately' ) );
1178 }
1179
1180 /**
1181 * This method helps to sync user profile data to current one.
1182 * @return void
1183 */
1184 protected function clouds_sync() {
1185 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
1186
1187 if ( ! empty( $api_key ) ) {
1188 $file_type = isset( $_POST['fileType'] ) ? trim( $_POST['fileType'] ) : 'elementor';
1189 $search = isset( $_POST['search'] ) ? trim( $_POST['search'] ) : '';
1190 $per_page = isset( $_POST['per_page'] ) ? intval( $_POST['per_page'] ) : 12;
1191 $pagenum = isset( $_POST['page'] ) ? intval( $_POST['page'] ) : 1;
1192
1193 $query = \sprintf(
1194 '{ 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 } } }',
1195 $api_key,
1196 $pagenum,
1197 $per_page,
1198 $file_type,
1199 ! empty( $search ) ? ', files_search: "' . $search . '"' : ''
1200 );
1201 $data = Query::get( $query );
1202 if ( is_wp_error( $data ) ) {
1203 Helper::send_error( $data->get_error_code() . ': ' . $data->get_error_message() );
1204 }
1205 if ( isset( $data['data'], $data['data']['myCloud'] ) ) {
1206 $user_data = DB::get_user_specific_login_meta( '_templately_connect_data', [] );
1207 $user_data['clouds'] = $data['data']['myCloud'];
1208 DB::update_user_specific_login_meta( '_templately_connect_data', $user_data );
1209 if ( isset( $data['data']['myCloud']['last_pushed'] ) ) {
1210 DB::update_user_specific_login_meta( '_templately_cloud_last_activity', $data['data']['myCloud']['last_pushed'] );
1211 }
1212
1213 return $data['data']['myCloud']['files'];
1214 } else {
1215 // TODO: in future will be handled.
1216 }
1217 }
1218 Helper::send_error( __( 'You have to logged in first via your admin dashboard.', 'templately' ) );
1219 }
1220
1221 /**
1222 * This Method is responsible for Disconnect user.
1223 * @return void
1224 */
1225 protected function logout() {
1226 DB::delete_option( '_templately_trigger' );
1227 DB::delete_option( '_templately_dependencies' );
1228 DB::delete_option( '_templately_connect_email' );
1229
1230 DB::delete_user_specific_login_meta( '_templately_connected' );
1231 DB::delete_user_specific_login_meta( '_templately_api_key' );
1232 DB::delete_user_specific_login_meta( '_templately_connect_data' );
1233 DB::delete_user_specific_login_meta( '_templately_cloud_last_activity' );
1234
1235 if( Login::user_can_logout() ) {
1236 DB::delete_option( '_templately_user_login_choice' );
1237 }else {
1238 Login::delete_link_my_account();
1239 }
1240
1241 DB::delete_option( '_templately_categories' );
1242 DB::delete_option( '_templately_cloud_map' );
1243 Helper::send_success( __( 'Logged Out', 'templately' ) );
1244 }
1245
1246 /**
1247 * Verification Done AJAX Call.
1248 */
1249 protected function verification_done() {
1250 $data = DB::get_user_specific_login_meta( '_templately_connect_data', [] );
1251 if ( isset( $data['is_verified'] ) && $data['is_verified'] == false ) {
1252 unset( $data['is_verified'] );
1253 }
1254
1255 return DB::update_user_specific_login_meta( '_templately_connect_data', $data );
1256 }
1257
1258 protected function install_requirements() {
1259 $requirements = isset( $_POST['requirements'] ) ? \json_decode( \wp_unslash( $_POST['requirements'] ), true ) : false;
1260
1261 return RequirementInstaller::get_instance()->install_plugin( $requirements );
1262 die;
1263 }
1264
1265 /**
1266 * Platform Exists or Not
1267 *
1268 * @return void
1269 */
1270 protected function platform_exists() {
1271 $platform = isset( $_POST['platform'] ) ? trim( sanitize_text_field( $_POST['platform'] ) ) : 'elementor';
1272 if ( $platform === 'gutenberg' ) {
1273 $wp_version = get_bloginfo( 'version' );
1274 if ( version_compare( $wp_version, '5.0.0', '>=' ) ) {
1275 return true;
1276 } else {
1277 if ( \is_plugin_active( 'gutenberg/gutenberg.php' ) ) {
1278 return true;
1279 } else {
1280 return false;
1281 }
1282 }
1283 }
1284
1285 if ( $platform === 'elementor' ) {
1286 if ( \is_plugin_active( 'elementor/elementor.php' ) ) {
1287 return true;
1288 } else {
1289 return false;
1290 }
1291 }
1292
1293 return false;
1294 }
1295
1296 protected function set_favourite( $unfav = false ) {
1297 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
1298 if ( ! $api_key ) {
1299 Helper::send_error( __( 'Something went wrong. Please, try logged in again.', 'templately' ) );
1300 }
1301 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : false;
1302 $type = isset( $_POST['type'] ) ? trim( sanitize_text_field( $_POST['type'] ) ) : false;
1303 if ( $id === false || $type === false ) {
1304 Helper::send_error( __( 'Item ID or Item Type is not found.', 'templately' ) );
1305 }
1306
1307 $type = $type === 'block' || $type === 'page' ? 'item' : 'pack';
1308 $query = 'mutation { favourite( type_id: ' . $id . ', api_key: "' . $api_key . '", type: "' . $type . '" ){ status, message, data } }';
1309 if ( $unfav ) {
1310 $query = 'mutation { unFavourite( type_id: ' . $id . ', api_key: "' . $api_key . '", type: "' . $type . '" ) }';
1311 }
1312 $response = Query::get( $query );
1313 if ( is_wp_error( $response ) ) {
1314 Helper::send_error( $response->get_error_code() . ': ' . $response->get_error_message() );
1315 }
1316 if ( isset( $response['data']['favourite'] ) ) {
1317 $response = $response['data']['favourite'];
1318 if ( isset( $response['status'] ) && $response['status'] != 'success' ) {
1319 Helper::send_error( $response['message'] );
1320 }
1321
1322 $user_data = DB::get_user_specific_login_meta( '_templately_connect_data', [] );
1323 if ( ! empty( $user_data ) && array_key_exists( 'favourites', $user_data ) ) {
1324 if ( isset( $user_data['favourites'][ $type ] ) ) {
1325 if ( ! in_array( $id, $user_data['favourites'][ $type ] ) ) {
1326 $user_data['favourites'][ $type ][] = $id;
1327 }
1328 } else {
1329 $user_data['favourites'][ $type ] = [];
1330 $user_data['favourites'][ $type ][] = $id;
1331 }
1332 } else {
1333 $user_data['favourites'] = [];
1334 $user_data['favourites'][ $type ] = [];
1335 $user_data['favourites'][ $type ][] = $id;
1336 }
1337
1338 DB::update_user_specific_login_meta( '_templately_connect_data', $user_data );
1339
1340 return $user_data;
1341 }
1342 if ( $unfav && isset( $response['data']['unFavourite'] ) && $response['data']['unFavourite'] ) {
1343 $user_data = DB::get_user_specific_login_meta( '_templately_connect_data', [] );
1344 if ( $unfav && isset( $user_data['favourites'][ $type ] ) && is_array( $user_data['favourites'][ $type ] ) ) {
1345 $favourites = array_filter( $user_data['favourites'][ $type ], function ( $value ) use ( $id ) {
1346 return $value != $id;
1347 } );
1348 $user_data['favourites'][ $type ] = array_values( $favourites );
1349 DB::update_user_specific_login_meta( '_templately_connect_data', $user_data );
1350 }
1351
1352 return $user_data;
1353 }
1354 Helper::send_error( __( 'Something went wrong.', 'templately' ) );
1355 }
1356
1357 protected function get_workspace() {
1358 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
1359 if ( empty( $api_key ) ) {
1360 Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1361 }
1362
1363 $shared = isset( $_POST['shared'] ) ? boolval( $_POST['shared'] ) : false;
1364 $pagenum = isset( $_POST['pagenum'] ) ? intval( $_POST['pagenum'] ) : 1;
1365 $file_type = isset( $_POST['file_type'] ) ? trim( $_POST['file_type'] ) : 'elementor';
1366 $per_page = isset( $_POST['per_page'] ) ? intval( $_POST['per_page'] ) : 11;
1367
1368 $query = sprintf(
1369 '{ %s ( page: %s, per_page: %s, api_key: "%s", file_type: "%s" ){ data{ id, name, slug %s }, total_page, current_page } }',
1370 $shared ? 'sharedWorkspaces' : 'workspaces',
1371 $pagenum,
1372 $per_page,
1373 $api_key,
1374 $file_type,
1375 $pagenum !== - 1 ? ', sharedWith{ name, email, profile_photo, id }, owner{ id, name, joined, display_name }, pending_invitations' : ''
1376 );
1377 $response = Query::get( $query );
1378 if ( is_wp_error( $response ) ) {
1379 Helper::send_error( $response->get_error_code() . ': ' . $response->get_error_message() );
1380 }
1381
1382 if ( isset( $response['data'], $response['data']['workspaces'] ) && ! $shared ) {
1383 return $response['data']['workspaces'];
1384 }
1385
1386 if ( isset( $response['data'], $response['data']['sharedWorkspaces'] ) && $shared ) {
1387 return $response['data']['sharedWorkspaces'];
1388 }
1389
1390 Helper::send_error( __( 'Something went wrong loading WorkSpace. Try again or contact with Templately Support.', 'templately' ) );
1391 }
1392
1393 protected function create_workspace() {
1394 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
1395 if ( empty( $api_key ) ) {
1396 Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1397 }
1398 $title = isset( $_POST['title'] ) ? trim( $_POST['title'] ) : false;
1399 if ( $title === false ) {
1400 Helper::send_error( __( 'You have to give title at least.', 'templately' ) );
1401 }
1402 $share_with = isset( $_POST['share_with'] ) ? trim( $_POST['share_with'] ) : '';
1403
1404 $query = sprintf(
1405 '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 } } }',
1406 $api_key,
1407 $title,
1408 $share_with
1409 );
1410 $response = Query::get( $query );
1411 if ( is_wp_error( $response ) ) {
1412 Helper::send_error( $response->get_error_code() . ': ' . $response->get_error_message() );
1413 }
1414
1415 if ( isset( $response['data'], $response['data']['createWorkspace'] ) ) {
1416 return $response['data']['createWorkspace'];
1417 }
1418 Helper::send_error( __( 'Something went wrong regarding create WorkSpace. Try again or contact with Templately Support.', 'templately' ) );
1419 }
1420
1421 // protected function edit_workspace(){
1422 // $api_key = DB::get_option('_templately_api_key', false);
1423 // if( empty( $api_key ) ) {
1424 // Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1425 // }
1426 // $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : false;
1427 // if( $id === false ) {
1428 // Helper::send_error( __( 'Workspace not found.', 'templately' ) );
1429 // }
1430
1431 // $name = ( isset( $_POST['name'] ) && ! empty( $_POST['name'] ) ) ? trim( $_POST['name'] ) : '';
1432 // $share_with = ( isset( $_POST['share_with'] ) && ! empty( $_POST['share_with'] ) ) ? trim( $_POST['share_with'] ) : '';
1433 // $remove = ( isset( $_POST['remove'] ) && ! empty( $_POST['remove'] ) ) ? trim( $_POST['remove'] ) : '';
1434 // $platform = ( isset( $_POST['platform'] ) && ! empty( $_POST['platform'] ) ) ? trim( $_POST['platform'] ) : 'elementor';
1435
1436 // $query = sprintf(
1437 // 'mutation { editWorkspace ( id: %s, api_key: "%s", name: "%s", share_with: "%s", remove: "%s", file_type: "%s" ){ status } }',
1438 // $id,
1439 // $api_key,
1440 // $name,
1441 // $share_with,
1442 // $remove,
1443 // $platform
1444 // );
1445 // $response = Query::get( $query );
1446 // if( is_wp_error( $response ) ) {
1447 // Helper::send_error( $response->get_error_code() . ': ' . $response->get_error_message() );
1448 // }
1449
1450 // if( isset( $response['data'], $response['data']['editWorkspace'], $response['data']['editWorkspace']['data'] ) ) {
1451 // return \json_decode( $response['data']['editWorkspace']['data'] );
1452 // }
1453
1454
1455 // Helper::send_error( __( 'Something went wrong regarding edit WorkSpace. Try again or contact with Templately Support.', 'templately' ) );
1456 // }
1457
1458 protected function create_or_edit_workspace( $edit = false ) {
1459 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
1460 if ( empty( $api_key ) ) {
1461 Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1462 }
1463 $title = isset( $_POST['title'] ) ? trim( $_POST['title'] ) : false;
1464 if ( $title === false ) {
1465 Helper::send_error( __( 'You have to give title at least.', 'templately' ) );
1466 }
1467 $share_with = isset( $_POST['share_with'] ) ? trim( $_POST['share_with'] ) : '';
1468
1469 $query = sprintf(
1470 '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 } } }',
1471 $api_key,
1472 $title,
1473 \wp_slash( \json_encode( \explode( ',', $share_with ) ) )
1474 );
1475 $action = 'create';
1476 if ( $edit ) {
1477 $action = 'edit';
1478 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : false;
1479 $platform = ( isset( $_POST['platform'] ) && ! empty( $_POST['platform'] ) ) ? trim( $_POST['platform'] ) : 'elementor';
1480 if ( $id === false ) {
1481 Helper::send_error( __( 'Workspace not found.', 'templately' ) );
1482 }
1483 $remove = ( isset( $_POST['remove'] ) && ! empty( $_POST['remove'] ) ) ? trim( $_POST['remove'] ) : '';
1484 $query = sprintf(
1485 '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 } } }',
1486 $api_key,
1487 $title,
1488 $platform,
1489 $id,
1490 ! empty( $share_with ) ? 'share_with: "' . \wp_slash( \json_encode( \explode( ',', $share_with ) ) ) . '"' : '',
1491 ! empty( $remove ) ? ', remove: "' . $remove . '"' : ''
1492 );
1493 }
1494 $response = Query::get( $query );
1495 if ( is_wp_error( $response ) ) {
1496 Helper::send_error( $response->get_error_code() . ': ' . $response->get_error_message() );
1497 }
1498
1499 if ( $edit === false && isset( $response['data'], $response['data']['createWorkspace'] ) ) {
1500 return $response['data']['createWorkspace'];
1501 }
1502 if ( $edit && isset( $response['data'], $response['data']['editWorkspace'] ) ) {
1503 return $response['data']['editWorkspace'];
1504 }
1505 Helper::send_error( __( "Something went wrong regarding $action WorkSpace. Try again or contact with Templately Support.", 'templately' ) );
1506 }
1507
1508 protected function delete_workspace() {
1509 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
1510 if ( empty( $api_key ) ) {
1511 Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1512 }
1513 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : false;
1514 if ( $id === false ) {
1515 Helper::send_error( __( 'Workspace not found.', 'templately' ) );
1516 }
1517 $withFiles = isset( $_POST['withFiles'] ) ? boolval( intval( $_POST['withFiles'] ) ) : false;
1518
1519 $query = sprintf(
1520 'mutation { deleteWorkspace ( id: %s, api_key: "%s" %s ){ status } }',
1521 $id,
1522 $api_key,
1523 $withFiles ? ', delete_files: true' : ''
1524 );
1525 $response = Query::get( $query );
1526 if ( is_wp_error( $response ) ) {
1527 Helper::send_error( $response->get_error_code() . ': ' . $response->get_error_message() );
1528 }
1529 if ( isset( $response['data'], $response['data']['deleteWorkspace'], $response['data']['deleteWorkspace']['status'] ) ) {
1530 return $response['data']['deleteWorkspace']['status'];
1531 }
1532 Helper::send_error( __( 'Something went wrong regarding delete WorkSpace. Try again or contact with Templately Support.', 'templately' ) );
1533 }
1534
1535 protected function copy_or_move() {
1536 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
1537 if ( empty( $api_key ) ) {
1538 Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1539 }
1540 $action = isset( $_POST['trigger_action'] ) ? trim( $_POST['trigger_action'] ) : 'copy';
1541 $cloud_file_id = isset( $_POST['cloud_file_id'] ) ? intval( $_POST['cloud_file_id'] ) : false;
1542 if ( $cloud_file_id === false ) {
1543 Helper::send_error( __( 'No Cloud Item is selected to' . $action . '.', 'templately' ) );
1544 }
1545 $workspace_id = isset( $_POST['workspace_id'] ) ? intval( $_POST['workspace_id'] ) : false;
1546 if ( $workspace_id === false ) {
1547 Helper::send_error( __( 'No WorkSpace is selected, where the item will ' . $action . ' to.', 'templately' ) );
1548 }
1549 $query = sprintf(
1550 'mutation { copyOrMoveToWorkspace ( api_key: "%s", my_cloud_file_id: %s, workspace_id: %s, action: "%s" ){ status, message } }',
1551 $api_key,
1552 $cloud_file_id,
1553 $workspace_id,
1554 $action
1555 );
1556 $response = Query::get( $query );
1557 if ( is_wp_error( $response ) ) {
1558 Helper::send_error( $response->get_error_code() . ': ' . $response->get_error_message() );
1559 }
1560
1561 if ( isset( $response['data'], $response['data']['copyOrMoveToWorkspace'], $response['data']['copyOrMoveToWorkspace']['status'] ) ) {
1562 return $response['data']['copyOrMoveToWorkspace']['status'];
1563 }
1564 Helper::send_error( __( "Something went wrong regarding $action to WorkSpace. Try again or contact with Templately Support.", 'templately' ) );
1565 }
1566
1567 protected function workspace_details() {
1568 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
1569 if ( empty( $api_key ) ) {
1570 Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1571 }
1572 $slug = isset( $_POST['slug'] ) ? trim( $_POST['slug'] ) : false;
1573 if ( $slug === false ) {
1574 Helper::send_error( __( 'Slug can\'t be empty.', 'templately' ) );
1575 }
1576
1577 $file_type = isset( $_POST['file_type'] ) ? trim( $_POST['file_type'] ) : 'elementor';
1578 $search = isset( $_POST['search'] ) ? trim( $_POST['search'] ) : '';
1579 $files_per_page = isset( $_POST['per_page'] ) ? intval( $_POST['per_page'] ) : 10;
1580 $files_page = isset( $_POST['page'] ) ? intval( $_POST['page'] ) : 1;
1581
1582 $query = sprintf(
1583 '{ 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 } }',
1584 $api_key,
1585 $slug,
1586 $files_page,
1587 $files_per_page,
1588 $file_type,
1589 ! empty( $search ) ? ', files_search: "' . $search . '"' : ''
1590 );
1591 $response = Query::get( $query );
1592 if ( is_wp_error( $response ) ) {
1593 Helper::send_error( $response->get_error_code() . ': ' . $response->get_error_message() );
1594 }
1595 if ( isset( $response['data'], $response['data']['workspaceDetails'] ) ) {
1596 return $response['data']['workspaceDetails'];
1597 } else {
1598 if ( isset( $response['data'] ) && ! isset( $response['data']['workspaceDetails'] ) ) {
1599 return null;
1600 }
1601 }
1602 Helper::send_error( __( "Something went wrong regarding loading your WorkSpace. Try again or contact with Templately Support.", 'templately' ) );
1603 }
1604
1605 protected function cloud_usage_limit() {
1606 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
1607 if ( empty( $api_key ) ) {
1608 Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1609 }
1610
1611 $query = sprintf(
1612 '{ myCloudUsage( api_key: "%s" ){ data, status } }',
1613 $api_key
1614 );
1615 $response = Query::get( $query );
1616 if ( is_wp_error( $response ) ) {
1617 Helper::send_error( $response->get_error_code() . ': ' . $response->get_error_message() );
1618 }
1619 if ( isset( $response['data'], $response['data']['myCloudUsage'] ) ) {
1620 return $response['data']['myCloudUsage'];
1621 }
1622 Helper::send_error( __( "Something went wrong regarding collecting your usage limit. Try again or contact with Templately Support.", 'templately' ) );
1623 }
1624
1625 protected function add_files_to_workspace() {
1626 $api_key = DB::get_user_specific_login_meta( '_templately_api_key' );
1627 if ( empty( $api_key ) ) {
1628 Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1629 }
1630
1631 $workspace_id = isset( $_POST['workspace_id'] ) ? intval( $_POST['workspace_id'] ) : false;
1632 if ( $workspace_id === false ) {
1633 Helper::send_error( __( 'Workspace Not Found.', 'templately' ) );
1634 }
1635 $files = isset( $_POST['files'] ) ? trim( $_POST['files'] ) : false;
1636 if ( $files === false ) {
1637 Helper::send_error( __( 'Workspace Not Found.', 'templately' ) );
1638 }
1639 $query = sprintf(
1640 'mutation { addFileToWorkspace( api_key: "%s", workspace_id: %d, files: %s ){ data, status } }',
1641 $api_key,
1642 $workspace_id,
1643 \json_encode( $files )
1644 );
1645 $response = Query::get( $query );
1646 if ( is_wp_error( $response ) ) {
1647 Helper::send_error( $response->get_error_code() . ': ' . $response->get_error_message() );
1648 }
1649 if ( isset( $response['data'], $response['data']['addFileToWorkspace'] ) ) {
1650 return $response['data']['addFileToWorkspace'];
1651 }
1652 Helper::send_error( __( "Something went wrong regarding adding files to your Workspace. Try again or contact with Templately Support.", 'templately' ) );
1653 }
1654
1655 public function get_templates() {
1656 return Elementor::templates( false, [
1657 'page' => ( isset( $_POST['page'] ) ? intval( $_POST['page'] ) : 1 )
1658 ] );
1659 }
1660 }
1661