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