Make WordPress Core

Opened 9 years ago

Closed 8 years ago

#35666 closed enhancement (fixed)

Add support for HTTP response codes to wp_send_json_error(), wp_send_json_success(), and wp_send_json

Reported by: johnbillion's profile johnbillion Owned by: johnbillion's profile johnbillion
Milestone: 4.7 Priority: normal
Severity: normal Version: 3.5
Component: Formatting Keywords: has-patch commit
Focuses: Cc:

Description

wp_send_json_error() is the JSON-encoded equivalent of wp_die(). It should therefore support the sending of an HTTP response code in a similar way so a more appropriate response code can be set for error conditions. Currently, the response code needs to be set manually, otherwise it defaults to 200.

Attachments (5)

35666.diff (2.2 KB) - added by stephenharris 9 years ago.
Adds support for a 'status_code' parameter to wp_send_json(_error|_success)
35666-2.diff (2.2 KB) - added by stephenharris 9 years ago.
Alternative patch for allowing a header status to be set
35666.3.diff (1.9 KB) - added by westonruter 8 years ago.
35666.4.diff (456 bytes) - added by stevenkword 8 years ago.
Refresh after Changeset 38576
35666.2.diff (739 bytes) - added by johnbillion 8 years ago.

Download all attachments as: .zip

Change History (30)

#1 @ocean90
9 years ago

#36261 was marked as a duplicate.

@stephenharris
9 years ago

Adds support for a 'status_code' parameter to wp_send_json(_error|_success)

#2 @stephenharris
9 years ago

  • Keywords has-patch 2nd-opinion added; needs-patch removed

@stephenharris
9 years ago

Alternative patch for allowing a header status to be set

#3 @johnbillion
8 years ago

  • Keywords 2nd-opinion removed
  • Milestone changed from Awaiting Review to 4.7

#4 @johnbillion
8 years ago

  • Owner set to johnbillion
  • Resolution set to fixed
  • Status changed from new to closed

In 38422:

Formatting: Add a parameter to wp_send_json_error(), wp_send_json_success(), and wp_send_json() for specifying the HTTP response code.

Defaults to 200 in all cases, but can be used, for example, to return a 403 when using wp_send_json_error().

Fixes #35666
Props stephenharris

#5 @westonruter
8 years ago

In 38478:

Customize: Use new $status_code parameter for wp_send_json_error() instead of calling status_header() separately.

Props johnbillion.
See #35666.
Fixes #37897.

@westonruter
8 years ago

#6 @westonruter
8 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

I've found a defect with the changes here. Namely, if status_header() was being called previously to wp_send_json(), then the default $status_header of 200 would clobber the status_header() that was first set. In other words, this breaks backwards compatibility.

On a related note, I also found that _ajax_wp_die_handler was clobbering the status_header() call if output buffering is on (since headers_sent() would be false). See 35666.3.diff.

#7 @johnbillion
8 years ago

  • Keywords needs-testing added

Good catch.

#8 @johnbillion
8 years ago

In 38576:

Formatting: Don't send an HTTP status code in wp_send_json() by default. This avoids clobbering an HTTP status code that may have been set prior to calling this function.

Props westonruter
See #35666

#9 @johnbillion
8 years ago

The output buffering issue needs a bit more testing I think.

#10 @johnbillion
8 years ago

  • Keywords needs-refresh added

This ticket was mentioned in Slack in #core by stevenkword. View the logs.


8 years ago

#12 @stevenkword
8 years ago

@johnbillion -- Do you think you'd like to take a stab at the refresh, or would you like some fresh eyes on this?

This ticket was mentioned in Slack in #core by chriscct7. View the logs.


8 years ago

#14 @stevenkword
8 years ago

  • Owner changed from johnbillion to stevenkword
  • Status changed from reopened to reviewing

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


8 years ago

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


8 years ago

This ticket was mentioned in Slack in #core by desrosj. View the logs.


8 years ago

@stevenkword
8 years ago

Refresh after Changeset 38576

#18 @stevenkword
8 years ago

  • Keywords needs-refresh removed

Patch 35666.4.diff updated after 38576, but still needs testing on the output buffering.

Last edited 8 years ago by stevenkword (previous) (diff)

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


8 years ago

@johnbillion
8 years ago

#20 @johnbillion
8 years ago

35666.2.diff tackles this by always telling wp_die() not to set an HTTP status code header.

With output buffering on, this means a status set by wp_send_json() doesn't get clobbered. With output buffering off, the functionality doesn't change because headers_sent() in _ajax_wp_die_handler() is always true (due to the JSON echoed in wp_send_json()) so the status header doesn't get set anyway.

@westonruter Wanna give this a whirl?

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


8 years ago

#22 @westonruter
8 years ago

  • Keywords commit added; needs-testing removed

@johnbillion works for me!

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


8 years ago

#24 @johnbillion
8 years ago

  • Owner changed from stevenkword to johnbillion
  • Status changed from reviewing to accepted

#25 @johnbillion
8 years ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 38956:

Formatting: Don't overwrite the status header for Ajax responses that use output buffering or otherwise set their headers early.

Fixes #35666

Note: See TracTickets for help on using tickets.