| 1 |
<?php |
| 2 |
/** |
| 3 |
* MCP Tools for Code Snippets Operations |
| 4 |
* |
| 5 |
* Provides MCP tools for managing header and footer code snippets |
| 6 |
* (both global and post-level). |
| 7 |
* |
| 8 |
* @package MetaSync |
| 9 |
* @subpackage MCP_Server/Tools |
| 10 |
*/ |
| 11 |
|
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
require_once plugin_dir_path(dirname(__FILE__)) . 'class-mcp-tool-base.php'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Get Header Snippet Tool |
| 20 |
* |
| 21 |
* Gets global header code snippet |
| 22 |
*/ |
| 23 |
class MCP_Tool_Get_Header_Snippet extends MCP_Tool_Base { |
| 24 |
|
| 25 |
public function get_name() { |
| 26 |
return 'wordpress_get_header_snippet'; |
| 27 |
} |
| 28 |
|
| 29 |
public function get_description() { |
| 30 |
return 'Get global header code snippet that appears on all pages'; |
| 31 |
} |
| 32 |
|
| 33 |
public function get_input_schema() { |
| 34 |
return [ |
| 35 |
'type' => 'object', |
| 36 |
'properties' => (object)[], |
| 37 |
]; |
| 38 |
} |
| 39 |
|
| 40 |
public function execute($params) { |
| 41 |
$this->validate_params($params); |
| 42 |
$this->require_capability('manage_options'); |
| 43 |
|
| 44 |
$options = get_option(Metasync::option_name, []); |
| 45 |
$code_snippets = isset($options['codesnippets']) ? $options['codesnippets'] : []; |
| 46 |
$header_snippet = isset($code_snippets['header_snippet']) ? $code_snippets['header_snippet'] : ''; |
| 47 |
|
| 48 |
return $this->success([ |
| 49 |
'header_snippet' => $header_snippet, |
| 50 |
'length' => strlen($header_snippet), |
| 51 |
'enabled' => !empty($header_snippet), |
| 52 |
]); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Update Header Snippet Tool |
| 58 |
* |
| 59 |
* Updates global header code snippet |
| 60 |
*/ |
| 61 |
class MCP_Tool_Update_Header_Snippet extends MCP_Tool_Base { |
| 62 |
|
| 63 |
public function get_name() { |
| 64 |
return 'wordpress_update_header_snippet'; |
| 65 |
} |
| 66 |
|
| 67 |
public function get_description() { |
| 68 |
return 'Update global header code snippet (HTML/JS/CSS) that appears on all pages'; |
| 69 |
} |
| 70 |
|
| 71 |
public function get_input_schema() { |
| 72 |
return [ |
| 73 |
'type' => 'object', |
| 74 |
'properties' => [ |
| 75 |
'header_snippet' => [ |
| 76 |
'type' => 'string', |
| 77 |
'description' => 'Header code snippet (HTML/JS/CSS)', |
| 78 |
], |
| 79 |
], |
| 80 |
'required' => ['header_snippet'], |
| 81 |
]; |
| 82 |
} |
| 83 |
|
| 84 |
public function execute($params) { |
| 85 |
$this->validate_params($params); |
| 86 |
$this->require_capability('manage_options'); |
| 87 |
|
| 88 |
$header_snippet = wp_unslash($params['header_snippet']); |
| 89 |
|
| 90 |
// Get current options |
| 91 |
$options = get_option(Metasync::option_name, []); |
| 92 |
|
| 93 |
// Initialize codesnippets section if not exists |
| 94 |
if (!isset($options['codesnippets'])) { |
| 95 |
$options['codesnippets'] = []; |
| 96 |
} |
| 97 |
|
| 98 |
// Update header snippet |
| 99 |
$options['codesnippets']['header_snippet'] = $header_snippet; |
| 100 |
|
| 101 |
// Save options |
| 102 |
update_option(Metasync::option_name, $options); |
| 103 |
|
| 104 |
return $this->success([ |
| 105 |
'header_snippet' => $header_snippet, |
| 106 |
'length' => strlen($header_snippet), |
| 107 |
'message' => 'Header snippet updated successfully', |
| 108 |
]); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Get Footer Snippet Tool |
| 114 |
* |
| 115 |
* Gets global footer code snippet |
| 116 |
*/ |
| 117 |
class MCP_Tool_Get_Footer_Snippet extends MCP_Tool_Base { |
| 118 |
|
| 119 |
public function get_name() { |
| 120 |
return 'wordpress_get_footer_snippet'; |
| 121 |
} |
| 122 |
|
| 123 |
public function get_description() { |
| 124 |
return 'Get global footer code snippet that appears on all pages'; |
| 125 |
} |
| 126 |
|
| 127 |
public function get_input_schema() { |
| 128 |
return [ |
| 129 |
'type' => 'object', |
| 130 |
'properties' => (object)[], |
| 131 |
]; |
| 132 |
} |
| 133 |
|
| 134 |
public function execute($params) { |
| 135 |
$this->validate_params($params); |
| 136 |
$this->require_capability('manage_options'); |
| 137 |
|
| 138 |
$options = get_option(Metasync::option_name, []); |
| 139 |
$code_snippets = isset($options['codesnippets']) ? $options['codesnippets'] : []; |
| 140 |
$footer_snippet = isset($code_snippets['footer_snippet']) ? $code_snippets['footer_snippet'] : ''; |
| 141 |
|
| 142 |
return $this->success([ |
| 143 |
'footer_snippet' => $footer_snippet, |
| 144 |
'length' => strlen($footer_snippet), |
| 145 |
'enabled' => !empty($footer_snippet), |
| 146 |
]); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Update Footer Snippet Tool |
| 152 |
* |
| 153 |
* Updates global footer code snippet |
| 154 |
*/ |
| 155 |
class MCP_Tool_Update_Footer_Snippet extends MCP_Tool_Base { |
| 156 |
|
| 157 |
public function get_name() { |
| 158 |
return 'wordpress_update_footer_snippet'; |
| 159 |
} |
| 160 |
|
| 161 |
public function get_description() { |
| 162 |
return 'Update global footer code snippet (HTML/JS/CSS) that appears on all pages'; |
| 163 |
} |
| 164 |
|
| 165 |
public function get_input_schema() { |
| 166 |
return [ |
| 167 |
'type' => 'object', |
| 168 |
'properties' => [ |
| 169 |
'footer_snippet' => [ |
| 170 |
'type' => 'string', |
| 171 |
'description' => 'Footer code snippet (HTML/JS/CSS)', |
| 172 |
], |
| 173 |
], |
| 174 |
'required' => ['footer_snippet'], |
| 175 |
]; |
| 176 |
} |
| 177 |
|
| 178 |
public function execute($params) { |
| 179 |
$this->validate_params($params); |
| 180 |
$this->require_capability('manage_options'); |
| 181 |
|
| 182 |
$footer_snippet = wp_unslash($params['footer_snippet']); |
| 183 |
|
| 184 |
// Get current options |
| 185 |
$options = get_option(Metasync::option_name, []); |
| 186 |
|
| 187 |
// Initialize codesnippets section if not exists |
| 188 |
if (!isset($options['codesnippets'])) { |
| 189 |
$options['codesnippets'] = []; |
| 190 |
} |
| 191 |
|
| 192 |
// Update footer snippet |
| 193 |
$options['codesnippets']['footer_snippet'] = $footer_snippet; |
| 194 |
|
| 195 |
// Save options |
| 196 |
update_option(Metasync::option_name, $options); |
| 197 |
|
| 198 |
return $this->success([ |
| 199 |
'footer_snippet' => $footer_snippet, |
| 200 |
'length' => strlen($footer_snippet), |
| 201 |
'message' => 'Footer snippet updated successfully', |
| 202 |
]); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Get Post Snippets Tool |
| 208 |
* |
| 209 |
* Gets post-level code snippets |
| 210 |
*/ |
| 211 |
class MCP_Tool_Get_Post_Snippets extends MCP_Tool_Base { |
| 212 |
|
| 213 |
public function get_name() { |
| 214 |
return 'wordpress_get_post_snippets'; |
| 215 |
} |
| 216 |
|
| 217 |
public function get_description() { |
| 218 |
return 'Get post-level code snippets (header and footer) for a specific post'; |
| 219 |
} |
| 220 |
|
| 221 |
public function get_input_schema() { |
| 222 |
return [ |
| 223 |
'type' => 'object', |
| 224 |
'properties' => [ |
| 225 |
'post_id' => [ |
| 226 |
'type' => 'integer', |
| 227 |
'description' => 'Post ID', |
| 228 |
], |
| 229 |
], |
| 230 |
'required' => ['post_id'], |
| 231 |
]; |
| 232 |
} |
| 233 |
|
| 234 |
public function execute($params) { |
| 235 |
$this->validate_params($params); |
| 236 |
$this->require_capability('edit_posts'); |
| 237 |
|
| 238 |
$post_id = intval($params['post_id']); |
| 239 |
|
| 240 |
// Validate post exists |
| 241 |
$post = $this->verify_post_exists($post_id); |
| 242 |
|
| 243 |
// Get all post-level snippets |
| 244 |
$custom_post_header = get_post_meta($post_id, 'custom_post_header', true); |
| 245 |
$custom_post_footer = get_post_meta($post_id, 'custom_post_footer', true); |
| 246 |
$searchatlas_embed_top = get_post_meta($post_id, 'searchatlas_embed_top', true); |
| 247 |
$searchatlas_embed_bottom = get_post_meta($post_id, 'searchatlas_embed_bottom', true); |
| 248 |
|
| 249 |
return $this->success([ |
| 250 |
'post_id' => $post_id, |
| 251 |
'post_title' => $post->post_title, |
| 252 |
'snippets' => [ |
| 253 |
'custom_post_header' => $custom_post_header ?: '', |
| 254 |
'custom_post_footer' => $custom_post_footer ?: '', |
| 255 |
'searchatlas_embed_top' => $searchatlas_embed_top ?: '', |
| 256 |
'searchatlas_embed_bottom' => $searchatlas_embed_bottom ?: '', |
| 257 |
], |
| 258 |
'has_header_snippets' => !empty($custom_post_header) || !empty($searchatlas_embed_top), |
| 259 |
'has_footer_snippets' => !empty($custom_post_footer) || !empty($searchatlas_embed_bottom), |
| 260 |
]); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Update Post Snippets Tool |
| 266 |
* |
| 267 |
* Updates post-level code snippets |
| 268 |
*/ |
| 269 |
class MCP_Tool_Update_Post_Snippets extends MCP_Tool_Base { |
| 270 |
|
| 271 |
public function get_name() { |
| 272 |
return 'wordpress_update_post_snippets'; |
| 273 |
} |
| 274 |
|
| 275 |
public function get_description() { |
| 276 |
return 'Update post-level code snippets (header and footer) for a specific post'; |
| 277 |
} |
| 278 |
|
| 279 |
public function get_input_schema() { |
| 280 |
return [ |
| 281 |
'type' => 'object', |
| 282 |
'properties' => [ |
| 283 |
'post_id' => [ |
| 284 |
'type' => 'integer', |
| 285 |
'description' => 'Post ID', |
| 286 |
], |
| 287 |
'custom_post_header' => [ |
| 288 |
'type' => 'string', |
| 289 |
'description' => 'Custom header snippet for this post (optional)', |
| 290 |
], |
| 291 |
'custom_post_footer' => [ |
| 292 |
'type' => 'string', |
| 293 |
'description' => 'Custom footer snippet for this post (optional)', |
| 294 |
], |
| 295 |
'searchatlas_embed_top' => [ |
| 296 |
'type' => 'string', |
| 297 |
'description' => 'Plugin embed top snippet (optional)', |
| 298 |
], |
| 299 |
'searchatlas_embed_bottom' => [ |
| 300 |
'type' => 'string', |
| 301 |
'description' => 'Plugin embed bottom snippet (optional)', |
| 302 |
], |
| 303 |
], |
| 304 |
'required' => ['post_id'], |
| 305 |
]; |
| 306 |
} |
| 307 |
|
| 308 |
public function execute($params) { |
| 309 |
$this->validate_params($params); |
| 310 |
$this->require_plugin_access(); |
| 311 |
|
| 312 |
$post_id = intval($params['post_id']); |
| 313 |
|
| 314 |
// Validate post exists and user can edit it |
| 315 |
$post = $this->verify_post_exists($post_id); |
| 316 |
|
| 317 |
$this->check_post_permission($post_id); |
| 318 |
|
| 319 |
// Update snippets if provided |
| 320 |
if (isset($params['custom_post_header'])) { |
| 321 |
update_post_meta($post_id, 'custom_post_header', wp_unslash($params['custom_post_header'])); |
| 322 |
} |
| 323 |
|
| 324 |
if (isset($params['custom_post_footer'])) { |
| 325 |
update_post_meta($post_id, 'custom_post_footer', wp_unslash($params['custom_post_footer'])); |
| 326 |
} |
| 327 |
|
| 328 |
if (isset($params['searchatlas_embed_top'])) { |
| 329 |
update_post_meta($post_id, 'searchatlas_embed_top', wp_unslash($params['searchatlas_embed_top'])); |
| 330 |
} |
| 331 |
|
| 332 |
if (isset($params['searchatlas_embed_bottom'])) { |
| 333 |
update_post_meta($post_id, 'searchatlas_embed_bottom', wp_unslash($params['searchatlas_embed_bottom'])); |
| 334 |
} |
| 335 |
|
| 336 |
// Get updated snippets |
| 337 |
$custom_post_header = get_post_meta($post_id, 'custom_post_header', true); |
| 338 |
$custom_post_footer = get_post_meta($post_id, 'custom_post_footer', true); |
| 339 |
$searchatlas_embed_top = get_post_meta($post_id, 'searchatlas_embed_top', true); |
| 340 |
$searchatlas_embed_bottom = get_post_meta($post_id, 'searchatlas_embed_bottom', true); |
| 341 |
|
| 342 |
return $this->success([ |
| 343 |
'post_id' => $post_id, |
| 344 |
'post_title' => $post->post_title, |
| 345 |
'snippets' => [ |
| 346 |
'custom_post_header' => $custom_post_header ?: '', |
| 347 |
'custom_post_footer' => $custom_post_footer ?: '', |
| 348 |
'searchatlas_embed_top' => $searchatlas_embed_top ?: '', |
| 349 |
'searchatlas_embed_bottom' => $searchatlas_embed_bottom ?: '', |
| 350 |
], |
| 351 |
'message' => 'Post snippets updated successfully', |
| 352 |
]); |
| 353 |
} |
| 354 |
} |
| 355 |
|