PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.1
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.1
28.6 28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 All 130 releases
wordpress-seo / src / schema-aggregator / user-interface / site-schema-aggregator-xml-route.php

site-schema-aggregator-xml-route.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.1, at src/schema-aggregator/user-interface/site-schema-aggregator-xml-route.php

137 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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_Error;
6 use WP_REST_Response;
7 use Yoast\WP\SEO\Main;
8 use Yoast\WP\SEO\Routes\Route_Interface;
9 use Yoast\WP\SEO\Schema_Aggregator\Application\Aggregate_Site_Schema_Map_Command;
10 use Yoast\WP\SEO\Schema_Aggregator\Application\Aggregate_Site_Schema_Map_Command_Handler;
11 use Yoast\WP\SEO\Schema_Aggregator\Application\Cache\Xml_Manager;
12 use Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Aggregator_Config;
13 use Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Config;
14 use Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Schema_Aggregator_Conditional;
15
16 /**
17 * Handles the route to represent all indexable post types as XML.
18 *
19 * @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded
20 */
21 class Site_Schema_Aggregator_Xml_Route implements Route_Interface {
22 /**
23 * Represents the site schema prefix.
24 *
25 * @var string
26 */
27 public const ROUTE_PREFIX = 'schema-aggregator';
28
29 /**
30 * Represents route to view the schema.
31 *
32 * @var string
33 */
34 public const GET_SCHEMA_ROUTE = self::ROUTE_PREFIX . '/get-xml';
35
36 /**
37 * The command handler instance.
38 *
39 * @var Aggregate_Site_Schema_Map_Command_Handler
40 */
41 private $aggregate_site_schema_map_command_handler;
42
43 /**
44 * The XML cache manager instance.
45 *
46 * @var Xml_Manager
47 */
48 private $xml_cache_manager;
49
50 /**
51 * The aggregator configuration instance.
52 *
53 * @var Aggregator_Config
54 */
55 private $aggregator_config;
56
57 /**
58 * Returns the conditional for this route.
59 *
60 * @return array<string> The conditionals that must be met to load this.
61 */
62 public static function get_conditionals() {
63 return [ Schema_Aggregator_Conditional::class ];
64 }
65
66 /**
67 * Site_Schema_Aggregator_Route constructor.
68 *
69 * @param Aggregate_Site_Schema_Map_Command_Handler $aggregate_site_schema_map_command_handler The command handler.
70 * @param Xml_Manager $xml_cache_manager The XML cache
71 * manager.
72 * @param Aggregator_Config $aggregator_config The aggregator
73 * configuration.
74 */
75 public function __construct(
76 Aggregate_Site_Schema_Map_Command_Handler $aggregate_site_schema_map_command_handler,
77 Xml_Manager $xml_cache_manager,
78 Aggregator_Config $aggregator_config
79 ) {
80 $this->aggregate_site_schema_map_command_handler = $aggregate_site_schema_map_command_handler;
81 $this->xml_cache_manager = $xml_cache_manager;
82 $this->aggregator_config = $aggregator_config;
83 }
84
85 /**
86 * Registers routes with WordPress.
87 *
88 * @return void
89 */
90 public function register_routes() {
91 $schema_aggregator_xml_route = [
92 'methods' => 'GET',
93 'callback' => [ $this, 'render_schema_xml' ],
94 'permission_callback' => [ $this, 'get_permission_callback' ],
95 ];
96
97 \register_rest_route( Main::API_V1_NAMESPACE, self::GET_SCHEMA_ROUTE, $schema_aggregator_xml_route );
98 }
99
100 /**
101 * Permission callback for the route.
102 *
103 * @codeCoverageIgnore -- No sensible tests can be written for this.
104 *
105 * @return bool True if the user has permission, false otherwise.
106 */
107 public function get_permission_callback(): bool {
108 return true;
109 }
110
111 /**
112 * Returns a XML representation of the possible post types that can be used for schema.
113 *
114 * @return WP_REST_Response|WP_Error The success or failure response.
115 */
116 public function render_schema_xml() {
117 $cached_xml = $this->xml_cache_manager->get();
118 if ( $cached_xml !== null ) {
119 $xml = $cached_xml;
120 }
121 else {
122
123 $post_types = $this->aggregator_config->get_allowed_post_types();
124
125 $command = new Aggregate_Site_Schema_Map_Command( $post_types );
126 $xml = $this->aggregate_site_schema_map_command_handler->handle( $command );
127
128 $this->xml_cache_manager->set( $xml );
129 }
130 $response = new WP_REST_Response( $xml, 200 );
131 $response->header( 'Content-Type', 'application/xml; charset=UTF-8' );
132 $response->header( 'Cache-Control', 'public, max-age=300' );
133
134 return $response;
135 }
136 }
137