\/)?wp:(?P[a-z][a-z0-9_-]*\/)?(?P[a-z][a-z0-9_-]*)\s+(?P{(?:(?:[^}]+|}+(?=})|(?!}\s+\/?-->).)*+)?}\s+)?(?P\/)?-->/s'; /** @return array[]|null Top-level nodes, or null when the delimiters don't nest (broken markup). */ public static function build( $content ) { $root = array( 'children' => array() ); $stack = array( &$root ); $offset = 0; while ( preg_match( self::DELIMITER, $content, $m, PREG_OFFSET_CAPTURE, $offset ) ) { $start = $m[0][1]; $end = $start + strlen( $m[0][0] ); $offset = $end; $name = ( isset( $m['namespace'] ) && -1 !== $m['namespace'][1] ? $m['namespace'][0] : 'core/' ) . $m['name'][0]; $closer = isset( $m['closer'] ) && -1 !== $m['closer'][1]; $void = isset( $m['void'] ) && -1 !== $m['void'][1]; if ( $closer ) { $top = &$stack[ count( $stack ) - 1 ]; if ( count( $stack ) < 2 || $top['name'] !== $name ) { return null; } $top['inner_end'] = $start; $top['close_end'] = $end; array_pop( $stack ); unset( $top ); continue; } // The delimiter regex's attrs group includes the whitespace after the closing brace. $raw = isset( $m['attrs'] ) && -1 !== $m['attrs'][1] ? rtrim( $m['attrs'][0] ) : ''; $attrs = '' !== $raw ? json_decode( $raw, true ) : array(); $parent = &$stack[ count( $stack ) - 1 ]; $path = ( isset( $parent['path'] ) ? $parent['path'] . '.' : '' ) . count( $parent['children'] ); $parent['children'][] = array( 'name' => $name, 'attrs' => is_array( $attrs ) ? $attrs : array(), 'attrs_raw' => $raw, 'attrs_ok' => '' === $raw || is_array( $attrs ), 'void' => $void, 'open_start' => $start, 'open_end' => $end, 'inner_start' => $end, 'inner_end' => $void ? $end : null, 'close_end' => $void ? $end : null, 'children' => array(), 'path' => $path, ); if ( ! $void ) { $stack[] = &$parent['children'][ count( $parent['children'] ) - 1 ]; } unset( $parent ); } if ( PREG_NO_ERROR !== preg_last_error() ) { return null; // the regex gave up (backtrack/JIT limit): not a complete parse } if ( count( $stack ) !== 1 ) { return null; // an opener was never closed } return $root['children']; } /** Depth-first list of every node. */ public static function flatten( array $nodes, array $trail = array() ) { $out = array(); foreach ( $nodes as $node ) { $node['trail'] = $trail; $out[] = $node; $out = array_merge( $out, self::flatten( $node['children'], array_merge( $trail, array( $node['name'] ) ) ) ); } return $out; } public static function find( array $nodes, $path ) { foreach ( self::flatten( $nodes ) as $node ) { if ( $node['path'] === $path ) { return $node; } } return null; } /** The inner HTML of a leaf block exactly as stored. */ public static function inner( $content, array $node ) { return substr( $content, $node['inner_start'], $node['inner_end'] - $node['inner_start'] ); } /** $content with a leaf block's inner HTML replaced; every other byte unchanged. */ public static function splice_inner( $content, array $node, $inner ) { return substr( $content, 0, $node['inner_start'] ) . $inner . substr( $content, $node['inner_end'] ); } /** $content with bytes [$start, $end) replaced. */ public static function splice_range( $content, $start, $end, $with ) { return substr( $content, 0, $start ) . $with . substr( $content, $end ); } /** * $content with one key of a block's attributes set to $value, by rewriting only its opening * comment. The attributes are re-encoded as JSON objects (so `{}` stays `{}`), and only when * re-encoding the untouched attributes reproduces them byte-for-byte — otherwise the block is * refused rather than having settings nobody asked to change re-spelled. * * @return array{0: string, 1: int} New content and the byte-length change. */ public static function splice_attr( $content, array $node, $key, $value ) { if ( empty( $node['attrs_ok'] ) ) { throw new Tool_Error( "This block's settings can't be read, so ADI won't rewrite them. Edit it in the block editor." ); } $obj = '' !== $node['attrs_raw'] ? json_decode( $node['attrs_raw'] ) : new \stdClass(); if ( '' !== $node['attrs_raw'] && serialize_block_attributes( $obj ) !== $node['attrs_raw'] ) { throw new Tool_Error( "This block's settings are written in a form ADI can't reproduce exactly, so it won't rewrite them. Edit it in the block editor." ); } $obj->$key = $value; $name = 0 === strpos( $node['name'], 'core/' ) ? substr( $node['name'], 5 ) : $node['name']; $opener = ''; $delta = strlen( $opener ) - ( $node['open_end'] - $node['open_start'] ); $out = substr( $content, 0, $node['open_start'] ) . $opener . substr( $content, $node['open_end'] ); return array( $out, $delta ); } }