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

1,386 lines 60.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately;
4
5 use Elementor\Plugin;
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 // $error_code = $data->get_error_code();
379 Helper::send_error( __( 'Something went wrong: ' . $error_message, 'templately' ) );
380 }
381 if( $origin === 'remote' ) {
382 if( isset( $data['data'], $data['data']['itemContent'], $data['data']['itemContent']['status'] ) ) {
383 if( $data['data']['itemContent']['status'] == 'error' ) {
384 Helper::send_error( $data['data']['itemContent']['message'] );
385 }
386 }
387 if( isset( $data['data'], $data['data']['itemContent'], $data['data']['itemContent']['data'] ) ) {
388 $data = json_decode( $data['data']['itemContent']['data'], true );
389 return $data;
390 }
391 }
392 if( $origin === 'cloud' ) {
393 $data = json_decode( $data['data']['myCloudInsert'], true );
394 return $data;
395 }
396 if( $import ) {
397 return null;
398 }
399 Helper::send_error( __( 'Something went wrong!', 'templately' ) );
400 }
401 /**
402 * Item will come from local WP Installation.
403 */
404 if( $origin === 'local' ) {
405 $data = Query::getFromLibrary( $id );
406 if( isset( $data['content'] ) ) {
407 return $data;
408 }
409 }
410 Helper::send_error( __( 'Something went wrong!', 'templately' ) );
411 }
412 /**
413 * This Method is responsible for Inserting Template
414 * @param string $origin
415 * @return void
416 */
417 public function insert_template( $origin = 'remote' ){
418 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : null;
419 if( is_null( $id ) ) {
420 Helper::send_error( __( 'Templates ID Not Found.', 'templately' ) );
421 }
422 $default_args = array(
423 'editor_post_id' => false,
424 'item_id' => $id,
425 'origin' => $origin
426 );
427 if( isset( $_POST['file_type'] ) && ! empty( $_POST['file_type'] )) {
428 $default_args['file_type'] = sanitize_text_field( $_POST['file_type'] );
429 }
430 if( isset( $default_args['file_type'] ) && $default_args['file_type'] === 'gutenberg' ) {
431 $template = self::get_template_content( $default_args );
432 }
433 if( isset( $default_args['file_type'] ) && $default_args['file_type'] === 'elementor' ) {
434 $importer = new Importer;
435 $template = $importer->get_data( $default_args );
436 }
437 return isset( $template['content'] ) ? $template : Helper::send_error( __( 'There is no Template Content to be inserted.', 'templately' ) );
438 }
439 private function cloud_push( $name, $data, $item_type = 'elementor', $args = array() ){
440 if( empty( $name ) ) {
441 Helper::send_error( __( "Name can't be empty.", 'templately' ) );
442 }
443 if( $item_type === 'elementor' ) {
444 if( empty( $data ) || ! is_array( $data ) ) {
445 Helper::send_error( __( "Data has to be an array.", 'templately' ) );
446 }
447 if( ! isset( $data['content'] ) || ( isset( $data['content'] ) && empty( $data['content'] ) ) ) {
448 Helper::send_error( __( "There is no data to insert.", 'templately' ) );
449 }
450 }
451 $api_key = DB::get_option('_templately_api_key', false);
452 if( empty( $api_key ) ) {
453 Helper::send_error( __( "API Key is not found, try to re-login.", 'templately' ) );
454 }
455 if( $item_type === 'elementor' ) {
456 $data = wp_slash( \json_encode(
457 $data, JSON_UNESCAPED_LINE_TERMINATORS | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE
458 ) );
459 }
460
461 $response = Query::push( $name, $data, $api_key, $item_type, $args );
462 if( is_wp_error( $response ) ) {
463 $error_message = $response->get_error_message();
464 Helper::send_error( $error_message );
465 }
466 if( isset( $response['errors'] ) ) {
467 Helper::send_error( $response['errors'][0]['message'] );
468 }
469
470 $cloud_item = json_decode( $response['data']['pushToMyCloud']['data'] );
471 if( isset( $cloud_item->myCloud_item ) ) {
472 $cloud_map = DB::get_option('_templately_cloud_map', []);
473 if( ! isset( $cloud_map[ $id ] ) ) {
474 $cloud_map[] = array(
475 'template_id' => $id,
476 'cloud_item_id' => intval( $cloud_item->myCloud_item ),
477 );
478 }
479 DB::update_option('_templately_cloud_map', $cloud_map);
480 }
481 return $response['data']['pushToMyCloud'];
482 }
483 /**
484 * Push to Cloud
485 *
486 * @param integer $id
487 * @return void
488 */
489 private function push_to_cloud( $id = null, $item_type = 'elementor' ){
490 $args = array();
491 if( isset( $_POST['workspace_id'] ) ) {
492 $args['workspace_id'] = intval( $_POST['workspace_id'] );
493 }
494
495 if( $item_type === 'elementor' ) {
496 if( $id === null ) {
497 Helper::send_error( __( 'ID not found to push the item in cloud.', 'templately' ) );
498 }
499 $data = Query::getFromLibrary( $id );
500 if( ! empty( $data ) && isset( $data['content'] ) && ! empty( $data['content'] ) ) {
501 $name = \get_the_title( $id );
502 return $this->cloud_push( $name, $data, $item_type, $args );
503 }
504 }
505
506 if( $item_type === 'gutenberg' ) {
507 $name = isset( $_POST[ 'title' ] ) && ! empty( $_POST[ 'title' ] ) ? sanitize_text_field( $_POST[ 'title' ] ) : '';
508 $data = isset( $_POST[ 'data' ] ) && ! empty( $_POST[ 'data' ] ) ? $_POST[ 'data' ] : '';
509 return $this->cloud_push( $name, $data, 'gutenberg', $args );
510 }
511
512 Helper::send_error( __( 'Something went wrong', 'templately' ) );
513 }
514 /**
515 * This method is responsible for Removing item from Cloud
516 * @param integer $id
517 * @return void
518 */
519 private function remove_from_cloud( $id = null ){
520 if( is_null( $id ) ) {
521 Helper::send_error( __( 'ID not found to remove Template from Cloud.', 'templately' ) );
522 }
523 $api_key = DB::get_option('_templately_api_key', false);
524 if( ! $api_key ) {
525 Helper::send_error( __( 'You have no rights to delete file from Cloud. You have to logged in first', 'templately' ) );
526 }
527
528 $from = isset( $_POST['remove_from'] ) && ! empty( $_POST['remove_from'] ) ? trim( $_POST['remove_from'] ) : 'cloud';
529 $response = Query::remove_cloud_item( $id, $api_key, $from );
530
531 if( is_wp_error( $response ) ) {
532 $error_message = $response->get_error_message();
533 Helper::send_error( $error_message );
534 }
535
536 if( $from === 'cloud' ) {
537 $response = $response['data']['removeFromMyCloud'];
538 } else {
539 $response = $response['data']['deleteWorkspaceFile'];
540 }
541
542 if( isset( $response['status'] ) && $response['status'] != 'success' ) {
543 Helper::send_error( __( 'Something went wrong.', 'templately' ) );
544 }
545
546 if( $from === 'cloud' ) {
547 $cloud_map = DB::get_option('_templately_cloud_map', []);
548 if( ! empty( $cloud_map ) ) {
549 $new_cloud_map = array_filter( $cloud_map, function( $value ) use( $id ) {
550 return $value['cloud_item_id'] != $id;
551 });
552 DB::update_option( '_templately_cloud_map', array_values( $new_cloud_map ) );
553 }
554
555 $users_data = DB::get_option('_templately_connect_data', []);
556 if( array_key_exists( 'clouds', $users_data ) && array_key_exists( 'files', $users_data['clouds'] ) ) {
557 $new_users_data = array_filter( $users_data['clouds']['files'], function( $value ) use( $id ) {
558 return $value['id'] != $id;
559 });
560 $users_data['clouds']['files'] = array_values( $new_users_data );
561 DB::update_option( '_templately_connect_data', $users_data );
562 }
563
564 return $id;
565 }
566
567 return $response;
568 }
569 /**
570 * Template Export
571 *
572 * @param integer $id
573 * @return void
574 */
575 public function template_export( $id = null ){
576 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : null;
577 if( $id === null ) {
578 Helper::send_error( __( 'Something went wrong.' , 'templately') );
579 }
580 return Plugin::$instance->templates_manager->export_template( array(
581 'source' => 'local',
582 'template_id' => $id
583 ) );
584 }
585 /**
586 * Check Dependency for Items
587 * @return void
588 */
589 protected function check_dependency(){
590 $dependencies = json_decode( wp_unslash( $_POST['dependency'] ) );
591 $not_active_plugin_list = [];
592 if( ! empty( $dependencies ) && is_array( $dependencies ) ) {
593 foreach( $dependencies as $dependency ) {
594 if( is_null( $dependency->slug ) ) {
595 continue;
596 }
597 if( ! \is_plugin_active( $dependency->slug ) ) {
598 $not_active_plugin_list[] = $dependency->name;
599 }
600 }
601 }
602 Helper::send_error( array(
603 'dependency' => $not_active_plugin_list
604 ) );
605 }
606 /**
607 * This Method is responsible for Get Item from Server.
608 * @return void
609 */
610 protected function get_item(){
611 $slug = isset( $_POST[ 'slug' ] ) ? trim( sanitize_text_field( $_POST[ 'slug' ] ) ) : false;
612 if( ! $slug ) {
613 Helper::send_error( __('Something went wrong.', 'templately') );
614 }
615 $type = isset( $_POST[ 'type' ] ) ? trim( sanitize_text_field( $_POST[ 'type' ] ) ) : 'block';
616
617 $query = '';
618 if( $type === 'block' ) {
619 $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 } } } }';
620 }
621 if( $type === 'pack' ) {
622 $query = '{
623 packs( slug: "'. $slug .'" ) {
624 data { name, id, slug, rating, downloads, favourite_count, thumbnail, price, author{ display_name, name, profile_photo, joined }, tags{ name }, category{ name },
625 items {
626 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 } } }
627 }
628 }';
629 }
630
631 if( $type === 'page' ) {
632 $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 } } } }';
633 }
634
635 $data = Query::get( $query );
636 if( \is_wp_error( $data ) ) {
637 $error_message = $data->get_error_message();
638 Helper::send_error( $error_message );
639 }
640 if( $type === 'block' ) {
641 if( isset( $data['data'], $data['data']['items'] ) ) {
642 return $data['data']['items']['data'];
643 }
644 }
645 if( $type === 'page' ) {
646 if( isset( $data['data'], $data['data']['pages'] ) ) {
647 return $data['data']['pages']['data'];
648 }
649 }
650 if( $type === 'pack' ) {
651 if( isset( $data['data'], $data['data']['packs'] ) ) {
652 return $data['data']['packs']['data'];
653 }
654 }
655 Helper::send_error( __('Something went wrong!', 'templately') );
656 }
657 /**
658 * This Methods Helps to Import Template in Library
659 * @return void
660 */
661 public function import_to_library(){
662 $id = isset( $_POST[ 'id' ] ) ? intval( $_POST[ 'id' ] ) : false;
663 if( ! $id ) {
664 Helper::send_error( __('Something went wrong!', 'templately') );
665 }
666 $title = isset( $_POST[ 'title' ] ) ? trim( sanitize_text_field( $_POST[ 'title' ] ) ) : '';
667
668 $importer = new Importer;
669 $template = $importer->get_data( array(
670 'editor_post_id' => false,
671 'item_id' => $id
672 ) );
673 $imported_template = $importer->import_templately( $template );
674 if( ! is_wp_error( $imported_template ) || ! is_array( $imported_template ) ) {
675 return array(
676 'post_id' => $imported_template['template_id'],
677 'edit_link' => get_edit_post_link( $imported_template['template_id'], 'internal' ),
678 'elementor_edit_link' => Plugin::$instance->documents->get( $imported_template['template_id'] )->get_edit_url(),
679 'visit' => $imported_template['url']
680 );
681 }
682 Helper::send_error( __('Something went wrong with Import!', 'templately') );
683 }
684 /**
685 * This Method is Responsible for Create A Page From a Template.
686 * also its used to Import Template
687 * @param boolean $is_import
688 * @return void
689 */
690 public function create_page(){
691 $id = isset( $_POST[ 'id' ] ) ? intval( $_POST[ 'id' ] ) : false;
692 if( ! $id ) {
693 Helper::send_error( __('Something went wrong!', 'templately') );
694 }
695 $title = isset( $_POST[ 'title' ] ) ? trim( sanitize_text_field( $_POST[ 'title' ] ) ) : '';
696 $platform = isset( $_POST[ 'platform' ] ) ? trim( $_POST[ 'platform' ] ) : 'elementor';
697 if( $platform === 'elementor' ) {
698 $importer = new Importer;
699 $template_data = $importer->get_data( array(
700 'editor_post_id' => false,
701 'item_id' => $id
702 ) );
703 if( ! empty( $title ) ) {
704 $template_data['page_title'] = $title;
705 }
706 $inserted_ID = $importer->create_page( $template_data );
707 }
708
709 if( $platform === 'gutenberg' ) {
710 $inserted_ID = self::get_template_content( array(
711 'origin' => 'remote',
712 'item_id' => $id,
713 ) );
714
715 if( isset( $inserted_ID['content'] ) ) {
716 $inserted_ID = wp_insert_post( array(
717 'post_status' => 'draft',
718 'post_type' => 'page',
719 'post_title' => $title,
720 'post_content' => $inserted_ID['content'],
721 ) );
722 }
723 }
724
725 if ( $inserted_ID && ! is_wp_error( $inserted_ID ) ) {
726 return array(
727 'post_id' => $inserted_ID,
728 'edit_link' => get_edit_post_link( $inserted_ID, 'internal' ),
729 'elementor_edit_link' => $platform === 'elementor' ? Plugin::$instance->documents->get( $inserted_ID )->get_edit_url() : false,
730 'visit' => get_permalink( $inserted_ID )
731 );
732 }
733 Helper::send_error( __('Something went wrong!', 'templately') );
734 }
735 /**
736 * This Method is responsible for Check User Exists or Not and Make them Logged in to Server
737 * to collect profiles data and the API Key.
738 * @param boolean $login
739 * @return void
740 */
741 public function login_user(){
742 $errors = [];
743 $withApi = isset( $_POST['withApi'] ) ? trim( $_POST['withApi'] ) === 'true' : false;
744 if( $withApi ) {
745 $api_key = sanitize_text_field( trim( $_POST['api_key'] ) );
746 if( empty( $api_key ) ) {
747 $errors['api_key'] = __( 'API Key Field Cannot be Empty.', 'templately' );
748 }
749 $query = \sprintf(
750 '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 }}}',
751 $api_key,
752 \home_url('/'),
753 self::get_ip()
754 );
755 } else {
756 $email = sanitize_text_field( trim( $_POST['email'] ) );
757 if( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
758 $errors['email'] = __('Make sure you have given a valid email address.', 'templately');
759 }
760 $password = sanitize_text_field( trim( $_POST['password'] ) );
761 if( empty( $password ) ) {
762 $errors['password'] = __( 'Password Field Cannot be Empty.', 'templately' );
763 }
764 $query = \sprintf(
765 '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 }}}}}',
766 $email,
767 $password,
768 \home_url('/'),
769 self::get_ip()
770 );
771 }
772 $api_response = Query::get( $query );
773 if( is_wp_error( $api_response ) ) {
774 $error_message = $api_response->get_error_message();
775 Helper::send_error( __('Something went wrong: ' . $error_message, 'templately') );
776 }
777 if( $withApi ) {
778 $api_response = $api_response['data']['connectWithApiKey'];
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 } else {
784 $api_response = $api_response['data']['connect'];
785 if( isset( $api_response['status'] ) && $api_response['status'] !== 'success' ) {
786 Helper::send_error( $api_response['message'] );
787 }
788 return $this->user_data( $api_response );
789 }
790 Helper::send_error( __('Something went wrong', 'templately') );
791 }
792 public function create_user(){
793 $errors = [];
794 $first_name = sanitize_text_field( trim( $_POST['first_name'] ) );
795 $last_name = sanitize_text_field( trim( $_POST['last_name'] ) );
796 if( empty( $first_name ) ) {
797 $errors['first_name'] = __( 'First Name Can\'t be Empty.', 'templately' );
798 }
799 if( empty( $last_name ) ) {
800 $errors['last_name'] = __( 'Last Name Can\'t be Empty.', 'templately' );
801 }
802 $email = sanitize_text_field( trim( $_POST['email'] ) );
803 if( empty( $email ) ) {
804 $errors['email'] = __( 'Email Field Can\'t be Empty.', 'templately' );
805 }
806 if( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
807 $errors['email'] = __('Make sure you have given a valid email address.', 'templately');
808 }
809 $password = sanitize_text_field( trim( $_POST['password'] ) );
810 if( empty( $password ) ) {
811 $errors['password'] = __( 'Password Field Can\'t be Empty.', 'templately' );
812 }
813 if( ! empty( $errors ) ) {
814 Helper::send_error( $errors );
815 }
816
817 $query = \sprintf(
818 '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}}}',
819 $first_name,
820 $last_name,
821 $email,
822 $password,
823 \home_url('/'),
824 self::get_ip()
825 );
826 $api_response = Query::get( $query );
827 if( is_wp_error( $api_response ) ) {
828 $error_message = $api_response->get_error_message();
829 Helper::send_error( __('Something went wrong: ' . $error_message, 'templately') );
830 }
831 $api_response = $api_response['data']['createUser'];
832 if( isset( $api_response['status'] ) && $api_response['status'] !== 'success' ) {
833 Helper::send_error( $api_response['message'] );
834 }
835 return $this->user_data( $api_response, true );
836 }
837
838 /**
839 * Helper Function for checkNVerify methods
840 * @param array $api_response
841 * @return void
842 */
843 protected function user_data( $api_response, $create_user = false, $raw = false ){
844 if( isset( $api_response ) && ! is_null( $api_response ) ) {
845 $user_data = $api_response;
846 $userState = [];
847 if( isset( $user_data['user'], $user_data['user']['favourites'] ) ) {
848 $favourites = [
849 'item' => [],
850 'pack' => []
851 ];
852 array_walk( $user_data['user']['favourites'], function( $value ) use ( &$favourites ) {
853 if( ! is_null( $value ) ) {
854 if( ! in_array( $value['id'], isset( $favourites[ $value['type'] ] ) ? $favourites[ $value['type'] ] : [] ) ) {
855 $favourites[ $value['type'] ][] = $value['id'];
856 }
857 }
858 });
859 $userState['favourites'] = $favourites;
860 }
861
862 if( isset( $user_data['user'], $user_data['user']['id'] ) ) {
863 $userState['id'] = intval( $user_data['user']['id'] );
864 }
865 if( isset( $user_data['user'], $user_data['user']['first_name'] ) ) {
866 $userState['first_name'] = sanitize_text_field( $user_data['user']['first_name'] );
867 }
868 if( isset( $user_data['user'], $user_data['user']['profile_photo'] ) ) {
869 $userState['profile_photo'] = sanitize_text_field( $user_data['user']['profile_photo'] );
870 }
871 if( isset( $user_data['user'], $user_data['user']['last_name'] ) ) {
872 $userState['last_name'] = sanitize_text_field( $user_data['user']['last_name'] );
873 }
874 if( isset( $user_data['user'], $user_data['user']['name'] ) ) {
875 $userState['name'] = sanitize_text_field( $user_data['user']['name'] );
876 }
877 if( isset( $user_data['user'], $user_data['user']['email'] ) ) {
878 $userState['email'] = sanitize_text_field( $user_data['user']['email'] );
879 }
880 if( isset( $user_data['user'], $user_data['user']['plan'] ) ) {
881 $userState['plan'] = sanitize_text_field( $user_data['user']['plan'] );
882 }
883 if( isset( $user_data['user'], $user_data['user']['plan_expire_at'] ) ) {
884 $userState['plan_expire_at'] = sanitize_text_field( $user_data['user']['plan_expire_at'] );
885 }
886 if( isset( $user_data['user'], $user_data['user']['joined'] ) ) {
887 $userState['joined'] = sanitize_text_field( $user_data['user']['joined'] );
888 }
889 if( isset( $user_data['status'] ) ) {
890 $userState['status'] = sanitize_text_field( $user_data['status'] );
891 }
892 $api_key = '';
893 if( isset( $user_data['user'], $user_data['user']['api_key'] ) ) {
894 $api_key = sanitize_text_field( $user_data['user']['api_key'] );
895 }
896 if( isset( $user_data['user'], $user_data['user']['my_cloud'] ) ) {
897 $user_clouds['limit'] = $user_data['user']['my_cloud']['limit'];
898 $user_clouds['usages'] = $user_data['user']['my_cloud']['usages'];
899 $user_clouds['name'] = $user_data['user']['my_cloud']['name'];
900 $user_cloud_files = [];
901 array_walk( $user_data['user']['my_cloud']['files'], function( $value ) use ( &$user_cloud_files ) {
902 //TODO: mycloud
903 });
904 $user_clouds['files'] = $user_cloud_files;
905 $userState['clouds'] = $user_clouds;
906 }
907 if( empty( $api_key ) ) {
908 Helper::send_error( __( 'API Key is not found', 'templately' ) );
909 }
910
911 if( $create_user ) {
912 $userState['is_verified'] = false;
913 }
914
915 DB::update_option( '_templately_api_key', $api_key );
916 DB::update_option( '_templately_connected', true );
917 DB::update_option( '_templately_connect_data', $userState );
918
919 if( $raw ) {
920 return $userState;
921 }
922 Helper::send_success(array(
923 'message' => __( 'Successfully Connected', 'templately' ),
924 'admin' => $userState
925 ));
926 }
927 }
928 /**
929 * This method delete templates from library.
930 * @return void
931 */
932 protected function delete_template(){
933 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : false;
934 if( ! $id ) {
935 Helper::send_error( __('Template ID is not found to delete', 'templately' ) );
936 }
937 // $type = isset( $_POST['type'] ) ? trim( $_POST['type'] ) : 'template';
938 \wp_delete_post( $id );
939 $cloud_map = DB::get_option('_templately_cloud_map', []);
940 $new_cloud_map = [];
941 array_walk( $cloud_map, function( $cloud ) use( &$cloud_map, $id, &$new_cloud_map ) {
942 if( $cloud[ 'template_id'] !== $id ) {
943 $new_cloud_map[] = $cloud;
944 }
945 });
946 DB::update_option( '_templately_cloud_map', $new_cloud_map );
947 Helper::send_success( __('Successfully Deleted.', 'templately') );
948 }
949 /**
950 * This method helps to sync user profile data to current one.
951 * @return void
952 */
953 protected function clouds_sync(){
954 $api_key = DB::get_option( '_templately_api_key', false );
955 if( ! empty( $api_key ) ) {
956 $file_type = isset( $_POST[ 'fileType' ] ) ? trim( $_POST['fileType'] ) : 'elementor';
957 $search = isset( $_POST[ 'search' ] ) ? trim( $_POST['search'] ) : '';
958 $per_page = isset( $_POST[ 'per_page' ] ) ? intval( $_POST['per_page'] ) : 12;
959 $pagenum = isset( $_POST[ 'page' ] ) ? intval( $_POST['page'] ) : 1;
960
961 $query = \sprintf(
962 '{ myCloud( api_key: "%s", cloud_files_page: %s, cloud_files_per_page: %s, file_type: "%s"%s ) { limit, usages, name, files { data{ id, name, thumbnail, file_type, created_at }, current_page, total_page } } }',
963 $api_key,
964 $pagenum,
965 $per_page,
966 $file_type,
967 ! empty( $search ) ? ', files_search: "'. $search .'"' : ''
968 );
969 $data = Query::get( $query );
970 if( is_wp_error( $data ) ) {
971 $error_message = $data->get_error_message();
972 Helper::send_error( $error_message );
973 }
974 if( isset( $data['data'], $data['data']['myCloud'] ) ) {
975 $user_data = DB::get_option( '_templately_connect_data', false );
976 $user_data['clouds'] = $data['data']['myCloud'];
977 DB::update_option( '_templately_connect_data', $user_data );
978 return $data['data']['myCloud']['files'];
979 } else {
980 // TODO: in future will be handled.
981 }
982 }
983 Helper::send_error( __( 'You have logged in first via your admin dashboard.', 'templately' ) );
984 }
985 /**
986 * This Method is responsible for Disconnect user.
987 * @return void
988 */
989 protected function logout(){
990 DB::delete_option( '_templately_trigger' );
991 DB::delete_option( '_templately_api_key' );
992 DB::delete_option( '_templately_connected' );
993 DB::delete_option( '_templately_connect_data' );
994 DB::delete_option( '_templately_connect_email' );
995 Helper::send_success( __('Logged Out', 'templately') );
996 }
997 /**
998 * Verification Done AJAX Call.
999 */
1000 protected function verification_done(){
1001 $data = DB::get_option( '_templately_connect_data' );
1002 if( isset( $data[ 'is_verified' ] ) && $data[ 'is_verified' ] == false ) {
1003 unset( $data[ 'is_verified' ] );
1004 }
1005 return DB::update_option( '_templately_connect_data', $data );
1006 }
1007 /**
1008 * Platform Exists or Not
1009 *
1010 * @return void
1011 */
1012 protected function platform_exists(){
1013 $platform = isset( $_POST['platform'] ) ? trim( sanitize_text_field( $_POST['platform'] ) ) : 'elementor';
1014 if( $platform === 'gutenberg' ) {
1015 $wp_version = get_bloginfo( 'version' );
1016 if( version_compare( $wp_version, '5.0.0', '>=' ) ) {
1017 return true;
1018 } else {
1019 if( \is_plugin_active( 'gutenberg/gutenberg.php' ) ) {
1020 return true;
1021 } else {
1022 return false;
1023 }
1024 }
1025 }
1026
1027 if( $platform === 'elementor' ) {
1028 if( \is_plugin_active( 'elementor/elementor.php' ) ) {
1029 return true;
1030 } else {
1031 return false;
1032 }
1033 }
1034
1035 return false;
1036 }
1037 protected function set_favourite( $unfav = false ){
1038 $api_key = DB::get_option('_templately_api_key', false);
1039 if( ! $api_key ) {
1040 Helper::send_error( __('Something went wrong. Please, try logged in again.', 'templately') );
1041 }
1042 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : false;
1043 $type = isset( $_POST['type'] ) ? trim( sanitize_text_field( $_POST['type'] ) ) : false;
1044 if( $id === false || $type === false ) {
1045 Helper::send_error( __('Item ID or Item Type is not found.', 'templately') );
1046 }
1047
1048 $type = $type === 'block' || $type === 'page' ? 'item' : 'pack';
1049 $query = 'mutation { favourite( type_id: '. $id .', api_key: "'. $api_key .'", type: "'. $type .'" ){ status, message, data } }';
1050 if( $unfav ) {
1051 $query = 'mutation { unFavourite( type_id: '. $id .', api_key: "'. $api_key .'", type: "'. $type .'" ) }';
1052 }
1053 $response = Query::get( $query );
1054 if( is_wp_error( $response ) ) {
1055 $error_message = $response->get_error_message();
1056 Helper::send_error( $error_message );
1057 }
1058 if( isset( $response['data']['favourite'] ) ) {
1059 $response = $response['data']['favourite'];
1060 if( isset( $response[ 'status' ] ) && $response[ 'status' ] != 'success' ) {
1061 Helper::send_error( $response[ 'message' ] );
1062 }
1063
1064 $user_data = DB::get_option('_templately_connect_data', []);
1065 if( ! empty( $user_data ) && array_key_exists( 'favourites', $user_data ) ) {
1066 if( isset( $user_data['favourites'][ $type ] ) ) {
1067 if( ! in_array( $id, $user_data['favourites'][ $type ] ) ) {
1068 $user_data['favourites'][ $type ][] = $id;
1069 }
1070 } else {
1071 $user_data['favourites'][ $type ] = [];
1072 $user_data['favourites'][ $type ][] = $id;
1073 }
1074 } else {
1075 $user_data['favourites'] = [];
1076 $user_data['favourites'][ $type ] = [];
1077 $user_data['favourites'][ $type ][] = $id;
1078 }
1079 DB::update_option( '_templately_connect_data', $user_data );
1080 return $user_data;
1081 }
1082 if( $unfav && isset( $response['data']['unFavourite'] ) && $response['data']['unFavourite'] ) {
1083 $user_data = DB::get_option('_templately_connect_data', []);
1084 if( $unfav && isset( $user_data['favourites'][ $type ] ) && is_array( $user_data['favourites'][ $type ] ) ) {
1085 $favourites = array_filter( $user_data['favourites'][ $type ], function( $value ) {
1086 return $value == $id;
1087 } );
1088 $user_data['favourites'][ $type ] = array_values( $favourites );
1089 DB::update_option( '_templately_connect_data', $user_data );
1090 }
1091 return $user_data;
1092 }
1093 Helper::send_error( __( 'Something went wrong.', 'templately' ) );
1094 }
1095
1096 protected function get_workspace(){
1097 $api_key = DB::get_option('_templately_api_key', false);
1098 if( empty( $api_key ) ) {
1099 Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1100 }
1101
1102 $shared = isset( $_POST['shared'] ) ? boolval( $_POST['shared'] ) : false;
1103 $pagenum = isset( $_POST['pagenum'] ) ? intval( $_POST['pagenum'] ) : 1;
1104 $file_type = isset( $_POST['file_type'] ) ? trim( $_POST['file_type'] ) : 'elementor';
1105 $per_page = isset( $_POST['per_page'] ) ? intval( $_POST['per_page'] ) : 11;
1106
1107 $query = sprintf(
1108 '{ %s ( page: %s, per_page: %s, api_key: "%s", file_type: "%s" ){ data{ id, name, slug %s }, total_page, current_page } }',
1109 $shared ? 'sharedWorkspaces' : 'workspaces',
1110 $pagenum,
1111 $per_page,
1112 $api_key,
1113 $file_type,
1114 $pagenum !== -1 ? ', sharedWith{ name, email, profile_photo, id }, owner{ id, name, joined, display_name }, pending_invitations' : ''
1115 );
1116 $response = Query::get( $query );
1117 if( is_wp_error( $response ) ) {
1118 Helper::send_error( __( 'Something went wrong loading WorkSpace. Try again reloading.', 'templately' ) );
1119 }
1120
1121 if( isset( $response['data'], $response['data']['workspaces'] ) && ! $shared ) {
1122 return $response['data']['workspaces'];
1123 }
1124
1125 if( isset( $response['data'], $response['data']['sharedWorkspaces'] ) && $shared ) {
1126 return $response['data']['sharedWorkspaces'];
1127 }
1128
1129
1130 Helper::send_error( __( 'Something went wrong loading WorkSpace. Try again or contact with Templately Support.', 'templately' ) );
1131 }
1132
1133 protected function create_workspace(){
1134 $api_key = DB::get_option('_templately_api_key', false);
1135 if( empty( $api_key ) ) {
1136 Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1137 }
1138 $title = isset( $_POST['title'] ) ? trim( $_POST['title'] ) : false;
1139 if( $title === false ) {
1140 Helper::send_error( __( 'You have to give title at least.', 'templately' ) );
1141 }
1142 $share_with = isset( $_POST['share_with'] ) ? trim( $_POST['share_with'] ) : '';
1143
1144 $query = sprintf(
1145 '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 } } }',
1146 $api_key,
1147 $title,
1148 $share_with
1149 );
1150 $response = Query::get( $query );
1151 if( is_wp_error( $response ) ) {
1152 Helper::send_error( __( 'Something went wrong regarding create WorkSpace. Try again.', 'templately' ) );
1153 }
1154
1155 if( isset( $response['data'], $response['data']['createWorkspace'] ) ) {
1156 return $response['data']['createWorkspace'];
1157 }
1158 Helper::send_error( __( 'Something went wrong regarding create WorkSpace. Try again or contact with Templately Support.', 'templately' ) );
1159 }
1160
1161 protected function edit_workspace(){
1162 $api_key = DB::get_option('_templately_api_key', false);
1163 if( empty( $api_key ) ) {
1164 Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1165 }
1166 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : false;
1167 if( $id === false ) {
1168 Helper::send_error( __( 'Workspace not found.', 'templately' ) );
1169 }
1170
1171 $name = ( isset( $_POST['name'] ) && ! empty( $_POST['name'] ) ) ? trim( $_POST['name'] ) : '';
1172 $share_with = ( isset( $_POST['share_with'] ) && ! empty( $_POST['share_with'] ) ) ? trim( $_POST['share_with'] ) : '';
1173 $remove = ( isset( $_POST['remove'] ) && ! empty( $_POST['remove'] ) ) ? trim( $_POST['remove'] ) : '';
1174
1175 $query = sprintf(
1176 'mutation { editWorkspace ( id: %s, api_key: "%s", name: "%s", share_with: "%s", remove: "%s" ){ status } }',
1177 $id,
1178 $api_key,
1179 $name,
1180 $share_with,
1181 $remove
1182 );
1183 $response = Query::get( $query );
1184 if( is_wp_error( $response ) ) {
1185 Helper::send_error( __( 'Something went wrong regarding edit WorkSpace. Try again.', 'templately' ) );
1186 }
1187
1188 if( isset( $response['data'], $response['data']['editWorkspace'], $response['data']['editWorkspace']['data'] ) ) {
1189 return \json_decode( $response['data']['editWorkspace']['data'] );
1190 }
1191
1192
1193 Helper::send_error( __( 'Something went wrong regarding edit WorkSpace. Try again or contact with Templately Support.', 'templately' ) );
1194 }
1195
1196 protected function create_or_edit_workspace( $edit = false ){
1197 $api_key = DB::get_option('_templately_api_key', false);
1198 if( empty( $api_key ) ) {
1199 Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1200 }
1201 $title = isset( $_POST['title'] ) ? trim( $_POST['title'] ) : false;
1202 if( $title === false ) {
1203 Helper::send_error( __( 'You have to give title at least.', 'templately' ) );
1204 }
1205 $share_with = isset( $_POST['share_with'] ) ? trim( $_POST['share_with'] ) : '';
1206
1207 $query = sprintf(
1208 '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 } } }',
1209 $api_key,
1210 $title,
1211 \wp_slash( \json_encode( \explode(',', $share_with ) ) )
1212 );
1213 $action = 'create';
1214 if( $edit ) {
1215 $action = 'edit';
1216 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : false;
1217 if( $id === false ) {
1218 Helper::send_error( __( 'Workspace not found.', 'templately' ) );
1219 }
1220 $remove = ( isset( $_POST['remove'] ) && ! empty( $_POST['remove'] ) ) ? trim( $_POST['remove'] ) : '';
1221 $query = sprintf(
1222 '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 } } }',
1223 $api_key,
1224 $title,
1225 $id,
1226 ! empty( $share_with ) ? 'share_with: "'. \wp_slash( \json_encode( \explode(',', $share_with ) ) ) .'"' : '',
1227 ! empty( $remove ) ? ', remove: "'. $remove .'"' : ''
1228 );
1229 }
1230 $response = Query::get( $query );
1231 if( is_wp_error( $response ) ) {
1232 Helper::send_error( __( "Something went wrong regarding $action WorkSpace. Try again.", 'templately' ) );
1233 }
1234
1235 if( $edit === false && isset( $response['data'], $response['data']['createWorkspace'] ) ) {
1236 return $response['data']['createWorkspace'];
1237 }
1238 if( $edit && isset( $response['data'], $response['data']['editWorkspace'] ) ) {
1239 return $response['data']['editWorkspace'];
1240 }
1241 Helper::send_error( __( "Something went wrong regarding $action WorkSpace. Try again or contact with Templately Support.", 'templately' ) );
1242 }
1243
1244 protected function delete_workspace(){
1245 $api_key = DB::get_option('_templately_api_key', false);
1246 if( empty( $api_key ) ) {
1247 Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1248 }
1249 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : false;
1250 if( $id === false ) {
1251 Helper::send_error( __( 'Workspace not found.', 'templately' ) );
1252 }
1253 $withFiles = isset( $_POST['withFiles'] ) ? boolval( intval( $_POST['withFiles'] ) ) : false;
1254
1255 $query = sprintf(
1256 'mutation { deleteWorkspace ( id: %s, api_key: "%s" %s ){ status } }',
1257 $id,
1258 $api_key,
1259 $withFiles ? ', delete_files: true' : ''
1260 );
1261 $response = Query::get( $query );
1262 if( is_wp_error( $response ) ) {
1263 Helper::send_error( __( 'Something went wrong regarding delete WorkSpace. Try again.', 'templately' ) );
1264 }
1265 if( isset( $response['data'], $response['data']['deleteWorkspace'], $response['data']['deleteWorkspace']['status'] ) ) {
1266 return $response['data']['deleteWorkspace']['status'];
1267 }
1268 Helper::send_error( __( 'Something went wrong regarding delete WorkSpace. Try again or contact with Templately Support.', 'templately' ) );
1269 }
1270 protected function copy_or_move(){
1271 $api_key = DB::get_option('_templately_api_key', false);
1272 if( empty( $api_key ) ) {
1273 Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1274 }
1275 $action = isset( $_POST['trigger_action'] ) ? trim( $_POST['trigger_action'] ) : 'copy';
1276 $cloud_file_id = isset( $_POST['cloud_file_id'] ) ? intval( $_POST['cloud_file_id'] ) : false;
1277 if( $cloud_file_id === false ) {
1278 Helper::send_error( __( 'No Cloud Item is selected to' . $action . '.', 'templately' ) );
1279 }
1280 $workspace_id = isset( $_POST['workspace_id'] ) ? intval( $_POST['workspace_id'] ) : false;
1281 if( $workspace_id === false ) {
1282 Helper::send_error( __( 'No WorkSpace is selected, where the item will ' . $action . ' to.', 'templately' ) );
1283 }
1284 $query = sprintf(
1285 'mutation { copyOrMoveToWorkspace ( api_key: "%s", my_cloud_file_id: %s, workspace_id: %s, action: "%s" ){ status, message } }',
1286 $api_key,
1287 $cloud_file_id,
1288 $workspace_id,
1289 $action
1290 );
1291 $response = Query::get( $query );
1292 if( is_wp_error( $response ) ) {
1293 Helper::send_error( __( "Something went wrong regarding $action to WorkSpace. Try again.", 'templately' ) );
1294 }
1295
1296 if( isset( $response['data'], $response['data']['copyOrMoveToWorkspace'], $response['data']['copyOrMoveToWorkspace']['status'] ) ) {
1297 return $response['data']['copyOrMoveToWorkspace']['status'];
1298 }
1299 Helper::send_error( __( "Something went wrong regarding $action to WorkSpace. Try again or contact with Templately Support.", 'templately' ) );
1300 }
1301 protected function workspace_details() {
1302 $api_key = DB::get_option('_templately_api_key', false);
1303 if( empty( $api_key ) ) {
1304 Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1305 }
1306 $slug = isset( $_POST['slug'] ) ? trim( $_POST['slug'] ) : false;
1307 if( $slug === false ) {
1308 Helper::send_error( __( 'Slug can\'t be empty.', 'templately' ) );
1309 }
1310
1311 $file_type = isset( $_POST['file_type'] ) ? trim( $_POST['file_type'] ) : 'elementor';
1312 $search = isset( $_POST['search'] ) ? trim( $_POST['search'] ) : '';
1313 $files_per_page = isset( $_POST['per_page'] ) ? intval( $_POST['per_page'] ) : 10;
1314 $files_page = isset( $_POST['page'] ) ? intval( $_POST['page'] ) : 1;
1315
1316 $query = sprintf(
1317 '{ 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 } }',
1318 $api_key,
1319 $slug,
1320 $files_page,
1321 $files_per_page,
1322 $file_type,
1323 ! empty( $search ) ? ', files_search: "'. $search .'"' : ''
1324 );
1325 $response = Query::get( $query );
1326 if( is_wp_error( $response ) ) {
1327 Helper::send_error( __( "Something went wrong regarding loading your WorkSpace. Try again.", 'templately' ) );
1328 }
1329 if( isset( $response['data'], $response['data']['workspaceDetails'] ) ) {
1330 return $response['data']['workspaceDetails'];
1331 } else {
1332 if( isset( $response['data'] ) && ! isset( $response['data']['workspaceDetails'] ) ) {
1333 return null;
1334 }
1335 }
1336 Helper::send_error( __( "Something went wrong regarding loading your WorkSpace. Try again or contact with Templately Support.", 'templately' ) );
1337 }
1338 protected function cloud_usage_limit() {
1339 $api_key = DB::get_option('_templately_api_key', false);
1340 if( empty( $api_key ) ) {
1341 Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1342 }
1343
1344 $query = sprintf(
1345 '{ myCloudUsage( api_key: "%s" ){ data, status } }',
1346 $api_key
1347 );
1348 $response = Query::get( $query );
1349 if( is_wp_error( $response ) ) {
1350 Helper::send_error( __( "Something went wrong regarding collecting your usage limit. Try again.", 'templately' ) );
1351 }
1352 if( isset( $response['data'], $response['data']['myCloudUsage'] ) ) {
1353 return $response['data']['myCloudUsage'];
1354 }
1355 Helper::send_error( __( "Something went wrong regarding collecting your usage limit. Try again or contact with Templately Support.", 'templately' ) );
1356 }
1357 protected function add_files_to_workspace() {
1358 $api_key = DB::get_option('_templately_api_key', false);
1359 if( empty( $api_key ) ) {
1360 Helper::send_error( __( 'You need to re-logged in.', 'templately' ) );
1361 }
1362
1363 $workspace_id = isset( $_POST['workspace_id'] ) ? intval( $_POST['workspace_id'] ) : false;
1364 if( $workspace_id === false ) {
1365 Helper::send_error( __( 'Workspace Not Found.', 'templately' ) );
1366 }
1367 $files = isset( $_POST['files'] ) ? trim( $_POST['files'] ) : false;
1368 if( $files === false ) {
1369 Helper::send_error( __( 'Workspace Not Found.', 'templately' ) );
1370 }
1371 $query = sprintf(
1372 'mutation { addFileToWorkspace( api_key: "%s", workspace_id: %d, files: %s ){ data, status } }',
1373 $api_key,
1374 $workspace_id,
1375 \json_encode( $files )
1376 );
1377 $response = Query::get( $query );
1378 if( is_wp_error( $response ) ) {
1379 Helper::send_error( __( "Something went wrong regarding adding files to your Workspace. Try again.", 'templately' ) );
1380 }
1381 if( isset( $response['data'], $response['data']['addFileToWorkspace'] ) ) {
1382 return $response['data']['addFileToWorkspace'];
1383 }
1384 Helper::send_error( __( "Something went wrong regarding adding files to your Workspace. Try again or contact with Templately Support.", 'templately' ) );
1385 }
1386 }