@@ -493,24 +493,48 @@ public function validate_input( $input = null ) {
493493 sprintf (
494494 /* translators: %s ability name. */
495495 __ ( 'Ability "%s" does not define an input schema required to validate the provided input. ' ),
496- esc_html ( $ this ->name )
496+ $ this ->name
497497 )
498498 );
499499 }
500500
501501 $ valid_input = rest_validate_value_from_schema ( $ input , $ input_schema , 'input ' );
502502 if ( is_wp_error ( $ valid_input ) ) {
503- return new WP_Error (
503+ $ is_valid = new WP_Error (
504504 'ability_invalid_input ' ,
505505 sprintf (
506506 /* translators: %1$s ability name, %2$s error message. */
507507 __ ( 'Ability "%1$s" has invalid input. Reason: %2$s ' ),
508- esc_html ( $ this ->name ) ,
508+ $ this ->name ,
509509 $ valid_input ->get_error_message ()
510510 )
511511 );
512+ } else {
513+ $ is_valid = true ;
512514 }
513515
516+ /**
517+ * Filters the input validation result for an ability.
518+ *
519+ * Allows developers to add custom validation logic on top of the default
520+ * JSON Schema validation. If default validation already failed, the filter
521+ * receives the WP_Error object and can add additional error information or
522+ * override it. If default validation passed, the filter can add additional
523+ * validation checks and return a WP_Error if those checks fail.
524+ *
525+ * @since 7.1.0
526+ *
527+ * @param true|WP_Error $is_valid The validation result from default validation.
528+ * @param mixed $input The input data being validated.
529+ * @param string $ability_name The name of the ability.
530+ */
531+ $ validity = apply_filters ( 'wp_ability_validate_input ' , $ is_valid , $ input , $ this ->name );
532+ if ( false === $ validity ) {
533+ return new WP_Error ( 'ability_invalid_input ' , __ ( 'Invalid input. ' ) );
534+ }
535+ if ( is_wp_error ( $ validity ) && $ validity ->has_errors () ) {
536+ return $ validity ;
537+ }
514538 return true ;
515539 }
516540
@@ -653,22 +677,46 @@ protected function do_execute( $input = null ) {
653677 protected function validate_output ( $ output ) {
654678 $ output_schema = $ this ->get_output_schema ();
655679 if ( empty ( $ output_schema ) ) {
656- return true ;
680+ $ is_valid = true ;
681+ } else {
682+ $ valid_output = rest_validate_value_from_schema ( $ output , $ output_schema , 'output ' );
683+ if ( is_wp_error ( $ valid_output ) ) {
684+ $ is_valid = new WP_Error (
685+ 'ability_invalid_output ' ,
686+ sprintf (
687+ /* translators: %1$s ability name, %2$s error message. */
688+ __ ( 'Ability "%1$s" has invalid output. Reason: %2$s ' ),
689+ $ this ->name ,
690+ $ valid_output ->get_error_message ()
691+ )
692+ );
693+ } else {
694+ $ is_valid = true ;
695+ }
657696 }
658697
659- $ valid_output = rest_validate_value_from_schema ( $ output , $ output_schema , 'output ' );
660- if ( is_wp_error ( $ valid_output ) ) {
661- return new WP_Error (
662- 'ability_invalid_output ' ,
663- sprintf (
664- /* translators: %1$s ability name, %2$s error message. */
665- __ ( 'Ability "%1$s" has invalid output. Reason: %2$s ' ),
666- esc_html ( $ this ->name ),
667- $ valid_output ->get_error_message ()
668- )
669- );
698+ /**
699+ * Filters the output validation result for an ability.
700+ *
701+ * Allows developers to add custom validation logic on top of the default
702+ * JSON Schema validation. If default validation already failed, the filter
703+ * receives the WP_Error object and can add additional error information or
704+ * override it. If default validation passed, the filter can add additional
705+ * validation checks and return a WP_Error if those checks fail.
706+ *
707+ * @since 7.1.0
708+ *
709+ * @param true|WP_Error $is_valid The validation result from default validation.
710+ * @param mixed $output The output data being validated.
711+ * @param string $ability_name The name of the ability.
712+ */
713+ $ validity = apply_filters ( 'wp_ability_validate_output ' , $ is_valid , $ output , $ this ->name );
714+ if ( false === $ validity ) {
715+ return new WP_Error ( 'ability_invalid_output ' , __ ( 'Invalid output. ' ) );
716+ }
717+ if ( is_wp_error ( $ validity ) && $ validity ->has_errors () ) {
718+ return $ validity ;
670719 }
671-
672720 return true ;
673721 }
674722
0 commit comments