1414 */
1515class 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