class-wp-block-list.php 4.6 KB

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