root/trunk/wp-includes/classes.php

Revision 8761, 24.5 kB (checked in by ryan, 1 week ago)

Don't 404 for categories, tags, and authors that exist but have no posts. see #5324

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2
3 class WP {
4     var $public_query_vars = array('m', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence', 'debug', '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');
5
6     var $private_query_vars = array('offset', 'posts_per_page', 'posts_per_archive_page', 'what_to_show', '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');
7     var $extra_query_vars = array();
8
9     var $query_vars;
10     var $query_string;
11     var $request;
12     var $matched_rule;
13     var $matched_query;
14     var $did_permalink = false;
15
16     function add_query_var($qv) {
17         if ( !in_array($qv, $this->public_query_vars) )
18             $this->public_query_vars[] = $qv;
19     }
20
21     function set_query_var($key, $value) {
22         $this->query_vars[$key] = $value;
23     }
24
25     function parse_request($extra_query_vars = '') {
26         global $wp_rewrite;
27
28         $this->query_vars = array();
29         $taxonomy_query_vars = array();
30
31         if ( is_array($extra_query_vars) )
32             $this->extra_query_vars = & $extra_query_vars;
33         else if (! empty($extra_query_vars))
34             parse_str($extra_query_vars, $this->extra_query_vars);
35
36         // Process PATH_INFO, REQUEST_URI, and 404 for permalinks.
37
38         // Fetch the rewrite rules.
39         $rewrite = $wp_rewrite->wp_rewrite_rules();
40
41         if (! empty($rewrite)) {
42             // If we match a rewrite rule, this will be cleared.
43             $error = '404';
44             $this->did_permalink = true;
45
46             if ( isset($_SERVER['PATH_INFO']) )
47                 $pathinfo = $_SERVER['PATH_INFO'];
48             else
49                 $pathinfo = '';
50             $pathinfo_array = explode('?', $pathinfo);
51             $pathinfo = str_replace("%", "%25", $pathinfo_array[0]);
52             $req_uri = $_SERVER['REQUEST_URI'];
53             $req_uri_array = explode('?', $req_uri);
54             $req_uri = $req_uri_array[0];
55             $self = $_SERVER['PHP_SELF'];
56             $home_path = parse_url(get_option('home'));
57             if ( isset($home_path['path']) )
58                 $home_path = $home_path['path'];
59             else
60                 $home_path = '';
61             $home_path = trim($home_path, '/');
62
63             // Trim path info from the end and the leading home path from the
64             // front.  For path info requests, this leaves us with the requesting
65             // filename, if any.  For 404 requests, this leaves us with the
66             // requested permalink.
67             $req_uri = str_replace($pathinfo, '', rawurldecode($req_uri));
68             $req_uri = trim($req_uri, '/');
69             $req_uri = preg_replace("|^$home_path|", '', $req_uri);
70             $req_uri = trim($req_uri, '/');
71             $pathinfo = trim($pathinfo, '/');
72             $pathinfo = preg_replace("|^$home_path|", '', $pathinfo);
73             $pathinfo = trim($pathinfo, '/');
74             $self = trim($self, '/');
75             $self = preg_replace("|^$home_path|", '', $self);
76             $self = trim($self, '/');
77
78             // The requested permalink is in $pathinfo for path info requests and
79             //  $req_uri for other requests.
80             if ( ! empty($pathinfo) && !preg_match('|^.*' . $wp_rewrite->index . '$|', $pathinfo) ) {
81                 $request = $pathinfo;
82             } else {
83                 // If the request uri is the index, blank it out so that we don't try to match it against a rule.
84                 if ( $req_uri == $wp_rewrite->index )
85                     $req_uri = '';
86                 $request = $req_uri;
87             }
88
89             $this->request = $request;
90
91             // Look for matches.
92             $request_match = $request;
93             foreach ( (array) $rewrite as $match => $query) {
94                 // Don't try to match against AtomPub calls
95                 if ( $req_uri == 'wp-app.php' )
96                     break;
97
98                 // If the requesting file is the anchor of the match, prepend it
99                 // to the path info.
100                 if ((! empty($req_uri)) && (strpos($match, $req_uri) === 0) && ($req_uri != $request)) {
101                     $request_match = $req_uri . '/' . $request;
102                 }
103
104                 if (preg_match("!^$match!", $request_match, $matches) ||
105                     preg_match("!^$match!", urldecode($request_match), $matches)) {
106                     // Got a match.
107                     $this->matched_rule = $match;
108
109                     // Trim the query of everything up to the '?'.
110                     $query = preg_replace("!^.+\?!", '', $query);
111
112                     // Substitute the substring matches into the query.
113                     eval("@\$query = \"" . addslashes($query) . "\";");
114                     
115                     $this->matched_query = $query;
116
117                     // Parse the query.
118                     parse_str($query, $perma_query_vars);
119
120                     // If we're processing a 404 request, clear the error var
121                     // since we found something.
122                     if (isset($_GET['error']))
123                         unset($_GET['error']);
124
125                     if (isset($error))
126                         unset($error);
127
128                     break;
129                 }
130             }
131
132             // If req_uri is empty or if it is a request for ourself, unset error.
133             if (empty($request) || $req_uri == $self || strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false) {
134                 if (isset($_GET['error']))
135                     unset($_GET['error']);
136
137                 if (isset($error))
138                     unset($error);
139
140                 if (isset($perma_query_vars) && strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false)
141                     unset($perma_query_vars);
142
143                 $this->did_permalink = false;
144             }
145         }
146
147         $this->public_query_vars = apply_filters('query_vars', $this->public_query_vars);
148
149         foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t )
150             if ( isset($t->query_var) )
151                 $taxonomy_query_vars[$t->query_var] = $taxonomy;
152
153         for ($i=0; $i<count($this->public_query_vars); $i += 1) {
154             $wpvar = $this->public_query_vars[$i];
155             if (isset($this->extra_query_vars[$wpvar]))
156                 $this->query_vars[$wpvar] = $this->extra_query_vars[$wpvar];
157             elseif (isset($GLOBALS[$wpvar]))
158                 $this->query_vars[$wpvar] = $GLOBALS[$wpvar];
159             elseif (!empty($_POST[$wpvar]))
160                 $this->query_vars[$wpvar] = $_POST[$wpvar];
161             elseif (!empty($_GET[$wpvar]))
162                 $this->query_vars[$wpvar] = $_GET[$wpvar];
163             elseif (!empty($perma_query_vars[$wpvar]))
164                 $this->query_vars[$wpvar] = $perma_query_vars[$wpvar];
165
166             if ( !empty( $this->query_vars[$wpvar] ) ) {
167                 $this->query_vars[$wpvar] = (string) $this->query_vars[$wpvar];
168                 if ( in_array( $wpvar, $taxonomy_query_vars ) ) {
169                     $this->query_vars['taxonomy'] = $taxonomy_query_vars[$wpvar];
170                     $this->query_vars['term'] = $this->query_vars[$wpvar];
171                 }
172             }
173         }
174
175         foreach ( (array) $this->private_query_vars as $var) {
176             if (isset($this->extra_query_vars[$var]))
177                 $this->query_vars[$var] = $this->extra_query_vars[$var];
178             elseif (isset($GLOBALS[$var]) && '' != $GLOBALS[$var])
179                 $this->query_vars[$var] = $GLOBALS[$var];
180         }
181
182         if ( isset($error) )
183             $this->query_vars['error'] = $error;
184
185         $this->query_vars = apply_filters('request', $this->query_vars);
186
187         do_action_ref_array('parse_request', array(&$this));
188     }
189
190     function send_headers() {
191         @header('X-Pingback: '. get_bloginfo('pingback_url'));
192         if ( is_user_logged_in() )
193             nocache_headers();
194         if ( !empty($this->query_vars['error']) && '404' == $this->query_vars['error'] ) {
195             status_header( 404 );
196             if ( !is_user_logged_in() )
197                 nocache_headers();
198             @header('Content-Type: ' . get_option('html_type') . '; charset=' . get_option('blog_charset'));
199         } else if ( empty($this->query_vars['feed']) ) {
200             @header('Content-Type: ' . get_option('html_type') . '; charset=' . get_option('blog_charset'));
201         } else {
202             // We're showing a feed, so WP is indeed the only thing that last changed
203             if ( !empty($this->query_vars['withcomments'])
204                 || ( empty($this->query_vars['withoutcomments'])
205                     && ( !empty($this->query_vars['p'])
206                         || !empty($this->query_vars['name'])
207                         || !empty($this->query_vars['page_id'])
208                         || !empty($this->query_vars['pagename'])
209                         || !empty($this->query_vars['attachment'])
210                         || !empty($this->query_vars['attachment_id'])
211                     )
212                 )
213             )
214                 $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastcommentmodified('GMT'), 0).' GMT';
215             else
216                 $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0).' GMT';
217             $wp_etag = '"' . md5($wp_last_modified) . '"';
218             @header("Last-Modified: $wp_last_modified");
219             @header("ETag: $wp_etag");
220
221             // Support for Conditional GET
222             if (isset($_SERVER['HTTP_IF_NONE_MATCH']))
223                 $client_etag = stripslashes(stripslashes($_SERVER['HTTP_IF_NONE_MATCH']));
224             else $client_etag = false;
225
226             $client_last_modified = empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? '' : trim($_SERVER['HTTP_IF_MODIFIED_SINCE']);
227             // If string is empty, return 0. If not, attempt to parse into a timestamp
228             $client_modified_timestamp = $client_last_modified ? strtotime($client_last_modified) : 0;
229
230             // Make a timestamp for our most recent modification...
231             $wp_modified_timestamp = strtotime($wp_last_modified);
232
233             if ( ($client_last_modified && $client_etag) ?
234                      (($client_modified_timestamp >= $wp_modified_timestamp) && ($client_etag == $wp_etag)) :
235                      (($client_modified_timestamp >= $wp_modified_timestamp) || ($client_etag == $wp_etag)) ) {
236                 status_header( 304 );
237                 exit;
238             }
239         }
240
241         do_action_ref_array('send_headers', array(&$this));
242     }
243
244     function build_query_string() {
245         $this->query_string = '';
246         foreach ( (array) array_keys($this->query_vars) as $wpvar) {
247             if ( '' != $this->query_vars[$wpvar] ) {
248                 $this->query_string .= (strlen($this->query_string) < 1) ? '' : '&';
249                 if ( !is_scalar($this->query_vars[$wpvar]) ) // Discard non-scalars.
250                     continue;
251                 $this->query_string .= $wpvar . '=' . rawurlencode($this->query_vars[$wpvar]);
252             }
253         }
254
255         // query_string filter deprecated.  Use request filter instead.
256         if ( has_filter('query_string') ) {  // Don't bother filtering and parsing if no plugins are hooked in.
257             $this->query_string = apply_filters('query_string', $this->query_string);
258             parse_str($this->query_string, $this->query_vars);
259         }
260     }
261
262     function register_globals() {
263         global $wp_query;
264         // Extract updated query vars back into global namespace.
265         foreach ( (array) $wp_query->query_vars as $key => $value) {
266             $GLOBALS[$key] = $value;
267         }
268
269         $GLOBALS['query_string'] = & $this->query_string;
270         $GLOBALS['posts'] = & $wp_query->posts;
271         $GLOBALS['post'] = & $wp_query->post;
272         $GLOBALS['request'] = & $wp_query->request;
273
274         if ( is_single() || is_page() ) {
275             $GLOBALS['more'] = 1;
276             $GLOBALS['single'] = 1;
277         }
278     }
279
280     function init() {
281         wp_get_current_user();
282     }
283
284     function query_posts() {
285         global $wp_the_query;
286         $this->build_query_string();
287         $wp_the_query->query($this->query_vars);
288      }
289
290     function handle_404() {
291         global $wp_query;
292         // Issue a 404 if a permalink request doesn't match any posts.  Don't
293         // issue a 404 if one was already issued, if the request was a search,
294         // or if the request was a regular query string request rather than a
295         // permalink request.
296         if ( (0 == count($wp_query->posts)) && !is_404() && !is_search() && ( $this->did_permalink || (!empty($_SERVER['QUERY_STRING']) && (false === strpos($_SERVER['REQUEST_URI'], '?'))) ) ) {
297             // Don't 404 for these queries if they matched an object.
298             if ( ( is_tag() || is_category() || is_author() ) && $wp_query->get_queried_object() ) {
299                 if ( !is_404() )
300                     status_header( 200 );
301                 return;
302             }
303             $wp_query->set_404();
304             status_header( 404 );
305             nocache_headers();
306         } elseif ( !is_404() ) {
307             status_header( 200 );
308         }
309     }
310
311     function main($query_args = '') {
312         $this->init();
313         $this->parse_request($query_args);
314         $this->send_headers();
315         $this->query_posts();
316         $this->handle_404();
317         $this->register_globals();
318         do_action_ref_array('wp', array(&$this));
319     }
320
321     function WP() {
322         // Empty.
323     }
324 }
325
326 class WP_Error {
327     var $errors = array();
328     var $error_data = array();
329
330     function WP_Error($code = '', $message = '', $data = '') {
331         if ( empty($code) )
332             return;
333
334         $this->errors[$code][] = $message;
335
336         if ( ! empty($data) )
337             $this->error_data[$code] = $data;
338     }
339
340     function get_error_codes() {
341         if ( empty($this->errors) )
342             return array();
343
344         return array_keys($this->errors);
345     }
346
347     function get_error_code() {
348         $codes = $this->get_error_codes();
349
350         if ( empty($codes) )
351             return '';
352
353         return $codes[0];
354     }
355
356     function get_error_messages($code = '') {
357         // Return all messages if no code specified.
358         if ( empty($code) ) {
359             $all_messages = array();
360             foreach ( (array) $this->errors as $code => $messages )
361                 $all_messages = array_merge($all_messages, $messages);
362
363             return $all_messages;
364         }
365
366         if ( isset($this->errors[$code]) )
367             return $this->errors[$code];
368         else
369             return array();
370     }
371
372     function get_error_message($code = '') {
373         if ( empty($code) )
374             $code = $this->get_error_code();
375         $messages = $this->get_error_messages($code);
376         if ( empty($messages) )
377             return '';
378         return $messages[0];
379     }
380
381     function get_error_data($code = '') {
382         if ( empty($code) )
383             $code = $this->get_error_code();
384
385         if ( isset($this->error_data[$code]) )
386             return $this->error_data[$code];
387         return null;
388     }
389
390     function add($code, $message, $data = '') {
391         $this->errors[$code][] = $message;
392         if ( ! empty($data) )
393             $this->error_data[$code] = $data;
394     }
395
396     function add_data($data, $code = '') {
397         if ( empty($code) )
398             $code = $this->get_error_code();
399
400         $this->error_data[$code] = $data;
401     }
402 }
403
404 function is_wp_error($thing) {
405     if ( is_object($thing) && is_a($thing, 'WP_Error') )
406         return true;
407     return false;
408 }
409
410 /*
411  * A class for displaying various tree-like structures.
412  * Extend the Walker class to use it, see examples at the bottom
413  */
414 class Walker {
415     var $tree_type;
416     var $db_fields;
417
418     //abstract callbacks
419     function start_lvl(&$output) {}
420     function end_lvl(&$output)   {}
421     function start_el(&$output)  {}
422     function end_el(&$output)    {}
423
424     /*
425       * display one element if the element doesn't have any children
426       * otherwise, display the element and its children
427       */
428     function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
429
430         if ( !$element )
431             return;
432
433         $id_field = $this->db_fields['id'];
434
435         //display this element
436         $cb_args = array_merge( array(&$output, $element, $depth), $args);
437         call_user_func_array(array(&$this, 'start_el'), $cb_args);
438
439