Related Posts: enqueue block stylesheet on the block render path#50420
Related Posts: enqueue block stylesheet on the block render path#50420jeherve wants to merge 3 commits into
Conversation
The Related Posts block renders through its own block callback, but its frontend CSS was only enqueued as a side effect of the module's asset gate (enabled_for_request()), which on classic themes only fires on single posts. A block placed on a page (or any view the gate skips) therefore rendered as unstyled HTML. Enqueue the stylesheet in render_block() whenever the block outputs markup, so it is styled everywhere it can be used. enabled_for_request() is left untouched so the automatic the_content insertion stays scoped as in #39784 (no related posts auto-appended to classic-theme pages). Adds a regression test asserting the style is enqueued when the block renders without the module gate running.
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! Jetpack plugin: No scheduled milestone found for this plugin. If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack. |
Code Coverage SummaryCannot generate coverage summary while tests are failing. 🤐 Please fix the tests, or re-run the Code coverage job if it was something being flaky. |
Use the standard Type: bugfix changelog body instead of Comment: (which would ship an empty user-facing changelog line). Add test coverage pinning the fix's intent: the block render path enqueues the stylesheet but not the async script, and no style is enqueued when the block has no related posts to render.
Fixes JETPACK-1876
Proposed changes
The Related Posts block renders through its own block callback, but its frontend stylesheet was never enqueued by the block itself. The CSS (
modules/related-posts/related-posts.css) is only enqueued as a side effect of the module's front-end asset gate,enabled_for_request()— which on classic themes only fires on single posts (is_single()). So a Related Posts block placed on a page (or any view the gate skips) rendered itsjp-relatedposts-i2markup with no matching CSS: an unstyled bullet list with full-size images and visibledt/ddlabels.This enqueues the stylesheet inside
render_block(), right after we know the block will output markup, reusing the module's existingenqueue_assets( false, true )helper (handlejetpack_related-posts, including RTL + AMP handling). The block is now styled everywhere it can be placed, independent of theme type or the page/post distinction.Deliberately not changed:
enabled_for_request()is left untouched. That gate governs the automaticthe_contentinsertion of related posts and was intentionally scoped in #39784 to avoid auto-appending related posts to classic-theme pages (#39783). Widening it would re-introduce that bug. The fix is confined to the block render path — it only adds CSS to markup the user explicitly placed — so the auto-insertion behavior is unchanged. The enqueue is idempotent on single posts, where the module already enqueues the same handle.Related product discussion/links
Does this pull request change what data or activity we track or use?
No.
Testing instructions
Related Posts can render three ways (block,
[jetpack-related-posts]shortcode, automaticthe_contentinsertion) and behaves differently on classic vs. block themes, so please cover the matrix below. This fix only changes the block render path; the shortcode and automatic-insertion paths are intentionally unchanged.Setup
1. The fix — classic theme, block on a page
jetpack_related-postsstylesheet is present on the page.2. Block inserted via a Site Editor template (block theme)
This is a different insertion path than "block inside post content", and it's the path #39784 was tested against.
3. AMP
The AMP path is the only other caller of the changed method.
?ampon a supported setup), view a post that shows related posts.4. Regression checks (behavior should be unchanged)
jetpack_related-postsstylesheet on the page (no double-enqueue).[jetpack-related-posts]on a single post: still renders as before.5. Nice-to-have
jetpack_related-postsstylesheet is loaded.Automated:
jetpack docker phpunit jetpack -- --filter=Jetpack_RelatedPosts_Test→OK (5 tests, 8 assertions).