Repository navigation
Expand file tree
/
Copy pathwpbakery-uncode-mcp.php
More file actions
278 lines (244 loc) · 8.12 KB
/
Copy pathwpbakery-uncode-mcp.php
File metadata and controls
278 lines (244 loc) · 8.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
/**
* Plugin Name: MCP Tools for WPBakery + Uncode
* Description: Extends the WordPress MCP Adapter to expose WPBakery and Uncode editing actions as MCP tools for AI agents.
* Version: 0.1.0
* Requires at least: 6.9
* Tested up to: 6.9
* Requires PHP: 8.0
* Author: Local
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wpbakery-uncode-mcp
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
define( 'WPBAKERY_UNCODE_MCP_VERSION', '0.1.0' );
define( 'WPBAKERY_UNCODE_MCP_DIR', plugin_dir_path( __FILE__ ) );
define( 'WPBAKERY_UNCODE_MCP_URL', plugin_dir_url( __FILE__ ) );
define( 'WPBAKERY_UNCODE_MCP_BASENAME', plugin_basename( __FILE__ ) );
define( 'WPBAKERY_UNCODE_MCP_DISABLED_TOOLS_OPTION', 'wpbakery_uncode_mcp_disabled_tools' );
/**
* Recursively sanitize JSON schemas for MCP client compatibility.
*
* @param array $schema Schema array.
* @return array
*/
function wpbakery_uncode_mcp_sanitize_schema( array $schema ): array {
if ( isset( $schema['enum'] ) && is_array( $schema['enum'] ) ) {
$schema['enum'] = array_values(
array_filter(
$schema['enum'],
static function ( $value ) {
return '' !== $value;
}
)
);
if ( empty( $schema['enum'] ) ) {
unset( $schema['enum'] );
}
}
if ( isset( $schema['properties'] ) && is_array( $schema['properties'] ) ) {
if ( empty( $schema['properties'] ) ) {
$schema['properties'] = new \stdClass();
} else {
foreach ( $schema['properties'] as $key => $prop ) {
if ( is_array( $prop ) ) {
$schema['properties'][ $key ] = wpbakery_uncode_mcp_sanitize_schema( $prop );
}
}
}
}
if ( isset( $schema['items'] ) && is_array( $schema['items'] ) ) {
$schema['items'] = wpbakery_uncode_mcp_sanitize_schema( $schema['items'] );
}
foreach ( array( 'allOf', 'oneOf', 'anyOf' ) as $keyword ) {
if ( isset( $schema[ $keyword ] ) && is_array( $schema[ $keyword ] ) ) {
foreach ( $schema[ $keyword ] as $index => $sub ) {
if ( is_array( $sub ) ) {
$schema[ $keyword ][ $index ] = wpbakery_uncode_mcp_sanitize_schema( $sub );
}
}
}
}
return $schema;
}
/**
* Wrapper around wp_register_ability that sanitizes schemas first.
*
* @param string $name Ability name.
* @param array $args Ability args.
* @return mixed
*/
function wpbakery_uncode_mcp_register_ability( string $name, array $args ) {
if ( ! wpbakery_uncode_mcp_is_tool_enabled( $name ) ) {
return true;
}
if ( isset( $args['input_schema'] ) && is_array( $args['input_schema'] ) ) {
$args['input_schema'] = wpbakery_uncode_mcp_sanitize_schema( $args['input_schema'] );
}
if ( isset( $args['output_schema'] ) && is_array( $args['output_schema'] ) ) {
$args['output_schema'] = wpbakery_uncode_mcp_sanitize_schema( $args['output_schema'] );
}
return wp_register_ability( $name, $args );
}
/**
* Whether an MCP tool is enabled via admin settings.
*
* @param string $ability_name Ability slug.
* @return bool
*/
function wpbakery_uncode_mcp_is_tool_enabled( string $ability_name ): bool {
$disabled = get_option( WPBAKERY_UNCODE_MCP_DISABLED_TOOLS_OPTION, array() );
if ( ! is_array( $disabled ) ) {
$disabled = array();
}
return ! in_array( $ability_name, $disabled, true );
}
/**
* Check plugin dependencies.
*
* @return bool
*/
function wpbakery_uncode_mcp_check_dependencies(): bool {
$missing = array();
if ( ! class_exists( '\\WP\\MCP\\Core\\McpAdapter' ) ) {
$missing[] = 'WordPress MCP Adapter';
}
if ( ! function_exists( 'wp_register_ability' ) ) {
$missing[] = 'WordPress Abilities API (WordPress 6.9+)';
}
if ( ! defined( 'WPB_VC_VERSION' ) ) {
$missing[] = 'WPBakery Page Builder';
}
if ( ! empty( $missing ) ) {
add_action(
'admin_notices',
static function () use ( $missing ) {
printf(
'<div class="notice notice-error"><p>%s</p></div>',
esc_html(
sprintf(
/* translators: %s: dependency list */
__( 'MCP Tools for WPBakery + Uncode requires: %s', 'wpbakery-uncode-mcp' ),
implode( ', ', $missing )
)
)
);
}
);
return false;
}
if ( ! wpbakery_uncode_mcp_is_uncode_active() ) {
add_action(
'admin_notices',
static function () {
printf(
'<div class="notice notice-warning"><p>%s</p></div>',
esc_html__( 'Uncode theme was not detected. WPBakery tools still work, but Uncode-specific meta tools may be limited.', 'wpbakery-uncode-mcp' )
);
}
);
}
return true;
}
/**
* Detect whether the current active theme is Uncode.
*
* @return bool
*/
function wpbakery_uncode_mcp_is_uncode_active(): bool {
$theme = wp_get_theme();
if ( ! $theme ) {
return false;
}
$name = strtolower( (string) $theme->get( 'Name' ) );
$template = strtolower( (string) $theme->get_template() );
$stylesheet = strtolower( (string) $theme->get_stylesheet() );
$parent_name = strtolower( (string) $theme->parent()->get( 'Name' ) );
$targets = array( $name, $template, $stylesheet, $parent_name );
foreach ( $targets as $candidate ) {
if ( false !== strpos( $candidate, 'uncode' ) ) {
return true;
}
}
return false;
}
/**
* Bootstrap plugin.
*/
function wpbakery_uncode_mcp_init(): void {
if ( ! wpbakery_uncode_mcp_check_dependencies() ) {
return;
}
require_once WPBAKERY_UNCODE_MCP_DIR . 'includes/class-data.php';
require_once WPBAKERY_UNCODE_MCP_DIR . 'includes/class-shortcode-service.php';
require_once WPBAKERY_UNCODE_MCP_DIR . 'includes/abilities/class-page-abilities.php';
require_once WPBAKERY_UNCODE_MCP_DIR . 'includes/abilities/class-shortcode-abilities.php';
require_once WPBAKERY_UNCODE_MCP_DIR . 'includes/abilities/class-wpbakery-api-abilities.php';
require_once WPBAKERY_UNCODE_MCP_DIR . 'includes/abilities/class-uncode-meta-abilities.php';
require_once WPBAKERY_UNCODE_MCP_DIR . 'includes/abilities/class-theme-options-abilities.php';
require_once WPBAKERY_UNCODE_MCP_DIR . 'includes/abilities/class-composer-abilities.php';
require_once WPBAKERY_UNCODE_MCP_DIR . 'includes/abilities/class-ability-registrar.php';
require_once WPBAKERY_UNCODE_MCP_DIR . 'includes/class-plugin.php';
if ( is_admin() ) {
require_once WPBAKERY_UNCODE_MCP_DIR . 'includes/admin/class-admin.php';
}
WPBakery_Uncode_MCP_Plugin::instance();
if ( is_admin() && class_exists( 'WPBakery_Uncode_MCP_Admin' ) ) {
$GLOBALS['wpbakery_uncode_mcp_admin'] = new WPBakery_Uncode_MCP_Admin();
$GLOBALS['wpbakery_uncode_mcp_admin']->init();
}
}
add_action( 'plugins_loaded', 'wpbakery_uncode_mcp_init', 20 );
/**
* Apply persisted WPBakery element visibility.
*
* @return void
*/
function wpbakery_uncode_mcp_apply_element_visibility(): void {
if ( ! function_exists( 'vc_remove_element' ) ) {
return;
}
$disabled = get_option( 'wpbakery_uncode_mcp_disabled_elements', array() );
if ( ! is_array( $disabled ) ) {
return;
}
foreach ( $disabled as $tag ) {
$clean_tag = sanitize_key( (string) $tag );
if ( '' !== $clean_tag ) {
vc_remove_element( $clean_tag );
}
}
}
add_action( 'vc_after_init', 'wpbakery_uncode_mcp_apply_element_visibility', 30 );
/**
* Optional shortcode output debug wrapper for troubleshooting.
*
* @param string $output Shortcode output.
* @param object $shortcode Shortcode object.
* @param array $atts Shortcode atts.
* @return string
*/
function wpbakery_uncode_mcp_debug_shortcode_output( $output, $shortcode, $atts ) {
$config = get_option( 'wpbakery_uncode_mcp_output_debug', array() );
if ( ! is_array( $config ) || empty( $config['enabled'] ) ) {
return $output;
}
$tag = '';
if ( is_object( $shortcode ) && isset( $shortcode->shortcode ) ) {
$tag = sanitize_key( (string) $shortcode->shortcode );
}
$allow_tags = is_array( $config['tags'] ?? null ) ? $config['tags'] : array();
if ( ! empty( $allow_tags ) && ! in_array( $tag, $allow_tags, true ) ) {
return $output;
}
$meta = array(
'tag' => $tag,
'atts' => is_array( $atts ) ? array_keys( $atts ) : array(),
'output_len' => strlen( (string) $output ),
);
return "<!-- mcp-wpbakery-debug " . esc_html( wp_json_encode( $meta ) ) . " -->\n" . $output;
}
add_filter( 'vc_shortcode_output', 'wpbakery_uncode_mcp_debug_shortcode_output', 99, 3 );