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

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