| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Mcp\Abilities\Utils; |
| 4 |
|
| 5 |
use Elementor\Modules\AtomicWidgets\Styles\Style_Props_To_Css; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
class Llm_Guidance_Builder { |
| 12 |
|
| 13 |
const DEFAULT_STYLES_INSTRUCTION = 'These are the default styles applied to the widget. Override only when necessary.'; |
| 14 |
|
| 15 |
public static function build( array $config, string $widget_type, array $parents_index ): array { |
| 16 |
$guidance = [ |
| 17 |
'can_have_children' => ! empty( $config['meta']['is_container'] ), |
| 18 |
]; |
| 19 |
|
| 20 |
$default_styles = self::collect_default_styles( $config['base_styles'] ?? [] ); |
| 21 |
|
| 22 |
$instructions = array_filter( [ |
| 23 |
! empty( $default_styles ) ? self::DEFAULT_STYLES_INSTRUCTION : null, |
| 24 |
] ); |
| 25 |
|
| 26 |
if ( $instructions ) { |
| 27 |
$guidance['instructions'] = implode( ' ', $instructions ); |
| 28 |
} |
| 29 |
|
| 30 |
if ( ! empty( $default_styles ) ) { |
| 31 |
$guidance['default_styles'] = $default_styles; |
| 32 |
} |
| 33 |
|
| 34 |
$nesting = self::build_nesting( $config, $widget_type, $parents_index ); |
| 35 |
|
| 36 |
if ( ! empty( $nesting ) ) { |
| 37 |
$guidance['nesting'] = $nesting; |
| 38 |
} |
| 39 |
|
| 40 |
$required_children = self::get_required_default_child_types( $config ); |
| 41 |
|
| 42 |
if ( ! empty( $required_children ) ) { |
| 43 |
$guidance['required_direct_children'] = $required_children; |
| 44 |
} |
| 45 |
|
| 46 |
return $guidance; |
| 47 |
} |
| 48 |
|
| 49 |
private static function collect_default_styles( array $base_styles ): array { |
| 50 |
$default_styles = []; |
| 51 |
|
| 52 |
foreach ( $base_styles as $style ) { |
| 53 |
foreach ( $style['variants'] ?? [] as $variant ) { |
| 54 |
$default_styles = array_merge( $default_styles, $variant['props'] ?? [] ); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return Style_Props_To_Css::to_map( $default_styles ); |
| 59 |
} |
| 60 |
|
| 61 |
private static function build_nesting( array $config, string $widget_type, array $parents_index ): array { |
| 62 |
$allowed_child_types = $config['allowed_child_types'] ?? []; |
| 63 |
$emit_allowed_parents = empty( $config['show_in_panel'] ); |
| 64 |
$allowed_parents = $emit_allowed_parents ? ( $parents_index[ $widget_type ] ?? [] ) : []; |
| 65 |
|
| 66 |
return array_filter( |
| 67 |
[ |
| 68 |
'allowed_child_types' => empty( $allowed_child_types ) ? null : $allowed_child_types, |
| 69 |
'allowed_parents' => empty( $allowed_parents ) ? null : $allowed_parents, |
| 70 |
], |
| 71 |
fn( $value ) => null !== $value |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
private static function get_required_default_child_types( array $config ): array { |
| 76 |
$default_children = $config['default_children'] ?? null; |
| 77 |
|
| 78 |
if ( ! is_array( $default_children ) ) { |
| 79 |
return []; |
| 80 |
} |
| 81 |
|
| 82 |
return Default_Children_Utils::get_required_child_types( $default_children ); |
| 83 |
} |
| 84 |
} |
| 85 |
|