Skip to content

Commit 9dfe841

Browse files
committed
Remove ability to get all scores and refactor to simplify solution
1 parent 7d9f139 commit 9dfe841

File tree

5 files changed

+111
-456
lines changed

5 files changed

+111
-456
lines changed

src/abilities/application/score-retriever.php

Lines changed: 10 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
use WPSEO_Rank;
77
use Yoast\WP\SEO\Abilities\Domain\Score_Result;
8-
use Yoast\WP\SEO\Abilities\Infrastructure\Enabled_Analysis_Features_Checker;
98
use Yoast\WP\SEO\Repositories\Indexable_Repository;
109

1110
/**
@@ -28,25 +27,15 @@ class Score_Retriever {
2827
*/
2928
private $indexable_repository;
3029

31-
/**
32-
* The enabled analysis features checker.
33-
*
34-
* @var Enabled_Analysis_Features_Checker
35-
*/
36-
private $enabled_analysis_features_checker;
37-
3830
/**
3931
* Constructor.
4032
*
41-
* @param Indexable_Repository $indexable_repository The indexable repository.
42-
* @param Enabled_Analysis_Features_Checker $enabled_analysis_features_checker The enabled analysis features checker.
33+
* @param Indexable_Repository $indexable_repository The indexable repository.
4334
*/
4435
public function __construct(
45-
Indexable_Repository $indexable_repository,
46-
Enabled_Analysis_Features_Checker $enabled_analysis_features_checker
36+
Indexable_Repository $indexable_repository
4737
) {
48-
$this->indexable_repository = $indexable_repository;
49-
$this->enabled_analysis_features_checker = $enabled_analysis_features_checker;
38+
$this->indexable_repository = $indexable_repository;
5039
}
5140

5241
/**
@@ -88,49 +77,6 @@ public function get_inclusive_language_scores( array $input ): array {
8877
return \array_map( [ $this, 'build_inclusive_language_score_for_indexable' ], $indexables );
8978
}
9079

91-
/**
92-
* Retrieves all scores for the most recently modified posts.
93-
*
94-
* @param array<string, int> $input The input containing optional 'number_of_posts'.
95-
*
96-
* @return array<int, array<string, array<string, int|string|null>|string|null>> The combined score data for each post.
97-
*/
98-
public function get_all_scores( array $input ): array {
99-
$indexables = $this->get_recent_indexables( $this->get_number_of_posts( $input ) );
100-
$results = [];
101-
102-
foreach ( $indexables as $indexable ) {
103-
$title = $this->get_indexable_title( $indexable );
104-
105-
$seo_score = null;
106-
if ( $this->enabled_analysis_features_checker->is_keyword_analysis_enabled() ) {
107-
$seo_score = $this->build_seo_score_for_indexable( $indexable );
108-
unset( $seo_score['title'] );
109-
}
110-
111-
$readability_score = null;
112-
if ( $this->enabled_analysis_features_checker->is_content_analysis_enabled() ) {
113-
$readability_score = $this->build_readability_score_for_indexable( $indexable );
114-
unset( $readability_score['title'] );
115-
}
116-
117-
$inclusive_language_score = null;
118-
if ( $this->enabled_analysis_features_checker->is_inclusive_language_enabled() ) {
119-
$inclusive_language_score = $this->build_inclusive_language_score_for_indexable( $indexable );
120-
unset( $inclusive_language_score['title'] );
121-
}
122-
123-
$results[] = [
124-
'title' => $title,
125-
'seo' => $seo_score,
126-
'readability' => $readability_score,
127-
'inclusive_language' => $inclusive_language_score,
128-
];
129-
}
130-
131-
return $results;
132-
}
133-
13480
/**
13581
* Builds the SEO score result for a single indexable.
13682
*
@@ -142,29 +88,25 @@ private function build_seo_score_for_indexable( $indexable ): array {
14288
$title = $this->get_indexable_title( $indexable );
14389

14490
if ( $indexable->is_robots_noindex ) {
145-
$rank = new WPSEO_Rank( WPSEO_Rank::NO_INDEX );
146-
$result = ( new Score_Result(
91+
$rank = new WPSEO_Rank( WPSEO_Rank::NO_INDEX );
92+
return ( new Score_Result(
14793
$title,
14894
0,
14995
$rank->get_rank(),
15096
$rank->get_label(),
97+
$indexable->primary_focus_keyword,
15198
) )->to_array();
152-
153-
$result['focus_keyphrase'] = $indexable->primary_focus_keyword;
154-
return $result;
15599
}
156100

157-
$score = (int) $indexable->primary_focus_keyword_score;
158-
$rank = WPSEO_Rank::from_numeric_score( $score );
159-
$result = ( new Score_Result(
101+
$score = (int) $indexable->primary_focus_keyword_score;
102+
$rank = WPSEO_Rank::from_numeric_score( $score );
103+
return ( new Score_Result(
160104
$title,
161105
$score,
162106
$rank->get_rank(),
163107
$rank->get_label(),
108+
$indexable->primary_focus_keyword,
164109
) )->to_array();
165-
166-
$result['focus_keyphrase'] = $indexable->primary_focus_keyword;
167-
return $result;
168110
}
169111

170112
/**

src/abilities/domain/score-result.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,17 @@ public function get_focus_keyphrase(): ?string {
7575
* @return array<string, int|string> The serialized score result.
7676
*/
7777
public function to_array(): array {
78-
return [
78+
$result = [
7979
'title' => $this->title,
8080
'score' => $this->score,
8181
'rating' => $this->rating,
8282
'label' => $this->label,
8383
];
84+
85+
if ( $this->focus_keyphrase !== null ) {
86+
$result['focus_keyphrase'] = $this->focus_keyphrase;
87+
}
88+
89+
return $result;
8490
}
8591
}

src/abilities/user-interface/abilities-integration.php

Lines changed: 91 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@
1414
*/
1515
class Abilities_Integration implements Integration_Interface {
1616

17+
/**
18+
* Valid ratings for SEO scores.
19+
*
20+
* @var array<string>
21+
*/
22+
private const SEO_RATINGS = [ 'na', 'bad', 'ok', 'good', 'noindex' ];
23+
24+
/**
25+
* Valid ratings for content analysis scores (readability and inclusive language).
26+
*
27+
* @var array<string>
28+
*/
29+
private const CONTENT_ANALYSIS_RATINGS = [ 'na', 'bad', 'ok', 'good' ];
30+
1731
/**
1832
* The score retriever.
1933
*
@@ -92,109 +106,93 @@ public function register_categories() {
92106
* @return void
93107
*/
94108
public function register_abilities() {
95-
$seo_score_item_schema = $this->get_score_output_schema( [ 'na', 'bad', 'ok', 'good', 'noindex' ] );
96-
$seo_score_item_schema['properties']['focus_keyphrase'] = [
97-
'type' => [ 'string', 'null' ],
98-
'description' => \__( 'The focus keyphrase for the post, or null if not set.', 'wordpress-seo' ),
99-
];
100-
101-
$readability_score_item_schema = $this->get_score_output_schema( [ 'na', 'bad', 'ok', 'good' ] );
102-
$inclusive_language_score_item_schema = $this->get_score_output_schema( [ 'na', 'bad', 'ok', 'good' ] );
103-
104-
if ( $this->enabled_analysis_features_checker->is_keyword_analysis_enabled() ) {
105-
\wp_register_ability(
106-
'yoast-seo/get-seo-scores',
107-
$this->get_shared_ability_args(
108-
[
109-
'label' => \__( 'Get SEO Scores', 'wordpress-seo' ),
110-
'description' => \__( 'Get the SEO scores for the most recently modified posts.', 'wordpress-seo' ),
111-
'output_schema' => $this->wrap_in_array_schema( $seo_score_item_schema ),
112-
'execute_callback' => [ $this->score_retriever, 'get_seo_scores' ],
113-
],
114-
),
115-
);
116-
}
109+
$this->register_seo_scores_ability();
110+
$this->register_readability_scores_ability();
111+
$this->register_inclusive_language_scores_ability();
112+
}
117113

118-
if ( $this->enabled_analysis_features_checker->is_content_analysis_enabled() ) {
119-
\wp_register_ability(
120-
'yoast-seo/get-readability-scores',
121-
$this->get_shared_ability_args(
122-
[
123-
'label' => \__( 'Get Readability Scores', 'wordpress-seo' ),
124-
'description' => \__( 'Get the readability scores for the most recently modified posts.', 'wordpress-seo' ),
125-
'output_schema' => $this->wrap_in_array_schema( $readability_score_item_schema ),
126-
'execute_callback' => [ $this->score_retriever, 'get_readability_scores' ],
127-
],
128-
),
129-
);
130-
}
114+
/**
115+
* Checks whether the current user can read scores.
116+
*
117+
* @return bool Whether the current user can read scores.
118+
*/
119+
public function can_read_scores(): bool {
120+
return $this->capability_helper->current_user_can( 'wpseo_manage_options' );
121+
}
131122

132-
if ( $this->enabled_analysis_features_checker->is_inclusive_language_enabled() ) {
133-
\wp_register_ability(
134-
'yoast-seo/get-inclusive-language-scores',
135-
$this->get_shared_ability_args(
136-
[
137-
'label' => \__( 'Get Inclusive Language Scores', 'wordpress-seo' ),
138-
'description' => \__( 'Get the inclusive language scores for the most recently modified posts.', 'wordpress-seo' ),
139-
'output_schema' => $this->wrap_in_array_schema( $inclusive_language_score_item_schema ),
140-
'execute_callback' => [ $this->score_retriever, 'get_inclusive_language_scores' ],
141-
],
142-
),
143-
);
123+
/**
124+
* Registers the SEO scores ability if keyword analysis is enabled.
125+
*
126+
* @return void
127+
*/
128+
private function register_seo_scores_ability(): void {
129+
if ( ! $this->enabled_analysis_features_checker->is_keyword_analysis_enabled() ) {
130+
return;
144131
}
145132

146-
$nullable_schema = static function ( array $schema ): array {
147-
return [
148-
'oneOf' => [
149-
$schema,
150-
[ 'type' => 'null' ],
151-
],
152-
];
153-
};
154-
155-
// For sub-scores inside get-all-scores, use schemas without the title property.
156-
$seo_sub_schema = $this->get_score_sub_schema( [ 'na', 'bad', 'ok', 'good', 'noindex' ] );
157-
$seo_sub_schema['properties']['focus_keyphrase'] = [
133+
$output_schema = $this->get_score_output_schema( self::SEO_RATINGS );
134+
$output_schema['properties']['focus_keyphrase'] = [
158135
'type' => [ 'string', 'null' ],
159136
'description' => \__( 'The focus keyphrase for the post, or null if not set.', 'wordpress-seo' ),
160137
];
161138

162-
$readability_sub_schema = $this->get_score_sub_schema( [ 'na', 'bad', 'ok', 'good' ] );
163-
$inclusive_language_sub_schema = $this->get_score_sub_schema( [ 'na', 'bad', 'ok', 'good' ] );
164-
165-
$all_scores_item_schema = [
166-
'type' => 'object',
167-
'properties' => [
168-
'title' => [
169-
'type' => 'string',
170-
'description' => \__( 'The post title.', 'wordpress-seo' ),
139+
\wp_register_ability(
140+
'yoast-seo/get-seo-scores',
141+
$this->get_shared_ability_args(
142+
[
143+
'label' => \__( 'Get SEO Scores', 'wordpress-seo' ),
144+
'description' => \__( 'Get the SEO scores for the most recently modified posts.', 'wordpress-seo' ),
145+
'output_schema' => $this->wrap_in_array_schema( $output_schema ),
146+
'execute_callback' => [ $this->score_retriever, 'get_seo_scores' ],
171147
],
172-
'seo' => $nullable_schema( $seo_sub_schema ),
173-
'readability' => $nullable_schema( $readability_sub_schema ),
174-
'inclusive_language' => $nullable_schema( $inclusive_language_sub_schema ),
175-
],
176-
];
148+
),
149+
);
150+
}
151+
152+
/**
153+
* Registers the readability scores ability if content analysis is enabled.
154+
*
155+
* @return void
156+
*/
157+
private function register_readability_scores_ability(): void {
158+
if ( ! $this->enabled_analysis_features_checker->is_content_analysis_enabled() ) {
159+
return;
160+
}
177161

178162
\wp_register_ability(
179-
'yoast-seo/get-all-scores',
163+
'yoast-seo/get-readability-scores',
180164
$this->get_shared_ability_args(
181165
[
182-
'label' => \__( 'Get All Analysis Scores', 'wordpress-seo' ),
183-
'description' => \__( 'Get all analysis scores (SEO, readability, inclusive language) for the most recently modified posts.', 'wordpress-seo' ),
184-
'output_schema' => $this->wrap_in_array_schema( $all_scores_item_schema ),
185-
'execute_callback' => [ $this->score_retriever, 'get_all_scores' ],
166+
'label' => \__( 'Get Readability Scores', 'wordpress-seo' ),
167+
'description' => \__( 'Get the readability scores for the most recently modified posts.', 'wordpress-seo' ),
168+
'output_schema' => $this->wrap_in_array_schema( $this->get_score_output_schema( self::CONTENT_ANALYSIS_RATINGS ) ),
169+
'execute_callback' => [ $this->score_retriever, 'get_readability_scores' ],
186170
],
187171
),
188172
);
189173
}
190174

191175
/**
192-
* Checks whether the current user can read scores.
176+
* Registers the inclusive language scores ability if inclusive language analysis is enabled.
193177
*
194-
* @return bool Whether the current user can read scores.
178+
* @return void
195179
*/
196-
public function can_read_scores(): bool {
197-
return $this->capability_helper->current_user_can( 'wpseo_manage_options' );
180+
private function register_inclusive_language_scores_ability(): void {
181+
if ( ! $this->enabled_analysis_features_checker->is_inclusive_language_enabled() ) {
182+
return;
183+
}
184+
185+
\wp_register_ability(
186+
'yoast-seo/get-inclusive-language-scores',
187+
$this->get_shared_ability_args(
188+
[
189+
'label' => \__( 'Get Inclusive Language Scores', 'wordpress-seo' ),
190+
'description' => \__( 'Get the inclusive language scores for the most recently modified posts.', 'wordpress-seo' ),
191+
'output_schema' => $this->wrap_in_array_schema( $this->get_score_output_schema( self::CONTENT_ANALYSIS_RATINGS ) ),
192+
'execute_callback' => [ $this->score_retriever, 'get_inclusive_language_scores' ],
193+
],
194+
),
195+
);
198196
}
199197

200198
/**
@@ -208,7 +206,17 @@ private function get_shared_ability_args( array $ability_specific_args ): array
208206
return \array_merge(
209207
[
210208
'category' => 'yoast-seo',
211-
'input_schema' => $this->get_number_of_posts_schema(),
209+
'input_schema' => [
210+
'type' => 'object',
211+
'properties' => [
212+
'number_of_posts' => [
213+
'type' => 'integer',
214+
'description' => \__( 'The number of recently modified posts to retrieve scores for. Defaults to 10.', 'wordpress-seo' ),
215+
'minimum' => 1,
216+
'default' => 10,
217+
],
218+
],
219+
],
212220
'permission_callback' => [ $this, 'can_read_scores' ],
213221
'meta' => [
214222
'show_in_rest' => true,
@@ -290,32 +298,4 @@ private function get_score_output_schema( array $ratings ): array {
290298
],
291299
];
292300
}
293-
294-
/**
295-
* Returns the score sub-schema (without title) for use inside the all-scores ability.
296-
*
297-
* @param array<string> $ratings The valid rating slugs for this score type.
298-
*
299-
* @return array<string, array<string, string>> The score sub-schema.
300-
*/
301-
private function get_score_sub_schema( array $ratings ): array {
302-
return [
303-
'type' => 'object',
304-
'properties' => [
305-
'score' => [
306-
'type' => 'integer',
307-
'description' => \__( 'The numeric score from 0 to 100.', 'wordpress-seo' ),
308-
],
309-
'rating' => [
310-
'type' => 'string',
311-
'enum' => $ratings,
312-
'description' => \__( 'The rating slug.', 'wordpress-seo' ),
313-
],
314-
'label' => [
315-
'type' => 'string',
316-
'description' => \__( 'A human-readable label for the rating.', 'wordpress-seo' ),
317-
],
318-
],
319-
];
320-
}
321301
}

0 commit comments

Comments
 (0)