| @@ -2,10 +2,16 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Elementor\Modules\Mcp; |
| 4 | 4 | |
| 5 | 5 | use Elementor\Core\Base\Module as BaseModule; |
| 6 | -use Elementor\Plugin; | |
| 7 | -use Elementor\Core\Experiments\Manager as ExperimentsManager; | |
| 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; | |
| 8 | 14 | use WP\MCP\Core\McpAdapter; |
| 9 | 15 | |
| 10 | 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | 17 | exit; |
| @@ -11,44 +17,56 @@ | ||
| 11 | 17 | exit; |
| 12 | 18 | } |
| 13 | 19 | |
| 14 | 20 | class Module extends BaseModule { |
| 15 | - const EXPERIMENT_NAME = 'e_wp_abilities_api'; | |
| 16 | 21 | |
| 22 | + const ANALYTICS_REGISTRAR_HANDLE = 'elementor-mcp-analytics-registrar'; | |
| 23 | + | |
| 24 | + private Ability_Registry $registry; | |
| 25 | + | |
| 17 | 26 | public function get_name() { |
| 18 | 27 | return 'mcp'; |
| 19 | 28 | } |
| 20 | 29 | |
| 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 | + | |
| 21 | 40 | public static function is_active() { |
| 22 | 41 | return class_exists( McpAdapter::class ) && |
| 23 | 42 | function_exists( 'wp_register_ability' ) && |
| 24 | - Plugin::instance()->experiments->is_feature_active( self::EXPERIMENT_NAME ); | |
| 43 | + class_exists( Shared_Registry::class ); | |
| 25 | 44 | } |
| 26 | 45 | |
| 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 | - | |
| 37 | 46 | public function __construct() { |
| 38 | 47 | parent::__construct(); |
| 39 | 48 | |
| 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 | + | |
| 40 | 55 | if ( ! $this->is_active() ) { |
| 41 | 56 | return; |
| 42 | 57 | } |
| 43 | 58 | |
| 44 | - McpAdapter::instance(); | |
| 45 | - | |
| 46 | 59 | add_action( 'wp_abilities_api_categories_init', [ $this, 'register_ability_category' ] ); |
| 47 | 60 | add_action( 'wp_abilities_api_init', [ $this, 'register_abilities' ] ); |
| 48 | - add_action( 'mcp_adapter_init', [ $this, 'register_server' ] ); | |
| 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 ); | |
| 49 | 63 | } |
| 50 | 64 | |
| 65 | + public function registry(): Ability_Registry { | |
| 66 | + return $this->registry; | |
| 67 | + } | |
| 68 | + | |
| 51 | 69 | public function register_ability_category() { |
| 52 | 70 | if ( ! function_exists( 'wp_register_ability_category' ) ) { |
| 53 | 71 | return; |
| 54 | 72 | } |
| @@ -66,44 +84,84 @@ | ||
| 66 | 84 | if ( ! function_exists( 'wp_register_ability' ) ) { |
| 67 | 85 | return; |
| 68 | 86 | } |
| 69 | 87 | |
| 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(); | |
| 88 | + foreach ( $this->registry->all() as $ability ) { | |
| 89 | + $ability->register(); | |
| 90 | + } | |
| 75 | 91 | } |
| 76 | 92 | |
| 77 | - public function register_server( $adapter ) { | |
| 78 | - if ( ! $adapter instanceof McpAdapter ) { | |
| 79 | - return; | |
| 93 | + public function register_shared_registry_slugs(): void { | |
| 94 | + $shared = Shared_Registry::instance(); | |
| 95 | + | |
| 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 ] | |
| 104 | + ); | |
| 105 | + } | |
| 106 | + | |
| 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 ); | |
| 80 | 112 | } |
| 81 | 113 | |
| 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 | - [] | |
| 101 | - ); | |
| 114 | + return $registry; | |
| 115 | + } | |
| 102 | 116 | |
| 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; | |
| 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 | + } | |
| 107 | 163 | } |
| 164 | + | |
| 165 | + return $ids; | |
| 108 | 166 | } |
| 109 | 167 | } |