| 1 |
<?php |
| 2 |
namespace Elementor\Modules\CloudLibrary\Connect; |
| 3 |
|
| 4 |
use Elementor\Core\Common\Modules\Connect\Apps\Library; |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; // Exit if accessed directly. |
| 8 |
} |
| 9 |
|
| 10 |
class Cloud_Library extends Library { |
| 11 |
const API_URL = 'https://my.elementor.com/api/v1'; |
| 12 |
|
| 13 |
public function get_title(): string { |
| 14 |
return esc_html__( 'Cloud Library', 'elementor' ); |
| 15 |
} |
| 16 |
|
| 17 |
protected function get_slug(): string { |
| 18 |
return 'cloud-library'; |
| 19 |
} |
| 20 |
|
| 21 |
public function get_resources( $args = [] ): array { |
| 22 |
$templates = []; |
| 23 |
|
| 24 |
$endpoint = 'resources'; |
| 25 |
|
| 26 |
$query_string = http_build_query( [ |
| 27 |
'limit' => $args['limit'] ? (int) $args['limit'] : null, |
| 28 |
'offset' => $args['offset'] ? (int) $args['offset'] : null, |
| 29 |
'search' => $args['search'], |
| 30 |
'parentId' => $args['parentId'], |
| 31 |
] ); |
| 32 |
|
| 33 |
$endpoint .= '?' . $query_string; |
| 34 |
|
| 35 |
$cloud_templates = $this->http_request( 'GET', $endpoint, $args, [ |
| 36 |
'return_type' => static::HTTP_RETURN_TYPE_ARRAY, |
| 37 |
] ); |
| 38 |
|
| 39 |
if ( is_wp_error( $cloud_templates ) || ! is_array( $cloud_templates['data'] ) ) { |
| 40 |
return $templates; |
| 41 |
} |
| 42 |
|
| 43 |
foreach ( $cloud_templates['data'] as $cloud_template ) { |
| 44 |
$templates[] = $this->prepare_template( $cloud_template ); |
| 45 |
} |
| 46 |
|
| 47 |
return [ |
| 48 |
'templates' => $templates, |
| 49 |
'total' => $cloud_templates['total'], |
| 50 |
]; |
| 51 |
} |
| 52 |
|
| 53 |
public function get_resource( array $args ): array { |
| 54 |
return $this->http_request( 'GET', 'resources/' . $args['id'], $args, [ |
| 55 |
'return_type' => static::HTTP_RETURN_TYPE_ARRAY, |
| 56 |
] ); |
| 57 |
} |
| 58 |
|
| 59 |
protected function prepare_template( array $template_data ): array { |
| 60 |
return [ |
| 61 |
'template_id' => $template_data['id'], |
| 62 |
'source' => 'cloud', |
| 63 |
'type' => ucfirst( $template_data['templateType'] ), |
| 64 |
'subType' => $template_data['type'], |
| 65 |
'title' => $template_data['title'], |
| 66 |
'author' => $template_data['authorEmail'], |
| 67 |
'human_date' => date_i18n( get_option( 'date_format' ), strtotime( $template_data['createdAt'] ) ), |
| 68 |
'export_link' => $this->get_export_link( $template_data['id'] ), |
| 69 |
'hasPageSettings' => $template_data['hasPageSettings'], |
| 70 |
]; |
| 71 |
} |
| 72 |
|
| 73 |
private function get_export_link( $template_id ) { |
| 74 |
return add_query_arg( |
| 75 |
[ |
| 76 |
'action' => 'elementor_library_direct_actions', |
| 77 |
'library_action' => 'export_template', |
| 78 |
'source' => 'cloud', |
| 79 |
'_nonce' => wp_create_nonce( 'elementor_ajax' ), |
| 80 |
'template_id' => $template_id, |
| 81 |
], |
| 82 |
admin_url( 'admin-ajax.php' ) |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
public function post_resource( $data ): array { |
| 87 |
$resource = [ |
| 88 |
'headers' => [ |
| 89 |
'Content-Type' => 'application/json', |
| 90 |
], |
| 91 |
'body' => wp_json_encode( $data ), |
| 92 |
]; |
| 93 |
|
| 94 |
return $this->http_request( 'POST', 'resources', $resource, [ |
| 95 |
'return_type' => static::HTTP_RETURN_TYPE_ARRAY, |
| 96 |
] ); |
| 97 |
} |
| 98 |
|
| 99 |
public function delete_resource( $template_id ): bool { |
| 100 |
$request = $this->http_request( 'DELETE', 'resources/' . $template_id ); |
| 101 |
|
| 102 |
if ( isset( $request->errors[204] ) && 'No Content' === $request->errors[204][0] ) { |
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
if ( is_wp_error( $request ) ) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
return true; |
| 111 |
} |
| 112 |
|
| 113 |
public function update_resource( array $template_data ) { |
| 114 |
$endpoint = 'resources/' . $template_data['template_id']; |
| 115 |
|
| 116 |
$request = $this->http_request( 'PATCH', $endpoint, [ 'body' => $template_data ], [ |
| 117 |
'return_type' => static::HTTP_RETURN_TYPE_ARRAY, |
| 118 |
] ); |
| 119 |
|
| 120 |
if ( is_wp_error( $request ) ) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
return true; |
| 125 |
} |
| 126 |
|
| 127 |
protected function init() {} |
| 128 |
} |
| 129 |
|