Skip to content

Post Media: Add Shortcodes class for shortcode ID extraction#47200

Merged
jeherve merged 2 commits intotrunkfrom
jeherve/shortcode-extractors
Feb 19, 2026
Merged

Post Media: Add Shortcodes class for shortcode ID extraction#47200
jeherve merged 2 commits intotrunkfrom
jeherve/shortcode-extractors

Conversation

@jeherve
Copy link
Member

@jeherve jeherve commented Feb 18, 2026

Fixes CM-544

Note

This only adds the functions to the package, it doesn't replace their usage across the codebase. I'll do that in a follow-up PR.
That means we only need to check that the CI checks pass in this PR.

Proposed changes:

  • Add a new Shortcodes class to the post-media package that consolidates shortcode ID extraction logic currently scattered across the Jetpack plugin codebase.
  • The class provides static methods for extracting media identifiers from shortcode attributes for: YouTube, Vimeo, TED, VideoPress (wpvideo/videopress), Hulu, and Archive.org (including books).
  • Includes a sanitize_youtube_url() helper that normalizes YouTube URLs (youtu.be, /v/, /shorts/, encoded ampersands, playlists).

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

n/a

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

No.

Testing instructions:

  • This PR adds new code that is not yet used anywhere — it only introduces the new class and tests. Testing if CI is happy should be enough.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 18, 2026

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!

@github-actions github-actions bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Feb 18, 2026
@jp-launch-control
Copy link

jp-launch-control bot commented Feb 18, 2026

Code Coverage Summary

1 file is newly checked for coverage.

File Coverage
projects/packages/post-media/src/class-shortcodes.php 59/60 (98.33%) 💚

Full summary · PHP report

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces a new Automattic\Jetpack\Shortcodes utility in the jetpack-post-media package to centralize extraction of media identifiers from shortcode attributes (e.g., YouTube, Vimeo, TED, VideoPress, HTML5 audio/video, Hulu, Archive.org), along with PHPUnit coverage and a changelog entry.

Changes:

  • Added Shortcodes class with static helper/extractor methods (including YouTube URL normalization).
  • Added PHPUnit tests covering supported shortcode formats and edge cases.
  • Added a post-media package changelog entry documenting the addition.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
projects/packages/post-media/src/class-shortcodes.php Adds the new Shortcodes class with media ID extraction and YouTube URL normalization logic.
projects/packages/post-media/tests/php/Shortcodes_Test.php Adds unit tests for the new extraction/normalization methods.
projects/packages/post-media/changelog/jeherve-shortcode-extractors Records the new functionality in the package changelog.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@jeherve jeherve added [Status] In Progress [Pri] Normal Code Quality 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 Feb 18, 2026
@jeherve jeherve self-assigned this Feb 18, 2026
Copilot AI review requested due to automatic review settings February 18, 2026 16:58
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI review requested due to automatic review settings February 18, 2026 17:17
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI review requested due to automatic review settings February 18, 2026 17:49
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@jeherve jeherve force-pushed the jeherve/shortcode-extractors branch from 52422fe to 131a3f8 Compare February 18, 2026 18:01
…ode attributes

Consolidate scattered shortcode ID extraction functions into a single
Shortcodes class in the post-media package. Methods cover YouTube,
Vimeo, TED, VideoPress (wpvideo and videopress), HTML5 video, audio,
Hulu, and Archive.org (including books). Includes comprehensive tests
(69 test cases) ported from existing tests and new coverage for
previously untested extraction logic.
Copilot AI review requested due to automatic review settings February 18, 2026 18:08
@jeherve jeherve force-pushed the jeherve/shortcode-extractors branch from 131a3f8 to 03b8dbf Compare February 18, 2026 18:08
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@jeherve jeherve requested a review from a team February 18, 2026 18:25
@jeherve jeherve added the [Status] Needs Review This PR is ready for review. label Feb 18, 2026
allilevine
allilevine previously approved these changes Feb 18, 2026
Copy link
Member

@allilevine allilevine left a comment

Choose a reason for hiding this comment

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

CI passes and the extraction looks good ✅

Co-authored-by: Allison Levine <1689238+allilevine@users.noreply.github.com>
Copilot AI review requested due to automatic review settings February 19, 2026 14:33
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

* https://vimeo.com/album/1234567/video/7654321
* https://player.vimeo.com/video/18427511
*/
$pattern = '/(?:https?:\/\/)?vimeo\.com\/(?:groups\/[^\/]+\/videos\/|album\/\d+\/video\/|video\/|channels\/[^\/]+\/videos\/|[^\/]+\/)?([0-9]+)(?:[^\'\"0-9<]|$)/i';
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

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

The regex pattern does not match player.vimeo.com URLs, but based on the test case at line 197-200 of the test file and the documentation comment at lines 145, it should. The pattern should be updated to include an optional subdomain match before vimeo.com. Consider changing the pattern to include (?:[^.]+\.)? before vimeo\.com to match domains like player.vimeo.com.

Suggested change
$pattern = '/(?:https?:\/\/)?vimeo\.com\/(?:groups\/[^\/]+\/videos\/|album\/\d+\/video\/|video\/|channels\/[^\/]+\/videos\/|[^\/]+\/)?([0-9]+)(?:[^\'\"0-9<]|$)/i';
$pattern = '/(?:https?:\/\/)?(?:[^.]+\.)?vimeo\.com\/(?:groups\/[^\/]+\/videos\/|album\/\d+\/video\/|video\/|channels\/[^\/]+\/videos\/|[^\/]+\/)?([0-9]+)(?:[^\'\"0-9<]|$)/i';

Copilot uses AI. Check for mistakes.
}

$url = trim( $url, ' "' );
$url = trim( $url );
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

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

There are two consecutive trim calls where the second one is redundant. Line 55 already trims spaces (among other characters), so the second trim on line 56 has no effect. Consider combining these into a single call: trim($url, ' "') which will trim both spaces and quotes.

Suggested change
$url = trim( $url );

Copilot uses AI. Check for mistakes.
@jeherve jeherve merged commit bc3da94 into trunk Feb 19, 2026
74 checks passed
@jeherve jeherve deleted the jeherve/shortcode-extractors branch February 19, 2026 15:38
@github-actions github-actions bot removed the [Status] Needs Review This PR is ready for review. label Feb 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants