Make WordPress Core

Opened 4 years ago

Last modified 8 days ago

#54327 new feature request

Support wp_die from array print

Reported by: myousefi08's profile myousefi08 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.8.1
Component: General Keywords: has-patch changes-requested
Focuses: Cc:

Description

I think wp_die() function is very used in wordpress plugin and theme developement and that is better to support array when pass an array to this and prevent from array to string notice.

in wp-includes/functions.php
line ~3800

before:

echo $message;

after:

if(is_array($message)) {
    print_r($message);
}else {
    echo $message;
}

tanx.

Attachments (2)

54327.diff (524 bytes) - added by sabbirshouvo 4 years ago.
Sounds like a valid proposal to me. Adding a patch.
54327.2.diff (1.5 KB) - added by sabbirshouvo 4 years ago.
update: Added support for all available error handlers

Download all attachments as: .zip

Change History (8)

@sabbirshouvo
4 years ago

Sounds like a valid proposal to me. Adding a patch.

#1 @sabbirshouvo
4 years ago

  • Keywords has-patch needs-testing added

#2 @SergeyBiryukov
4 years ago

Hi there, welcome to WordPress Trac! Thanks for the ticket.

I think the patch might have to be a bit more complex than that, to support all the current wp_die() handlers in core:

  • _default_wp_die_handler()
  • _ajax_wp_die_handler()
  • _json_wp_die_handler()
  • _jsonp_wp_die_handler()
  • _xmlrpc_wp_die_handler()
  • _xml_wp_die_handler()
  • _scalar_wp_die_handler()

I also think we'd want to avoid using print_r() as its output is a bit too technical, and instead update _wp_die_process_input() to use the first item of the array as the main message, and add the rest to $args['additional_errors'], like we already do if $message is a WP_Error object, see lines 4076 and 4088.

#3 @myousefi08
4 years ago

tnx @SergeyBiryukov

Of course, I mean using this method for the developers in plugin or theme development. Because of this, the technicality of the output is not a problem. Rather, my goal in printing arrays or objects is to see their content very quickly. (first element does not help)

Otherwise, I know that from the user's point of view, and for items that are used in printing errors, such as wp_error object, it may create restrictions.

tanx again.

Last edited 4 years ago by myousefi08 (previous) (diff)

#4 @sabbirshouvo
4 years ago

@SergeyBiryukov I took a look into _wp_die_process_input(). I think there is nothing to modify to achieve the proposed result. that mentioned method nicely handling the $message returning as an array. In order to display the user input array, we must print it in some way. I think print_r() is a decent way to print the array. _wp_die_process_input() already checking if it's an error object coming from WordPress itself or not and the $message is always a string unless the user inputs an array as $message.

I agree with you regarding modifying all error handlers.

  • _default_wp_die_handler() : Current patch should work fine. But output can be nicer with <pre></pre>
  • _ajax_wp_die_handler() : Same as _default_wp_die_handler but without <pre></pre>
  • _json_wp_die_handler() : Current code should work just fine
  • _jsonp_wp_die_handler() : Current code should work just fine
  • _xmlrpc_wp_die_handler() : As error output is always string for IXR_Error we can json_encode the $message if is an array.
  • _xml_wp_die_handler() : same solution as _xmlrpc_wp_die_handler
  • _scalar_wp_die_handler() : I couldn't find any use of this handler. I think it's possible to skip this or remove this.

I'll be adding a patch with my update across all handlers.

Last edited 4 years ago by sabbirshouvo (previous) (diff)

@sabbirshouvo
4 years ago

update: Added support for all available error handlers

#5 @devasheeshkaul
8 days ago

Test Report

Description

This report validates whether the indicated patch works as expected.

Environment

  • WordPress: 6.9-alpha-60093-src
  • PHP: 8.2.28
  • Server: nginx/1.29.0
  • Database: mysqli (Server: 8.4.5 / Client: mysqlnd 8.2.28)
  • Browser: Chrome 137.0.0.0
  • OS: macOS
  • Theme: Twenty Twenty-Five 1.2
  • MU Plugins: None activated
  • Plugins:
    • Test Reports 1.2.0

Actual Results

  1. wp_die() with an array now outputs structured results for the following handlers:
    • _default_wp_die_handler() – array is printed as expected
    • _json_wp_die_handler()/_jsonp_wp_die_handler() – array is returned as JSON
    • _xml_wp_die_handler()/_xmlrpc_wp_die_handler() – array is serialized properly in XML
  1. _ajax_wp_die_handler() still outputs only 0 when passed an array.

Additional Notes

  • This occurs before any opportunity for the patch to render the array using print_r().
  • To improve _ajax_wp_die_handler() while maintaining backward compatibility, one option is to add an additional check for when $message is an array or object, and output it as formatted text or JSON:
    if ( is_array( $message ) || is_object( $message ) ) {
        $message = print_r( $message, true ); // Or wp_json_encode( $message )
    } elseif ( ! is_scalar( $message ) ) {
        $message = '0';
    } else {
        $message = (string) $message;
    }
    

Supplemental Artifacts

Below are the code examples used to verify the behavior of wp_die() when passed an array, across various die handlers. You can add these snippets to your plugin's main file or the active theme’s functions.php to test locally.

  1. _default_wp_die_handler()
add_action( 'init', function () {
	if ( isset( $_GET['test-default-die'] ) ) {
		wp_die( array( 'error' => 'Something went wrong', 'code' => 500 ) );
	}
} );

Visit /wp-admin/?test-default-die to check output

  1. _json_wp_die_handler() (same internal logic for _jsonp_wp_die_handler())
add_action( 'rest_api_init', function () {
	register_rest_route( 'test/v1', '/wp-die-json', [
		'methods'  => 'GET',
		'callback' => 'test_wp_die_json_handler',
		'permission_callback' => '__return_true',
	] );
} );

function test_wp_die_json_handler() {
	wp_die( array( 'json_error' => 'Something failed', 'code' => 500 ) );
}

Visit /wp-json/test/v1/wp-die-json to check output

  1. _xml_wp_die_handler() (same patch logic used for _xmlrpc_wp_die_handler()). This test manually forces the use of _xml_wp_die_handler() via the wp_die_handler filter.
add_action( 'init', function () {
	if ( isset( $_GET['test-xml-die'] ) ) {
		add_filter( 'wp_die_handler', function () {
			return '_xml_wp_die_handler';
		} );
		wp_die( array( 'xml' => 'Something broke', 'code' => 999 ) );
	}
} );

Visit /?test-xml-die to check output

  1. _ajax_wp_die_handler()
add_action( 'wp_ajax_test_ajax_die', function () {
    wp_die( array( 'ajax' => 'AJAX failed', 'code' => 403 ) );
} );

Visit /wp-admin/admin-ajax.php?action=test_ajax_die to check output

#6 @SirLouen
8 days ago

  • Keywords changes-requested added; needs-testing removed
Note: See TracTickets for help on using tickets.