-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Block API: Block Context: Filter content, prepare attributes at render, pass block to render #21925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d049341
Block API: WP_Block: Run filter on rendered content
aduth 4bde09c
Block API: WP_Block: Prepare attributes for render at render
aduth 52db02b
Block API: WP_Block: Pass block as third argument of render_callback
aduth 688d4b7
Block API: WP_Block: Use magic method for lazy attributes initialization
aduth 9f24221
Block API: Implement iterable WP_Block_List
aduth c205883
Block API: Unskip block context test
aduth d5298c0
Block Library: Post Title: Document function attributes
aduth 87b20dc
Block API: WP_Block_List: Format constructor to prepare defaults
aduth f0bbd32
Block API: WP_Block_List: Replace reference of initialized block
aduth 1c32a1c
Block API: WP_Block_List: Implement Countable
aduth 494e62e
Block API: WP_Block_List: Add unit tests for array access, iterable
aduth c03051d
Block API: WP_Block: Add test case for render_block filtering
aduth a1492b8
Docs: Restore documentation for block context PHP API
aduth c3e0b9d
Block API: WP_Block: Remove unnecessary isset
aduth 6e6efd4
Framework: Fix formatting for multi-line comment
aduth cd3d81b
Framework: Include postType context server-side
aduth 3020f1b
Framework: Improve postType comment grammar
aduth 75f64c9
Framework: PHPCS formatting
aduth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| <?php | ||
| /** | ||
| * Blocks API: WP_Block_List class | ||
| * | ||
| * @package Gutenberg | ||
| */ | ||
|
|
||
| /** | ||
| * Class representing a list of block instances. | ||
| */ | ||
| class WP_Block_List implements Iterator, ArrayAccess { | ||
|
|
||
| /** | ||
| * Original array of parsed block data. | ||
| * | ||
| * @var array|WP_Block[] | ||
| * @access protected | ||
| */ | ||
| protected $blocks; | ||
|
|
||
| /** | ||
| * All available context of the current hierarchy. | ||
| * | ||
| * @var array | ||
| * @access protected | ||
| */ | ||
| protected $available_context; | ||
|
|
||
| /** | ||
| * Block type registry to use in constructing block instances. | ||
| * | ||
| * @var WP_Block_Type_Registry | ||
| * @access protected | ||
| */ | ||
| protected $registry; | ||
|
|
||
| /** | ||
| * Constructor. | ||
| * | ||
| * Populates object properties from the provided block instance argument. | ||
| * | ||
| * @param array|WP_Block[] $blocks Array of parsed block data, or block instances. | ||
| * @param array $available_context Optional array of ancestry context values. | ||
| * @param WP_Block_Type_Registry $registry Optional block type registry. | ||
| */ | ||
| public function __construct( $blocks, $available_context = array(), $registry = null ) { | ||
| $this->blocks = $blocks; | ||
| $this->available_context = $available_context; | ||
| $this->registry = is_null( $registry ) ? | ||
| WP_Block_Type_Registry::get_instance() : | ||
| $registry; | ||
| } | ||
|
|
||
| /* | ||
| * ArrayAccess interface methods. | ||
| */ | ||
|
|
||
| /** | ||
| * Returns true if a block exists by the specified block index, or false | ||
| * otherwise. | ||
| * | ||
| * @link https://www.php.net/manual/en/arrayaccess.offsetexists.php | ||
| * | ||
| * @param string $index Index of block to check. | ||
| * | ||
| * @return bool Whether block exists. | ||
| */ | ||
| public function offsetExists( $index ) { | ||
| return isset( $this->blocks[ $index ] ); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the value by the specified block index. | ||
| * | ||
| * @link https://www.php.net/manual/en/arrayaccess.offsetget.php | ||
| * | ||
| * @param string $index Index of block value to retrieve. | ||
| * | ||
| * @return mixed|null Block value if exists, or null. | ||
| */ | ||
| public function offsetGet( $index ) { | ||
| $block = $this->blocks[ $index ]; | ||
|
|
||
| if ( isset( $block ) && is_array( $block ) ) { | ||
| $block = new WP_Block( $block, $this->available_context, $this->registry ); | ||
| } | ||
|
|
||
| return $block; | ||
| } | ||
|
|
||
| /** | ||
| * Assign a block value by the specified block index. | ||
| * | ||
| * @link https://www.php.net/manual/en/arrayaccess.offsetset.php | ||
| * | ||
| * @param string $index Index of block value to set. | ||
epiqueras marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * @param mixed $value Block value. | ||
| */ | ||
| public function offsetSet( $index, $value ) { | ||
| if ( is_null( $index ) ) { | ||
| $this->blocks[] = $value; | ||
| } else { | ||
| $this->blocks[ $index ] = $value; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Unset a block. | ||
| * | ||
| * @link https://www.php.net/manual/en/arrayaccess.offsetunset.php | ||
| * | ||
| * @param string $index Index of block value to unset. | ||
| */ | ||
| public function offsetUnset( $index ) { | ||
| unset( $this->blocks[ $index ] ); | ||
| } | ||
|
|
||
| /* | ||
| * Iterator interface methods. | ||
| */ | ||
|
|
||
| /** | ||
| * Rewinds back to the first element of the Iterator. | ||
| * | ||
| * @link https://www.php.net/manual/en/iterator.rewind.php | ||
| */ | ||
| public function rewind() { | ||
| reset( $this->blocks ); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the current element of the block list. | ||
| * | ||
| * @link https://www.php.net/manual/en/iterator.current.php | ||
| * | ||
| * @return mixed Current element. | ||
| */ | ||
| public function current() { | ||
| return $this->offsetGet( $this->key() ); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the key of the current element of the block list. | ||
| * | ||
| * @link https://www.php.net/manual/en/iterator.key.php | ||
| * | ||
| * @return mixed Key of the current element. | ||
| */ | ||
| public function key() { | ||
| return key( $this->blocks ); | ||
| } | ||
|
|
||
| /** | ||
| * Moves the current position of the block list to the next element. | ||
| * | ||
| * @link https://www.php.net/manual/en/iterator.next.php | ||
| */ | ||
| public function next() { | ||
| next( $this->blocks ); | ||
| } | ||
|
|
||
| /** | ||
| * Checks if current position is valid. | ||
| * | ||
| * @link https://www.php.net/manual/en/iterator.valid.php | ||
| */ | ||
| public function valid() { | ||
| return null !== key( $this->blocks ); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.