Skip to content

Sharing Buttons block: Keep ids unique when the block shows more than once on a page - #53837

Merged
jeherve merged 2 commits into
trunkfrom
fix/sharing-buttons-duplicate-ids
Oct 20, 2026
Merged

jeherve merged 2 commits into
trunkfrom
fix/sharing-buttons-duplicate-ids

Conversation

@jeherve

@jeherve jeherve commented Oct 20, 2026

Copy link
Copy Markdown
Member

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" that save.jsx hard-codes. Each button's hidden label span is sharing-{service}-{postId}, so two copies for the same post share it too, and every link's aria-labelledby resolves 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:

  • The first list rendered on a page keeps jetpack-sharing-serivces-list. Later ones get a numbered suffix (jetpack-sharing-serivces-list-6).
  • Each button's label span and its link's aria-labelledby get a numbered suffix (sharing-facebook-5-2), so every link is labelled by its own span.
  • data-shared doesn't change. The legacy sharing.js looks 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:

  • A blog home or archive whose loop shows the full content of several posts that each contain the block.
  • A hooked block plus one the owner added to a template part or synced pattern; Hooked_Blocks::already_placed() doesn't look there.
  • An owner adding the block twice by hand.

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_Markup in packages/sharing-likes also mirrors save.jsx so 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-123 would no longer match. Scripts that look links up by data-shared, as sharing.js does, keep working.

Before, two blocks on one post:

<ul … id="jetpack-sharing-serivces-list">
  <a … data-shared="sharing-facebook-5" aria-labelledby="sharing-facebook-5"><span id="sharing-facebook-5" hidden>…
<ul … id="jetpack-sharing-serivces-list">
  <a … data-shared="sharing-facebook-5" aria-labelledby="sharing-facebook-5"><span id="sharing-facebook-5" hidden>…

After:

<ul … id="jetpack-sharing-serivces-list">
  <a … data-shared="sharing-facebook-5" aria-labelledby="sharing-facebook-5-2"><span id="sharing-facebook-5-2" hidden>…
<ul … id="jetpack-sharing-serivces-list-6">
  <a … data-shared="sharing-facebook-5" aria-labelledby="sharing-facebook-5-4"><span id="sharing-facebook-5-4" hidden>…

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 running a block theme (Twenty Twenty-Five works), with the Sharing module off:

  • Create two posts that each contain a Sharing Buttons block with a couple of services, and add a second Sharing Buttons block to one of them.
  • View that post and look at the page source. The first list has id="jetpack-sharing-serivces-list", the second has jetpack-sharing-serivces-list- plus a number, and each link's aria-labelledby matches the id of the hidden span inside it. Both links for the same service have the same data-shared.
  • View the blog home. Twenty Twenty-Five shows full post content there, so you get one list per block, and no id appears twice.
  • Add a Sharing Buttons block to the Footer template part in the Site Editor, then hook one after the content: 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.
  • Click a few buttons. The share windows still open, and the "Share" button, if you added one, still copies the link.
  • Turn the Sharing module on as well (wp jetpack module activate sharedaddy) and view the post again: the buttons still work alongside the legacy ones.

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.
@jeherve jeherve added Bug When a feature is broken and / or not performing as intended [Focus] Accessibility Improving usability for all users (a11y) [Status] Needs Review This PR is ready for review. [Block] Sharing Buttons [Tests] Includes Tests labels Oct 20, 2026
@jeherve jeherve self-assigned this Oct 20, 2026
@github-actions github-actions Bot added [Block] Sharing Button [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ labels Oct 20, 2026
@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

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:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

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.

@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack), and enable the fix/sharing-buttons-duplicate-ids branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack fix/sharing-buttons-duplicate-ids

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@jp-launch-control

Copy link
Copy Markdown

Code Coverage Summary

Coverage changed in 3 files.

File Coverage Δ% Δ Uncovered
projects/plugins/jetpack/extensions/blocks/sharing-button/class-sharing-source-block.php 0/313 (0.00%) 0.00% -1 💚
projects/plugins/jetpack/extensions/blocks/sharing-buttons/render.php 9/12 (75.00%) 75.00% -2 💚
projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php 73/114 (64.04%) 58.68% -65 💚

Full summary · PHP report · JS report

@jeherve
jeherve merged commit 5eef1e7 into trunk Oct 20, 2026
87 of 88 checks passed
@jeherve
jeherve deleted the fix/sharing-buttons-duplicate-ids branch October 20, 2026 11:24
@github-actions github-actions Bot added this to the jetpack/16.4 milestone Oct 20, 2026
@github-actions github-actions Bot removed the [Status] Needs Review This PR is ready for review. label Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Sharing Button [Block] Sharing Buttons Bug When a feature is broken and / or not performing as intended [Focus] Accessibility Improving usability for all users (a11y) [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Tests] Includes Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant