]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/feed.php
WordPress 4.4-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  *
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  * @since 4.4.0 The optional `$sep` parameter was deprecated and renamed to `$deprecated`.
95  *
96  * @param string $deprecated Unused..
97  * @return string The document title.
98  */
99 function get_wp_title_rss( $deprecated = '&#8211;' ) {
100         if ( '&#8211;' !== $deprecated ) {
101                 /* translators: %s: 'document_title_separator' filter name */
102                 _deprecated_argument( __FUNCTION__, '4.4.0', sprintf( __( 'Use the %s filter instead.' ), '<code>document_title_separator</code>' ) );
103         }
104
105         /**
106          * Filter the blog title for use as the feed title.
107          *
108          * @since 2.2.0
109          * @since 4.4.0 The `$sep` parameter was deprecated and renamed to `$deprecated`.
110          *
111          * @param string $title      The current blog title.
112          * @param string $deprecated Unused.
113          */
114         return apply_filters( 'get_wp_title_rss', wp_get_document_title(), $deprecated );
115 }
116
117 /**
118  * Display the blog title for display of the feed title.
119  *
120  * @since 2.2.0
121  * @since 4.4.0 The optional `$sep` parameter was deprecated and renamed to `$deprecated`.
122  *
123  * @param string $deprecated Unused.
124  */
125 function wp_title_rss( $deprecated = '&#8211;' ) {
126         if ( '&#8211;' !== $deprecated ) {
127                 /* translators: %s: 'document_title_separator' filter name */
128                 _deprecated_argument( __FUNCTION__, '4.4.0', sprintf( __( 'Use the %s filter instead.' ), '<code>document_title_separator</code>' ) );
129         }
130
131         /**
132          * Filter the blog title for display of the feed title.
133          *
134          * @since 2.2.0
135          * @since 4.4.0 The `$sep` parameter was deprecated and renamed to `$deprecated`.
136          *
137          * @see get_wp_title_rss()
138          *
139          * @param string $wp_title_rss The current blog title.
140          * @param string $deprecated   Unused.
141          */
142         echo apply_filters( 'wp_title_rss', get_wp_title_rss(), $deprecated );
143 }
144
145 /**
146  * Retrieve the current post title for the feed.
147  *
148  * @since 2.0.0
149  *
150  * @return string Current post title.
151  */
152 function get_the_title_rss() {
153         $title = get_the_title();
154
155         /**
156          * Filter the post title for use in a feed.
157          *
158          * @since 1.2.0
159          *
160          * @param string $title The current post title.
161          */
162         $title = apply_filters( 'the_title_rss', $title );
163         return $title;
164 }
165
166 /**
167  * Display the post title in the feed.
168  *
169  * @since 0.71
170  */
171 function the_title_rss() {
172         echo get_the_title_rss();
173 }
174
175 /**
176  * Retrieve the post content for feeds.
177  *
178  * @since 2.9.0
179  * @see get_the_content()
180  *
181  * @param string $feed_type The type of feed. rss2 | atom | rss | rdf
182  * @return string The filtered content.
183  */
184 function get_the_content_feed($feed_type = null) {
185         if ( !$feed_type )
186                 $feed_type = get_default_feed();
187
188         /** This filter is documented in wp-includes/post-template.php */
189         $content = apply_filters( 'the_content', get_the_content() );
190         $content = str_replace(']]>', ']]&gt;', $content);
191         /**
192          * Filter the post content for use in feeds.
193          *
194          * @since 2.9.0
195          *
196          * @param string $content   The current post content.
197          * @param string $feed_type Type of feed. Possible values include 'rss2', 'atom'.
198          *                          Default 'rss2'.
199          */
200         return apply_filters( 'the_content_feed', $content, $feed_type );
201 }
202
203 /**
204  * Display the post content for feeds.
205  *
206  * @since 2.9.0
207  *
208  * @param string $feed_type The type of feed. rss2 | atom | rss | rdf
209  */
210 function the_content_feed($feed_type = null) {
211         echo get_the_content_feed($feed_type);
212 }
213
214 /**
215  * Display the post excerpt for the feed.
216  *
217  * @since 0.71
218  */
219 function the_excerpt_rss() {
220         $output = get_the_excerpt();
221         /**
222          * Filter the post excerpt for a feed.
223          *
224          * @since 1.2.0
225          *
226          * @param string $output The current post excerpt.
227          */
228         echo apply_filters( 'the_excerpt_rss', $output );
229 }
230
231 /**
232  * Display the permalink to the post for use in feeds.
233  *
234  * @since 2.3.0
235  */
236 function the_permalink_rss() {
237         /**
238          * Filter the permalink to the post for use in feeds.
239          *
240          * @since 2.3.0
241          *
242          * @param string $post_permalink The current post permalink.
243          */
244         echo esc_url( apply_filters( 'the_permalink_rss', get_permalink() ) );
245 }
246
247 /**
248  * Outputs the link to the comments for the current post in an xml safe way
249  *
250  * @since 3.0.0
251  * @return none
252  */
253 function comments_link_feed() {
254         /**
255          * Filter the comments permalink for the current post.
256          *
257          * @since 3.6.0
258          *
259          * @param string $comment_permalink The current comment permalink with
260          *                                  '#comments' appended.
261          */
262         echo esc_url( apply_filters( 'comments_link_feed', get_comments_link() ) );
263 }
264
265 /**
266  * Display the feed GUID for the current comment.
267  *
268  * @since 2.5.0
269  *
270  * @param int|WP_Comment $comment_id Optional comment object or id. Defaults to global comment object.
271  */
272 function comment_guid($comment_id = null) {
273         echo esc_url( get_comment_guid($comment_id) );
274 }
275
276 /**
277  * Retrieve the feed GUID for the current comment.
278  *
279  * @since 2.5.0
280  *
281  * @param int|WP_Comment $comment_id Optional comment object or id. Defaults to global comment object.
282  * @return false|string false on failure or guid for comment on success.
283  */
284 function get_comment_guid($comment_id = null) {
285         $comment = get_comment($comment_id);
286
287         if ( !is_object($comment) )
288                 return false;
289
290         return get_the_guid($comment->comment_post_ID) . '#comment-' . $comment->comment_ID;
291 }
292
293 /**
294  * Display the link to the comments.
295  *
296  * @since 1.5.0
297  * @since 4.4.0 Introduced the `$comment` argument.
298  *
299  * @param int|WP_Comment $comment Optional. Comment object or id. Defaults to global comment object.
300  */
301 function comment_link( $comment = null ) {
302         /**
303          * Filter the current comment's permalink.
304          *
305          * @since 3.6.0
306          *
307          * @see get_comment_link()
308          *
309          * @param string $comment_permalink The current comment permalink.
310          */
311         echo esc_url( apply_filters( 'comment_link', get_comment_link( $comment ) ) );
312 }
313
314 /**
315  * Retrieve the current comment author for use in the feeds.
316  *
317  * @since 2.0.0
318  *
319  * @return string Comment Author
320  */
321 function get_comment_author_rss() {
322         /**
323          * Filter the current comment author for use in a feed.
324          *
325          * @since 1.5.0
326          *
327          * @see get_comment_author()
328          *
329          * @param string $comment_author The current comment author.
330          */
331         return apply_filters( 'comment_author_rss', get_comment_author() );
332 }
333
334 /**
335  * Display the current comment author in the feed.
336  *
337  * @since 1.0.0
338  */
339 function comment_author_rss() {
340         echo get_comment_author_rss();
341 }
342
343 /**
344  * Display the current comment content for use in the feeds.
345  *
346  * @since 1.0.0
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  */
458 function rss_enclosure() {
459         if ( post_password_required() )
460                 return;
461
462         foreach ( (array) get_post_custom() as $key => $val) {
463                 if ($key == 'enclosure') {
464                         foreach ( (array) $val as $enc ) {
465                                 $enclosure = explode("\n", $enc);
466
467                                 // only get the first element, e.g. audio/mpeg from 'audio/mpeg mpga mp2 mp3'
468                                 $t = preg_split('/[ \t]/', trim($enclosure[2]) );
469                                 $type = $t[0];
470
471                                 /**
472                                  * Filter the RSS enclosure HTML link tag for the current post.
473                                  *
474                                  * @since 2.2.0
475                                  *
476                                  * @param string $html_link_tag The HTML link tag with a URI and other attributes.
477                                  */
478                                 echo apply_filters( 'rss_enclosure', '<enclosure url="' . trim( htmlspecialchars( $enclosure[0] ) ) . '" length="' . trim( $enclosure[1] ) . '" type="' . $type . '" />' . "\n" );
479                         }
480                 }
481         }
482 }
483
484 /**
485  * Display the atom enclosure for the current post.
486  *
487  * Uses the global $post to check whether the post requires a password and if
488  * the user has the password for the post. If not then it will return before
489  * displaying.
490  *
491  * Also uses the function get_post_custom() to get the post's 'enclosure'
492  * metadata field and parses the value to display the enclosure(s). The
493  * enclosure(s) consist of link HTML tag(s) with a URI and other attributes.
494  *
495  * @since 2.2.0
496  */
497 function atom_enclosure() {
498         if ( post_password_required() )
499                 return;
500
501         foreach ( (array) get_post_custom() as $key => $val ) {
502                 if ($key == 'enclosure') {
503                         foreach ( (array) $val as $enc ) {
504                                 $enclosure = explode("\n", $enc);
505                                 /**
506                                  * Filter the atom enclosure HTML link tag for the current post.
507                                  *
508                                  * @since 2.2.0
509                                  *
510                                  * @param string $html_link_tag The HTML link tag with a URI and other attributes.
511                                  */
512                                 echo apply_filters( 'atom_enclosure', '<link href="' . trim( htmlspecialchars( $enclosure[0] ) ) . '" rel="enclosure" length="' . trim( $enclosure[1] ) . '" type="' . trim( $enclosure[2] ) . '" />' . "\n" );
513                         }
514                 }
515         }
516 }
517
518 /**
519  * Determine the type of a string of data with the data formatted.
520  *
521  * Tell whether the type is text, html, or xhtml, per RFC 4287 section 3.1.
522  *
523  * In the case of WordPress, text is defined as containing no markup,
524  * xhtml is defined as "well formed", and html as tag soup (i.e., the rest).
525  *
526  * Container div tags are added to xhtml values, per section 3.1.1.3.
527  *
528  * @link http://www.atomenabled.org/developers/syndication/atom-format-spec.php#rfc.section.3.1
529  *
530  * @since 2.5.0
531  *
532  * @param string $data Input string
533  * @return array array(type, value)
534  */
535 function prep_atom_text_construct($data) {
536         if (strpos($data, '<') === false && strpos($data, '&') === false) {
537                 return array('text', $data);
538         }
539
540         $parser = xml_parser_create();
541         xml_parse($parser, '<div>' . $data . '</div>', true);
542         $code = xml_get_error_code($parser);
543         xml_parser_free($parser);
544
545         if (!$code) {
546                 if (strpos($data, '<') === false) {
547                         return array('text', $data);
548                 } else {
549                         $data = "<div xmlns='http://www.w3.org/1999/xhtml'>$data</div>";
550                         return array('xhtml', $data);
551                 }
552         }
553
554         if (strpos($data, ']]>') === false) {
555                 return array('html', "<![CDATA[$data]]>");
556         } else {
557                 return array('html', htmlspecialchars($data));
558         }
559 }
560
561 /**
562  * Displays Site Icon in atom feeds.
563  *
564  * @since 4.3.0
565  *
566  * @see get_site_icon_url()
567  */
568 function atom_site_icon() {
569         $url = get_site_icon_url( 32 );
570         if ( $url ) {
571                 echo "<icon>$url</icon>\n";
572         }
573 }
574
575 /**
576  * Displays Site Icon in RSS2.
577  *
578  * @since 4.3.0
579  */
580 function rss2_site_icon() {
581         $rss_title = get_wp_title_rss();
582         if ( empty( $rss_title ) ) {
583                 $rss_title = get_bloginfo_rss( 'name' );
584         }
585
586         $url = get_site_icon_url( 32 );
587         if ( $url ) {
588                 echo '
589 <image>
590         <url>' . convert_chars( $url ) . '</url>
591         <title>' . $rss_title . '</title>
592         <link>' . get_bloginfo_rss( 'url' ) . '</link>
593         <width>32</width>
594         <height>32</height>
595 </image> ' . "\n";
596         }
597 }
598
599 /**
600  * Display the link for the currently displayed feed in a XSS safe way.
601  *
602  * Generate a correct link for the atom:self element.
603  *
604  * @since 2.5.0
605  */
606 function self_link() {
607         $host = @parse_url(home_url());
608         /**
609          * Filter the current feed URL.
610          *
611          * @since 3.6.0
612          *
613          * @see set_url_scheme()
614          * @see wp_unslash()
615          *
616          * @param string $feed_link The link for the feed with set URL scheme.
617          */
618         echo esc_url( apply_filters( 'self_link', set_url_scheme( 'http://' . $host['host'] . wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) );
619 }
620
621 /**
622  * Return the content type for specified feed type.
623  *
624  * @since 2.8.0
625  */
626 function feed_content_type( $type = '' ) {
627         if ( empty($type) )
628                 $type = get_default_feed();
629
630         $types = array(
631                 'rss'  => 'application/rss+xml',
632                 'rss2' => 'application/rss+xml',
633                 'rss-http'  => 'text/xml',
634                 'atom' => 'application/atom+xml',
635                 'rdf'  => 'application/rdf+xml'
636         );
637
638         $content_type = ( !empty($types[$type]) ) ? $types[$type] : 'application/octet-stream';
639
640         /**
641          * Filter the content type for a specific feed type.
642          *
643          * @since 2.8.0
644          *
645          * @param string $content_type Content type indicating the type of data that a feed contains.
646          * @param string $type         Type of feed. Possible values include 'rss2', 'atom'.
647          *                             Default 'rss2'.
648          */
649         return apply_filters( 'feed_content_type', $content_type, $type );
650 }
651
652 /**
653  * Build SimplePie object based on RSS or Atom feed from URL.
654  *
655  * @since 2.8.0
656  *
657  * @param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged
658  * using SimplePie's multifeed feature.
659  * See also {@link â€‹http://simplepie.org/wiki/faq/typical_multifeed_gotchas}
660  *
661  * @return WP_Error|SimplePie WP_Error object on failure or SimplePie object on success
662  */
663 function fetch_feed( $url ) {
664         require_once( ABSPATH . WPINC . '/class-feed.php' );
665
666         $feed = new SimplePie();
667
668         $feed->set_sanitize_class( 'WP_SimplePie_Sanitize_KSES' );
669         // We must manually overwrite $feed->sanitize because SimplePie's
670         // constructor sets it before we have a chance to set the sanitization class
671         $feed->sanitize = new WP_SimplePie_Sanitize_KSES();
672
673         $feed->set_cache_class( 'WP_Feed_Cache' );
674         $feed->set_file_class( 'WP_SimplePie_File' );
675
676         $feed->set_feed_url( $url );
677         /** This filter is documented in wp-includes/class-feed.php */
678         $feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url ) );
679         /**
680          * Fires just before processing the SimplePie feed object.
681          *
682          * @since 3.0.0
683          *
684          * @param object &$feed SimplePie feed object, passed by reference.
685          * @param mixed  $url   URL of feed to retrieve. If an array of URLs, the feeds are merged.
686          */
687         do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
688         $feed->init();
689         $feed->set_output_encoding( get_option( 'blog_charset' ) );
690         $feed->handle_content_type();
691
692         if ( $feed->error() )
693                 return new WP_Error( 'simplepie-error', $feed->error() );
694
695         return $feed;
696 }