You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

711 lines
19 KiB

  1. <?php
  2. /**
  3. * WordPress Feed API
  4. *
  5. * Many of the functions used in here belong in The Loop, or The Loop for the
  6. * Feeds.
  7. *
  8. * @package WordPress
  9. * @subpackage Feed
  10. * @since 2.1.0
  11. */
  12. /**
  13. * RSS container for the bloginfo function.
  14. *
  15. * You can retrieve anything that you can using the get_bloginfo() function.
  16. * Everything will be stripped of tags and characters converted, when the values
  17. * are retrieved for use in the feeds.
  18. *
  19. * @since 1.5.1
  20. * @see get_bloginfo() For the list of possible values to display.
  21. *
  22. * @param string $show See get_bloginfo() for possible values.
  23. * @return string
  24. */
  25. function get_bloginfo_rss($show = '') {
  26. $info = strip_tags(get_bloginfo($show));
  27. /**
  28. * Filters the bloginfo for use in RSS feeds.
  29. *
  30. * @since 2.2.0
  31. *
  32. * @see convert_chars()
  33. * @see get_bloginfo()
  34. *
  35. * @param string $info Converted string value of the blog information.
  36. * @param string $show The type of blog information to retrieve.
  37. */
  38. return apply_filters( 'get_bloginfo_rss', convert_chars( $info ), $show );
  39. }
  40. /**
  41. * Display RSS container for the bloginfo function.
  42. *
  43. * You can retrieve anything that you can using the get_bloginfo() function.
  44. * Everything will be stripped of tags and characters converted, when the values
  45. * are retrieved for use in the feeds.
  46. *
  47. * @since 0.71
  48. * @see get_bloginfo() For the list of possible values to display.
  49. *
  50. * @param string $show See get_bloginfo() for possible values.
  51. */
  52. function bloginfo_rss($show = '') {
  53. /**
  54. * Filters the bloginfo for display in RSS feeds.
  55. *
  56. * @since 2.1.0
  57. *
  58. * @see get_bloginfo()
  59. *
  60. * @param string $rss_container RSS container for the blog information.
  61. * @param string $show The type of blog information to retrieve.
  62. */
  63. echo apply_filters( 'bloginfo_rss', get_bloginfo_rss( $show ), $show );
  64. }
  65. /**
  66. * Retrieve the default feed.
  67. *
  68. * The default feed is 'rss2', unless a plugin changes it through the
  69. * {@see 'default_feed'} filter.
  70. *
  71. * @since 2.5.0
  72. *
  73. * @return string Default feed, or for example 'rss2', 'atom', etc.
  74. */
  75. function get_default_feed() {
  76. /**
  77. * Filters the default feed type.
  78. *
  79. * @since 2.5.0
  80. *
  81. * @param string $feed_type Type of default feed. Possible values include 'rss2', 'atom'.
  82. * Default 'rss2'.
  83. */
  84. $default_feed = apply_filters( 'default_feed', 'rss2' );
  85. return 'rss' == $default_feed ? 'rss2' : $default_feed;
  86. }
  87. /**
  88. * Retrieve the blog title for the feed title.
  89. *
  90. * @since 2.2.0
  91. * @since 4.4.0 The optional `$sep` parameter was deprecated and renamed to `$deprecated`.
  92. *
  93. * @param string $deprecated Unused..
  94. * @return string The document title.
  95. */
  96. function get_wp_title_rss( $deprecated = '&#8211;' ) {
  97. if ( '&#8211;' !== $deprecated ) {
  98. /* translators: %s: 'document_title_separator' filter name */
  99. _deprecated_argument( __FUNCTION__, '4.4.0', sprintf( __( 'Use the %s filter instead.' ), '<code>document_title_separator</code>' ) );
  100. }
  101. /**
  102. * Filters the blog title for use as the feed title.
  103. *
  104. * @since 2.2.0
  105. * @since 4.4.0 The `$sep` parameter was deprecated and renamed to `$deprecated`.
  106. *
  107. * @param string $title The current blog title.
  108. * @param string $deprecated Unused.
  109. */
  110. return apply_filters( 'get_wp_title_rss', wp_get_document_title(), $deprecated );
  111. }
  112. /**
  113. * Display the blog title for display of the feed title.
  114. *
  115. * @since 2.2.0
  116. * @since 4.4.0 The optional `$sep` parameter was deprecated and renamed to `$deprecated`.
  117. *
  118. * @param string $deprecated Unused.
  119. */
  120. function wp_title_rss( $deprecated = '&#8211;' ) {
  121. if ( '&#8211;' !== $deprecated ) {
  122. /* translators: %s: 'document_title_separator' filter name */
  123. _deprecated_argument( __FUNCTION__, '4.4.0', sprintf( __( 'Use the %s filter instead.' ), '<code>document_title_separator</code>' ) );
  124. }
  125. /**
  126. * Filters the blog title for display of the feed title.
  127. *
  128. * @since 2.2.0
  129. * @since 4.4.0 The `$sep` parameter was deprecated and renamed to `$deprecated`.
  130. *
  131. * @see get_wp_title_rss()
  132. *
  133. * @param string $wp_title_rss The current blog title.
  134. * @param string $deprecated Unused.
  135. */
  136. echo apply_filters( 'wp_title_rss', get_wp_title_rss(), $deprecated );
  137. }
  138. /**
  139. * Retrieve the current post title for the feed.
  140. *
  141. * @since 2.0.0
  142. *
  143. * @return string Current post title.
  144. */
  145. function get_the_title_rss() {
  146. $title = get_the_title();
  147. /**
  148. * Filters the post title for use in a feed.
  149. *
  150. * @since 1.2.0
  151. *
  152. * @param string $title The current post title.
  153. */
  154. $title = apply_filters( 'the_title_rss', $title );
  155. return $title;
  156. }
  157. /**
  158. * Display the post title in the feed.
  159. *
  160. * @since 0.71
  161. */
  162. function the_title_rss() {
  163. echo get_the_title_rss();
  164. }
  165. /**
  166. * Retrieve the post content for feeds.
  167. *
  168. * @since 2.9.0
  169. * @see get_the_content()
  170. *
  171. * @param string $feed_type The type of feed. rss2 | atom | rss | rdf
  172. * @return string The filtered content.
  173. */
  174. function get_the_content_feed($feed_type = null) {
  175. if ( !$feed_type )
  176. $feed_type = get_default_feed();
  177. /** This filter is documented in wp-includes/post-template.php */
  178. $content = apply_filters( 'the_content', get_the_content() );
  179. $content = str_replace(']]>', ']]&gt;', $content);
  180. /**
  181. * Filters the post content for use in feeds.
  182. *
  183. * @since 2.9.0
  184. *
  185. * @param string $content The current post content.
  186. * @param string $feed_type Type of feed. Possible values include 'rss2', 'atom'.
  187. * Default 'rss2'.
  188. */
  189. return apply_filters( 'the_content_feed', $content, $feed_type );
  190. }
  191. /**
  192. * Display the post content for feeds.
  193. *
  194. * @since 2.9.0
  195. *
  196. * @param string $feed_type The type of feed. rss2 | atom | rss | rdf
  197. */
  198. function the_content_feed($feed_type = null) {
  199. echo get_the_content_feed($feed_type);
  200. }
  201. /**
  202. * Display the post excerpt for the feed.
  203. *
  204. * @since 0.71
  205. */
  206. function the_excerpt_rss() {
  207. $output = get_the_excerpt();
  208. /**
  209. * Filters the post excerpt for a feed.
  210. *
  211. * @since 1.2.0
  212. *
  213. * @param string $output The current post excerpt.
  214. */
  215. echo apply_filters( 'the_excerpt_rss', $output );
  216. }
  217. /**
  218. * Display the permalink to the post for use in feeds.
  219. *
  220. * @since 2.3.0
  221. */
  222. function the_permalink_rss() {
  223. /**
  224. * Filters the permalink to the post for use in feeds.
  225. *
  226. * @since 2.3.0
  227. *
  228. * @param string $post_permalink The current post permalink.
  229. */
  230. echo esc_url( apply_filters( 'the_permalink_rss', get_permalink() ) );
  231. }
  232. /**
  233. * Outputs the link to the comments for the current post in an xml safe way
  234. *
  235. * @since 3.0.0
  236. * @return none
  237. */
  238. function comments_link_feed() {
  239. /**
  240. * Filters the comments permalink for the current post.
  241. *
  242. * @since 3.6.0
  243. *
  244. * @param string $comment_permalink The current comment permalink with
  245. * '#comments' appended.
  246. */
  247. echo esc_url( apply_filters( 'comments_link_feed', get_comments_link() ) );
  248. }
  249. /**
  250. * Display the feed GUID for the current comment.
  251. *
  252. * @since 2.5.0
  253. *
  254. * @param int|WP_Comment $comment_id Optional comment object or id. Defaults to global comment object.
  255. */
  256. function comment_guid($comment_id = null) {
  257. echo esc_url( get_comment_guid($comment_id) );
  258. }
  259. /**
  260. * Retrieve the feed GUID for the current comment.
  261. *
  262. * @since 2.5.0
  263. *
  264. * @param int|WP_Comment $comment_id Optional comment object or id. Defaults to global comment object.
  265. * @return false|string false on failure or guid for comment on success.
  266. */
  267. function get_comment_guid($comment_id = null) {
  268. $comment = get_comment($comment_id);
  269. if ( !is_object($comment) )
  270. return false;
  271. return get_the_guid($comment->comment_post_ID) . '#comment-' . $comment->comment_ID;
  272. }
  273. /**
  274. * Display the link to the comments.
  275. *
  276. * @since 1.5.0
  277. * @since 4.4.0 Introduced the `$comment` argument.
  278. *
  279. * @param int|WP_Comment $comment Optional. Comment object or id. Defaults to global comment object.
  280. */
  281. function comment_link( $comment = null ) {
  282. /**
  283. * Filters the current comment's permalink.
  284. *
  285. * @since 3.6.0
  286. *
  287. * @see get_comment_link()
  288. *
  289. * @param string $comment_permalink The current comment permalink.
  290. */
  291. echo esc_url( apply_filters( 'comment_link', get_comment_link( $comment ) ) );
  292. }
  293. /**
  294. * Retrieve the current comment author for use in the feeds.
  295. *
  296. * @since 2.0.0
  297. *
  298. * @return string Comment Author
  299. */
  300. function get_comment_author_rss() {
  301. /**
  302. * Filters the current comment author for use in a feed.
  303. *
  304. * @since 1.5.0
  305. *
  306. * @see get_comment_author()
  307. *
  308. * @param string $comment_author The current comment author.
  309. */
  310. return apply_filters( 'comment_author_rss', get_comment_author() );
  311. }
  312. /**
  313. * Display the current comment author in the feed.
  314. *
  315. * @since 1.0.0
  316. */
  317. function comment_author_rss() {
  318. echo get_comment_author_rss();
  319. }
  320. /**
  321. * Display the current comment content for use in the feeds.
  322. *
  323. * @since 1.0.0
  324. */
  325. function comment_text_rss() {
  326. $comment_text = get_comment_text();
  327. /**
  328. * Filters the current comment content for use in a feed.
  329. *
  330. * @since 1.5.0
  331. *
  332. * @param string $comment_text The content of the current comment.
  333. */
  334. $comment_text = apply_filters( 'comment_text_rss', $comment_text );
  335. echo $comment_text;
  336. }
  337. /**
  338. * Retrieve all of the post categories, formatted for use in feeds.
  339. *
  340. * All of the categories for the current post in the feed loop, will be
  341. * retrieved and have feed markup added, so that they can easily be added to the
  342. * RSS2, Atom, or RSS1 and RSS0.91 RDF feeds.
  343. *
  344. * @since 2.1.0
  345. *
  346. * @param string $type Optional, default is the type returned by get_default_feed().
  347. * @return string All of the post categories for displaying in the feed.
  348. */
  349. function get_the_category_rss($type = null) {
  350. if ( empty($type) )
  351. $type = get_default_feed();
  352. $categories = get_the_category();
  353. $tags = get_the_tags();
  354. $the_list = '';
  355. $cat_names = array();
  356. $filter = 'rss';
  357. if ( 'atom' == $type )
  358. $filter = 'raw';
  359. if ( !empty($categories) ) foreach ( (array) $categories as $category ) {
  360. $cat_names[] = sanitize_term_field('name', $category->name, $category->term_id, 'category', $filter);
  361. }
  362. if ( !empty($tags) ) foreach ( (array) $tags as $tag ) {
  363. $cat_names[] = sanitize_term_field('name', $tag->name, $tag->term_id, 'post_tag', $filter);
  364. }
  365. $cat_names = array_unique($cat_names);
  366. foreach ( $cat_names as $cat_name ) {
  367. if ( 'rdf' == $type )
  368. $the_list .= "\t\t<dc:subject><![CDATA[$cat_name]]></dc:subject>\n";
  369. elseif ( 'atom' == $type )
  370. $the_list .= sprintf( '<category scheme="%1$s" term="%2$s" />', esc_attr( get_bloginfo_rss( 'url' ) ), esc_attr( $cat_name ) );
  371. else
  372. $the_list .= "\t\t<category><![CDATA[" . @html_entity_decode( $cat_name, ENT_COMPAT, get_option('blog_charset') ) . "]]></category>\n";
  373. }
  374. /**
  375. * Filters all of the post categories for display in a feed.
  376. *
  377. * @since 1.2.0
  378. *
  379. * @param string $the_list All of the RSS post categories.
  380. * @param string $type Type of feed. Possible values include 'rss2', 'atom'.
  381. * Default 'rss2'.
  382. */
  383. return apply_filters( 'the_category_rss', $the_list, $type );
  384. }
  385. /**
  386. * Display the post categories in the feed.
  387. *
  388. * @since 0.71
  389. * @see get_the_category_rss() For better explanation.
  390. *
  391. * @param string $type Optional, default is the type returned by get_default_feed().
  392. */
  393. function the_category_rss($type = null) {
  394. echo get_the_category_rss($type);
  395. }
  396. /**
  397. * Display the HTML type based on the blog setting.
  398. *
  399. * The two possible values are either 'xhtml' or 'html'.
  400. *
  401. * @since 2.2.0
  402. */
  403. function html_type_rss() {
  404. $type = get_bloginfo('html_type');
  405. if (strpos($type, 'xhtml') !== false)
  406. $type = 'xhtml';
  407. else
  408. $type = 'html';
  409. echo $type;
  410. }
  411. /**
  412. * Display the rss enclosure for the current post.
  413. *
  414. * Uses the global $post to check whether the post requires a password and if
  415. * the user has the password for the post. If not then it will return before
  416. * displaying.
  417. *
  418. * Also uses the function get_post_custom() to get the post's 'enclosure'
  419. * metadata field and parses the value to display the enclosure(s). The
  420. * enclosure(s) consist of enclosure HTML tag(s) with a URI and other
  421. * attributes.
  422. *
  423. * @since 1.5.0
  424. */
  425. function rss_enclosure() {
  426. if ( post_password_required() )
  427. return;
  428. foreach ( (array) get_post_custom() as $key => $val) {
  429. if ($key == 'enclosure') {
  430. foreach ( (array) $val as $enc ) {
  431. $enclosure = explode("\n", $enc);
  432. // only get the first element, e.g. audio/mpeg from 'audio/mpeg mpga mp2 mp3'
  433. $t = preg_split('/[ \t]/', trim($enclosure[2]) );
  434. $type = $t[0];
  435. /**
  436. * Filters the RSS enclosure HTML link tag for the current post.
  437. *
  438. * @since 2.2.0
  439. *
  440. * @param string $html_link_tag The HTML link tag with a URI and other attributes.
  441. */
  442. echo apply_filters( 'rss_enclosure', '<enclosure url="' . trim( htmlspecialchars( $enclosure[0] ) ) . '" length="' . trim( $enclosure[1] ) . '" type="' . $type . '" />' . "\n" );
  443. }
  444. }
  445. }
  446. }
  447. /**
  448. * Display the atom enclosure for the current post.
  449. *
  450. * Uses the global $post to check whether the post requires a password and if
  451. * the user has the password for the post. If not then it will return before
  452. * displaying.
  453. *
  454. * Also uses the function get_post_custom() to get the post's 'enclosure'
  455. * metadata field and parses the value to display the enclosure(s). The
  456. * enclosure(s) consist of link HTML tag(s) with a URI and other attributes.
  457. *
  458. * @since 2.2.0
  459. */
  460. function atom_enclosure() {
  461. if ( post_password_required() )
  462. return;
  463. foreach ( (array) get_post_custom() as $key => $val ) {
  464. if ($key == 'enclosure') {
  465. foreach ( (array) $val as $enc ) {
  466. $enclosure = explode("\n", $enc);
  467. /**
  468. * Filters the atom enclosure HTML link tag for the current post.
  469. *
  470. * @since 2.2.0
  471. *
  472. * @param string $html_link_tag The HTML link tag with a URI and other attributes.
  473. */
  474. echo apply_filters( 'atom_enclosure', '<link href="' . trim( htmlspecialchars( $enclosure[0] ) ) . '" rel="enclosure" length="' . trim( $enclosure[1] ) . '" type="' . trim( $enclosure[2] ) . '" />' . "\n" );
  475. }
  476. }
  477. }
  478. }
  479. /**
  480. * Determine the type of a string of data with the data formatted.
  481. *
  482. * Tell whether the type is text, html, or xhtml, per RFC 4287 section 3.1.
  483. *
  484. * In the case of WordPress, text is defined as containing no markup,
  485. * xhtml is defined as "well formed", and html as tag soup (i.e., the rest).
  486. *
  487. * Container div tags are added to xhtml values, per section 3.1.1.3.
  488. *
  489. * @link http://www.atomenabled.org/developers/syndication/atom-format-spec.php#rfc.section.3.1
  490. *
  491. * @since 2.5.0
  492. *
  493. * @param string $data Input string
  494. * @return array array(type, value)
  495. */
  496. function prep_atom_text_construct($data) {
  497. if (strpos($data, '<') === false && strpos($data, '&') === false) {
  498. return array('text', $data);
  499. }
  500. if ( ! function_exists( 'xml_parser_create' ) ) {
  501. trigger_error( __( "PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension." ) );
  502. return array( 'html', "<![CDATA[$data]]>" );
  503. }
  504. $parser = xml_parser_create();
  505. xml_parse($parser, '<div>' . $data . '</div>', true);
  506. $code = xml_get_error_code($parser);
  507. xml_parser_free($parser);
  508. if (!$code) {
  509. if (strpos($data, '<') === false) {
  510. return array('text', $data);
  511. } else {
  512. $data = "<div xmlns='http://www.w3.org/1999/xhtml'>$data</div>";
  513. return array('xhtml', $data);
  514. }
  515. }
  516. if (strpos($data, ']]>') === false) {
  517. return array('html', "<![CDATA[$data]]>");
  518. } else {
  519. return array('html', htmlspecialchars($data));
  520. }
  521. }
  522. /**
  523. * Displays Site Icon in atom feeds.
  524. *
  525. * @since 4.3.0
  526. *
  527. * @see get_site_icon_url()
  528. */
  529. function atom_site_icon() {
  530. $url = get_site_icon_url( 32 );
  531. if ( $url ) {
  532. echo "<icon>$url</icon>\n";
  533. }
  534. }
  535. /**
  536. * Displays Site Icon in RSS2.
  537. *
  538. * @since 4.3.0
  539. */
  540. function rss2_site_icon() {
  541. $rss_title = get_wp_title_rss();
  542. if ( empty( $rss_title ) ) {
  543. $rss_title = get_bloginfo_rss( 'name' );
  544. }
  545. $url = get_site_icon_url( 32 );
  546. if ( $url ) {
  547. echo '
  548. <image>
  549. <url>' . convert_chars( $url ) . '</url>
  550. <title>' . $rss_title . '</title>
  551. <link>' . get_bloginfo_rss( 'url' ) . '</link>
  552. <width>32</width>
  553. <height>32</height>
  554. </image> ' . "\n";
  555. }
  556. }
  557. /**
  558. * Display the link for the currently displayed feed in a XSS safe way.
  559. *
  560. * Generate a correct link for the atom:self element.
  561. *
  562. * @since 2.5.0
  563. */
  564. function self_link() {
  565. $host = @parse_url(home_url());
  566. /**
  567. * Filters the current feed URL.
  568. *
  569. * @since 3.6.0
  570. *
  571. * @see set_url_scheme()
  572. * @see wp_unslash()
  573. *
  574. * @param string $feed_link The link for the feed with set URL scheme.
  575. */
  576. echo esc_url( apply_filters( 'self_link', set_url_scheme( 'http://' . $host['host'] . wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) );
  577. }
  578. /**
  579. * Return the content type for specified feed type.
  580. *
  581. * @since 2.8.0
  582. *
  583. * @param string $type Type of feed. Possible values include 'rss', rss2', 'atom', and 'rdf'.
  584. */
  585. function feed_content_type( $type = '' ) {
  586. if ( empty($type) )
  587. $type = get_default_feed();
  588. $types = array(
  589. 'rss' => 'application/rss+xml',
  590. 'rss2' => 'application/rss+xml',
  591. 'rss-http' => 'text/xml',
  592. 'atom' => 'application/atom+xml',
  593. 'rdf' => 'application/rdf+xml'
  594. );
  595. $content_type = ( !empty($types[$type]) ) ? $types[$type] : 'application/octet-stream';
  596. /**
  597. * Filters the content type for a specific feed type.
  598. *
  599. * @since 2.8.0
  600. *
  601. * @param string $content_type Content type indicating the type of data that a feed contains.
  602. * @param string $type Type of feed. Possible values include 'rss', rss2', 'atom', and 'rdf'.
  603. */
  604. return apply_filters( 'feed_content_type', $content_type, $type );
  605. }
  606. /**
  607. * Build SimplePie object based on RSS or Atom feed from URL.
  608. *
  609. * @since 2.8.0
  610. *
  611. * @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged
  612. * using SimplePie's multifeed feature.
  613. * See also {@link ​http://simplepie.org/wiki/faq/typical_multifeed_gotchas}
  614. *
  615. * @return WP_Error|SimplePie WP_Error object on failure or SimplePie object on success
  616. */
  617. function fetch_feed( $url ) {
  618. if ( ! class_exists( 'SimplePie', false ) ) {
  619. require_once( ABSPATH . WPINC . '/class-simplepie.php' );
  620. }
  621. require_once( ABSPATH . WPINC . '/class-wp-feed-cache.php' );
  622. require_once( ABSPATH . WPINC . '/class-wp-feed-cache-transient.php' );
  623. require_once( ABSPATH . WPINC . '/class-wp-simplepie-file.php' );
  624. require_once( ABSPATH . WPINC . '/class-wp-simplepie-sanitize-kses.php' );
  625. $feed = new SimplePie();
  626. $feed->set_sanitize_class( 'WP_SimplePie_Sanitize_KSES' );
  627. // We must manually overwrite $feed->sanitize because SimplePie's
  628. // constructor sets it before we have a chance to set the sanitization class
  629. $feed->sanitize = new WP_SimplePie_Sanitize_KSES();
  630. $feed->set_cache_class( 'WP_Feed_Cache' );
  631. $feed->set_file_class( 'WP_SimplePie_File' );
  632. $feed->set_feed_url( $url );
  633. /** This filter is documented in wp-includes/class-wp-feed-cache-transient.php */
  634. $feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url ) );
  635. /**
  636. * Fires just before processing the SimplePie feed object.
  637. *
  638. * @since 3.0.0
  639. *
  640. * @param object &$feed SimplePie feed object, passed by reference.
  641. * @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged.
  642. */
  643. do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
  644. $feed->init();
  645. $feed->set_output_encoding( get_option( 'blog_charset' ) );
  646. if ( $feed->error() )
  647. return new WP_Error( 'simplepie-error', $feed->error() );
  648. return $feed;
  649. }