| @@ -7,34 +7,19 @@ | ||
| 7 | 7 | |
| 8 | 8 | namespace Disco\Engine; |
| 9 | 9 | |
| 10 | 10 | /** |
| 11 | - * Prevents stray output from corrupting REST and AJAX JSON responses. | |
| 11 | + * Prevents PHP debug notices/warnings from corrupting REST and AJAX JSON responses. | |
| 12 | 12 | * |
| 13 | - * Other plugins frequently emit output during an API request: PHP notices | |
| 14 | - * (e.g. "Function _load_textdomain_just_in_time was called incorrectly"), | |
| 15 | - * deprecation warnings, or plain `echo` calls inside `shutdown`/`rest_post_dispatch`. | |
| 16 | - * Anything printed before the JSON body, and anything printed after it, makes the | |
| 17 | - * response unparseable in the browser. | |
| 18 | - * | |
| 19 | - * Strategy: | |
| 20 | - * 1. Open an output buffer before `plugins_loaded` so nothing reaches the client early. | |
| 21 | - * 2. For our own REST routes, render and send the JSON body ourselves, discarding | |
| 22 | - * everything buffered up to that point. | |
| 23 | - * 3. Immediately after the body is flushed, open a buffer that discards everything, | |
| 24 | - * so late output (shutdown hooks, destructors, deprecations) is never appended. | |
| 25 | - * | |
| 26 | - * AJAX handlers call clean() before wp_send_json_* for step 2, and the | |
| 27 | - * `wp_die_ajax_handler` filter performs step 3. | |
| 13 | + * When WP_DEBUG and WP_DEBUG_DISPLAY are both true, notices from plugins (e.g. | |
| 14 | + * early textdomain loading) are printed before the JSON body, breaking JSON | |
| 15 | + * parsing in the browser. We start an output buffer before plugins_loaded fires | |
| 16 | + * so all stray output is captured, then discard it cleanly before the response | |
| 17 | + * is sent. AJAX handlers call clean() manually before wp_send_json_*. | |
| 28 | 18 | */ |
| 29 | 19 | class OutputBuffer { |
| 30 | 20 | |
| 31 | 21 | /** |
| 32 | - * REST route prefix owned by this plugin. | |
| 33 | - */ | |
| 34 | - private const ROUTE_PREFIX = '/disco/'; | |
| 35 | - | |
| 36 | - /** | |
| 37 | 22 | * Start output buffering for REST/AJAX requests. |
| 38 | 23 | * Call this immediately after the Composer autoload is required in disco.php. |
| 39 | 24 | */ |
| 40 | 25 | public static function start(): void { |
| @@ -52,25 +37,24 @@ | ||
| 52 | 37 | |
| 53 | 38 | if ( self::is_rest_request() ) { |
| 54 | 39 | add_filter( 'rest_pre_serve_request', array( __CLASS__, 'handle_rest' ), 1, 4 ); |
| 55 | 40 | } |
| 56 | - | |
| 57 | - if ( self::is_ajax_request() ) { | |
| 58 | - // Runs after wp_send_json_* has echoed its body, before the script dies. | |
| 59 | - add_filter( 'wp_die_ajax_handler', array( __CLASS__, 'handle_ajax_die' ), PHP_INT_MAX ); | |
| 60 | - } | |
| 61 | 41 | } |
| 62 | 42 | |
| 63 | 43 | /** |
| 64 | 44 | * Hooked on rest_pre_serve_request at priority 1. |
| 65 | 45 | * |
| 66 | - * For Disco routes we take over serving: discard whatever stray output was | |
| 67 | - * buffered, echo the JSON body, flush it, and then swallow any later output. | |
| 68 | - * For third-party routes we only drop the stray output we captured and let | |
| 69 | - * WordPress serve the response as usual. | |
| 46 | + * Strategy: | |
| 47 | + * Level N = whatever PHP had before disco.php loaded (DISCO_OB_LEVEL) | |
| 48 | + * Level N+1 = our buffer, containing stray debug notices | |
| 49 | + * Level N+2 = WordPress's dispatch buffer (if WP uses ob_start in serve_request) | |
| 70 | 50 | * |
| 71 | - * @param bool $served Whether the request has already been served. | |
| 72 | - * @param \WP_HTTP_Response $result The response object. | |
| 51 | + * We pop WP's dispatch buffer, discard our stray-output buffer, open a fresh | |
| 52 | + * buffer, and return false so WP echoes the JSON body into the clean buffer. | |
| 53 | + * WP's ob_get_clean() then collects only the actual JSON response. | |
| 54 | + * | |
| 55 | + * @param bool $served Whether the request has already been served. | |
| 56 | + * @param \WP_REST_Response $result The response object. | |
| 73 | 57 | * @param \WP_REST_Request $request The current REST request. |
| 74 | 58 | * @param \WP_REST_Server $server The REST server instance. |
| 75 | 59 | * @return bool |
| 76 | 60 | */ |
| @@ -75,77 +59,33 @@ | ||
| 75 | 59 | * @return bool |
| 76 | 60 | */ |
| 77 | 61 | public static function handle_rest( $served, $result, $request, $server ): bool { |
| 78 | 62 | if ( $served ) { |
| 79 | - return true; | |
| 63 | + return $served; | |
| 80 | 64 | } |
| 81 | 65 | |
| 82 | - // Drop everything buffered so far (notices printed while WP booted/dispatched). | |
| 83 | - self::clean(); | |
| 66 | + $initial_level = DISCO_OB_LEVEL; | |
| 84 | 67 | |
| 85 | - if ( ! $result instanceof \WP_HTTP_Response | |
| 86 | - || ! $request instanceof \WP_REST_Request | |
| 87 | - || ! $server instanceof \WP_REST_Server | |
| 88 | - || ! self::is_disco_route( $request ) | |
| 89 | - || null !== $request->get_param( '_jsonp' ) ) { | |
| 90 | - return false; // Let WordPress echo the body itself. | |
| 91 | - } | |
| 68 | + // Pop WP's dispatch buffer (level N+2) if it exists, preserving its content. | |
| 69 | + $wp_dispatch = ob_get_level() > ( $initial_level + 1 ) ? (string) ob_get_clean() : ''; | |
| 92 | 70 | |
| 93 | - if ( 'HEAD' === $request->get_method() ) { | |
| 94 | - self::seal(); | |
| 95 | - | |
| 96 | - return true; | |
| 71 | + // Discard our stray-notices buffer (level N+1). | |
| 72 | + if ( ob_get_level() > $initial_level ) { | |
| 73 | + ob_end_clean(); | |
| 97 | 74 | } |
| 98 | 75 | |
| 99 | - $embed = isset( $_GET['_embed'] ) ? rest_parse_embed_param( wp_unslash( $_GET['_embed'] ) ) : false; // phpcs:ignore WordPress.Security | |
| 100 | - $data = $server->response_to_data( $result, $embed ); | |
| 76 | + // Open a fresh, clean buffer at the level WP expects for its ob_get_clean(). | |
| 77 | + ob_start(); | |
| 101 | 78 | |
| 102 | - /** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */ | |
| 103 | - $data = apply_filters( 'rest_pre_echo_response', $data, $server, $request ); | |
| 104 | - | |
| 105 | - // Filters above may have printed something; drop it before we emit the body. | |
| 106 | - self::clean(); | |
| 107 | - | |
| 108 | - if ( null === $data || 204 === $result->get_status() ) { | |
| 109 | - self::seal(); | |
| 110 | - | |
| 111 | - return true; | |
| 79 | + // Restore any output WP produced during dispatch (almost always empty). | |
| 80 | + if ( '' !== $wp_dispatch ) { | |
| 81 | + echo $wp_dispatch; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 112 | 82 | } |
| 113 | 83 | |
| 114 | - $options = ( defined( 'WP_DEBUG' ) && WP_DEBUG && $request->has_param( '_pretty' ) ) ? JSON_PRETTY_PRINT : 0; | |
| 115 | - $json = wp_json_encode( $data, $options ); | |
| 116 | - | |
| 117 | - if ( false === $json ) { | |
| 118 | - $json = (string) wp_json_encode( | |
| 119 | - array( | |
| 120 | - 'code' => 'rest_encode_error', | |
| 121 | - 'message' => json_last_error_msg(), | |
| 122 | - 'data' => array( 'status' => 500 ), | |
| 123 | - ) | |
| 124 | - ); | |
| 125 | - } | |
| 126 | - | |
| 127 | - echo $json; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 128 | - | |
| 129 | - self::seal(); | |
| 130 | - | |
| 131 | - return true; | |
| 84 | + return false; // Let WP echo the JSON body and collect it via ob_get_clean(). | |
| 132 | 85 | } |
| 133 | 86 | |
| 134 | 87 | /** |
| 135 | - * Hooked on wp_die_ajax_handler, which fires after wp_send_json_* echoed its body. | |
| 136 | - * Seals the output so late plugin output cannot be appended to the JSON. | |
| 137 | - * | |
| 138 | - * @param callable $handler The wp_die handler. | |
| 139 | - * @return callable | |
| 140 | - */ | |
| 141 | - public static function handle_ajax_die( $handler ) { | |
| 142 | - self::seal(); | |
| 143 | - | |
| 144 | - return $handler; | |
| 145 | - } | |
| 146 | - | |
| 147 | - /** | |
| 148 | 88 | * Discard all output buffered above DISCO_OB_LEVEL. |
| 149 | 89 | * Call this in AJAX handlers before wp_send_json_* to strip stray debug output. |
| 150 | 90 | */ |
| 151 | 91 | public static function clean(): void { |
| @@ -154,31 +94,8 @@ | ||
| 154 | 94 | } |
| 155 | 95 | while ( ob_get_level() > DISCO_OB_LEVEL ) { |
| 156 | 96 | ob_end_clean(); |
| 157 | 97 | } |
| 158 | - } | |
| 159 | - | |
| 160 | - /** | |
| 161 | - * Push the response we already echoed to the client, then open a buffer that | |
| 162 | - * throws away everything written afterwards (shutdown hooks, deprecations, | |
| 163 | - * destructors), so nothing can be appended to the JSON body. | |
| 164 | - */ | |
| 165 | - private static function seal(): void { | |
| 166 | - while ( defined( 'DISCO_OB_LEVEL' ) && ob_get_level() > DISCO_OB_LEVEL ) { | |
| 167 | - ob_end_flush(); | |
| 168 | - } | |
| 169 | - | |
| 170 | - flush(); | |
| 171 | - | |
| 172 | - ob_start( | |
| 173 | - static function () { | |
| 174 | - return ''; | |
| 175 | - } | |
| 176 | - ); | |
| 177 | - } | |
| 178 | - | |
| 179 | - private static function is_disco_route( \WP_REST_Request $request ): bool { | |
| 180 | - return 0 === strpos( (string) $request->get_route(), self::ROUTE_PREFIX ); | |
| 181 | 98 | } |
| 182 | 99 | |
| 183 | 100 | private static function is_api_request(): bool { |
| 184 | 101 | return self::is_ajax_request() || self::is_rest_request(); |