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