PageRenderTime 44ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/feed.php

https://bitbucket.org/Wallynm/iptb
PHP | 547 lines | 196 code | 49 blank | 302 comment | 31 complexity | 5865ca306949bc81176d845601682ee6 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0, GPL-3.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. * @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. */
  136. function get_the_content_feed($feed_type = null) {
  137. if ( !$feed_type )
  138. $feed_type = get_default_feed();
  139. $content = apply_filters('the_content', get_the_content());
  140. $content = str_replace(']]>', ']]&gt;', $content);
  141. return apply_filters('the_content_feed', $content, $feed_type);
  142. }
  143. /**
  144. * Display the post content for feeds.
  145. *
  146. * @package WordPress
  147. * @subpackage Feed
  148. * @since 2.9.0
  149. * @uses apply_filters() Calls 'the_content_feed' on the content before processing.
  150. * @see get_the_content()
  151. *
  152. * @param string $feed_type The type of feed. rss2 | atom | rss | rdf
  153. */
  154. function the_content_feed($feed_type = null) {
  155. echo get_the_content_feed($feed_type);
  156. }
  157. /**
  158. * Display the post excerpt for the feed.
  159. *
  160. * @package WordPress
  161. * @subpackage Feed
  162. * @since 0.71
  163. * @uses apply_filters() Calls 'the_excerpt_rss' hook on the excerpt.
  164. */
  165. function the_excerpt_rss() {
  166. $output = get_the_excerpt();
  167. echo apply_filters('the_excerpt_rss', $output);
  168. }
  169. /**
  170. * Display the permalink to the post for use in feeds.
  171. *
  172. * @package WordPress
  173. * @subpackage Feed
  174. * @since 2.3.0
  175. * @uses apply_filters() Call 'the_permalink_rss' on the post permalink
  176. */
  177. function the_permalink_rss() {
  178. echo esc_url( apply_filters('the_permalink_rss', get_permalink() ));
  179. }
  180. /**
  181. * Outputs the link to the comments for the current post in an xml safe way
  182. *
  183. * @since 3.0.0
  184. * @return none
  185. */
  186. function comments_link_feed() {
  187. echo esc_url( get_comments_link() );
  188. }
  189. /**
  190. * Display the feed GUID for the current comment.
  191. *
  192. * @package WordPress
  193. * @subpackage Feed
  194. * @since 2.5.0
  195. *
  196. * @param int|object $comment_id Optional comment object or id. Defaults to global comment object.
  197. */
  198. function comment_guid($comment_id = null) {
  199. echo esc_url( get_comment_guid($comment_id) );
  200. }
  201. /**
  202. * Retrieve the feed GUID for the current comment.
  203. *
  204. * @package WordPress
  205. * @subpackage Feed
  206. * @since 2.5.0
  207. *
  208. * @param int|object $comment_id Optional comment object or id. Defaults to global comment object.
  209. * @return bool|string false on failure or guid for comment on success.
  210. */
  211. function get_comment_guid($comment_id = null) {
  212. $comment = get_comment($comment_id);
  213. if ( !is_object($comment) )
  214. return false;
  215. return get_the_guid($comment->comment_post_ID) . '#comment-' . $comment->comment_ID;
  216. }
  217. /**
  218. * Display the link to the comments.
  219. *
  220. * @since 1.5.0
  221. */
  222. function comment_link() {
  223. echo esc_url( get_comment_link() );
  224. }
  225. /**
  226. * Retrieve the current comment author for use in the feeds.
  227. *
  228. * @package WordPress
  229. * @subpackage Feed
  230. * @since 2.0.0
  231. * @uses apply_filters() Calls 'comment_author_rss' hook on comment author.
  232. * @uses get_comment_author()
  233. *
  234. * @return string Comment Author
  235. */
  236. function get_comment_author_rss() {
  237. return apply_filters('comment_author_rss', get_comment_author() );
  238. }
  239. /**
  240. * Display the current comment author in the feed.
  241. *
  242. * @package WordPress
  243. * @subpackage Feed
  244. * @since 1.0.0
  245. */
  246. function comment_author_rss() {
  247. echo get_comment_author_rss();
  248. }
  249. /**
  250. * Display the current comment content for use in the feeds.
  251. *
  252. * @package WordPress
  253. * @subpackage Feed
  254. * @since 1.0.0
  255. * @uses apply_filters() Calls 'comment_text_rss' filter on comment content.
  256. * @uses get_comment_text()
  257. */
  258. function comment_text_rss() {
  259. $comment_text = get_comment_text();
  260. $comment_text = apply_filters('comment_text_rss', $comment_text);
  261. echo $comment_text;
  262. }
  263. /**
  264. * Retrieve all of the post categories, formatted for use in feeds.
  265. *
  266. * All of the categories for the current post in the feed loop, will be
  267. * retrieved and have feed markup added, so that they can easily be added to the
  268. * RSS2, Atom, or RSS1 and RSS0.91 RDF feeds.
  269. *
  270. * @package WordPress
  271. * @subpackage Feed
  272. * @since 2.1.0
  273. * @uses apply_filters()
  274. *
  275. * @param string $type Optional, default is the type returned by get_default_feed().
  276. * @return string All of the post categories for displaying in the feed.
  277. */
  278. function get_the_category_rss($type = null) {
  279. if ( empty($type) )
  280. $type = get_default_feed();
  281. $categories = get_the_category();
  282. $tags = get_the_tags();
  283. $the_list = '';
  284. $cat_names = array();
  285. $filter = 'rss';
  286. if ( 'atom' == $type )
  287. $filter = 'raw';
  288. if ( !empty($categories) ) foreach ( (array) $categories as $category ) {
  289. $cat_names[] = sanitize_term_field('name', $category->name, $category->term_id, 'category', $filter);
  290. }
  291. if ( !empty($tags) ) foreach ( (array) $tags as $tag ) {
  292. $cat_names[] = sanitize_term_field('name', $tag->name, $tag->term_id, 'post_tag', $filter);
  293. }
  294. $cat_names = array_unique($cat_names);
  295. foreach ( $cat_names as $cat_name ) {
  296. if ( 'rdf' == $type )
  297. $the_list .= "\t\t<dc:subject><![CDATA[$cat_name]]></dc:subject>\n";
  298. elseif ( 'atom' == $type )
  299. $the_list .= sprintf( '<category scheme="%1$s" term="%2$s" />', esc_attr( apply_filters( 'get_bloginfo_rss', get_bloginfo( 'url' ) ) ), esc_attr( $cat_name ) );
  300. else
  301. $the_list .= "\t\t<category><![CDATA[" . @html_entity_decode( $cat_name, ENT_COMPAT, get_option('blog_charset') ) . "]]></category>\n";
  302. }
  303. return apply_filters('the_category_rss', $the_list, $type);
  304. }
  305. /**
  306. * Display the post categories in the feed.
  307. *
  308. * @package WordPress
  309. * @subpackage Feed
  310. * @since 0.71
  311. * @see get_the_category_rss() For better explanation.
  312. *
  313. * @param string $type Optional, default is the type returned by get_default_feed().
  314. */
  315. function the_category_rss($type = null) {
  316. echo get_the_category_rss($type);
  317. }
  318. /**
  319. * Display the HTML type based on the blog setting.
  320. *
  321. * The two possible values are either 'xhtml' or 'html'.
  322. *
  323. * @package WordPress
  324. * @subpackage Feed
  325. * @since 2.2.0
  326. */
  327. function html_type_rss() {
  328. $type = get_bloginfo('html_type');
  329. if (strpos($type, 'xhtml') !== false)
  330. $type = 'xhtml';
  331. else
  332. $type = 'html';
  333. echo $type;
  334. }
  335. /**
  336. * Display the rss enclosure for the current post.
  337. *
  338. * Uses the global $post to check whether the post requires a password and if
  339. * the user has the password for the post. If not then it will return before
  340. * displaying.
  341. *
  342. * Also uses the function get_post_custom() to get the post's 'enclosure'
  343. * metadata field and parses the value to display the enclosure(s). The
  344. * enclosure(s) consist of enclosure HTML tag(s) with a URI and other
  345. * attributes.
  346. *
  347. * @package WordPress
  348. * @subpackage Template
  349. * @since 1.5.0
  350. * @uses apply_filters() Calls 'rss_enclosure' hook on rss enclosure.
  351. * @uses get_post_custom() To get the current post enclosure metadata.
  352. */
  353. function rss_enclosure() {
  354. if ( post_password_required() )
  355. return;
  356. foreach ( (array) get_post_custom() as $key => $val) {
  357. if ($key == 'enclosure') {
  358. foreach ( (array) $val as $enc ) {
  359. $enclosure = explode("\n", $enc);
  360. //only get the the first element eg, audio/mpeg from 'audio/mpeg mpga mp2 mp3'
  361. $t = preg_split('/[ \t]/', trim($enclosure[2]) );
  362. $type = $t[0];
  363. echo apply_filters('rss_enclosure', '<enclosure url="' . trim(htmlspecialchars($enclosure[0])) . '" length="' . trim($enclosure[1]) . '" type="' . $type . '" />' . "\n");
  364. }
  365. }
  366. }
  367. }
  368. /**
  369. * Display the atom enclosure for the current post.
  370. *
  371. * Uses the global $post to check whether the post requires a password and if
  372. * the user has the password for the post. If not then it will return before
  373. * displaying.
  374. *
  375. * Also uses the function get_post_custom() to get the post's 'enclosure'
  376. * metadata field and parses the value to display the enclosure(s). The
  377. * enclosure(s) consist of link HTML tag(s) with a URI and other attributes.
  378. *
  379. * @package WordPress
  380. * @subpackage Template
  381. * @since 2.2.0
  382. * @uses apply_filters() Calls 'atom_enclosure' hook on atom enclosure.
  383. * @uses get_post_custom() To get the current post enclosure metadata.
  384. */
  385. function atom_enclosure() {
  386. if ( post_password_required() )
  387. return;
  388. foreach ( (array) get_post_custom() as $key => $val ) {
  389. if ($key == 'enclosure') {
  390. foreach ( (array) $val as $enc ) {
  391. $enclosure = split("\n", $enc);
  392. echo apply_filters('atom_enclosure', '<link href="' . trim(htmlspecialchars($enclosure[0])) . '" rel="enclosure" length="' . trim($enclosure[1]) . '" type="' . trim($enclosure[2]) . '" />' . "\n");
  393. }
  394. }
  395. }
  396. }
  397. /**
  398. * Determine the type of a string of data with the data formatted.
  399. *
  400. * Tell whether the type is text, html, or xhtml, per RFC 4287 section 3.1.
  401. *
  402. * In the case of WordPress, text is defined as containing no markup,
  403. * xhtml is defined as "well formed", and html as tag soup (i.e., the rest).
  404. *
  405. * Container div tags are added to xhtml values, per section 3.1.1.3.
  406. *
  407. * @link http://www.atomenabled.org/developers/syndication/atom-format-spec.php#rfc.section.3.1
  408. *
  409. * @package WordPress
  410. * @subpackage Feed
  411. * @since 2.5
  412. *
  413. * @param string $data Input string
  414. * @return array array(type, value)
  415. */
  416. function prep_atom_text_construct($data) {
  417. if (strpos($data, '<') === false && strpos($data, '&') === false) {
  418. return array('text', $data);
  419. }
  420. $parser = xml_parser_create();
  421. xml_parse($parser, '<div>' . $data . '</div>', true);
  422. $code = xml_get_error_code($parser);
  423. xml_parser_free($parser);
  424. if (!$code) {
  425. if (strpos($data, '<') === false) {
  426. return array('text', $data);
  427. } else {
  428. $data = "<div xmlns='http://www.w3.org/1999/xhtml'>$data</div>";
  429. return array('xhtml', $data);
  430. }
  431. }
  432. if (strpos($data, ']]>') == false) {
  433. return array('html', "<![CDATA[$data]]>");
  434. } else {
  435. return array('html', htmlspecialchars($data));
  436. }
  437. }
  438. /**
  439. * Display the link for the currently displayed feed in a XSS safe way.
  440. *
  441. * Generate a correct link for the atom:self element.
  442. *
  443. * @package WordPress
  444. * @subpackage Feed
  445. * @since 2.5
  446. */
  447. function self_link() {
  448. $host = @parse_url(home_url());
  449. $host = $host['host'];
  450. echo esc_url(
  451. 'http'
  452. . ( (isset($_SERVER['https']) && $_SERVER['https'] == 'on') ? 's' : '' ) . '://'
  453. . $host
  454. . stripslashes($_SERVER['REQUEST_URI'])
  455. );
  456. }
  457. /**
  458. * Return the content type for specified feed type.
  459. *
  460. * @package WordPress
  461. * @subpackage Feed
  462. * @since 2.8.0
  463. */
  464. function feed_content_type( $type = '' ) {
  465. if ( empty($type) )
  466. $type = get_default_feed();
  467. $types = array(
  468. 'rss' => 'application/rss+xml',
  469. 'rss2' => 'application/rss+xml',
  470. 'rss-http' => 'text/xml',
  471. 'atom' => 'application/atom+xml',
  472. 'rdf' => 'application/rdf+xml'
  473. );
  474. $content_type = ( !empty($types[$type]) ) ? $types[$type] : 'application/octet-stream';
  475. return apply_filters( 'feed_content_type', $content_type, $type );
  476. }
  477. /**
  478. * Build SimplePie object based on RSS or Atom feed from URL.
  479. *
  480. * @since 2.8
  481. *
  482. * @param string $url URL to retrieve feed
  483. * @return WP_Error|SimplePie WP_Error object on failure or SimplePie object on success
  484. */
  485. function fetch_feed($url) {
  486. require_once (ABSPATH . WPINC . '/class-feed.php');
  487. $feed = new SimplePie();
  488. $feed->set_feed_url($url);
  489. $feed->set_cache_class('WP_Feed_Cache');
  490. $feed->set_file_class('WP_SimplePie_File');
  491. $feed->set_cache_duration(apply_filters('wp_feed_cache_transient_lifetime', 43200, $url));
  492. do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
  493. $feed->init();
  494. $feed->handle_content_type();
  495. if ( $feed->error() )
  496. return new WP_Error('simplepie-error', $feed->error());
  497. return $feed;
  498. }