Repository navigation
Conversation
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
19297b2 to
121f67b
Compare
Introduce `wp_get_style_link_tag()` and `wp_print_style_link_tag()`, which build a `<link rel="stylesheet">` tag with `WP_HTML_Tag_Processor`, and the `wp_style_attributes` filter, so that attributes such as a CSP `nonce`, `integrity` or `crossorigin` can be added to stylesheet links without editing HTML strings. This complements `wp_get_script_tag()` and the `wp_script_attributes` filter, and `wp_get_inline_style_tag()` for inline STYLE tags. `WP_Styles::do_item()`, including right-to-left and alternate stylesheets, the concatenated stylesheet printed by `_print_styles()`, `wp_admin_css()` and `locale_stylesheet()` now print their LINK tags with the API. Backward compatibility: - The `style_loader_tag` and `wp_admin_css` filters are kept, and the markup they are passed is unchanged: the values Core supplies are still escaped with `esc_attr()` or `esc_url()`, single-quoted and in the same order, since callbacks have long edited that markup as a string. Attributes added by the `wp_style_attributes` filter are printed after them. - `style_loader_tag` is still passed the escaped URL from before the `wp_style_attributes` filter, and each URL is still escaped, and passed to the `clean_url` filter, once. - In the markup passed to `style_loader_tag` and `wp_admin_css`, a quote returned by a filtered escaping function, such as a `clean_url` callback, is encoded, so it can no longer end an attribute value early. Like `wp_get_script_tag()`, `wp_get_style_link_tag()` prints the `href` as the Tag Processor escapes it. - A supplied `id` cannot be changed or removed by the filter, since code locates LINK tags by it, such as the color scheme picker on the user profile screen with `colors-css`. A `_doing_it_wrong()` notice naming the function that printed the tag is issued if the filter tries. - `WP_Styles::do_item()` no longer calls `WP_Styles::_css_href()`, so overriding that method in a subclass no longer changes the printed URL. - The URL printed by `locale_stylesheet()` is now escaped. Attributes are printed in the order they are given. An attribute that the Tag Processor rejects, such as a URL with a disallowed protocol, is left out rather than printed empty. See #66342, #51325.
121f67b to
da52cb2
Compare
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Keep the new stylesheet link tag API, filter and backward compatibility tests, while retaining the Core removal of script and style concatenation (r64585). Drop the two tests for concatenated stylesheet output, which no longer exists.
Adds
wp_get_style_link_tag()andwp_print_style_link_tag(), which build a<link rel="stylesheet">tag withWP_HTML_Tag_Processor, and thewp_style_attributesfilter, so that attributes such as a CSPnonce,integrityorcrossorigincan be added to stylesheet LINK tags without editing HTML strings. This complementswp_get_script_tag()and thewp_script_attributesfilter, aswp_get_inline_style_tag()from #13250 complementswp_get_inline_script_tag(). The names follow the suggestion on Core-51325, where stylesheet LINK tags were noted as out of scope for that ticket.WP_Styles::do_item(), including right-to-left and alternate stylesheets,wp_admin_css()andlocale_stylesheet()print their LINK tags with the API.Stylesheet concatenation was removed from Core in r64585. This PR has been updated to the current
trunk, preserving that removal and dropping the_print_styles()integration and its two obsolete concatenation-specific tests.Backward compatibility
style_loader_tagandwp_admin_cssfilters are kept, and the markup they are passed is unchanged: the values Core supplies are still escaped withesc_attr()oresc_url(), single-quoted and in the same order, since callbacks have long edited it as a string, for example to replacerel='stylesheet'. Only attributes thatwp_style_attributesadds or changes are escaped by the Tag Processor, and added attributes are printed after Core's. Tests pin this with exact strings, including values thatesc_attr()and the Tag Processor escape differently, such as an apostrophe or a character reference in a title or handle, so the test for #30036 is kept as it was.true, or an array passed by mistake as the$mediaargument, is printed as before, rather than as a boolean attribute or a fatal error.style_loader_tagis still passed the escaped URL from beforewp_style_attributes, and each URL is still escaped, and passed to theclean_urlfilter, once. A right-to-left URL that escaping empties is still printed ashref=''.clean_urlcallback, is encoded in these tags, so it cannot end an attribute value early.idcannot be changed or removed bywp_style_attributes, as forwp_inline_style_attributesin Script Loader: Add an API for filtering inline style attributes #13250, since code locates LINK tags by it, such asuser-profile.jswith#colors-css. A_doing_it_wrong()notice that names the function which printed the tag is issued if a filter tries.WP_Styles::do_item()no longer callsWP_Styles::_css_href(), so overriding that method in a subclass no longer changes the printed URL. Its docblock says so.locale_stylesheet()is now escaped.Attribute escaping
Attribute values passed to
wp_style_attributesare not yet escaped, as forwp_script_attributes; the filter documentation says that the printed URL may differ (see Core-66308). As inwp_get_script_tag(), the Tag Processor prints thehrefofwp_get_style_link_tag()asesc_url()returns it, so aclean_urlcallback that returns a double quote can end that attribute early, as it can for a script'ssrc. Hardening that belongs inWP_HTML_Tag_Processor::set_attribute(), for every URL attribute, in a follow-up.For scripts, r56687 and r61485 changed the quoting and order of the markup passed to
script_loader_tag; styles keep the historical markup, so callbacks that edit it as a string keep working.Related PRs
Two follow-up PRs depend on this one:
wp_ajax_parse_embed(),wp_ajax_parse_media_shortcode()and the theme compatibility header).This replaces #14310.
Trac ticket: https://core.trac.wordpress.org/ticket/66342
Use of AI Tools
AI assistance: Yes
Tool(s): GitHub Copilot, Claude Code, ChatGPT
Model(s): GPT-6 Luna, GPT-6, Claude Opus 5.5, Claude Fable 5.1
Used for: The initial implementation and tests were written with GitHub Copilot. Claude Code was used under my direction for review, the backward compatibility fixes, splitting the change, and tests, and I reviewed the result. ChatGPT was used to resolve the merge conflict with the current
trunkand adapt the implementation and tests to the removal of stylesheet concatenation.This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.