]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class-wp.php
WordPress 3.5-scripts
[autoinstalls/wordpress.git] / wp-includes / class-wp.php
1 <?php
2 /**
3  * WordPress environment setup class.
4  *
5  * @package WordPress
6  * @since 2.0.0
7  */
8 class WP {
9         /**
10          * Public query variables.
11          *
12          * Long list of public query variables.
13          *
14          * @since 2.0.0
15          * @access public
16          * @var array
17          */
18         var $public_query_vars = array('m', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'tag', 'feed', 'author_name', 'static', 'pagename', 'page_id', 'error', 'comments_popup', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview', 'robots', 'taxonomy', 'term', 'cpage', 'post_type');
19
20         /**
21          * Private query variables.
22          *
23          * Long list of private query variables.
24          *
25          * @since 2.0.0
26          * @var array
27          */
28         var $private_query_vars = array('offset', 'posts_per_page', 'posts_per_archive_page', 'showposts', 'nopaging', 'post_type', 'post_status', 'category__in', 'category__not_in', 'category__and', 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'tag_id', 'post_mime_type', 'perm', 'comments_per_page', 'post__in', 'post__not_in');
29
30         /**
31          * Extra query variables set by the user.
32          *
33          * @since 2.1.0
34          * @var array
35          */
36         var $extra_query_vars = array();
37
38         /**
39          * Query variables for setting up the WordPress Query Loop.
40          *
41          * @since 2.0.0
42          * @var array
43          */
44         var $query_vars;
45
46         /**
47          * String parsed to set the query variables.
48          *
49          * @since 2.0.0
50          * @var string
51          */
52         var $query_string;
53
54         /**
55          * Permalink or requested URI.
56          *
57          * @since 2.0.0
58          * @var string
59          */
60         var $request;
61
62         /**
63          * Rewrite rule the request matched.
64          *
65          * @since 2.0.0
66          * @var string
67          */
68         var $matched_rule;
69
70         /**
71          * Rewrite query the request matched.
72          *
73          * @since 2.0.0
74          * @var string
75          */
76         var $matched_query;
77
78         /**
79          * Whether already did the permalink.
80          *
81          * @since 2.0.0
82          * @var bool
83          */
84         var $did_permalink = false;
85
86         /**
87          * Add name to list of public query variables.
88          *
89          * @since 2.1.0
90          *
91          * @param string $qv Query variable name.
92          */
93         function add_query_var($qv) {
94                 if ( !in_array($qv, $this->public_query_vars) )
95                         $this->public_query_vars[] = $qv;
96         }
97
98         /**
99          * Set the value of a query variable.
100          *
101          * @since 2.3.0
102          *
103          * @param string $key Query variable name.
104          * @param mixed $value Query variable value.
105          */
106         function set_query_var($key, $value) {
107                 $this->query_vars[$key] = $value;
108         }
109
110         /**
111          * Parse request to find correct WordPress query.
112          *
113          * Sets up the query variables based on the request. There are also many
114          * filters and actions that can be used to further manipulate the result.
115          *
116          * @since 2.0.0
117          *
118          * @param array|string $extra_query_vars Set the extra query variables.
119          */
120         function parse_request($extra_query_vars = '') {
121                 global $wp_rewrite;
122
123                 if ( ! apply_filters( 'do_parse_request', true, $this, $extra_query_vars ) )
124                         return;
125
126                 $this->query_vars = array();
127                 $post_type_query_vars = array();
128
129                 if ( is_array($extra_query_vars) )
130                         $this->extra_query_vars = & $extra_query_vars;
131                 else if (! empty($extra_query_vars))
132                         parse_str($extra_query_vars, $this->extra_query_vars);
133
134                 // Process PATH_INFO, REQUEST_URI, and 404 for permalinks.
135
136                 // Fetch the rewrite rules.
137                 $rewrite = $wp_rewrite->wp_rewrite_rules();
138
139                 if ( ! empty($rewrite) ) {
140                         // If we match a rewrite rule, this will be cleared.
141                         $error = '404';
142                         $this->did_permalink = true;
143
144                         if ( isset($_SERVER['PATH_INFO']) )
145                                 $pathinfo = $_SERVER['PATH_INFO'];
146                         else
147                                 $pathinfo = '';
148                         $pathinfo_array = explode('?', $pathinfo);
149                         $pathinfo = str_replace("%", "%25", $pathinfo_array[0]);
150                         $req_uri = $_SERVER['REQUEST_URI'];
151                         $req_uri_array = explode('?', $req_uri);
152                         $req_uri = $req_uri_array[0];
153                         $self = $_SERVER['PHP_SELF'];
154                         $home_path = parse_url(home_url());
155                         if ( isset($home_path['path']) )
156                                 $home_path = $home_path['path'];
157                         else
158                                 $home_path = '';
159                         $home_path = trim($home_path, '/');
160
161                         // Trim path info from the end and the leading home path from the
162                         // front. For path info requests, this leaves us with the requesting
163                         // filename, if any. For 404 requests, this leaves us with the
164                         // requested permalink.
165                         $req_uri = str_replace($pathinfo, '', $req_uri);
166                         $req_uri = trim($req_uri, '/');
167                         $req_uri = preg_replace("|^$home_path|i", '', $req_uri);
168                         $req_uri = trim($req_uri, '/');
169                         $pathinfo = trim($pathinfo, '/');
170                         $pathinfo = preg_replace("|^$home_path|i", '', $pathinfo);
171                         $pathinfo = trim($pathinfo, '/');
172                         $self = trim($self, '/');
173                         $self = preg_replace("|^$home_path|i", '', $self);
174                         $self = trim($self, '/');
175
176                         // The requested permalink is in $pathinfo for path info requests and
177                         //  $req_uri for other requests.
178                         if ( ! empty($pathinfo) && !preg_match('|^.*' . $wp_rewrite->index . '$|', $pathinfo) ) {
179                                 $request = $pathinfo;
180                         } else {
181                                 // If the request uri is the index, blank it out so that we don't try to match it against a rule.
182                                 if ( $req_uri == $wp_rewrite->index )
183                                         $req_uri = '';
184                                 $request = $req_uri;
185                         }
186
187                         $this->request = $request;
188
189                         // Look for matches.
190                         $request_match = $request;
191                         if ( empty( $request_match ) ) {
192                                 // An empty request could only match against ^$ regex
193                                 if ( isset( $rewrite['$'] ) ) {
194                                         $this->matched_rule = '$';
195                                         $query = $rewrite['$'];
196                                         $matches = array('');
197                                 }
198                         } else {
199                                 foreach ( (array) $rewrite as $match => $query ) {
200                                         // If the requesting file is the anchor of the match, prepend it to the path info.
201                                         if ( ! empty($req_uri) && strpos($match, $req_uri) === 0 && $req_uri != $request )
202                                                 $request_match = $req_uri . '/' . $request;
203
204                                         if ( preg_match("#^$match#", $request_match, $matches) ||
205                                                 preg_match("#^$match#", urldecode($request_match), $matches) ) {
206
207                                                 if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) {
208                                                         // this is a verbose page match, lets check to be sure about it
209                                                         if ( ! get_page_by_path( $matches[ $varmatch[1] ] ) )
210                                                                 continue;
211                                                 }
212
213                                                 // Got a match.
214                                                 $this->matched_rule = $match;
215                                                 break;
216                                         }
217                                 }
218                         }
219
220                         if ( isset( $this->matched_rule ) ) {
221                                 // Trim the query of everything up to the '?'.
222                                 $query = preg_replace("!^.+\?!", '', $query);
223
224                                 // Substitute the substring matches into the query.
225                                 $query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
226
227                                 $this->matched_query = $query;
228
229                                 // Parse the query.
230                                 parse_str($query, $perma_query_vars);
231
232                                 // If we're processing a 404 request, clear the error var since we found something.
233                                 if ( '404' == $error )
234                                         unset( $error, $_GET['error'] );
235                         }
236
237                         // If req_uri is empty or if it is a request for ourself, unset error.
238                         if ( empty($request) || $req_uri == $self || strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false ) {
239                                 unset( $error, $_GET['error'] );
240
241                                 if ( isset($perma_query_vars) && strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false )
242                                         unset( $perma_query_vars );
243
244                                 $this->did_permalink = false;
245                         }
246                 }
247
248                 $this->public_query_vars = apply_filters('query_vars', $this->public_query_vars);
249
250                 foreach ( $GLOBALS['wp_post_types'] as $post_type => $t )
251                         if ( $t->query_var )
252                                 $post_type_query_vars[$t->query_var] = $post_type;
253
254                 foreach ( $this->public_query_vars as $wpvar ) {
255                         if ( isset( $this->extra_query_vars[$wpvar] ) )
256                                 $this->query_vars[$wpvar] = $this->extra_query_vars[$wpvar];
257                         elseif ( isset( $_POST[$wpvar] ) )
258                                 $this->query_vars[$wpvar] = $_POST[$wpvar];
259                         elseif ( isset( $_GET[$wpvar] ) )
260                                 $this->query_vars[$wpvar] = $_GET[$wpvar];
261                         elseif ( isset( $perma_query_vars[$wpvar] ) )
262                                 $this->query_vars[$wpvar] = $perma_query_vars[$wpvar];
263
264                         if ( !empty( $this->query_vars[$wpvar] ) ) {
265                                 if ( ! is_array( $this->query_vars[$wpvar] ) ) {
266                                         $this->query_vars[$wpvar] = (string) $this->query_vars[$wpvar];
267                                 } else {
268                                         foreach ( $this->query_vars[$wpvar] as $vkey => $v ) {
269                                                 if ( !is_object( $v ) ) {
270                                                         $this->query_vars[$wpvar][$vkey] = (string) $v;
271                                                 }
272                                         }
273                                 }
274
275                                 if ( isset($post_type_query_vars[$wpvar] ) ) {
276                                         $this->query_vars['post_type'] = $post_type_query_vars[$wpvar];
277                                         $this->query_vars['name'] = $this->query_vars[$wpvar];
278                                 }
279                         }
280                 }
281
282                 // Convert urldecoded spaces back into +
283                 foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t )
284                         if ( $t->query_var && isset( $this->query_vars[$t->query_var] ) )
285                                 $this->query_vars[$t->query_var] = str_replace( ' ', '+', $this->query_vars[$t->query_var] );
286
287                 // Limit publicly queried post_types to those that are publicly_queryable
288                 if ( isset( $this->query_vars['post_type']) ) {
289                         $queryable_post_types = get_post_types( array('publicly_queryable' => true) );
290                         if ( ! is_array( $this->query_vars['post_type'] ) ) {
291                                 if ( ! in_array( $this->query_vars['post_type'], $queryable_post_types ) )
292                                         unset( $this->query_vars['post_type'] );
293                         } else {
294                                 $this->query_vars['post_type'] = array_intersect( $this->query_vars['post_type'], $queryable_post_types );
295                         }
296                 }
297
298                 foreach ( (array) $this->private_query_vars as $var) {
299                         if ( isset($this->extra_query_vars[$var]) )
300                                 $this->query_vars[$var] = $this->extra_query_vars[$var];
301                 }
302
303                 if ( isset($error) )
304                         $this->query_vars['error'] = $error;
305
306                 $this->query_vars = apply_filters('request', $this->query_vars);
307
308                 do_action_ref_array('parse_request', array(&$this));
309         }
310
311         /**
312          * Send additional HTTP headers for caching, content type, etc.
313          *
314          * Sets the X-Pingback header, 404 status (if 404), Content-type. If showing
315          * a feed, it will also send last-modified, etag, and 304 status if needed.
316          *
317          * @since 2.0.0
318          */
319         function send_headers() {
320                 $headers = array('X-Pingback' => get_bloginfo('pingback_url'));
321                 $status = null;
322                 $exit_required = false;
323
324                 if ( is_user_logged_in() )
325                         $headers = array_merge($headers, wp_get_nocache_headers());
326                 if ( ! empty( $this->query_vars['error'] ) ) {
327                         $status = (int) $this->query_vars['error'];
328                         if ( 404 === $status ) {
329                                 if ( ! is_user_logged_in() )
330                                         $headers = array_merge($headers, wp_get_nocache_headers());
331                                 $headers['Content-Type'] = get_option('html_type') . '; charset=' . get_option('blog_charset');
332                         } elseif ( in_array( $status, array( 403, 500, 502, 503 ) ) ) {
333                                 $exit_required = true;
334                         }
335                 } else if ( empty($this->query_vars['feed']) ) {
336                         $headers['Content-Type'] = get_option('html_type') . '; charset=' . get_option('blog_charset');
337                 } else {
338                         // We're showing a feed, so WP is indeed the only thing that last changed
339                         if ( !empty($this->query_vars['withcomments'])
340                                 || ( empty($this->query_vars['withoutcomments'])
341                                         && ( !empty($this->query_vars['p'])
342                                                 || !empty($this->query_vars['name'])
343                                                 || !empty($this->query_vars['page_id'])
344                                                 || !empty($this->query_vars['pagename'])
345                                                 || !empty($this->query_vars['attachment'])
346                                                 || !empty($this->query_vars['attachment_id'])
347                                         )
348                                 )
349                         )
350                                 $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastcommentmodified('GMT'), 0).' GMT';
351                         else
352                                 $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0).' GMT';
353                         $wp_etag = '"' . md5($wp_last_modified) . '"';
354                         $headers['Last-Modified'] = $wp_last_modified;
355                         $headers['ETag'] = $wp_etag;
356
357                         // Support for Conditional GET
358                         if (isset($_SERVER['HTTP_IF_NONE_MATCH']))
359                                 $client_etag = stripslashes(stripslashes($_SERVER['HTTP_IF_NONE_MATCH']));
360                         else $client_etag = false;
361
362                         $client_last_modified = empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? '' : trim($_SERVER['HTTP_IF_MODIFIED_SINCE']);
363                         // If string is empty, return 0. If not, attempt to parse into a timestamp
364                         $client_modified_timestamp = $client_last_modified ? strtotime($client_last_modified) : 0;
365
366                         // Make a timestamp for our most recent modification...
367                         $wp_modified_timestamp = strtotime($wp_last_modified);
368
369                         if ( ($client_last_modified && $client_etag) ?
370                                          (($client_modified_timestamp >= $wp_modified_timestamp) && ($client_etag == $wp_etag)) :
371                                          (($client_modified_timestamp >= $wp_modified_timestamp) || ($client_etag == $wp_etag)) ) {
372                                 $status = 304;
373                                 $exit_required = true;
374                         }
375                 }
376
377                 $headers = apply_filters('wp_headers', $headers, $this);
378
379                 if ( ! empty( $status ) )
380                         status_header( $status );
381                 foreach( (array) $headers as $name => $field_value )
382                         @header("{$name}: {$field_value}");
383
384                 if ( isset( $headers['Last-Modified'] ) && empty( $headers['Last-Modified'] ) && function_exists( 'header_remove' ) )
385                         @header_remove( 'Last-Modified' );
386
387                 if ( $exit_required )
388                         exit();
389
390                 do_action_ref_array('send_headers', array(&$this));
391         }
392
393         /**
394          * Sets the query string property based off of the query variable property.
395          *
396          * The 'query_string' filter is deprecated, but still works. Plugins should
397          * use the 'request' filter instead.
398          *
399          * @since 2.0.0
400          */
401         function build_query_string() {
402                 $this->query_string = '';
403                 foreach ( (array) array_keys($this->query_vars) as $wpvar) {
404                         if ( '' != $this->query_vars[$wpvar] ) {
405                                 $this->query_string .= (strlen($this->query_string) < 1) ? '' : '&';
406                                 if ( !is_scalar($this->query_vars[$wpvar]) ) // Discard non-scalars.
407                                         continue;
408                                 $this->query_string .= $wpvar . '=' . rawurlencode($this->query_vars[$wpvar]);
409                         }
410                 }
411
412                 // query_string filter deprecated. Use request filter instead.
413                 if ( has_filter('query_string') ) {  // Don't bother filtering and parsing if no plugins are hooked in.
414                         $this->query_string = apply_filters('query_string', $this->query_string);
415                         parse_str($this->query_string, $this->query_vars);
416                 }
417         }
418
419         /**
420          * Set up the WordPress Globals.
421          *
422          * The query_vars property will be extracted to the GLOBALS. So care should
423          * be taken when naming global variables that might interfere with the
424          * WordPress environment.
425          *
426          * @global string $query_string Query string for the loop.
427          * @global int $more Only set, if single page or post.
428          * @global int $single If single page or post. Only set, if single page or post.
429          *
430          * @since 2.0.0
431          */
432         function register_globals() {
433                 global $wp_query;
434                 // Extract updated query vars back into global namespace.
435                 foreach ( (array) $wp_query->query_vars as $key => $value) {
436                         $GLOBALS[$key] = $value;
437                 }
438
439                 $GLOBALS['query_string'] = $this->query_string;
440                 $GLOBALS['posts'] = & $wp_query->posts;
441                 $GLOBALS['post'] = (isset($wp_query->post)) ? $wp_query->post : null;
442                 $GLOBALS['request'] = $wp_query->request;
443
444                 if ( is_single() || is_page() ) {
445                         $GLOBALS['more'] = 1;
446                         $GLOBALS['single'] = 1;
447                 }
448         }
449
450         /**
451          * Set up the current user.
452          *
453          * @since 2.0.0
454          */
455         function init() {
456                 wp_get_current_user();
457         }
458
459         /**
460          * Set up the Loop based on the query variables.
461          *
462          * @uses WP::$query_vars
463          * @since 2.0.0
464          */
465         function query_posts() {
466                 global $wp_the_query;
467                 $this->build_query_string();
468                 $wp_the_query->query($this->query_vars);
469         }
470
471         /**
472          * Set the Headers for 404, if nothing is found for requested URL.
473          *
474          * Issue a 404 if a request doesn't match any posts and doesn't match
475          * any object (e.g. an existing-but-empty category, tag, author) and a 404 was not already
476          * issued, and if the request was not a search or the homepage.
477          *
478          * Otherwise, issue a 200.
479          *
480          * @since 2.0.0
481          */
482         function handle_404() {
483                 global $wp_query;
484
485                 // If we've already issued a 404, bail.
486                 if ( is_404() )
487                         return;
488
489                 // Never 404 for the admin, robots, or if we found posts.
490                 if ( is_admin() || is_robots() || $wp_query->posts ) {
491                         status_header( 200 );
492                         return;
493                 }
494
495                 // We will 404 for paged queries, as no posts were found.
496                 if ( ! is_paged() ) {
497
498                         // Don't 404 for these queries if they matched an object.
499                         if ( ( is_tag() || is_category() || is_tax() || is_author() || is_post_type_archive() ) && $wp_query->get_queried_object() ) {
500                                 status_header( 200 );
501                                 return;
502                         }
503
504                         // Don't 404 for these queries either.
505                         if ( is_home() || is_search() ) {
506                                 status_header( 200 );
507                                 return;
508                         }
509                 }
510
511                 // Guess it's time to 404.
512                 $wp_query->set_404();
513                 status_header( 404 );
514                 nocache_headers();
515         }
516
517         /**
518          * Sets up all of the variables required by the WordPress environment.
519          *
520          * The action 'wp' has one parameter that references the WP object. It
521          * allows for accessing the properties and methods to further manipulate the
522          * object.
523          *
524          * @since 2.0.0
525          *
526          * @param string|array $query_args Passed to {@link parse_request()}
527          */
528         function main($query_args = '') {
529                 $this->init();
530                 $this->parse_request($query_args);
531                 $this->send_headers();
532                 $this->query_posts();
533                 $this->handle_404();
534                 $this->register_globals();
535                 do_action_ref_array('wp', array(&$this));
536         }
537
538 }
539
540 /**
541  * Helper class to remove the need to use eval to replace $matches[] in query strings.
542  *
543  * @since 2.9.0
544  */
545 class WP_MatchesMapRegex {
546         /**
547          * store for matches
548          *
549          * @access private
550          * @var array
551          */
552         var $_matches;
553
554         /**
555          * store for mapping result
556          *
557          * @access public
558          * @var string
559          */
560         var $output;
561
562         /**
563          * subject to perform mapping on (query string containing $matches[] references
564          *
565          * @access private
566          * @var string
567          */
568         var $_subject;
569
570         /**
571          * regexp pattern to match $matches[] references
572          *
573          * @var string
574          */
575         var $_pattern = '(\$matches\[[1-9]+[0-9]*\])'; // magic number
576
577         /**
578          * constructor
579          *
580          * @param string $subject subject if regex
581          * @param array  $matches data to use in map
582          * @return self
583          */
584         function WP_MatchesMapRegex($subject, $matches) {
585                 $this->_subject = $subject;
586                 $this->_matches = $matches;
587                 $this->output = $this->_map();
588         }
589
590         /**
591          * Substitute substring matches in subject.
592          *
593          * static helper function to ease use
594          *
595          * @access public
596          * @param string $subject subject
597          * @param array  $matches data used for substitution
598          * @return string
599          */
600         public static function apply($subject, $matches) {
601                 $oSelf = new WP_MatchesMapRegex($subject, $matches);
602                 return $oSelf->output;
603         }
604
605         /**
606          * do the actual mapping
607          *
608          * @access private
609          * @return string
610          */
611         function _map() {
612                 $callback = array($this, 'callback');
613                 return preg_replace_callback($this->_pattern, $callback, $this->_subject);
614         }
615
616         /**
617          * preg_replace_callback hook
618          *
619          * @access public
620          * @param  array $matches preg_replace regexp matches
621          * @return string
622          */
623         function callback($matches) {
624                 $index = intval(substr($matches[0], 9, -1));
625                 return ( isset( $this->_matches[$index] ) ? urlencode($this->_matches[$index]) : '' );
626         }
627
628 }