Domain
4 weeks ago
REST
4 weeks ago
AbilitiesCategories.php
4 weeks ago
AbilitiesLoader.php
4 weeks ago
AbilitiesRegistry.php
4 weeks ago
AbilitiesRestBridge.php
4 weeks ago
AbilitiesRestBridge.php
130 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Abilities REST Bridge class file. |
| 4 | */ |
| 5 | |
| 6 | declare( strict_types=1 ); |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Internal\Abilities; |
| 9 | |
| 10 | use Automattic\WooCommerce\Internal\Abilities\REST\RestAbilityFactory; |
| 11 | use Automattic\WooCommerce\Internal\MCP\MCPAdapterProvider; |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | /** |
| 16 | * Abilities REST Bridge class for WooCommerce. |
| 17 | * |
| 18 | * Configuration-driven registry that exposes REST endpoints as WordPress abilities. |
| 19 | * Each ability is explicitly configured with ID, label, description, and operation. |
| 20 | */ |
| 21 | class AbilitiesRestBridge { |
| 22 | |
| 23 | /** |
| 24 | * Get REST controller configurations with explicit IDs, labels, and descriptions. |
| 25 | * |
| 26 | * @return array Controller configurations. |
| 27 | */ |
| 28 | private static function get_configurations(): array { |
| 29 | return array( |
| 30 | array( |
| 31 | 'controller' => \WC_REST_Products_Controller::class, |
| 32 | 'route' => '/wc/v3/products', |
| 33 | 'abilities' => array( |
| 34 | array( |
| 35 | 'id' => 'woocommerce/products-list', |
| 36 | 'operation' => 'list', |
| 37 | 'label' => __( 'List Products', 'woocommerce' ), |
| 38 | 'description' => __( 'Retrieve a paginated list of products with optional filters for status, category, price range, and other attributes.', 'woocommerce' ), |
| 39 | ), |
| 40 | array( |
| 41 | 'id' => 'woocommerce/products-get', |
| 42 | 'operation' => 'get', |
| 43 | 'label' => __( 'Get Product', 'woocommerce' ), |
| 44 | 'description' => __( 'Retrieve detailed information about a single product by ID, including price, description, images, and metadata.', 'woocommerce' ), |
| 45 | ), |
| 46 | array( |
| 47 | 'id' => 'woocommerce/products-create', |
| 48 | 'operation' => 'create', |
| 49 | 'label' => __( 'Create Product', 'woocommerce' ), |
| 50 | 'description' => __( 'Create a new product with name, price, description, and other product attributes.', 'woocommerce' ), |
| 51 | ), |
| 52 | array( |
| 53 | 'id' => 'woocommerce/products-update', |
| 54 | 'operation' => 'update', |
| 55 | 'label' => __( 'Update Product', 'woocommerce' ), |
| 56 | 'description' => __( 'Update an existing product by modifying its attributes such as price, stock, description, or metadata.', 'woocommerce' ), |
| 57 | ), |
| 58 | array( |
| 59 | 'id' => 'woocommerce/products-delete', |
| 60 | 'operation' => 'delete', |
| 61 | 'label' => __( 'Delete Product', 'woocommerce' ), |
| 62 | 'description' => __( 'Permanently delete a product from the store. This action cannot be undone.', 'woocommerce' ), |
| 63 | ), |
| 64 | ), |
| 65 | ), |
| 66 | array( |
| 67 | 'controller' => \WC_REST_Orders_Controller::class, |
| 68 | 'route' => '/wc/v3/orders', |
| 69 | 'abilities' => array( |
| 70 | array( |
| 71 | 'id' => 'woocommerce/orders-list', |
| 72 | 'operation' => 'list', |
| 73 | 'label' => __( 'List Orders', 'woocommerce' ), |
| 74 | 'description' => __( 'Retrieve a paginated list of orders with optional filters for status, customer, date range, and other criteria.', 'woocommerce' ), |
| 75 | ), |
| 76 | array( |
| 77 | 'id' => 'woocommerce/orders-get', |
| 78 | 'operation' => 'get', |
| 79 | 'label' => __( 'Get Order', 'woocommerce' ), |
| 80 | 'description' => __( 'Retrieve detailed information about a single order by ID, including line items, customer details, and payment information.', 'woocommerce' ), |
| 81 | ), |
| 82 | array( |
| 83 | 'id' => 'woocommerce/orders-create', |
| 84 | 'operation' => 'create', |
| 85 | 'label' => __( 'Create Order', 'woocommerce' ), |
| 86 | 'description' => __( 'Create a new order with customer information, line items, shipping details, and payment information.', 'woocommerce' ), |
| 87 | ), |
| 88 | array( |
| 89 | 'id' => 'woocommerce/orders-update', |
| 90 | 'operation' => 'update', |
| 91 | 'label' => __( 'Update Order', 'woocommerce' ), |
| 92 | 'description' => __( 'Update an existing order by modifying status, customer information, line items, or other order details.', 'woocommerce' ), |
| 93 | ), |
| 94 | ), |
| 95 | ), |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Initialize the ability registration. |
| 101 | * |
| 102 | * @internal |
| 103 | */ |
| 104 | final public static function init(): void { |
| 105 | /* |
| 106 | * Register abilities when Abilities API is ready. |
| 107 | * Support both old (pre-6.9) and new (6.9+) action names. |
| 108 | */ |
| 109 | add_action( 'abilities_api_init', array( __CLASS__, 'register_abilities' ) ); |
| 110 | add_action( 'wp_abilities_api_init', array( __CLASS__, 'register_abilities' ) ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Register all configured abilities. |
| 115 | */ |
| 116 | public static function register_abilities(): void { |
| 117 | // Only register abilities if this is an MCP endpoint request. |
| 118 | // We check here (on abilities_api_init action) rather than earlier |
| 119 | // because REST request detection requires the WordPress REST infrastructure |
| 120 | // to be fully initialized. |
| 121 | if ( ! MCPAdapterProvider::is_mcp_request() ) { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | foreach ( self::get_configurations() as $config ) { |
| 126 | RestAbilityFactory::register_controller_abilities( $config ); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 |