feed.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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. }
  178. /** This filter is documented in wp-includes/post-template.php */
  179. $content = apply_filters( 'the_content', get_the_content() );
  180. $content = str_replace( ']]>', ']]&gt;', $content );
  181. /**
  182. * Filters the post content for use in feeds.
  183. *
  184. * @since 2.9.0
  185. *
  186. * @param string $content The current post content.
  187. * @param string $feed_type Type of feed. Possible values include 'rss2', 'atom'.
  188. * Default 'rss2'.
  189. */
  190. return apply_filters( 'the_content_feed', $content, $feed_type );
  191. }
  192. /**
  193. * Display the post content for feeds.
  194. *
  195. * @since 2.9.0
  196. *
  197. * @param string $feed_type The type of feed. rss2 | atom | rss | rdf
  198. */
  199. function the_content_feed( $feed_type = null ) {
  200. echo get_the_content_feed( $feed_type );
  201. }
  202. /**
  203. * Display the post excerpt for the feed.
  204. *
  205. * @since 0.71
  206. */
  207. function the_excerpt_rss() {
  208. $output = get_the_excerpt();
  209. /**
  210. * Filters the post excerpt for a feed.
  211. *
  212. * @since 1.2.0
  213. *
  214. * @param string $output The current post excerpt.
  215. */
  216. echo apply_filters( 'the_excerpt_rss', $output );
  217. }
  218. /**
  219. * Display the permalink to the post for use in feeds.
  220. *
  221. * @since 2.3.0
  222. */
  223. function the_permalink_rss() {
  224. /**
  225. * Filters the permalink to the post for use in feeds.
  226. *
  227. * @since 2.3.0
  228. *
  229. * @param string $post_permalink The current post permalink.
  230. */
  231. echo esc_url( apply_filters( 'the_permalink_rss', get_permalink() ) );
  232. }
  233. /**
  234. * Outputs the link to the comments for the current post in an xml safe way
  235. *
  236. * @since 3.0.0
  237. * @return none
  238. */
  239. function comments_link_feed() {
  240. /**
  241. * Filters the comments permalink for the current post.
  242. *
  243. * @since 3.6.0
  244. *
  245. * @param string $comment_permalink The current comment permalink with
  246. * '#comments' appended.
  247. */
  248. echo esc_url( apply_filters( 'comments_link_feed', get_comments_link() ) );
  249. }
  250. /**
  251. * Display the feed GUID for the current comment.
  252. *
  253. * @since 2.5.0
  254. *
  255. * @param int|WP_Comment $comment_id Optional comment object or id. Defaults to global comment object.
  256. */
  257. function comment_guid( $comment_id = null ) {
  258. echo esc_url( get_comment_guid( $comment_id ) );
  259. }
  260. /**
  261. * Retrieve the feed GUID for the current comment.
  262. *
  263. * @since 2.5.0
  264. *
  265. * @param int|WP_Comment $comment_id Optional comment object or id. Defaults to global comment object.
  266. * @return false|string false on failure or guid for comment on success.
  267. */
  268. function get_comment_guid( $comment_id = null ) {
  269. $comment = get_comment( $comment_id );
  270. if ( ! is_object( $comment ) ) {
  271. return false;
  272. }
  273. return get_the_guid( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID;
  274. }
  275. /**
  276. * Display the link to the comments.
  277. *
  278. * @since 1.5.0
  279. * @since 4.4.0 Introduced the `$comment` argument.
  280. *
  281. * @param int|WP_Comment $comment Optional. Comment object or id. Defaults to global comment object.
  282. */
  283. function comment_link( $comment = null ) {
  284. /**
  285. * Filters the current comment's permalink.
  286. *
  287. * @since 3.6.0
  288. *
  289. * @see get_comment_link()
  290. *
  291. * @param string $comment_permalink The current comment permalink.
  292. */
  293. echo esc_url( apply_filters( 'comment_link', get_comment_link( $comment ) ) );
  294. }
  295. /**
  296. * Retrieve the current comment author for use in the feeds.
  297. *
  298. * @since 2.0.0
  299. *
  300. * @return string Comment Author
  301. */
  302. function get_comment_author_rss() {
  303. /**
  304. * Filters the current comment author for use in a feed.
  305. *
  306. * @since 1.5.0
  307. *
  308. * @see get_comment_author()
  309. *
  310. * @param string $comment_author The current comment author.
  311. */
  312. return apply_filters( 'comment_author_rss', get_comment_author() );
  313. }
  314. /**
  315. * Display the current comment author in the feed.
  316. *
  317. * @since 1.0.0
  318. */
  319. function comment_author_rss() {
  320. echo get_comment_author_rss();
  321. }
  322. /**
  323. * Display the current comment content for use in the feeds.
  324. *
  325. * @since 1.0.0
  326. */
  327. function comment_text_rss() {
  328. $comment_text = get_comment_text();
  329. /**
  330. * Filters the current comment content for use in a feed.
  331. *
  332. * @since 1.5.0
  333. *
  334. * @param string $comment_text The content of the current comment.
  335. */
  336. $comment_text = apply_filters( 'comment_text_rss', $comment_text );
  337. echo $comment_text;
  338. }
  339. /**
  340. * Retrieve all of the post categories, formatted for use in feeds.
  341. *
  342. * All of the categories for the current post in the feed loop, will be
  343. * retrieved and have feed markup added, so that they can easily be added to the
  344. * RSS2, Atom, or RSS1 and RSS0.91 RDF feeds.
  345. *
  346. * @since 2.1.0
  347. *
  348. * @param string $type Optional, default is the type returned by get_default_feed().
  349. * @return string All of the post categories for displaying in the feed.
  350. */
  351. function get_the_category_rss( $type = null ) {
  352. if ( empty( $type ) ) {
  353. $type = get_default_feed();
  354. }
  355. $categories = get_the_category();
  356. $tags = get_the_tags();
  357. $the_list = '';
  358. $cat_names = array();
  359. $filter = 'rss';
  360. if ( 'atom' == $type ) {
  361. $filter = 'raw';
  362. }
  363. if ( ! empty( $categories ) ) {
  364. foreach ( (array) $categories as $category ) {
  365. $cat_names[] = sanitize_term_field( 'name', $category->name, $category->term_id, 'category', $filter );
  366. }
  367. }
  368. if ( ! empty( $tags ) ) {
  369. foreach ( (array) $tags as $tag ) {
  370. $cat_names[] = sanitize_term_field( 'name', $tag->name, $tag->term_id, 'post_tag', $filter );
  371. }
  372. }
  373. $cat_names = array_unique( $cat_names );
  374. foreach ( $cat_names as $cat_name ) {
  375. if ( 'rdf' == $type ) {
  376. $the_list .= "\t\t<dc:subject><![CDATA[$cat_name]]></dc:subject>\n";
  377. } elseif ( 'atom' == $type ) {
  378. $the_list .= sprintf( '<category scheme="%1$s" term="%2$s" />', esc_attr( get_bloginfo_rss( 'url' ) ), esc_attr( $cat_name ) );
  379. } else {
  380. $the_list .= "\t\t<category><![CDATA[" . html_entity_decode( $cat_name, ENT_COMPAT, get_option( 'blog_charset' ) ) . "]]></category>\n";
  381. }
  382. }
  383. /**
  384. * Filters all of the post categories for display in a feed.
  385. *
  386. * @since 1.2.0
  387. *
  388. * @param string $the_list All of the RSS post categories.
  389. * @param string $type Type of feed. Possible values include 'rss2', 'atom'.
  390. * Default 'rss2'.
  391. */
  392. return apply_filters( 'the_category_rss', $the_list, $type );
  393. }
  394. /**
  395. * Display the post categories in the feed.
  396. *
  397. * @since 0.71
  398. * @see get_the_category_rss() For better explanation.
  399. *
  400. * @param string $type Optional, default is the type returned by get_default_feed().
  401. */
  402. function the_category_rss( $type = null ) {
  403. echo get_the_category_rss( $type );
  404. }
  405. /**
  406. * Display the HTML type based on the blog setting.
  407. *
  408. * The two possible values are either 'xhtml' or 'html'.
  409. *
  410. * @since 2.2.0
  411. */
  412. function html_type_rss() {
  413. $type = get_bloginfo( 'html_type' );
  414. if ( strpos( $type, 'xhtml' ) !== false ) {
  415. $type = 'xhtml';
  416. } else {
  417. $type = 'html';
  418. }
  419. echo $type;
  420. }
  421. /**
  422. * Display the rss enclosure for the current post.
  423. *
  424. * Uses the global $post to check whether the post requires a password and if
  425. * the user has the password for the post. If not then it will return before
  426. * displaying.
  427. *
  428. * Also uses the function get_post_custom() to get the post's 'enclosure'
  429. * metadata field and parses the value to display the enclosure(s). The
  430. * enclosure(s) consist of enclosure HTML tag(s) with a URI and other
  431. * attributes.
  432. *
  433. * @since 1.5.0
  434. */
  435. function rss_enclosure() {
  436. if ( post_password_required() ) {
  437. return;
  438. }
  439. foreach ( (array) get_post_custom() as $key => $val ) {
  440. if ( $key == 'enclosure' ) {
  441. foreach ( (array) $val as $enc ) {
  442. $enclosure = explode( "\n", $enc );
  443. // only get the first element, e.g. audio/mpeg from 'audio/mpeg mpga mp2 mp3'
  444. $t = preg_split( '/[ \t]/', trim( $enclosure[2] ) );
  445. $type = $t[0];
  446. /**
  447. * Filters the RSS enclosure HTML link tag for the current post.
  448. *
  449. * @since 2.2.0
  450. *
  451. * @param string $html_link_tag The HTML link tag with a URI and other attributes.
  452. */
  453. echo apply_filters( 'rss_enclosure', '<enclosure url="' . esc_url( trim( $enclosure[0] ) ) . '" length="' . absint( trim( $enclosure[1] ) ) . '" type="' . esc_attr( $type ) . '" />' . "\n" );
  454. }
  455. }
  456. }
  457. }
  458. /**
  459. * Display the atom enclosure for the current post.
  460. *
  461. * Uses the global $post to check whether the post requires a password and if
  462. * the user has the password for the post. If not then it will return before
  463. * displaying.
  464. *
  465. * Also uses the function get_post_custom() to get the post's 'enclosure'
  466. * metadata field and parses the value to display the enclosure(s). The
  467. * enclosure(s) consist of link HTML tag(s) with a URI and other attributes.
  468. *
  469. * @since 2.2.0
  470. */
  471. function atom_enclosure() {
  472. if ( post_password_required() ) {
  473. return;
  474. }
  475. foreach ( (array) get_post_custom() as $key => $val ) {
  476. if ( $key == 'enclosure' ) {
  477. foreach ( (array) $val as $enc ) {
  478. $enclosure = explode( "\n", $enc );
  479. /**
  480. * Filters the atom enclosure HTML link tag for the current post.
  481. *
  482. * @since 2.2.0
  483. *
  484. * @param string $html_link_tag The HTML link tag with a URI and other attributes.
  485. */
  486. echo apply_filters( 'atom_enclosure', '<link href="' . esc_url( trim( $enclosure[0] ) ) . '" rel="enclosure" length="' . absint( trim( $enclosure[1] ) ) . '" type="' . esc_attr( trim( $enclosure[2] ) ) . '" />' . "\n" );
  487. }
  488. }
  489. }
  490. }
  491. /**
  492. * Determine the type of a string of data with the data formatted.
  493. *
  494. * Tell whether the type is text, html, or xhtml, per RFC 4287 section 3.1.
  495. *
  496. * In the case of WordPress, text is defined as containing no markup,
  497. * xhtml is defined as "well formed", and html as tag soup (i.e., the rest).
  498. *
  499. * Container div tags are added to xhtml values, per section 3.1.1.3.
  500. *
  501. * @link http://www.atomenabled.org/developers/syndication/atom-format-spec.php#rfc.section.3.1
  502. *
  503. * @since 2.5.0
  504. *
  505. * @param string $data Input string
  506. * @return array array(type, value)
  507. */
  508. function prep_atom_text_construct( $data ) {
  509. if ( strpos( $data, '<' ) === false && strpos( $data, '&' ) === false ) {
  510. return array( 'text', $data );
  511. }
  512. if ( ! function_exists( 'xml_parser_create' ) ) {
  513. trigger_error( __( "PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension." ) );
  514. return array( 'html', "<![CDATA[$data]]>" );
  515. }
  516. $parser = xml_parser_create();
  517. xml_parse( $parser, '<div>' . $data . '</div>', true );
  518. $code = xml_get_error_code( $parser );
  519. xml_parser_free( $parser );
  520. if ( ! $code ) {
  521. if ( strpos( $data, '<' ) === false ) {
  522. return array( 'text', $data );
  523. } else {
  524. $data = "<div xmlns='http://www.w3.org/1999/xhtml'>$data</div>";
  525. return array( 'xhtml', $data );
  526. }
  527. }
  528. if ( strpos( $data, ']]>' ) === false ) {
  529. return array( 'html', "<![CDATA[$data]]>" );
  530. } else {
  531. return array( 'html', htmlspecialchars( $data ) );
  532. }
  533. }
  534. /**
  535. * Displays Site Icon in atom feeds.
  536. *
  537. * @since 4.3.0
  538. *
  539. * @see get_site_icon_url()
  540. */
  541. function atom_site_icon() {
  542. $url = get_site_icon_url( 32 );
  543. if ( $url ) {
  544. echo '<icon>' . convert_chars( $url ) . "</icon>\n";
  545. }
  546. }
  547. /**
  548. * Displays Site Icon in RSS2.
  549. *
  550. * @since 4.3.0
  551. */
  552. function rss2_site_icon() {
  553. $rss_title = get_wp_title_rss();
  554. if ( empty( $rss_title ) ) {
  555. $rss_title = get_bloginfo_rss( 'name' );
  556. }
  557. $url = get_site_icon_url( 32 );
  558. if ( $url ) {
  559. echo '
  560. <image>
  561. <url>' . convert_chars( $url ) . '</url>
  562. <title>' . $rss_title . '</title>
  563. <link>' . get_bloginfo_rss( 'url' ) . '</link>
  564. <width>32</width>
  565. <height>32</height>
  566. </image> ' . "\n";
  567. }
  568. }
  569. /**
  570. * Returns the link for the currently displayed feed.
  571. *
  572. * @since 5.3.0
  573. *
  574. * @return string Correct link for the atom:self element.
  575. */
  576. function get_self_link() {
  577. $host = @parse_url( home_url() );
  578. return set_url_scheme( 'http://' . $host['host'] . wp_unslash( $_SERVER['REQUEST_URI'] ) );
  579. }
  580. /**
  581. * Display the link for the currently displayed feed in a XSS safe way.
  582. *
  583. * Generate a correct link for the atom:self element.
  584. *
  585. * @since 2.5.0
  586. */
  587. function self_link() {
  588. /**
  589. * Filters the current feed URL.
  590. *
  591. * @since 3.6.0
  592. *
  593. * @see set_url_scheme()
  594. * @see wp_unslash()
  595. *
  596. * @param string $feed_link The link for the feed with set URL scheme.
  597. */
  598. echo esc_url( apply_filters( 'self_link', get_self_link() ) );
  599. }
  600. /**
  601. * Get the timestamp of the most recently modified post from WP_Query.
  602. *
  603. * If viewing a comment feed, the timestamp of the most recently modified
  604. * comment will be returned.
  605. *
  606. * @global WP_Query $wp_query WordPress Query object.
  607. *
  608. * @since 5.2.0
  609. *
  610. * @param string $format Format of the timestamp to return, passed to mysql2date.
  611. *
  612. * @return string The timestamp.
  613. */
  614. function get_feed_build_date( $format ) {
  615. global $wp_query;
  616. if ( empty( $wp_query ) || ! $wp_query->have_posts() ) {
  617. // Fallback to last time any post was modified or published.
  618. return get_lastpostmodified( 'GMT' );
  619. }
  620. // Extract the post modified times from the posts.
  621. $modified_times = wp_list_pluck( $wp_query->posts, 'post_modified_gmt' );
  622. // If this is a comment feed, check those objects too.
  623. if ( $wp_query->is_comment_feed() && $wp_query->comment_count ) {
  624. // Extract the comment modified times from the comments.
  625. $comment_times = wp_list_pluck( $wp_query->comments, 'comment_date_gmt' );
  626. // Add the comment times to the post times for comparison.
  627. $modified_times = array_merge( $modified_times, $comment_times );
  628. }
  629. // Determine the maximum modified time.
  630. $max_modified_time = mysql2date( $format, max( $modified_times ), false );
  631. /**
  632. * Filters the date the last post or comment in the query was modified.
  633. *
  634. * @since 5.2.0
  635. *
  636. * @param string $max_modified_time Date the last post or comment was modified in the query.
  637. * @param string $format The date format requested in get_feed_build_date.
  638. */
  639. return apply_filters( 'get_feed_build_date', $max_modified_time, $format );
  640. }
  641. /**
  642. * Return the content type for specified feed type.
  643. *
  644. * @since 2.8.0
  645. *
  646. * @param string $type Type of feed. Possible values include 'rss', rss2', 'atom', and 'rdf'.
  647. */
  648. function feed_content_type( $type = '' ) {
  649. if ( empty( $type ) ) {
  650. $type = get_default_feed();
  651. }
  652. $types = array(
  653. 'rss' => 'application/rss+xml',
  654. 'rss2' => 'application/rss+xml',
  655. 'rss-http' => 'text/xml',
  656. 'atom' => 'application/atom+xml',
  657. 'rdf' => 'application/rdf+xml',
  658. );
  659. $content_type = ( ! empty( $types[ $type ] ) ) ? $types[ $type ] : 'application/octet-stream';
  660. /**
  661. * Filters the content type for a specific feed type.
  662. *
  663. * @since 2.8.0
  664. *
  665. * @param string $content_type Content type indicating the type of data that a feed contains.
  666. * @param string $type Type of feed. Possible values include 'rss', rss2', 'atom', and 'rdf'.
  667. */
  668. return apply_filters( 'feed_content_type', $content_type, $type );
  669. }
  670. /**
  671. * Build SimplePie object based on RSS or Atom feed from URL.
  672. *
  673. * @since 2.8.0
  674. *
  675. * @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged
  676. * using SimplePie's multifeed feature.
  677. * See also {@link http://simplepie.org/wiki/faq/typical_multifeed_gotchas}
  678. *
  679. * @return WP_Error|SimplePie WP_Error object on failure or SimplePie object on success
  680. */
  681. function fetch_feed( $url ) {
  682. if ( ! class_exists( 'SimplePie', false ) ) {
  683. require_once( ABSPATH . WPINC . '/class-simplepie.php' );
  684. }
  685. require_once( ABSPATH . WPINC . '/class-wp-feed-cache.php' );
  686. require_once( ABSPATH . WPINC . '/class-wp-feed-cache-transient.php' );
  687. require_once( ABSPATH . WPINC . '/class-wp-simplepie-file.php' );
  688. require_once( ABSPATH . WPINC . '/class-wp-simplepie-sanitize-kses.php' );
  689. $feed = new SimplePie();
  690. $feed->set_sanitize_class( 'WP_SimplePie_Sanitize_KSES' );
  691. // We must manually overwrite $feed->sanitize because SimplePie's
  692. // constructor sets it before we have a chance to set the sanitization class
  693. $feed->sanitize = new WP_SimplePie_Sanitize_KSES();
  694. $feed->set_cache_class( 'WP_Feed_Cache' );
  695. $feed->set_file_class( 'WP_SimplePie_File' );
  696. $feed->set_feed_url( $url );
  697. /** This filter is documented in wp-includes/class-wp-feed-cache-transient.php */
  698. $feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url ) );
  699. /**
  700. * Fires just before processing the SimplePie feed object.
  701. *
  702. * @since 3.0.0
  703. *
  704. * @param object $feed SimplePie feed object (passed by reference).
  705. * @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged.
  706. */
  707. do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
  708. $feed->init();
  709. $feed->set_output_encoding( get_option( 'blog_charset' ) );
  710. if ( $feed->error() ) {
  711. return new WP_Error( 'simplepie-error', $feed->error() );
  712. }
  713. return $feed;
  714. }