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

/APP/wp-includes/feed.php

https://bitbucket.org/AFelipeTrujillo/goblog
PHP | 653 lines | 192 code | 52 blank | 409 comment | 29 complexity | 8cea0eff37b2240119282dde472dea59 MD5 | raw file
Possible License(s): GPL-2.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. /** This filter is documented in wp-includes/feed.php */
  366. $the_list .= sprintf( '<category scheme="%1$s" term="%2$s" />', esc_attr( apply_filters( 'get_bloginfo_rss', get_bloginfo( 'url' ) ) ), esc_attr( $cat_name ) );
  367. else
  368. $the_list .= "\t\t<category><![CDATA[" . @html_entity_decode( $cat_name, ENT_COMPAT, get_option('blog_charset') ) . "]]></category>\n";
  369. }
  370. /**
  371. * Filter all of the post categories for display in a feed.
  372. *
  373. * @since 1.2.0
  374. *
  375. * @param string $the_list All of the RSS post categories.
  376. * @param string $type Type of feed. Possible values include 'rss2', 'atom'.
  377. * Default 'rss2'.
  378. */
  379. return apply_filters( 'the_category_rss', $the_list, $type );
  380. }
  381. /**
  382. * Display the post categories in the feed.
  383. *
  384. * @since 0.71
  385. * @see get_the_category_rss() For better explanation.
  386. *
  387. * @param string $type Optional, default is the type returned by get_default_feed().
  388. */
  389. function the_category_rss($type = null) {
  390. echo get_the_category_rss($type);
  391. }
  392. /**
  393. * Display the HTML type based on the blog setting.
  394. *
  395. * The two possible values are either 'xhtml' or 'html'.
  396. *
  397. * @since 2.2.0
  398. */
  399. function html_type_rss() {
  400. $type = get_bloginfo('html_type');
  401. if (strpos($type, 'xhtml') !== false)
  402. $type = 'xhtml';
  403. else
  404. $type = 'html';
  405. echo $type;
  406. }
  407. /**
  408. * Display the rss enclosure for the current post.
  409. *
  410. * Uses the global $post to check whether the post requires a password and if
  411. * the user has the password for the post. If not then it will return before
  412. * displaying.
  413. *
  414. * Also uses the function get_post_custom() to get the post's 'enclosure'
  415. * metadata field and parses the value to display the enclosure(s). The
  416. * enclosure(s) consist of enclosure HTML tag(s) with a URI and other
  417. * attributes.
  418. *
  419. * @since 1.5.0
  420. * @uses get_post_custom() To get the current post enclosure metadata.
  421. */
  422. function rss_enclosure() {
  423. if ( post_password_required() )
  424. return;
  425. foreach ( (array) get_post_custom() as $key => $val) {
  426. if ($key == 'enclosure') {
  427. foreach ( (array) $val as $enc ) {
  428. $enclosure = explode("\n", $enc);
  429. // only get the first element, e.g. audio/mpeg from 'audio/mpeg mpga mp2 mp3'
  430. $t = preg_split('/[ \t]/', trim($enclosure[2]) );
  431. $type = $t[0];
  432. /**
  433. * Filter the RSS enclosure HTML link tag for the current post.
  434. *
  435. * @since 2.2.0
  436. *
  437. * @param string $html_link_tag The HTML link tag with a URI and other attributes.
  438. */
  439. echo apply_filters( 'rss_enclosure', '<enclosure url="' . trim( htmlspecialchars( $enclosure[0] ) ) . '" length="' . trim( $enclosure[1] ) . '" type="' . $type . '" />' . "\n" );
  440. }
  441. }
  442. }
  443. }
  444. /**
  445. * Display the atom enclosure for the current post.
  446. *
  447. * Uses the global $post to check whether the post requires a password and if
  448. * the user has the password for the post. If not then it will return before
  449. * displaying.
  450. *
  451. * Also uses the function get_post_custom() to get the post's 'enclosure'
  452. * metadata field and parses the value to display the enclosure(s). The
  453. * enclosure(s) consist of link HTML tag(s) with a URI and other attributes.
  454. *
  455. * @since 2.2.0
  456. * @uses get_post_custom() To get the current post enclosure metadata.
  457. */
  458. function atom_enclosure() {
  459. if ( post_password_required() )
  460. return;
  461. foreach ( (array) get_post_custom() as $key => $val ) {
  462. if ($key == 'enclosure') {
  463. foreach ( (array) $val as $enc ) {
  464. $enclosure = explode("\n", $enc);
  465. /**
  466. * Filter the atom enclosure HTML link tag for the current post.
  467. *
  468. * @since 2.2.0
  469. *
  470. * @param string $html_link_tag The HTML link tag with a URI and other attributes.
  471. */
  472. echo apply_filters( 'atom_enclosure', '<link href="' . trim( htmlspecialchars( $enclosure[0] ) ) . '" rel="enclosure" length="' . trim( $enclosure[1] ) . '" type="' . trim( $enclosure[2] ) . '" />' . "\n" );
  473. }
  474. }
  475. }
  476. }
  477. /**
  478. * Determine the type of a string of data with the data formatted.
  479. *
  480. * Tell whether the type is text, html, or xhtml, per RFC 4287 section 3.1.
  481. *
  482. * In the case of WordPress, text is defined as containing no markup,
  483. * xhtml is defined as "well formed", and html as tag soup (i.e., the rest).
  484. *
  485. * Container div tags are added to xhtml values, per section 3.1.1.3.
  486. *
  487. * @link http://www.atomenabled.org/developers/syndication/atom-format-spec.php#rfc.section.3.1
  488. *
  489. * @since 2.5.0
  490. *
  491. * @param string $data Input string
  492. * @return array array(type, value)
  493. */
  494. function prep_atom_text_construct($data) {
  495. if (strpos($data, '<') === false && strpos($data, '&') === false) {
  496. return array('text', $data);
  497. }
  498. $parser = xml_parser_create();
  499. xml_parse($parser, '<div>' . $data . '</div>', true);
  500. $code = xml_get_error_code($parser);
  501. xml_parser_free($parser);
  502. if (!$code) {
  503. if (strpos($data, '<') === false) {
  504. return array('text', $data);
  505. } else {
  506. $data = "<div xmlns='http://www.w3.org/1999/xhtml'>$data</div>";
  507. return array('xhtml', $data);
  508. }
  509. }
  510. if (strpos($data, ']]>') == false) {
  511. return array('html', "<![CDATA[$data]]>");
  512. } else {
  513. return array('html', htmlspecialchars($data));
  514. }
  515. }
  516. /**
  517. * Display the link for the currently displayed feed in a XSS safe way.
  518. *
  519. * Generate a correct link for the atom:self element.
  520. *
  521. * @since 2.5.0
  522. */
  523. function self_link() {
  524. $host = @parse_url(home_url());
  525. /**
  526. * Filter the current feed URL.
  527. *
  528. * @since 3.6.0
  529. *
  530. * @see set_url_scheme()
  531. * @see wp_unslash()
  532. *
  533. * @param string $feed_link The link for the feed with set URL scheme.
  534. */
  535. echo esc_url( apply_filters( 'self_link', set_url_scheme( 'http://' . $host['host'] . wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) );
  536. }
  537. /**
  538. * Return the content type for specified feed type.
  539. *
  540. * @since 2.8.0
  541. */
  542. function feed_content_type( $type = '' ) {
  543. if ( empty($type) )
  544. $type = get_default_feed();
  545. $types = array(
  546. 'rss' => 'application/rss+xml',
  547. 'rss2' => 'application/rss+xml',
  548. 'rss-http' => 'text/xml',
  549. 'atom' => 'application/atom+xml',
  550. 'rdf' => 'application/rdf+xml'
  551. );
  552. $content_type = ( !empty($types[$type]) ) ? $types[$type] : 'application/octet-stream';
  553. /**
  554. * Filter the content type for a specific feed type.
  555. *
  556. * @since 2.8.0
  557. *
  558. * @param string $content_type Content type indicating the type of data that a feed contains.
  559. * @param string $type Type of feed. Possible values include 'rss2', 'atom'.
  560. * Default 'rss2'.
  561. */
  562. return apply_filters( 'feed_content_type', $content_type, $type );
  563. }
  564. /**
  565. * Build SimplePie object based on RSS or Atom feed from URL.
  566. *
  567. * @since 2.8.0
  568. *
  569. * @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged
  570. * using SimplePie's multifeed feature.
  571. * See also {@link ​http://simplepie.org/wiki/faq/typical_multifeed_gotchas}
  572. *
  573. * @return WP_Error|SimplePie WP_Error object on failure or SimplePie object on success
  574. */
  575. function fetch_feed( $url ) {
  576. require_once( ABSPATH . WPINC . '/class-feed.php' );
  577. $feed = new SimplePie();
  578. $feed->set_sanitize_class( 'WP_SimplePie_Sanitize_KSES' );
  579. // We must manually overwrite $feed->sanitize because SimplePie's
  580. // constructor sets it before we have a chance to set the sanitization class
  581. $feed->sanitize = new WP_SimplePie_Sanitize_KSES();
  582. $feed->set_cache_class( 'WP_Feed_Cache' );
  583. $feed->set_file_class( 'WP_SimplePie_File' );
  584. $feed->set_feed_url( $url );
  585. /** This filter is documented in wp-includes/class-feed.php */
  586. $feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url ) );
  587. /**
  588. * Fires just before processing the SimplePie feed object.
  589. *
  590. * @since 3.0.0
  591. *
  592. * @param object &$feed SimplePie feed object, passed by reference.
  593. * @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged.
  594. */
  595. do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
  596. $feed->init();
  597. $feed->handle_content_type();
  598. if ( $feed->error() )
  599. return new WP_Error( 'simplepie-error', $feed->error() );
  600. return $feed;
  601. }