Edit File by line
/home/zeestwma/redstone.../wp-inclu...
File: class-wp-block-parser.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Block Serialization Parser
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
/**
[7] Fix | Delete
* Class WP_Block_Parser
[8] Fix | Delete
*
[9] Fix | Delete
* Parses a document and constructs a list of parsed block objects
[10] Fix | Delete
*
[11] Fix | Delete
* @since 5.0.0
[12] Fix | Delete
* @since 4.0.0 returns arrays not objects, all attributes are arrays
[13] Fix | Delete
*/
[14] Fix | Delete
class WP_Block_Parser {
[15] Fix | Delete
/**
[16] Fix | Delete
* Input document being parsed
[17] Fix | Delete
*
[18] Fix | Delete
* @example "Pre-text\n<!-- wp:paragraph -->This is inside a block!<!-- /wp:paragraph -->"
[19] Fix | Delete
*
[20] Fix | Delete
* @since 5.0.0
[21] Fix | Delete
* @var string
[22] Fix | Delete
*/
[23] Fix | Delete
public $document;
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* Tracks parsing progress through document
[27] Fix | Delete
*
[28] Fix | Delete
* @since 5.0.0
[29] Fix | Delete
* @var int
[30] Fix | Delete
*/
[31] Fix | Delete
public $offset;
[32] Fix | Delete
[33] Fix | Delete
/**
[34] Fix | Delete
* List of parsed blocks
[35] Fix | Delete
*
[36] Fix | Delete
* @since 5.0.0
[37] Fix | Delete
* @var array[]
[38] Fix | Delete
*/
[39] Fix | Delete
public $output;
[40] Fix | Delete
[41] Fix | Delete
/**
[42] Fix | Delete
* Stack of partially-parsed structures in memory during parse
[43] Fix | Delete
*
[44] Fix | Delete
* @since 5.0.0
[45] Fix | Delete
* @var WP_Block_Parser_Frame[]
[46] Fix | Delete
*/
[47] Fix | Delete
public $stack;
[48] Fix | Delete
[49] Fix | Delete
/**
[50] Fix | Delete
* Parses a document and returns a list of block structures
[51] Fix | Delete
*
[52] Fix | Delete
* When encountering an invalid parse will return a best-effort
[53] Fix | Delete
* parse. In contrast to the specification parser this does not
[54] Fix | Delete
* return an error on invalid inputs.
[55] Fix | Delete
*
[56] Fix | Delete
* @since 5.0.0
[57] Fix | Delete
*
[58] Fix | Delete
* @param string $document Input document being parsed.
[59] Fix | Delete
* @return array[]
[60] Fix | Delete
*/
[61] Fix | Delete
public function parse( $document ) {
[62] Fix | Delete
$this->document = $document;
[63] Fix | Delete
$this->offset = 0;
[64] Fix | Delete
$this->output = array();
[65] Fix | Delete
$this->stack = array();
[66] Fix | Delete
[67] Fix | Delete
while ( $this->proceed() ) {
[68] Fix | Delete
continue;
[69] Fix | Delete
}
[70] Fix | Delete
[71] Fix | Delete
return $this->output;
[72] Fix | Delete
}
[73] Fix | Delete
[74] Fix | Delete
/**
[75] Fix | Delete
* Processes the next token from the input document
[76] Fix | Delete
* and returns whether to proceed eating more tokens
[77] Fix | Delete
*
[78] Fix | Delete
* This is the "next step" function that essentially
[79] Fix | Delete
* takes a token as its input and decides what to do
[80] Fix | Delete
* with that token before descending deeper into a
[81] Fix | Delete
* nested block tree or continuing along the document
[82] Fix | Delete
* or breaking out of a level of nesting.
[83] Fix | Delete
*
[84] Fix | Delete
* @internal
[85] Fix | Delete
* @since 5.0.0
[86] Fix | Delete
* @return bool
[87] Fix | Delete
*/
[88] Fix | Delete
public function proceed() {
[89] Fix | Delete
$next_token = $this->next_token();
[90] Fix | Delete
list( $token_type, $block_name, $attrs, $start_offset, $token_length ) = $next_token;
[91] Fix | Delete
$stack_depth = count( $this->stack );
[92] Fix | Delete
[93] Fix | Delete
// we may have some HTML soup before the next block.
[94] Fix | Delete
$leading_html_start = $start_offset > $this->offset ? $this->offset : null;
[95] Fix | Delete
[96] Fix | Delete
switch ( $token_type ) {
[97] Fix | Delete
case 'no-more-tokens':
[98] Fix | Delete
// if not in a block then flush output.
[99] Fix | Delete
if ( 0 === $stack_depth ) {
[100] Fix | Delete
$this->add_freeform();
[101] Fix | Delete
return false;
[102] Fix | Delete
}
[103] Fix | Delete
[104] Fix | Delete
/*
[105] Fix | Delete
* Otherwise we have a problem
[106] Fix | Delete
* This is an error
[107] Fix | Delete
*
[108] Fix | Delete
* we have options
[109] Fix | Delete
* - treat it all as freeform text
[110] Fix | Delete
* - assume an implicit closer (easiest when not nesting)
[111] Fix | Delete
*/
[112] Fix | Delete
[113] Fix | Delete
// for the easy case we'll assume an implicit closer.
[114] Fix | Delete
if ( 1 === $stack_depth ) {
[115] Fix | Delete
$this->add_block_from_stack();
[116] Fix | Delete
return false;
[117] Fix | Delete
}
[118] Fix | Delete
[119] Fix | Delete
/*
[120] Fix | Delete
* for the nested case where it's more difficult we'll
[121] Fix | Delete
* have to assume that multiple closers are missing
[122] Fix | Delete
* and so we'll collapse the whole stack piecewise
[123] Fix | Delete
*/
[124] Fix | Delete
while ( 0 < count( $this->stack ) ) {
[125] Fix | Delete
$this->add_block_from_stack();
[126] Fix | Delete
}
[127] Fix | Delete
return false;
[128] Fix | Delete
[129] Fix | Delete
case 'void-block':
[130] Fix | Delete
/*
[131] Fix | Delete
* easy case is if we stumbled upon a void block
[132] Fix | Delete
* in the top-level of the document
[133] Fix | Delete
*/
[134] Fix | Delete
if ( 0 === $stack_depth ) {
[135] Fix | Delete
if ( isset( $leading_html_start ) ) {
[136] Fix | Delete
$this->output[] = (array) $this->freeform(
[137] Fix | Delete
substr(
[138] Fix | Delete
$this->document,
[139] Fix | Delete
$leading_html_start,
[140] Fix | Delete
$start_offset - $leading_html_start
[141] Fix | Delete
)
[142] Fix | Delete
);
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
$this->output[] = (array) new WP_Block_Parser_Block( $block_name, $attrs, array(), '', array() );
[146] Fix | Delete
$this->offset = $start_offset + $token_length;
[147] Fix | Delete
return true;
[148] Fix | Delete
}
[149] Fix | Delete
[150] Fix | Delete
// otherwise we found an inner block.
[151] Fix | Delete
$this->add_inner_block(
[152] Fix | Delete
new WP_Block_Parser_Block( $block_name, $attrs, array(), '', array() ),
[153] Fix | Delete
$start_offset,
[154] Fix | Delete
$token_length
[155] Fix | Delete
);
[156] Fix | Delete
$this->offset = $start_offset + $token_length;
[157] Fix | Delete
return true;
[158] Fix | Delete
[159] Fix | Delete
case 'block-opener':
[160] Fix | Delete
// track all newly-opened blocks on the stack.
[161] Fix | Delete
array_push(
[162] Fix | Delete
$this->stack,
[163] Fix | Delete
new WP_Block_Parser_Frame(
[164] Fix | Delete
new WP_Block_Parser_Block( $block_name, $attrs, array(), '', array() ),
[165] Fix | Delete
$start_offset,
[166] Fix | Delete
$token_length,
[167] Fix | Delete
$start_offset + $token_length,
[168] Fix | Delete
$leading_html_start
[169] Fix | Delete
)
[170] Fix | Delete
);
[171] Fix | Delete
$this->offset = $start_offset + $token_length;
[172] Fix | Delete
return true;
[173] Fix | Delete
[174] Fix | Delete
case 'block-closer':
[175] Fix | Delete
/*
[176] Fix | Delete
* if we're missing an opener we're in trouble
[177] Fix | Delete
* This is an error
[178] Fix | Delete
*/
[179] Fix | Delete
if ( 0 === $stack_depth ) {
[180] Fix | Delete
/*
[181] Fix | Delete
* we have options
[182] Fix | Delete
* - assume an implicit opener
[183] Fix | Delete
* - assume _this_ is the opener
[184] Fix | Delete
* - give up and close out the document
[185] Fix | Delete
*/
[186] Fix | Delete
$this->add_freeform();
[187] Fix | Delete
return false;
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
// if we're not nesting then this is easy - close the block.
[191] Fix | Delete
if ( 1 === $stack_depth ) {
[192] Fix | Delete
$this->add_block_from_stack( $start_offset );
[193] Fix | Delete
$this->offset = $start_offset + $token_length;
[194] Fix | Delete
return true;
[195] Fix | Delete
}
[196] Fix | Delete
[197] Fix | Delete
/*
[198] Fix | Delete
* otherwise we're nested and we have to close out the current
[199] Fix | Delete
* block and add it as a new innerBlock to the parent
[200] Fix | Delete
*/
[201] Fix | Delete
$stack_top = array_pop( $this->stack );
[202] Fix | Delete
$html = substr( $this->document, $stack_top->prev_offset, $start_offset - $stack_top->prev_offset );
[203] Fix | Delete
$stack_top->block->innerHTML .= $html;
[204] Fix | Delete
$stack_top->block->innerContent[] = $html;
[205] Fix | Delete
$stack_top->prev_offset = $start_offset + $token_length;
[206] Fix | Delete
[207] Fix | Delete
$this->add_inner_block(
[208] Fix | Delete
$stack_top->block,
[209] Fix | Delete
$stack_top->token_start,
[210] Fix | Delete
$stack_top->token_length,
[211] Fix | Delete
$start_offset + $token_length
[212] Fix | Delete
);
[213] Fix | Delete
$this->offset = $start_offset + $token_length;
[214] Fix | Delete
return true;
[215] Fix | Delete
[216] Fix | Delete
default:
[217] Fix | Delete
// This is an error.
[218] Fix | Delete
$this->add_freeform();
[219] Fix | Delete
return false;
[220] Fix | Delete
}
[221] Fix | Delete
}
[222] Fix | Delete
[223] Fix | Delete
/**
[224] Fix | Delete
* Scans the document from where we last left off
[225] Fix | Delete
* and finds the next valid token to parse if it exists
[226] Fix | Delete
*
[227] Fix | Delete
* Returns the type of the find: kind of find, block information, attributes
[228] Fix | Delete
*
[229] Fix | Delete
* @internal
[230] Fix | Delete
* @since 5.0.0
[231] Fix | Delete
* @since 4.6.1 fixed a bug in attribute parsing which caused catastrophic backtracking on invalid block comments
[232] Fix | Delete
* @return array
[233] Fix | Delete
*/
[234] Fix | Delete
public function next_token() {
[235] Fix | Delete
$matches = null;
[236] Fix | Delete
[237] Fix | Delete
/*
[238] Fix | Delete
* aye the magic
[239] Fix | Delete
* we're using a single RegExp to tokenize the block comment delimiters
[240] Fix | Delete
* we're also using a trick here because the only difference between a
[241] Fix | Delete
* block opener and a block closer is the leading `/` before `wp:` (and
[242] Fix | Delete
* a closer has no attributes). we can trap them both and process the
[243] Fix | Delete
* match back in PHP to see which one it was.
[244] Fix | Delete
*/
[245] Fix | Delete
$has_match = preg_match(
[246] Fix | Delete
'/<!--\s+(?P<closer>\/)?wp:(?P<namespace>[a-z][a-z0-9_-]*\/)?(?P<name>[a-z][a-z0-9_-]*)\s+(?P<attrs>{(?:(?:[^}]+|}+(?=})|(?!}\s+\/?-->).)*+)?}\s+)?(?P<void>\/)?-->/s',
[247] Fix | Delete
$this->document,
[248] Fix | Delete
$matches,
[249] Fix | Delete
PREG_OFFSET_CAPTURE,
[250] Fix | Delete
$this->offset
[251] Fix | Delete
);
[252] Fix | Delete
[253] Fix | Delete
// if we get here we probably have catastrophic backtracking or out-of-memory in the PCRE.
[254] Fix | Delete
if ( false === $has_match ) {
[255] Fix | Delete
return array( 'no-more-tokens', null, null, null, null );
[256] Fix | Delete
}
[257] Fix | Delete
[258] Fix | Delete
// we have no more tokens.
[259] Fix | Delete
if ( 0 === $has_match ) {
[260] Fix | Delete
return array( 'no-more-tokens', null, null, null, null );
[261] Fix | Delete
}
[262] Fix | Delete
[263] Fix | Delete
list( $match, $started_at ) = $matches[0];
[264] Fix | Delete
[265] Fix | Delete
$length = strlen( $match );
[266] Fix | Delete
$is_closer = isset( $matches['closer'] ) && -1 !== $matches['closer'][1];
[267] Fix | Delete
$is_void = isset( $matches['void'] ) && -1 !== $matches['void'][1];
[268] Fix | Delete
$namespace = $matches['namespace'];
[269] Fix | Delete
$namespace = ( isset( $namespace ) && -1 !== $namespace[1] ) ? $namespace[0] : 'core/';
[270] Fix | Delete
$name = $namespace . $matches['name'][0];
[271] Fix | Delete
$has_attrs = isset( $matches['attrs'] ) && -1 !== $matches['attrs'][1];
[272] Fix | Delete
[273] Fix | Delete
/*
[274] Fix | Delete
* Fun fact! It's not trivial in PHP to create "an empty associative array" since all arrays
[275] Fix | Delete
* are associative arrays. If we use `array()` we get a JSON `[]`
[276] Fix | Delete
*/
[277] Fix | Delete
$attrs = $has_attrs
[278] Fix | Delete
? json_decode( $matches['attrs'][0], /* as-associative */ true )
[279] Fix | Delete
: array();
[280] Fix | Delete
[281] Fix | Delete
/*
[282] Fix | Delete
* This state isn't allowed
[283] Fix | Delete
* This is an error
[284] Fix | Delete
*/
[285] Fix | Delete
if ( $is_closer && ( $is_void || $has_attrs ) ) {
[286] Fix | Delete
// we can ignore them since they don't hurt anything.
[287] Fix | Delete
}
[288] Fix | Delete
[289] Fix | Delete
if ( $is_void ) {
[290] Fix | Delete
return array( 'void-block', $name, $attrs, $started_at, $length );
[291] Fix | Delete
}
[292] Fix | Delete
[293] Fix | Delete
if ( $is_closer ) {
[294] Fix | Delete
return array( 'block-closer', $name, null, $started_at, $length );
[295] Fix | Delete
}
[296] Fix | Delete
[297] Fix | Delete
return array( 'block-opener', $name, $attrs, $started_at, $length );
[298] Fix | Delete
}
[299] Fix | Delete
[300] Fix | Delete
/**
[301] Fix | Delete
* Returns a new block object for freeform HTML
[302] Fix | Delete
*
[303] Fix | Delete
* @internal
[304] Fix | Delete
* @since 5.0.0
[305] Fix | Delete
*
[306] Fix | Delete
* @param string $inner_html HTML content of block.
[307] Fix | Delete
* @return WP_Block_Parser_Block freeform block object.
[308] Fix | Delete
*/
[309] Fix | Delete
public function freeform( $inner_html ) {
[310] Fix | Delete
return new WP_Block_Parser_Block( null, array(), array(), $inner_html, array( $inner_html ) );
[311] Fix | Delete
}
[312] Fix | Delete
[313] Fix | Delete
/**
[314] Fix | Delete
* Pushes a length of text from the input document
[315] Fix | Delete
* to the output list as a freeform block.
[316] Fix | Delete
*
[317] Fix | Delete
* @internal
[318] Fix | Delete
* @since 5.0.0
[319] Fix | Delete
* @param null $length how many bytes of document text to output.
[320] Fix | Delete
*/
[321] Fix | Delete
public function add_freeform( $length = null ) {
[322] Fix | Delete
$length = $length ? $length : strlen( $this->document ) - $this->offset;
[323] Fix | Delete
[324] Fix | Delete
if ( 0 === $length ) {
[325] Fix | Delete
return;
[326] Fix | Delete
}
[327] Fix | Delete
[328] Fix | Delete
$this->output[] = (array) $this->freeform( substr( $this->document, $this->offset, $length ) );
[329] Fix | Delete
}
[330] Fix | Delete
[331] Fix | Delete
/**
[332] Fix | Delete
* Given a block structure from memory pushes
[333] Fix | Delete
* a new block to the output list.
[334] Fix | Delete
*
[335] Fix | Delete
* @internal
[336] Fix | Delete
* @since 5.0.0
[337] Fix | Delete
* @param WP_Block_Parser_Block $block The block to add to the output.
[338] Fix | Delete
* @param int $token_start Byte offset into the document where the first token for the block starts.
[339] Fix | Delete
* @param int $token_length Byte length of entire block from start of opening token to end of closing token.
[340] Fix | Delete
* @param int|null $last_offset Last byte offset into document if continuing form earlier output.
[341] Fix | Delete
*/
[342] Fix | Delete
public function add_inner_block( WP_Block_Parser_Block $block, $token_start, $token_length, $last_offset = null ) {
[343] Fix | Delete
$parent = $this->stack[ count( $this->stack ) - 1 ];
[344] Fix | Delete
$parent->block->innerBlocks[] = (array) $block;
[345] Fix | Delete
$html = substr( $this->document, $parent->prev_offset, $token_start - $parent->prev_offset );
[346] Fix | Delete
[347] Fix | Delete
if ( ! empty( $html ) ) {
[348] Fix | Delete
$parent->block->innerHTML .= $html;
[349] Fix | Delete
$parent->block->innerContent[] = $html;
[350] Fix | Delete
}
[351] Fix | Delete
[352] Fix | Delete
$parent->block->innerContent[] = null;
[353] Fix | Delete
$parent->prev_offset = $last_offset ? $last_offset : $token_start + $token_length;
[354] Fix | Delete
}
[355] Fix | Delete
[356] Fix | Delete
/**
[357] Fix | Delete
* Pushes the top block from the parsing stack to the output list.
[358] Fix | Delete
*
[359] Fix | Delete
* @internal
[360] Fix | Delete
* @since 5.0.0
[361] Fix | Delete
* @param int|null $end_offset byte offset into document for where we should stop sending text output as HTML.
[362] Fix | Delete
*/
[363] Fix | Delete
public function add_block_from_stack( $end_offset = null ) {
[364] Fix | Delete
$stack_top = array_pop( $this->stack );
[365] Fix | Delete
$prev_offset = $stack_top->prev_offset;
[366] Fix | Delete
[367] Fix | Delete
$html = isset( $end_offset )
[368] Fix | Delete
? substr( $this->document, $prev_offset, $end_offset - $prev_offset )
[369] Fix | Delete
: substr( $this->document, $prev_offset );
[370] Fix | Delete
[371] Fix | Delete
if ( ! empty( $html ) ) {
[372] Fix | Delete
$stack_top->block->innerHTML .= $html;
[373] Fix | Delete
$stack_top->block->innerContent[] = $html;
[374] Fix | Delete
}
[375] Fix | Delete
[376] Fix | Delete
if ( isset( $stack_top->leading_html_start ) ) {
[377] Fix | Delete
$this->output[] = (array) $this->freeform(
[378] Fix | Delete
substr(
[379] Fix | Delete
$this->document,
[380] Fix | Delete
$stack_top->leading_html_start,
[381] Fix | Delete
$stack_top->token_start - $stack_top->leading_html_start
[382] Fix | Delete
)
[383] Fix | Delete
);
[384] Fix | Delete
}
[385] Fix | Delete
[386] Fix | Delete
$this->output[] = (array) $stack_top->block;
[387] Fix | Delete
}
[388] Fix | Delete
}
[389] Fix | Delete
[390] Fix | Delete
/**
[391] Fix | Delete
* WP_Block_Parser_Block class.
[392] Fix | Delete
*
[393] Fix | Delete
* Required for backward compatibility in WordPress Core.
[394] Fix | Delete
*/
[395] Fix | Delete
require_once __DIR__ . '/class-wp-block-parser-block.php';
[396] Fix | Delete
[397] Fix | Delete
/**
[398] Fix | Delete
* WP_Block_Parser_Frame class.
[399] Fix | Delete
*
[400] Fix | Delete
* Required for backward compatibility in WordPress Core.
[401] Fix | Delete
*/
[402] Fix | Delete
require_once __DIR__ . '/class-wp-block-parser-frame.php';
[403] Fix | Delete
[404] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function