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

Fix embedded blocks when converted to reusable blocks #21043

Closed
wants to merge 6 commits into from

Conversation

earnjam
Copy link
Contributor

@earnjam earnjam commented Mar 20, 2020

This PR fixes the issue of embed blocks not displaying on the front-end after being converted to reusable blocks.

#14663 added a test for this, but that PR had become stale. This PR updates that new test to support the UI changes that have occurred since it was written. It also reorders the_content filters, which fixes the issue and gets that new test to pass. This would fix #18098, and close #14608.

It feels very risky to mess with the filter order because it is so brittle. I've run all the tests for this on core and in Gutenberg, as well as done some manual testing and it appears to work and resolve the issue.

We'd probably want to commit the core change early in release cycle to try to catch any issues.

Related Trac ticket: https://core.trac.wordpress.org/ticket/46457

Pull request that shows core tests passing: WordPress/wordpress-develop#192

@github-actions
Copy link

github-actions bot commented Mar 20, 2020

Size Change: +37 B (0%)

Total Size: 856 kB

Filename Size Change
build/block-library/index.js 110 kB +31 B (0%)
build/edit-post/index.js 91.2 kB +6 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 998 B 0 B
build/annotations/index.js 3.43 kB 0 B
build/api-fetch/index.js 3.39 kB 0 B
build/autop/index.js 2.58 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/index.js 6.02 kB 0 B
build/block-directory/style-rtl.css 760 B 0 B
build/block-directory/style.css 760 B 0 B
build/block-editor/index.js 100 kB 0 B
build/block-editor/style-rtl.css 10.9 kB 0 B
build/block-editor/style.css 10.9 kB 0 B
build/block-library/editor-rtl.css 7.24 kB 0 B
build/block-library/editor.css 7.24 kB 0 B
build/block-library/style-rtl.css 7.41 kB 0 B
build/block-library/style.css 7.42 kB 0 B
build/block-library/theme-rtl.css 669 B 0 B
build/block-library/theme.css 671 B 0 B
build/block-serialization-default-parser/index.js 1.65 kB 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/blocks/index.js 57.5 kB 0 B
build/components/index.js 191 kB 0 B
build/components/style-rtl.css 15.8 kB 0 B
build/components/style.css 15.7 kB 0 B
build/compose/index.js 6.21 kB 0 B
build/core-data/index.js 10.6 kB 0 B
build/data-controls/index.js 1.04 kB 0 B
build/data/index.js 8.2 kB 0 B
build/date/index.js 5.37 kB 0 B
build/deprecated/index.js 771 B 0 B
build/dom-ready/index.js 568 B 0 B
build/dom/index.js 3.06 kB 0 B
build/edit-post/style-rtl.css 8.47 kB 0 B
build/edit-post/style.css 8.46 kB 0 B
build/edit-site/index.js 5.56 kB 0 B
build/edit-site/style-rtl.css 2.62 kB 0 B
build/edit-site/style.css 2.62 kB 0 B
build/edit-widgets/index.js 4.43 kB 0 B
build/edit-widgets/style-rtl.css 2.58 kB 0 B
build/edit-widgets/style.css 2.58 kB 0 B
build/editor/editor-styles-rtl.css 381 B 0 B
build/editor/editor-styles.css 382 B 0 B
build/editor/index.js 43.8 kB 0 B
build/editor/style-rtl.css 3.97 kB 0 B
build/editor/style.css 3.96 kB 0 B
build/element/index.js 4.44 kB 0 B
build/escape-html/index.js 733 B 0 B
build/format-library/index.js 6.95 kB 0 B
build/format-library/style-rtl.css 502 B 0 B
build/format-library/style.css 502 B 0 B
build/hooks/index.js 1.93 kB 0 B
build/html-entities/index.js 621 B 0 B
build/i18n/index.js 3.49 kB 0 B
build/is-shallow-equal/index.js 711 B 0 B
build/keyboard-shortcuts/index.js 2.3 kB 0 B
build/keycodes/index.js 1.69 kB 0 B
build/list-reusable-blocks/index.js 2.99 kB 0 B
build/list-reusable-blocks/style-rtl.css 226 B 0 B
build/list-reusable-blocks/style.css 226 B 0 B
build/media-utils/index.js 4.84 kB 0 B
build/notices/index.js 1.58 kB 0 B
build/nux/index.js 3.01 kB 0 B
build/nux/style-rtl.css 616 B 0 B
build/nux/style.css 613 B 0 B
build/plugins/index.js 2.54 kB 0 B
build/primitives/index.js 1.5 kB 0 B
build/priority-queue/index.js 780 B 0 B
build/redux-routine/index.js 2.83 kB 0 B
build/rich-text/index.js 14.4 kB 0 B
build/server-side-render/index.js 2.55 kB 0 B
build/shortcode/index.js 1.7 kB 0 B
build/token-list/index.js 1.27 kB 0 B
build/url/index.js 4.01 kB 0 B
build/viewport/index.js 1.61 kB 0 B
build/warning/index.js 1.14 kB 0 B
build/wordcount/index.js 1.18 kB 0 B

compressed-size-action

@earnjam earnjam changed the title WIP: Reorder the_content hooks Fix embedded blocks when converted to reusable blocks Mar 20, 2020
@earnjam earnjam marked this pull request as ready for review March 20, 2020 14:00
remove_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 );
remove_filter( 'the_content', 'do_blocks', 9 );
add_filter( 'the_content', 'do_blocks', 8 );
add_filter( 'the_content', array( $wp_embed, 'autoembed' ), 9 );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently these has the potential of breaking autop things, so I'd like some good testing for this PR. Also cc @aduth since you're better aware of the potential issues.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this has the potential to affect dynamic blocks, so we need to check these too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I was thinking that the tests from #11050 had those things covered now, but it's always possible we're missing things.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least within the block filtering itself, the handling of wpautop is pretty self-contained, and generally tolerant to adjustments in its own priority.

https://github.com/WordPress/wordpress-develop/blob/7b5d78f82f90a4a1274cba8bd078296461a6e768/src/wp-includes/blocks.php#L540-L545

This specific change also wouldn't have an impact so far as: Blocks filtering occurred before wpautop before these changes, and still occurs before wpautop after these changes. Specifically, wpautop happens at the default priority (10).

https://github.com/WordPress/wordpress-develop/blob/7b5d78f82f90a4a1274cba8bd078296461a6e768/src/wp-includes/default-filters.php#L175

Relevant to my previous comment:

All of this processing should be expected to occur before the default priority (10) since a filter at the default priority would expect to receive markup in roughly its "ready" state, at least so far as what is provided by core.

At first I wondered if this contradicted my statement. But before default-filters.php runs fairly early, even though it uses the same priority, since the filter is added early, it's likely still run long before any plugins would. Aside from the bugs and resulting changes here, it may have even been appropriate that do_blocks a few lines above be registered with the default priority as well, since the order of the filter adds would have largely emulated the same effect.

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm approving this PR as it solves the issue but I'd love more eyes from people more familiar with the hooks setup. Maybe @pento

@aduth
Copy link
Member

aduth commented Apr 8, 2020

I don't recall all of the specifics for why certain priorities were chosen.

In trying to sift through some history, I observe:

Thinking about the priorities in general:

  • All of this processing should be expected to occur before the default priority (10) since a filter at the default priority would expect to receive markup in roughly its "ready" state, at least so far as what is provided by core.
  • It is probably sensible that blocks are transformed before most other transforms (e.g. embeds, shortcodes), since those transforms may otherwise interfere with the block rendering

In fact, the implementation in Gutenberg prior to WordPress 5.0 seemed to go this direction, noted by the inline code comment of the filtering mentioned earlier.

A few things that are worth following-up on:

  • We should check on shortcode rendering, given the comment above about "before do_shortcode and oembed".
  • We should consider how this change would be ported to core, since that's where it is most relevant. I had seen that oembed filtering (class-wp-embed.php) is separate from the block filter (default-filters.php). It's useful here largely for testing as part of the end-to-end tests, and perhaps as a period of "beta" testing in the plugin.
  • Part of me wonders if it should be the embed block itself (through render_callback) that handles the behavior of its own embedding, rather than assume that expansion to occur as a separate filter. I'm not entirely sure if this would be enough to address the original issues.

@pento
Copy link
Member

pento commented Apr 9, 2020

This is an interesting problem, and a fairly neat demonstration of the inherent conflict between the (ideally) self-contained nature of blocks, and the historical handling of post_content as being a giant blob of HTML. In some ways, it's similar to the struggles WordPress had with complex shortcode handling.

I do not recommend changing either the priority or order of these filters. It will almost certainly break things out in the WordPress ecosystem, and it'll break them in wildly unexpected ways. Instead, I highly recommend taking a "lightest touch" approach when fixing issues related to filter ordering.

In this case, I think @aduth's suggestion of having the embed block handle its own embedding would address the original issues, and avoid having to re-order these filters. The WP_Embed::autoembed() method also runs as a filter on widget_text_content, so anything that hooks into filter within that method (or it's children) cannot assume it's running in the context of a post, or that it's only run once per page load.

Another method to explore, which is a little more complex than @aduth's suggestion, but will potentially give more reliable results, is to modify render_block_core_block() to run filters that were registered to run earlier in the_content than the current priority. This would catch WP_Embed::autoembed(), as well as any other missing filters.

@noisysocks noisysocks added [Feature] Synced Patterns Related to synced patterns (formerly reusable blocks) [Type] Bug An existing feature does not function as intended labels Apr 20, 2020
@earnjam
Copy link
Contributor Author

earnjam commented May 1, 2020

I'm going to close this PR in favor of pursuing the alternative options proposed above.

@earnjam earnjam closed this May 1, 2020
@earnjam earnjam deleted the try/reorder-content-hooks branch May 1, 2020 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Synced Patterns Related to synced patterns (formerly reusable blocks) [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reusable Block: Embed block only shows URL on frontend Embed block fails when converted to reusable
6 participants