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