| @@ -2,16 +2,10 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Elementor\Modules\Mcp; |
| 4 | 4 | |
| 5 | 5 | use Elementor\Core\Base\Module as BaseModule; |
| 6 | -use Elementor\MCP\Composer\Mcp\Registry as Shared_Registry; | |
| 7 | -use Elementor\Modules\EditorOne\Classes\Menu_Data_Provider; | |
| 8 | -use Elementor\Modules\Mcp\Abilities\Abstract_Ability; | |
| 9 | -use Elementor\Modules\Mcp\AdminMenuItems\Editor_One_Mcp_Menu; | |
| 10 | -use Elementor\Modules\Mcp\Preview\Public_Preview_Handler; | |
| 11 | -use Elementor\Modules\Mcp\Registry\Ability_Registry; | |
| 12 | -use Elementor\Modules\Mcp\RestApi\Mcp_Proxy_REST_API; | |
| 13 | -use Elementor\Modules\Mcp\Utils\Editor_Sync_State; | |
| 6 | +use Elementor\Plugin; | |
| 7 | +use Elementor\Core\Experiments\Manager as ExperimentsManager; | |
| 14 | 8 | use WP\MCP\Core\McpAdapter; |
| 15 | 9 | |
| 16 | 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | 11 | exit; |
| @@ -17,56 +11,44 @@ | ||
| 17 | 11 | exit; |
| 18 | 12 | } |
| 19 | 13 | |
| 20 | 14 | class Module extends BaseModule { |
| 15 | + const EXPERIMENT_NAME = 'e_wp_abilities_api'; | |
| 21 | 16 | |
| 22 | - const ANALYTICS_REGISTRAR_HANDLE = 'elementor-mcp-analytics-registrar'; | |
| 23 | - | |
| 24 | - private Ability_Registry $registry; | |
| 25 | - | |
| 26 | 17 | public function get_name() { |
| 27 | 18 | return 'mcp'; |
| 28 | 19 | } |
| 29 | 20 | |
| 30 | - public function enqueue_analytics_registrar(): void { | |
| 31 | - wp_enqueue_script( | |
| 32 | - self::ANALYTICS_REGISTRAR_HANDLE, | |
| 33 | - $this->get_js_assets_url( 'mcp-analytics-registrar' ), | |
| 34 | - [ 'elementor-common', \Elementor\MCP\Composer\Admin\Page::SCRIPT_HANDLE ], | |
| 35 | - ELEMENTOR_VERSION, | |
| 36 | - true | |
| 37 | - ); | |
| 38 | - } | |
| 39 | - | |
| 40 | 21 | public static function is_active() { |
| 41 | 22 | return class_exists( McpAdapter::class ) && |
| 42 | 23 | function_exists( 'wp_register_ability' ) && |
| 43 | - class_exists( Shared_Registry::class ); | |
| 24 | + Plugin::instance()->experiments->is_feature_active( self::EXPERIMENT_NAME ); | |
| 44 | 25 | } |
| 45 | 26 | |
| 27 | + public static function get_experimental_data() { | |
| 28 | + return [ | |
| 29 | + 'name' => self::EXPERIMENT_NAME, | |
| 30 | + 'title' => __( 'Elementor MCP WP Abilities API', 'elementor' ), | |
| 31 | + 'description' => __( 'Enable Elementor MCP WP Abilities API. Requirements: 1. WordPress 7.0 or higher. 2. Create an application password for your agent user. 3. Add to your MCP config: {url: "https://<your-site-url>/wp-json/elementor/mcp", headers: {Authorization: "Basic <base64(user:application-password)>"}}', 'elementor' ), | |
| 32 | + 'hidden' => true, | |
| 33 | + 'default' => ExperimentsManager::STATE_INACTIVE, | |
| 34 | + ]; | |
| 35 | + } | |
| 36 | + | |
| 46 | 37 | public function __construct() { |
| 47 | 38 | parent::__construct(); |
| 48 | 39 | |
| 49 | - $this->registry = self::build_core_registry(); | |
| 50 | - | |
| 51 | - ( new Mcp_Proxy_REST_API( $this->registry ) )->register_hooks(); | |
| 52 | - ( new Public_Preview_Handler() )->register(); | |
| 53 | - ( new Editor_Sync_State() )->register_hooks(); | |
| 54 | - | |
| 55 | 40 | if ( ! $this->is_active() ) { |
| 56 | 41 | return; |
| 57 | 42 | } |
| 58 | 43 | |
| 44 | + McpAdapter::instance(); | |
| 45 | + | |
| 59 | 46 | add_action( 'wp_abilities_api_categories_init', [ $this, 'register_ability_category' ] ); |
| 60 | 47 | add_action( 'wp_abilities_api_init', [ $this, 'register_abilities' ] ); |
| 61 | - add_action( 'init', [ $this, 'register_shared_registry_slugs' ], 5 ); | |
| 62 | - add_action( 'elementor/editor-one/menu/register', [ $this, 'register_editor_one_menu' ], Editor_One_Mcp_Menu::REGISTER_PRIORITY_AFTER_SUBMISSIONS ); | |
| 48 | + add_action( 'mcp_adapter_init', [ $this, 'register_server' ] ); | |
| 63 | 49 | } |
| 64 | 50 | |
| 65 | - public function registry(): Ability_Registry { | |
| 66 | - return $this->registry; | |
| 67 | - } | |
| 68 | - | |
| 69 | 51 | public function register_ability_category() { |
| 70 | 52 | if ( ! function_exists( 'wp_register_ability_category' ) ) { |
| 71 | 53 | return; |
| 72 | 54 | } |
| @@ -84,84 +66,44 @@ | ||
| 84 | 66 | if ( ! function_exists( 'wp_register_ability' ) ) { |
| 85 | 67 | return; |
| 86 | 68 | } |
| 87 | 69 | |
| 88 | - foreach ( $this->registry->all() as $ability ) { | |
| 89 | - $ability->register(); | |
| 90 | - } | |
| 70 | + ( new Abilities\List_Pages_Ability() )->register(); | |
| 71 | + ( new Abilities\Get_Structure_Ability() )->register(); | |
| 72 | + ( new Abilities\Update_Settings_Ability() )->register(); | |
| 73 | + ( new Abilities\Create_Page_Ability() )->register(); | |
| 74 | + ( new Abilities\Get_Globals_Ability() )->register(); | |
| 91 | 75 | } |
| 92 | 76 | |
| 93 | - public function register_shared_registry_slugs(): void { | |
| 94 | - $shared = Shared_Registry::instance(); | |
| 77 | + public function register_server( $adapter ) { | |
| 78 | + if ( ! $adapter instanceof McpAdapter ) { | |
| 79 | + return; | |
| 80 | + } | |
| 95 | 81 | |
| 96 | - $shared->register_tools( $this->collect_server_ids( $this->registry->tools() ) ); | |
| 97 | - $shared->register_resources( $this->collect_server_ids( $this->registry->resources() ) ); | |
| 98 | - } | |
| 99 | - | |
| 100 | - public function register_editor_one_menu( Menu_Data_Provider $menu_data_provider ): void { | |
| 101 | - $menu_data_provider->register_menu( | |
| 102 | - new Editor_One_Mcp_Menu(), | |
| 103 | - [ 'preserve_label_casing' => true ] | |
| 82 | + $result = $adapter->create_server( | |
| 83 | + 'elementor-mcp-server', | |
| 84 | + 'elementor', | |
| 85 | + 'mcp', | |
| 86 | + 'Elementor MCP', | |
| 87 | + 'Read and modify Elementor Editor abilities.', | |
| 88 | + 'v1.0.0', | |
| 89 | + [ \WP\MCP\Transport\HttpTransport::class ], | |
| 90 | + \WP\MCP\Infrastructure\ErrorHandling\ErrorLogMcpErrorHandler::class, | |
| 91 | + \WP\MCP\Infrastructure\Observability\NullMcpObservabilityHandler::class, | |
| 92 | + [ | |
| 93 | + 'elementor/list-pages', | |
| 94 | + 'elementor/get-page-structure', | |
| 95 | + 'elementor/update-page-settings', | |
| 96 | + 'elementor/create-page', | |
| 97 | + 'elementor/get-globals', | |
| 98 | + ], | |
| 99 | + [], | |
| 100 | + [] | |
| 104 | 101 | ); |
| 105 | - } | |
| 106 | 102 | |
| 107 | - public static function build_core_registry(): Ability_Registry { | |
| 108 | - $registry = new Ability_Registry(); | |
| 109 | - | |
| 110 | - foreach ( self::get_core_abilities( $registry ) as $ability ) { | |
| 111 | - $registry->add( $ability ); | |
| 103 | + if ( is_wp_error( $result ) ) { | |
| 104 | + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log | |
| 105 | + error_log( sprintf( '[Elementor MCP] Server registration failed: %s', $result->get_error_message() ) ); | |
| 106 | + return; | |
| 112 | 107 | } |
| 113 | - | |
| 114 | - return $registry; | |
| 115 | - } | |
| 116 | - | |
| 117 | - /** @return Abstract_Ability[] */ | |
| 118 | - private static function get_core_abilities( Ability_Registry $registry ): array { | |
| 119 | - $abilities = [ | |
| 120 | - new Abilities\Get_Structure_Ability(), | |
| 121 | - new Abilities\Update_Settings_Ability(), | |
| 122 | - new Abilities\Create_Page_Ability(), | |
| 123 | - new Abilities\Create_Preview_Link_Ability(), | |
| 124 | - new Abilities\Publish_Document_Ability(), | |
| 125 | - new Abilities\Style_Best_Practices_Ability(), | |
| 126 | - new Abilities\Wordpress_Best_Practices_Ability(), | |
| 127 | - new Abilities\Manage_Variable_Ability(), | |
| 128 | - new Abilities\Manage_Classes_Ability(), | |
| 129 | - new Abilities\Manage_Default_Styles_Ability(), | |
| 130 | - new Abilities\Get_Default_Styles_Ability(), | |
| 131 | - new Abilities\Reorder_Classes_Ability(), | |
| 132 | - new Abilities\Manage_Variable_Guide_Ability(), | |
| 133 | - new Abilities\Get_Widget_Schema_Ability(), | |
| 134 | - new Abilities\List_Widget_Schemas_Ability(), | |
| 135 | - new Abilities\List_Dynamic_Tags_Ability(), | |
| 136 | - new Abilities\Build_Composition_Ability(), | |
| 137 | - new Abilities\Manage_Elements_Ability(), | |
| 138 | - new Abilities\Global_Classes_Resource_Ability(), | |
| 139 | - new Abilities\List_Assets_Ability(), | |
| 140 | - new Abilities\Global_Variables_Resource_Ability(), | |
| 141 | - new Abilities\Interactions_Schema_Resource_Ability(), | |
| 142 | - new Abilities\List_Resources_Ability( $registry ), | |
| 143 | - new Abilities\Read_Resource_Ability( $registry ), | |
| 144 | - new Abilities\List_Components_Ability(), | |
| 145 | - new Abilities\Manage_Component_Ability(), | |
| 146 | - new Abilities\List_Posts_Ability(), | |
| 147 | - ]; | |
| 148 | - | |
| 149 | - return $abilities; | |
| 150 | - } | |
| 151 | - | |
| 152 | - /** | |
| 153 | - * @param Abstract_Ability[] $abilities | |
| 154 | - * @return string[] | |
| 155 | - */ | |
| 156 | - private function collect_server_ids( array $abilities ): array { | |
| 157 | - $ids = []; | |
| 158 | - | |
| 159 | - foreach ( $abilities as $ability ) { | |
| 160 | - if ( $ability->is_exposed_on_server() ) { | |
| 161 | - $ids[] = $ability->get_id(); | |
| 162 | - } | |
| 163 | - } | |
| 164 | - | |
| 165 | - return $ids; | |
| 166 | 108 | } |
| 167 | 109 | } |