Shortcodes: fix [archiveorg] query parameter handling#49195
Conversation
Build the embed URL with add_query_arg so autoplay, poster, and the newly-supported playlist attribute render as a proper query string. Also accept query parameters baked into the identifier (either "?" or "&" separator) so existing posts continue to work.
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
|
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. |
add_query_arg() does not urlencode values, so the poster URL is embedded verbatim in the query string.
Code Coverage SummaryCoverage changed in 1 file.
|
Done. No issues. |
From the test instruction, |
When the_content encodes "&" to "&" before do_shortcode runs, the shortcode parser receives "myitem&playlist=1" and the split picks up "amp;playlist=1" as the trailing query string. Decode & back to & in the identifier before splitting, matching what the sibling jetpack_archiveorg_embed_to_shortcode() function already does.
|
Tested on Simple again with 7e51465 and this time everything worked as expeted. |
There was a problem hiding this comment.
Pull request overview
This PR fixes Archive.org shortcode embed URL generation so query parameters are appended as proper query strings, including newly supported playlist handling and legacy query parameters embedded in the identifier.
Changes:
- Builds Archive.org embed URLs with
add_query_arg(). - Adds support for
playlist=1as a shortcode attribute. - Adds PHPUnit coverage for query parameters supplied via attributes and positional identifiers.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
projects/plugins/jetpack/modules/shortcodes/archiveorg.php |
Updates Archive.org shortcode URL construction and query-parameter handling. |
projects/plugins/jetpack/tests/php/modules/shortcodes/Jetpack_Shortcodes_ArchiveOrg_Test.php |
Adds tests for autoplay/poster, playlist, and query strings embedded in identifiers. |
projects/plugins/jetpack/changelog/fix-archiveorg-shortcode-query-parameters |
Adds a Jetpack changelog entry for the shortcode fix. |
There was a problem hiding this comment.
Really nice fix. 🙌 Moving the URL construction to add_query_arg() is the right call, and the &-normalization is a thoughtful catch for the the_content-encodes-before-do_shortcode case. The PHPUnit coverage across all four scenarios is much appreciated too.
Just a few minor, non-blocking tweaks inline before merge. None are blockers and the core behavior is correct for the normal cases. I also double-checked the security side: with the hardcoded archive.org/embed/ host and the final esc_url(), there's no escape/XSS concern here, and poster is actually a touch safer than before.
| // jetpack_archiveorg_embed_to_shortcode() splits on "&" for the same reason. | ||
| $id = str_replace( '&', '&', $id ); | ||
| $extra_query = ''; | ||
| if ( preg_match( '/^([^?&]*)[?&](.*)$/', $id, $id_match ) ) { |
There was a problem hiding this comment.
Small edge case: the missing-ID guard above (if ( ! $atts['id'] )) runs before this split, so an identifier that's only a query string — e.g. [archiveorg ?playlist=1] or [archiveorg &playlist=1] — slips past it. After the split $id ends up empty and the output becomes https://archive.org/embed/?playlist=1 (an item-less embed) instead of the intended error comment. A quick '' === $id re-check right after the split would close it. Very minor since it only fires on malformed input.
There was a problem hiding this comment.
Good catch — addressed in 699ae7c with a '' === $id re-check right after the split, returning the same <!-- error: missing archive.org ID --> comment. Added a test that covers both ?playlist=1 and &playlist=1 shapes.
| $url = add_query_arg( $query_args, $url ); | ||
| } | ||
| if ( '' !== $extra_query ) { | ||
| $url .= ( str_contains( $url, '?' ) ? '&' : '?' ) . $extra_query; |
There was a problem hiding this comment.
Optional consolidation (fine for a follow-up): appending $extra_query raw after add_query_arg() means the two query sources can't merge or dedupe, which leaves a couple of edge cases:
- Duplicate keys when a param is supplied both ways:
[archiveorg myitem&playlist=1 playlist=1]→?playlist=1&playlist=1. - A
#in the id (myitem#frag&playlist=1) lands the params inside the fragment, so they never reach archive.org.
Running $extra_query through wp_parse_str() and merging it into $query_args before a single add_query_arg() call would resolve all of these in one path — add_query_arg() is fragment-aware, so it'd even place the query before any # — and the ?/& ternary could go away. Current behavior is correct for the common cases, so no need to block on this.
There was a problem hiding this comment.
Decided to fold this in now since the diff is fresh — 699ae7c parses the trailing query string via wp_parse_str() and merges it into a single add_query_arg() call. That dedupes duplicate keys (shortcode atts win), and add_query_arg()'s fragment-awareness puts the query before any #frag in the id. Tests added for both cases.
- Re-check that the id is non-empty after splitting off query parameters, so identifiers like "?playlist=1" or "&playlist=1" return the missing-ID error comment instead of producing an item-less embed URL. - Parse query parameters baked into the id via wp_parse_str() and merge them into a single add_query_arg() call, which dedupes duplicate keys and stays fragment-aware so "#frag" placed in the id no longer swallows the query string.
|
Thank you for your thorough review and comments, @kraftbj 🙌 |
PHPCS (MediaWiki.PHPUnit.AssertEquals.Int) prefers assertSame over assertEquals for non-numeric or strict equality checks.
Proposed changes
[archiveorg]embed URL withadd_query_arg()soautoplay,poster, and the newly-supportedplaylistattribute render as a proper?key=value&key=valuequery string instead of being concatenated into the URL path with stray&separators.[archiveorg myitem&playlist=1]or[archiveorg myitem?playlist=1]), splitting them off the id and re-appending them with the correct separator. This keeps existing posts working — including the?-prefix workaround that's been shared as guidance.Related product discussion/links
Does this pull request change what data or activity we track or use?
No.
Testing instructions
[archiveorg sentidodelobjeto&playlist=1 width=640 height=300][archiveorg sentidodelobjeto?playlist=1 width=640 height=300][archiveorg id=sentidodelobjeto playlist=1][archiveorg id=Wonderfu1958 autoplay=1 poster=https://archive.org/img.png]src:https://archive.org/embed/sentidodelobjeto?playlist=1and the playlist should display on archive.org's end.https://archive.org/embed/Wonderfu1958?autoplay=1&poster=....[archiveorg Wonderfu1958](no extras) still produceshttps://archive.org/embed/Wonderfu1958unchanged.jp docker phpunit jetpack -- --filter Jetpack_Shortcodes_ArchiveOrg_Test.