PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-includes/feed.php

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