Skip to content

Commit 4317761

Browse files
authored
Fix dynamic blocks not rendering in the frontend (#11050)
* Fix dynamic blocks not rendering in the frontend * Use variables for hook priorities * Test that dynamic blocks are rendered when meta boxes use the excerpt * Fix autop hook order * Fix linting * Fix scoping issues * Add an e2e test for the autop issue * copy/paste issues * Trying to fix travis * Trim content to avoid small differences between themes
1 parent c5227f3 commit 4317761

File tree

6 files changed

+77
-4
lines changed

6 files changed

+77
-4
lines changed

lib/blocks.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ function do_blocks( $content ) {
262262

263263
return $rendered_content;
264264
}
265+
265266
add_filter( 'the_content', 'do_blocks', 7 ); // BEFORE do_shortcode() and oembed.
266267
}
267268

@@ -292,7 +293,7 @@ function strip_dynamic_blocks( $content ) {
292293
* @return string
293294
*/
294295
function strip_dynamic_blocks_add_filter( $text ) {
295-
add_filter( 'the_content', 'strip_dynamic_blocks', 6 ); // Before do_blocks().
296+
add_filter( 'the_content', 'strip_dynamic_blocks', 6 );
296297

297298
return $text;
298299
}
@@ -312,7 +313,7 @@ function strip_dynamic_blocks_add_filter( $text ) {
312313
* @return string
313314
*/
314315
function strip_dynamic_blocks_remove_filter( $text ) {
315-
remove_filter( 'the_content', 'strip_dynamic_blocks', 8 );
316+
remove_filter( 'the_content', 'strip_dynamic_blocks', 6 );
316317

317318
return $text;
318319
}

lib/compat.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ function gutenberg_wpautop( $content ) {
114114
return wpautop( $content );
115115
}
116116
remove_filter( 'the_content', 'wpautop' );
117-
add_filter( 'the_content', 'gutenberg_wpautop', 8 );
117+
add_filter( 'the_content', 'gutenberg_wpautop', 6 );
118118

119119

120120
/**
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Compatibility with Classic Editor Should not apply autop when rendering blocks 1`] = `
4+
"<a>
5+
Random Link
6+
</a>"
7+
`;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Internal dependencies
3+
*/
4+
import { newPost, insertBlock, publishPost } from '../support/utils';
5+
6+
describe( 'Compatibility with Classic Editor', () => {
7+
beforeEach( async () => {
8+
await newPost();
9+
} );
10+
11+
it( 'Should not apply autop when rendering blocks', async () => {
12+
await insertBlock( 'Custom HTML' );
13+
await page.keyboard.type( '<a>' );
14+
await page.keyboard.press( 'Enter' );
15+
await page.keyboard.type( 'Random Link' );
16+
await page.keyboard.press( 'Enter' );
17+
await page.keyboard.type( '</a>' );
18+
await publishPost();
19+
20+
// View the post.
21+
const viewPostLinks = await page.$x( "//a[contains(text(), 'View Post')]" );
22+
await viewPostLinks[ 0 ].click();
23+
await page.waitForNavigation();
24+
25+
// Check the the content doesn't contain <p> tags
26+
await page.waitForSelector( '.entry-content' );
27+
const content = await page.$eval( '.entry-content', ( element ) => element.innerHTML.trim() );
28+
expect( content ).toMatchSnapshot();
29+
} );
30+
} );

test/e2e/specs/meta-boxes.test.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Internal dependencies
33
*/
4-
import { newPost } from '../support/utils';
4+
import { newPost, insertBlock, publishPost } from '../support/utils';
55
import { activatePlugin, deactivatePlugin } from '../support/plugins';
66

77
describe( 'Meta boxes', () => {
@@ -35,4 +35,27 @@ describe( 'Meta boxes', () => {
3535
page.keyboard.up( 'Meta' ),
3636
] );
3737
} );
38+
39+
it( 'Should render dynamic blocks when the meta box uses the excerpt for front end rendering', async () => {
40+
// Publish a post so there's something for the latest posts dynamic block to render.
41+
await newPost();
42+
await page.type( '.editor-post-title__input', 'A published post' );
43+
await insertBlock( 'Paragraph' );
44+
await page.keyboard.type( 'Hello there!' );
45+
await publishPost();
46+
47+
// Publish a post with the latest posts dynamic block.
48+
await newPost();
49+
await page.type( '.editor-post-title__input', 'Dynamic block test' );
50+
await insertBlock( 'Latest Posts' );
51+
await publishPost();
52+
53+
// View the post.
54+
const viewPostLinks = await page.$x( "//a[contains(text(), 'View Post')]" );
55+
await viewPostLinks[ 0 ].click();
56+
await page.waitForNavigation();
57+
58+
// Check the the dynamic block appears.
59+
await page.waitForSelector( '.wp-block-latest-posts' );
60+
} );
3861
} );

test/e2e/test-plugins/meta-box.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,15 @@ function gutenberg_test_meta_box_add_meta_box() {
2323
);
2424
}
2525
add_action( 'add_meta_boxes', 'gutenberg_test_meta_box_add_meta_box' );
26+
27+
28+
function gutenberg_test_meta_box_render_head() {
29+
// Emulates what plugins like Yoast do with meta data on the front end.
30+
// Tests that our excerpt processing does not interfere with dynamic blocks.
31+
$excerpt = wp_strip_all_tags( get_the_excerpt() );
32+
?>
33+
<meta property="gutenberg:hello" content="<?php echo esc_attr( $excerpt ); ?>" />
34+
<?php
35+
}
36+
37+
add_action( 'wp_head', 'gutenberg_test_meta_box_render_head' );

0 commit comments

Comments
 (0)