Make WordPress Core

Opened 10 years ago

Closed 10 years ago

#30139 closed defect (bug) (fixed)

Allow JSON_PRETTY_PRINT to be used without notice on PHP < 5.4

Reported by: markjaquith's profile markjaquith Owned by: markjaquith's profile markjaquith
Milestone: 4.1 Priority: low
Severity: minor Version: 4.1
Component: General Keywords: has-patch
Focuses: Cc:

Description

JSON_PRETTY_PRINT was introduced in PHP 5.4. If you attempt to use it in PHP < 5.4 as an option passed to json_encode() or wp_json_encode() , you'll get a notice about the undefined constant.

As it is just a human-readable thing and doesn't functionally affect the output of the JSON, we should just define the constant if it’s not defined. That way people can use it without doing a version check, and if PHP supports it, they get pretty output, otherwise, they silently get standard non-pretty output. I don't think we should add other constants, because they do affect the functionality of the output, and so silent failure would be a bad thing.

See also #29435 and #28786 (wp_json_encode() will support the option as well, on PHP versions that can accept the options argument).

Attachments (1)

30139.diff (465 bytes) - added by markjaquith 10 years ago.

Download all attachments as: .zip

Change History (5)

@markjaquith
10 years ago

#1 @markjaquith
10 years ago

  • Keywords has-patch added
  • Owner set to markjaquith
  • Status changed from new to accepted

I added it to compat.php instead of default-constants.php because it’s not our constant — it’s part of our PHP compat layer. But I don't feel strongly about that.

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


10 years ago

#3 @nacin
10 years ago

I agree with compat.php.

I'd add "to prevent a notice when used with wp_json_encode().", otherwise my next question would be why we need to have this.

#4 @markjaquith
10 years ago

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

In 30075:

Define JSON_PRETTY_PRINT so it can be used with wp_json_encode()

  • JSON_PRETTY_PRINT was introduced in PHP 5.4
  • Now you can use it with lower PHP versions, without a notice

fixes #30139
see #28786

Note: See TracTickets for help on using tickets.