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