| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Mcp\Abilities\Utils; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class V3_Controls_Metadata { |
| 10 |
|
| 11 |
const LAYOUT_CONTROL_TYPES = [ 'section', 'tab', 'tabs' ]; |
| 12 |
|
| 13 |
/** |
| 14 |
* @param mixed $controls Widget controls stack. |
| 15 |
* @param string[]|null $allowed_keys When provided, only these control keys are emitted. |
| 16 |
*/ |
| 17 |
public static function extract( $controls, ?array $allowed_keys = null ): array { |
| 18 |
if ( ! is_array( $controls ) || empty( $controls ) ) { |
| 19 |
return []; |
| 20 |
} |
| 21 |
|
| 22 |
$allowed_lookup = null === $allowed_keys |
| 23 |
? null |
| 24 |
: array_fill_keys( $allowed_keys, true ); |
| 25 |
|
| 26 |
$result = []; |
| 27 |
|
| 28 |
foreach ( $controls as $control_key => $control ) { |
| 29 |
if ( ! is_array( $control ) ) { |
| 30 |
continue; |
| 31 |
} |
| 32 |
|
| 33 |
if ( null !== $allowed_lookup && ! isset( $allowed_lookup[ $control_key ] ) ) { |
| 34 |
continue; |
| 35 |
} |
| 36 |
|
| 37 |
$control_type = is_string( $control['type'] ?? null ) ? $control['type'] : null; |
| 38 |
|
| 39 |
if ( $control_type && in_array( $control_type, self::LAYOUT_CONTROL_TYPES, true ) ) { |
| 40 |
continue; |
| 41 |
} |
| 42 |
|
| 43 |
$result[ $control_key ] = self::build_entry( $control, $control_type ); |
| 44 |
} |
| 45 |
|
| 46 |
return $result; |
| 47 |
} |
| 48 |
|
| 49 |
private static function build_entry( array $control, ?string $control_type ): array { |
| 50 |
$entry = []; |
| 51 |
|
| 52 |
if ( array_key_exists( 'default', $control ) ) { |
| 53 |
$entry['default'] = $control['default']; |
| 54 |
} |
| 55 |
|
| 56 |
if ( $control_type ) { |
| 57 |
$entry['type'] = $control_type; |
| 58 |
} |
| 59 |
|
| 60 |
if ( isset( $control['options'] ) ) { |
| 61 |
$options = $control['options']; |
| 62 |
$entry['options'] = ( is_array( $options ) && self::is_associative_array( $options ) ) |
| 63 |
? array_keys( $options ) |
| 64 |
: $options; |
| 65 |
} |
| 66 |
|
| 67 |
return $entry; |
| 68 |
} |
| 69 |
|
| 70 |
private static function is_associative_array( array $arr ): bool { |
| 71 |
return array_keys( $arr ) !== range( 0, count( $arr ) - 1 ); |
| 72 |
} |
| 73 |
} |
| 74 |
|