| 1 |
<?php |
| 2 |
/** |
| 3 |
* Blocks API: WP_Block_List class |
| 4 |
* |
| 5 |
* @package Gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Class representing a list of block instances. |
| 10 |
* |
| 11 |
* This class can be removed when plugin support requires WordPress 5.5.0+. |
| 12 |
* |
| 13 |
* @see https://core.trac.wordpress.org/ticket/49926 |
| 14 |
* @see https://core.trac.wordpress.org/changeset/48159 |
| 15 |
*/ |
| 16 |
class WP_Block_List implements Iterator, ArrayAccess, Countable { |
| 17 |
|
| 18 |
/** |
| 19 |
* Original array of parsed block data. |
| 20 |
* |
| 21 |
* @var array|WP_Block[] |
| 22 |
* @access protected |
| 23 |
*/ |
| 24 |
protected $blocks; |
| 25 |
|
| 26 |
/** |
| 27 |
* All available context of the current hierarchy. |
| 28 |
* |
| 29 |
* @var array |
| 30 |
* @access protected |
| 31 |
*/ |
| 32 |
protected $available_context; |
| 33 |
|
| 34 |
/** |
| 35 |
* Block type registry to use in constructing block instances. |
| 36 |
* |
| 37 |
* @var WP_Block_Type_Registry |
| 38 |
* @access protected |
| 39 |
*/ |
| 40 |
protected $registry; |
| 41 |
|
| 42 |
/** |
| 43 |
* Constructor. |
| 44 |
* |
| 45 |
* Populates object properties from the provided block instance argument. |
| 46 |
* |
| 47 |
* @param array|WP_Block[] $blocks Array of parsed block data, or block instances. |
| 48 |
* @param array $available_context Optional array of ancestry context values. |
| 49 |
* @param WP_Block_Type_Registry $registry Optional block type registry. |
| 50 |
*/ |
| 51 |
public function __construct( $blocks, $available_context = array(), $registry = null ) { |
| 52 |
if ( is_null( $registry ) ) { |
| 53 |
$registry = WP_Block_Type_Registry::get_instance(); |
| 54 |
} |
| 55 |
|
| 56 |
$this->blocks = $blocks; |
| 57 |
$this->available_context = $available_context; |
| 58 |
$this->registry = $registry; |
| 59 |
} |
| 60 |
|
| 61 |
/* |
| 62 |
* ArrayAccess interface methods. |
| 63 |
*/ |
| 64 |
|
| 65 |
/** |
| 66 |
* Returns true if a block exists by the specified block index, or false |
| 67 |
* otherwise. |
| 68 |
* |
| 69 |
* @link https://www.php.net/manual/en/arrayaccess.offsetexists.php |
| 70 |
* |
| 71 |
* @param string $index Index of block to check. |
| 72 |
* |
| 73 |
* @return bool Whether block exists. |
| 74 |
*/ |
| 75 |
public function offsetExists( $index ) { |
| 76 |
return isset( $this->blocks[ $index ] ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Returns the value by the specified block index. |
| 81 |
* |
| 82 |
* @link https://www.php.net/manual/en/arrayaccess.offsetget.php |
| 83 |
* |
| 84 |
* @param string $index Index of block value to retrieve. |
| 85 |
* |
| 86 |
* @return mixed|null Block value if exists, or null. |
| 87 |
*/ |
| 88 |
public function offsetGet( $index ) { |
| 89 |
$block = $this->blocks[ $index ]; |
| 90 |
|
| 91 |
if ( isset( $block ) && is_array( $block ) ) { |
| 92 |
$block = new WP_Block( $block, $this->available_context, $this->registry ); |
| 93 |
$this->blocks[ $index ] = $block; |
| 94 |
} |
| 95 |
|
| 96 |
return $block; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Assign a block value by the specified block index. |
| 101 |
* |
| 102 |
* @link https://www.php.net/manual/en/arrayaccess.offsetset.php |
| 103 |
* |
| 104 |
* @param string $index Index of block value to set. |
| 105 |
* @param mixed $value Block value. |
| 106 |
*/ |
| 107 |
public function offsetSet( $index, $value ) { |
| 108 |
if ( is_null( $index ) ) { |
| 109 |
$this->blocks[] = $value; |
| 110 |
} else { |
| 111 |
$this->blocks[ $index ] = $value; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Unset a block. |
| 117 |
* |
| 118 |
* @link https://www.php.net/manual/en/arrayaccess.offsetunset.php |
| 119 |
* |
| 120 |
* @param string $index Index of block value to unset. |
| 121 |
*/ |
| 122 |
public function offsetUnset( $index ) { |
| 123 |
unset( $this->blocks[ $index ] ); |
| 124 |
} |
| 125 |
|
| 126 |
/* |
| 127 |
* Iterator interface methods. |
| 128 |
*/ |
| 129 |
|
| 130 |
/** |
| 131 |
* Rewinds back to the first element of the Iterator. |
| 132 |
* |
| 133 |
* @link https://www.php.net/manual/en/iterator.rewind.php |
| 134 |
*/ |
| 135 |
public function rewind() { |
| 136 |
reset( $this->blocks ); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Returns the current element of the block list. |
| 141 |
* |
| 142 |
* @link https://www.php.net/manual/en/iterator.current.php |
| 143 |
* |
| 144 |
* @return mixed Current element. |
| 145 |
*/ |
| 146 |
public function current() { |
| 147 |
return $this->offsetGet( $this->key() ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Returns the key of the current element of the block list. |
| 152 |
* |
| 153 |
* @link https://www.php.net/manual/en/iterator.key.php |
| 154 |
* |
| 155 |
* @return mixed Key of the current element. |
| 156 |
*/ |
| 157 |
public function key() { |
| 158 |
return key( $this->blocks ); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Moves the current position of the block list to the next element. |
| 163 |
* |
| 164 |
* @link https://www.php.net/manual/en/iterator.next.php |
| 165 |
*/ |
| 166 |
public function next() { |
| 167 |
next( $this->blocks ); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Checks if current position is valid. |
| 172 |
* |
| 173 |
* @link https://www.php.net/manual/en/iterator.valid.php |
| 174 |
*/ |
| 175 |
public function valid() { |
| 176 |
return null !== key( $this->blocks ); |
| 177 |
} |
| 178 |
|
| 179 |
/* |
| 180 |
* Countable interface methods. |
| 181 |
*/ |
| 182 |
|
| 183 |
/** |
| 184 |
* Returns the count of blocks in the list. |
| 185 |
* |
| 186 |
* @link https://www.php.net/manual/en/countable.count.php |
| 187 |
* |
| 188 |
* @return int Block count. |
| 189 |
*/ |
| 190 |
public function count() { |
| 191 |
return count( $this->blocks ); |
| 192 |
} |
| 193 |
|
| 194 |
} |
| 195 |
|