PluginProbe ʕ •ᴥ•ʔ
ElementsKit Elementor Addons – Advanced Widgets & Templates Addons for Elementor / 4.0.1
ElementsKit Elementor Addons – Advanced Widgets & Templates Addons for Elementor v4.0.1
4.0.2 4.0.1 4.0.0 3.10.02 3.10.01 3.9.10 3.9.9 3.9.8 3.9.7 3.9.5 3.9.6 3.9.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.3.0 2.3.1 2.3.1.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.4.0 2.5.0 2.5.1 2.5.10 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.8.5 2.8.6 2.8.7 2.8.8 2.9.0 2.9.1 2.9.2 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.4.7 3.4.8 3.4.9 3.5.0 3.5.1 3.5.2 3.5.3 3.5.4 3.5.5 3.5.6 3.6.0 3.6.1 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.7.5 3.7.6 3.7.7 3.7.8 3.7.9 3.8.0 3.8.1 3.8.2 3.9.0 3.9.1 3.9.2 trunk 1.2.6 1.2.7 1.2.9 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.10 1.5.11 1.5.12 1.5.2 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.0.9.1 2.0.9.2 2.0.9.3
elementskit-lite / mcp / rest-proxy.php
elementskit-lite / mcp Last commit date
assets 3 weeks ago abilities.php 3 weeks ago manager.php 3 weeks ago rest-proxy.php 3 weeks ago server.php 3 weeks ago service.php 3 weeks ago
rest-proxy.php
147 lines
1 <?php
2 namespace ElementsKit_Lite\Mcp;
3
4 defined( 'ABSPATH' ) || exit;
5
6 /**
7 * REST proxy for the Angie editor tools.
8 *
9 * Exposes a single endpoint — POST /wp-json/elementskit/v1/mcp-proxy — that the
10 * editor-side Angie tools call. It validates the requested tool and input,
11 * enforces permissions and delegates to the shared {@see Service}. No insertion
12 * or template logic lives here; this is purely a dispatcher.
13 *
14 * Request body:
15 * { "tool": "insert-widget", "input": { ... } }
16 *
17 * @since 3.10.x
18 */
19 class Rest_Proxy {
20
21 const NAMESPACE = 'elementskit/v1';
22 const ROUTE = '/mcp-proxy';
23
24 /**
25 * Map of proxy tool names to the Service method that handles them, together
26 * with whether the tool mutates a document (which raises the capability bar).
27 *
28 * @return array<string, array{method:string, mutating:bool}>
29 */
30 private function tool_map() {
31 return [
32 'list-widgets' => [ 'method' => 'list_widgets', 'mutating' => false ],
33 'insert-widget' => [ 'method' => 'insert_widget', 'mutating' => true ],
34 'list-templates' => [ 'method' => 'list_templates', 'mutating' => false ],
35 'insert-template' => [ 'method' => 'insert_template', 'mutating' => true ],
36 'create-page' => [ 'method' => 'create_page', 'mutating' => true ],
37 'create-template' => [ 'method' => 'create_template', 'mutating' => true ],
38 'create-header-footer' => [ 'method' => 'create_header_footer', 'mutating' => true ],
39 'list-custom-widgets' => [ 'method' => 'list_custom_widgets', 'mutating' => false ],
40 'create-custom-widget' => [ 'method' => 'create_custom_widget', 'mutating' => true ],
41 'list-mega-menus' => [ 'method' => 'list_mega_menus', 'mutating' => false ],
42 'enable-mega-menu' => [ 'method' => 'enable_mega_menu', 'mutating' => true ],
43 'update-mega-menu-item' => [ 'method' => 'update_mega_menu_item', 'mutating' => true ],
44 'create-mega-menu' => [ 'method' => 'create_mega_menu', 'mutating' => true ],
45 'list-library-templates' => [ 'method' => 'list_library_templates', 'mutating' => false ],
46 'import-library-template' => [ 'method' => 'import_library_template', 'mutating' => true ],
47 ];
48 }
49
50 public function hooks() {
51 add_action( 'rest_api_init', [ $this, 'register_routes' ] );
52 }
53
54 public function register_routes() {
55 register_rest_route(
56 self::NAMESPACE,
57 self::ROUTE,
58 [
59 'methods' => \WP_REST_Server::CREATABLE,
60 'callback' => [ $this, 'handle' ],
61 'permission_callback' => [ $this, 'permission' ],
62 'args' => [
63 'tool' => [
64 'type' => 'string',
65 'required' => true,
66 ],
67 'input' => [
68 'type' => 'object',
69 'required' => false,
70 'default' => [],
71 ],
72 ],
73 ]
74 );
75 }
76
77 /**
78 * Base gate: the user must at least be able to edit posts. Per-document
79 * capability checks happen inside the Service for mutating tools.
80 *
81 * @return bool
82 */
83 public function permission() {
84 return current_user_can( 'edit_posts' );
85 }
86
87 /**
88 * @param \WP_REST_Request $request
89 * @return \WP_REST_Response
90 */
91 public function handle( \WP_REST_Request $request ) {
92 $tool = (string) $request->get_param( 'tool' );
93 $input = $request->get_param( 'input' );
94 $input = is_array( $input ) ? $input : [];
95
96 $map = $this->tool_map();
97
98 if ( ! isset( $map[ $tool ] ) ) {
99 return $this->error_response(
100 'ekit_mcp_unknown_tool',
101 /* translators: %s: tool name */
102 sprintf( __( 'Unknown proxy tool: %s', 'elementskit-lite' ), $tool ),
103 404
104 );
105 }
106
107 $method = $map[ $tool ]['method'];
108 $result = ( new Service() )->$method( $input );
109
110 if ( is_wp_error( $result ) ) {
111 return $this->error_from_wp_error( $result );
112 }
113
114 return new \WP_REST_Response( $result, 200 );
115 }
116
117 /**
118 * @param \WP_Error $error
119 * @return \WP_REST_Response
120 */
121 private function error_from_wp_error( \WP_Error $error ) {
122 $data = $error->get_error_data();
123 $status = is_array( $data ) && isset( $data['status'] ) ? (int) $data['status'] : 400;
124
125 return $this->error_response( $error->get_error_code(), $error->get_error_message(), $status );
126 }
127
128 /**
129 * @param string $code
130 * @param string $message
131 * @param int $status
132 * @return \WP_REST_Response
133 */
134 private function error_response( $code, $message, $status ) {
135 return new \WP_REST_Response(
136 [
137 'success' => false,
138 'error' => [
139 'code' => $code,
140 'message' => $message,
141 ],
142 ],
143 $status
144 );
145 }
146 }
147