Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/wp-includes/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Comment on lines +288 to +289
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it, though? During REST API responses, the PHP display_errors config option is set to 0:

/*
* The 'REST_REQUEST' check here is optimistic as the constant is most
* likely not set at this point even if it is in fact a REST request.
*/
if ( defined( 'XMLRPC_REQUEST' ) || defined( 'REST_REQUEST' ) || defined( 'MS_FILES_REQUEST' )
|| ( defined( 'WP_INSTALLING' ) && WP_INSTALLING )
|| wp_doing_ajax() || wp_is_json_request()
) {
ini_set( 'display_errors', 0 );
}

Therefore, why not just remove the doing_it_wrong_trigger_error filter altogether?

}

/**
* Registers default REST API routes.
*
Expand Down
Loading