← All changes
|
modules/global-classes/global-classes-parser.php
+32
-78
4.2.0-dev2
→
3.34.1
View file →
| @@ -1,27 +1,20 @@ | ||
| 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\OptIn\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; |
| 8 | 11 | |
| 9 | 12 | class Global_Classes_Parser { |
| 10 | - private ?Style_Parser $style_parser = null; | |
| 11 | - | |
| 12 | 13 | public static function make() { |
| 13 | 14 | return new static(); |
| 14 | 15 | } |
| 15 | 16 | |
| 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 | 17 | public function parse( $data ): Parse_Result { |
| 25 | 18 | $result = Parse_Result::make(); |
| 26 | 19 | |
| 27 | 20 | if ( ! isset( $data['items'] ) ) { |
| @@ -58,12 +51,10 @@ | ||
| 58 | 51 | |
| 59 | 52 | return $result; |
| 60 | 53 | } |
| 61 | 54 | |
| 62 | - $sanitized_items = $items_result->unwrap(); | |
| 55 | + $order_result = $this->parse_order( $order, $items_result->unwrap() ); | |
| 63 | 56 | |
| 64 | - $order_result = $this->parse_order( $order, array_keys( $sanitized_items ) ); | |
| 65 | - | |
| 66 | 57 | if ( ! $order_result->is_valid() ) { |
| 67 | 58 | $result->errors()->merge( $order_result->errors(), 'order' ); |
| 68 | 59 | |
| 69 | 60 | return $result; |
| @@ -68,13 +59,11 @@ | ||
| 68 | 59 | |
| 69 | 60 | return $result; |
| 70 | 61 | } |
| 71 | 62 | |
| 72 | - $sanitized_order = $order_result->unwrap(); | |
| 73 | - | |
| 74 | 63 | return $result->wrap( [ |
| 75 | - 'items' => $sanitized_items, | |
| 76 | - 'order' => $sanitized_order, | |
| 64 | + 'items' => $items_result->unwrap(), | |
| 65 | + 'order' => $order_result->unwrap(), | |
| 77 | 66 | ] ); |
| 78 | 67 | } |
| 79 | 68 | |
| 80 | 69 | public function parse_items( array $items ) { |
| @@ -79,9 +68,10 @@ | ||
| 79 | 68 | |
| 80 | 69 | public function parse_items( array $items ) { |
| 81 | 70 | $sanitized_items = []; |
| 82 | 71 | $result = Parse_Result::make(); |
| 83 | - $style_parser = $this->get_style_parser(); | |
| 72 | + $style_parser = Style_Parser::make( Style_Schema::get() ); | |
| 73 | + $existing_labels = []; | |
| 84 | 74 | |
| 85 | 75 | foreach ( $items as $item_id => $item ) { |
| 86 | 76 | $item_result = $style_parser->parse( $item ); |
| 87 | 77 | |
| @@ -99,88 +89,52 @@ | ||
| 99 | 89 | continue; |
| 100 | 90 | } |
| 101 | 91 | |
| 102 | 92 | $sanitized_items[ $sanitized_item['id'] ] = $sanitized_item; |
| 93 | + $existing_labels[] = $sanitized_item['label']; | |
| 103 | 94 | } |
| 104 | 95 | |
| 105 | 96 | return $result->wrap( $sanitized_items ); |
| 106 | 97 | } |
| 107 | 98 | |
| 108 | - public function parse_order( array $order, array $final_item_ids ) { | |
| 99 | + public function parse_order( array $order, array $items ): Parse_Result { | |
| 109 | 100 | $result = Parse_Result::make(); |
| 110 | 101 | |
| 111 | - $expected_ids = array_values( $final_item_ids ); | |
| 112 | - $order_unique = array_values( array_unique( array_filter( $order, 'is_string' ) ) ); | |
| 102 | + $items = Collection::make( $items ); | |
| 113 | 103 | |
| 114 | - $missing_ids = array_diff( $expected_ids, $order_unique ); | |
| 115 | - $excess_ids = array_diff( $order_unique, $expected_ids ); | |
| 104 | + $order = Collection::make( $order ) | |
| 105 | + ->filter( fn( $item ) => is_string( $item ) ) | |
| 106 | + ->unique(); | |
| 116 | 107 | |
| 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 | - } | |
| 108 | + $existing_ids = $items->keys(); | |
| 123 | 109 | |
| 110 | + $excess_ids = $order->diff( $existing_ids ); | |
| 111 | + $missing_ids = $existing_ids->diff( $order ); | |
| 112 | + | |
| 113 | + $excess_ids->each( fn( $id ) => $result->errors()->add( $id, 'excess' ) ); | |
| 114 | + $missing_ids->each( fn( $id ) => $result->errors()->add( $id, 'missing' ) ); | |
| 115 | + | |
| 124 | 116 | return $result->is_valid() |
| 125 | - ? $result->wrap( $order_unique ) | |
| 117 | + ? $result->wrap( $order->values() ) | |
| 126 | 118 | : $result; |
| 127 | 119 | } |
| 128 | 120 | |
| 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 | - ) { | |
| 121 | + public static function check_for_duplicate_labels( array $existing_labels, array $items, array $new_items_ids ) { | |
| 122 | + | |
| 135 | 123 | if ( empty( $new_items_ids ) ) { |
| 136 | - return []; | |
| 124 | + return false; | |
| 137 | 125 | } |
| 126 | + $new_added_items = array_filter( $items, fn( $item ) => in_array( $item['id'], $new_items_ids, true ) ); | |
| 138 | 127 | |
| 139 | - $new_added_items = array_filter( | |
| 140 | - $items, | |
| 141 | - fn( $item ) => in_array( $item['id'], $new_items_ids, true ) | |
| 142 | - ); | |
| 143 | - | |
| 144 | 128 | $duplicates = []; |
| 145 | 129 | |
| 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 | - } | |
| 130 | + foreach ( $new_added_items as $item_id => $item ) { | |
| 131 | + if ( in_array( $item['label'], $existing_labels, true ) ) { | |
| 132 | + $duplicates[] = [ | |
| 133 | + 'item_id' => $item_id, | |
| 134 | + 'label' => $item['label'], | |
| 135 | + ]; | |
| 166 | 136 | } |
| 167 | 137 | } |
| 168 | - | |
| 169 | 138 | 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 | 139 | } |
| 186 | 140 | } |