PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/feed.php

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