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 / yoast-head-rest-field.php

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

224 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore Yoast.Files.FileName.InvalidClassFileName -- Reason: this explicitly concerns the Yoast head fields.
2
3 namespace Yoast\WP\SEO\Routes;
4
5 use Yoast\WP\SEO\Actions\Indexables\Indexable_Head_Action;
6 use Yoast\WP\SEO\Conditionals\Headless_Rest_Endpoints_Enabled_Conditional;
7 use Yoast\WP\SEO\Helpers\Post_Helper;
8 use Yoast\WP\SEO\Helpers\Post_Type_Helper;
9 use Yoast\WP\SEO\Helpers\Taxonomy_Helper;
10
11 /**
12 * Yoast_Head_REST_Field class.
13 *
14 * Registers the yoast head REST field.
15 * Not technically a route but behaves the same so is included here.
16 */
17 class Yoast_Head_REST_Field implements Route_Interface {
18
19 /**
20 * The name of the Yoast head field.
21 *
22 * @var string
23 */
24 public const YOAST_HEAD_ATTRIBUTE_NAME = 'yoast_head';
25
26 /**
27 * The name of the Yoast head JSON field.
28 *
29 * @var string
30 */
31 public const YOAST_JSON_HEAD_ATTRIBUTE_NAME = 'yoast_head_json';
32
33 /**
34 * The post type helper.
35 *
36 * @var Post_Type_Helper
37 */
38 protected $post_type_helper;
39
40 /**
41 * The taxonomy helper.
42 *
43 * @var Taxonomy_Helper
44 */
45 protected $taxonomy_helper;
46
47 /**
48 * The post helper.
49 *
50 * @var Post_Helper
51 */
52 protected $post_helper;
53
54 /**
55 * The head action.
56 *
57 * @var Indexable_Head_Action
58 */
59 protected $head_action;
60
61 /**
62 * Returns the conditionals based in which this loadable should be active.
63 *
64 * @return array
65 */
66 public static function get_conditionals() {
67 return [ Headless_Rest_Endpoints_Enabled_Conditional::class ];
68 }
69
70 /**
71 * Yoast_Head_REST_Field constructor.
72 *
73 * @param Post_Type_Helper $post_type_helper The post type helper.
74 * @param Taxonomy_Helper $taxonomy_helper The taxonomy helper.
75 * @param Post_Helper $post_helper The post helper.
76 * @param Indexable_Head_Action $head_action The head action.
77 */
78 public function __construct(
79 Post_Type_Helper $post_type_helper,
80 Taxonomy_Helper $taxonomy_helper,
81 Post_Helper $post_helper,
82 Indexable_Head_Action $head_action
83 ) {
84 $this->post_type_helper = $post_type_helper;
85 $this->taxonomy_helper = $taxonomy_helper;
86 $this->post_helper = $post_helper;
87 $this->head_action = $head_action;
88 }
89
90 /**
91 * Registers routes with WordPress.
92 *
93 * @return void
94 */
95 public function register_routes() {
96 $public_post_types = $this->post_type_helper->get_indexable_post_types();
97
98 foreach ( $public_post_types as $post_type ) {
99 $this->register_rest_fields( $post_type, 'for_post' );
100 }
101
102 $public_taxonomies = $this->taxonomy_helper->get_indexable_taxonomies();
103
104 foreach ( $public_taxonomies as $taxonomy ) {
105 if ( $taxonomy === 'post_tag' ) {
106 $taxonomy = 'tag';
107 }
108 $this->register_rest_fields( $taxonomy, 'for_term' );
109 }
110
111 $this->register_rest_fields( 'user', 'for_author' );
112 $this->register_rest_fields( 'type', 'for_post_type_archive' );
113 }
114
115 /**
116 * Returns the head for a post.
117 *
118 * @param array $params The rest request params.
119 * @param string $format The desired output format.
120 *
121 * @return string|null The head.
122 */
123 public function for_post( $params, $format = self::YOAST_HEAD_ATTRIBUTE_NAME ) {
124 if ( ! isset( $params['id'] ) ) {
125 return null;
126 }
127
128 if ( ! $this->post_helper->is_post_indexable( $params['id'] ) ) {
129 return null;
130 }
131 $obj = $this->head_action->for_post( $params['id'] );
132
133 return $this->render_object( $obj, $format );
134 }
135
136 /**
137 * Returns the head for a term.
138 *
139 * @param array $params The rest request params.
140 * @param string $format The desired output format.
141 *
142 * @return string|null The head.
143 */
144 public function for_term( $params, $format = self::YOAST_HEAD_ATTRIBUTE_NAME ) {
145 $obj = $this->head_action->for_term( $params['id'] );
146
147 return $this->render_object( $obj, $format );
148 }
149
150 /**
151 * Returns the head for an author.
152 *
153 * @param array $params The rest request params.
154 * @param string $format The desired output format.
155 *
156 * @return string|null The head.
157 */
158 public function for_author( $params, $format = self::YOAST_HEAD_ATTRIBUTE_NAME ) {
159 $obj = $this->head_action->for_author( $params['id'] );
160
161 return $this->render_object( $obj, $format );
162 }
163
164 /**
165 * Returns the head for a post type archive.
166 *
167 * @param array $params The rest request params.
168 * @param string $format The desired output format.
169 *
170 * @return string|null The head.
171 */
172 public function for_post_type_archive( $params, $format = self::YOAST_HEAD_ATTRIBUTE_NAME ) {
173 if ( $params['slug'] === 'post' ) {
174 $obj = $this->head_action->for_posts_page();
175 }
176 elseif ( ! $this->post_type_helper->has_archive( $params['slug'] ) ) {
177 return null;
178 }
179 else {
180 $obj = $this->head_action->for_post_type_archive( $params['slug'] );
181 }
182
183 return $this->render_object( $obj, $format );
184 }
185
186 /**
187 * Registers the Yoast rest fields.
188 *
189 * @param string $object_type The object type.
190 * @param string $callback The function name of the callback.
191 *
192 * @return void
193 */
194 protected function register_rest_fields( $object_type, $callback ) {
195 // Output metadata in page head meta tags.
196 \register_rest_field( $object_type, self::YOAST_HEAD_ATTRIBUTE_NAME, [ 'get_callback' => [ $this, $callback ] ] );
197 // Output metadata in a json object in a head meta tag.
198 \register_rest_field( $object_type, self::YOAST_JSON_HEAD_ATTRIBUTE_NAME, [ 'get_callback' => [ $this, $callback ] ] );
199 }
200
201 /**
202 * Returns the correct property for the Yoast head.
203 *
204 * @param stdObject $head The Yoast head.
205 * @param string $format The format to return.
206 *
207 * @return string|array|null The output value. String if HTML was requested, array otherwise.
208 */
209 protected function render_object( $head, $format = self::YOAST_HEAD_ATTRIBUTE_NAME ) {
210 if ( $head->status === 404 ) {
211 return null;
212 }
213
214 switch ( $format ) {
215 case self::YOAST_HEAD_ATTRIBUTE_NAME:
216 return $head->html;
217 case self::YOAST_JSON_HEAD_ATTRIBUTE_NAME:
218 return $head->json;
219 }
220
221 return null;
222 }
223 }
224