add a filterable list of feed namespaces - #12812
Conversation
|
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. |
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. |
| * @param string $type Type of feed. Possible values include 'rss2', 'rss2-comments', | ||
| * 'rdf', 'atom', and 'atom-comments'. | ||
| * @return string[] Array of namespace URIs, keyed by their prefix. | ||
| */ | ||
| function get_feed_namespaces( $type ) { |
There was a problem hiding this comment.
| * @param string $type Type of feed. Possible values include 'rss2', 'rss2-comments', | |
| * 'rdf', 'atom', and 'atom-comments'. | |
| * @return string[] Array of namespace URIs, keyed by their prefix. | |
| */ | |
| function get_feed_namespaces( $type ) { | |
| * @param string $type Type of feed. Possible values include 'rss2', 'rss2-comments', | |
| * 'rdf', 'atom', and 'atom-comments'. | |
| * @return array<string, string> Array of namespace URIs, keyed by their prefix. | |
| * @phpstan-param 'rss2'|'rss2-comments'|'rdf'|'atom'|'atom-comments' $type | |
| * @phpstan-return array<non-falsy-string, non-falsy-string> | |
| */ | |
| function get_feed_namespaces( string $type ): array { |
There was a problem hiding this comment.
Not sure if the 'rss2'|'rss2-comments'|'rdf'|'atom'|'atom-comments' enum is warranted, if other feed types are intended to be supported.
There was a problem hiding this comment.
Otherwise, a non-falsy-string would be a good narrowed type.
There was a problem hiding this comment.
🤔 good point, but I would assume that modern feed formats are no longer XML, so I am not sure if we need a "generic" fallback!?
There was a problem hiding this comment.
But I have no strong opinion on that! I am fine with having a general namespace support that could be adopted by third party implementers too!
| */ | ||
| function feed_namespaces( $type ) { | ||
| foreach ( get_feed_namespaces( $type ) as $prefix => $uri ) { | ||
| printf( "xmlns:%s=\"%s\"\n\t", $prefix, esc_attr( $uri ) ); |
There was a problem hiding this comment.
| printf( "xmlns:%s=\"%s\"\n\t", $prefix, esc_attr( $uri ) ); | |
| printf( "xmlns:%s=\"%s\"\n\t", $prefix, esc_url( $uri ) ); |
There was a problem hiding this comment.
I applied your suggestion, but I am not sure it works for every value. XML namespaces are IRIs, so they can also be URNs for example (urn:schemas-microsoft-com:office:office), so we may have to stick with esc_attr or have more specific check(s)!?
There was a problem hiding this comment.
Should WordPress have an esc_iri? Maybe based on src/wp-includes/SimplePie/src/IRI.php???
Co-authored-by: Weston Ruter <westonruter@gmail.com>
Co-authored-by: Weston Ruter <westonruter@gmail.com>
Co-authored-by: Weston Ruter <westonruter@gmail.com>
Co-authored-by: Weston Ruter <westonruter@gmail.com>
Trac ticket: https://core.trac.wordpress.org/ticket/65785
The
rss2_nsandatom_nsactions are echo-only, so two plugins that add the same namespace produce a duplicate attribute, and the whole feed becomes invalid XML.This adds
get_feed_namespaces()andfeed_namespaces()(named afterfeed_content_type()) and awp_feed_namespacesfilter. Namespaces are keyed by their prefix, so the same namespace can not be printed twice. The default namespaces of the templates moved into the filtered list, otherwise a plugin could still duplicate them. The*_nsactions stay untouched for backwards compatibility.What it does not solve: a plugin that still echoes a namespace via the old actions can produce a duplicate, like before. That would need output buffering around the actions, and I did not want to go there.