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

Sharing Block: automatically add block to templates in block themes #35542

Merged
merged 8 commits into from
Feb 22, 2024

Conversation

jeherve
Copy link
Member

@jeherve jeherve commented Feb 8, 2024

Important

This is an experiment to play with the Block Hooks API. It can only work with recent changes made to WP Core, and scheduled to ship in WordPress 6.5 next month.

Proposed changes:

Under a few specific conditions, we want to automatically add a sharing buttons block (and 3 inner blocks, Facebook, X, and Mastodon) to the bottom of the post content.
Site owners can then go to Appearance > Editor if they want to remove or customize that block.

For now, we're trying to automatically add the block right after the post content block, in post and page templates (that includes templates like page-wide, page-no-sidebar, etc).

Here are the conditions:

  • The site must be using a block theme.
  • An option (jetpack_sharing_buttons_auto_add) must be set. Later on we can add UI to set that option under Jetpack > Settings > Social.
  • The sharing module must be deactivated. We wouldn't want to automatically add sharing buttons to the post content twice.
  • On WordPress.com Simple, the module is always active. There, we check if sharing posts are enabled on single posts and / or pages, since that matches the template where we'd like to automatically add the block.

To do

  • This doesn't quite work just yet, I haven't been able to figure out how to insert inner blocks. The site editor currently shows a validation error when viewing the single post template:
  • We should figure out how this should play with the sharedaddy module. Should we look at sharedaddy settings like we do right now and base our response on that? Or should we not look at it at all? In this case, when should auto-addition be triggered on WordPress.com Simple, where the sharing module is always active?

Resources:

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Jetpack product discussion

  • p1707216098587689-slack-C065QF2R1B6

Does this pull request change what data or activity we track or use?

  • No

Testing instructions:

  • Start by installing the development version of WordPress. Alternatively, launch a quick test site via this link.
  • Go to Appearance > Themes and install a block-based theme.
  • Go to Jetpack > Settings > Sharing and ensure that the Sharing buttons module is off.
  • Via WP CLI, set the jetpack_sharing_buttons_auto_add option: wp option add jetpack_sharing_buttons_auto_add true
  • Visit a single post on the frontend
    • Sharing buttons (Facebook, X, Mastodon) should be added below the post content.

Under a few specific conditions, we want to automatically add a sharing buttons block (and 3 inner blocks, Facebook, X, and Mastodon) to the bottom of the post content.
Site owners can then go to Appearance > Editor if they want to remove or customize that block.

For now, we're trying to automatically add the block right after the post content block, in post and page templates (that includes templates like `page-wide`, `page-no-sidebar`, etc).

Here are the conditions:

- The site must be using a block theme.
- An option (`jetpack_sharing_buttons_auto_add`) must be set. Later on we can add UI to set that option under Jetpack > Settings > Social.
- The sharing module must be deactivated. We wouldn't want to automatically add sharing buttons to the post content twice.
- On WordPress.com Simple, the module is always active. There, we check if sharing posts are enabled on single posts and / or pages, since that matches the template where we'd like to automatically add the block.

Resources:

- https://make.wordpress.org/core/2023/10/15/introducing-block-hooks-for-dynamic-blocks/
- https://github.com/ockham/like-button
- https://core.trac.wordpress.org/ticket/59572
- https://core.trac.wordpress.org/ticket/60126

Internal references:

- p1707216098587689-slack-C065QF2R1B6
@jeherve jeherve added [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Feature] Sharing Post sharing, sharing buttons [Status] In Progress [Status] Proposal [Pri] Normal [Block] Sharing Buttons [Block] Sharing Button labels Feb 8, 2024
@jeherve jeherve self-assigned this Feb 8, 2024
Copy link
Contributor

github-actions bot commented Feb 8, 2024

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 WordPress.com Simple 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, and enable the add/hook-sharing-block branch.

  • To test on Simple, run the following command on your sandbox:

    bin/jetpack-downloader test jetpack add/hook-sharing-block
    

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

@github-actions github-actions bot added the [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ label Feb 8, 2024
Copy link
Contributor

github-actions bot commented Feb 8, 2024

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 Team 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 🤖


The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available.


Once your PR is ready for review, check one last time that all required checks appearing at the bottom of this PR are passing or skipped.
Then, add the "[Status] Needs Team Review" label and ask someone from your team review the code. Once reviewed, it can then be merged.
If you need an extra review from someone familiar with the codebase, you can update the labels from "[Status] Needs Team Review" to "[Status] Needs Review", and in that case Jetpack Approvers will do a final review of your PR.


Jetpack plugin:

The Jetpack plugin has different release cadences depending on the platform:

  • WordPress.com Simple releases happen daily.
  • WoA releases happen weekly.
  • Releases to self-hosted sites happen monthly. The next release is scheduled for March 5, 2024 (scheduled code freeze on March 4, 2024).

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

@jeherve jeherve requested review from andrii-lysenko and a team February 8, 2024 17:15
@jeherve jeherve added [Status] Needs Team Review Obsolete. Use Needs Review instead. and removed [Status] In Progress labels Feb 8, 2024
Copy link
Contributor

@andrii-lysenko andrii-lysenko left a comment

Choose a reason for hiding this comment

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

Thanks for working on this. This is a great POC and works as described.

I understand that it's not a ready solution yet, just wanted to point out what we also should consider in the future.

  • We probably need to consider if Sharing Buttons already integrated as a part of Post template and not insert it if it is.
  • We also won't need to add these if we add the block manually to the page.
  • It would be great to see these buttons in editor as well, so they could be adjusted if needed.

andrii-lysenko
andrii-lysenko previously approved these changes Feb 13, 2024
Copy link
Contributor

@andrii-lysenko andrii-lysenko left a comment

Choose a reason for hiding this comment

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

Oh, I just got it completely wrong. We actually do change the template of the Post. I it's a great way to do this. 👍

@jeherve
Copy link
Member Author

jeherve commented Feb 21, 2024

I think this is ready to go now. It won't appear for anyone since it relies on the jetpack_sharing_buttons_auto_add option that you can only set via CLI today, but it will allow us to start testing, and then to develop an interface for it in the Sharing settings screen in the Jetpack dashboard.

Copy link
Contributor

@andrii-lysenko andrii-lysenko left a comment

Choose a reason for hiding this comment

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

I agree that it's safe to move forward as it won't actually show unless option is set tu true.
Let's merge it so we would be able to iterate on this.

@jeherve jeherve merged commit 2f218fa into trunk Feb 22, 2024
56 checks passed
@jeherve jeherve deleted the add/hook-sharing-block branch February 22, 2024 14:54
@github-actions github-actions bot removed [Status] Needs Team Review Obsolete. Use Needs Review instead. [Status] Needs Review This PR is ready for review. labels Feb 22, 2024
@github-actions github-actions bot added this to the jetpack/13.2 milestone Feb 22, 2024
jeherve added a commit that referenced this pull request Feb 23, 2024
Follow-up to #35542

Core recently changed the definition of the filter we're using to automatically add the sharing block to post templates. As a result, our code is not compatible with WordPress 6.4 anymore:

```
PHP Fatal error:  Uncaught ArgumentCountError: Too few arguments to function Automattic\Jetpack\Extensions\Sharing_Button_Block\add_default_services_to_block(), 4 passed in /var/www/html/wp-includes/class-wp-hook.php on line 324 and exactly 5 expected in /usr/local/src/jetpack-monorepo/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php:254
```

Let's gate that functionality to WordPress 6.5+ to avoid that error.
jeherve added a commit that referenced this pull request Feb 23, 2024
Follow-up to #35542

Core recently changed the definition of the filter we're using to automatically add the sharing block to post templates. As a result, our code is not compatible with WordPress 6.4 anymore:

```
PHP Fatal error:  Uncaught ArgumentCountError: Too few arguments to function Automattic\Jetpack\Extensions\Sharing_Button_Block\add_default_services_to_block(), 4 passed in /var/www/html/wp-includes/class-wp-hook.php on line 324 and exactly 5 expected in /usr/local/src/jetpack-monorepo/projects/plugins/jetpack/extensions/blocks/sharing-button/sharing-button.php:254
```

Let's gate that functionality to WordPress 6.5+ to avoid that error.
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 [Feature] Sharing Post sharing, sharing buttons [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Pri] Normal [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants