wp/wp-includes/feed.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    21  * @see get_bloginfo() For the list of possible values to display.
    21  * @see get_bloginfo() For the list of possible values to display.
    22  *
    22  *
    23  * @param string $show See get_bloginfo() for possible values.
    23  * @param string $show See get_bloginfo() for possible values.
    24  * @return string
    24  * @return string
    25  */
    25  */
    26 function get_bloginfo_rss($show = '') {
    26 function get_bloginfo_rss( $show = '' ) {
    27 	$info = strip_tags(get_bloginfo($show));
    27 	$info = strip_tags( get_bloginfo( $show ) );
    28 	/**
    28 	/**
    29 	 * Filters the bloginfo for use in RSS feeds.
    29 	 * Filters the bloginfo for use in RSS feeds.
    30 	 *
    30 	 *
    31 	 * @since 2.2.0
    31 	 * @since 2.2.0
    32 	 *
    32 	 *
    49  * @since 0.71
    49  * @since 0.71
    50  * @see get_bloginfo() For the list of possible values to display.
    50  * @see get_bloginfo() For the list of possible values to display.
    51  *
    51  *
    52  * @param string $show See get_bloginfo() for possible values.
    52  * @param string $show See get_bloginfo() for possible values.
    53  */
    53  */
    54 function bloginfo_rss($show = '') {
    54 function bloginfo_rss( $show = '' ) {
    55 	/**
    55 	/**
    56 	 * Filters the bloginfo for display in RSS feeds.
    56 	 * Filters the bloginfo for display in RSS feeds.
    57 	 *
    57 	 *
    58 	 * @since 2.1.0
    58 	 * @since 2.1.0
    59 	 *
    59 	 *
   180  * @see get_the_content()
   180  * @see get_the_content()
   181  *
   181  *
   182  * @param string $feed_type The type of feed. rss2 | atom | rss | rdf
   182  * @param string $feed_type The type of feed. rss2 | atom | rss | rdf
   183  * @return string The filtered content.
   183  * @return string The filtered content.
   184  */
   184  */
   185 function get_the_content_feed($feed_type = null) {
   185 function get_the_content_feed( $feed_type = null ) {
   186 	if ( !$feed_type )
   186 	if ( ! $feed_type ) {
   187 		$feed_type = get_default_feed();
   187 		$feed_type = get_default_feed();
       
   188 	}
   188 
   189 
   189 	/** This filter is documented in wp-includes/post-template.php */
   190 	/** This filter is documented in wp-includes/post-template.php */
   190 	$content = apply_filters( 'the_content', get_the_content() );
   191 	$content = apply_filters( 'the_content', get_the_content() );
   191 	$content = str_replace(']]>', ']]>', $content);
   192 	$content = str_replace( ']]>', ']]>', $content );
   192 	/**
   193 	/**
   193 	 * Filters the post content for use in feeds.
   194 	 * Filters the post content for use in feeds.
   194 	 *
   195 	 *
   195 	 * @since 2.9.0
   196 	 * @since 2.9.0
   196 	 *
   197 	 *
   206  *
   207  *
   207  * @since 2.9.0
   208  * @since 2.9.0
   208  *
   209  *
   209  * @param string $feed_type The type of feed. rss2 | atom | rss | rdf
   210  * @param string $feed_type The type of feed. rss2 | atom | rss | rdf
   210  */
   211  */
   211 function the_content_feed($feed_type = null) {
   212 function the_content_feed( $feed_type = null ) {
   212 	echo get_the_content_feed($feed_type);
   213 	echo get_the_content_feed( $feed_type );
   213 }
   214 }
   214 
   215 
   215 /**
   216 /**
   216  * Display the post excerpt for the feed.
   217  * Display the post excerpt for the feed.
   217  *
   218  *
   268  *
   269  *
   269  * @since 2.5.0
   270  * @since 2.5.0
   270  *
   271  *
   271  * @param int|WP_Comment $comment_id Optional comment object or id. Defaults to global comment object.
   272  * @param int|WP_Comment $comment_id Optional comment object or id. Defaults to global comment object.
   272  */
   273  */
   273 function comment_guid($comment_id = null) {
   274 function comment_guid( $comment_id = null ) {
   274 	echo esc_url( get_comment_guid($comment_id) );
   275 	echo esc_url( get_comment_guid( $comment_id ) );
   275 }
   276 }
   276 
   277 
   277 /**
   278 /**
   278  * Retrieve the feed GUID for the current comment.
   279  * Retrieve the feed GUID for the current comment.
   279  *
   280  *
   280  * @since 2.5.0
   281  * @since 2.5.0
   281  *
   282  *
   282  * @param int|WP_Comment $comment_id Optional comment object or id. Defaults to global comment object.
   283  * @param int|WP_Comment $comment_id Optional comment object or id. Defaults to global comment object.
   283  * @return false|string false on failure or guid for comment on success.
   284  * @return false|string false on failure or guid for comment on success.
   284  */
   285  */
   285 function get_comment_guid($comment_id = null) {
   286 function get_comment_guid( $comment_id = null ) {
   286 	$comment = get_comment($comment_id);
   287 	$comment = get_comment( $comment_id );
   287 
   288 
   288 	if ( !is_object($comment) )
   289 	if ( ! is_object( $comment ) ) {
   289 		return false;
   290 		return false;
   290 
   291 	}
   291 	return get_the_guid($comment->comment_post_ID) . '#comment-' . $comment->comment_ID;
   292 
       
   293 	return get_the_guid( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID;
   292 }
   294 }
   293 
   295 
   294 /**
   296 /**
   295  * Display the link to the comments.
   297  * Display the link to the comments.
   296  *
   298  *
   369  * @since 2.1.0
   371  * @since 2.1.0
   370  *
   372  *
   371  * @param string $type Optional, default is the type returned by get_default_feed().
   373  * @param string $type Optional, default is the type returned by get_default_feed().
   372  * @return string All of the post categories for displaying in the feed.
   374  * @return string All of the post categories for displaying in the feed.
   373  */
   375  */
   374 function get_the_category_rss($type = null) {
   376 function get_the_category_rss( $type = null ) {
   375 	if ( empty($type) )
   377 	if ( empty( $type ) ) {
   376 		$type = get_default_feed();
   378 		$type = get_default_feed();
       
   379 	}
   377 	$categories = get_the_category();
   380 	$categories = get_the_category();
   378 	$tags = get_the_tags();
   381 	$tags       = get_the_tags();
   379 	$the_list = '';
   382 	$the_list   = '';
   380 	$cat_names = array();
   383 	$cat_names  = array();
   381 
   384 
   382 	$filter = 'rss';
   385 	$filter = 'rss';
   383 	if ( 'atom' == $type )
   386 	if ( 'atom' == $type ) {
   384 		$filter = 'raw';
   387 		$filter = 'raw';
   385 
   388 	}
   386 	if ( !empty($categories) ) foreach ( (array) $categories as $category ) {
   389 
   387 		$cat_names[] = sanitize_term_field('name', $category->name, $category->term_id, 'category', $filter);
   390 	if ( ! empty( $categories ) ) {
   388 	}
   391 		foreach ( (array) $categories as $category ) {
   389 
   392 			$cat_names[] = sanitize_term_field( 'name', $category->name, $category->term_id, 'category', $filter );
   390 	if ( !empty($tags) ) foreach ( (array) $tags as $tag ) {
   393 		}
   391 		$cat_names[] = sanitize_term_field('name', $tag->name, $tag->term_id, 'post_tag', $filter);
   394 	}
   392 	}
   395 
   393 
   396 	if ( ! empty( $tags ) ) {
   394 	$cat_names = array_unique($cat_names);
   397 		foreach ( (array) $tags as $tag ) {
       
   398 			$cat_names[] = sanitize_term_field( 'name', $tag->name, $tag->term_id, 'post_tag', $filter );
       
   399 		}
       
   400 	}
       
   401 
       
   402 	$cat_names = array_unique( $cat_names );
   395 
   403 
   396 	foreach ( $cat_names as $cat_name ) {
   404 	foreach ( $cat_names as $cat_name ) {
   397 		if ( 'rdf' == $type )
   405 		if ( 'rdf' == $type ) {
   398 			$the_list .= "\t\t<dc:subject><![CDATA[$cat_name]]></dc:subject>\n";
   406 			$the_list .= "\t\t<dc:subject><![CDATA[$cat_name]]></dc:subject>\n";
   399 		elseif ( 'atom' == $type )
   407 		} elseif ( 'atom' == $type ) {
   400 			$the_list .= sprintf( '<category scheme="%1$s" term="%2$s" />', esc_attr( get_bloginfo_rss( 'url' ) ), esc_attr( $cat_name ) );
   408 			$the_list .= sprintf( '<category scheme="%1$s" term="%2$s" />', esc_attr( get_bloginfo_rss( 'url' ) ), esc_attr( $cat_name ) );
   401 		else
   409 		} else {
   402 			$the_list .= "\t\t<category><![CDATA[" . @html_entity_decode( $cat_name, ENT_COMPAT, get_option('blog_charset') ) . "]]></category>\n";
   410 			$the_list .= "\t\t<category><![CDATA[" . @html_entity_decode( $cat_name, ENT_COMPAT, get_option( 'blog_charset' ) ) . "]]></category>\n";
       
   411 		}
   403 	}
   412 	}
   404 
   413 
   405 	/**
   414 	/**
   406 	 * Filters all of the post categories for display in a feed.
   415 	 * Filters all of the post categories for display in a feed.
   407 	 *
   416 	 *
   420  * @since 0.71
   429  * @since 0.71
   421  * @see get_the_category_rss() For better explanation.
   430  * @see get_the_category_rss() For better explanation.
   422  *
   431  *
   423  * @param string $type Optional, default is the type returned by get_default_feed().
   432  * @param string $type Optional, default is the type returned by get_default_feed().
   424  */
   433  */
   425 function the_category_rss($type = null) {
   434 function the_category_rss( $type = null ) {
   426 	echo get_the_category_rss($type);
   435 	echo get_the_category_rss( $type );
   427 }
   436 }
   428 
   437 
   429 /**
   438 /**
   430  * Display the HTML type based on the blog setting.
   439  * Display the HTML type based on the blog setting.
   431  *
   440  *
   432  * The two possible values are either 'xhtml' or 'html'.
   441  * The two possible values are either 'xhtml' or 'html'.
   433  *
   442  *
   434  * @since 2.2.0
   443  * @since 2.2.0
   435  */
   444  */
   436 function html_type_rss() {
   445 function html_type_rss() {
   437 	$type = get_bloginfo('html_type');
   446 	$type = get_bloginfo( 'html_type' );
   438 	if (strpos($type, 'xhtml') !== false)
   447 	if ( strpos( $type, 'xhtml' ) !== false ) {
   439 		$type = 'xhtml';
   448 		$type = 'xhtml';
   440 	else
   449 	} else {
   441 		$type = 'html';
   450 		$type = 'html';
       
   451 	}
   442 	echo $type;
   452 	echo $type;
   443 }
   453 }
   444 
   454 
   445 /**
   455 /**
   446  * Display the rss enclosure for the current post.
   456  * Display the rss enclosure for the current post.
   455  * attributes.
   465  * attributes.
   456  *
   466  *
   457  * @since 1.5.0
   467  * @since 1.5.0
   458  */
   468  */
   459 function rss_enclosure() {
   469 function rss_enclosure() {
   460 	if ( post_password_required() )
   470 	if ( post_password_required() ) {
   461 		return;
   471 		return;
   462 
   472 	}
   463 	foreach ( (array) get_post_custom() as $key => $val) {
   473 
   464 		if ($key == 'enclosure') {
   474 	foreach ( (array) get_post_custom() as $key => $val ) {
       
   475 		if ( $key == 'enclosure' ) {
   465 			foreach ( (array) $val as $enc ) {
   476 			foreach ( (array) $val as $enc ) {
   466 				$enclosure = explode("\n", $enc);
   477 				$enclosure = explode( "\n", $enc );
   467 
   478 
   468 				// only get the first element, e.g. audio/mpeg from 'audio/mpeg mpga mp2 mp3'
   479 				// only get the first element, e.g. audio/mpeg from 'audio/mpeg mpga mp2 mp3'
   469 				$t = preg_split('/[ \t]/', trim($enclosure[2]) );
   480 				$t    = preg_split( '/[ \t]/', trim( $enclosure[2] ) );
   470 				$type = $t[0];
   481 				$type = $t[0];
   471 
   482 
   472 				/**
   483 				/**
   473 				 * Filters the RSS enclosure HTML link tag for the current post.
   484 				 * Filters the RSS enclosure HTML link tag for the current post.
   474 				 *
   485 				 *
   494  * enclosure(s) consist of link HTML tag(s) with a URI and other attributes.
   505  * enclosure(s) consist of link HTML tag(s) with a URI and other attributes.
   495  *
   506  *
   496  * @since 2.2.0
   507  * @since 2.2.0
   497  */
   508  */
   498 function atom_enclosure() {
   509 function atom_enclosure() {
   499 	if ( post_password_required() )
   510 	if ( post_password_required() ) {
   500 		return;
   511 		return;
       
   512 	}
   501 
   513 
   502 	foreach ( (array) get_post_custom() as $key => $val ) {
   514 	foreach ( (array) get_post_custom() as $key => $val ) {
   503 		if ($key == 'enclosure') {
   515 		if ( $key == 'enclosure' ) {
   504 			foreach ( (array) $val as $enc ) {
   516 			foreach ( (array) $val as $enc ) {
   505 				$enclosure = explode("\n", $enc);
   517 				$enclosure = explode( "\n", $enc );
   506 				/**
   518 				/**
   507 				 * Filters the atom enclosure HTML link tag for the current post.
   519 				 * Filters the atom enclosure HTML link tag for the current post.
   508 				 *
   520 				 *
   509 				 * @since 2.2.0
   521 				 * @since 2.2.0
   510 				 *
   522 				 *
   531  * @since 2.5.0
   543  * @since 2.5.0
   532  *
   544  *
   533  * @param string $data Input string
   545  * @param string $data Input string
   534  * @return array array(type, value)
   546  * @return array array(type, value)
   535  */
   547  */
   536 function prep_atom_text_construct($data) {
   548 function prep_atom_text_construct( $data ) {
   537 	if (strpos($data, '<') === false && strpos($data, '&') === false) {
   549 	if ( strpos( $data, '<' ) === false && strpos( $data, '&' ) === false ) {
   538 		return array('text', $data);
   550 		return array( 'text', $data );
   539 	}
   551 	}
   540 
   552 
   541 	if ( ! function_exists( 'xml_parser_create' ) ) {
   553 	if ( ! function_exists( 'xml_parser_create' ) ) {
   542 		trigger_error( __( "PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension." ) );
   554 		trigger_error( __( "PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension." ) );
   543 
   555 
   544 		return array( 'html', "<![CDATA[$data]]>" );
   556 		return array( 'html', "<![CDATA[$data]]>" );
   545 	}
   557 	}
   546 
   558 
   547 	$parser = xml_parser_create();
   559 	$parser = xml_parser_create();
   548 	xml_parse($parser, '<div>' . $data . '</div>', true);
   560 	xml_parse( $parser, '<div>' . $data . '</div>', true );
   549 	$code = xml_get_error_code($parser);
   561 	$code = xml_get_error_code( $parser );
   550 	xml_parser_free($parser);
   562 	xml_parser_free( $parser );
   551 
   563 
   552 	if (!$code) {
   564 	if ( ! $code ) {
   553 		if (strpos($data, '<') === false) {
   565 		if ( strpos( $data, '<' ) === false ) {
   554 			return array('text', $data);
   566 			return array( 'text', $data );
   555 		} else {
   567 		} else {
   556 			$data = "<div xmlns='http://www.w3.org/1999/xhtml'>$data</div>";
   568 			$data = "<div xmlns='http://www.w3.org/1999/xhtml'>$data</div>";
   557 			return array('xhtml', $data);
   569 			return array( 'xhtml', $data );
   558 		}
   570 		}
   559 	}
   571 	}
   560 
   572 
   561 	if (strpos($data, ']]>') === false) {
   573 	if ( strpos( $data, ']]>' ) === false ) {
   562 		return array('html', "<![CDATA[$data]]>");
   574 		return array( 'html', "<![CDATA[$data]]>" );
   563 	} else {
   575 	} else {
   564 		return array('html', htmlspecialchars($data));
   576 		return array( 'html', htmlspecialchars( $data ) );
   565 	}
   577 	}
   566 }
   578 }
   567 
   579 
   568 /**
   580 /**
   569  * Displays Site Icon in atom feeds.
   581  * Displays Site Icon in atom feeds.
   609  * Generate a correct link for the atom:self element.
   621  * Generate a correct link for the atom:self element.
   610  *
   622  *
   611  * @since 2.5.0
   623  * @since 2.5.0
   612  */
   624  */
   613 function self_link() {
   625 function self_link() {
   614 	$host = @parse_url(home_url());
   626 	$host = @parse_url( home_url() );
   615 	/**
   627 	/**
   616 	 * Filters the current feed URL.
   628 	 * Filters the current feed URL.
   617 	 *
   629 	 *
   618 	 * @since 3.6.0
   630 	 * @since 3.6.0
   619 	 *
   631 	 *
   623 	 * @param string $feed_link The link for the feed with set URL scheme.
   635 	 * @param string $feed_link The link for the feed with set URL scheme.
   624 	 */
   636 	 */
   625 	echo esc_url( apply_filters( 'self_link', set_url_scheme( 'http://' . $host['host'] . wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) );
   637 	echo esc_url( apply_filters( 'self_link', set_url_scheme( 'http://' . $host['host'] . wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) );
   626 }
   638 }
   627 
   639 
       
   640 /*
       
   641  * Get the timestamp of the most recently modified post from WP_Query.
       
   642  *
       
   643  * If viewing a comment feed, the timestamp of the most recently modified
       
   644  * comment will be returned.
       
   645  *
       
   646  * @global WP_Query  $wp_query The global WP_Query object.
       
   647  *
       
   648  * @since 5.2.0
       
   649  *
       
   650  * @param string $format Format of the timestamp to return, passed to mysql2date.
       
   651  *
       
   652  * @return string The timestamp.
       
   653  */
       
   654 function get_feed_build_date( $format ) {
       
   655 	global $wp_query;
       
   656 
       
   657 	if ( empty( $wp_query ) || ! $wp_query->have_posts() ) {
       
   658 		// Fallback to last time any post was modified or published.
       
   659 		return get_lastpostmodified( 'GMT' );
       
   660 	}
       
   661 
       
   662 	// Extract the post modified times from the posts.
       
   663 	$modified_times = wp_list_pluck( $wp_query->posts, 'post_modified_gmt' );
       
   664 
       
   665 	// If this is a comment feed, check those objects too.
       
   666 	if ( $wp_query->is_comment_feed() && $wp_query->comment_count ) {
       
   667 		// Extract the comment modified times from the comments.
       
   668 		$comment_times = wp_list_pluck( $wp_query->comments, 'comment_date_gmt' );
       
   669 
       
   670 		// Add the comment times to the post times for comparison.
       
   671 		$modified_times = array_merge( $modified_times, $comment_times );
       
   672 	}
       
   673 
       
   674 	// Determine the maximum modified time.
       
   675 	$max_modified_time = mysql2date( $format, max( $modified_times ), false );
       
   676 
       
   677 	/**
       
   678 	 * Filters the date the last post or comment in the query was modified.
       
   679 	 *
       
   680 	 * @since 5.2.0
       
   681 	 *
       
   682 	 * @param string $max_modified_time Date the last post or comment was modified in the query.
       
   683 	 * @param string $format            The date format requested in get_feed_build_date.
       
   684 	 */
       
   685 	return apply_filters( 'get_feed_build_date', $max_modified_time, $format );
       
   686 }
       
   687 
   628 /**
   688 /**
   629  * Return the content type for specified feed type.
   689  * Return the content type for specified feed type.
   630  *
   690  *
   631  * @since 2.8.0
   691  * @since 2.8.0
   632  *
   692  *
   633  * @param string $type Type of feed. Possible values include 'rss', rss2', 'atom', and 'rdf'.
   693  * @param string $type Type of feed. Possible values include 'rss', rss2', 'atom', and 'rdf'.
   634  */
   694  */
   635 function feed_content_type( $type = '' ) {
   695 function feed_content_type( $type = '' ) {
   636 	if ( empty($type) )
   696 	if ( empty( $type ) ) {
   637 		$type = get_default_feed();
   697 		$type = get_default_feed();
       
   698 	}
   638 
   699 
   639 	$types = array(
   700 	$types = array(
   640 		'rss'      => 'application/rss+xml',
   701 		'rss'      => 'application/rss+xml',
   641 		'rss2'     => 'application/rss+xml',
   702 		'rss2'     => 'application/rss+xml',
   642 		'rss-http' => 'text/xml',
   703 		'rss-http' => 'text/xml',
   643 		'atom'     => 'application/atom+xml',
   704 		'atom'     => 'application/atom+xml',
   644 		'rdf'      => 'application/rdf+xml'
   705 		'rdf'      => 'application/rdf+xml',
   645 	);
   706 	);
   646 
   707 
   647 	$content_type = ( !empty($types[$type]) ) ? $types[$type] : 'application/octet-stream';
   708 	$content_type = ( ! empty( $types[ $type ] ) ) ? $types[ $type ] : 'application/octet-stream';
   648 
   709 
   649 	/**
   710 	/**
   650 	 * Filters the content type for a specific feed type.
   711 	 * Filters the content type for a specific feed type.
   651 	 *
   712 	 *
   652 	 * @since 2.8.0
   713 	 * @since 2.8.0
   662  *
   723  *
   663  * @since 2.8.0
   724  * @since 2.8.0
   664  *
   725  *
   665  * @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged
   726  * @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged
   666  * using SimplePie's multifeed feature.
   727  * using SimplePie's multifeed feature.
   667  * See also {@link ​http://simplepie.org/wiki/faq/typical_multifeed_gotchas}
   728  * See also {@link http://simplepie.org/wiki/faq/typical_multifeed_gotchas}
   668  *
   729  *
   669  * @return WP_Error|SimplePie WP_Error object on failure or SimplePie object on success
   730  * @return WP_Error|SimplePie WP_Error object on failure or SimplePie object on success
   670  */
   731  */
   671 function fetch_feed( $url ) {
   732 function fetch_feed( $url ) {
   672 	if ( ! class_exists( 'SimplePie', false ) ) {
   733 	if ( ! class_exists( 'SimplePie', false ) ) {
   701 	 */
   762 	 */
   702 	do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
   763 	do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
   703 	$feed->init();
   764 	$feed->init();
   704 	$feed->set_output_encoding( get_option( 'blog_charset' ) );
   765 	$feed->set_output_encoding( get_option( 'blog_charset' ) );
   705 
   766 
   706 	if ( $feed->error() )
   767 	if ( $feed->error() ) {
   707 		return new WP_Error( 'simplepie-error', $feed->error() );
   768 		return new WP_Error( 'simplepie-error', $feed->error() );
       
   769 	}
   708 
   770 
   709 	return $feed;
   771 	return $feed;
   710 }
   772 }