PluginProbe
Elementor Website Builder – more than just a page builder / 3.30.1
Elementor Website Builder – more than just a page builder v3.30.1
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 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / variables / classes / rest-api.php

rest-api.php in Elementor Website Builder – more than just a page builder 3.30.1, at modules/variables/classes/rest-api.php

357 lines 9.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\Variables\Classes;
4
5 use Exception;
6 use WP_Error;
7 use WP_REST_Response;
8 use WP_REST_Request;
9 use WP_REST_Server;
10
11 use Elementor\Plugin;
12 use Elementor\Modules\Variables\PropTypes\Color_Variable_Prop_Type;
13 use Elementor\Modules\Variables\PropTypes\Font_Variable_Prop_Type;
14 use Elementor\Modules\Variables\Storage\Repository as Variables_Repository;
15 use Elementor\Modules\Variables\Storage\Exceptions\VariablesLimitReached;
16 use Elementor\Modules\Variables\Storage\Exceptions\RecordNotFound;
17
18 if ( ! defined( 'ABSPATH' ) ) {
19 exit; // Exit if accessed directly.
20 }
21
22 class Rest_Api {
23 const API_NAMESPACE = 'elementor/v1';
24 const API_BASE = 'variables';
25
26 const HTTP_OK = 200;
27 const HTTP_CREATED = 201;
28 const HTTP_BAD_REQUEST = 400;
29 const HTTP_NOT_FOUND = 404;
30 const HTTP_SERVER_ERROR = 500;
31
32 const MAX_ID_LENGTH = 64;
33 const MAX_LABEL_LENGTH = 50;
34 const MAX_VALUE_LENGTH = 512;
35
36 private Variables_Repository $variables_repository;
37
38 public function __construct( Variables_Repository $variables_repository ) {
39 $this->variables_repository = $variables_repository;
40 }
41
42 public function enough_permissions_to_perform_action() {
43 return current_user_can( 'edit_posts' );
44 }
45
46 public function register_routes() {
47 register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE . '/list', [
48 'methods' => WP_REST_Server::READABLE,
49 'callback' => [ $this, 'get_variables' ],
50 'permission_callback' => [ $this, 'enough_permissions_to_perform_action' ],
51 ] );
52
53 register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE . '/create', [
54 'methods' => WP_REST_Server::CREATABLE,
55 'callback' => [ $this, 'create_variable' ],
56 'permission_callback' => [ $this, 'enough_permissions_to_perform_action' ],
57 'args' => [
58 'type' => [
59 'required' => true,
60 'type' => 'string',
61 'validate_callback' => [ $this, 'is_valid_variable_type' ],
62 'sanitize_callback' => [ $this, 'trim_and_sanitize_text_field' ],
63 ],
64 'label' => [
65 'required' => true,
66 'type' => 'string',
67 'validate_callback' => [ $this, 'is_valid_variable_label' ],
68 'sanitize_callback' => [ $this, 'trim_and_sanitize_text_field' ],
69 ],
70 'value' => [
71 'required' => true,
72 'type' => 'string',
73 'validate_callback' => [ $this, 'is_valid_variable_value' ],
74 'sanitize_callback' => [ $this, 'trim_and_sanitize_text_field' ],
75 ],
76 ],
77 ] );
78
79 register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE . '/update', [
80 'methods' => WP_REST_Server::EDITABLE,
81 'callback' => [ $this, 'update_variable' ],
82 'permission_callback' => [ $this, 'enough_permissions_to_perform_action' ],
83 'args' => [
84 'id' => [
85 'required' => true,
86 'type' => 'string',
87 'validate_callback' => [ $this, 'is_valid_variable_id' ],
88 'sanitize_callback' => [ $this, 'trim_and_sanitize_text_field' ],
89 ],
90 'label' => [
91 'required' => true,
92 'type' => 'string',
93 'validate_callback' => [ $this, 'is_valid_variable_label' ],
94 'sanitize_callback' => [ $this, 'trim_and_sanitize_text_field' ],
95 ],
96 'value' => [
97 'required' => true,
98 'type' => 'string',
99 'validate_callback' => [ $this, 'is_valid_variable_value' ],
100 'sanitize_callback' => [ $this, 'trim_and_sanitize_text_field' ],
101 ],
102 ],
103 ] );
104
105 register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE . '/delete', [
106 'methods' => WP_REST_Server::DELETABLE,
107 'callback' => [ $this, 'delete_variable' ],
108 'permission_callback' => [ $this, 'enough_permissions_to_perform_action' ],
109 'args' => [
110 'id' => [
111 'required' => true,
112 'type' => 'string',
113 'validate_callback' => [ $this, 'is_valid_variable_id' ],
114 'sanitize_callback' => [ $this, 'trim_and_sanitize_text_field' ],
115 ],
116 ],
117 ] );
118
119 register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE . '/restore', [
120 'methods' => WP_REST_Server::EDITABLE,
121 'callback' => [ $this, 'restore_variable' ],
122 'permission_callback' => [ $this, 'enough_permissions_to_perform_action' ],
123 'args' => [
124 'id' => [
125 'required' => true,
126 'type' => 'string',
127 'validate_callback' => [ $this, 'is_valid_variable_id' ],
128 'sanitize_callback' => [ $this, 'trim_and_sanitize_text_field' ],
129 ],
130 ],
131 ] );
132 }
133
134 public function trim_and_sanitize_text_field( $value ) {
135 return trim( sanitize_text_field( $value ) );
136 }
137
138 public function is_valid_variable_id( $id ) {
139 $id = trim( $id );
140
141 if ( empty( $id ) ) {
142 return new WP_Error(
143 'invalid_variable_id_empty',
144 __( 'ID cannot be empty', 'elementor' )
145 );
146 }
147
148 if ( self::MAX_ID_LENGTH < strlen( $id ) ) {
149 return new WP_Error( 'invalid_variable_id_length', sprintf(
150 __( 'ID cannot exceed %d characters', 'elementor' ),
151 self::MAX_ID_LENGTH
152 ) );
153 }
154
155 return true;
156 }
157
158 public function is_valid_variable_type( $type ) {
159 return in_array( $type, [
160 Color_Variable_Prop_Type::get_key(),
161 Font_Variable_Prop_Type::get_key(),
162 ], true );
163 }
164
165 public function is_valid_variable_label( $label ) {
166 $label = trim( $label );
167
168 if ( empty( $label ) ) {
169 return new WP_Error(
170 'invalid_variable_label_empty',
171 __( 'Label cannot be empty', 'elementor' )
172 );
173 }
174
175 if ( self::MAX_LABEL_LENGTH < strlen( $label ) ) {
176 return new WP_Error( 'invalid_variable_label_length', sprintf(
177 __( 'Label cannot exceed %d characters', 'elementor' ),
178 self::MAX_LABEL_LENGTH
179 ) );
180 }
181
182 return true;
183 }
184
185 public function is_valid_variable_value( $value ) {
186 $value = trim( $value );
187
188 if ( empty( $value ) ) {
189 return new WP_Error(
190 'invalid_variable_value_empty',
191 __( 'Value cannot be empty', 'elementor' )
192 );
193 }
194
195 if ( self::MAX_VALUE_LENGTH < strlen( $value ) ) {
196 return new WP_Error( 'invalid_variable_value_length', sprintf(
197 __( 'Value cannot exceed %d characters', 'elementor' ),
198 self::MAX_VALUE_LENGTH
199 ) );
200 }
201
202 return true;
203 }
204
205 public function create_variable( WP_REST_Request $request ) {
206 try {
207 return $this->create_new_variable( $request );
208 } catch ( Exception $e ) {
209 return $this->error_response( $e );
210 }
211 }
212
213 protected function clear_cache() {
214 Plugin::$instance->files_manager->clear_cache();
215 }
216
217 private function create_new_variable( WP_REST_Request $request ) {
218 $type = $request->get_param( 'type' );
219 $label = $request->get_param( 'label' );
220 $value = $request->get_param( 'value' );
221
222 $result = $this->variables_repository->create( [
223 'type' => $type,
224 'label' => $label,
225 'value' => $value,
226 ] );
227
228 $this->clear_cache();
229
230 return $this->success_response( [
231 'variable' => $result['variable'],
232 'watermark' => $result['watermark'],
233 ], self::HTTP_CREATED );
234 }
235
236 public function update_variable( WP_REST_Request $request ) {
237 try {
238 return $this->update_existing_variable( $request );
239 } catch ( Exception $e ) {
240 return $this->error_response( $e );
241 }
242 }
243
244 private function update_existing_variable( WP_REST_Request $request ) {
245 $id = $request->get_param( 'id' );
246 $label = $request->get_param( 'label' );
247 $value = $request->get_param( 'value' );
248
249 $result = $this->variables_repository->update( $id, [
250 'label' => $label,
251 'value' => $value,
252 ] );
253
254 return $this->success_response( [
255 'variable' => $result['variable'],
256 'watermark' => $result['watermark'],
257 ] );
258 }
259
260 public function delete_variable( WP_REST_Request $request ) {
261 try {
262 return $this->delete_existing_variable( $request );
263 } catch ( Exception $e ) {
264 return $this->error_response( $e );
265 }
266 }
267
268 private function delete_existing_variable( WP_REST_Request $request ) {
269 $id = $request->get_param( 'id' );
270
271 $result = $this->variables_repository->delete( $id );
272
273 return $this->success_response( [
274 'variable' => $result['variable'],
275 'watermark' => $result['watermark'],
276 ] );
277 }
278
279 public function restore_variable( WP_REST_Request $request ) {
280 try {
281 return $this->restore_existing_variable( $request );
282 } catch ( Exception $e ) {
283 return $this->error_response( $e );
284 }
285 }
286
287 private function restore_existing_variable( WP_REST_Request $request ) {
288 $id = $request->get_param( 'id' );
289
290 $result = $this->variables_repository->restore( $id );
291
292 return $this->success_response( [
293 'variable' => $result['variable'],
294 'watermark' => $result['watermark'],
295 ] );
296 }
297
298 public function get_variables() {
299 try {
300 return $this->list_of_variables();
301 } catch ( Exception $e ) {
302 return $this->error_response( $e );
303 }
304 }
305
306 private function list_of_variables() {
307 $db_record = $this->variables_repository->load();
308
309 return $this->success_response( [
310 'variables' => $db_record['data'],
311 'total' => count( $db_record['data'] ),
312 'watermark' => $db_record['watermark'],
313 ] );
314 }
315
316 private function success_response( $payload, $status_code = null ) {
317 return new WP_REST_Response( [
318 'success' => true,
319 'data' => $payload,
320 ], $status_code ?? self::HTTP_OK );
321 }
322
323 private function error_response( Exception $e ) {
324 if ( $e instanceof VariablesLimitReached ) {
325 return $this->prepare_error_response(
326 self::HTTP_BAD_REQUEST,
327 'invalid_variable_limit_reached',
328 __( 'Reached the maximum number of variables', 'elementor' )
329 );
330 }
331
332 if ( $e instanceof RecordNotFound ) {
333 return $this->prepare_error_response(
334 self::HTTP_NOT_FOUND,
335 'variable_not_found',
336 __( 'Variable not found', 'elementor' )
337 );
338 }
339
340 return $this->prepare_error_response(
341 self::HTTP_SERVER_ERROR,
342 'unexpected_server_error',
343 __( 'Unexpected server error', 'elementor' )
344 );
345 }
346
347 private function prepare_error_response( $status_code, $error, $message ) {
348 return new WP_REST_Response( [
349 'code' => $error,
350 'message' => $message,
351 'data' => [
352 'status' => $status_code,
353 ],
354 ], $status_code );
355 }
356 }
357