wp/wp-includes/class-wp-block-list.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
     9 /**
     9 /**
    10  * Class representing a list of block instances.
    10  * Class representing a list of block instances.
    11  *
    11  *
    12  * @since 5.5.0
    12  * @since 5.5.0
    13  */
    13  */
       
    14 #[AllowDynamicProperties]
    14 class WP_Block_List implements Iterator, ArrayAccess, Countable {
    15 class WP_Block_List implements Iterator, ArrayAccess, Countable {
    15 
    16 
    16 	/**
    17 	/**
    17 	 * Original array of parsed block data, or block instances.
    18 	 * Original array of parsed block data, or block instances.
    18 	 *
    19 	 *
    60 		$this->available_context = $available_context;
    61 		$this->available_context = $available_context;
    61 		$this->registry          = $registry;
    62 		$this->registry          = $registry;
    62 	}
    63 	}
    63 
    64 
    64 	/**
    65 	/**
    65 	 * Returns true if a block exists by the specified block index, or false
    66 	 * Returns true if a block exists by the specified block offset, or false
    66 	 * otherwise.
    67 	 * otherwise.
    67 	 *
    68 	 *
    68 	 * @since 5.5.0
    69 	 * @since 5.5.0
    69 	 *
    70 	 *
    70 	 * @link https://www.php.net/manual/en/arrayaccess.offsetexists.php
    71 	 * @link https://www.php.net/manual/en/arrayaccess.offsetexists.php
    71 	 *
    72 	 *
    72 	 * @param string $index Index of block to check.
    73 	 * @param string $offset Offset of block to check for.
    73 	 * @return bool Whether block exists.
    74 	 * @return bool Whether block exists.
    74 	 */
    75 	 */
    75 	#[ReturnTypeWillChange]
    76 	#[ReturnTypeWillChange]
    76 	public function offsetExists( $index ) {
    77 	public function offsetExists( $offset ) {
    77 		return isset( $this->blocks[ $index ] );
    78 		return isset( $this->blocks[ $offset ] );
    78 	}
    79 	}
    79 
    80 
    80 	/**
    81 	/**
    81 	 * Returns the value by the specified block index.
    82 	 * Returns the value by the specified block offset.
    82 	 *
    83 	 *
    83 	 * @since 5.5.0
    84 	 * @since 5.5.0
    84 	 *
    85 	 *
    85 	 * @link https://www.php.net/manual/en/arrayaccess.offsetget.php
    86 	 * @link https://www.php.net/manual/en/arrayaccess.offsetget.php
    86 	 *
    87 	 *
    87 	 * @param string $index Index of block value to retrieve.
    88 	 * @param string $offset Offset of block value to retrieve.
    88 	 * @return mixed|null Block value if exists, or null.
    89 	 * @return mixed|null Block value if exists, or null.
    89 	 */
    90 	 */
    90 	#[ReturnTypeWillChange]
    91 	#[ReturnTypeWillChange]
    91 	public function offsetGet( $index ) {
    92 	public function offsetGet( $offset ) {
    92 		$block = $this->blocks[ $index ];
    93 		$block = $this->blocks[ $offset ];
    93 
    94 
    94 		if ( isset( $block ) && is_array( $block ) ) {
    95 		if ( isset( $block ) && is_array( $block ) ) {
    95 			$block                  = new WP_Block( $block, $this->available_context, $this->registry );
    96 			$block = new WP_Block( $block, $this->available_context, $this->registry );
    96 			$this->blocks[ $index ] = $block;
    97 
       
    98 			$this->blocks[ $offset ] = $block;
    97 		}
    99 		}
    98 
   100 
    99 		return $block;
   101 		return $block;
   100 	}
   102 	}
   101 
   103 
   102 	/**
   104 	/**
   103 	 * Assign a block value by the specified block index.
   105 	 * Assign a block value by the specified block offset.
   104 	 *
   106 	 *
   105 	 * @since 5.5.0
   107 	 * @since 5.5.0
   106 	 *
   108 	 *
   107 	 * @link https://www.php.net/manual/en/arrayaccess.offsetset.php
   109 	 * @link https://www.php.net/manual/en/arrayaccess.offsetset.php
   108 	 *
   110 	 *
   109 	 * @param string $index Index of block value to set.
   111 	 * @param string $offset Offset of block value to set.
   110 	 * @param mixed  $value Block value.
   112 	 * @param mixed  $value Block value.
   111 	 */
   113 	 */
   112 	#[ReturnTypeWillChange]
   114 	#[ReturnTypeWillChange]
   113 	public function offsetSet( $index, $value ) {
   115 	public function offsetSet( $offset, $value ) {
   114 		if ( is_null( $index ) ) {
   116 		if ( is_null( $offset ) ) {
   115 			$this->blocks[] = $value;
   117 			$this->blocks[] = $value;
   116 		} else {
   118 		} else {
   117 			$this->blocks[ $index ] = $value;
   119 			$this->blocks[ $offset ] = $value;
   118 		}
   120 		}
   119 	}
   121 	}
   120 
   122 
   121 	/**
   123 	/**
   122 	 * Unset a block.
   124 	 * Unset a block.
   123 	 *
   125 	 *
   124 	 * @since 5.5.0
   126 	 * @since 5.5.0
   125 	 *
   127 	 *
   126 	 * @link https://www.php.net/manual/en/arrayaccess.offsetunset.php
   128 	 * @link https://www.php.net/manual/en/arrayaccess.offsetunset.php
   127 	 *
   129 	 *
   128 	 * @param string $index Index of block value to unset.
   130 	 * @param string $offset Offset of block value to unset.
   129 	 */
   131 	 */
   130 	#[ReturnTypeWillChange]
   132 	#[ReturnTypeWillChange]
   131 	public function offsetUnset( $index ) {
   133 	public function offsetUnset( $offset ) {
   132 		unset( $this->blocks[ $index ] );
   134 		unset( $this->blocks[ $offset ] );
   133 	}
   135 	}
   134 
   136 
   135 	/**
   137 	/**
   136 	 * Rewinds back to the first element of the Iterator.
   138 	 * Rewinds back to the first element of the Iterator.
   137 	 *
   139 	 *
   207 	 */
   209 	 */
   208 	#[ReturnTypeWillChange]
   210 	#[ReturnTypeWillChange]
   209 	public function count() {
   211 	public function count() {
   210 		return count( $this->blocks );
   212 		return count( $this->blocks );
   211 	}
   213 	}
   212 
       
   213 }
   214 }