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