Skip to content

Jetpack Shortcodes: Prevent hooking onto pre_kses in front-end #44373

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

Open
wants to merge 18 commits into
base: trunk
Choose a base branch
from

Conversation

dilirity
Copy link
Member

@dilirity dilirity commented Jul 18, 2025

Fixes HOG-132

pre_kses runs everywhere. However, shortcodes have code that "preserves" content (mostly embeds) by transforming it into a shortcode string. This code is necessary only when the post content is updated/added. It's not necessary in the front-end.

This PR adds a check to conditionally allow hooking onto pre_kses. To avoid calling the is_frontend of Request many times, I created a helper function with a static variable. I thought it's preferable to calling is_frontend every time a shortcode hooks onto pre_kses.

It also updates the is_frontend method to allow not sending the Vary header. I didn't want to cause side-effects from just checking if we're in the front-end multiple times throughout Jetpack.

Proposed changes:

  • Update shortcode "preserve" functionality, to not be hooked onto pre_kses for front-end requests;
  • Update Request::is_frontend method to allow not sending the Vary header;
  • Fix variable type in PHPdoc.

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

HOG-132

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

no

Testing instructions:

  • Make sure the Shortcode embeds module in Jetpack is enabled:
CleanShot 2025-07-18 at 15 54 39@2x
  • The easies way to test this is to pick a shortcode and take the example code from the top of the file and use it to make sure the code works.
  • Let's take archiveorg-book.php for example:
<iframe src="https://www.archive.org/stream/goodytwoshoes00newyiala?ui=embed#mode/1up" width="480px" height="430px" frameborder="0" ></iframe>
  • On your test website, create a user with the Contributor role.
  • Create a post and paste this into the content field. When you submit the post for review, the iframe should get transformed into a shortcode like this:
[archiveorg id=goodytwoshoes00newyiala width=480 height=430]
  • That means that the "preserve" functionality is working.
  • You can test this with other shortcodes, but the idea remains the same.
  • Other than this, make sure there are no fatal errors

@dilirity dilirity requested a review from kraftbj July 18, 2025 12:57
@dilirity dilirity self-assigned this Jul 18, 2025
@dilirity dilirity added [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Status] Needs Review This PR is ready for review. labels Jul 18, 2025
Copy link
Contributor

github-actions bot commented Jul 18, 2025

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 or WordPress.com Site Helper), and enable the update/jetpack/modules/shortcodes/conditionally-hook-on-pre-kses branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack update/jetpack/modules/shortcodes/conditionally-hook-on-pre-kses
bin/jetpack-downloader test jetpack-mu-wpcom-plugin update/jetpack/modules/shortcodes/conditionally-hook-on-pre-kses

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 [Feature] Shortcodes / Embeds [Package] Status [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ labels Jul 18, 2025
Copy link
Contributor

github-actions bot commented Jul 18, 2025

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 a "[Type]" label (Bug, Enhancement, Janitorial, Task).
  • ✅ 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:

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

  • WordPress.com Simple releases happen as soon as you deploy your changes after merging this PR (PCYsg-Jjm-p2).
  • WoA releases happen weekly.
  • Releases to self-hosted sites happen monthly:
    • Scheduled release: August 5, 2025
    • Code freeze: August 4, 2025

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

Copy link

jp-launch-control bot commented Jul 18, 2025

Code Coverage Summary

Coverage changed in 20 files. Only the first 5 are listed here.

File Coverage Δ% Δ Uncovered
projects/plugins/jetpack/modules/shortcodes/class.filter-embedded-html-objects.php 0/140 (0.00%) -37.41% 53 💔
projects/plugins/jetpack/modules/shortcodes/dailymotion.php 97/178 (54.49%) -3.70% 7 💔
projects/plugins/jetpack/modules/shortcodes/archiveorg-book.php 23/54 (42.59%) -4.58% 3 ❤️‍🩹
projects/plugins/jetpack/modules/shortcodes/archiveorg.php 29/71 (40.85%) -3.44% 3 ❤️‍🩹
projects/plugins/jetpack/modules/shortcodes/crowdsignal.php 305/443 (68.85%) -0.61% 3 ❤️‍🩹

1 file is newly checked for coverage.

File Coverage
projects/plugins/jetpack/modules/shortcodes/shortcode-utils.php 5/6 (83.33%) 💚

Full summary · PHP report · JS report

Coverage check overridden by I don't care about code coverage for this PR Use this label to ignore the check for insufficient code coveage. .

@dilirity dilirity added the I don't care about code coverage for this PR Use this label to ignore the check for insufficient code coveage. label Jul 21, 2025
Copy link
Contributor

@kraftbj kraftbj left a comment

Choose a reason for hiding this comment

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

I didn't specifically test the code, but it reads well.

I left some comments on 188368-ghe-Automattic/wpcom to denote how to prepare WP.com for this (and after that, I believe the tests on this PR would pass).

Marking request changes since it needs the wp.com work done before merging, so will give a final approval after wp.com is prepared and those tests pass.

@kraftbj kraftbj added [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. and removed [Status] Needs Review This PR is ready for review. I don't care about code coverage for this PR Use this label to ignore the check for insufficient code coveage. labels Jul 21, 2025
@kraftbj
Copy link
Contributor

kraftbj commented Jul 21, 2025

Additionally, please add tests for the new function to ensure that it is true in a frontend condition and that the filter is not added in a frontend condition.

At least, add tests for the new function and get coverage good for the new file. Bonus for increasing the coverage in an existing file.

@dilirity dilirity added [Status] In Progress and removed [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. labels Jul 23, 2025
@dilirity dilirity requested a review from kraftbj July 23, 2025 13:49
@dilirity dilirity added [Status] Needs Review This PR is ready for review. and removed [Status] In Progress labels Jul 23, 2025
…es/shortcodes/conditionally-hook-on-pre-kses
kraftbj

This comment was marked as outdated.

@kraftbj kraftbj added the I don't care about code coverage for this PR Use this label to ignore the check for insufficient code coveage. label Jul 25, 2025
kraftbj

This comment was marked as duplicate.

Copy link
Contributor

@kraftbj kraftbj left a comment

Choose a reason for hiding this comment

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

Alright, some legit wp.com test failures (at least needing more investigation). Un-approving :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Shortcodes / Embeds I don't care about code coverage for this PR Use this label to ignore the check for insufficient code coveage. [Package] Status [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Status] Needs Review This PR is ready for review. [Tests] Includes Tests [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.

2 participants