Skip to content

Commit

Permalink
try gating on PHP version
Browse files Browse the repository at this point in the history
  • Loading branch information
dmsnell committed Nov 3, 2018
1 parent 551cd00 commit 7613e65
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 18 deletions.
23 changes: 18 additions & 5 deletions lib/parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private function peg_f2($blockName, $a) { return $a; }
private function peg_f3($blockName, $attrs) {
return array(
'blockName' => $blockName,
'attrs' => empty( $attrs ) ? json_decode( '{}', false ) : $attrs,
'attrs' => empty( $attrs ) ? peg_empty_attrs() : $attrs,
'innerBlocks' => array(),
'innerHTML' => '',
);
Expand All @@ -270,7 +270,7 @@ private function peg_f4($s, $children, $e) {

return array(
'blockName' => $s['blockName'],
'attrs' => empty( $s['attrs'] ) ? json_decode( '{}', false ) : $s['attrs'],
'attrs' => empty( $s['attrs'] ) ? peg_empty_attrs() : $s['attrs'],
'innerBlocks' => $innerBlocks,
'innerHTML' => implode( '', $innerHTML ),
);
Expand Down Expand Up @@ -1440,6 +1440,19 @@ public function parse($input) {
// The `maybeJSON` function is not needed in PHP because its return semantics
// are the same as `json_decode`

if ( ! function_exists( 'peg_empty_attrs' ) ) {
function peg_empty_attrs() {
static $empty_attrs = null;

if ( null === $empty_attrs ) {
$force_assoc = version_compare( PHP_VERSION, '7.0.0' ) < 0;
$empty_attrs = json_decode( '{}', $force_assoc );
}

return $empty_attrs;
}
}

// array arguments are backwards because of PHP
if ( ! function_exists( 'peg_array_partition' ) ) {
function peg_array_partition( $array, $predicate ) {
Expand All @@ -1463,7 +1476,7 @@ function peg_join_blocks( $pre, $tokens, $post ) {
if ( ! empty( $pre ) ) {
$blocks[] = array(
'blockName' => null,
'attrs' => json_decode( '{}', false ),
'attrs' => peg_empty_attrs(),
'innerBlocks' => array(),
'innerHTML' => $pre
);
Expand All @@ -1477,7 +1490,7 @@ function peg_join_blocks( $pre, $tokens, $post ) {
if ( ! empty( $html ) ) {
$blocks[] = array(
'blockName' => null,
'attrs' => json_decode( '{}', false ),
'attrs' => peg_empty_attrs(),
'innerBlocks' => array(),
'innerHTML' => $html
);
Expand All @@ -1487,7 +1500,7 @@ function peg_join_blocks( $pre, $tokens, $post ) {
if ( ! empty( $post ) ) {
$blocks[] = array(
'blockName' => null,
'attrs' => json_decode( '{}', false ),
'attrs' => peg_empty_attrs(),
'innerBlocks' => array(),
'innerHTML' => $post
);
Expand Down
17 changes: 14 additions & 3 deletions packages/block-serialization-default-parser/parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ class WP_Block_Parser {
*/
public $stack;

/**
* Empty associative array, here due to PHP quirks
*
* @since 4.2.0
* @var array empty associative array
*/
public $empty_attrs;

/**
* Parses a document and returns a list of block structures
*
Expand All @@ -176,6 +184,9 @@ function parse( $document ) {
$this->output = array();
$this->stack = array();

$force_assoc = version_compare( PHP_VERSION, '7.0.0' ) < 0;
$this->empty_attrs = json_decode( '{}', $force_assoc );

do {
// twiddle our thumbs
} while ( $this->proceed() );
Expand Down Expand Up @@ -375,7 +386,7 @@ function next_token() {
*/
$attrs = $has_attrs
? json_decode( $matches[ 'attrs' ][ 0 ], /* as-associative */ true )
: json_decode( '{}', /* don't ask why, just verify in PHP */ false );
: $this->empty_attrs;

/*
* This state isn't allowed
Expand Down Expand Up @@ -405,8 +416,8 @@ function next_token() {
* @param string $innerHTML HTML content of block
* @return WP_Block_Parser_Block freeform block object
*/
static function freeform( $innerHTML ) {
return new WP_Block_Parser_Block( null, json_decode( '{}', false ), array(), $innerHTML );
function freeform( $innerHTML ) {
return new WP_Block_Parser_Block( null, $this->empty_attrs, array(), $innerHTML );
}

/**
Expand Down
23 changes: 18 additions & 5 deletions packages/block-serialization-spec-parser/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@
// The `maybeJSON` function is not needed in PHP because its return semantics
// are the same as `json_decode`
if ( ! function_exists( 'peg_empty_attrs' ) ) {
function peg_empty_attrs() {
static $empty_attrs = null;
if ( null === $empty_attrs ) {
$force_assoc = version_compare( PHP_VERSION, '7.0.0' ) < 0;
$empty_attrs = json_decode( '{}', $force_assoc );
}
return $empty_attrs;
}
}
// array arguments are backwards because of PHP
if ( ! function_exists( 'peg_array_partition' ) ) {
function peg_array_partition( $array, $predicate ) {
Expand All @@ -73,7 +86,7 @@ if ( ! function_exists( 'peg_join_blocks' ) ) {
if ( ! empty( $pre ) ) {
$blocks[] = array(
'blockName' => null,
'attrs' => json_decode( '{}', false ),
'attrs' => peg_empty_attrs(),
'innerBlocks' => array(),
'innerHTML' => $pre
);
Expand All @@ -87,7 +100,7 @@ if ( ! function_exists( 'peg_join_blocks' ) ) {
if ( ! empty( $html ) ) {
$blocks[] = array(
'blockName' => null,
'attrs' => json_decode( '{}', false ),
'attrs' => peg_empty_attrs(),
'innerBlocks' => array(),
'innerHTML' => $html
);
Expand All @@ -97,7 +110,7 @@ if ( ! function_exists( 'peg_join_blocks' ) ) {
if ( ! empty( $post ) ) {
$blocks[] = array(
'blockName' => null,
'attrs' => json_decode( '{}', false ),
'attrs' => peg_empty_attrs(),
'innerBlocks' => array(),
'innerHTML' => $post
);
Expand Down Expand Up @@ -198,7 +211,7 @@ Block_Void
/** <?php
return array(
'blockName' => $blockName,
'attrs' => empty( $attrs ) ? json_decode( '{}', false ) : $attrs,
'attrs' => empty( $attrs ) ? peg_empty_attrs() : $attrs,
'innerBlocks' => array(),
'innerHTML' => '',
);
Expand All @@ -220,7 +233,7 @@ Block_Balanced
return array(
'blockName' => $s['blockName'],
'attrs' => empty( $s['attrs'] ) ? json_decode( '{}', false ) : $s['attrs'],
'attrs' => empty( $s['attrs'] ) ? peg_empty_attrs() : $s['attrs'],
'innerBlocks' => $innerBlocks,
'innerHTML' => implode( '', $innerHTML ),
);
Expand Down
23 changes: 18 additions & 5 deletions packages/block-serialization-spec-parser/parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7613e65

Please sign in to comment.