| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Mcp; |
| 4 |
|
| 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; |
| 14 |
use WP\MCP\Core\McpAdapter; |
| 15 |
|
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
|
| 20 |
class Module extends BaseModule { |
| 21 |
|
| 22 |
const ANALYTICS_REGISTRAR_HANDLE = 'elementor-mcp-analytics-registrar'; |
| 23 |
|
| 24 |
private Ability_Registry $registry; |
| 25 |
|
| 26 |
public function get_name() { |
| 27 |
return 'mcp'; |
| 28 |
} |
| 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 |
|
| 40 |
public static function is_active() { |
| 41 |
return class_exists( McpAdapter::class ) && |
| 42 |
function_exists( 'wp_register_ability' ) && |
| 43 |
class_exists( Shared_Registry::class ); |
| 44 |
} |
| 45 |
|
| 46 |
public function __construct() { |
| 47 |
parent::__construct(); |
| 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 |
|
| 55 |
if ( ! $this->is_active() ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
add_action( 'wp_abilities_api_categories_init', [ $this, 'register_ability_category' ] ); |
| 60 |
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 ); |
| 63 |
} |
| 64 |
|
| 65 |
public function registry(): Ability_Registry { |
| 66 |
return $this->registry; |
| 67 |
} |
| 68 |
|
| 69 |
public function register_ability_category() { |
| 70 |
if ( ! function_exists( 'wp_register_ability_category' ) ) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
wp_register_ability_category( |
| 75 |
'elementor', |
| 76 |
[ |
| 77 |
'label' => __( 'Elementor', 'elementor' ), |
| 78 |
'description' => __( 'Elementor page builder data, global classes, and variables.', 'elementor' ), |
| 79 |
] |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
public function register_abilities() { |
| 84 |
if ( ! function_exists( 'wp_register_ability' ) ) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
foreach ( $this->registry->all() as $ability ) { |
| 89 |
$ability->register(); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 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 ); |
| 112 |
} |
| 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 |
} |
| 167 |
} |
| 168 |
|