root/branches/2.1/wp-includes/classes.php

Revision 5052, 22.5 kB (checked in by ryan, 2 years ago)

Don't cast to string if empty. Props donncha. fixes #3979 for 2.1

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