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

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