| 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_Response; |
| 6 |
use Yoast\WP\SEO\Main; |
| 7 |
use Yoast\WP\SEO\Routes\Route_Interface; |
| 8 |
use Yoast\WP\SEO\Schema_Aggregator\Application\Schema_Map\Schema_Map_Xml_Provider; |
| 9 |
use Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Schema_Aggregator_Conditional; |
| 10 |
|
| 11 |
/** |
| 12 |
* Handles the route to represent all indexable post types as XML. |
| 13 |
* |
| 14 |
* @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded |
| 15 |
*/ |
| 16 |
class Site_Schema_Aggregator_Xml_Route implements Route_Interface { |
| 17 |
/** |
| 18 |
* Represents the site schema prefix. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
public const ROUTE_PREFIX = 'schema-aggregator'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Represents route to view the schema. |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
public const GET_SCHEMA_ROUTE = self::ROUTE_PREFIX . '/get-xml'; |
| 30 |
|
| 31 |
/** |
| 32 |
* The schema map XML provider. |
| 33 |
* |
| 34 |
* @var Schema_Map_Xml_Provider |
| 35 |
*/ |
| 36 |
private $schema_map_xml_provider; |
| 37 |
|
| 38 |
/** |
| 39 |
* Returns the conditional for this route. |
| 40 |
* |
| 41 |
* @return array<string> The conditionals that must be met to load this. |
| 42 |
*/ |
| 43 |
public static function get_conditionals() { |
| 44 |
return [ Schema_Aggregator_Conditional::class ]; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Site_Schema_Aggregator_Route constructor. |
| 49 |
* |
| 50 |
* @param Schema_Map_Xml_Provider $schema_map_xml_provider The schema map XML provider. |
| 51 |
*/ |
| 52 |
public function __construct( Schema_Map_Xml_Provider $schema_map_xml_provider ) { |
| 53 |
$this->schema_map_xml_provider = $schema_map_xml_provider; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Registers routes with WordPress. |
| 58 |
* |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
public function register_routes() { |
| 62 |
$schema_aggregator_xml_route = [ |
| 63 |
'methods' => 'GET', |
| 64 |
'callback' => [ $this, 'render_schema_xml' ], |
| 65 |
'permission_callback' => [ $this, 'get_permission_callback' ], |
| 66 |
]; |
| 67 |
|
| 68 |
\register_rest_route( Main::API_V1_NAMESPACE, self::GET_SCHEMA_ROUTE, $schema_aggregator_xml_route ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Permission callback for the route. |
| 73 |
* |
| 74 |
* @codeCoverageIgnore -- No sensible tests can be written for this. |
| 75 |
* |
| 76 |
* @return bool True if the user has permission, false otherwise. |
| 77 |
*/ |
| 78 |
public function get_permission_callback(): bool { |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Returns a XML representation of the post types that are used for schema. |
| 84 |
* |
| 85 |
* @return WP_REST_Response The response. |
| 86 |
*/ |
| 87 |
public function render_schema_xml() { |
| 88 |
$response = new WP_REST_Response( $this->schema_map_xml_provider->get_xml(), 200 ); |
| 89 |
$response->header( 'Content-Type', 'application/xml; charset=UTF-8' ); |
| 90 |
$response->header( 'Cache-Control', 'public, max-age=300' ); |
| 91 |
|
| 92 |
return $response; |
| 93 |
} |
| 94 |
} |
| 95 |
|