diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index d33a52ef51d93..9c63a466819da 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -245,7 +245,7 @@ function rest_api_default_filters() { add_action( 'deprecated_argument_run', 'rest_handle_deprecated_argument', 10, 3 ); add_filter( 'deprecated_argument_trigger_error', '__return_false' ); add_action( 'doing_it_wrong_run', 'rest_handle_doing_it_wrong', 10, 3 ); - add_filter( 'doing_it_wrong_trigger_error', '__return_false' ); + add_filter( 'doing_it_wrong_trigger_error', 'rest_handle_doing_it_wrong_trigger_error', 10, 4 ); } // Default serving. @@ -257,6 +257,38 @@ function rest_api_default_filters() { add_filter( 'rest_index', 'rest_add_application_passwords_to_index' ); } +/** + * Handles doing_it_wrong errors for REST API requests. + * + * Logs doing_it_wrong notices to the debug log during REST API requests + * while preventing trigger_error() from interfering with JSON responses. + * + * @since 6.x.0 + * + * @param bool $trigger Whether to trigger an error. + * @param string $function_name The function that was called incorrectly. + * @param string $message A message explaining what was called incorrectly. + * @param string $version The version of WordPress where the message was added. + * @return bool Always returns false to prevent trigger_error(). + */ +function rest_handle_doing_it_wrong_trigger_error( $trigger, $function_name, $message, $version ) { + // Only log when WordPress debugging and debug logging are enabled. + if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { + $log_message = sprintf( + 'REST API: %1$s was called incorrectly. %2$s', + $function_name, + $message + ); + if ( ! empty( $version ) ) { + $log_message .= sprintf( ' (This message was added in version %s.)', $version ); + } + error_log( $log_message ); + } + + // Prevent PHP's trigger_error() to avoid corrupting JSON responses. + return false; +} + /** * Registers default REST API routes. *