PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.4
Elementor Website Builder – more than just a page builder v4.3.4
4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 All 457 releases
elementor / modules / mcp / abilities / utils / llm-guidance-builder.php

llm-guidance-builder.php in Elementor Website Builder – more than just a page builder 4.3.4, at modules/mcp/abilities/utils/llm-guidance-builder.php

89 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 if ( Atomic_Container_Presentation::applies_to( $config ) ) {
23 $default_styles = array_merge( Atomic_Container_Presentation::SHELL_STYLES, $default_styles );
24 }
25
26 $instructions = array_filter( [
27 ! empty( $default_styles ) ? self::DEFAULT_STYLES_INSTRUCTION : null,
28 ] );
29
30 if ( $instructions ) {
31 $guidance['instructions'] = implode( ' ', $instructions );
32 }
33
34 if ( ! empty( $default_styles ) ) {
35 $guidance['default_styles'] = $default_styles;
36 }
37
38 $nesting = self::build_nesting( $config, $widget_type, $parents_index );
39
40 if ( ! empty( $nesting ) ) {
41 $guidance['nesting'] = $nesting;
42 }
43
44 $required_children = self::get_required_default_child_types( $config );
45
46 if ( ! empty( $required_children ) ) {
47 $guidance['required_direct_children'] = $required_children;
48 }
49
50 return $guidance;
51 }
52
53 private static function collect_default_styles( array $base_styles ): array {
54 $default_styles = [];
55
56 foreach ( $base_styles as $style ) {
57 foreach ( $style['variants'] ?? [] as $variant ) {
58 $default_styles = array_merge( $default_styles, $variant['props'] ?? [] );
59 }
60 }
61
62 return Style_Props_To_Css::to_map( $default_styles );
63 }
64
65 private static function build_nesting( array $config, string $widget_type, array $parents_index ): array {
66 $allowed_child_types = $config['allowed_child_types'] ?? [];
67 $emit_allowed_parents = empty( $config['show_in_panel'] );
68 $allowed_parents = $emit_allowed_parents ? ( $parents_index[ $widget_type ] ?? [] ) : [];
69
70 return array_filter(
71 [
72 'allowed_child_types' => empty( $allowed_child_types ) ? null : $allowed_child_types,
73 'allowed_parents' => empty( $allowed_parents ) ? null : $allowed_parents,
74 ],
75 fn( $value ) => null !== $value
76 );
77 }
78
79 private static function get_required_default_child_types( array $config ): array {
80 $default_children = $config['default_children'] ?? null;
81
82 if ( ! is_array( $default_children ) ) {
83 return [];
84 }
85
86 return Default_Children_Utils::get_required_child_types( $default_children );
87 }
88 }
89