Skip to content

Commit 191d64a

Browse files
dmsnellmcsfMukesh Panchalyouknowriad
committed
Build: Restore block parser in Core. (WordPress#10761)
Trac ticket: Core-64521 The work in WordPress#10638 [[61438]](https://core.trac.wordpress.org/changeset/61438) for Core-64393 removed the block parser classes from Core, which caused numerous scripts to fail because they were missing. Conditional checks were added in WordPress#10718 [[61492]](https://core.trac.wordpress.org/changeset/61492) which left WordPress in an inoperable state. This patch restores the block parser in Core, in preparation for work to remove it from Gutenberg (in a separate patch). Ironically, the files were removed because the new build was copying them over from Gutenberg and the intent was to avoid having two sources of truth, but this was previously the existing mechanism, so having done nothing to the parser files would have left the status quo. This patch removes the problems originally created by removing the files. They will not be copied from Gutenberg any more and the only source of truth will be Core. Until removed from Gutenberg, because of the build changes, any changes made on the Gutenberg side will be lost unless manually copied over. Developed in: WordPress#10761 Discussed in: https://core.trac.wordpress.org/ticket/64521 Follow-up to [61438], [61492]. Props dmsnell, mcsf, mukesh27, youknowriad. Co-authored-by: Miguel Fonseca <mcsf@git.wordpress.org> Co-authored-by: Mukesh Panchal <mukesh27@git.wordpress.org> Co-authored-by: Riad Benguella <youknowriad@git.wordpress.org> Github-PR: 10761 Github-PR-URL: WordPress#10761 Trac-Ticket: 64521 Trac-Ticket-URL: https://core.trac.wordpress.org/ticket/64521 Branch-Name: build/restore-block-parser
1 parent 16aa10f commit 191d64a

File tree

8 files changed

+581
-73
lines changed

8 files changed

+581
-73
lines changed

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ wp-tests-config.php
3838
/src/wp-includes/blocks/*
3939
!/src/wp-includes/blocks/index.php
4040
/src/wp-includes/build
41-
/src/wp-includes/class-wp-block-parser.php
42-
/src/wp-includes/class-wp-block-parser-block.php
43-
/src/wp-includes/class-wp-block-parser-frame.php
4441
/src/wp-includes/theme.json
4542
/packagehash.txt
4643
/.gutenberg-hash

src/wp-includes/blocks.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,10 +2421,6 @@ function parse_blocks( $content ) {
24212421
*/
24222422
$parser_class = apply_filters( 'block_parser_class', 'WP_Block_Parser' );
24232423

2424-
if ( ! class_exists( $parser_class ) ) {
2425-
return array();
2426-
}
2427-
24282424
$parser = new $parser_class();
24292425
return $parser->parse( $content );
24302426
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* Block Serialization Parser
4+
*
5+
* @package WordPress
6+
*/
7+
8+
/**
9+
* Class WP_Block_Parser_Block
10+
*
11+
* Holds the block structure in memory
12+
*
13+
* @since 5.0.0
14+
*/
15+
class WP_Block_Parser_Block {
16+
/**
17+
* Name of block
18+
*
19+
* @example "core/paragraph"
20+
*
21+
* @since 5.0.0
22+
* @var string
23+
*/
24+
public $blockName; // phpcs:ignore WordPress.NamingConventions.ValidVariableName
25+
26+
/**
27+
* Optional set of attributes from block comment delimiters
28+
*
29+
* @example null
30+
* @example array( 'columns' => 3 )
31+
*
32+
* @since 5.0.0
33+
* @var array|null
34+
*/
35+
public $attrs;
36+
37+
/**
38+
* List of inner blocks (of this same class)
39+
*
40+
* @since 5.0.0
41+
* @var WP_Block_Parser_Block[]
42+
*/
43+
public $innerBlocks; // phpcs:ignore WordPress.NamingConventions.ValidVariableName
44+
45+
/**
46+
* Resultant HTML from inside block comment delimiters
47+
* after removing inner blocks
48+
*
49+
* @example "...Just <!-- wp:test /--> testing..." -> "Just testing..."
50+
*
51+
* @since 5.0.0
52+
* @var string
53+
*/
54+
public $innerHTML; // phpcs:ignore WordPress.NamingConventions.ValidVariableName
55+
56+
/**
57+
* List of string fragments and null markers where inner blocks were found
58+
*
59+
* @example array(
60+
* 'innerHTML' => 'BeforeInnerAfter',
61+
* 'innerBlocks' => array( block, block ),
62+
* 'innerContent' => array( 'Before', null, 'Inner', null, 'After' ),
63+
* )
64+
*
65+
* @since 5.0.0
66+
* @var array
67+
*/
68+
public $innerContent; // phpcs:ignore WordPress.NamingConventions.ValidVariableName
69+
70+
/**
71+
* Constructor.
72+
*
73+
* Will populate object properties from the provided arguments.
74+
*
75+
* @since 5.0.0
76+
*
77+
* @param string $name Name of block.
78+
* @param array $attrs Optional set of attributes from block comment delimiters.
79+
* @param array $inner_blocks List of inner blocks (of this same class).
80+
* @param string $inner_html Resultant HTML from inside block comment delimiters after removing inner blocks.
81+
* @param array $inner_content List of string fragments and null markers where inner blocks were found.
82+
*/
83+
public function __construct( $name, $attrs, $inner_blocks, $inner_html, $inner_content ) {
84+
$this->blockName = $name; // phpcs:ignore WordPress.NamingConventions.ValidVariableName
85+
$this->attrs = $attrs;
86+
$this->innerBlocks = $inner_blocks; // phpcs:ignore WordPress.NamingConventions.ValidVariableName
87+
$this->innerHTML = $inner_html; // phpcs:ignore WordPress.NamingConventions.ValidVariableName
88+
$this->innerContent = $inner_content; // phpcs:ignore WordPress.NamingConventions.ValidVariableName
89+
}
90+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* Block Serialization Parser
4+
*
5+
* @package WordPress
6+
*/
7+
8+
/**
9+
* Class WP_Block_Parser_Frame
10+
*
11+
* Holds partial blocks in memory while parsing
12+
*
13+
* @internal
14+
* @since 5.0.0
15+
*/
16+
class WP_Block_Parser_Frame {
17+
/**
18+
* Full or partial block
19+
*
20+
* @since 5.0.0
21+
* @var WP_Block_Parser_Block
22+
*/
23+
public $block;
24+
25+
/**
26+
* Byte offset into document for start of parse token
27+
*
28+
* @since 5.0.0
29+
* @var int
30+
*/
31+
public $token_start;
32+
33+
/**
34+
* Byte length of entire parse token string
35+
*
36+
* @since 5.0.0
37+
* @var int
38+
*/
39+
public $token_length;
40+
41+
/**
42+
* Byte offset into document for after parse token ends
43+
* (used during reconstruction of stack into parse production)
44+
*
45+
* @since 5.0.0
46+
* @var int
47+
*/
48+
public $prev_offset;
49+
50+
/**
51+
* Byte offset into document where leading HTML before token starts
52+
*
53+
* @since 5.0.0
54+
* @var int
55+
*/
56+
public $leading_html_start;
57+
58+
/**
59+
* Constructor
60+
*
61+
* Will populate object properties from the provided arguments.
62+
*
63+
* @since 5.0.0
64+
*
65+
* @param WP_Block_Parser_Block $block Full or partial block.
66+
* @param int $token_start Byte offset into document for start of parse token.
67+
* @param int $token_length Byte length of entire parse token string.
68+
* @param int|null $prev_offset Optional. Byte offset into document for after parse token ends. Default null.
69+
* @param int|null $leading_html_start Optional. Byte offset into document where leading HTML before token starts.
70+
* Default null.
71+
*/
72+
public function __construct( $block, $token_start, $token_length, $prev_offset = null, $leading_html_start = null ) {
73+
$this->block = $block;
74+
$this->token_start = $token_start;
75+
$this->token_length = $token_length;
76+
$this->prev_offset = isset( $prev_offset ) ? $prev_offset : $token_start + $token_length;
77+
$this->leading_html_start = $leading_html_start;
78+
}
79+
}

0 commit comments

Comments
 (0)