Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/wp-includes/feed-atom-comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@
<feed
xmlns="http://www.w3.org/2005/Atom"
xml:lang="<?php bloginfo_rss( 'language' ); ?>"
xmlns:thr="http://purl.org/syndication/thread/1.0"
<?php
feed_namespaces( 'atom-comments' );

/** This action is documented in wp-includes/feed-atom.php */
do_action( 'atom_ns' );

/**
* Fires inside the feed tag in the Atom comment feed.
*
* Consider using the `wp_feed_namespaces` filter instead, which
* prevents duplicate `xmlns` attributes.
*
* @since 2.8.0
*/
do_action( 'atom_comments_ns' );
Expand Down
6 changes: 5 additions & 1 deletion src/wp-includes/feed-atom.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@
?>
<feed
xmlns="http://www.w3.org/2005/Atom"
xmlns:thr="http://purl.org/syndication/thread/1.0"
xml:lang="<?php bloginfo_rss( 'language' ); ?>"
<?php
feed_namespaces( 'atom' );

/**
* Fires at end of the Atom feed root to add namespaces.
*
* Consider using the `wp_feed_namespaces` filter instead, which prevents
* duplicate `xmlns` attributes.
*
* @since 2.0.0
*/
do_action( 'atom_ns' );
Expand Down
10 changes: 5 additions & 5 deletions src/wp-includes/feed-rdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
?>
<rdf:RDF
xmlns="http://purl.org/rss/1.0/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:admin="http://webns.net/mvcb/"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
<?php
feed_namespaces( 'rdf' );

/**
* Fires at the end of the feed root to add namespaces.
*
* Consider using the `wp_feed_namespaces` filter instead, which prevents
* duplicate `xmlns` attributes.
*
* @since 2.0.0
*/
do_action( 'rdf_ns' );
Expand Down
9 changes: 5 additions & 4 deletions src/wp-includes/feed-rss2-comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
do_action( 'rss_tag_pre', 'rss2-comments' );
?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
<?php
feed_namespaces( 'rss2-comments' );

/** This action is documented in wp-includes/feed-rss2.php */
do_action( 'rss2_ns' );
?>
Expand All @@ -26,6 +24,9 @@
/**
* Fires at the end of the RSS root to add namespaces.
*
* Consider using the `wp_feed_namespaces` filter instead, which
* prevents duplicate `xmlns` attributes.
*
* @since 2.8.0
*/
do_action( 'rss2_comments_ns' );
Expand Down
11 changes: 5 additions & 6 deletions src/wp-includes/feed-rss2.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@
do_action( 'rss_tag_pre', 'rss2' );
?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
<?php
feed_namespaces( 'rss2' );

/**
* Fires at the end of the RSS root to add namespaces.
*
* Consider using the `wp_feed_namespaces` filter instead, which prevents
* duplicate `xmlns` attributes.
*
* @since 2.0.0
*/
do_action( 'rss2_ns' );
Expand Down
120 changes: 120 additions & 0 deletions src/wp-includes/feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,126 @@ function self_link() {
echo esc_url( apply_filters( 'self_link', get_self_link() ) );
}

/**
* Retrieves the XML namespaces for the root element of a feed.
*
* Namespaces are keyed by their prefix, so the same prefix cannot be
* declared twice. The default namespaces of a feed type cannot be removed,
* as the bundled feed templates use them in their static markup.
*
* @since 7.2.0
*
* @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 {
$defaults = array();

switch ( $type ) {
case 'rss2':
$defaults = array(
'content' => 'http://purl.org/rss/1.0/modules/content/',
'wfw' => 'http://wellformedweb.org/CommentAPI/',
'dc' => 'http://purl.org/dc/elements/1.1/',
'atom' => 'http://www.w3.org/2005/Atom',
'sy' => 'http://purl.org/rss/1.0/modules/syndication/',
'slash' => 'http://purl.org/rss/1.0/modules/slash/',
);
break;

case 'rss2-comments':
$defaults = array(
'content' => 'http://purl.org/rss/1.0/modules/content/',
'dc' => 'http://purl.org/dc/elements/1.1/',
'atom' => 'http://www.w3.org/2005/Atom',
'sy' => 'http://purl.org/rss/1.0/modules/syndication/',
);
break;

case 'rdf':
$defaults = array(
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'dc' => 'http://purl.org/dc/elements/1.1/',
'sy' => 'http://purl.org/rss/1.0/modules/syndication/',
'admin' => 'http://webns.net/mvcb/',
'content' => 'http://purl.org/rss/1.0/modules/content/',
);
break;

case 'atom':
case 'atom-comments':
$defaults = array(
'thr' => 'http://purl.org/syndication/thread/1.0',
);
break;
}

/**
* Filters the XML namespaces of a feed's root element.
*
* Namespaces are keyed by their prefix, which makes duplicate `xmlns`
* attributes impossible.
*
* @since 7.2.0
*
* @param array<string, string> $namespaces Array of namespace URIs, keyed by their prefix.
* @param string $type Type of feed. Possible values include 'rss2',
* 'rss2-comments', 'rdf', 'atom', and 'atom-comments'.
*/
$namespaces = apply_filters( 'wp_feed_namespaces', $defaults, $type );

if ( ! is_array( $namespaces ) ) {
$namespaces = array();
}

// The bundled feed templates use the default namespaces, so they cannot be removed.
$namespaces = array_merge( $namespaces, $defaults );

$sanitized = array();

foreach ( $namespaces as $prefix => $uri ) {
$prefix = (string) $prefix;

// Prefixes must be valid XML names, and the `xml` and `xmlns` prefixes are reserved.
if ( ! preg_match( '/^[\p{L}_][\p{L}\p{M}\p{N}._\-\x{B7}]*$/u', $prefix )
|| in_array( strtolower( $prefix ), array( 'xml', 'xmlns' ), true )
) {
continue;
}

if ( ! is_string( $uri ) || empty( $uri ) ) {
continue;
}

$sanitized[ $prefix ] = $uri;
}

return $sanitized;
}

/**
* Displays the XML namespaces for the root element of a feed.
*
* Plugins should add namespaces via the `wp_feed_namespaces` filter instead
* of the older `{$type}_ns` actions, as two action callbacks printing the
* same namespace produce a duplicate attribute, which is a well-formedness
* error in XML.
*
* @since 7.2.0
*
* @param string $type Type of feed. Possible values include 'rss2', 'rss2-comments',
* 'rdf', 'atom', and 'atom-comments'.
* @phpstan-param 'rss2'|'rss2-comments'|'rdf'|'atom'|'atom-comments' $type
*/
function feed_namespaces( string $type ): void {
foreach ( get_feed_namespaces( $type ) as $prefix => $uri ) {
printf( "xmlns:%s=\"%s\"\n\t", $prefix, esc_url( $uri ) );
}
}

/**
* Gets the UTC time of the most recently modified post from WP_Query.
*
Expand Down
Loading
Loading