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