Make WordPress Core

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#40718 closed enhancement (worksforme)

MCE editor_settings first_init 'format' JSON names should be quoted

Reported by: mrgregwaugh's profile MrGregWaugh Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Editor Keywords: has-patch
Focuses: Cc:

Description

The default 'format' JSON value does not quote the key names in objects. This is required by JSON spec RFC 7159 (https://tools.ietf.org/html/rfc7159#page-6) and also required by PHP's json_decode (Example 3: http://php.net/manual/en/function.json-decode.php). This makes it difficult to parse and modify using built-in libraries.

Example:

{alignleft: [{selector: "p,h1,etc", styles: {textAlign:"left"}}]}

Should be:

{"alignleft": [{"selector": "p,h1,etc", "styles": {"textAlign":"left"}}]}

Source:

https://core.trac.wordpress.org/browser/tags/4.7/src/wp-includes/class-wp-editor.php#L525

Attaching patch for review.

Attachments (1)

class-wp-editor-patch_40718.diff (1.7 KB) - added by MrGregWaugh 7 years ago.
JSON format patch for 40718

Download all attachments as: .zip

Change History (5)

@MrGregWaugh
7 years ago

JSON format patch for 40718

#1 @ocean90
7 years ago

  • Component changed from TinyMCE to Editor
  • Keywords has-patch added
  • Type changed from defect (bug) to enhancement
  • Version trunk deleted

Hello @MrGregWaugh, thanks for your patch!

Wondering if we shouldn't just use json_encode() here. @azaozz Any thoughts on this?

#2 @azaozz
7 years ago

  • Milestone changed from Awaiting Review to Future Release

This is the "setting format" for that TinyMCE option (object of arrays containing other objects), not really JSON.

Looks like this can be enhanced to pass the needed setting as PHP arrays and objects, which then can be filtered (on the PHP side) and JSON encoded before adding the TinyMCE init array. This could be useful for plugins that want to extend that (quite complex) setting.

Other than that, don't see a good reason to convert the format to "hard-coded" JSON.

This is somewhere between maybelater and future release. Would be good to hear from plugin authors that would want to use a PHP filter there before even making a patch.

#3 @parsmizban
7 years ago

  • Resolution set to worksforme
  • Status changed from new to closed

Use this decoder for such json arrays:

https://github.com/parsmizban/services-json

// create a new instance of Services_JSON
$json = new Services_JSON();

// convert a complexe value to JSON notation, and send it to the browser
$value = array('foo', 'bar', array(1, 2, 'baz'), array(3, array(4)));
$output = $json->encode($value);

print($output);
// prints: ["foo","bar",[1,2,"baz"],[3,[4]]]

// accept incoming POST data, assumed to be in JSON notation
$input = file_get_contents('php://input', 1000000);
$value = $json->decode($input);

// if you want to convert json to php arrays:
$json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);

#4 @netweb
7 years ago

  • Milestone Future Release deleted
Note: See TracTickets for help on using tickets.