PluginProbe
Elementor Website Builder – more than just a page builder / 3.30.0-beta1
Elementor Website Builder – more than just a page builder v3.30.0-beta1
4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 All 457 releases
elementor / app / modules / kit-library / connect / cloud-kits.php

cloud-kits.php in Elementor Website Builder – more than just a page builder 3.30.0-beta1, at app/modules/kit-library/connect/cloud-kits.php

161 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\App\Modules\KitLibrary\Connect;
3
4 use Elementor\Core\Common\Modules\Connect\Apps\Library;
5 use Elementor\Core\Utils\Exceptions;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit; // Exit if accessed directly.
9 }
10
11 class Cloud_Kits extends Library {
12 const THRESHOLD_UNLIMITED = -1;
13 const FAILED_TO_FETCH_QUOTA_KEY = 'failed-to-fetch-quota';
14 const INSUFFICIENT_QUOTA_KEY = 'insufficient-quota';
15
16 public function get_title() {
17 return esc_html__( 'Cloud Kits', 'elementor' );
18 }
19
20 protected function get_api_url(): string {
21 return 'https://cloud-library.prod.builder.elementor.red/api/v1/cloud-library';
22 }
23
24 /**
25 * @return array|\WP_Error
26 */
27 public function get_all( $args = [] ) {
28 return $this->http_request( 'GET', 'kits', [], [
29 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
30 ] );
31 }
32
33 /**
34 * @return array|\WP_Error
35 */
36 public function get_quota() {
37 return $this->http_request( 'GET', 'quota/kits', [], [
38 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
39 ] );
40 }
41
42 public function validate_quota() {
43 $quota = $this->get_quota();
44
45 if ( is_wp_error( $quota ) ) {
46 throw new \Error( static::FAILED_TO_FETCH_QUOTA_KEY ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
47 }
48
49 $is_unlimited = self::THRESHOLD_UNLIMITED === $quota['threshold'];
50 $has_quota = $quota['currentUsage'] < $quota['threshold'];
51
52 if ( ! $is_unlimited && ! $has_quota ) {
53 throw new \Error( static::INSUFFICIENT_QUOTA_KEY ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
54 }
55 }
56
57 public function is_eligible() {
58 $quota = $this->get_quota();
59
60 if ( is_wp_error( $quota ) ) {
61 return false;
62 }
63
64 return isset( $quota['threshold'] ) && 0 !== $quota['threshold'];
65 }
66
67 public function create_kit( $title, $description, $content_file_data, $preview_file_data, array $includes ) {
68 $this->validate_quota();
69
70 $endpoint = 'kits';
71
72 $boundary = wp_generate_password( 24, false );
73
74 $headers = [
75 'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
76 ];
77
78 $body = $this->create_multipart_body(
79 [
80 'title' => $title,
81 'description' => $description,
82 'includes' => wp_json_encode( $includes ),
83 ],
84 [
85 'file' => [
86 'filename' => 'content.zip',
87 'content' => $content_file_data,
88 'content_type' => 'application/zip',
89 ],
90 'previewFile' => [
91 'filename' => 'preview.png',
92 'content' => $preview_file_data,
93 'content_type' => 'image/png',
94 ],
95 ],
96 $boundary
97 );
98
99 $payload = [
100 'headers' => $headers,
101 'body' => $body,
102 'timeout' => 120,
103 ];
104
105 $response = $this->http_request( 'POST', $endpoint, $payload, [
106 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
107 ] );
108
109 if ( empty( $response['id'] ) ) {
110 $error_message = esc_html__( 'Failed to create kit: Invalid response', 'elementor' );
111 throw new \Exception( $error_message, Exceptions::INTERNAL_SERVER_ERROR ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
112 }
113
114 return $response;
115 }
116
117 public function get_kit( array $args ) {
118 $args = array_merge_recursive( $args, [
119 'timeout' => 60, // just in case if zip is big
120 ] );
121
122 return $this->http_request( 'GET', 'kits/' . $args['id'], $args, [
123 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
124 ] );
125 }
126
127 public function delete_kit( int $id ) {
128 return $this->http_request( 'DELETE', 'kits/' . $id, [], [
129 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
130 ] );
131 }
132
133 private function create_multipart_body( $fields, $files, $boundary ): string {
134 $eol = "\r\n";
135 $body = '';
136
137 foreach ( $fields as $name => $value ) {
138 $body .= "--{$boundary}{$eol}";
139 $body .= "Content-Disposition: form-data; name=\"{$name}\"{$eol}{$eol}";
140 $body .= "{$value}{$eol}";
141 }
142
143 foreach ( $files as $name => $file ) {
144 $filename = basename( $file['filename'] );
145 $content_type = $file['content_type'];
146 $content = $file['content'];
147
148 $body .= "--{$boundary}{$eol}";
149 $body .= "Content-Disposition: form-data; name=\"{$name}\"; filename=\"{$filename}\"{$eol}";
150 $body .= "Content-Type: {$content_type}{$eol}{$eol}";
151 $body .= $content . $eol;
152 }
153
154 $body .= "--{$boundary}--{$eol}";
155
156 return $body;
157 }
158
159 protected function init() {}
160 }
161