ru-se.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

class-wp-block-list.php (4473B)


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