PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/feed.php

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