Repository navigation
Sharing Buttons block: Keep ids unique when the block shows more than once on a page - #53837
Conversation
Fixes CM-974 A page that renders the block more than once repeats two ids: the `jetpack-sharing-serivces-list` id that save.jsx hard-codes on the list, and each button's `sharing-{service}-{postId}` label id, which its link's aria-labelledby points at. That happens on a blog home showing the full content of several posts that each hold the block, when an owner adds one to a template part or synced pattern on top of a hooked placement (Hooked_Blocks::already_placed() can't see those), or when the block is placed twice by hand. Duplicate ids are invalid HTML, and a link could end up labelled by another instance's span; it only reads right today because both spans hold the same text. Rewrite both at render time and leave the saved markup alone, since changing save.jsx needs a block deprecation (#45075 reverted a typo fix to this id for that reason): - The first list rendered in a request keeps the saved id, in case site CSS targets it; later ones get a wp_unique_id() suffix. - Each button's label span and aria-labelledby get a wp_unique_id() suffix. data-shared keeps its per-post value, because the legacy sharing.js looks links up by it to show share counts.
|
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 3 files.
|
Fixes CM-974
Proposed changes
When a page shows the Sharing Buttons block more than once, its ids repeat. Every list carries the
id="jetpack-sharing-serivces-list"thatsave.jsxhard-codes. Each button's hidden label span issharing-{service}-{postId}, so two copies for the same post share it too, and every link'saria-labelledbyresolves to the first one on the page. Duplicate ids are invalid HTML. The labels only read correctly today because both spans hold the same text.This PR makes them unique at render time:
jetpack-sharing-serivces-list. Later ones get a numbered suffix (jetpack-sharing-serivces-list-6).aria-labelledbyget a numbered suffix (sharing-facebook-5-2), so every link is labelled by its own span.data-shareddoesn't change. The legacysharing.jslooks links up by it to show share counts.Pages that can still show the block twice, now that #53413 places it on one side of the content only:
Hooked_Blocks::already_placed()doesn't look there.Rationale
At render time, not in
save.jsx. Changing the saved markup needs a block deprecation; #45075 reverted a typo fix to this id for that reason.Sharing_Buttons_Markupinpackages/sharing-likesalso mirrorssave.jsxso hooked blocks stay valid in the editor, and that stays as it is too.The first list keeps its id. Nothing in the monorepo reads
jetpack-sharing-serivces-list, but site CSS might. Keeping it on the first list means a page with a single block keeps the list id it always had; only the repeats change.Label ids change on every instance. The old
sharing-{service}-{postId}span id is gone, even on a single block. Nothing in Jetpack reads it, but custom CSS or JS aimed at#sharing-facebook-123would no longer match. Scripts that look links up bydata-shared, assharing.jsdoes, keep working.Before, two blocks on one post:
After:
Related product discussion/links
save.jsxkeeps its idDoes this pull request change what data or activity we track or use?
No.
Testing instructions
On a connected Jetpack site running a block theme (Twenty Twenty-Five works), with the Sharing module off:
id="jetpack-sharing-serivces-list", the second hasjetpack-sharing-serivces-list-plus a number, and each link'saria-labelledbymatches theidof the hidden span inside it. Both links for the same service have the samedata-shared.wp option update jetpack_sharing_buttons_auto_add '["after_content"]' --format=json. View a post that has no block of its own. The hooked block after the content keeps the plain id, the footer one gets the suffix.wp jetpack module activate sharedaddy) and view the post again: the buttons still work alongside the legacy ones.