PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
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
← All changes | src/abilities/user-interface/abilities-integration.php +316 -6 28.028.5 View file →
@@ -2,10 +2,14 @@
2 2
3 3 // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
4 4 namespace Yoast\WP\SEO\Abilities\User_Interface;
5 5
6 +use Yoast\WP\SEO\Abilities\Application\Post_SEO_Data_Collector;
7 +use Yoast\WP\SEO\Abilities\Application\Post_SEO_Data_Updater;
6 8 use Yoast\WP\SEO\Abilities\Application\Score_Retriever;
7 9 use Yoast\WP\SEO\Conditionals\Abilities_API_Conditional;
10 +use Yoast\WP\SEO\Conditionals\Should_Index_Indexables_Conditional;
11 +use Yoast\WP\SEO\Config\Schema_Types;
8 12 use Yoast\WP\SEO\Editors\Application\Analysis_Features\Enabled_Analysis_Features_Repository;
9 13 use Yoast\WP\SEO\Editors\Framework\Inclusive_Language_Analysis;
10 14 use Yoast\WP\SEO\Editors\Framework\Keyphrase_Analysis;
11 15 use Yoast\WP\SEO\Editors\Framework\Readability_Analysis;
@@ -38,14 +42,31 @@
38 42 */
39 43 private $enabled_analysis_features_repository;
40 44
41 45 /**
46 + * The post SEO data collector.
47 + *
48 + * @var Post_SEO_Data_Collector
49 + */
50 + private $post_seo_data_collector;
51 +
52 + /**
53 + * The post SEO data updater.
54 + *
55 + * @var Post_SEO_Data_Updater
56 + */
57 + private $post_seo_data_updater;
58 +
59 + /**
42 60 * Returns the conditionals based on which this loadable should be active.
43 61 *
44 62 * @return array<string> The conditionals.
45 63 */
46 64 public static function get_conditionals() {
47 - return [ Abilities_API_Conditional::class ];
65 + return [
66 + Abilities_API_Conditional::class,
67 + Should_Index_Indexables_Conditional::class,
68 + ];
48 69 }
49 70
50 71 /**
51 72 * Constructor.
@@ -52,17 +73,23 @@
52 73 *
53 74 * @param Score_Retriever $score_retriever The score retriever.
54 75 * @param Capability_Helper $capability_helper The capability helper.
55 76 * @param Enabled_Analysis_Features_Repository $enabled_analysis_features_repository The enabled analysis features repository.
77 + * @param Post_SEO_Data_Collector $post_seo_data_collector The post SEO data collector.
78 + * @param Post_SEO_Data_Updater $post_seo_data_updater The post SEO data updater.
56 79 */
57 80 public function __construct(
58 81 Score_Retriever $score_retriever,
59 82 Capability_Helper $capability_helper,
60 - Enabled_Analysis_Features_Repository $enabled_analysis_features_repository
83 + Enabled_Analysis_Features_Repository $enabled_analysis_features_repository,
84 + Post_SEO_Data_Collector $post_seo_data_collector,
85 + Post_SEO_Data_Updater $post_seo_data_updater
61 86 ) {
62 87 $this->score_retriever = $score_retriever;
63 88 $this->capability_helper = $capability_helper;
64 89 $this->enabled_analysis_features_repository = $enabled_analysis_features_repository;
90 + $this->post_seo_data_collector = $post_seo_data_collector;
91 + $this->post_seo_data_updater = $post_seo_data_updater;
65 92 }
66 93
67 94 /**
68 95 * Registers hooks with WordPress.
@@ -100,14 +127,44 @@
100 127 }
101 128 }
102 129
103 130 /**
131 + * Checks whether the current user can manage Yoast SEO.
132 + *
133 + * Gates the score abilities behind the Yoast SEO management capability.
134 + *
135 + * @return bool Whether the current user can manage Yoast SEO.
136 + */
137 + public function can_manage_seo(): bool {
138 + return $this->capability_helper->current_user_can( 'wpseo_manage_options' );
139 + }
140 +
141 + /**
142 + * Checks whether the current user can edit advanced SEO metadata.
143 + *
144 + * Gates the post SEO data abilities on the same capability that gates the advanced
145 + * and schema fields in the editors, so the abilities can never grant a field the
146 + * editor UI denies. The capability helper also passes wpseo_manage_options holders.
147 + * Per-post edit access is enforced on top, in the execute callbacks.
148 + *
149 + * @return bool Whether the current user can edit advanced SEO metadata.
150 + */
151 + public function can_edit_advanced_metadata(): bool {
152 + return $this->capability_helper->current_user_can( 'wpseo_edit_advanced_metadata' );
153 + }
154 +
155 + /**
104 156 * Checks whether the current user can read scores.
105 157 *
158 + * @deprecated 28.2
159 + * @codeCoverageIgnore Because of deprecation.
160 + *
106 161 * @return bool Whether the current user can read scores.
107 162 */
108 163 public function can_read_scores(): bool {
109 - return $this->capability_helper->current_user_can( 'wpseo_manage_options' );
164 + \_deprecated_function( __METHOD__, 'Yoast SEO 28.2', 'Use can_manage_seo() instead.' );
165 +
166 + return $this->can_manage_seo();
110 167 }
111 168
112 169 /**
113 170 * Registers the SEO scores ability.
@@ -171,8 +228,61 @@
171 228 ),
172 229 );
173 230 }
174 231
232 + /**
233 + * Registers the get post SEO data ability.
234 + *
235 + * @return void
236 + */
237 + private function register_get_post_seo_data_ability(): void {
238 + \wp_register_ability(
239 + Ability_Categories_Integration::CATEGORY_SLUG . '/get-post-seo-data',
240 + $this->get_shared_ability_args(
241 + [
242 + 'label' => \__( 'Get Post SEO Data', 'wordpress-seo' ),
243 + 'description' => \__( 'Get the SEO data for a post. Identify the post by post_id, by permalink (URL), or by title keywords; the title may be a comma-separated list and returns the SEO data for every post matching any of the values, paginated most recently modified first (use the page parameter to reach older matches). At least one identifier is required. Only posts the current user is allowed to edit are returned.', 'wordpress-seo' ),
244 + 'input_schema' => $this->get_post_identifier_input_schema(),
245 + 'output_schema' => $this->wrap_in_array_schema( $this->get_post_seo_data_output_schema() ),
246 + 'permission_callback' => [ $this, 'can_edit_advanced_metadata' ],
247 + 'execute_callback' => [ $this->post_seo_data_collector, 'get_post_seo_data' ],
248 + ],
249 + ),
250 + );
251 + }
252 +
253 + /**
254 + * Registers the update post SEO data ability.
255 + *
256 + * @return void
257 + */
258 + private function register_update_post_seo_data_ability(): void {
259 + \wp_register_ability(
260 + Ability_Categories_Integration::CATEGORY_SLUG . '/update-post-seo-data',
261 + $this->get_shared_ability_args(
262 + [
263 + 'label' => \__( 'Update Post SEO Data', 'wordpress-seo' ),
264 + 'description' => \__( 'Update the SEO data for a single post. Identify the post by post_id or by permalink (URL). Only the fields you provide are changed; a provided empty value clears that field. Only posts the current user is allowed to edit can be updated.', 'wordpress-seo' ),
265 + 'input_schema' => $this->get_update_post_seo_data_input_schema(),
266 + 'output_schema' => $this->get_post_seo_data_output_schema(),
267 + 'permission_callback' => [ $this, 'can_edit_advanced_metadata' ],
268 + 'execute_callback' => [ $this->post_seo_data_updater, 'update_post_seo_data' ],
269 + 'meta' => [
270 + 'show_in_rest' => true,
271 + 'annotations' => [
272 + 'readonly' => false,
273 + 'destructive' => false,
274 + 'idempotent' => true,
275 + ],
276 + 'mcp' => [
277 + 'public' => true,
278 + ],
279 + ],
280 + ],
281 + ),
282 + );
283 + }
284 +
175 285 // phpcs:disable SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint.DisallowedMixedTypeHint -- Too complicated of a param declaration for this case.
176 286
177 287 /**
178 288 * Returns the shared ability arguments merged with ability-specific arguments.
@@ -186,10 +296,11 @@
186 296 return \array_merge(
187 297 [
188 298 'category' => Ability_Categories_Integration::CATEGORY_SLUG,
189 299 'input_schema' => [
190 - 'type' => 'object',
191 - 'properties' => [
300 + 'type' => 'object',
301 + 'additionalProperties' => false,
302 + 'properties' => [
192 303 'number_of_posts' => [
193 304 'type' => 'integer',
194 305 'description' => \__( 'The number of recently modified posts to retrieve scores for. Defaults to 10.', 'wordpress-seo' ),
195 306 'minimum' => 1,
@@ -197,9 +308,9 @@
197 308 'default' => 10,
198 309 ],
199 310 ],
200 311 ],
201 - 'permission_callback' => [ $this, 'can_read_scores' ],
312 + 'permission_callback' => [ $this, 'can_manage_seo' ],
202 313 'meta' => [
203 314 'show_in_rest' => true,
204 315 'annotations' => [
205 316 'readonly' => true,
@@ -253,5 +364,204 @@
253 364 ],
254 365 ],
255 366 ];
256 367 }
368 +
369 + // phpcs:disable SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint.DisallowedMixedTypeHint -- The JSON schema arrays are heterogeneous by nature.
370 +
371 + /**
372 + * Returns the input schema for identifying a post (read path).
373 + *
374 + * @return array<string, mixed> The input schema.
375 + */
376 + private function get_post_identifier_input_schema(): array {
377 + return [
378 + 'type' => 'object',
379 + 'additionalProperties' => false,
380 + 'properties' => [
381 + 'post_id' => [
382 + 'type' => 'integer',
383 + 'description' => \__( 'The ID of the post to retrieve.', 'wordpress-seo' ),
384 + 'minimum' => 1,
385 + ],
386 + 'permalink' => [
387 + 'type' => 'string',
388 + 'description' => \__( 'The permalink (URL) of the post to retrieve.', 'wordpress-seo' ),
389 + ],
390 + 'title' => [
391 + 'type' => 'string',
392 + 'description' => \__( 'Keywords to search for in post titles. Provide a comma-separated list to search for several titles at once; each value is matched as a whole phrase against the post title, and a post matching any value is returned. At most 10 phrases are used per request; any beyond the first 10 are ignored. Results are paginated to 10 entities per page; see the page parameter.', 'wordpress-seo' ),
393 + ],
394 + 'page' => [
395 + 'type' => 'integer',
396 + 'description' => \__( 'The page of title-search results to return, 1-based and defaulting to 1. Matches are ordered most recently modified first, so request a later page to reach older matches. An empty result means there are no further pages. Only applies to a title search.', 'wordpress-seo' ),
397 + 'minimum' => 1,
398 + 'default' => 1,
399 + ],
400 + ],
401 + ];
402 + }
403 +
404 + /**
405 + * Returns the input schema for updating a post's SEO data (write path).
406 + *
407 + * @return array<string, mixed> The input schema.
408 + */
409 + private function get_update_post_seo_data_input_schema(): array {
410 + $nullable_string = [ 'type' => [ 'string', 'null' ] ];
411 +
412 + return [
413 + 'type' => 'object',
414 + 'additionalProperties' => false,
415 + 'properties' => [
416 + 'post_id' => [
417 + 'type' => 'integer',
418 + 'description' => \__( 'The ID of the post to update.', 'wordpress-seo' ),
419 + 'minimum' => 1,
420 + ],
421 + 'permalink' => [
422 + 'type' => 'string',
423 + 'description' => \__( 'The permalink (URL) of the post to update.', 'wordpress-seo' ),
424 + ],
425 + 'seo_title' => $nullable_string,
426 + 'meta_description' => $nullable_string,
427 + 'focus_keyphrase' => \array_merge( $nullable_string, [ 'maxLength' => 191 ] ),
428 + 'canonical' => $nullable_string,
429 + 'is_cornerstone' => [ 'type' => 'boolean' ],
430 + 'noindex' => [
431 + 'type' => [ 'boolean', 'null' ],
432 + 'description' => \__( 'Whether search engines should be told not to index this post. true sets noindex (the post is excluded from search results); false forces the post to be indexed; null clears the setting and falls back to the post-type default.', 'wordpress-seo' ),
433 + ],
434 + 'nofollow' => [ 'type' => 'boolean' ],
435 + 'noimageindex' => [ 'type' => 'boolean' ],
436 + 'noarchive' => [ 'type' => 'boolean' ],
437 + 'nosnippet' => [ 'type' => 'boolean' ],
438 + 'open_graph_title' => $nullable_string,
439 + 'open_graph_description' => $nullable_string,
440 + 'twitter_title' => $nullable_string,
441 + 'twitter_description' => $nullable_string,
442 + 'schema_page_type' => $this->nullable_enum_schema(
443 + \array_keys( Schema_Types::PAGE_TYPES ),
444 + \__( 'The Schema.org page type for the post. Must be one of the supported page types. Use null to clear it and fall back to the default.', 'wordpress-seo' ),
445 + ),
446 + 'schema_article_type' => $this->nullable_enum_schema(
447 + $this->get_schema_article_types(),
448 + \__( 'The Schema.org article type for the post. Must be one of the supported article types. Use null to clear it and fall back to the default.', 'wordpress-seo' ),
449 + ),
450 + ],
451 + ];
452 + }
453 +
454 + /**
455 + * Returns the allowed Schema.org article type values.
456 + *
457 + * Mirrors the validation in WPSEO_Option_Titles so the ability accepts exactly the
458 + * article types the editor does, including any registered through the filter.
459 + *
460 + * @return array<int, string> The allowed article type values.
461 + */
462 + private function get_schema_article_types(): array {
463 + /**
464 + * Filter: 'wpseo_schema_article_types' - Allow developers to filter the available article types.
465 + *
466 + * Make sure when you filter this to also filter `wpseo_schema_article_types_labels`.
467 + *
468 + * @param array $schema_article_types The available schema article types.
469 + */
470 + return \array_keys( \apply_filters( 'wpseo_schema_article_types', Schema_Types::ARTICLE_TYPES ) );
471 + }
472 +
473 + /**
474 + * Returns a nullable-string input schema constrained to a fixed set of allowed values.
475 + *
476 + * Null and the empty string are always allowed on top of the enum so the field can be
477 + * cleared, matching the patch-clear semantics of the other write fields.
478 + *
479 + * @param array<int, string> $allowed_values The allowed string values.
480 + * @param string $description The field description.
481 + *
482 + * @return array<string, mixed> The input schema fragment.
483 + */
484 + private function nullable_enum_schema( array $allowed_values, string $description ): array {
485 + return [
486 + 'type' => [ 'string', 'null' ],
487 + 'description' => $description,
488 + 'enum' => \array_merge( $allowed_values, [ '', null ] ),
489 + ];
490 + }
491 +
492 + /**
493 + * Returns the output schema describing a post's SEO data.
494 + *
495 + * @return array<string, mixed> The output schema.
496 + */
497 + private function get_post_seo_data_output_schema(): array {
498 + $nullable_string = [
499 + 'type' => [ 'string', 'null' ],
500 + ];
501 + $score = static function ( $analysis ) {
502 + return [
503 + 'type' => 'string',
504 + 'enum' => [ 'na', 'bad', 'ok', 'good' ],
505 + 'description' => \sprintf(
506 + /* translators: %s expands to the name of the analysis, e.g. "SEO analysis". */
507 + \__( 'The result of the %s that ran on the post when it was last saved.', 'wordpress-seo' ),
508 + $analysis,
509 + ),
510 + ];
511 + };
512 +
513 + // The rendered companion of a field carries the value as actually output on the front end: the global default template applied where no custom value is set, with replacement variables expanded.
514 + $rendered = static function ( $field ) {
515 + return [
516 + 'type' => [ 'string', 'null' ],
517 + 'description' => \sprintf(
518 + /* translators: %s expands to the name of the SEO field, e.g. "SEO title". */
519 + \__( 'The %s as output on the front end: the global default template applied when no custom value is set, with replacement variables expanded. Null when nothing is output.', 'wordpress-seo' ),
520 + $field,
521 + ),
522 + ];
523 + };
524 +
525 + return [
526 + 'type' => 'object',
527 + 'properties' => [
528 + 'post_id' => [ 'type' => 'integer' ],
529 + 'post_title' => $nullable_string,
530 + 'permalink' => $nullable_string,
531 + 'post_type' => [ 'type' => 'string' ],
532 + 'post_status' => $nullable_string,
533 + 'seo_title' => $nullable_string,
534 + 'seo_title_rendered' => $rendered( \__( 'SEO title', 'wordpress-seo' ) ),
535 + 'meta_description' => $nullable_string,
536 + 'meta_description_rendered' => $rendered( \__( 'meta description', 'wordpress-seo' ) ),
537 + 'focus_keyphrase' => $nullable_string,
538 + 'canonical' => $nullable_string,
539 + 'canonical_rendered' => $rendered( \__( 'canonical URL', 'wordpress-seo' ) ),
540 + 'is_cornerstone' => [ 'type' => 'boolean' ],
541 + 'noindex' => [
542 + 'type' => [ 'boolean', 'null' ],
543 + 'description' => \__( 'Whether search engines are told not to index this post. true means noindex (the post is excluded from search results); false means the post is forced to be indexed; null means no setting is stored and the post-type default applies.', 'wordpress-seo' ),
544 + ],
545 + 'nofollow' => [ 'type' => 'boolean' ],
546 + 'noimageindex' => [ 'type' => 'boolean' ],
547 + 'noarchive' => [ 'type' => 'boolean' ],
548 + 'nosnippet' => [ 'type' => 'boolean' ],
549 + 'open_graph_title' => $nullable_string,
550 + 'open_graph_title_rendered' => $rendered( \__( 'Open Graph title', 'wordpress-seo' ) ),
551 + 'open_graph_description' => $nullable_string,
552 + 'open_graph_description_rendered' => $rendered( \__( 'Open Graph description', 'wordpress-seo' ) ),
553 + 'twitter_title' => $nullable_string,
554 + 'twitter_title_rendered' => $rendered( \__( 'Twitter title', 'wordpress-seo' ) ),
555 + 'twitter_description' => $nullable_string,
556 + 'twitter_description_rendered' => $rendered( \__( 'Twitter description', 'wordpress-seo' ) ),
557 + 'schema_page_type' => $nullable_string,
558 + 'schema_article_type' => $nullable_string,
559 + 'seo_score' => $score( \__( 'SEO analysis', 'wordpress-seo' ) ),
560 + 'readability_score' => $score( \__( 'readability analysis', 'wordpress-seo' ) ),
561 + 'inclusive_language_score' => $score( \__( 'inclusive language analysis', 'wordpress-seo' ) ),
562 + ],
563 + ];
564 + }
565 +
566 + // phpcs:enable SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint.DisallowedMixedTypeHint
257 567 }