| 1 |
<?php |
| 2 |
/** |
| 3 |
* MCP Module Init |
| 4 |
* |
| 5 |
* Bootstraps the MCP (Model Context Protocol) module: the SureCookie |
| 6 |
* MCP server registration and the localized data consumed by the |
| 7 |
* Settings > MCP admin page. |
| 8 |
* |
| 9 |
* @package SureCookie |
| 10 |
* @subpackage SureCookie/Inc/Modules/Mcp |
| 11 |
* @since 1.1.0 |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace SureCookie\Inc\Modules\Mcp; |
| 15 |
|
| 16 |
use SureCookie\Inc\Traits\GetInstance; |
| 17 |
|
| 18 |
if ( ! defined( 'ABSPATH' ) ) { |
| 19 |
exit; // Exit if accessed directly. |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Init class |
| 24 |
* |
| 25 |
* Handles initialization and WordPress hooks for the MCP module. |
| 26 |
* |
| 27 |
* @since 1.1.0 |
| 28 |
*/ |
| 29 |
class Init { |
| 30 |
use GetInstance; |
| 31 |
|
| 32 |
/** |
| 33 |
* Constructor. |
| 34 |
* |
| 35 |
* @since 1.1.0 |
| 36 |
*/ |
| 37 |
private function __construct() { |
| 38 |
Server::get_instance(); |
| 39 |
|
| 40 |
// Add localization variables for the Settings > MCP admin page. |
| 41 |
add_filter( 'surecookie_localized_admin_data', [ $this, 'add_admin_localization_vars' ] ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Add MCP localization variables for the admin React app. |
| 46 |
* |
| 47 |
* Added even when enable_mcp is off so the page can explain adapter/WP gaps. |
| 48 |
* |
| 49 |
* @param array<string, mixed> $variables Localization variables. |
| 50 |
* @return array<string, mixed> Localization variables. |
| 51 |
* @since 1.1.0 |
| 52 |
*/ |
| 53 |
public function add_admin_localization_vars( $variables = [] ) { |
| 54 |
return array_merge( |
| 55 |
$variables, |
| 56 |
[ |
| 57 |
'mcp_rest_url' => esc_url_raw( trailingslashit( rest_url() ) ), |
| 58 |
'mcp_username' => wp_get_current_user()->user_login, |
| 59 |
'mcp_adapter_installed' => Server::is_adapter_available(), |
| 60 |
'mcp_abilities_supported' => function_exists( 'wp_register_ability' ), |
| 61 |
'mcp_app_password_url' => esc_url_raw( admin_url( 'profile.php' ) . '#application-passwords-section' ), |
| 62 |
'mcp_adapter_download_url' => esc_url_raw( |
| 63 |
/** Filters the download URL shown when the MCP Adapter plugin is missing. */ |
| 64 |
apply_filters( 'surecookie_mcp_adapter_download_url', 'https://github.com/WordPress/mcp-adapter/releases/latest' ) |
| 65 |
), |
| 66 |
] |
| 67 |
); |
| 68 |
} |
| 69 |
} |
| 70 |
|