wordpress-seo
/
src
/
schema-aggregator
/
user-interface
/
site-schema-response-header-integration.php
site-schema-response-header-integration.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.4, at src/schema-aggregator/user-interface/site-schema-response-header-integration.php
| 1 | <?php |
| 2 | // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 3 | namespace Yoast\WP\SEO\Schema_Aggregator\User_Interface; |
| 4 | |
| 5 | use WP_REST_Request; |
| 6 | use WP_REST_Response; |
| 7 | use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 8 | use Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Schema_Aggregator_Conditional; |
| 9 | use Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Schema_Map\Schema_Map_Header_Adapter; |
| 10 | |
| 11 | /** |
| 12 | * Integration to set proper response headers for Site Schema endpoints. |
| 13 | */ |
| 14 | class Site_Schema_Response_Header_Integration implements Integration_Interface { |
| 15 | |
| 16 | /** |
| 17 | * The schema map header adapter. |
| 18 | * |
| 19 | * @var Schema_Map_Header_Adapter |
| 20 | */ |
| 21 | private $schema_map_header_adapter; |
| 22 | |
| 23 | /** |
| 24 | * Returns the conditional for this route. |
| 25 | * |
| 26 | * @return array<string> The conditionals that must be met to load this. |
| 27 | */ |
| 28 | public static function get_conditionals() { |
| 29 | return [ Schema_Aggregator_Conditional::class ]; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Constructor. |
| 34 | * |
| 35 | * @param Schema_Map_Header_Adapter $schema_map_header_adapter The schema map header adapter. |
| 36 | */ |
| 37 | public function __construct( Schema_Map_Header_Adapter $schema_map_header_adapter ) { |
| 38 | $this->schema_map_header_adapter = $schema_map_header_adapter; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Registers the hooks for the integration. |
| 43 | * |
| 44 | * @return void |
| 45 | */ |
| 46 | public function register_hooks() { |
| 47 | \add_filter( 'rest_pre_serve_request', [ $this, 'serve_custom_response' ], 10, 4 ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Serve custom responses (XML or JSON) for schemamap endpoints |
| 52 | * |
| 53 | * Intercepts schemamap index endpoints to serve either XML or JSON with |
| 54 | * proper content types and formatting. For XML responses (from /schema), |
| 55 | * outputs raw XML. For JSON responses (from /schema.json or post-type endpoints), |
| 56 | * outputs JSON with unescaped slashes for cleaner URLs. |
| 57 | * |
| 58 | * Only affects /yoast/v1/schema-aggregator endpoints. Other endpoints are unaffected. |
| 59 | * |
| 60 | * @param bool $served Whether the request has already been served. |
| 61 | * @param WP_REST_Response $result Result to send to the client. |
| 62 | * @param WP_REST_Request $request Request object. |
| 63 | * |
| 64 | * @return bool True if we served the request, false otherwise. |
| 65 | * @codeCoverageIgnore ignore this since its needs to rely on headers being sent. Which does not work in integration tests. |
| 66 | */ |
| 67 | public function serve_custom_response( $served, $result, $request ): bool { |
| 68 | if ( ! $request instanceof WP_REST_Request || \strpos( $request->get_route(), '/yoast/v1/schema-aggregator' ) !== 0 ) { |
| 69 | return (bool) $served; |
| 70 | } |
| 71 | |
| 72 | if ( ! $result instanceof WP_REST_Response || $result->is_error() ) { |
| 73 | return (bool) $served; |
| 74 | } |
| 75 | |
| 76 | $this->schema_map_header_adapter->set_header_for_request( $result ); |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | } |
| 81 |