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