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