Skip to content

Commit 8d65ca5

Browse files
committed
REST API: Log doing_it_wrong notices to debug.log
Fixes #64260. The doing_it_wrong_trigger_error filter was preventing all doing_it_wrong notices from being logged during REST requests. This change ensures notices are written to debug.log while still preventing trigger_error() from interfering with REST responses.
1 parent 7e27cc2 commit 8d65ca5

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

src/wp-includes/rest-api.php

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -248,35 +248,6 @@ function rest_api_default_filters() {
248248
add_filter( 'doing_it_wrong_trigger_error', 'rest_handle_doing_it_wrong_trigger_error', 10, 4 );
249249
}
250250

251-
/**
252-
* Handles doing_it_wrong errors for REST API requests.
253-
*
254-
* Logs doing_it_wrong notices to the debug log during REST API requests
255-
* while preventing trigger_error() from interfering with JSON responses.
256-
*
257-
* @since 7.0.0
258-
*
259-
* @param bool $trigger Whether to trigger an error.
260-
* @param string $function_name The function that was called incorrectly.
261-
* @param string $message A message explaining what was called incorrectly.
262-
* @param string $version The version of WordPress where the message was added.
263-
* @return bool Always returns false to prevent trigger_error().
264-
*/
265-
function rest_handle_doing_it_wrong_trigger_error( $trigger, $function_name, $message, $version ) {
266-
if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
267-
$log_message = sprintf(
268-
'REST API - Doing it wrong: %s - %s',
269-
$function_name,
270-
$message
271-
);
272-
if ( $version ) {
273-
$log_message .= sprintf( ' (This message was added in version %s.)', $version );
274-
}
275-
error_log( $log_message );
276-
}
277-
return false;
278-
}
279-
280251
// Default serving.
281252
add_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' );
282253
add_filter( 'rest_post_dispatch', 'rest_send_allow_header', 10, 3 );
@@ -286,6 +257,38 @@ function rest_handle_doing_it_wrong_trigger_error( $trigger, $function_name, $me
286257
add_filter( 'rest_index', 'rest_add_application_passwords_to_index' );
287258
}
288259

260+
/**
261+
* Handles doing_it_wrong errors for REST API requests.
262+
*
263+
* Logs doing_it_wrong notices to the debug log during REST API requests
264+
* while preventing trigger_error() from interfering with JSON responses.
265+
*
266+
* @since 6.x.0
267+
*
268+
* @param bool $trigger Whether to trigger an error.
269+
* @param string $function_name The function that was called incorrectly.
270+
* @param string $message A message explaining what was called incorrectly.
271+
* @param string $version The version of WordPress where the message was added.
272+
* @return bool Always returns false to prevent trigger_error().
273+
*/
274+
function rest_handle_doing_it_wrong_trigger_error( $trigger, $function_name, $message, $version ) {
275+
// Only log when WordPress debugging and debug logging are enabled.
276+
if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
277+
$log_message = sprintf(
278+
'REST API: %1$s was called incorrectly. %2$s',
279+
$function_name,
280+
$message
281+
);
282+
if ( ! empty( $version ) ) {
283+
$log_message .= sprintf( ' (This message was added in version %s.)', $version );
284+
}
285+
error_log( $log_message );
286+
}
287+
288+
// Prevent PHP's trigger_error() to avoid corrupting JSON responses.
289+
return false;
290+
}
291+
289292
/**
290293
* Registers default REST API routes.
291294
*

0 commit comments

Comments
 (0)