PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
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 19.0 All 129 releases
wordpress-seo / src / routes / indexables-head-route.php

indexables-head-route.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI trunk, at src/routes/indexables-head-route.php

105 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Routes;
4
5 use WP_REST_Request;
6 use WP_REST_Response;
7 use WPSEO_Utils;
8 use Yoast\WP\SEO\Actions\Indexables\Indexable_Head_Action;
9 use Yoast\WP\SEO\Conditionals\Headless_Rest_Endpoints_Enabled_Conditional;
10 use Yoast\WP\SEO\Main;
11
12 /**
13 * Head route for indexables.
14 */
15 class Indexables_Head_Route implements Route_Interface {
16
17 /**
18 * The posts route constant.
19 *
20 * @var string
21 */
22 public const HEAD_FOR_URL_ROUTE = 'get_head';
23
24 /**
25 * The full posts route constant.
26 *
27 * @var string
28 */
29 public const FULL_HEAD_FOR_URL_ROUTE = Main::API_V1_NAMESPACE . '/' . self::HEAD_FOR_URL_ROUTE;
30
31 /**
32 * The head action.
33 *
34 * @var Indexable_Head_Action
35 */
36 private $head_action;
37
38 /**
39 * Indexable_Indexation_Route constructor.
40 *
41 * @param Indexable_Head_Action $head_action The head action.
42 */
43 public function __construct( Indexable_Head_Action $head_action ) {
44 $this->head_action = $head_action;
45 }
46
47 /**
48 * Returns the conditionals based in which this loadable should be active.
49 *
50 * @return array<string>
51 */
52 public static function get_conditionals() {
53 return [ Headless_Rest_Endpoints_Enabled_Conditional::class ];
54 }
55
56 /**
57 * Registers routes with WordPress.
58 *
59 * @return void
60 */
61 public function register_routes() {
62 $route_args = [
63 'methods' => 'GET',
64 'callback' => [ $this, 'get_head' ],
65 'permission_callback' => '__return_true',
66 'args' => [
67 'url' => [
68 'validate_callback' => [ $this, 'is_valid_url' ],
69 'required' => true,
70 ],
71 ],
72 ];
73 \register_rest_route( Main::API_V1_NAMESPACE, self::HEAD_FOR_URL_ROUTE, $route_args );
74 }
75
76 /**
77 * Gets the head of a page for a given URL.
78 *
79 * @param WP_REST_Request $request The request. This request should have a url param set.
80 *
81 * @return WP_REST_Response The response.
82 */
83 public function get_head( WP_REST_Request $request ) {
84 $url = \esc_url_raw( \utf8_uri_encode( $request['url'] ) );
85 $data = $this->head_action->for_url( $url );
86
87 return new WP_REST_Response( $data, $data->status );
88 }
89
90 /**
91 * Checks if a url is a valid url.
92 *
93 * @param string $url The url to check.
94 *
95 * @return bool Whether or not the url is valid.
96 */
97 public function is_valid_url( $url ) {
98 $url = WPSEO_Utils::sanitize_url( \utf8_uri_encode( $url ) );
99 if ( \filter_var( $url, \FILTER_VALIDATE_URL ) === false ) {
100 return false;
101 }
102 return true;
103 }
104 }
105