| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Mcp\Abilities\Appliers\V3; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Indexes a V3 widget's controls for generic CSS-property → setting reverse lookup. |
| 11 |
* |
| 12 |
* Only considers simple selector-backed controls (not Group_Control_* siblings). |
| 13 |
* Group controls are handled exclusively via registry style_overrides. |
| 14 |
*/ |
| 15 |
class V3_Style_Settings_Index { |
| 16 |
|
| 17 |
const GROUP_CONTROL_PREFIXES = [ |
| 18 |
'typography_', |
| 19 |
'text_stroke_', |
| 20 |
'text_shadow_', |
| 21 |
'box_shadow_', |
| 22 |
'border_', |
| 23 |
'background_', |
| 24 |
'css_filters_', |
| 25 |
'image_border_', |
| 26 |
'image_box_shadow_', |
| 27 |
'caption_typography_', |
| 28 |
'caption_text_shadow_', |
| 29 |
'menu_typography_', |
| 30 |
'dropdown_typography_', |
| 31 |
'dropdown_border_', |
| 32 |
'dropdown_box_shadow_', |
| 33 |
'dropdown_divider_', |
| 34 |
]; |
| 35 |
|
| 36 |
/** |
| 37 |
* @param array $controls Widget controls from get_config()['controls']. |
| 38 |
* @param array<string, array> $style_overrides Registry overrides (excluded from generic index). |
| 39 |
* @return array<string, array{setting: string, resolver: string, responsive: bool}> |
| 40 |
* Keyed by "property" or "property@state". Only unique matches are kept. |
| 41 |
*/ |
| 42 |
public static function build( array $controls, array $style_overrides = [] ): array { |
| 43 |
$candidates = []; |
| 44 |
|
| 45 |
foreach ( $controls as $setting_key => $control ) { |
| 46 |
if ( ! is_array( $control ) || ! is_string( $setting_key ) ) { |
| 47 |
continue; |
| 48 |
} |
| 49 |
|
| 50 |
if ( self::is_group_control_key( $setting_key ) ) { |
| 51 |
continue; |
| 52 |
} |
| 53 |
|
| 54 |
$selectors = $control['selectors'] ?? null; |
| 55 |
if ( ! is_array( $selectors ) || empty( $selectors ) ) { |
| 56 |
continue; |
| 57 |
} |
| 58 |
|
| 59 |
foreach ( $selectors as $selector_template => $css_template ) { |
| 60 |
if ( ! is_string( $selector_template ) || ! is_string( $css_template ) ) { |
| 61 |
continue; |
| 62 |
} |
| 63 |
|
| 64 |
$property = self::extract_css_property( $css_template ); |
| 65 |
if ( null === $property ) { |
| 66 |
continue; |
| 67 |
} |
| 68 |
|
| 69 |
$state = self::extract_pseudo_state( $selector_template ); |
| 70 |
$match_key = null === $state ? $property : $property . '@' . $state; |
| 71 |
|
| 72 |
if ( isset( $style_overrides[ $match_key ] ) ) { |
| 73 |
continue; |
| 74 |
} |
| 75 |
|
| 76 |
$candidates[ $match_key ][] = [ |
| 77 |
'setting' => $setting_key, |
| 78 |
'resolver' => self::infer_resolver( $control, $property ), |
| 79 |
'responsive' => isset( $controls[ $setting_key . '_tablet' ] ) || isset( $controls[ $setting_key . '_mobile' ] ), |
| 80 |
]; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
$index = []; |
| 85 |
foreach ( $candidates as $match_key => $matches ) { |
| 86 |
if ( 1 !== count( $matches ) ) { |
| 87 |
continue; |
| 88 |
} |
| 89 |
$index[ $match_key ] = $matches[0]; |
| 90 |
} |
| 91 |
|
| 92 |
return $index; |
| 93 |
} |
| 94 |
|
| 95 |
private static function is_group_control_key( string $setting_key ): bool { |
| 96 |
foreach ( self::GROUP_CONTROL_PREFIXES as $prefix ) { |
| 97 |
if ( str_starts_with( $setting_key, $prefix ) ) { |
| 98 |
return true; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
private static function extract_css_property( string $css_template ): ?string { |
| 106 |
if ( ! preg_match( '/^\s*([a-zA-Z-]+)\s*:/', $css_template, $matches ) ) { |
| 107 |
return null; |
| 108 |
} |
| 109 |
|
| 110 |
return strtolower( $matches[1] ); |
| 111 |
} |
| 112 |
|
| 113 |
private static function extract_pseudo_state( string $selector_template ): ?string { |
| 114 |
if ( preg_match( '/:(hover|focus|active)\b/i', $selector_template, $matches ) ) { |
| 115 |
return strtolower( $matches[1] ); |
| 116 |
} |
| 117 |
|
| 118 |
return null; |
| 119 |
} |
| 120 |
|
| 121 |
private static function infer_resolver( array $control, string $property ): string { |
| 122 |
$type = $control['type'] ?? null; |
| 123 |
|
| 124 |
if ( 'color' === $type || str_ends_with( $property, 'color' ) || 'fill' === $property ) { |
| 125 |
return 'color'; |
| 126 |
} |
| 127 |
|
| 128 |
if ( in_array( $property, [ 'padding', 'margin', 'border-radius' ], true ) ) { |
| 129 |
return 'sides'; |
| 130 |
} |
| 131 |
|
| 132 |
if ( in_array( $type, [ 'slider', 'dimensions' ], true ) ) { |
| 133 |
return 'dimensions' === $type ? 'sides' : 'dimension'; |
| 134 |
} |
| 135 |
|
| 136 |
if ( in_array( $property, [ 'width', 'height', 'max-width', 'min-height', 'font-size', 'line-height', 'letter-spacing', 'word-spacing', 'opacity', 'top', 'right', 'bottom', 'left' ], true ) ) { |
| 137 |
return 'dimension'; |
| 138 |
} |
| 139 |
|
| 140 |
if ( 'box-shadow' === $property ) { |
| 141 |
return 'box_shadow'; |
| 142 |
} |
| 143 |
|
| 144 |
if ( 'border' === $property ) { |
| 145 |
return 'border'; |
| 146 |
} |
| 147 |
|
| 148 |
return 'text'; |
| 149 |
} |
| 150 |
} |
| 151 |
|