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