← All changes
|
modules/global-classes/global-classes-parser.php
+31
-88
4.2.0-dev2
→
3.32.2
View file →
| @@ -1,27 +1,21 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Elementor\Modules\GlobalClasses; |
| 4 | 4 | |
| 5 | +use Elementor\Core\Utils\Collection; | |
| 6 | +use Elementor\Modules\AtomicWidgets\Module; | |
| 7 | +use Elementor\Modules\AtomicWidgets\Opt_In; | |
| 5 | 8 | use Elementor\Core\Utils\Api\Parse_Result; |
| 6 | 9 | use Elementor\Modules\AtomicWidgets\Parsers\Style_Parser; |
| 7 | 10 | use Elementor\Modules\AtomicWidgets\Styles\Style_Schema; |
| 11 | +use Elementor\Plugin; | |
| 8 | 12 | |
| 9 | 13 | class Global_Classes_Parser { |
| 10 | - private ?Style_Parser $style_parser = null; | |
| 11 | - | |
| 12 | 14 | public static function make() { |
| 13 | 15 | return new static(); |
| 14 | 16 | } |
| 15 | 17 | |
| 16 | - private function get_style_parser(): Style_Parser { | |
| 17 | - if ( null === $this->style_parser ) { | |
| 18 | - $this->style_parser = Style_Parser::make( Style_Schema::get() ); | |
| 19 | - } | |
| 20 | - | |
| 21 | - return $this->style_parser; | |
| 22 | - } | |
| 23 | - | |
| 24 | 18 | public function parse( $data ): Parse_Result { |
| 25 | 19 | $result = Parse_Result::make(); |
| 26 | 20 | |
| 27 | 21 | if ( ! isset( $data['items'] ) ) { |
| @@ -58,12 +52,10 @@ | ||
| 58 | 52 | |
| 59 | 53 | return $result; |
| 60 | 54 | } |
| 61 | 55 | |
| 62 | - $sanitized_items = $items_result->unwrap(); | |
| 56 | + $order_result = $this->parse_order( $order, $items_result->unwrap() ); | |
| 63 | 57 | |
| 64 | - $order_result = $this->parse_order( $order, array_keys( $sanitized_items ) ); | |
| 65 | - | |
| 66 | 58 | if ( ! $order_result->is_valid() ) { |
| 67 | 59 | $result->errors()->merge( $order_result->errors(), 'order' ); |
| 68 | 60 | |
| 69 | 61 | return $result; |
| @@ -68,13 +60,11 @@ | ||
| 68 | 60 | |
| 69 | 61 | return $result; |
| 70 | 62 | } |
| 71 | 63 | |
| 72 | - $sanitized_order = $order_result->unwrap(); | |
| 73 | - | |
| 74 | 64 | return $result->wrap( [ |
| 75 | - 'items' => $sanitized_items, | |
| 76 | - 'order' => $sanitized_order, | |
| 65 | + 'items' => $items_result->unwrap(), | |
| 66 | + 'order' => $order_result->unwrap(), | |
| 77 | 67 | ] ); |
| 78 | 68 | } |
| 79 | 69 | |
| 80 | 70 | public function parse_items( array $items ) { |
| @@ -79,9 +69,10 @@ | ||
| 79 | 69 | |
| 80 | 70 | public function parse_items( array $items ) { |
| 81 | 71 | $sanitized_items = []; |
| 82 | 72 | $result = Parse_Result::make(); |
| 83 | - $style_parser = $this->get_style_parser(); | |
| 73 | + $style_parser = Style_Parser::make( Style_Schema::get() ); | |
| 74 | + $existing_labels = []; | |
| 84 | 75 | |
| 85 | 76 | foreach ( $items as $item_id => $item ) { |
| 86 | 77 | $item_result = $style_parser->parse( $item ); |
| 87 | 78 | |
| @@ -98,89 +89,41 @@ | ||
| 98 | 89 | |
| 99 | 90 | continue; |
| 100 | 91 | } |
| 101 | 92 | |
| 93 | + if ( Plugin::$instance->experiments->is_feature_active( Opt_In::EXPERIMENT_NAME ) ) { | |
| 94 | + if ( in_array( $sanitized_item['label'], $existing_labels, true ) ) { | |
| 95 | + $result->errors()->add( "$item_id.id", 'duplicated_class_label' ); | |
| 96 | + | |
| 97 | + continue; | |
| 98 | + } | |
| 99 | + } | |
| 100 | + | |
| 102 | 101 | $sanitized_items[ $sanitized_item['id'] ] = $sanitized_item; |
| 102 | + $existing_labels[] = $sanitized_item['label']; | |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | return $result->wrap( $sanitized_items ); |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | - public function parse_order( array $order, array $final_item_ids ) { | |
| 108 | + public function parse_order( array $order, array $items ): Parse_Result { | |
| 109 | 109 | $result = Parse_Result::make(); |
| 110 | 110 | |
| 111 | - $expected_ids = array_values( $final_item_ids ); | |
| 112 | - $order_unique = array_values( array_unique( array_filter( $order, 'is_string' ) ) ); | |
| 111 | + $items = Collection::make( $items ); | |
| 113 | 112 | |
| 114 | - $missing_ids = array_diff( $expected_ids, $order_unique ); | |
| 115 | - $excess_ids = array_diff( $order_unique, $expected_ids ); | |
| 113 | + $order = Collection::make( $order ) | |
| 114 | + ->filter( fn( $item ) => is_string( $item ) ) | |
| 115 | + ->unique(); | |
| 116 | 116 | |
| 117 | - foreach ( $missing_ids as $id ) { | |
| 118 | - $result->errors()->add( $id, 'missing' ); | |
| 119 | - } | |
| 120 | - foreach ( $excess_ids as $id ) { | |
| 121 | - $result->errors()->add( $id, 'excess' ); | |
| 122 | - } | |
| 117 | + $existing_ids = $items->keys(); | |
| 123 | 118 | |
| 119 | + $excess_ids = $order->diff( $existing_ids ); | |
| 120 | + $missing_ids = $existing_ids->diff( $order ); | |
| 121 | + | |
| 122 | + $excess_ids->each( fn( $id ) => $result->errors()->add( $id, 'excess' ) ); | |
| 123 | + $missing_ids->each( fn( $id ) => $result->errors()->add( $id, 'missing' ) ); | |
| 124 | + | |
| 124 | 125 | return $result->is_valid() |
| 125 | - ? $result->wrap( $order_unique ) | |
| 126 | + ? $result->wrap( $order->values() ) | |
| 126 | 127 | : $result; |
| 127 | - } | |
| 128 | - | |
| 129 | - public static function check_for_duplicate_labels( | |
| 130 | - array $label_by_id, | |
| 131 | - array $deleted_ids, | |
| 132 | - array $items, | |
| 133 | - array $new_items_ids | |
| 134 | - ) { | |
| 135 | - if ( empty( $new_items_ids ) ) { | |
| 136 | - return []; | |
| 137 | - } | |
| 138 | - | |
| 139 | - $new_added_items = array_filter( | |
| 140 | - $items, | |
| 141 | - fn( $item ) => in_array( $item['id'], $new_items_ids, true ) | |
| 142 | - ); | |
| 143 | - | |
| 144 | - $duplicates = []; | |
| 145 | - | |
| 146 | - foreach ( $new_added_items as $item ) { | |
| 147 | - $item_id = $item['id']; | |
| 148 | - $label = $item['label']; | |
| 149 | - | |
| 150 | - foreach ( $label_by_id as $other_id => $other_label ) { | |
| 151 | - if ( in_array( $other_id, $deleted_ids, true ) ) { | |
| 152 | - continue; | |
| 153 | - } | |
| 154 | - | |
| 155 | - if ( $other_id === $item_id ) { | |
| 156 | - continue; | |
| 157 | - } | |
| 158 | - | |
| 159 | - if ( $other_label === $label ) { | |
| 160 | - $duplicates[] = [ | |
| 161 | - 'item_id' => $item_id, | |
| 162 | - 'label' => $label, | |
| 163 | - ]; | |
| 164 | - break; | |
| 165 | - } | |
| 166 | - } | |
| 167 | - } | |
| 168 | - | |
| 169 | - return $duplicates; | |
| 170 | - } | |
| 171 | - | |
| 172 | - public static function sanitize_order( array $items, array $order ): array { | |
| 173 | - if ( empty( $items ) ) { | |
| 174 | - return []; | |
| 175 | - } | |
| 176 | - | |
| 177 | - $item_ids = array_keys( $items ); | |
| 178 | - $order = array_filter( $order, 'is_string' ); | |
| 179 | - $order = array_unique( $order ); | |
| 180 | - $order_existing = array_values( array_intersect( $order, $item_ids ) ); | |
| 181 | - $missing = array_diff( $item_ids, $order_existing ); | |
| 182 | - sort( $missing, SORT_STRING ); | |
| 183 | - | |
| 184 | - return array_merge( $order_existing, $missing ); | |
| 185 | 128 | } |
| 186 | 129 | } |