Skip to content
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

Block API: Block Context: Remove block filter #21921

Merged
merged 5 commits into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,37 +175,3 @@ function gutenberg_get_post_from_context() {
}
return get_post();
}

/**
* Shim that hooks into `pre_render_block` so as to override `render_block` with
* a function that assigns block context.
*
* This can be removed when plugin support requires WordPress 5.5.0+.
*
* @see (TBD Trac Link)
*
* @param string|null $pre_render The pre-rendered content. Defaults to null.
* @param array $parsed_block The parsed block being rendered.
*
* @return string String of rendered HTML.
*/
function gutenberg_render_block_with_assigned_block_context( $pre_render, $parsed_block ) {
global $post;

// If a non-null value is provided, a filter has run at an earlier priority
// and has already handled custom rendering and should take precedence.
if ( null !== $pre_render ) {
return $pre_render;
}

$source_block = $parsed_block;

/** This filter is documented in src/wp-includes/blocks.php */
$parsed_block = apply_filters( 'render_block_data', $parsed_block, $source_block );
$context = array( 'postId' => $post->ID );
$block = new WP_Block( $parsed_block, $context );

/** This filter is documented in src/wp-includes/blocks.php */
return apply_filters( 'render_block', $block->render(), $parsed_block );
}
add_filter( 'pre_render_block', 'gutenberg_render_block_with_assigned_block_context', 9, 2 );
6 changes: 3 additions & 3 deletions packages/block-library/src/post-title/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
* @return string Returns the filtered post title for the current post wrapped inside "h1" tags.
*/
function render_block_core_post_title() {
global $_experimental_block;
if ( ! isset( $_experimental_block->context['postId'] ) ) {
$post = gutenberg_get_post_from_context();
aduth marked this conversation as resolved.
Show resolved Hide resolved
if ( ! $post ) {
return '';
}

return '<h1>' . get_the_title( $_experimental_block->context['postId'] ) . '</h1>';
return '<h1>' . get_the_title( $post ) . '</h1>';
}

/**
Expand Down
13 changes: 1 addition & 12 deletions packages/e2e-tests/plugins/block-context.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,7 @@ function gutenberg_test_register_context_blocks() {
register_block_type(
'gutenberg/test-context-consumer',
array(
'context' => array( 'gutenberg/recordId' ),
'render_callback' => function() {
global $_experimental_block;

$record_id = $_experimental_block->context['gutenberg/recordId'];

if ( ! is_int( $record_id ) ) {
throw new Exception( 'Expected numeric recordId' );
}

return 'The record ID is: ' . filter_var( $record_id, FILTER_VALIDATE_INT );
},
'context' => array( 'gutenberg/recordId' ),
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ describe( 'Block context', () => {
expect( innerBlockText ).toBe( 'The record ID is: 123' );
} );

test( 'Block context is reflected in the preview', async () => {
// Disable reason: Block context PHP implementation is temporarily reverted.
// This will be unskipped once the implementation is restored. Skipping was
// the most direct option for revert given time constraints.

/* eslint-disable-next-line jest/no-disabled-tests */
test.skip( 'Block context is reflected in the preview', async () => {
await insertBlock( 'Test Context Provider' );
const editorPage = page;
const previewPage = await openPreviewPage( editorPage );
Expand Down
2 changes: 2 additions & 0 deletions phpunit/class-block-context-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ protected function register_block_type( $name, $args ) {
* its inner blocks.
*/
function test_provides_block_context() {
$this->markTestSkipped();

$provided_context = array();

$this->register_block_type(
Expand Down