PluginProbe
Gutenberg / 8.2.1
Gutenberg v8.2.1
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / class-wp-block-list.php

class-wp-block-list.php in Gutenberg 8.2.1, at lib/class-wp-block-list.php

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