| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Boots the wpDataTables MCP integration after the loaded adapter is confirmed usable. |
| 5 |
* |
| 6 |
* @package wpDataTables |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WDTMCP\Infrastructure\WP\MCP; |
| 10 |
|
| 11 |
use Throwable; |
| 12 |
use WP\MCP\Core\McpAdapter; |
| 13 |
|
| 14 |
defined('ABSPATH') or die('Access denied.'); |
| 15 |
|
| 16 |
/** |
| 17 |
* Orchestrates notices, compatibility gating, requires, and hook registration. |
| 18 |
* |
| 19 |
* Loaded from the main plugin file before any class that extends WP\MCP\* is required. |
| 20 |
*/ |
| 21 |
class WdtmcpBootstrap |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Parent classes this build subclasses, mapped to methods we implement. |
| 25 |
* |
| 26 |
* @return array<string, list<string>> |
| 27 |
*/ |
| 28 |
private static function extendedClasses(): array |
| 29 |
{ |
| 30 |
return array( |
| 31 |
'WP\\MCP\\Transport\\HttpTransport' => array('__construct', 'register_routes'), |
| 32 |
); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Plugin files needed once the adapter has passed the compatibility gate. |
| 37 |
* |
| 38 |
* @return list<string> |
| 39 |
*/ |
| 40 |
private static function integrationFiles(): array |
| 41 |
{ |
| 42 |
return array( |
| 43 |
'Infrastructure/WP/MCP/Helpers/McpHelpers/WdtmcpAbilitiesHelper.php', |
| 44 |
'Infrastructure/WP/MCP/Helpers/McpHelpers/WdtmcpLicenseGuideHelper.php', |
| 45 |
'Infrastructure/WP/MCP/WdtmcpAbilitiesRegistrar.php', |
| 46 |
'Infrastructure/WP/MCP/WdtmcpMcpHttpTransport.php', |
| 47 |
'Infrastructure/WP/MCP/WdtmcpMcpServerRegistrar.php', |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Load and register the wpDataTables MCP Adapter integration. |
| 53 |
*/ |
| 54 |
public static function load(): void |
| 55 |
{ |
| 56 |
global $wp_version; |
| 57 |
|
| 58 |
if (version_compare($wp_version, WDTMCP_MIN_WP_VERSION, '<')) { |
| 59 |
self::registerUnavailableNotice( |
| 60 |
sprintf( |
| 61 |
/* translators: 1: required WordPress version, 2: current WordPress version */ |
| 62 |
__( |
| 63 |
'MCP integration requires WordPress %1$s or newer. Your site is running WordPress %2$s.', |
| 64 |
'wpdatatables' |
| 65 |
), |
| 66 |
WDTMCP_MIN_WP_VERSION, |
| 67 |
$wp_version |
| 68 |
) |
| 69 |
); |
| 70 |
|
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
if (! function_exists('wp_register_ability') || ! class_exists(McpAdapter::class)) { |
| 75 |
$missing = array(); |
| 76 |
|
| 77 |
if (! function_exists('wp_register_ability')) { |
| 78 |
$missing[] = __('WordPress Abilities API', 'wpdatatables'); |
| 79 |
} |
| 80 |
|
| 81 |
if (! class_exists(McpAdapter::class)) { |
| 82 |
$missing[] = __('MCP Adapter', 'wpdatatables'); |
| 83 |
} |
| 84 |
|
| 85 |
self::registerUnavailableNotice( |
| 86 |
sprintf( |
| 87 |
/* translators: %s: comma-separated list of missing components */ |
| 88 |
__( |
| 89 |
'MCP integration is unavailable because required components are missing: %s. ' |
| 90 |
. 'Try reinstalling or updating wpDataTables.', |
| 91 |
'wpdatatables' |
| 92 |
), |
| 93 |
implode(', ', $missing) |
| 94 |
) |
| 95 |
); |
| 96 |
|
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
$incompatibilities = WdtmcpAdapterCompatibility::findProblems(self::extendedClasses()); |
| 101 |
|
| 102 |
if ($incompatibilities) { |
| 103 |
self::registerUnavailableNotice( |
| 104 |
sprintf( |
| 105 |
/* translators: %s: comma-separated list of reasons the loaded MCP Adapter is unusable */ |
| 106 |
__( |
| 107 |
'Another plugin loaded an MCP Adapter that wpDataTables cannot build on (%s), ' |
| 108 |
. 'so MCP features stay disabled. Update or deactivate the conflicting plugin to enable them.', |
| 109 |
'wpdatatables' |
| 110 |
), |
| 111 |
implode('; ', $incompatibilities) |
| 112 |
) |
| 113 |
); |
| 114 |
|
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
foreach (self::integrationFiles() as $relativePath) { |
| 119 |
require_once WDT_ROOT_PATH . $relativePath; |
| 120 |
} |
| 121 |
|
| 122 |
try { |
| 123 |
McpAdapter::instance(); |
| 124 |
} catch (Throwable $exception) { |
| 125 |
self::registerUnavailableNotice( |
| 126 |
sprintf( |
| 127 |
/* translators: %s: error message reported by the MCP Adapter */ |
| 128 |
__('The MCP Adapter could not be started: %s', 'wpdatatables'), |
| 129 |
$exception->getMessage() |
| 130 |
) |
| 131 |
); |
| 132 |
|
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
add_action('mcp_adapter_init', array(WdtmcpMcpServerRegistrar::class, 'init')); |
| 137 |
add_action('wp_abilities_api_categories_init', array(WdtmcpAbilitiesRegistrar::class, 'registerCategories')); |
| 138 |
add_action('wp_abilities_api_init', array(WdtmcpAbilitiesRegistrar::class, 'registerAbilities')); |
| 139 |
|
| 140 |
if (function_exists('wdtmcp_maybe_migrate_css_to_global')) { |
| 141 |
add_action('init', 'wdtmcp_maybe_migrate_css_to_global', 99); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Show an admin notice when MCP integration cannot be loaded. |
| 147 |
* |
| 148 |
* Limited to wpDataTables screens so a broken MCP setup never interrupts admins working on |
| 149 |
* unrelated parts of the site. |
| 150 |
* |
| 151 |
* @param string $message Human-readable reason (already translated). |
| 152 |
*/ |
| 153 |
private static function registerUnavailableNotice(string $message): void |
| 154 |
{ |
| 155 |
if (! is_admin()) { |
| 156 |
return; |
| 157 |
} |
| 158 |
|
| 159 |
add_action( |
| 160 |
'admin_notices', |
| 161 |
static function () use ($message): void { |
| 162 |
if (! current_user_can('manage_options')) { |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
$page = isset($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : ''; |
| 167 |
|
| 168 |
if (strpos($page, 'wpdatatables') === false) { |
| 169 |
return; |
| 170 |
} |
| 171 |
?> |
| 172 |
<div class="notice notice-warning"> |
| 173 |
<p> |
| 174 |
<strong><?php esc_html_e('wpDataTables MCP:', 'wpdatatables'); ?></strong> |
| 175 |
<?php echo esc_html($message); ?> |
| 176 |
</p> |
| 177 |
</div> |
| 178 |
<?php |
| 179 |
} |
| 180 |
); |
| 181 |
} |
| 182 |
} |
| 183 |
|