PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.8.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.8.0
3.8.0 3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 All 112 releases
templately / modules / mcp-core / Support / RestDispatcher.php

RestDispatcher.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.8.0, at modules/mcp-core/Support/RestDispatcher.php

77 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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