| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Mcp\Abilities; |
| 4 |
|
| 5 |
use Elementor\Modules\Mcp\Abilities\Utils\Prompt_Loader; |
| 6 |
use Elementor\Modules\Mcp\Abilities\Utils\Widget_Context_Helper; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
class List_Widget_Schemas_Ability extends Abstract_Ability { |
| 13 |
|
| 14 |
protected function get_ability_id(): string { |
| 15 |
return 'elementor/list-widget-schemas'; |
| 16 |
} |
| 17 |
|
| 18 |
protected function get_definition(): Ability_Definition { |
| 19 |
return new Ability_Definition( |
| 20 |
__( 'List Elementor Widget Schemas', 'elementor' ), |
| 21 |
Prompt_Loader::load( 'list-widget-schemas' ), |
| 22 |
'elementor', |
| 23 |
[ 'type' => 'object' ], |
| 24 |
[ |
| 25 |
'annotations' => [ |
| 26 |
'readonly' => true, |
| 27 |
'idempotent' => true, |
| 28 |
'destructive' => false, |
| 29 |
], |
| 30 |
], |
| 31 |
function () { |
| 32 |
return current_user_can( 'edit_posts' ); |
| 33 |
}, |
| 34 |
[ |
| 35 |
'type' => 'object', |
| 36 |
'properties' => [ |
| 37 |
'summary' => [ |
| 38 |
'type' => 'boolean', |
| 39 |
'default' => false, |
| 40 |
'description' => 'When true, returns an array of { type, description } for discovery instead of full schemas.', |
| 41 |
], |
| 42 |
], |
| 43 |
] |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
public function execute( $input = [] ) { |
| 48 |
$input = is_array( $input ) ? $input : []; |
| 49 |
$summary = ! empty( $input['summary'] ); |
| 50 |
|
| 51 |
$widgets = array_filter( |
| 52 |
Widget_Context_Helper::get_llm_eligible_widgets(), |
| 53 |
static function ( $config, $type ) { |
| 54 |
if ( Widget_Context_Helper::VERSION_V4 === Widget_Context_Helper::get_widget_version( $config ) ) { |
| 55 |
return true; |
| 56 |
} |
| 57 |
|
| 58 |
return Widget_Context_Helper::is_v3_allowlisted( (string) $type ); |
| 59 |
}, |
| 60 |
ARRAY_FILTER_USE_BOTH |
| 61 |
); |
| 62 |
|
| 63 |
if ( $summary ) { |
| 64 |
return $this->build_summaries( $widgets ); |
| 65 |
} |
| 66 |
|
| 67 |
return $this->build_schemas( $widgets ); |
| 68 |
} |
| 69 |
|
| 70 |
private function build_summaries( array $widgets ): array { |
| 71 |
$summaries = []; |
| 72 |
|
| 73 |
foreach ( $widgets as $widget_type => $config ) { |
| 74 |
$full_summary = Widget_Context_Helper::build_widget_summary( $widget_type, $config ); |
| 75 |
$summaries[] = [ |
| 76 |
'type' => $full_summary['type'], |
| 77 |
'description' => $full_summary['description'] ?? null, |
| 78 |
]; |
| 79 |
} |
| 80 |
|
| 81 |
return [ 'widgets' => $summaries ]; |
| 82 |
} |
| 83 |
|
| 84 |
private function build_schemas( array $widgets ): array { |
| 85 |
$parents_index = Widget_Context_Helper::build_parents_index( $widgets ); |
| 86 |
|
| 87 |
$schemas = []; |
| 88 |
|
| 89 |
foreach ( $widgets as $widget_type => $config ) { |
| 90 |
$schemas[ $widget_type ] = Widget_Context_Helper::build_widget_schema( $widget_type, $config, $parents_index ); |
| 91 |
} |
| 92 |
|
| 93 |
return $schemas; |
| 94 |
} |
| 95 |
} |
| 96 |
|