Repository navigation
Sharing & Likes: Hook the Sharing Buttons and Like blocks before or after the post content - #53413
Conversation
Owners will choose where the Sharing Buttons and Like blocks go in block theme templates: before or after the post content of single posts and pages. Template_Placements owns the two options holding that choice, and is the one writer the upcoming REST settings will save through. jetpack_sharing_buttons_auto_add keeps the name the plugin's unfinished auto-add code read as a boolean, so a truthy scalar still means "after the content". jetpack_likes_auto_add is new. Both hold a list, leaving room for placements other than the post content later, but at most one side of it: core inserts a block hooked both before and after the same anchor only before it, because the "before" pass adds the type to the anchor's ignoredHookedBlocks and the "after" pass then skips it. "After" wins, as the legacy value meant.
…post content Today the Sharing settings send owners of block themes to the Site Editor to add the blocks themselves, leaving them to find the right template and the right spot in it. With the Block Hooks API, the blocks appear wherever Template_Placements asks for them, as real blocks the owner can move or remove in the Site Editor. The blocks go before or after core/post-content in the single post and page templates (single, single-post*, page, page-*, singular, and theme custom templates declared for posts or pages), and in patterns declared for those template types. Other patterns fall back to the current request, as Newsletter's Subscribe placement does. The hooks apply only where both blocks can load and the legacy buttons are gone, so visitors never get both. They don't ask the block registry: like and sharing-buttons are registered lazily on first render on the front end, after core has built the template. Environment::jetpack_blocks_load() checks the conditions Jetpack_Gutenberg::should_load() uses instead, minus its filter. Sharing Buttons carry over the site's visible legacy services and button style where the block supports them, with labels, so the default style doesn't print raw service slugs. The hooked_block_types filter runs at priority 5 so both blocks stay ahead of Subscribe after the content on Simple too, where this package is set up after Subscribe registers its filter.
The block file added the Sharing Buttons block after the post content of single and page templates when jetpack_sharing_buttons_auto_add was set. Nothing ever wrote that option, so the code never ran, and the Sharing & Likes package now hooks the block (and the Like block) from the same option, read as a list. Leaving both in place would hook the block twice once the option is set, and the old Simple check read global['show'] in a way that ignores the "Switch to the block" route.
|
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. |
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
Code Coverage SummaryCoverage changed in 4 files.
4 files are newly checked for coverage.
|
…mplate-placements
The removed code never ran, since nothing ever wrote the option it read, so site owners have nothing to read about. The changelog check still wants a file for every project a PR touches, so the entry stays with a comment and no text, which keeps it out of CHANGELOG.md and readme.txt.
It described what the template placement gate does not check, which reads as more confusing than helpful in a guide. The reason the gate skips the block registry stays where the gate lives, in the docblock of Hooked_Blocks::blocks_can_load().
Review follow-ups for the template placements: - Skip a block the template, pattern or viewed post already holds, so sites that added Sharing Buttons by hand don't get a second copy. Theme patterns reach the filter before core reads their file, so read it there. - On the front end, give singular requests for other post types nothing: `single` and `singular` serve every post type. - Match `single-post-*` only when no `post-*` post type owns the slug, match pattern template types like template slugs, and place the blocks in custom templates that declare no post types while a post or page is viewed. - Carry over the services behind "More" when none are visible, and leave the `official` style out: the block accepts it but has no styles for it.
The package spelled out `jetpack/sharing-buttons`, `jetpack/sharing-button` and `jetpack/like` in Hooked_Blocks, Sharing_Buttons_Markup, Environment and across the tests. A single Block_Names class now holds them, so every caller reads the same constant.
…blocks
The template placements only hook the Sharing Buttons and Like blocks
into single views of posts and pages. A site that wants them on a custom
post type had no way in: its `single-{type}` template didn't match, and
the front-end guard hid the blocks from its views served by `single`.
jetpack_sharing_likes_template_placement_post_types returns the list,
posts and pages by default. It drives the front-end guard, which
`single-{type}` (and one post's `single-{type}-{slug}`) templates count,
custom templates declared for those types, and the request-time fallback
for patterns and for custom templates that declare no post types.
`page` templates only count while pages are listed, and `single` only
while a type other than pages is. An empty list places nothing, and a
value that isn't an array falls back to the default.
…mplate-placements
Environment::jetpack_blocks_load() copied Jetpack_Gutenberg::should_load() but checked the raw connection and skipped its jetpack_gutenberg filter. It said yes on two kinds of sites where no Jetpack block registers: - a site that turns the blocks off with that filter; - an Atomic site whose connection has no owner, where wpcomsh filters jetpack_is_connection_ready off. Both got an empty group, and for sharing an empty list, where the buttons should be, plus "unsupported block" placeholders in the Site Editor. Apply both filters as the plugin does. The jetpack_gutenberg filter can turn the blocks on too, so it now does here as well.
…nt end On WordPress.com Simple, Initializer::init() only ran from sharing_admin_init() in modules/sharedaddy/sharing.php. wpcom loads that file through admin-plugins, which only load for wp-admin and public-api requests, so on a page view the Hooked_Blocks filters were never registered. The Site Editor showed the hooked blocks, but live posts never got them until the owner saved the template. The Sharing Buttons block is registered eagerly wherever Jetpack's blocks load, the same condition the placements need, so register the filters from there on Simple. Self-hosted and Atomic sites keep getting them from load-jetpack.php, and registering them twice is harmless.
…ocks
Review follow-ups for the template placements:
- Keep a block out of a post that keeps it out of the legacy buttons:
its own Sharing or Likes switch, and a password for the Like block.
On Simple, a post that switched Likes on still shows the legacy
button after Likes go off sitewide, so it got two Like buttons;
leave the block out there.
- Copy the post content's wide or full alignment to the wrapper group,
with the class the group saves. Without it, designs like Twenty
Twenty-Five's left-aligned single post put the buttons in a centred
column about 350px from the text.
- Once the front end has picked a template, require a singular view of
a listed post type, so a pattern typed for both archives and single
posts no longer adds the blocks to every post in an archive.
- Give `single-{type}-{slug}` to the longest post type name that fits.
`single-post-review-great-book` belonged to `post` when a
`post-review` post type existed, so the Site Editor showed the blocks
in that post-review's template.
- Test the Like block's wrapper and the `twitter` alias, and make the
posts-and-pages fallback row fail when the fallback is gone.
…he block Simple has no Sharing module to turn off, so Feature_Actions switches it to the block by emptying both sharing-services lists. The hooked Sharing Buttons block builds its buttons from those lists, so on Simple it always found none and fell back to Facebook, X and Mastodon, whatever the site had set up. Save a copy of the lists, not autoloaded, before emptying them, and read it when the live lists give the block nothing. A site that never saved its services keeps no copy and still gets the block's defaults.
A template made from the post editor lists no post types, so only the request could say whether it served one of ours. The front end added the blocks while a post was viewed, but the Site Editor's REST request never views one, so the owner could neither see the blocks there nor remove them: core only records a removed hooked block it inserted. Give those templates nothing, as Newsletter's Subscribe block does by matching only the `single` template. Posts on such a template lose the buttons, but the front end and the Site Editor always agree. Theme custom templates that list their post types are unchanged.
Fixes CM-972
Proposed changes
On block themes, the Sharing settings send owners to the Site Editor to add the Sharing Buttons and Like blocks themselves. This adds the PHP half of doing it for them: two options say where each block goes, and the Block Hooks API puts it there, before or after the post content of single posts and pages.
jetpack_sharing_buttons_auto_addandjetpack_likes_auto_addeach hold a list:before_contentorafter_content.Template_Placementsowns both and is their only writer; the REST settings (CM-975) will save through it. The Sharing option reuses the name the plugin's unfinished auto-add code read as a boolean, so a truthy old value still means "after the content".Hooked_Blockshooksjetpack/sharing-buttonsandjetpack/likenext tocore/post-contentin thesingle,single-post*,page,page-*andsingulartemplates, in theme custom templates declared for posts or pages, and in patterns declared for those template types. Template parts, post content and custom templates that declare no post types get nothing.Feature_Actionskeeps a copy first, and the block reads from it.Jetpack_Gutenberg::should_load()decides: the Blocks module with a ready connection or offline mode, then thejetpack_gutenbergfilter. On Simple, the first part always holds. On top of that, the Sharing Buttons block needs the Sharing module off or every service removed; the Like block needs the Likes module off (or switched to the block on Simple) on a site that can show Likes at all.sharing.phponly sets the package up on admin and API requests, so the Sharing Buttons block's registration sets up the hooked blocks on Simple as well. Without it, they showed in the Site Editor but never on live posts.extensions/blocks/sharing-button/sharing-button.phpgoes. Nothing ever wrote its option, so it never ran.There's no UI yet: the cards that set these options come in CM-976, on the React screen, which is still opt-in. Until then, WP-CLI is the only way in :)
Screenshots
Twenty Twenty-Five, with the Like block set before the content and the Sharing Buttons block after it:
The same blocks in the Single Posts template in the Site Editor, where they can be moved or removed:
Both blocks after the content, with Newsletter's "Subscribe block at the end of each post" on:
Rationale
One side of the content per block. Core can't hook the same block type both before and after one anchor in a template. Theme templates, saved templates and patterns are built with
insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata(), which records the type in the anchor'signoredHookedBlocksright after thebeforepass, so theafterpass skips it. Each option therefore holds at most one side; given both, it keepsafter_content. Two different blocks on different sides work fine.No block registry check.
likeandsharing-buttonsare lazy-loaded on the front end since #49840: they register when they first render, after core has built the template. Like Subscribe's own placement, the callbacks check the conditions the blocks load under instead, and the lazy loader registers the inserted blocks as they render. Both blocks stay lazy. The trade-off: a site that drops one of these two withjetpack_set_available_extensionsstill gets them hooked.Filters on every request, options read in the callbacks. WordPress.com REST requests switch blogs after
init, which is why Newsletter defers its setup there. Reading everything inside the callbacks avoids that, and nothing is memoised per site. Thehooked_block_typescallback runs at priority 5, because Simple sets this package up after Subscribe registers its own filter at 10.No blocks in custom templates that declare no post types. A template made from the post editor doesn't say which post types it's for, so only the request could tell. The front end would add the blocks while a post is viewed, but the Site Editor never views one, so the owner could neither see them there nor remove them. Those templates get nothing instead, as with Subscribe, which only matches the
singletemplate. Posts on such a template lose the buttons, but the front end and the Site Editor agree.Known gap. On a theme that puts
core/post-contentin a pattern without declaring its template types, the blocks show on the front end but not in the Site Editor, and saving that template there turns them off for good. The default themes don't do this. The package's AGENTS.md documents it, along with the core behaviours above.Related product discussion/links
Does this pull request change what data or activity we track or use?
No.
Testing instructions
On a connected Jetpack site (a JN site works) running Twenty Twenty-Five, with the Sharing and Likes modules off:
'["after_content"]', and turn on Jetpack > Newsletter > "Subscribe block at the end of each post". After the content, the order should be Sharing Buttons, Like, Subscribe.wp post meta update <post-id> sharing_disabled 1, and Likes off on another withwp post meta update <post-id> switch_like_status 0. Each should lose that block only.jetpack_sharing_buttons_auto_addas above. A single post should show the Sharing Buttons block on the front end, with LinkedIn and Reddit.wp-content/debug.logshould list neither file: