| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\GlobalClasses; |
| 4 |
|
| 5 |
use Elementor\Core\Utils\Api\Parse_Result; |
| 6 |
use Elementor\Modules\AtomicWidgets\Parsers\Style_Parser; |
| 7 |
use Elementor\Modules\AtomicWidgets\Styles\Style_Schema; |
| 8 |
|
| 9 |
class Global_Classes_Parser { |
| 10 |
private ?Style_Parser $style_parser = null; |
| 11 |
|
| 12 |
public static function make() { |
| 13 |
return new static(); |
| 14 |
} |
| 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 |
|
| 24 |
public function parse( $data ): Parse_Result { |
| 25 |
$result = Parse_Result::make(); |
| 26 |
|
| 27 |
if ( ! isset( $data['items'] ) ) { |
| 28 |
$result->errors()->add( 'items', 'missing' ); |
| 29 |
|
| 30 |
return $result; |
| 31 |
} |
| 32 |
|
| 33 |
if ( ! isset( $data['order'] ) ) { |
| 34 |
$result->errors()->add( 'order', 'missing' ); |
| 35 |
|
| 36 |
return $result; |
| 37 |
} |
| 38 |
|
| 39 |
$items = $data['items']; |
| 40 |
$order = $data['order']; |
| 41 |
|
| 42 |
if ( ! is_array( $items ) ) { |
| 43 |
$result->errors()->add( 'items', 'invalid' ); |
| 44 |
|
| 45 |
return $result; |
| 46 |
} |
| 47 |
|
| 48 |
if ( ! is_array( $order ) ) { |
| 49 |
$result->errors()->add( 'order', 'invalid' ); |
| 50 |
|
| 51 |
return $result; |
| 52 |
} |
| 53 |
|
| 54 |
$items_result = $this->parse_items( $items ); |
| 55 |
|
| 56 |
if ( ! $items_result->is_valid() ) { |
| 57 |
$result->errors()->merge( $items_result->errors(), 'items' ); |
| 58 |
|
| 59 |
return $result; |
| 60 |
} |
| 61 |
|
| 62 |
$sanitized_items = $items_result->unwrap(); |
| 63 |
|
| 64 |
$order_result = $this->parse_order( $order, array_keys( $sanitized_items ) ); |
| 65 |
|
| 66 |
if ( ! $order_result->is_valid() ) { |
| 67 |
$result->errors()->merge( $order_result->errors(), 'order' ); |
| 68 |
|
| 69 |
return $result; |
| 70 |
} |
| 71 |
|
| 72 |
$sanitized_order = $order_result->unwrap(); |
| 73 |
|
| 74 |
return $result->wrap( [ |
| 75 |
'items' => $sanitized_items, |
| 76 |
'order' => $sanitized_order, |
| 77 |
] ); |
| 78 |
} |
| 79 |
|
| 80 |
public function parse_items( array $items ) { |
| 81 |
$sanitized_items = []; |
| 82 |
$result = Parse_Result::make(); |
| 83 |
$style_parser = $this->get_style_parser(); |
| 84 |
|
| 85 |
foreach ( $items as $item_id => $item ) { |
| 86 |
$item_result = $style_parser->parse( $item ); |
| 87 |
|
| 88 |
if ( ! $item_result->is_valid() ) { |
| 89 |
$result->errors()->merge( $item_result->errors(), $item_id ); |
| 90 |
|
| 91 |
continue; |
| 92 |
} |
| 93 |
|
| 94 |
$sanitized_item = $item_result->unwrap(); |
| 95 |
|
| 96 |
if ( $item_id !== $sanitized_item['id'] ) { |
| 97 |
$result->errors()->add( "$item_id.id", 'mismatching_value' ); |
| 98 |
|
| 99 |
continue; |
| 100 |
} |
| 101 |
|
| 102 |
$sanitized_items[ $sanitized_item['id'] ] = $sanitized_item; |
| 103 |
} |
| 104 |
|
| 105 |
return $result->wrap( $sanitized_items ); |
| 106 |
} |
| 107 |
|
| 108 |
public function parse_order( array $order, array $final_item_ids ) { |
| 109 |
$result = Parse_Result::make(); |
| 110 |
|
| 111 |
$expected_ids = array_values( $final_item_ids ); |
| 112 |
$order_unique = array_values( array_unique( array_filter( $order, 'is_string' ) ) ); |
| 113 |
|
| 114 |
$missing_ids = array_diff( $expected_ids, $order_unique ); |
| 115 |
$excess_ids = array_diff( $order_unique, $expected_ids ); |
| 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 |
} |
| 123 |
|
| 124 |
return $result->is_valid() |
| 125 |
? $result->wrap( $order_unique ) |
| 126 |
: $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 |
} |
| 186 |
} |
| 187 |
|