Skip to content

Commit

Permalink
Block API: Implement iterable WP_Block_List
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Apr 29, 2020
1 parent 688d4b7 commit 9f24221
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 6 deletions.
171 changes: 171 additions & 0 deletions lib/class-wp-block-list.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<?php
/**
* Blocks API: WP_Block_List class
*
* @package Gutenberg
*/

/**
* Class representing a list of block instances.
*/
class WP_Block_List implements Iterator, ArrayAccess {

/**
* Original array of parsed block data.
*
* @var array|WP_Block[]
* @access protected
*/
protected $blocks;

/**
* All available context of the current hierarchy.
*
* @var array
* @access protected
*/
protected $available_context;

/**
* Block type registry to use in constructing block instances.
*
* @var WP_Block_Type_Registry
* @access protected
*/
protected $registry;

/**
* Constructor.
*
* Populates object properties from the provided block instance argument.
*
* @param array|WP_Block[] $blocks Array of parsed block data, or block instances.
* @param array $available_context Optional array of ancestry context values.
* @param WP_Block_Type_Registry $registry Optional block type registry.
*/
public function __construct( $blocks, $available_context = array(), $registry = null ) {
$this->blocks = $blocks;
$this->available_context = $available_context;
$this->registry = is_null( $registry ) ?
WP_Block_Type_Registry::get_instance() :
$registry;
}

/*
* ArrayAccess interface methods.
*/

/**
* Returns true if a block exists by the specified block index, or false
* otherwise.
*
* @link https://www.php.net/manual/en/arrayaccess.offsetexists.php
*
* @param string $index Index of block to check.
*
* @return bool Whether block exists.
*/
public function offsetExists( $index ) {
return isset( $this->blocks[ $index ] );
}

/**
* Returns the value by the specified block index.
*
* @link https://www.php.net/manual/en/arrayaccess.offsetget.php
*
* @param string $index Index of block value to retrieve.
*
* @return mixed|null Block value if exists, or null.
*/
public function offsetGet( $index ) {
$block = $this->blocks[ $index ];

if ( isset( $block ) && is_array( $block ) ) {
$block = new WP_Block( $block, $this->available_context, $this->registry );
}

return $block;
}

/**
* Assign a block value by the specified block index.
*
* @link https://www.php.net/manual/en/arrayaccess.offsetset.php
*
* @param string $index Index of block value to set.
* @param mixed $value Block value.
*/
public function offsetSet( $index, $value ) {
if ( is_null( $index ) ) {
$this->blocks[] = $value;
} else {
$this->blocks[ $index ] = $value;
}
}

/**
* Unset a block.
*
* @link https://www.php.net/manual/en/arrayaccess.offsetunset.php
*
* @param string $index Index of block value to unset.
*/
public function offsetUnset( $index ) {
unset( $this->blocks[ $index ] );
}

/*
* Iterator interface methods.
*/

/**
* Rewinds back to the first element of the Iterator.
*
* @link https://www.php.net/manual/en/iterator.rewind.php
*/
public function rewind() {
reset( $this->blocks );
}

/**
* Returns the current element of the block list.
*
* @link https://www.php.net/manual/en/iterator.current.php
*
* @return mixed Current element.
*/
public function current() {
return $this->offsetGet( $this->key() );
}

/**
* Returns the key of the current element of the block list.
*
* @link https://www.php.net/manual/en/iterator.key.php
*
* @return mixed Key of the current element.
*/
public function key() {
return key( $this->blocks );
}

/**
* Moves the current position of the block list to the next element.
*
* @link https://www.php.net/manual/en/iterator.next.php
*/
public function next() {
next( $this->blocks );
}

/**
* Checks if current position is valid.
*
* @link https://www.php.net/manual/en/iterator.valid.php
*/
public function valid() {
return null !== key( $this->blocks );
}

}
7 changes: 1 addition & 6 deletions lib/class-wp-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,7 @@ public function __construct( $block, $available_context = array(), $registry = n
}
/* phpcs:enable */

$this->inner_blocks = array_map(
function( $inner_block ) use ( $child_context, $registry ) {
return new WP_Block( $inner_block, $child_context, $registry );
},
$block['innerBlocks']
);
$this->inner_blocks = new WP_Block_List( $block['innerBlocks'], $child_context, $registry );
}

if ( ! empty( $block['innerHTML'] ) ) {
Expand Down
4 changes: 4 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ function gutenberg_is_experiment_enabled( $name ) {
require dirname( __FILE__ ) . '/class-wp-block.php';
}

if ( ! class_exists( 'WP_Block_List' ) ) {
require dirname( __FILE__ ) . '/class-wp-block-list.php';
}

require dirname( __FILE__ ) . '/compat.php';

require dirname( __FILE__ ) . '/blocks.php';
Expand Down

0 comments on commit 9f24221

Please sign in to comment.