← All changes
|
modules/global-classes/global-classes-rest-api.php
+82
-238
4.0.7
→
3.28.0-dev2
View file →
| @@ -1,12 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Elementor\Modules\GlobalClasses; |
| 4 | 4 | |
| 5 | -use Elementor\Modules\GlobalClasses\Usage\Applied_Global_Classes_Usage; | |
| 6 | -use Elementor\Core\Utils\Api\Error_Builder; | |
| 7 | -use Elementor\Core\Utils\Api\Response_Builder; | |
| 8 | -use Elementor\Modules\GlobalClasses\Database\Migrations\Add_Capabilities; | |
| 5 | +use Elementor\Core\Utils\Collection; | |
| 6 | +use Elementor\Modules\AtomicWidgets\Styles\Style_Schema; | |
| 7 | +use Elementor\Modules\AtomicWidgets\Parsers\Style_Parser; | |
| 8 | +use Elementor\Modules\GlobalClasses\Utils\Error_Builder; | |
| 9 | +use Elementor\Modules\GlobalClasses\Utils\Response_Builder; | |
| 9 | 10 | |
| 10 | 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | 12 | exit; // Exit if accessed directly. |
| 12 | 13 | } |
| @@ -13,12 +14,9 @@ | ||
| 13 | 14 | |
| 14 | 15 | class Global_Classes_REST_API { |
| 15 | 16 | const API_NAMESPACE = 'elementor/v1'; |
| 16 | 17 | const API_BASE = 'global-classes'; |
| 17 | - const API_BASE_USAGE = self::API_BASE . '/usage'; | |
| 18 | - const MAX_ITEMS = 100; | |
| 19 | - const LABEL_PREFIX = 'DUP_'; | |
| 20 | - const MAX_LABEL_LENGTH = 50; | |
| 18 | + | |
| 21 | 19 | private $repository = null; |
| 22 | 20 | |
| 23 | 21 | public function register_hooks() { |
| 24 | 22 | add_action( 'rest_api_init', fn() => $this->register_routes() ); |
| @@ -31,43 +29,17 @@ | ||
| 31 | 29 | |
| 32 | 30 | return $this->repository; |
| 33 | 31 | } |
| 34 | 32 | |
| 33 | + /** | |
| 34 | + * TODO: Add sanitization when implemented on prop types [EDS-574] | |
| 35 | + */ | |
| 35 | 36 | private function register_routes() { |
| 36 | 37 | register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE, [ |
| 37 | 38 | [ |
| 38 | 39 | 'methods' => 'GET', |
| 39 | - 'callback' => fn( $request ) => $this->route_wrapper( fn() => $this->all( $request ) ), | |
| 40 | - 'permission_callback' => fn() => is_user_logged_in(), | |
| 41 | - 'args' => [ | |
| 42 | - 'context' => [ | |
| 43 | - 'type' => 'string', | |
| 44 | - 'required' => false, | |
| 45 | - 'default' => Global_Classes_Repository::CONTEXT_FRONTEND, | |
| 46 | - 'enum' => [ | |
| 47 | - Global_Classes_Repository::CONTEXT_FRONTEND, | |
| 48 | - Global_Classes_Repository::CONTEXT_PREVIEW, | |
| 49 | - ], | |
| 50 | - ], | |
| 51 | - ], | |
| 52 | - ], | |
| 53 | - ] ); | |
| 54 | - | |
| 55 | - register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE_USAGE, [ | |
| 56 | - [ | |
| 57 | - 'callback' => fn() => $this->route_wrapper( fn() => $this->get_usage() ), | |
| 40 | + 'callback' => fn() => $this->route_wrapper( fn() => $this->all() ), | |
| 58 | 41 | 'permission_callback' => fn() => current_user_can( 'manage_options' ), |
| 59 | - 'args' => [ | |
| 60 | - 'context' => [ | |
| 61 | - 'type' => 'string', | |
| 62 | - 'required' => false, | |
| 63 | - 'default' => Global_Classes_Repository::CONTEXT_FRONTEND, | |
| 64 | - 'enum' => [ | |
| 65 | - Global_Classes_Repository::CONTEXT_FRONTEND, | |
| 66 | - Global_Classes_Repository::CONTEXT_PREVIEW, | |
| 67 | - ], | |
| 68 | - ], | |
| 69 | - ], | |
| 70 | 42 | ], |
| 71 | 43 | ] ); |
| 72 | 44 | |
| 73 | 45 | register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE, [ |
| @@ -73,64 +45,37 @@ | ||
| 73 | 45 | register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE, [ |
| 74 | 46 | [ |
| 75 | 47 | 'methods' => 'PUT', |
| 76 | 48 | 'callback' => fn( $request ) => $this->route_wrapper( fn() => $this->put( $request ) ), |
| 77 | - 'permission_callback' => fn() => current_user_can( Add_Capabilities::UPDATE_CLASS ), | |
| 49 | + 'permission_callback' => fn() => current_user_can( 'manage_options' ), | |
| 78 | 50 | 'args' => [ |
| 79 | - 'context' => [ | |
| 80 | - 'type' => 'string', | |
| 81 | - 'required' => false, | |
| 82 | - 'default' => Global_Classes_Repository::CONTEXT_FRONTEND, | |
| 83 | - 'enum' => [ | |
| 84 | - Global_Classes_Repository::CONTEXT_FRONTEND, | |
| 85 | - Global_Classes_Repository::CONTEXT_PREVIEW, | |
| 86 | - ], | |
| 87 | - ], | |
| 88 | - 'changes' => [ | |
| 89 | - 'type' => 'object', | |
| 90 | - 'required' => true, | |
| 91 | - 'additionalProperties' => false, | |
| 92 | - 'properties' => [ | |
| 93 | - 'added' => [ | |
| 94 | - 'type' => 'array', | |
| 95 | - 'required' => true, | |
| 96 | - 'items' => [ 'type' => 'string' ], | |
| 97 | - ], | |
| 98 | - 'deleted' => [ | |
| 99 | - 'type' => 'array', | |
| 100 | - 'required' => true, | |
| 101 | - 'items' => [ 'type' => 'string' ], | |
| 102 | - ], | |
| 103 | - 'modified' => [ | |
| 104 | - 'type' => 'array', | |
| 105 | - 'required' => true, | |
| 106 | - 'items' => [ 'type' => 'string' ], | |
| 107 | - ], | |
| 108 | - ], | |
| 109 | - ], | |
| 110 | 51 | 'items' => [ |
| 111 | 52 | 'required' => true, |
| 112 | 53 | 'type' => 'object', |
| 113 | - 'additionalProperties' => [ | |
| 114 | - 'type' => 'object', | |
| 115 | - 'properties' => [ | |
| 116 | - 'id' => [ | |
| 117 | - 'type' => 'string', | |
| 118 | - 'required' => true, | |
| 54 | + 'additionalProperties' => false, | |
| 55 | + 'patternProperties' => [ | |
| 56 | + '^g-[a-z0-9]+$' => [ | |
| 57 | + 'type' => 'object', | |
| 58 | + 'properties' => [ | |
| 59 | + 'id' => [ | |
| 60 | + 'type' => 'string', | |
| 61 | + 'pattern' => '^g-[a-z0-9]+$', | |
| 62 | + 'required' => true, | |
| 63 | + ], | |
| 64 | + 'variants' => [ | |
| 65 | + 'type' => 'array', | |
| 66 | + 'required' => true, | |
| 67 | + ], | |
| 68 | + 'type' => [ | |
| 69 | + 'type' => 'string', | |
| 70 | + 'enum' => [ 'class' ], | |
| 71 | + 'required' => true, | |
| 72 | + ], | |
| 73 | + 'label' => [ | |
| 74 | + 'type' => 'string', | |
| 75 | + 'required' => true, | |
| 76 | + ], | |
| 119 | 77 | ], |
| 120 | - 'variants' => [ | |
| 121 | - 'type' => 'array', | |
| 122 | - 'required' => true, | |
| 123 | - ], | |
| 124 | - 'type' => [ | |
| 125 | - 'type' => 'string', | |
| 126 | - 'enum' => [ 'class' ], | |
| 127 | - 'required' => true, | |
| 128 | - ], | |
| 129 | - 'label' => [ | |
| 130 | - 'type' => 'string', | |
| 131 | - 'required' => true, | |
| 132 | - ], | |
| 133 | 78 | ], |
| 134 | 79 | ], |
| 135 | 80 | ], |
| 136 | 81 | 'order' => [ |
| @@ -137,8 +82,9 @@ | ||
| 137 | 82 | 'required' => true, |
| 138 | 83 | 'type' => 'array', |
| 139 | 84 | 'items' => [ |
| 140 | 85 | 'type' => 'string', |
| 86 | + 'pattern' => '^g-[a-z0-9]+$', | |
| 141 | 87 | ], |
| 142 | 88 | ], |
| 143 | 89 | ], |
| 144 | 90 | ], |
| @@ -144,117 +90,84 @@ | ||
| 144 | 90 | ], |
| 145 | 91 | ] ); |
| 146 | 92 | } |
| 147 | 93 | |
| 148 | - private function all( \WP_REST_Request $request ) { | |
| 149 | - $context = $request->get_param( 'context' ); | |
| 94 | + private function all() { | |
| 95 | + $classes = $this->get_repository()->all(); | |
| 150 | 96 | |
| 151 | - $classes = $this->get_repository()->context( $context )->all(); | |
| 152 | - | |
| 153 | 97 | return Response_Builder::make( (object) $classes->get_items()->all() ) |
| 154 | 98 | ->set_meta( [ 'order' => $classes->get_order()->all() ] ) |
| 155 | 99 | ->build(); |
| 156 | 100 | } |
| 157 | 101 | |
| 158 | - private function get_usage() { | |
| 159 | - $classes_usage = ( new Applied_Global_Classes_Usage() )->get_detailed_usage(); | |
| 160 | - | |
| 161 | - return Response_Builder::make( (object) $classes_usage )->build(); | |
| 162 | - } | |
| 163 | - | |
| 164 | 102 | private function put( \WP_REST_Request $request ) { |
| 165 | - $context = $request->get_param( 'context' ); | |
| 166 | - $changes = $request->get_param( 'changes' ) ?? []; | |
| 167 | - $new_added_items_ids = $changes['added'] ?? []; | |
| 168 | - $parser = Global_Classes_Parser::make(); | |
| 169 | - $existing_labels = Global_Classes_Repository::make() | |
| 170 | - ->context( $context ) | |
| 171 | - ->all() | |
| 172 | - ->get_items() | |
| 173 | - ->map( function ( $item ) { | |
| 174 | - return $item['label']; | |
| 175 | - } ) | |
| 176 | - ->all(); | |
| 103 | + $items = $request->get_param( 'items' ); | |
| 177 | 104 | |
| 178 | - $items_result = $parser->parse_items( | |
| 179 | - $request->get_param( 'items' ) | |
| 180 | - ); | |
| 105 | + [$is_valid, $sanitized_items, $errors] = $this->sanitize_items( $items ); | |
| 181 | 106 | |
| 182 | - $items_count = count( $items_result->unwrap() ); | |
| 183 | - | |
| 184 | - if ( $items_count > self::MAX_ITEMS ) { | |
| 185 | - return Error_Builder::make( 'global_classes_limit_exceeded' ) | |
| 107 | + if ( ! $is_valid ) { | |
| 108 | + return Error_Builder::make( 'invalid_items' ) | |
| 186 | 109 | ->set_status( 400 ) |
| 187 | - ->set_meta([ | |
| 188 | - 'current_count' => $items_count, | |
| 189 | - 'max_allowed' => self::MAX_ITEMS, | |
| 190 | - ]) | |
| 191 | - ->set_message(sprintf( | |
| 192 | - /* translators: %d: Maximum allowed items. */ | |
| 193 | - __( 'Global classes limit exceeded. Maximum allowed: %d', 'elementor' ), | |
| 194 | - self::MAX_ITEMS | |
| 195 | - )) | |
| 110 | + ->set_message( 'Invalid items: ' . join( ', ', array_keys( $errors ) ) ) | |
| 196 | 111 | ->build(); |
| 197 | 112 | } |
| 198 | 113 | |
| 199 | - if ( ! $items_result->is_valid() ) { | |
| 200 | - return Error_Builder::make( 'invalid_items' ) | |
| 114 | + $order = $request->get_param( 'order' ); | |
| 115 | + | |
| 116 | + if ( ! $this->is_valid_order( $order, $sanitized_items ) ) { | |
| 117 | + return Error_Builder::make( 'invalid_order' ) | |
| 201 | 118 | ->set_status( 400 ) |
| 202 | - ->set_message( 'Invalid items: ' . $items_result->errors()->to_string() ) | |
| 119 | + ->set_message( 'Invalid order' ) | |
| 203 | 120 | ->build(); |
| 204 | 121 | } |
| 205 | 122 | |
| 206 | - $order_result = $parser->parse_order( | |
| 207 | - $request->get_param( 'order' ), | |
| 208 | - $items_result->unwrap() | |
| 123 | + $this->get_repository()->put( | |
| 124 | + $sanitized_items, | |
| 125 | + $order | |
| 209 | 126 | ); |
| 210 | 127 | |
| 211 | - if ( ! $order_result->is_valid() ) { | |
| 212 | - return Error_Builder::make( 'invalid_order' ) | |
| 213 | - ->set_status( 400 ) | |
| 214 | - ->set_message( 'Invalid order: ' . $order_result->errors()->to_string() ) | |
| 215 | - ->build(); | |
| 216 | - } | |
| 128 | + return Response_Builder::make()->no_content()->build(); | |
| 129 | + } | |
| 217 | 130 | |
| 218 | - $repository = $this->get_repository() | |
| 219 | - ->context( $request->get_param( 'context' ) ); | |
| 131 | + private function sanitize_items( array $items ) { | |
| 132 | + $errors = []; | |
| 133 | + $sanitized_items = []; | |
| 220 | 134 | |
| 221 | - $changes_resolver = Global_Classes_Changes_Resolver::make( | |
| 222 | - $repository, | |
| 223 | - $changes, | |
| 224 | - ); | |
| 135 | + foreach ( $items as $item_id => $item ) { | |
| 136 | + [$is_item_valid, $sanitized_item, $item_errors] = Style_Parser::make( Style_Schema::get() )->parse( $item ); | |
| 225 | 137 | |
| 226 | - $duplicated_labels = Global_Classes_Parser::check_for_duplicate_labels( | |
| 227 | - $existing_labels, | |
| 228 | - $items_result->unwrap(), | |
| 229 | - $new_added_items_ids | |
| 230 | - ); | |
| 138 | + if ( ! $is_item_valid ) { | |
| 139 | + $errors[ $item_id ] = $item_errors; | |
| 140 | + continue; | |
| 141 | + } | |
| 231 | 142 | |
| 232 | - $final_items = $items_result->unwrap(); | |
| 233 | - $duplicate_validation_result = null; | |
| 143 | + if ( $item_id !== $sanitized_item['id'] ) { | |
| 144 | + $errors[ $item_id ] = [ 'id' ]; | |
| 234 | 145 | |
| 235 | - if ( ! empty( $duplicated_labels ) ) { | |
| 236 | - $modified_labels = $this->handle_duplicates( $duplicated_labels, $existing_labels ); | |
| 237 | - $duplicate_validation_result = $modified_labels; | |
| 238 | - foreach ( $modified_labels as $item_id => $labels ) { | |
| 239 | - $final_items[ $item_id ]['label'] = $labels['modified']; | |
| 146 | + continue; | |
| 240 | 147 | } |
| 148 | + | |
| 149 | + $sanitized_items[ $sanitized_item['id'] ] = $sanitized_item; | |
| 241 | 150 | } |
| 242 | 151 | |
| 243 | - $repository->put( | |
| 244 | - $changes_resolver->resolve_items( $final_items ), | |
| 245 | - $changes_resolver->resolve_order( $order_result->unwrap() ), | |
| 246 | - ); | |
| 152 | + $is_valid = count( $errors ) === 0; | |
| 247 | 153 | |
| 248 | - if ( $duplicate_validation_result ) { | |
| 249 | - $response_data = [ | |
| 250 | - 'code' => 'DUPLICATED_LABEL', | |
| 251 | - 'modifiedLabels' => $duplicate_validation_result, | |
| 252 | - ]; | |
| 253 | - return Response_Builder::make( $response_data )->build(); | |
| 254 | - } | |
| 154 | + return [ $is_valid, $sanitized_items, $errors ]; | |
| 155 | + } | |
| 255 | 156 | |
| 256 | - return Response_Builder::make()->no_content()->build(); | |
| 157 | + private function is_valid_order( array $order, array $items ) { | |
| 158 | + $existing_ids = array_keys( $items ); | |
| 159 | + | |
| 160 | + $excess_ids = Collection::make( $order )->diff( $existing_ids ); | |
| 161 | + $missing_ids = Collection::make( $existing_ids )->diff( $order ); | |
| 162 | + | |
| 163 | + $has_duplications = Collection::make( $order )->unique()->all() !== $order; | |
| 164 | + | |
| 165 | + return ( | |
| 166 | + $excess_ids->is_empty() && | |
| 167 | + $missing_ids->is_empty() && | |
| 168 | + ! $has_duplications | |
| 169 | + ); | |
| 257 | 170 | } |
| 258 | 171 | |
| 259 | 172 | private function route_wrapper( callable $cb ) { |
| 260 | 173 | try { |
| @@ -265,75 +178,6 @@ | ||
| 265 | 178 | ->build(); |
| 266 | 179 | } |
| 267 | 180 | |
| 268 | 181 | return $response; |
| 269 | - } | |
| 270 | - | |
| 271 | - private function handle_duplicates( array $duplicate_labels, array $existing_labels ) { | |
| 272 | - | |
| 273 | - $modified_labels = []; | |
| 274 | - | |
| 275 | - foreach ( $duplicate_labels as $duplicate_label ) { | |
| 276 | - $item_id = $duplicate_label['item_id']; | |
| 277 | - $original_label = $duplicate_label['label']; | |
| 278 | - | |
| 279 | - $modified_label = $this->generate_unique_label( $original_label, $existing_labels ); | |
| 280 | - | |
| 281 | - $modified_labels[ $item_id ] = [ | |
| 282 | - 'original' => $original_label, | |
| 283 | - 'modified' => $modified_label, | |
| 284 | - ]; | |
| 285 | - } | |
| 286 | - | |
| 287 | - return $modified_labels; | |
| 288 | - } | |
| 289 | - | |
| 290 | - | |
| 291 | - private function generate_unique_label( $original_label, $existing_labels ) { | |
| 292 | - $prefix = self::LABEL_PREFIX; | |
| 293 | - $max_length = self::MAX_LABEL_LENGTH; | |
| 294 | - | |
| 295 | - $has_prefix = strpos( $original_label, $prefix ) === 0; | |
| 296 | - | |
| 297 | - if ( $has_prefix ) { | |
| 298 | - $base_label = substr( $original_label, strlen( $prefix ) ); | |
| 299 | - | |
| 300 | - $counter = 1; | |
| 301 | - $new_label = $prefix . $base_label . $counter; | |
| 302 | - | |
| 303 | - while ( in_array( $new_label, $existing_labels, true ) ) { | |
| 304 | - ++$counter; | |
| 305 | - $new_label = $prefix . $base_label . $counter; | |
| 306 | - } | |
| 307 | - | |
| 308 | - if ( strlen( $new_label ) > $max_length ) { | |
| 309 | - $available_length = $max_length - strlen( $prefix . $counter ); | |
| 310 | - $base_label = substr( $base_label, 0, $available_length ); | |
| 311 | - $new_label = $prefix . $base_label . $counter; | |
| 312 | - } | |
| 313 | - } else { | |
| 314 | - $new_label = $prefix . $original_label; | |
| 315 | - | |
| 316 | - if ( strlen( $new_label ) > $max_length ) { | |
| 317 | - $available_length = $max_length - strlen( $prefix ); | |
| 318 | - $new_label = $prefix . substr( $original_label, 0, $available_length ); | |
| 319 | - } | |
| 320 | - | |
| 321 | - $counter = 1; | |
| 322 | - $base_label = substr( $original_label, 0, $available_length ?? strlen( $original_label ) ); | |
| 323 | - | |
| 324 | - while ( in_array( $new_label, $existing_labels, true ) ) { | |
| 325 | - $new_label = $prefix . $base_label . $counter; | |
| 326 | - | |
| 327 | - if ( strlen( $new_label ) > $max_length ) { | |
| 328 | - $available_length = $max_length - strlen( $prefix . $counter ); | |
| 329 | - $base_label = substr( $original_label, 0, $available_length ); | |
| 330 | - $new_label = $prefix . $base_label . $counter; | |
| 331 | - } | |
| 332 | - | |
| 333 | - ++$counter; | |
| 334 | - } | |
| 335 | - } | |
| 336 | - | |
| 337 | - return $new_label; | |
| 338 | 182 | } |
| 339 | 183 | } |