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