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

Revision 1139, 22.8 kB (checked in by donncha, 1 year ago)

Merge with WordPress? 2.3.1

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