PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.8
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.8
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 18.8, at src/routes/yoast-head-rest-field.php

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