| 1 |
<?php |
| 2 |
/** |
| 3 |
* MCP Tools for Google Instant Index Operations |
| 4 |
* |
| 5 |
* Provides MCP tools for managing Google Instant Indexing API integration. |
| 6 |
* Uses the native PHP implementation in google-index/class-google-index-direct.php |
| 7 |
* via the google_index_direct() singleton. |
| 8 |
* |
| 9 |
* @package MetaSync |
| 10 |
* @subpackage MCP_Server/Tools |
| 11 |
*/ |
| 12 |
|
| 13 |
if (!defined('ABSPATH')) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
require_once plugin_dir_path(dirname(__FILE__)) . 'class-mcp-tool-base.php'; |
| 18 |
|
| 19 |
/** |
| 20 |
* Instant Index Update Tool |
| 21 |
* |
| 22 |
* Submit URL to Google for indexing |
| 23 |
*/ |
| 24 |
class MCP_Tool_Instant_Index_Update extends MCP_Tool_Base { |
| 25 |
|
| 26 |
public function get_name() { |
| 27 |
return 'wordpress_instant_index_update'; |
| 28 |
} |
| 29 |
|
| 30 |
public function get_description() { |
| 31 |
return 'Submit a URL to Google Instant Index API for indexing (URL_UPDATED)'; |
| 32 |
} |
| 33 |
|
| 34 |
public function get_input_schema() { |
| 35 |
return [ |
| 36 |
'type' => 'object', |
| 37 |
'properties' => [ |
| 38 |
'url' => [ |
| 39 |
'type' => 'string', |
| 40 |
'description' => 'The URL to submit for indexing', |
| 41 |
], |
| 42 |
], |
| 43 |
'required' => ['url'], |
| 44 |
]; |
| 45 |
} |
| 46 |
|
| 47 |
public function execute($params) { |
| 48 |
$this->validate_params($params); |
| 49 |
$this->require_capability('manage_options'); |
| 50 |
|
| 51 |
$url = esc_url_raw($params['url']); |
| 52 |
|
| 53 |
if (empty($url)) { |
| 54 |
throw new Exception('Invalid URL provided'); |
| 55 |
} |
| 56 |
|
| 57 |
if (!function_exists('google_index_direct')) { |
| 58 |
throw new Exception('Google Index Direct module is not available'); |
| 59 |
} |
| 60 |
|
| 61 |
$service_info = google_index_direct()->get_service_account_info(); |
| 62 |
if (isset($service_info['error'])) { |
| 63 |
throw new Exception('Google Instant Index API is not configured. Please add your service account JSON in settings.'); |
| 64 |
} |
| 65 |
|
| 66 |
$result = google_index_direct()->index_url($url, 'update'); |
| 67 |
|
| 68 |
if (!empty($result['success'])) { |
| 69 |
return $this->success([ |
| 70 |
'url' => $url, |
| 71 |
'action' => 'update', |
| 72 |
'result' => $result, |
| 73 |
'message' => 'URL submitted for indexing successfully', |
| 74 |
]); |
| 75 |
} |
| 76 |
|
| 77 |
$error_msg = isset($result['error']['message']) ? $result['error']['message'] : 'Unknown error'; |
| 78 |
throw new Exception('Google API error: ' . $error_msg); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Instant Index Delete Tool |
| 84 |
* |
| 85 |
* Request URL removal from Google index |
| 86 |
*/ |
| 87 |
class MCP_Tool_Instant_Index_Delete extends MCP_Tool_Base { |
| 88 |
|
| 89 |
public function get_name() { |
| 90 |
return 'wordpress_instant_index_delete'; |
| 91 |
} |
| 92 |
|
| 93 |
public function get_description() { |
| 94 |
return 'Request URL removal from Google Instant Index API (URL_DELETED)'; |
| 95 |
} |
| 96 |
|
| 97 |
public function get_input_schema() { |
| 98 |
return [ |
| 99 |
'type' => 'object', |
| 100 |
'properties' => [ |
| 101 |
'url' => [ |
| 102 |
'type' => 'string', |
| 103 |
'description' => 'The URL to request removal for', |
| 104 |
], |
| 105 |
], |
| 106 |
'required' => ['url'], |
| 107 |
]; |
| 108 |
} |
| 109 |
|
| 110 |
public function execute($params) { |
| 111 |
$this->validate_params($params); |
| 112 |
$this->require_capability('manage_options'); |
| 113 |
|
| 114 |
$url = esc_url_raw($params['url']); |
| 115 |
|
| 116 |
if (empty($url)) { |
| 117 |
throw new Exception('Invalid URL provided'); |
| 118 |
} |
| 119 |
|
| 120 |
if (!function_exists('google_index_direct')) { |
| 121 |
throw new Exception('Google Index Direct module is not available'); |
| 122 |
} |
| 123 |
|
| 124 |
$service_info = google_index_direct()->get_service_account_info(); |
| 125 |
if (isset($service_info['error'])) { |
| 126 |
throw new Exception('Google Instant Index API is not configured. Please add your service account JSON in settings.'); |
| 127 |
} |
| 128 |
|
| 129 |
$result = google_index_direct()->index_url($url, 'delete'); |
| 130 |
|
| 131 |
if (!empty($result['success'])) { |
| 132 |
return $this->success([ |
| 133 |
'url' => $url, |
| 134 |
'action' => 'delete', |
| 135 |
'result' => $result, |
| 136 |
'message' => 'URL removal requested successfully', |
| 137 |
]); |
| 138 |
} |
| 139 |
|
| 140 |
$error_msg = isset($result['error']['message']) ? $result['error']['message'] : 'Unknown error'; |
| 141 |
throw new Exception('Google API error: ' . $error_msg); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Instant Index Status Tool |
| 147 |
* |
| 148 |
* Check indexing status of a URL |
| 149 |
*/ |
| 150 |
class MCP_Tool_Instant_Index_Status extends MCP_Tool_Base { |
| 151 |
|
| 152 |
public function get_name() { |
| 153 |
return 'wordpress_instant_index_status'; |
| 154 |
} |
| 155 |
|
| 156 |
public function get_description() { |
| 157 |
return 'Check the indexing status of a URL in Google Instant Index API'; |
| 158 |
} |
| 159 |
|
| 160 |
public function get_input_schema() { |
| 161 |
return [ |
| 162 |
'type' => 'object', |
| 163 |
'properties' => [ |
| 164 |
'url' => [ |
| 165 |
'type' => 'string', |
| 166 |
'description' => 'The URL to check status for', |
| 167 |
], |
| 168 |
], |
| 169 |
'required' => ['url'], |
| 170 |
]; |
| 171 |
} |
| 172 |
|
| 173 |
public function execute($params) { |
| 174 |
$this->validate_params($params); |
| 175 |
$this->require_capability('manage_options'); |
| 176 |
|
| 177 |
$url = esc_url_raw($params['url']); |
| 178 |
|
| 179 |
if (empty($url)) { |
| 180 |
throw new Exception('Invalid URL provided'); |
| 181 |
} |
| 182 |
|
| 183 |
if (!function_exists('google_index_direct')) { |
| 184 |
throw new Exception('Google Index Direct module is not available'); |
| 185 |
} |
| 186 |
|
| 187 |
$service_info = google_index_direct()->get_service_account_info(); |
| 188 |
if (isset($service_info['error'])) { |
| 189 |
throw new Exception('Google Instant Index API is not configured. Please add your service account JSON in settings.'); |
| 190 |
} |
| 191 |
|
| 192 |
$result = google_index_direct()->get_url_status($url); |
| 193 |
|
| 194 |
if (!empty($result['success'])) { |
| 195 |
return $this->success([ |
| 196 |
'url' => $url, |
| 197 |
'status' => $result, |
| 198 |
'message' => 'Status retrieved successfully', |
| 199 |
]); |
| 200 |
} |
| 201 |
|
| 202 |
$error_msg = isset($result['error']['message']) ? $result['error']['message'] : 'Unknown error'; |
| 203 |
throw new Exception('Google API error: ' . $error_msg); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Instant Index Bulk Update Tool |
| 209 |
* |
| 210 |
* Submit multiple URLs to Google for indexing |
| 211 |
*/ |
| 212 |
class MCP_Tool_Instant_Index_Bulk_Update extends MCP_Tool_Base { |
| 213 |
|
| 214 |
public function get_name() { |
| 215 |
return 'wordpress_instant_index_bulk_update'; |
| 216 |
} |
| 217 |
|
| 218 |
public function get_description() { |
| 219 |
return 'Submit multiple URLs to Google Instant Index API for indexing (batch operation, max 100 URLs)'; |
| 220 |
} |
| 221 |
|
| 222 |
public function get_input_schema() { |
| 223 |
return [ |
| 224 |
'type' => 'object', |
| 225 |
'properties' => [ |
| 226 |
'urls' => [ |
| 227 |
'type' => 'array', |
| 228 |
'description' => 'Array of URLs to submit for indexing (max 100)', |
| 229 |
'items' => [ |
| 230 |
'type' => 'string', |
| 231 |
], |
| 232 |
'maxItems' => 100, |
| 233 |
], |
| 234 |
], |
| 235 |
'required' => ['urls'], |
| 236 |
]; |
| 237 |
} |
| 238 |
|
| 239 |
public function execute($params) { |
| 240 |
$this->validate_params($params); |
| 241 |
$this->require_capability('manage_options'); |
| 242 |
|
| 243 |
if (!isset($params['urls']) || !is_array($params['urls'])) { |
| 244 |
throw new Exception('URLs must be provided as an array'); |
| 245 |
} |
| 246 |
|
| 247 |
$urls = array_map('esc_url_raw', array_filter($params['urls'])); |
| 248 |
|
| 249 |
if (empty($urls)) { |
| 250 |
throw new Exception('No valid URLs provided'); |
| 251 |
} |
| 252 |
|
| 253 |
if (count($urls) > 100) { |
| 254 |
throw new Exception('Maximum 100 URLs can be submitted at once'); |
| 255 |
} |
| 256 |
|
| 257 |
if (!function_exists('google_index_direct')) { |
| 258 |
throw new Exception('Google Index Direct module is not available'); |
| 259 |
} |
| 260 |
|
| 261 |
$service_info = google_index_direct()->get_service_account_info(); |
| 262 |
if (isset($service_info['error'])) { |
| 263 |
throw new Exception('Google Instant Index API is not configured. Please add your service account JSON in settings.'); |
| 264 |
} |
| 265 |
|
| 266 |
$results = []; |
| 267 |
foreach ($urls as $url) { |
| 268 |
$results[$url] = google_index_direct()->index_url($url, 'update'); |
| 269 |
} |
| 270 |
|
| 271 |
return $this->success([ |
| 272 |
'urls_count' => count($urls), |
| 273 |
'urls' => $urls, |
| 274 |
'action' => 'update', |
| 275 |
'results' => $results, |
| 276 |
'message' => count($urls) . ' URL(s) submitted for indexing successfully', |
| 277 |
]); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Get Instant Index Settings Tool |
| 283 |
* |
| 284 |
* Get Google Instant Index API configuration |
| 285 |
*/ |
| 286 |
class MCP_Tool_Get_Instant_Index_Settings extends MCP_Tool_Base { |
| 287 |
|
| 288 |
public function get_name() { |
| 289 |
return 'wordpress_get_instant_index_settings'; |
| 290 |
} |
| 291 |
|
| 292 |
public function get_description() { |
| 293 |
return 'Get Google Instant Index API settings and configuration'; |
| 294 |
} |
| 295 |
|
| 296 |
public function get_input_schema() { |
| 297 |
return [ |
| 298 |
'type' => 'object', |
| 299 |
'properties' => (object)[], |
| 300 |
]; |
| 301 |
} |
| 302 |
|
| 303 |
public function execute($params) { |
| 304 |
$this->validate_params($params); |
| 305 |
$this->require_capability('manage_options'); |
| 306 |
|
| 307 |
if (!function_exists('google_index_direct')) { |
| 308 |
throw new Exception('Google Index Direct module is not available'); |
| 309 |
} |
| 310 |
|
| 311 |
$service_info = google_index_direct()->get_service_account_info(); |
| 312 |
$is_configured = !isset($service_info['error']); |
| 313 |
|
| 314 |
$options = get_option('metasync_options_instant_indexing', ['json_key' => '', 'post_types' => []]); |
| 315 |
$post_types = isset($options['post_types']) && is_array($options['post_types']) ? $options['post_types'] : []; |
| 316 |
|
| 317 |
return $this->success([ |
| 318 |
'api_configured' => $is_configured, |
| 319 |
'service_account' => $service_info, |
| 320 |
'post_types' => $post_types, |
| 321 |
'guide_url' => 'https://developers.google.com/search/apis/indexing-api/v3/quickstart', |
| 322 |
]); |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Update Instant Index Settings Tool |
| 328 |
* |
| 329 |
* Update Google Instant Index API configuration |
| 330 |
*/ |
| 331 |
class MCP_Tool_Update_Instant_Index_Settings extends MCP_Tool_Base { |
| 332 |
|
| 333 |
public function get_name() { |
| 334 |
return 'wordpress_update_instant_index_settings'; |
| 335 |
} |
| 336 |
|
| 337 |
public function get_description() { |
| 338 |
return 'Update Google Instant Index API settings (JSON key and post types)'; |
| 339 |
} |
| 340 |
|
| 341 |
public function get_input_schema() { |
| 342 |
return [ |
| 343 |
'type' => 'object', |
| 344 |
'properties' => [ |
| 345 |
'json_key' => [ |
| 346 |
'type' => 'string', |
| 347 |
'description' => 'Google service account JSON key (entire JSON content)', |
| 348 |
], |
| 349 |
'post_types' => [ |
| 350 |
'type' => 'array', |
| 351 |
'description' => 'Array of post type slugs to enable instant indexing for', |
| 352 |
'items' => [ |
| 353 |
'type' => 'string', |
| 354 |
], |
| 355 |
], |
| 356 |
], |
| 357 |
]; |
| 358 |
} |
| 359 |
|
| 360 |
public function execute($params) { |
| 361 |
$this->validate_params($params); |
| 362 |
$this->require_capability('manage_options'); |
| 363 |
|
| 364 |
if (!function_exists('google_index_direct')) { |
| 365 |
throw new Exception('Google Index Direct module is not available'); |
| 366 |
} |
| 367 |
|
| 368 |
$options = get_option('metasync_options_instant_indexing', ['json_key' => '', 'post_types' => []]); |
| 369 |
|
| 370 |
// Update JSON key if provided |
| 371 |
if (isset($params['json_key'])) { |
| 372 |
$json_key = sanitize_textarea_field($params['json_key']); |
| 373 |
|
| 374 |
// Validate JSON format |
| 375 |
$decoded = json_decode($json_key, true); |
| 376 |
if (json_last_error() !== JSON_ERROR_NONE) { |
| 377 |
throw new Exception('Invalid JSON key format'); |
| 378 |
} |
| 379 |
|
| 380 |
// Save parsed credentials via google_index_direct |
| 381 |
$save_result = google_index_direct()->save_service_account_config($decoded); |
| 382 |
if (!$save_result) { |
| 383 |
throw new Exception('Failed to save service account configuration'); |
| 384 |
} |
| 385 |
|
| 386 |
// Also store raw JSON in legacy option for settings page display |
| 387 |
$options['json_key'] = $json_key; |
| 388 |
} |
| 389 |
|
| 390 |
// Update post types if provided |
| 391 |
if (isset($params['post_types'])) { |
| 392 |
if (!is_array($params['post_types'])) { |
| 393 |
throw new Exception('post_types must be an array'); |
| 394 |
} |
| 395 |
|
| 396 |
$valid_post_types = get_post_types(['public' => true]); |
| 397 |
$post_types = []; |
| 398 |
foreach ($params['post_types'] as $post_type) { |
| 399 |
$post_type = sanitize_text_field($post_type); |
| 400 |
if (isset($valid_post_types[$post_type])) { |
| 401 |
$post_types[] = $post_type; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
$options['post_types'] = $post_types; |
| 406 |
} |
| 407 |
|
| 408 |
// Save legacy options (post_types + json_key for display) |
| 409 |
update_option('metasync_options_instant_indexing', $options); |
| 410 |
|
| 411 |
$service_info = google_index_direct()->get_service_account_info(); |
| 412 |
$is_configured = !isset($service_info['error']); |
| 413 |
|
| 414 |
return $this->success([ |
| 415 |
'api_configured' => $is_configured, |
| 416 |
'post_types' => isset($options['post_types']) ? $options['post_types'] : [], |
| 417 |
'message' => 'Instant Index settings updated successfully', |
| 418 |
]); |
| 419 |
} |
| 420 |
} |
| 421 |
|