| 1 |
<?php |
| 2 |
/** |
| 3 |
* Dispatches to Templately's existing internal REST routes in-process |
| 4 |
* (no HTTP wire traffic) so abilities can reuse `Templately\API\*` logic |
| 5 |
* without duplicating it (research.md §2). Fresh implementation — FR-011 |
| 6 |
* forbids depending on the existing dev-only modules/developer/mcp-tools/ registry. |
| 7 |
* |
| 8 |
* @package Templately\Modules\McpCore\Support |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace Templately\Modules\McpCore\Support; |
| 12 |
|
| 13 |
use WP_Error; |
| 14 |
use WP_REST_Request; |
| 15 |
use WP_REST_Response; |
| 16 |
|
| 17 |
class RestDispatcher { |
| 18 |
|
| 19 |
/** |
| 20 |
* @param string $method GET|POST|etc. |
| 21 |
* @param string $route Route path, e.g. '/templately/v1/items'. |
| 22 |
* @param array $params Request params. |
| 23 |
* |
| 24 |
* @return WP_REST_Response|WP_Error |
| 25 |
*/ |
| 26 |
public static function dispatch( string $method, string $route, array $params = [] ) { |
| 27 |
$normalized_route = strpos( $route, '/' ) === 0 ? $route : '/' . ltrim( $route, '/' ); |
| 28 |
|
| 29 |
$request = new WP_REST_Request( strtoupper( $method ), $normalized_route ); |
| 30 |
foreach ( $params as $key => $value ) { |
| 31 |
$request->set_param( $key, $value ); |
| 32 |
} |
| 33 |
|
| 34 |
$response = rest_do_request( $request ); |
| 35 |
|
| 36 |
if ( is_wp_error( $response ) ) { |
| 37 |
// Only truly fundamental dispatch failures (e.g. no matching route) |
| 38 |
// come back this way — application-level errors returned by a route |
| 39 |
// callback are converted by WP_REST_Server into an error-shaped |
| 40 |
// WP_REST_Response instead (handled below). |
| 41 |
return $response; |
| 42 |
} |
| 43 |
|
| 44 |
if ( ! $response instanceof WP_REST_Response ) { |
| 45 |
return new WP_Error( 'invalid_rest_response', __( 'Unexpected internal REST response.', 'templately' ) ); |
| 46 |
} |
| 47 |
|
| 48 |
$data = $response->get_data(); |
| 49 |
|
| 50 |
if ( $response->get_status() >= 400 ) { |
| 51 |
return new WP_Error( |
| 52 |
$data['code'] ?? 'internal_rest_error', |
| 53 |
$data['message'] ?? __( 'An internal REST request failed.', 'templately' ), |
| 54 |
$data['data'] ?? [] |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
// A failed route can also come back with a status BELOW 400: a |
| 59 |
// transport-level failure (cloud unreachable) carries `data.status = 0`, |
| 60 |
// and WP_REST_Server derives the HTTP status from that field — so the |
| 61 |
// check above never fires and callers would iterate an error body as if |
| 62 |
// it were their payload. Detect the flattened WP_Error shape directly |
| 63 |
// (same signature RestEnvelope::is_wp_error_shape() keys on). |
| 64 |
if ( |
| 65 |
is_array( $data ) |
| 66 |
&& isset( $data['code'], $data['message'] ) |
| 67 |
&& is_string( $data['code'] ) |
| 68 |
&& ! array_key_exists( 'success', $data ) |
| 69 |
&& isset( $data['data'] ) && is_array( $data['data'] ) && array_key_exists( 'status', $data['data'] ) |
| 70 |
) { |
| 71 |
return new WP_Error( $data['code'], $data['message'], $data['data'] ); |
| 72 |
} |
| 73 |
|
| 74 |
return $response; |
| 75 |
} |
| 76 |
} |
| 77 |
|