暂无描述

class-wp-block-list.php 4.6KB

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. 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. #[ReturnTypeWillChange]
  69. public function offsetExists( $index ) {
  70. return isset( $this->blocks[ $index ] );
  71. }
  72. /**
  73. * Returns the value by the specified block index.
  74. *
  75. * @since 5.5.0
  76. *
  77. * @link https://www.php.net/manual/en/arrayaccess.offsetget.php
  78. *
  79. * @param string $index Index of block value to retrieve.
  80. * @return mixed|null Block value if exists, or null.
  81. */
  82. #[ReturnTypeWillChange]
  83. public function offsetGet( $index ) {
  84. $block = $this->blocks[ $index ];
  85. if ( isset( $block ) && is_array( $block ) ) {
  86. $block = new WP_Block( $block, $this->available_context, $this->registry );
  87. $this->blocks[ $index ] = $block;
  88. }
  89. return $block;
  90. }
  91. /**
  92. * Assign a block value by the specified block index.
  93. *
  94. * @since 5.5.0
  95. *
  96. * @link https://www.php.net/manual/en/arrayaccess.offsetset.php
  97. *
  98. * @param string $index Index of block value to set.
  99. * @param mixed $value Block value.
  100. */
  101. #[ReturnTypeWillChange]
  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. * Unset a block.
  111. *
  112. * @since 5.5.0
  113. *
  114. * @link https://www.php.net/manual/en/arrayaccess.offsetunset.php
  115. *
  116. * @param string $index Index of block value to unset.
  117. */
  118. #[ReturnTypeWillChange]
  119. public function offsetUnset( $index ) {
  120. unset( $this->blocks[ $index ] );
  121. }
  122. /**
  123. * Rewinds back to the first element of the Iterator.
  124. *
  125. * @since 5.5.0
  126. *
  127. * @link https://www.php.net/manual/en/iterator.rewind.php
  128. */
  129. #[ReturnTypeWillChange]
  130. public function rewind() {
  131. reset( $this->blocks );
  132. }
  133. /**
  134. * Returns the current element of the block list.
  135. *
  136. * @since 5.5.0
  137. *
  138. * @link https://www.php.net/manual/en/iterator.current.php
  139. *
  140. * @return mixed Current element.
  141. */
  142. #[ReturnTypeWillChange]
  143. public function current() {
  144. return $this->offsetGet( $this->key() );
  145. }
  146. /**
  147. * Returns the key of the current element of the block list.
  148. *
  149. * @since 5.5.0
  150. *
  151. * @link https://www.php.net/manual/en/iterator.key.php
  152. *
  153. * @return mixed Key of the current element.
  154. */
  155. #[ReturnTypeWillChange]
  156. public function key() {
  157. return key( $this->blocks );
  158. }
  159. /**
  160. * Moves the current position of the block list to the next element.
  161. *
  162. * @since 5.5.0
  163. *
  164. * @link https://www.php.net/manual/en/iterator.next.php
  165. */
  166. #[ReturnTypeWillChange]
  167. public function next() {
  168. next( $this->blocks );
  169. }
  170. /**
  171. * Checks if current position is valid.
  172. *
  173. * @since 5.5.0
  174. *
  175. * @link https://www.php.net/manual/en/iterator.valid.php
  176. */
  177. #[ReturnTypeWillChange]
  178. public function valid() {
  179. return null !== key( $this->blocks );
  180. }
  181. /**
  182. * Returns the count of blocks in the list.
  183. *
  184. * @since 5.5.0
  185. *
  186. * @link https://www.php.net/manual/en/countable.count.php
  187. *
  188. * @return int Block count.
  189. */
  190. #[ReturnTypeWillChange]
  191. public function count() {
  192. return count( $this->blocks );
  193. }
  194. }