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