root/tags/2.1.1/wp-includes/classes.php

Revision 4779, 22.3 kB (checked in by ryan, 2 years ago)

Walker fix ups. fixes #3080

  • 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
153         foreach ($this->private_query_vars as $var) {
154             if (isset($this->extra_query_vars[$var]))
155                 $this->query_vars[$var] = $this->extra_query_vars[$var];
156             elseif (isset($GLOBALS[$var]) && '' != $GLOBALS[$var])
157                 $this->query_vars[$var] = $GLOBALS[$var];
158         }
159
160         if ( isset($error) )
161             $this->query_vars['error'] = $error;
162
163         $this->query_vars = apply_filters('request', $this->query_vars);
164
165         do_action_ref_array('parse_request', array(&$this));
166     }
167
168     function send_headers() {
169         @header('X-Pingback: '. get_bloginfo('pingback_url'));
170         if ( is_user_logged_in() )
171             nocache_headers();
172         if ( !empty($this->query_vars['error']) && '404' == $this->query_vars['error'] ) {
173             status_header( 404 );
174             if ( !is_user_logged_in() )
175                 nocache_headers();
176             @header('Content-type: ' . get_option('html_type') . '; charset=' . get_option('blog_charset'));
177         } else if ( empty($this->query_vars['feed']) ) {
178             @header('Content-type: ' . get_option('html_type') . '; charset=' . get_option('blog_charset'));
179         } else {
180             // We're showing a feed, so WP is indeed the only thing that last changed
181             if ( $this->query_vars['withcomments']
182                 || ( !$this->query_vars['withoutcomments']
183                     && ( $this->query_vars['p']
184                         || $this->query_vars['name']
185                         || $this->query_vars['page_id']
186                         || $this->query_vars['pagename']
187                         || $this->query_vars['attachment']
188                         || $this->query_vars['attachment_id']
189                     )
190                 )
191             )
192                 $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastcommentmodified('GMT'), 0).' GMT';
193             else
194                 $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0).' GMT';
195             $wp_etag = '"' . md5($wp_last_modified) . '"';
196             @header("Last-Modified: $wp_last_modified");
197             @header("ETag: $wp_etag");
198
199             // Support for Conditional GET
200             if (isset($_SERVER['HTTP_IF_NONE_MATCH']))
201                 $client_etag = stripslashes(stripslashes($_SERVER['HTTP_IF_NONE_MATCH']));
202             else $client_etag = false;
203
204             $client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE']);
205             // If string is empty, return 0. If not, attempt to parse into a timestamp
206             $client_modified_timestamp = $client_last_modified ? strtotime($client_last_modified) : 0;
207
208             // Make a timestamp for our most recent modification...
209             $wp_modified_timestamp = strtotime($wp_last_modified);
210
211             if ( ($client_last_modified && $client_etag) ?
212                      (($client_modified_timestamp >= $wp_modified_timestamp) && ($client_etag == $wp_etag)) :
213                      (($client_modified_timestamp >= $wp_modified_timestamp) || ($client_etag == $wp_etag)) ) {
214                 status_header( 304 );
215                 exit;
216             }
217         }
218
219         do_action_ref_array('send_headers', array(&$this));
220     }
221
222     function build_query_string() {
223         $this->query_string = '';
224         foreach (array_keys($this->query_vars) as $wpvar) {
225             if ( '' != $this->query_vars[$wpvar] ) {
226                 $this->query_string .= (strlen($this->query_string) < 1) ? '' : '&';
227                 if ( !is_scalar($this->query_vars[$wpvar]) ) // Discard non-scalars.
228                     continue;
229                 $this->query_string .= $wpvar . '=' . rawurlencode($this->query_vars[$wpvar]);
230             }
231         }
232
233         // query_string filter deprecated.  Use request filter instead.
234         global $wp_filter;
235         if ( isset($wp_filter['query_string']) ) {  // Don't bother filtering and parsing if no plugins are hooked in.
236             $this->query_string = apply_filters('query_string', $this->query_string);
237             parse_str($this->query_string, $this->query_vars);
238         }
239     }
240
241     function register_globals() {
242         global $wp_query;
243         // Extract updated query vars back into global namespace.
244         foreach ($wp_query->query_vars as $key => $value) {
245             $GLOBALS[$key] = $value;
246         }
247
248         $GLOBALS['query_string'] = & $this->query_string;
249         $GLOBALS['posts'] = & $wp_query->posts;
250         $GLOBALS['post'] = & $wp_query->post;
251         $GLOBALS['request'] = & $wp_query->request;
252
253         if ( is_single() || is_page() ) {
254             $GLOBALS['more'] = 1;
255             $GLOBALS['single'] = 1;
256         }
257     }
258
259     function init() {
260         wp_get_current_user();
261     }
262
263     function query_posts() {
264         global $wp_the_query;
265         $this->build_query_string();
266         $wp_the_query->query($this->query_vars);
267      }
268
269     function handle_404() {
270         global $wp_query;
271         // Issue a 404 if a permalink request doesn't match any posts.  Don't
272         // issue a 404 if one was already issued, if the request was a search,
273         // or if the request was a regular query string request rather than a
274         // permalink request.
275         if ( (0 == count($wp_query->posts)) && !is_404() && !is_search() && ( $this->did_permalink || (!empty($_SERVER['QUERY_STRING']) && (false === strpos($_SERVER['REQUEST_URI'], '?'))) ) ) {
276             $wp_query->set_404();
277             status_header( 404 );
278             nocache_headers();
279         }    elseif( is_404() != true ) {
280             status_header( 200 );
281         }
282     }
283
284     function main($query_args = '') {
285         $this->init();
286         $this->parse_request($query_args);
287         $this->send_headers();
288         $this->query_posts();
289         $this->handle_404();
290         $this->register_globals();
291         do_action_ref_array('wp', array(&$this));
292     }
293
294     function WP() {
295         // Empty.
296     }
297 }
298
299 class WP_Error {
300     var $errors = array();
301     var $error_data = array();
302
303     function WP_Error($code = '', $message = '', $data = '') {
304         if ( empty($code) )
305             return;
306
307         $this->errors[$code][] = $message;
308
309         if ( ! empty($data) )
310             $this->error_data[$code] = $data;
311     }
312
313     function get_error_codes() {
314         if ( empty($this->errors) )
315             return array();
316
317         return array_keys($this->errors);
318     }
319
320     function get_error_code() {
321         $codes = $this->get_error_codes();
322
323         if ( empty($codes) )
324             return '';
325
326         return $codes[0];
327     }
328
329     function get_error_messages($code = '') {
330         // Return all messages if no code specified.
331         if ( empty($code) ) {
332             $all_messages = array();
333             foreach ( $this->errors as $code => $messages )
334                 $all_messages = array_merge($all_messages, $messages);
335
336             return $all_messages;
337         }
338
339         if ( isset($this->errors[$code]) )
340             return $this->errors[$code];
341         else
342             return array();
343     }
344
345     function get_error_message($code = '') {
346         if ( empty($code) )
347             $code = $this->get_error_code();
348         $messages = $this->get_error_messages($code);
349         if ( empty($messages) )
350             return '';
351         return $messages[0];
352     }
353
354     function get_error_data($code = '') {
355         if ( empty($code) )
356             $code = $this->get_error_code();
357
358         if ( isset($this->error_data[$code]) )
359             return $this->error_data[$code];
360         return null;
361     }
362
363     function add($code, $message, $data = '') {
364         $this->errors[$code][] = $message;
365         if ( ! empty($data) )
366             $this->error_data[$code] = $data;
367     }
368
369     function add_data($data, $code = '') {
370         if ( empty($code) )
371             $code = $this->get_error_code();
372
373         $this->error_data[$code] = $data;
374     }
375 }
376
377 function is_wp_error($thing) {
378     if ( is_object($thing) && is_a($thing, 'WP_Error') )
379         return true;
380     return false;
381 }
382
383
384 // A class for displaying various tree-like structures. Extend the Walker class to use it, see examples at the bottom
385
386 class Walker {
387     var $tree_type;
388     var $db_fields;
389
390     //abstract callbacks
391     function start_lvl($output) { return $output; }
392     function end_lvl($output)   { return $output; }
393     function start_el($output)  { return $output; }
394     function end_el($output)    { return $output; }
395
396     function walk($elements, $to_depth) {
397         $args = array_slice(func_get_args(), 2); $parents = array(); $depth = 1; $previous_element = ''; $output = '';
398
399         //padding at the end
400         $last_element->post_parent = 0;
401         $last_element->post_id = 0;
402         $elements[] = $last_element;
403
404         $id_field = $this->db_fields['id'];
405         $parent_field = $this->db_fields['parent'];
406
407         $flat = ($to_depth == -1) ? true : false;
408
409         foreach ( $elements as $element ) {
410             // If flat, start and end the element and skip the level checks.
411             if ( $flat) {
412                 // Start the element.
413                 if ( isset($element->$id_field) && $element->$id_field != 0 ) {
414                     $cb_args = array_merge( array($output, $element, $depth - 1), $args);
415                     $output = call_user_func_array(array(&$this, 'start_el'), $cb_args);
416                 }
417     
418                 // End the element.
419                 if ( isset($element->$id_field) && $element->$id_field != 0 ) {
420                     $cb_args = array_merge( array($output, $element, $depth - 1), $args);
421                     $output = call_user_func_array(array(&$this, 'end_el'), $cb_args);
422                 }
423     
424                 continue;   
425             }
426     
427             // Walk the tree.
428             if ( !empty($previous_element) && ($element->$parent_field == $previous_element->$id_field) ) {
429                 // Previous element is my parent. Descend a level.
430                 array_unshift($parents, $previous_element);
431                 if ( !$to_depth || ($depth < $to_depth) ) { //only descend if we're below $to_depth
432                     $cb_args = array_merge( array($output, $depth), $args);
433                     $output = call_user_func_array(array(&$this, 'start_lvl'), $cb_args);
434                 } else if ( $to_depth && $depth == $to_depth