| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Utils\Response; |
| 4 |
|
| 5 |
use WP_Error; |
| 6 |
|
| 7 |
/** |
| 8 |
* Builds THE downstream response envelope (spec 043 / FR-001). |
| 9 |
* |
| 10 |
* Every REST and AJAX response the plugin sends its own frontend has this |
| 11 |
* shape — one success shape, one error shape, no per-endpoint variations: |
| 12 |
* |
| 13 |
* success: { success: true, data: <payload>, meta?: {} } |
| 14 |
* error: { success: false, code, message, data: { status, severity, |
| 15 |
* retryable, fields, context, debug? } } |
| 16 |
* |
| 17 |
* `message` is ALWAYS plain text (FR-010) and `debug` exists only when the host |
| 18 |
* is in debug mode (FR-008) — a stack trace never reaches the browser. |
| 19 |
* |
| 20 |
* Validated against `contracts/error-envelope.schema.json`. |
| 21 |
*/ |
| 22 |
class Envelope { |
| 23 |
|
| 24 |
/** |
| 25 |
* @param mixed $data payload of any shape. |
| 26 |
* @param array $meta optional metadata (pagination, timings, …). |
| 27 |
* @return array |
| 28 |
*/ |
| 29 |
public static function success( $data = null, $meta = [] ) { |
| 30 |
$envelope = [ |
| 31 |
'success' => true, |
| 32 |
'data' => $data, |
| 33 |
]; |
| 34 |
|
| 35 |
if ( ! empty( $meta ) && is_array( $meta ) ) { |
| 36 |
$envelope['meta'] = $meta; |
| 37 |
} |
| 38 |
|
| 39 |
return $envelope; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* @param TemplatelyError|WP_Error|string $error A normalized error, a legacy WP_Error, or a bare code. |
| 44 |
* @param string $message Used only when $error is a bare code. |
| 45 |
* @param array $data Used only when $error is a bare code. |
| 46 |
* @return array |
| 47 |
*/ |
| 48 |
public static function error( $error, $message = '', $data = [] ) { |
| 49 |
$error = self::to_templately_error( $error, $message, $data ); |
| 50 |
|
| 51 |
$body = [ |
| 52 |
'success' => false, |
| 53 |
'code' => $error->code(), |
| 54 |
'message' => $error->message(), |
| 55 |
'data' => [ |
| 56 |
'status' => $error->status(), |
| 57 |
'severity' => $error->severity(), |
| 58 |
'retryable' => $error->is_retryable(), |
| 59 |
'fields' => (object) $error->fields(), |
| 60 |
'context' => (object) $error->context(), |
| 61 |
], |
| 62 |
]; |
| 63 |
|
| 64 |
$error_data = $error->data(); |
| 65 |
if ( self::is_debug_mode() && ! empty( $error_data['debug'] ) ) { |
| 66 |
$body['data']['debug'] = (object) self::scrub_debug( $error_data['debug'] ); |
| 67 |
} |
| 68 |
|
| 69 |
return $body; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* @param TemplatelyError|WP_Error|string $error |
| 74 |
* @param string $message |
| 75 |
* @param array $data |
| 76 |
* @return TemplatelyError |
| 77 |
*/ |
| 78 |
private static function to_templately_error( $error, $message, $data ) { |
| 79 |
if ( $error instanceof TemplatelyError ) { |
| 80 |
return $error; |
| 81 |
} |
| 82 |
|
| 83 |
if ( $error instanceof WP_Error ) { |
| 84 |
return TemplatelyError::from_wp_error( $error ); |
| 85 |
} |
| 86 |
|
| 87 |
return new TemplatelyError( (string) $error, $message, $data ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Whether an envelope array is the error variant. |
| 92 |
* |
| 93 |
* @param mixed $envelope |
| 94 |
* @return bool |
| 95 |
*/ |
| 96 |
public static function is_error( $envelope ) { |
| 97 |
return is_array( $envelope ) && isset( $envelope['success'] ) && false === $envelope['success']; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* @return bool |
| 102 |
*/ |
| 103 |
public static function is_debug_mode() { |
| 104 |
return ( defined( 'WP_DEBUG' ) && WP_DEBUG ) || ( defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Even in debug mode the envelope never carries a stack trace or a server |
| 109 |
* path — those go to the log, not the wire (FR-008 / INV-3). |
| 110 |
* |
| 111 |
* @param array $debug |
| 112 |
* @return array |
| 113 |
*/ |
| 114 |
public static function scrub_debug( $debug ) { |
| 115 |
if ( ! is_array( $debug ) ) { |
| 116 |
return []; |
| 117 |
} |
| 118 |
|
| 119 |
unset( $debug['trace'], $debug['file'], $debug['line'], $debug['exception'] ); |
| 120 |
|
| 121 |
return $debug; |
| 122 |
} |
| 123 |
} |
| 124 |
|