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

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