← All changes
|
modules/cloud-library/connect/cloud-library.php
+12
-233
4.0.7
→
3.28.0-dev2
View file →
| @@ -1,11 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Elementor\Modules\CloudLibrary\Connect; |
| 3 | 3 | |
| 4 | 4 | use Elementor\Core\Common\Modules\Connect\Apps\Library; |
| 5 | -use Elementor\Core\Utils\Exceptions; | |
| 6 | -use Elementor\Modules\CloudLibrary\Render_Mode_Preview; | |
| 7 | -use Elementor\TemplateLibrary\Source_Cloud; | |
| 8 | 5 | |
| 9 | 6 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | 7 | exit; // Exit if accessed directly. |
| 11 | 8 | } |
| @@ -10,14 +7,16 @@ | ||
| 10 | 7 | exit; // Exit if accessed directly. |
| 11 | 8 | } |
| 12 | 9 | |
| 13 | 10 | class Cloud_Library extends Library { |
| 11 | + const API_URL = 'https://my.elementor.com/api/v1'; | |
| 12 | + | |
| 14 | 13 | public function get_title(): string { |
| 15 | 14 | return esc_html__( 'Cloud Library', 'elementor' ); |
| 16 | 15 | } |
| 17 | 16 | |
| 18 | - protected function get_api_url(): string { | |
| 19 | - return 'https://cloud-library.prod.builder.elementor.red/api/v1/cloud-library'; | |
| 17 | + protected function get_slug(): string { | |
| 18 | + return 'cloud-library'; | |
| 20 | 19 | } |
| 21 | 20 | |
| 22 | 21 | public function get_resources( $args = [] ): array { |
| 23 | 22 | $templates = []; |
| @@ -24,15 +23,12 @@ | ||
| 24 | 23 | |
| 25 | 24 | $endpoint = 'resources'; |
| 26 | 25 | |
| 27 | 26 | $query_string = http_build_query( [ |
| 28 | - 'limit' => isset( $args['limit'] ) ? (int) $args['limit'] : null, | |
| 29 | - 'offset' => isset( $args['offset'] ) ? (int) $args['offset'] : null, | |
| 30 | - 'search' => isset( $args['search'] ) ? $args['search'] : null, | |
| 31 | - 'parentId' => isset( $args['parentId'] ) ? $args['parentId'] : null, | |
| 32 | - 'templateType' => isset( $args['templateType'] ) ? $args['templateType'] : null, | |
| 33 | - 'orderBy' => isset( $args['orderby'] ) ? $args['orderby'] : null, | |
| 34 | - 'order' => isset( $args['order'] ) ? strtoupper( $args['order'] ) : null, | |
| 27 | + 'limit' => $args['limit'] ? (int) $args['limit'] : null, | |
| 28 | + 'offset' => $args['offset'] ? (int) $args['offset'] : null, | |
| 29 | + 'search' => $args['search'], | |
| 30 | + 'parentId' => $args['parentId'], | |
| 35 | 31 | ] ); |
| 36 | 32 | |
| 37 | 33 | $endpoint .= '?' . $query_string; |
| 38 | 34 | |
| @@ -53,12 +49,9 @@ | ||
| 53 | 49 | 'total' => $cloud_templates['total'], |
| 54 | 50 | ]; |
| 55 | 51 | } |
| 56 | 52 | |
| 57 | - /** | |
| 58 | - * @return array|\WP_Error | |
| 59 | - */ | |
| 60 | - public function get_resource( array $args ) { | |
| 53 | + public function get_resource( array $args ): array { | |
| 61 | 54 | return $this->http_request( 'GET', 'resources/' . $args['id'], $args, [ |
| 62 | 55 | 'return_type' => static::HTTP_RETURN_TYPE_ARRAY, |
| 63 | 56 | ] ); |
| 64 | 57 | } |
| @@ -63,50 +56,21 @@ | ||
| 63 | 56 | ] ); |
| 64 | 57 | } |
| 65 | 58 | |
| 66 | 59 | protected function prepare_template( array $template_data ): array { |
| 67 | - $template = [ | |
| 60 | + return [ | |
| 68 | 61 | 'template_id' => $template_data['id'], |
| 69 | 62 | 'source' => 'cloud', |
| 70 | - 'type' => $template_data['templateType'], | |
| 63 | + 'type' => ucfirst( $template_data['templateType'] ), | |
| 71 | 64 | 'subType' => $template_data['type'], |
| 72 | 65 | 'title' => $template_data['title'], |
| 73 | - 'status' => $template_data['status'], | |
| 74 | 66 | 'author' => $template_data['authorEmail'], |
| 75 | 67 | 'human_date' => date_i18n( get_option( 'date_format' ), strtotime( $template_data['createdAt'] ) ), |
| 76 | 68 | 'export_link' => $this->get_export_link( $template_data['id'] ), |
| 77 | 69 | 'hasPageSettings' => $template_data['hasPageSettings'], |
| 78 | - 'parentId' => $template_data['parentId'], | |
| 79 | - 'preview_url' => esc_url_raw( $template_data['previewUrl'] ?? '' ), | |
| 80 | - 'generate_preview_url' => esc_url_raw( $this->generate_preview_url( $template_data ) ?? '' ), | |
| 81 | 70 | ]; |
| 82 | - | |
| 83 | - if ( ! empty( $template_data['content'] ) ) { | |
| 84 | - $template['content'] = $template_data['content']; | |
| 85 | - } | |
| 86 | - | |
| 87 | - return $template; | |
| 88 | 71 | } |
| 89 | 72 | |
| 90 | - private function generate_preview_url( $template_data ): ?string { | |
| 91 | - if ( ! empty( $template_data['previewUrl'] ) || | |
| 92 | - Source_Cloud::FOLDER_RESOURCE_TYPE === $template_data['type'] || | |
| 93 | - empty( $template_data['id'] ) | |
| 94 | - ) { | |
| 95 | - return null; | |
| 96 | - } | |
| 97 | - | |
| 98 | - $template_id = $template_data['id']; | |
| 99 | - | |
| 100 | - $query_args = [ | |
| 101 | - 'render_mode_nonce' => wp_create_nonce( 'render_mode_' . $template_id ), | |
| 102 | - 'template_id' => $template_id, | |
| 103 | - 'render_mode' => Render_Mode_Preview::MODE, | |
| 104 | - ]; | |
| 105 | - | |
| 106 | - return set_url_scheme( add_query_arg( $query_args, site_url() ) ); | |
| 107 | - } | |
| 108 | - | |
| 109 | 73 | private function get_export_link( $template_id ) { |
| 110 | 74 | return add_query_arg( |
| 111 | 75 | [ |
| 112 | 76 | 'action' => 'elementor_library_direct_actions', |
| @@ -131,22 +95,8 @@ | ||
| 131 | 95 | 'return_type' => static::HTTP_RETURN_TYPE_ARRAY, |
| 132 | 96 | ] ); |
| 133 | 97 | } |
| 134 | 98 | |
| 135 | - public function post_bulk_resources( $data ): array { | |
| 136 | - $resource = [ | |
| 137 | - 'headers' => [ | |
| 138 | - 'Content-Type' => 'application/json', | |
| 139 | - ], | |
| 140 | - 'body' => wp_json_encode( $data ), | |
| 141 | - 'timeout' => 120, | |
| 142 | - ]; | |
| 143 | - | |
| 144 | - return $this->http_request( 'POST', 'resources/bulk', $resource, [ | |
| 145 | - 'return_type' => static::HTTP_RETURN_TYPE_ARRAY, | |
| 146 | - ] ); | |
| 147 | - } | |
| 148 | - | |
| 149 | 99 | public function delete_resource( $template_id ): bool { |
| 150 | 100 | $request = $this->http_request( 'DELETE', 'resources/' . $template_id ); |
| 151 | 101 | |
| 152 | 102 | if ( isset( $request->errors[204] ) && 'No Content' === $request->errors[204][0] ) { |
| @@ -160,9 +110,9 @@ | ||
| 160 | 110 | return true; |
| 161 | 111 | } |
| 162 | 112 | |
| 163 | 113 | public function update_resource( array $template_data ) { |
| 164 | - $endpoint = 'resources/' . $template_data['id']; | |
| 114 | + $endpoint = 'resources/' . $template_data['template_id']; | |
| 165 | 115 | |
| 166 | 116 | $request = $this->http_request( 'PATCH', $endpoint, [ 'body' => $template_data ], [ |
| 167 | 117 | 'return_type' => static::HTTP_RETURN_TYPE_ARRAY, |
| 168 | 118 | ] ); |
| @@ -171,179 +121,8 @@ | ||
| 171 | 121 | return false; |
| 172 | 122 | } |
| 173 | 123 | |
| 174 | 124 | return true; |
| 175 | - } | |
| 176 | - | |
| 177 | - public function update_resource_preview( $template_id, $file_data ) { | |
| 178 | - $endpoint = 'resources/' . $template_id . '/preview'; | |
| 179 | - | |
| 180 | - $boundary = wp_generate_password( 24, false ); | |
| 181 | - | |
| 182 | - $headers = [ | |
| 183 | - 'Content-Type' => 'multipart/form-data; boundary=' . $boundary, | |
| 184 | - ]; | |
| 185 | - | |
| 186 | - $body = $this->generate_multipart_payload( $file_data, $boundary, $template_id . '_preview.png' ); | |
| 187 | - | |
| 188 | - $payload = [ | |
| 189 | - 'headers' => $headers, | |
| 190 | - 'body' => $body, | |
| 191 | - ]; | |
| 192 | - | |
| 193 | - $response = $this->http_request( 'PATCH', $endpoint, $payload, [ | |
| 194 | - 'return_type' => static::HTTP_RETURN_TYPE_ARRAY, | |
| 195 | - 'timeout' => 120, | |
| 196 | - ]); | |
| 197 | - | |
| 198 | - if ( is_wp_error( $response ) || empty( $response['preview_url'] ) ) { | |
| 199 | - $error_message = esc_html__( 'Failed to save preview.', 'elementor' ); | |
| 200 | - | |
| 201 | - throw new \Exception( $error_message, Exceptions::INTERNAL_SERVER_ERROR ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped | |
| 202 | - } | |
| 203 | - | |
| 204 | - return $response['preview_url']; | |
| 205 | - } | |
| 206 | - | |
| 207 | - public function mark_preview_as_failed( $template_id, $error ) { | |
| 208 | - $endpoint = 'resources/' . $template_id . '/preview'; | |
| 209 | - | |
| 210 | - $payload = [ | |
| 211 | - 'body' => [ | |
| 212 | - 'error' => $error, | |
| 213 | - ], | |
| 214 | - ]; | |
| 215 | - | |
| 216 | - $response = $this->http_request( 'PATCH', $endpoint, $payload, [ | |
| 217 | - 'return_type' => static::HTTP_RETURN_TYPE_ARRAY, | |
| 218 | - ]); | |
| 219 | - | |
| 220 | - if ( is_wp_error( $response ) ) { | |
| 221 | - $error_message = esc_html__( 'Failed to mark preview as failed.', 'elementor' ); | |
| 222 | - | |
| 223 | - throw new \Exception( $error_message, Exceptions::INTERNAL_SERVER_ERROR ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped | |
| 224 | - } | |
| 225 | - | |
| 226 | - return $response; | |
| 227 | - } | |
| 228 | - | |
| 229 | - /** | |
| 230 | - * @param $file_data | |
| 231 | - * @param $boundary | |
| 232 | - * @param $file_name | |
| 233 | - * @return string | |
| 234 | - */ | |
| 235 | - private function generate_multipart_payload( $file_data, $boundary, $file_name ): string { | |
| 236 | - $payload = ''; | |
| 237 | - | |
| 238 | - // Append the file | |
| 239 | - $payload .= "--{$boundary}\r\n"; | |
| 240 | - $payload .= 'Content-Disposition: form-data; name="file"; filename="' . esc_attr( $file_name ) . "\"\r\n"; | |
| 241 | - $payload .= "Content-Type: image/png\r\n\r\n"; | |
| 242 | - $payload .= $file_data . "\r\n"; | |
| 243 | - $payload .= "--{$boundary}--\r\n"; | |
| 244 | - | |
| 245 | - return $payload; | |
| 246 | - } | |
| 247 | - | |
| 248 | - public function bulk_delete_resources( $template_ids ) { | |
| 249 | - $endpoint = 'resources/bulk'; | |
| 250 | - | |
| 251 | - $endpoint .= '?ids=' . implode( ',', $template_ids ); | |
| 252 | - | |
| 253 | - $response = $this->http_request( 'DELETE', $endpoint, [], [ | |
| 254 | - 'return_type' => static::HTTP_RETURN_TYPE_ARRAY, | |
| 255 | - ] ); | |
| 256 | - | |
| 257 | - if ( isset( $response->errors[204] ) ) { | |
| 258 | - return true; | |
| 259 | - } | |
| 260 | - | |
| 261 | - if ( is_wp_error( $response ) ) { | |
| 262 | - return $response; | |
| 263 | - } | |
| 264 | - | |
| 265 | - return true; | |
| 266 | - } | |
| 267 | - | |
| 268 | - public function bulk_undo_delete_resources( $template_ids ) { | |
| 269 | - $endpoint = 'resources/bulk-delete/undo'; | |
| 270 | - | |
| 271 | - $body = wp_json_encode( [ 'ids' => $template_ids ] ); | |
| 272 | - | |
| 273 | - $request = [ | |
| 274 | - 'headers' => [ | |
| 275 | - 'Content-Type' => 'application/json', | |
| 276 | - ], | |
| 277 | - 'body' => $body, | |
| 278 | - ]; | |
| 279 | - | |
| 280 | - $response = $this->http_request( 'POST', $endpoint, $request, [ | |
| 281 | - 'return_type' => static::HTTP_RETURN_TYPE_ARRAY, | |
| 282 | - ] ); | |
| 283 | - | |
| 284 | - if ( is_wp_error( $response ) ) { | |
| 285 | - return $response; | |
| 286 | - } | |
| 287 | - | |
| 288 | - return true; | |
| 289 | - } | |
| 290 | - | |
| 291 | - public function get_bulk_resources_with_content( $args = [] ): array { | |
| 292 | - $templates = []; | |
| 293 | - | |
| 294 | - $endpoint = 'resources/bulk'; | |
| 295 | - | |
| 296 | - $query_string = http_build_query( [ | |
| 297 | - 'ids' => implode( ',', $args['from_template_id'] ), | |
| 298 | - ] ); | |
| 299 | - | |
| 300 | - $endpoint .= '?' . $query_string; | |
| 301 | - | |
| 302 | - $cloud_templates = $this->http_request( 'GET', $endpoint, $args, [ | |
| 303 | - 'return_type' => static::HTTP_RETURN_TYPE_ARRAY, | |
| 304 | - ] ); | |
| 305 | - | |
| 306 | - if ( is_wp_error( $cloud_templates ) || ! is_array( $cloud_templates ) ) { | |
| 307 | - return $templates; | |
| 308 | - } | |
| 309 | - | |
| 310 | - foreach ( $cloud_templates as $cloud_template ) { | |
| 311 | - $templates[] = $this->prepare_template( $cloud_template ); | |
| 312 | - } | |
| 313 | - | |
| 314 | - return $templates; | |
| 315 | - } | |
| 316 | - | |
| 317 | - public function bulk_move_templates( array $template_data ) { | |
| 318 | - $endpoint = 'resources/move'; | |
| 319 | - $args = [ | |
| 320 | - 'body' => wp_json_encode( $template_data ), | |
| 321 | - 'headers' => [ 'Content-Type' => 'application/json' ], | |
| 322 | - ]; | |
| 323 | - | |
| 324 | - $request = $this->http_request( 'PATCH', $endpoint, $args, [ | |
| 325 | - 'return_type' => static::HTTP_RETURN_TYPE_ARRAY, | |
| 326 | - ] ); | |
| 327 | - | |
| 328 | - if ( is_wp_error( $request ) ) { | |
| 329 | - return false; | |
| 330 | - } | |
| 331 | - | |
| 332 | - return true; | |
| 333 | - } | |
| 334 | - | |
| 335 | - /** | |
| 336 | - * @return array|\WP_Error | |
| 337 | - */ | |
| 338 | - public function get_quota() { | |
| 339 | - if ( ! $this->is_connected() ) { | |
| 340 | - return new \WP_Error( 'not_connected', esc_html__( 'Not connected', 'elementor' ) ); | |
| 341 | - } | |
| 342 | - | |
| 343 | - return $this->http_request( 'GET', 'quota', [], [ | |
| 344 | - 'return_type' => static::HTTP_RETURN_TYPE_ARRAY, | |
| 345 | - ] ); | |
| 346 | 125 | } |
| 347 | 126 | |
| 348 | 127 | protected function init() {} |
| 349 | 128 | } |