PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 19.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v19.0
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 19.0, at src/routes/indexables-head-route.php

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