| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Mcp; |
| 4 |
|
| 5 |
use Elementor\Core\Base\Module as BaseModule; |
| 6 |
use Elementor\Plugin; |
| 7 |
use Elementor\Core\Experiments\Manager as ExperimentsManager; |
| 8 |
use WP\MCP\Core\McpAdapter; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
class Module extends BaseModule { |
| 15 |
const EXPERIMENT_NAME = 'e_wp_abilities_api'; |
| 16 |
|
| 17 |
public function get_name() { |
| 18 |
return 'mcp'; |
| 19 |
} |
| 20 |
|
| 21 |
public static function is_active() { |
| 22 |
return class_exists( McpAdapter::class ) && |
| 23 |
function_exists( 'wp_register_ability' ) && |
| 24 |
Plugin::instance()->experiments->is_feature_active( self::EXPERIMENT_NAME ); |
| 25 |
} |
| 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 |
|
| 37 |
public function __construct() { |
| 38 |
parent::__construct(); |
| 39 |
|
| 40 |
if ( ! $this->is_active() ) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
McpAdapter::instance(); |
| 45 |
|
| 46 |
add_action( 'wp_abilities_api_categories_init', [ $this, 'register_ability_category' ] ); |
| 47 |
add_action( 'wp_abilities_api_init', [ $this, 'register_abilities' ] ); |
| 48 |
add_action( 'mcp_adapter_init', [ $this, 'register_server' ] ); |
| 49 |
} |
| 50 |
|
| 51 |
public function register_ability_category() { |
| 52 |
if ( ! function_exists( 'wp_register_ability_category' ) ) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
wp_register_ability_category( |
| 57 |
'elementor', |
| 58 |
[ |
| 59 |
'label' => __( 'Elementor', 'elementor' ), |
| 60 |
'description' => __( 'Elementor page builder data, global classes, and variables.', 'elementor' ), |
| 61 |
] |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
public function register_abilities() { |
| 66 |
if ( ! function_exists( 'wp_register_ability' ) ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 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(); |
| 75 |
} |
| 76 |
|
| 77 |
public function register_server( $adapter ) { |
| 78 |
if ( ! $adapter instanceof McpAdapter ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 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 |
); |
| 102 |
|
| 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; |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|