);
} else {
return array( 'html', htmlspecialchars( $data ) );
}
}
/**
* Displays Site Icon in atom feeds.
*
* @since 4.3.0
*
* @see get_site_icon_url()
*/
function atom_site_icon() {
$url = get_site_icon_url( 32 );
if ( $url ) {
echo '' . convert_chars( $url ) . "\n";
}
}
/**
* Displays Site Icon in RSS2.
*
* @since 4.3.0
*/
function rss2_site_icon() {
$rss_title = get_wp_title_rss();
if ( empty( $rss_title ) ) {
$rss_title = get_bloginfo_rss( 'name' );
}
$url = get_site_icon_url( 32 );
if ( $url ) {
echo '
' . convert_chars( $url ) . '' . $rss_title . '
' . get_bloginfo_rss( 'url' ) . '
3232 ' . "\n";
}
}
/**
* Returns the link for the currently displayed feed.
*
* @since 5.3.0
*
* @return string Correct link for the atom:self element.
*/
function get_self_link() {
$parsed = parse_url( home_url() );
$domain = $parsed['host'];
if ( isset( $parsed['port'] ) ) {
$domain .= ':' . $parsed['port'];
}
return set_url_scheme( 'http://' . $domain . wp_unslash( $_SERVER['REQUEST_URI'] ) );
}
/**
* Displays the link for the currently displayed feed in a XSS safe way.
*
* Generate a correct link for the atom:self element.
*
* @since 2.5.0
*/
function self_link() {
/**
* Filters the current feed URL.
*
* @since 3.6.0
*
* @see set_url_scheme()
* @see wp_unslash()
*
* @param string $feed_link The link for the feed with set URL scheme.
*/
echo esc_url( apply_filters( 'self_link', get_self_link() ) );
}
/**
* Gets the UTC time of the most recently modified post from WP_Query.
*
* If viewing a comment feed, the time of the most recently modified
* comment will be returned.
*
* @since 5.2.0
*
* @global WP_Query $wp_query WordPress Query object.
*
* @param string $format Date format string to return the time in.
* @return string|false The time in requested format, or false on failure.
*/
function get_feed_build_date( $format ) {
global $wp_query;
$datetime = false;
$max_modified_time = false;
$utc = new DateTimeZone( 'UTC' );
if ( ! empty( $wp_query ) && $wp_query->have_posts() ) {
// Extract the post modified times from the posts.
$modified_times = wp_list_pluck( $wp_query->posts, 'post_modified_gmt' );
// If this is a comment feed, check those objects too.
if ( $wp_query->is_comment_feed() && $wp_query->comment_count ) {
// Extract the comment modified times from the comments.
$comment_times = wp_list_pluck( $wp_query->comments, 'comment_date_gmt' );
// Add the comment times to the post times for comparison.
$modified_times = array_merge( $modified_times, $comment_times );
}
// Determine the maximum modified time.
$datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', max( $modified_times ), $utc );
}
if ( false === $datetime ) {
// Fall back to last time any post was modified or published.
$datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', get_lastpostmodified( 'GMT' ), $utc );
}
if ( false !== $datetime ) {
$max_modified_time = $datetime->format( $format );
}
/**
* Filters the date the last post or comment in the query was modified.
*
* @since 5.2.0
*
* @param string|false $max_modified_time Date the last post or comment was modified in the query, in UTC.
* False on failure.
* @param string $format The date format requested in get_feed_build_date().
*/
return apply_filters( 'get_feed_build_date', $max_modified_time, $format );
}
/**
* Returns the content type for specified feed type.
*
* @since 2.8.0
*
* @param string $type Type of feed. Possible values include 'rss', rss2', 'atom', and 'rdf'.
* @return string Content type for specified feed type.
*/
function feed_content_type( $type = '' ) {
if ( empty( $type ) ) {
$type = get_default_feed();
}
$types = array(
'rss' => 'application/rss+xml',
'rss2' => 'application/rss+xml',
'rss-http' => 'text/xml',
'atom' => 'application/atom+xml',
'rdf' => 'application/rdf+xml',
);
$content_type = ( ! empty( $types[ $type ] ) ) ? $types[ $type ] : 'application/octet-stream';
/**
* Filters the content type for a specific feed type.
*
* @since 2.8.0
*
* @param string $content_type Content type indicating the type of data that a feed contains.
* @param string $type Type of feed. Possible values include 'rss', rss2', 'atom', and 'rdf'.
*/
return apply_filters( 'feed_content_type', $content_type, $type );
}
/**
* Builds SimplePie object based on RSS or Atom feed from URL.
*
* @since 2.8.0
*
* @param string|string[] $url URL of feed to retrieve. If an array of URLs, the feeds are merged
* using SimplePie's multifeed feature.
* See also {@link http://simplepie.org/wiki/faq/typical_multifeed_gotchas}
* @return SimplePie|WP_Error SimplePie object on success or WP_Error object on failure.
*/
function fetch_feed( $url ) {
if ( ! class_exists( 'SimplePie', false ) ) {
require_once ABSPATH . WPINC . '/class-simplepie.php';
}
require_once ABSPATH . WPINC . '/class-wp-feed-cache-transient.php';
require_once ABSPATH . WPINC . '/class-wp-simplepie-file.php';
require_once ABSPATH . WPINC . '/class-wp-simplepie-sanitize-kses.php';
$feed = new SimplePie();
$feed->set_sanitize_class( 'WP_SimplePie_Sanitize_KSES' );
/*
* We must manually overwrite $feed->sanitize because SimplePie's constructor
* sets it before we have a chance to set the sanitization class.
*/
$feed->sanitize = new WP_SimplePie_Sanitize_KSES();
// Register the cache handler using the recommended method for SimplePie 1.3 or later.
if ( method_exists( 'SimplePie_Cache', 'register' ) ) {
SimplePie_Cache::register( 'wp_transient', 'WP_Feed_Cache_Transient' );
$feed->set_cache_location( 'wp_transient' );
} else {
// Back-compat for SimplePie 1.2.x.
require_once ABSPATH . WPINC . '/class-wp-feed-cache.php';
$feed->set_cache_class( 'WP_Feed_Cache' );
}
$feed->set_file_class( 'WP_SimplePie_File' );
$feed->set_feed_url( $url );
/** This filter is documented in wp-includes/class-wp-feed-cache-transient.php */
$feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url ) );
/**
* Fires just before processing the SimplePie feed object.
*
* @since 3.0.0
*
* @param SimplePie $feed SimplePie feed object (passed by reference).
* @param string|string[] $url URL of feed or array of URLs of feeds to retrieve.
*/
do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
$feed->init();
$feed->set_output_encoding( get_option( 'blog_charset' ) );
if ( $feed->error() ) {
return new WP_Error( 'simplepie-error', $feed->error() );
}
return $feed;
}