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