PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 1.1.1
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v1.1.1
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
templately / core / api / class-api.php

class-api.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 1.1.1, at core/api/class-api.php

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