PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / trunk
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! vtrunk
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 / includes / Utils / Response / Envelope.php

Envelope.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! trunk, at includes/Utils/Response/Envelope.php

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