wordpress-seo
/
src
/
schema-aggregator
/
infrastructure
/
schema_map
/
schema-map-header-adapter.php
schema-map-header-adapter.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.8, at src/schema-aggregator/infrastructure/schema_map/schema-map-header-adapter.php
| 1 | <?php |
| 2 | // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 3 | namespace Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Schema_Map; |
| 4 | |
| 5 | use WP_REST_Response; |
| 6 | |
| 7 | /** |
| 8 | * Adapter to set proper response headers for Schema Map responses. |
| 9 | */ |
| 10 | class Schema_Map_Header_Adapter { |
| 11 | |
| 12 | /** |
| 13 | * Set proper headers for Schema Map responses. |
| 14 | * |
| 15 | * @param WP_REST_Response $response The REST response object. |
| 16 | * |
| 17 | * @return void |
| 18 | */ |
| 19 | public function set_header_for_request( WP_REST_Response $response ) { |
| 20 | $data = $response->get_data(); |
| 21 | |
| 22 | foreach ( $response->get_headers() as $key => $value ) { |
| 23 | \header( \sprintf( '%s: %s', $key, $value ) ); |
| 24 | } |
| 25 | |
| 26 | $headers = $response->get_headers(); |
| 27 | $content_type = ( $headers['Content-Type'] ?? 'application/json; charset=UTF-8' ); |
| 28 | |
| 29 | if ( \strpos( $content_type, 'application/xml' ) !== false ) { |
| 30 | \header( 'Content-Type: application/xml; charset=UTF-8' ); |
| 31 | echo $data; //@phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $data should already be escaped here since this just adds headers to the request. |
| 32 | } |
| 33 | else { |
| 34 | \header( 'X-Accel-Buffering: no' ); |
| 35 | \header( 'Content-Type: application/json; charset=UTF-8' ); |
| 36 | foreach ( $data as $schema_piece ) { |
| 37 | // @phpcs:disable Yoast.Yoast.JsonEncodeAlternative.FoundWithAdditionalParams -- The pretty print option breaks the JSONL format. |
| 38 | echo \wp_json_encode( $schema_piece, ( \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE ) ) . \PHP_EOL; // @phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $data should already be escaped here since this just adds headers to the request. |
| 39 | if ( \ob_get_level() > 0 ) { |
| 40 | \ob_flush(); |
| 41 | } |
| 42 | \flush(); |
| 43 | // @phpcs:enable |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 |