| 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 Get_Widget_Schema_Ability extends Abstract_Ability { |
| 13 |
|
| 14 |
protected function get_ability_id(): string { |
| 15 |
return 'elementor/get-widget-schema'; |
| 16 |
} |
| 17 |
|
| 18 |
protected function get_definition(): Ability_Definition { |
| 19 |
return new Ability_Definition( |
| 20 |
__( 'Get Elementor Widget Schema', 'elementor' ), |
| 21 |
Prompt_Loader::load( 'get-widget-schema' ), |
| 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 |
'required' => [ 'widget_type' ], |
| 37 |
'properties' => [ |
| 38 |
'widget_type' => [ |
| 39 |
'type' => 'string', |
| 40 |
'description' => 'Registry identifier of the widget, e.g. "e-heading".', |
| 41 |
], |
| 42 |
], |
| 43 |
] |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
public function execute( $input = [] ) { |
| 48 |
$input = is_array( $input ) ? $input : []; |
| 49 |
$widget_type = isset( $input['widget_type'] ) ? sanitize_key( $input['widget_type'] ) : ''; |
| 50 |
|
| 51 |
if ( ! $widget_type ) { |
| 52 |
return new \WP_Error( |
| 53 |
'invalid_input', |
| 54 |
__( 'widget_type is required.', 'elementor' ), |
| 55 |
[ 'status' => \WP_Http::BAD_REQUEST ] |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
$config = Widget_Context_Helper::get_widget_config( $widget_type ); |
| 60 |
|
| 61 |
if ( ! $config || ! Widget_Context_Helper::is_widget_eligible_for_llm( $config ) ) { |
| 62 |
return new \WP_Error( |
| 63 |
'elementor_not_found', |
| 64 |
/* translators: %s: widget type */ |
| 65 |
sprintf( __( 'Unknown widget type: %s.', 'elementor' ), $widget_type ), |
| 66 |
[ 'status' => \WP_Http::NOT_FOUND ] |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
$is_v3 = Widget_Context_Helper::VERSION_V3 === Widget_Context_Helper::get_widget_version( $config ); |
| 71 |
|
| 72 |
if ( $is_v3 && ! Widget_Context_Helper::is_v3_allowlisted( $widget_type ) ) { |
| 73 |
return new \WP_Error( |
| 74 |
'elementor_v3_not_supported', |
| 75 |
__( 'This is a legacy V3 widget and cannot be modified through this MCP. Edit V3 widgets directly in the Elementor editor.', 'elementor' ), |
| 76 |
[ |
| 77 |
'status' => \WP_Http::BAD_REQUEST, |
| 78 |
'widget_type' => $widget_type, |
| 79 |
'version' => 'v3', |
| 80 |
] |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
if ( $is_v3 ) { |
| 85 |
return Widget_Context_Helper::build_widget_schema( $widget_type, $config ); |
| 86 |
} |
| 87 |
|
| 88 |
$all_widget_configs = Widget_Context_Helper::get_llm_eligible_widgets(); |
| 89 |
$parents_index = Widget_Context_Helper::build_parents_index( $all_widget_configs ); |
| 90 |
|
| 91 |
return Widget_Context_Helper::build_widget_schema( $widget_type, $config, $parents_index ); |
| 92 |
} |
| 93 |
} |
| 94 |
|