PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/feed.php

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