No Description

class-wp-block-list.php 4.4KB

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