content = (string) $content; $this->blocks = parse_blocks( $this->content ); } /** * Whether writing the parsed blocks back reproduces the content byte for * byte. Blaze changes only content for which this holds, so the blocks it * doesn't target stay exactly as they were. * * @return bool */ public function round_trips() { return serialize_blocks( $this->blocks ) === $this->content; } /** * Whether the content has no blocks at all: it was written in the classic * editor, or by something that writes plain HTML. * * @return bool */ public function is_classic() { foreach ( $this->blocks as $block ) { if ( null !== $block['blockName'] ) { return false; } } return true; } /** * The content the blocks serialize to now. * * @return string */ public function serialize() { return serialize_blocks( $this->blocks ); } /** * Whether a top-level block is only the whitespace between blocks. * * @param array $block Parsed block. * @return bool */ public static function is_separator( array $block ) { return null === $block['blockName'] && '' === trim( (string) $block['innerHTML'] ); } /** * Every block in document order, with its path and whether Blaze may * change it. * * @param string|null $post_reason Why no block of the post may change (a * page builder, a locked template, content * that doesn't round-trip), or null. * @return array[] Entries keyed by path. Each has `path`, `indices` (array * indices from the top), `block`, `editable`, `reason`, * `parent` (the parent's block name, or null at the top) * and `insertable` (whether blocks may follow it). */ public function entries( $post_reason = null ) { $entries = array(); $this->walk( $this->blocks, array(), array(), null, $post_reason, null === $post_reason, $entries ); return $entries; } /** * Walks one level of blocks. * * @param array[] $blocks Blocks at this level. * @param int[] $parent_indices Array indices of the parent. * @param string[] $parent_path Path parts of the parent. * @param string|null $parent_name The parent's block name. * @param string|null $inherited Why blocks at this level can't change, if an ancestor decides it. * @param bool $may_insert Whether blocks may be inserted at this level. * @param array $entries Collected entries. */ private function walk( array $blocks, array $parent_indices, array $parent_path, $parent_name, $inherited, $may_insert, array &$entries ) { $position = 0; foreach ( $blocks as $index => $block ) { if ( array() === $parent_indices && self::is_separator( $block ) ) { continue; } $path = array_merge( $parent_path, array( (string) $position ) ); $indices = array_merge( $parent_indices, array( $index ) ); ++$position; $reason = null !== $inherited ? $inherited : self::own_reason( $block ); $name = $block['blockName']; $entries[ implode( '.', $path ) ] = array( 'path' => implode( '.', $path ), 'indices' => $indices, 'block' => $block, 'editable' => null === $reason, 'reason' => $reason, 'parent' => $parent_name, 'insertable' => $may_insert, ); if ( empty( $block['innerBlocks'] ) ) { continue; } $child_reason = $inherited; if ( null === $child_reason ) { if ( self::locks_template( $block ) ) { $child_reason = 'locked'; } elseif ( ! in_array( $name, self::LAYOUT, true ) && ! in_array( $name, array( 'core/list', 'core/list-item' ), true ) ) { $child_reason = 'container'; } } $this->walk( $block['innerBlocks'], $indices, $path, $name, $child_reason, null === $child_reason && in_array( $name, self::INSERTABLE_PARENTS, true ), $entries ); }//end foreach } /** * Why a block can't change, judged by the block alone, or null when it can. * * @param array $block Parsed block. * @return string|null */ private static function own_reason( array $block ) { $name = $block['blockName']; if ( null === $name ) { return 'classic'; } if ( 'core/block' === $name ) { return 'synced_pattern'; } if ( 0 === strpos( $name, 'core-embed/' ) || 'core/embed' === $name ) { return 'embed'; } if ( 0 !== strpos( $name, 'core/' ) ) { return 'third_party'; } if ( 'core/shortcode' === $name ) { return 'shortcode'; } if ( in_array( $name, self::MEDIA, true ) ) { return 'media'; } if ( in_array( $name, self::LAYOUT, true ) ) { return 'layout'; } if ( ! in_array( $name, self::TEXT, true ) ) { return 'unsupported'; } if ( 'core/list' === $name && ! empty( $block['innerBlocks'] ) ) { // Its items are the editable blocks. return 'layout'; } if ( Text::has_shortcode_like( self::own_html( $block ) ) ) { return 'shortcode'; } return null; } /** * Whether a block locks the blocks inside it (`templateLock`). * * @param array $block Parsed block. * @return bool */ private static function locks_template( array $block ) { return ! empty( $block['attrs']['templateLock'] ); } /** * The block's own HTML: its markup without its inner blocks. * * @param array $block Parsed block. * @return string */ public static function own_html( array $block ) { return implode( '', self::own_parts( $block ) ); } /** * The string parts of a block's `innerContent`, keyed by their index. * * @param array $block Parsed block. * @return string[] */ public static function own_parts( array $block ) { $parts = array(); foreach ( (array) $block['innerContent'] as $index => $part ) { if ( is_string( $part ) ) { $parts[ $index ] = $part; } } return $parts; } /** * The hash Blaze compares to know a block didn't change. * * @param array $block Parsed block. * @return string */ public static function hash( array $block ) { return hash( 'sha256', serialize_block( $block ) ); } /** * A reference to the block at the given array indices. * * @param int[] $indices Array indices from the top. * @return array Reference to the block. */ public function &block_at( array $indices ) { $list = &$this->blocks; $last = array_pop( $indices ); foreach ( $indices as $index ) { $list = &$list[ $index ]['innerBlocks']; } return $list[ $last ]; } /** * Inserts blocks right after the block at the given array indices. * * At the top level each new block is preceded by the blank line the * editor writes between blocks. Inside a block, the parent's * `innerContent` gets a placeholder for each new block, so the parent's * own markup stays as it was. * * @param int[] $indices Array indices of the block to insert after. * @param array[] $new_blocks Parsed blocks to insert. */ public function insert_after( array $indices, array $new_blocks ) { $index = array_pop( $indices ); if ( array() === $indices ) { $spliced = array(); foreach ( $new_blocks as $new_block ) { $spliced[] = self::separator(); $spliced[] = $new_block; } array_splice( $this->blocks, $index + 1, 0, $spliced ); return; } $parent = &$this->block_at( $indices ); array_splice( $parent['innerBlocks'], $index + 1, 0, $new_blocks ); // Find the placeholder of the block at $index in innerContent. $seen = -1; foreach ( $parent['innerContent'] as $position => $part ) { if ( null === $part && ++$seen === $index ) { break; } } $placeholders = array(); foreach ( $new_blocks as $new_block ) { $placeholders[] = "\n\n"; $placeholders[] = null; } array_splice( $parent['innerContent'], $position + 1, 0, $placeholders ); } /** * The whitespace the editor writes between top-level blocks. * * @return array */ private static function separator() { return array( 'blockName' => null, 'attrs' => array(), 'innerBlocks' => array(), 'innerHTML' => "\n\n", 'innerContent' => array( "\n\n" ), ); } }