| 1 |
<?php |
| 2 |
/** |
| 3 |
* Publishes the registry's capabilities through the WordPress Abilities API |
| 4 |
* (spec 044, FR-010/FR-012). |
| 5 |
* |
| 6 |
* This is an ADAPTER, not a source of truth. Before 044 the abilities were |
| 7 |
* declared here (well, in MCP.php) and everything else read from them; now the |
| 8 |
* registry holds the declarations and this bridge projects them outward. That |
| 9 |
* inversion is what removes the Abilities API from the critical path — the |
| 10 |
* native server works identically whether or not this bridge ever attaches. |
| 11 |
* |
| 12 |
* Attached ONLY when `wp_register_ability()` exists. Its absence is not an |
| 13 |
* error and must produce no admin notice (FR-012): the overwhelming majority of |
| 14 |
* sites running this plugin are below the WordPress version that ships the |
| 15 |
* Abilities API, and the whole point of 044 is that they are fully supported. |
| 16 |
* |
| 17 |
* @package Templately\Modules\WpAbilitiesApi\Adapters |
| 18 |
*/ |
| 19 |
|
| 20 |
namespace Templately\Modules\WpAbilitiesApi\Adapters; |
| 21 |
|
| 22 |
use Templately\Modules\McpCore\Registry\ToolRegistry; |
| 23 |
|
| 24 |
class AbilitiesBridge { |
| 25 |
|
| 26 |
const CATEGORY = 'templately'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Whether WordPress core's Abilities API is present (core 6.9+). |
| 30 |
* |
| 31 |
* Split from the old all-or-nothing `MCP::is_available()`: the Abilities API |
| 32 |
* and mcp-adapter are INDEPENDENT optional consumers now, and neither gates |
| 33 |
* the built-in server. Templately supports WordPress 5.0+, so on most sites |
| 34 |
* this returns false and MCP still works fully. |
| 35 |
* |
| 36 |
* @return bool |
| 37 |
*/ |
| 38 |
public static function is_available(): bool { |
| 39 |
return function_exists( 'wp_register_ability' ); |
| 40 |
} |
| 41 |
|
| 42 |
public static function attach(): void { |
| 43 |
add_action( 'wp_abilities_api_categories_init', [ self::class, 'register_category' ] ); |
| 44 |
add_action( 'wp_abilities_api_init', [ self::class, 'register_abilities' ] ); |
| 45 |
} |
| 46 |
|
| 47 |
public static function register_category(): void { |
| 48 |
if ( ! function_exists( 'wp_register_ability_category' ) ) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
if ( function_exists( 'wp_has_ability_category' ) && wp_has_ability_category( self::CATEGORY ) ) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
wp_register_ability_category( |
| 57 |
self::CATEGORY, |
| 58 |
[ |
| 59 |
'label' => __( 'Templately', 'templately' ), |
| 60 |
'description' => __( 'Search, preview, and import Templately designs on this WordPress site.', 'templately' ), |
| 61 |
] |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* One ability per registry descriptor — no hand-maintained list (FR-011). |
| 67 |
* test-RegistryAbilitiesParity.php asserts this set equals the registry's. |
| 68 |
*/ |
| 69 |
public static function register_abilities(): void { |
| 70 |
if ( ! function_exists( 'wp_register_ability' ) ) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
foreach ( ToolRegistry::get_instance()->all() as $descriptor ) { |
| 75 |
wp_register_ability( $descriptor->id, $descriptor->to_ability_args() ); |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|