PageRenderTime 217ms CodeModel.GetById 31ms RepoModel.GetById 8ms app.codeStats 0ms

/wp-includes/feed.php

https://gitlab.com/geyson/geyson
PHP | 690 lines | 220 code | 58 blank | 412 comment | 34 complexity | 3e507db1a5595336e573ce200ba00df2 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  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. *
  72. * @return string Default feed, or for example 'rss2', 'atom', etc.
  73. */
  74. function get_default_feed() {
  75. /**
  76. * Filter the default feed type.
  77. *
  78. * @since 2.5.0
  79. *
  80. * @param string $feed_type Type of default feed. Possible values include 'rss2', 'atom'.
  81. * Default 'rss2'.
  82. */
  83. $default_feed = apply_filters( 'default_feed', 'rss2' );
  84. return 'rss' == $default_feed ? 'rss2' : $default_feed;
  85. }
  86. /**
  87. * Retrieve the blog title for the feed title.
  88. *
  89. * @since 2.2.0
  90. *
  91. * @param string $sep Optional. How to separate the title. See wp_title() for more info.
  92. * @return string Error message on failure or blog title on success.
  93. */
  94. function get_wp_title_rss( $sep = '&#187;' ) {
  95. $title = wp_title( $sep, false );
  96. if ( is_wp_error( $title ) ) {
  97. return $title->get_error_message();
  98. }
  99. if ( $title && $sep && ' ' !== substr( $title, 0, 1 ) ) {
  100. $title = " $sep " . $title;
  101. }
  102. /**
  103. * Filter the blog title for use as the feed title.
  104. *
  105. * @since 2.2.0
  106. *
  107. * @param string $title The current blog title.
  108. * @param string $sep Separator used by wp_title().
  109. */
  110. $title = apply_filters( 'get_wp_title_rss', $title, $sep );
  111. return $title;
  112. }
  113. /**
  114. * Display the blog title for display of the feed title.
  115. *
  116. * @since 2.2.0
  117. * @see wp_title() $sep parameter usage.
  118. *
  119. * @param string $sep Optional.
  120. */
  121. function wp_title_rss( $sep = '&#187;' ) {
  122. /**
  123. * Filter the blog title for display of the feed title.
  124. *
  125. * @since 2.2.0
  126. *
  127. * @see get_wp_title_rss()
  128. *
  129. * @param string $wp_title The current blog title.
  130. * @param string $sep Separator used by wp_title().
  131. */
  132. echo apply_filters( 'wp_title_rss', get_wp_title_rss( $sep ), $sep );
  133. }
  134. /**
  135. * Retrieve the current post title for the feed.
  136. *
  137. * @since 2.0.0
  138. *
  139. * @return string Current post title.
  140. */
  141. function get_the_title_rss() {
  142. $title = get_the_title();
  143. /**
  144. * Filter the post title for use in a feed.
  145. *
  146. * @since 1.2.0
  147. *
  148. * @param string $title The current post title.
  149. */
  150. $title = apply_filters( 'the_title_rss', $title );
  151. return $title;
  152. }
  153. /**
  154. * Display the post title in the feed.
  155. *
  156. * @since 0.71
  157. */
  158. function the_title_rss() {
  159. echo get_the_title_rss();
  160. }
  161. /**
  162. * Retrieve the post content for feeds.
  163. *
  164. * @since 2.9.0
  165. * @see get_the_content()
  166. *
  167. * @param string $feed_type The type of feed. rss2 | atom | rss | rdf
  168. * @return string The filtered content.
  169. */
  170. function get_the_content_feed($feed_type = null) {
  171. if ( !$feed_type )
  172. $feed_type = get_default_feed();
  173. /** This filter is documented in wp-includes/post-template.php */
  174. $content = apply_filters( 'the_content', get_the_content() );
  175. $content = str_replace(']]>', ']]&gt;', $content);
  176. /**
  177. * Filter the post content for use in feeds.
  178. *
  179. * @since 2.9.0
  180. *
  181. * @param string $content The current post content.
  182. * @param string $feed_type Type of feed. Possible values include 'rss2', 'atom'.
  183. * Default 'rss2'.
  184. */
  185. return apply_filters( 'the_content_feed', $content, $feed_type );
  186. }
  187. /**
  188. * Display the post content for feeds.
  189. *
  190. * @since 2.9.0
  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 false|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. *
  291. * @return string Comment Author
  292. */
  293. function get_comment_author_rss() {
  294. /**
  295. * Filter the current comment author for use in a feed.
  296. *
  297. * @since 1.5.0
  298. *
  299. * @see get_comment_author()
  300. *
  301. * @param string $comment_author The current comment author.
  302. */
  303. return apply_filters( 'comment_author_rss', get_comment_author() );
  304. }
  305. /**
  306. * Display the current comment author in the feed.
  307. *
  308. * @since 1.0.0
  309. */
  310. function comment_author_rss() {
  311. echo get_comment_author_rss();
  312. }
  313. /**
  314. * Display the current comment content for use in the feeds.
  315. *
  316. * @since 1.0.0
  317. */
  318. function comment_text_rss() {
  319. $comment_text = get_comment_text();
  320. /**
  321. * Filter the current comment content for use in a feed.
  322. *
  323. * @since 1.5.0
  324. *
  325. * @param string $comment_text The content of the current comment.
  326. */
  327. $comment_text = apply_filters( 'comment_text_rss', $comment_text );
  328. echo $comment_text;
  329. }
  330. /**
  331. * Retrieve all of the post categories, formatted for use in feeds.
  332. *
  333. * All of the categories for the current post in the feed loop, will be
  334. * retrieved and have feed markup added, so that they can easily be added to the
  335. * RSS2, Atom, or RSS1 and RSS0.91 RDF feeds.
  336. *
  337. * @since 2.1.0
  338. *
  339. * @param string $type Optional, default is the type returned by get_default_feed().
  340. * @return string All of the post categories for displaying in the feed.
  341. */
  342. function get_the_category_rss($type = null) {
  343. if ( empty($type) )
  344. $type = get_default_feed();
  345. $categories = get_the_category();
  346. $tags = get_the_tags();
  347. $the_list = '';
  348. $cat_names = array();
  349. $filter = 'rss';
  350. if ( 'atom' == $type )
  351. $filter = 'raw';
  352. if ( !empty($categories) ) foreach ( (array) $categories as $category ) {
  353. $cat_names[] = sanitize_term_field('name', $category->name, $category->term_id, 'category', $filter);
  354. }
  355. if ( !empty($tags) ) foreach ( (array) $tags as $tag ) {
  356. $cat_names[] = sanitize_term_field('name', $tag->name, $tag->term_id, 'post_tag', $filter);
  357. }
  358. $cat_names = array_unique($cat_names);
  359. foreach ( $cat_names as $cat_name ) {
  360. if ( 'rdf' == $type )
  361. $the_list .= "\t\t<dc:subject><![CDATA[$cat_name]]></dc:subject>\n";
  362. elseif ( 'atom' == $type )
  363. $the_list .= sprintf( '<category scheme="%1$s" term="%2$s" />', esc_attr( get_bloginfo_rss( 'url' ) ), esc_attr( $cat_name ) );
  364. else
  365. $the_list .= "\t\t<category><![CDATA[" . @html_entity_decode( $cat_name, ENT_COMPAT, get_option('blog_charset') ) . "]]></category>\n";
  366. }
  367. /**
  368. * Filter all of the post categories for display in a feed.
  369. *
  370. * @since 1.2.0
  371. *
  372. * @param string $the_list All of the RSS post categories.
  373. * @param string $type Type of feed. Possible values include 'rss2', 'atom'.
  374. * Default 'rss2'.
  375. */
  376. return apply_filters( 'the_category_rss', $the_list, $type );
  377. }
  378. /**
  379. * Display the post categories in the feed.
  380. *
  381. * @since 0.71
  382. * @see get_the_category_rss() For better explanation.
  383. *
  384. * @param string $type Optional, default is the type returned by get_default_feed().
  385. */
  386. function the_category_rss($type = null) {
  387. echo get_the_category_rss($type);
  388. }
  389. /**
  390. * Display the HTML type based on the blog setting.
  391. *
  392. * The two possible values are either 'xhtml' or 'html'.
  393. *
  394. * @since 2.2.0
  395. */
  396. function html_type_rss() {
  397. $type = get_bloginfo('html_type');
  398. if (strpos($type, 'xhtml') !== false)
  399. $type = 'xhtml';
  400. else
  401. $type = 'html';
  402. echo $type;
  403. }
  404. /**
  405. * Display the rss enclosure for the current post.
  406. *
  407. * Uses the global $post to check whether the post requires a password and if
  408. * the user has the password for the post. If not then it will return before
  409. * displaying.
  410. *
  411. * Also uses the function get_post_custom() to get the post's 'enclosure'
  412. * metadata field and parses the value to display the enclosure(s). The
  413. * enclosure(s) consist of enclosure HTML tag(s) with a URI and other
  414. * attributes.
  415. *
  416. * @since 1.5.0
  417. */
  418. function rss_enclosure() {
  419. if ( post_password_required() )
  420. return;
  421. foreach ( (array) get_post_custom() as $key => $val) {
  422. if ($key == 'enclosure') {
  423. foreach ( (array) $val as $enc ) {
  424. $enclosure = explode("\n", $enc);
  425. // only get the first element, e.g. audio/mpeg from 'audio/mpeg mpga mp2 mp3'
  426. $t = preg_split('/[ \t]/', trim($enclosure[2]) );
  427. $type = $t[0];
  428. /**
  429. * Filter the RSS enclosure HTML link tag for the current post.
  430. *
  431. * @since 2.2.0
  432. *
  433. * @param string $html_link_tag The HTML link tag with a URI and other attributes.
  434. */
  435. echo apply_filters( 'rss_enclosure', '<enclosure url="' . trim( htmlspecialchars( $enclosure[0] ) ) . '" length="' . trim( $enclosure[1] ) . '" type="' . $type . '" />' . "\n" );
  436. }
  437. }
  438. }
  439. }
  440. /**
  441. * Display the atom enclosure for the current post.
  442. *
  443. * Uses the global $post to check whether the post requires a password and if
  444. * the user has the password for the post. If not then it will return before
  445. * displaying.
  446. *
  447. * Also uses the function get_post_custom() to get the post's 'enclosure'
  448. * metadata field and parses the value to display the enclosure(s). The
  449. * enclosure(s) consist of link HTML tag(s) with a URI and other attributes.
  450. *
  451. * @since 2.2.0
  452. */
  453. function atom_enclosure() {
  454. if ( post_password_required() )
  455. return;
  456. foreach ( (array) get_post_custom() as $key => $val ) {
  457. if ($key == 'enclosure') {
  458. foreach ( (array) $val as $enc ) {
  459. $enclosure = explode("\n", $enc);
  460. /**
  461. * Filter the atom enclosure HTML link tag for the current post.
  462. *
  463. * @since 2.2.0
  464. *
  465. * @param string $html_link_tag The HTML link tag with a URI and other attributes.
  466. */
  467. echo apply_filters( 'atom_enclosure', '<link href="' . trim( htmlspecialchars( $enclosure[0] ) ) . '" rel="enclosure" length="' . trim( $enclosure[1] ) . '" type="' . trim( $enclosure[2] ) . '" />' . "\n" );
  468. }
  469. }
  470. }
  471. }
  472. /**
  473. * Determine the type of a string of data with the data formatted.
  474. *
  475. * Tell whether the type is text, html, or xhtml, per RFC 4287 section 3.1.
  476. *
  477. * In the case of WordPress, text is defined as containing no markup,
  478. * xhtml is defined as "well formed", and html as tag soup (i.e., the rest).
  479. *
  480. * Container div tags are added to xhtml values, per section 3.1.1.3.
  481. *
  482. * @link http://www.atomenabled.org/developers/syndication/atom-format-spec.php#rfc.section.3.1
  483. *
  484. * @since 2.5.0
  485. *
  486. * @param string $data Input string
  487. * @return array array(type, value)
  488. */
  489. function prep_atom_text_construct($data) {
  490. if (strpos($data, '<') === false && strpos($data, '&') === false) {
  491. return array('text', $data);
  492. }
  493. $parser = xml_parser_create();
  494. xml_parse($parser, '<div>' . $data . '</div>', true);
  495. $code = xml_get_error_code($parser);
  496. xml_parser_free($parser);
  497. if (!$code) {
  498. if (strpos($data, '<') === false) {
  499. return array('text', $data);
  500. } else {
  501. $data = "<div xmlns='http://www.w3.org/1999/xhtml'>$data</div>";
  502. return array('xhtml', $data);
  503. }
  504. }
  505. if (strpos($data, ']]>') === false) {
  506. return array('html', "<![CDATA[$data]]>");
  507. } else {
  508. return array('html', htmlspecialchars($data));
  509. }
  510. }
  511. /**
  512. * Displays Site Icon in atom feeds.
  513. *
  514. * @since 4.3.0
  515. *
  516. * @see get_site_icon_url()
  517. */
  518. function atom_site_icon() {
  519. $url = get_site_icon_url( 32 );
  520. if ( $url ) {
  521. echo "<icon>$url</icon>\n";
  522. }
  523. }
  524. /**
  525. * Displays Site Icon in RSS2.
  526. *
  527. * @since 4.3.0
  528. */
  529. function rss2_site_icon() {
  530. $rss_title = get_wp_title_rss();
  531. if ( empty( $rss_title ) ) {
  532. $rss_title = get_bloginfo_rss( 'name' );
  533. }
  534. $url = get_site_icon_url( 32 );
  535. if ( $url ) {
  536. echo '
  537. <image>
  538. <url>' . convert_chars( $url ) . '</url>
  539. <title>' . $rss_title . '</title>
  540. <link>' . get_bloginfo_rss( 'url' ) . '</link>
  541. <width>32</width>
  542. <height>32</height>
  543. </image> ' . "\n";
  544. }
  545. }
  546. /**
  547. * Display the link for the currently displayed feed in a XSS safe way.
  548. *
  549. * Generate a correct link for the atom:self element.
  550. *
  551. * @since 2.5.0
  552. */
  553. function self_link() {
  554. $host = @parse_url(home_url());
  555. /**
  556. * Filter the current feed URL.
  557. *
  558. * @since 3.6.0
  559. *
  560. * @see set_url_scheme()
  561. * @see wp_unslash()
  562. *
  563. * @param string $feed_link The link for the feed with set URL scheme.
  564. */
  565. echo esc_url( apply_filters( 'self_link', set_url_scheme( 'http://' . $host['host'] . wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) );
  566. }
  567. /**
  568. * Return the content type for specified feed type.
  569. *
  570. * @since 2.8.0
  571. */
  572. function feed_content_type( $type = '' ) {
  573. if ( empty($type) )
  574. $type = get_default_feed();
  575. $types = array(
  576. 'rss' => 'application/rss+xml',
  577. 'rss2' => 'application/rss+xml',
  578. 'rss-http' => 'text/xml',
  579. 'atom' => 'application/atom+xml',
  580. 'rdf' => 'application/rdf+xml'
  581. );
  582. $content_type = ( !empty($types[$type]) ) ? $types[$type] : 'application/octet-stream';
  583. /**
  584. * Filter the content type for a specific feed type.
  585. *
  586. * @since 2.8.0
  587. *
  588. * @param string $content_type Content type indicating the type of data that a feed contains.
  589. * @param string $type Type of feed. Possible values include 'rss2', 'atom'.
  590. * Default 'rss2'.
  591. */
  592. return apply_filters( 'feed_content_type', $content_type, $type );
  593. }
  594. /**
  595. * Build SimplePie object based on RSS or Atom feed from URL.
  596. *
  597. * @since 2.8.0
  598. *
  599. * @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged
  600. * using SimplePie's multifeed feature.
  601. * See also {@link ​http://simplepie.org/wiki/faq/typical_multifeed_gotchas}
  602. *
  603. * @return WP_Error|SimplePie WP_Error object on failure or SimplePie object on success
  604. */
  605. function fetch_feed( $url ) {
  606. require_once( ABSPATH . WPINC . '/class-feed.php' );
  607. $feed = new SimplePie();
  608. $feed->set_sanitize_class( 'WP_SimplePie_Sanitize_KSES' );
  609. // We must manually overwrite $feed->sanitize because SimplePie's
  610. // constructor sets it before we have a chance to set the sanitization class
  611. $feed->sanitize = new WP_SimplePie_Sanitize_KSES();
  612. $feed->set_cache_class( 'WP_Feed_Cache' );
  613. $feed->set_file_class( 'WP_SimplePie_File' );
  614. $feed->set_feed_url( $url );
  615. /** This filter is documented in wp-includes/class-feed.php */
  616. $feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url ) );
  617. /**
  618. * Fires just before processing the SimplePie feed object.
  619. *
  620. * @since 3.0.0
  621. *
  622. * @param object &$feed SimplePie feed object, passed by reference.
  623. * @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged.
  624. */
  625. do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
  626. $feed->init();
  627. $feed->set_output_encoding( get_option( 'blog_charset' ) );
  628. $feed->handle_content_type();
  629. if ( $feed->error() )
  630. return new WP_Error( 'simplepie-error', $feed->error() );
  631. return $feed;
  632. }