PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/feed.php

https://github.com/dedavidd/piratenpartij.nl
PHP | 659 lines | 196 code | 55 blank | 408 comment | 32 complexity | f1bcf2a1ccf3f14d7bc7b507481f4e2e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, 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. * @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. if ( $title && $sep && ' ' !== substr( $title, 0, 1 ) ) {
  101. $title = " $sep " . $title;
  102. }
  103. /**
  104. * Filter the blog title for use as the feed title.
  105. *
  106. * @since 2.2.0
  107. *
  108. * @param string $title The current blog title.
  109. * @param string $sep Separator used by wp_title().
  110. */
  111. $title = apply_filters( 'get_wp_title_rss', $title, $sep );
  112. return $title;
  113. }
  114. /**
  115. * Display the blog title for display of the feed title.
  116. *
  117. * @since 2.2.0
  118. * @see wp_title() $sep parameter usage.
  119. *
  120. * @param string $sep Optional.
  121. */
  122. function wp_title_rss( $sep = '&#187;' ) {
  123. /**
  124. * Filter the blog title for display of the feed title.
  125. *
  126. * @since 2.2.0
  127. *
  128. * @see get_wp_title_rss()
  129. *
  130. * @param string $wp_title The current blog title.
  131. * @param string $sep Separator used by wp_title().
  132. */
  133. echo apply_filters( 'wp_title_rss', get_wp_title_rss( $sep ), $sep );
  134. }
  135. /**
  136. * Retrieve the current post title for the feed.
  137. *
  138. * @since 2.0.0
  139. *
  140. * @return string Current post title.
  141. */
  142. function get_the_title_rss() {
  143. $title = get_the_title();
  144. /**
  145. * Filter the post title for use in a feed.
  146. *
  147. * @since 1.2.0
  148. *
  149. * @param string $title The current post title.
  150. */
  151. $title = apply_filters( 'the_title_rss', $title );
  152. return $title;
  153. }
  154. /**
  155. * Display the post title in the feed.
  156. *
  157. * @since 0.71
  158. * @uses get_the_title_rss() Used to retrieve current post title.
  159. */
  160. function the_title_rss() {
  161. echo get_the_title_rss();
  162. }
  163. /**
  164. * Retrieve the post content for feeds.
  165. *
  166. * @since 2.9.0
  167. * @see get_the_content()
  168. *
  169. * @param string $feed_type The type of feed. rss2 | atom | rss | rdf
  170. * @return string The filtered content.
  171. */
  172. function get_the_content_feed($feed_type = null) {
  173. if ( !$feed_type )
  174. $feed_type = get_default_feed();
  175. /** This filter is documented in wp-admin/post-template.php */
  176. $content = apply_filters( 'the_content', get_the_content() );
  177. $content = str_replace(']]>', ']]&gt;', $content);
  178. /**
  179. * Filter the post content for use in feeds.
  180. *
  181. * @since 2.9.0
  182. *
  183. * @param string $content The current post content.
  184. * @param string $feed_type Type of feed. Possible values include 'rss2', 'atom'.
  185. * Default 'rss2'.
  186. */
  187. return apply_filters( 'the_content_feed', $content, $feed_type );
  188. }
  189. /**
  190. * Display the post content for feeds.
  191. *
  192. * @since 2.9.0
  193. * @uses apply_filters() Calls 'the_content_feed' on the content before processing.
  194. * @see get_the_content()
  195. *
  196. * @param string $feed_type The type of feed. rss2 | atom | rss | rdf
  197. */
  198. function the_content_feed($feed_type = null) {
  199. echo get_the_content_feed($feed_type);
  200. }
  201. /**
  202. * Display the post excerpt for the feed.
  203. *
  204. * @since 0.71
  205. */
  206. function the_excerpt_rss() {
  207. $output = get_the_excerpt();
  208. /**
  209. * Filter the post excerpt for a feed.
  210. *
  211. * @since 1.2.0
  212. *
  213. * @param string $output The current post excerpt.
  214. */
  215. echo apply_filters( 'the_excerpt_rss', $output );
  216. }
  217. /**
  218. * Display the permalink to the post for use in feeds.
  219. *
  220. * @since 2.3.0
  221. */
  222. function the_permalink_rss() {
  223. /**
  224. * Filter the permalink to the post for use in feeds.
  225. *
  226. * @since 2.3.0
  227. *
  228. * @param string $post_permalink The current post permalink.
  229. */
  230. echo esc_url( apply_filters( 'the_permalink_rss', get_permalink() ) );
  231. }
  232. /**
  233. * Outputs the link to the comments for the current post in an xml safe way
  234. *
  235. * @since 3.0.0
  236. * @return none
  237. */
  238. function comments_link_feed() {
  239. /**
  240. * Filter the comments permalink for the current post.
  241. *
  242. * @since 3.6.0
  243. *
  244. * @param string $comment_permalink The current comment permalink with
  245. * '#comments' appended.
  246. */
  247. echo esc_url( apply_filters( 'comments_link_feed', get_comments_link() ) );
  248. }
  249. /**
  250. * Display the feed GUID for the current comment.
  251. *
  252. * @since 2.5.0
  253. *
  254. * @param int|object $comment_id Optional comment object or id. Defaults to global comment object.
  255. */
  256. function comment_guid($comment_id = null) {
  257. echo esc_url( get_comment_guid($comment_id) );
  258. }
  259. /**
  260. * Retrieve the feed GUID for the current comment.
  261. *
  262. * @since 2.5.0
  263. *
  264. * @param int|object $comment_id Optional comment object or id. Defaults to global comment object.
  265. * @return bool|string false on failure or guid for comment on success.
  266. */
  267. function get_comment_guid($comment_id = null) {
  268. $comment = get_comment($comment_id);
  269. if ( !is_object($comment) )
  270. return false;
  271. return get_the_guid($comment->comment_post_ID) . '#comment-' . $comment->comment_ID;
  272. }
  273. /**
  274. * Display the link to the comments.
  275. *
  276. * @since 1.5.0
  277. */
  278. function comment_link() {
  279. /**
  280. * Filter the current comment's permalink.
  281. *
  282. * @since 3.6.0
  283. *
  284. * @see get_comment_link()
  285. *
  286. * @param string $comment_permalink The current comment permalink.
  287. */
  288. echo esc_url( apply_filters( 'comment_link', get_comment_link() ) );
  289. }
  290. /**
  291. * Retrieve the current comment author for use in the feeds.
  292. *
  293. * @since 2.0.0
  294. * @uses get_comment_author()
  295. *
  296. * @return string Comment Author
  297. */
  298. function get_comment_author_rss() {
  299. /**
  300. * Filter the current comment author for use in a feed.
  301. *
  302. * @since 1.5.0
  303. *
  304. * @see get_comment_author()
  305. *
  306. * @param string $comment_author The current comment author.
  307. */
  308. return apply_filters( 'comment_author_rss', get_comment_author() );
  309. }
  310. /**
  311. * Display the current comment author in the feed.
  312. *
  313. * @since 1.0.0
  314. */
  315. function comment_author_rss() {
  316. echo get_comment_author_rss();
  317. }
  318. /**
  319. * Display the current comment content for use in the feeds.
  320. *
  321. * @since 1.0.0
  322. * @uses get_comment_text()
  323. */
  324. function comment_text_rss() {
  325. $comment_text = get_comment_text();
  326. /**
  327. * Filter the current comment content for use in a feed.
  328. *
  329. * @since 1.5.0
  330. *
  331. * @param string $comment_text The content of the current comment.
  332. */
  333. $comment_text = apply_filters( 'comment_text_rss', $comment_text );
  334. echo $comment_text;
  335. }
  336. /**
  337. * Retrieve all of the post categories, formatted for use in feeds.
  338. *
  339. * All of the categories for the current post in the feed loop, will be
  340. * retrieved and have feed markup added, so that they can easily be added to the
  341. * RSS2, Atom, or RSS1 and RSS0.91 RDF feeds.
  342. *
  343. * @since 2.1.0
  344. *
  345. * @param string $type Optional, default is the type returned by get_default_feed().
  346. * @return string All of the post categories for displaying in the feed.
  347. */
  348. function get_the_category_rss($type = null) {
  349. if ( empty($type) )
  350. $type = get_default_feed();
  351. $categories = get_the_category();
  352. $tags = get_the_tags();
  353. $the_list = '';
  354. $cat_names = array();
  355. $filter = 'rss';
  356. if ( 'atom' == $type )
  357. $filter = 'raw';
  358. if ( !empty($categories) ) foreach ( (array) $categories as $category ) {
  359. $cat_names[] = sanitize_term_field('name', $category->name, $category->term_id, 'category', $filter);
  360. }
  361. if ( !empty($tags) ) foreach ( (array) $tags as $tag ) {
  362. $cat_names[] = sanitize_term_field('name', $tag->name, $tag->term_id, 'post_tag', $filter);
  363. }
  364. $cat_names = array_unique($cat_names);
  365. foreach ( $cat_names as $cat_name ) {
  366. if ( 'rdf' == $type )
  367. $the_list .= "\t\t<dc:subject><![CDATA[$cat_name]]></dc:subject>\n";
  368. elseif ( 'atom' == $type )
  369. $the_list .= sprintf( '<category scheme="%1$s" term="%2$s" />', esc_attr( get_bloginfo_rss( 'url' ) ), esc_attr( $cat_name ) );
  370. else
  371. $the_list .= "\t\t<category><![CDATA[" . @html_entity_decode( $cat_name, ENT_COMPAT, get_option('blog_charset') ) . "]]></category>\n";
  372. }
  373. /**
  374. * Filter all of the post categories for display in a feed.
  375. *
  376. * @since 1.2.0
  377. *
  378. * @param string $the_list All of the RSS post categories.
  379. * @param string $type Type of feed. Possible values include 'rss2', 'atom'.
  380. * Default 'rss2'.
  381. */
  382. return apply_filters( 'the_category_rss', $the_list, $type );
  383. }
  384. /**
  385. * Display the post categories in the feed.
  386. *
  387. * @since 0.71
  388. * @see get_the_category_rss() For better explanation.
  389. *
  390. * @param string $type Optional, default is the type returned by get_default_feed().
  391. */
  392. function the_category_rss($type = null) {
  393. echo get_the_category_rss($type);
  394. }
  395. /**
  396. * Display the HTML type based on the blog setting.
  397. *
  398. * The two possible values are either 'xhtml' or 'html'.
  399. *
  400. * @since 2.2.0
  401. */
  402. function html_type_rss() {
  403. $type = get_bloginfo('html_type');
  404. if (strpos($type, 'xhtml') !== false)
  405. $type = 'xhtml';
  406. else
  407. $type = 'html';
  408. echo $type;
  409. }
  410. /**
  411. * Display the rss enclosure for the current post.
  412. *
  413. * Uses the global $post to check whether the post requires a password and if
  414. * the user has the password for the post. If not then it will return before
  415. * displaying.
  416. *
  417. * Also uses the function get_post_custom() to get the post's 'enclosure'
  418. * metadata field and parses the value to display the enclosure(s). The
  419. * enclosure(s) consist of enclosure HTML tag(s) with a URI and other
  420. * attributes.
  421. *
  422. * @since 1.5.0
  423. * @uses get_post_custom() To get the current post enclosure metadata.
  424. */
  425. function rss_enclosure() {
  426. if ( post_password_required() )
  427. return;
  428. foreach ( (array) get_post_custom() as $key => $val) {
  429. if ($key == 'enclosure') {
  430. foreach ( (array) $val as $enc ) {
  431. $enclosure = explode("\n", $enc);
  432. // only get the first element, e.g. audio/mpeg from 'audio/mpeg mpga mp2 mp3'
  433. $t = preg_split('/[ \t]/', trim($enclosure[2]) );
  434. $type = $t[0];
  435. /**
  436. * Filter the RSS enclosure HTML link tag for the current post.
  437. *
  438. * @since 2.2.0
  439. *
  440. * @param string $html_link_tag The HTML link tag with a URI and other attributes.
  441. */
  442. echo apply_filters( 'rss_enclosure', '<enclosure url="' . trim( htmlspecialchars( $enclosure[0] ) ) . '" length="' . trim( $enclosure[1] ) . '" type="' . $type . '" />' . "\n" );
  443. }
  444. }
  445. }
  446. }
  447. /**
  448. * Display the atom enclosure for the current post.
  449. *
  450. * Uses the global $post to check whether the post requires a password and if
  451. * the user has the password for the post. If not then it will return before
  452. * displaying.
  453. *
  454. * Also uses the function get_post_custom() to get the post's 'enclosure'
  455. * metadata field and parses the value to display the enclosure(s). The
  456. * enclosure(s) consist of link HTML tag(s) with a URI and other attributes.
  457. *
  458. * @since 2.2.0
  459. * @uses get_post_custom() To get the current post enclosure metadata.
  460. */
  461. function atom_enclosure() {
  462. if ( post_password_required() )
  463. return;
  464. foreach ( (array) get_post_custom() as $key => $val ) {
  465. if ($key == 'enclosure') {
  466. foreach ( (array) $val as $enc ) {
  467. $enclosure = explode("\n", $enc);
  468. /**
  469. * Filter the atom enclosure HTML link tag for the current post.
  470. *
  471. * @since 2.2.0
  472. *
  473. * @param string $html_link_tag The HTML link tag with a URI and other attributes.
  474. */
  475. echo apply_filters( 'atom_enclosure', '<link href="' . trim( htmlspecialchars( $enclosure[0] ) ) . '" rel="enclosure" length="' . trim( $enclosure[1] ) . '" type="' . trim( $enclosure[2] ) . '" />' . "\n" );
  476. }
  477. }
  478. }
  479. }
  480. /**
  481. * Determine the type of a string of data with the data formatted.
  482. *
  483. * Tell whether the type is text, html, or xhtml, per RFC 4287 section 3.1.
  484. *
  485. * In the case of WordPress, text is defined as containing no markup,
  486. * xhtml is defined as "well formed", and html as tag soup (i.e., the rest).
  487. *
  488. * Container div tags are added to xhtml values, per section 3.1.1.3.
  489. *
  490. * @link http://www.atomenabled.org/developers/syndication/atom-format-spec.php#rfc.section.3.1
  491. *
  492. * @since 2.5.0
  493. *
  494. * @param string $data Input string
  495. * @return array array(type, value)
  496. */
  497. function prep_atom_text_construct($data) {
  498. if (strpos($data, '<') === false && strpos($data, '&') === false) {
  499. return array('text', $data);
  500. }
  501. $parser = xml_parser_create();
  502. xml_parse($parser, '<div>' . $data . '</div>', true);
  503. $code = xml_get_error_code($parser);
  504. xml_parser_free($parser);
  505. if (!$code) {
  506. if (strpos($data, '<') === false) {
  507. return array('text', $data);
  508. } else {
  509. $data = "<div xmlns='http://www.w3.org/1999/xhtml'>$data</div>";
  510. return array('xhtml', $data);
  511. }
  512. }
  513. if (strpos($data, ']]>') == false) {
  514. return array('html', "<![CDATA[$data]]>");
  515. } else {
  516. return array('html', htmlspecialchars($data));
  517. }
  518. }
  519. /**
  520. * Display the link for the currently displayed feed in a XSS safe way.
  521. *
  522. * Generate a correct link for the atom:self element.
  523. *
  524. * @since 2.5.0
  525. */
  526. function self_link() {
  527. $host = @parse_url(home_url());
  528. /**
  529. * Filter the current feed URL.
  530. *
  531. * @since 3.6.0
  532. *
  533. * @see set_url_scheme()
  534. * @see wp_unslash()
  535. *
  536. * @param string $feed_link The link for the feed with set URL scheme.
  537. */
  538. echo esc_url( apply_filters( 'self_link', set_url_scheme( 'http://' . $host['host'] . wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) );
  539. }
  540. /**
  541. * Return the content type for specified feed type.
  542. *
  543. * @since 2.8.0
  544. */
  545. function feed_content_type( $type = '' ) {
  546. if ( empty($type) )
  547. $type = get_default_feed();
  548. $types = array(
  549. 'rss' => 'application/rss+xml',
  550. 'rss2' => 'application/rss+xml',
  551. 'rss-http' => 'text/xml',
  552. 'atom' => 'application/atom+xml',
  553. 'rdf' => 'application/rdf+xml'
  554. );
  555. $content_type = ( !empty($types[$type]) ) ? $types[$type] : 'application/octet-stream';
  556. /**
  557. * Filter the content type for a specific feed type.
  558. *
  559. * @since 2.8.0
  560. *
  561. * @param string $content_type Content type indicating the type of data that a feed contains.
  562. * @param string $type Type of feed. Possible values include 'rss2', 'atom'.
  563. * Default 'rss2'.
  564. */
  565. return apply_filters( 'feed_content_type', $content_type, $type );
  566. }
  567. /**
  568. * Build SimplePie object based on RSS or Atom feed from URL.
  569. *
  570. * @since 2.8.0
  571. *
  572. * @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged
  573. * using SimplePie's multifeed feature.
  574. * See also {@link ​http://simplepie.org/wiki/faq/typical_multifeed_gotchas}
  575. *
  576. * @return WP_Error|SimplePie WP_Error object on failure or SimplePie object on success
  577. */
  578. function fetch_feed( $url ) {
  579. require_once( ABSPATH . WPINC . '/class-feed.php' );
  580. $feed = new SimplePie();
  581. $feed->set_sanitize_class( 'WP_SimplePie_Sanitize_KSES' );
  582. // We must manually overwrite $feed->sanitize because SimplePie's
  583. // constructor sets it before we have a chance to set the sanitization class
  584. $feed->sanitize = new WP_SimplePie_Sanitize_KSES();
  585. $feed->set_cache_class( 'WP_Feed_Cache' );
  586. $feed->set_file_class( 'WP_SimplePie_File' );
  587. $feed->set_feed_url( $url );
  588. /** This filter is documented in wp-includes/class-feed.php */
  589. $feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url ) );
  590. /**
  591. * Fires just before processing the SimplePie feed object.
  592. *
  593. * @since 3.0.0
  594. *
  595. * @param object &$feed SimplePie feed object, passed by reference.
  596. * @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged.
  597. */
  598. do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
  599. $feed->init();
  600. $feed->handle_content_type();
  601. if ( $feed->error() )
  602. return new WP_Error( 'simplepie-error', $feed->error() );
  603. return $feed;
  604. }