]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/classes.php
Wordpress 2.3.3
[autoinstalls/wordpress.git] / wp-includes / classes.php
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->$parent_field == $previous_element->$id_field) ) {
436                                 // Previous element is my parent. Descend a level.
437                                 array_unshift($parents, $previous_element);
438                                 if ( !$to_depth || ($depth < $to_depth) ) { //only descend if we're below $to_depth
439                                         $cb_args = array_merge( array($output, $depth), $args);
440                                         $output = call_user_func_array(array(&$this, 'start_lvl'), $cb_args);
441                                 } else if ( $to_depth && $depth == $to_depth  ) {  // If we've reached depth, end the previous element.
442                                         $cb_args = array_merge( array($output, $previous_element, $depth), $args);
443                                         $output = call_user_func_array(array(&$this, 'end_el'), $cb_args);
444                                 }
445                                 $depth++; //always do this so when we start the element further down, we know where we are
446                         } else if ( $element->$parent_field == $previous_element->$parent_field) {
447                                 // On the same level as previous element.
448                                 if ( !$to_depth || ($depth <= $to_depth) ) {
449                                         $cb_args = array_merge( array($output, $previous_element, $depth - 1), $args);
450                                         $output = call_user_func_array(array(&$this, 'end_el'), $cb_args);
451                                 }
452                         } else if ( $depth > 1 ) {
453                                 // Ascend one or more levels.
454                                 if ( !$to_depth || ($depth <= $to_depth) ) {
455                                         $cb_args = array_merge( array($output, $previous_element, $depth - 1), $args);
456                                         $output = call_user_func_array(array(&$this, 'end_el'), $cb_args);
457                                 }
458
459                                 while ( $parent = array_shift($parents) ) {
460                                         $depth--;
461                                         if ( !$to_depth || ($depth < $to_depth) ) {
462                                                 $cb_args = array_merge( array($output, $depth), $args);
463                                                 $output = call_user_func_array(array(&$this, 'end_lvl'), $cb_args);
464                                                 $cb_args = array_merge( array($output, $parent, $depth - 1), $args);
465                                                 $output = call_user_func_array(array(&$this, 'end_el'), $cb_args);
466                                         }
467                                         if ( $element->$parent_field == $parents[0]->$id_field ) {
468                                                 break;
469                                         }
470                                 }
471                         } else if ( !empty($previous_element) ) {
472                                 // Close off previous element.
473                                 if ( !$to_depth || ($depth <= $to_depth) ) {
474                                         $cb_args = array_merge( array($output, $previous_element, $depth - 1), $args);
475                                         $output = call_user_func_array(array(&$this, 'end_el'), $cb_args);
476                                 }
477                         }
478
479                         // Start the element.
480                         if ( !$to_depth || ($depth <= $to_depth) ) {
481                                 if ( $element->$id_field != 0 ) {
482                                         $cb_args = array_merge( array($output, $element, $depth - 1), $args);
483                                         $output = call_user_func_array(array(&$this, 'start_el'), $cb_args);
484                                 }
485                         }
486
487                         $previous_element = $element;
488                 }
489
490                 return $output;
491         }
492 }
493
494 class Walker_Page extends Walker {
495         var $tree_type = 'page';
496         var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID'); //TODO: decouple this
497
498         function start_lvl($output, $depth) {
499                 $indent = str_repeat("\t", $depth);
500                 $output .= "\n$indent<ul>\n";
501                 return $output;
502         }
503
504         function end_lvl($output, $depth) {
505                 $indent = str_repeat("\t", $depth);
506                 $output .= "$indent</ul>\n";
507                 return $output;
508         }
509
510         function start_el($output, $page, $depth, $current_page, $args) {
511                 if ( $depth )
512                         $indent = str_repeat("\t", $depth);
513                 extract($args, EXTR_SKIP);
514                 $css_class = 'page_item page-item-'.$page->ID;
515                 $_current_page = get_page( $current_page );
516                 if ( $page->ID == $current_page )
517                         $css_class .= ' current_page_item ';
518                 elseif ( $_current_page && $page->ID == $_current_page->post_parent )
519                         $css_class .= ' current_page_parent';
520
521                 $output .= $indent . '<li class="' . $css_class . '"><a href="' . get_page_link($page->ID) . '" title="' . attribute_escape(apply_filters('the_title', $page->post_title)) . '">' . apply_filters('the_title', $page->post_title) . '</a>';
522
523                 if ( !empty($show_date) ) {
524                         if ( 'modified' == $show_date )
525                                 $time = $page->post_modified;
526                         else
527                                 $time = $page->post_date;
528
529                         $output .= " " . mysql2date($date_format, $time);
530                 }
531
532                 return $output;
533         }
534
535         function end_el($output, $page, $depth) {
536                 $output .= "</li>\n";
537
538                 return $output;
539         }
540
541 }
542
543 class Walker_PageDropdown extends Walker {
544         var $tree_type = 'page';
545         var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID'); //TODO: decouple this
546
547         function start_el($output, $page, $depth, $args) {
548                                 $pad = str_repeat('&nbsp;', $depth * 3);
549
550                                 $output .= "\t<option value=\"$page->ID\"";
551                                 if ( $page->ID == $args['selected'] )
552                                                                 $output .= ' selected="selected"';
553                                 $output .= '>';
554                                 $title = wp_specialchars($page->post_title);
555                                 $output .= "$pad$title";
556                                 $output .= "</option>\n";
557
558                                 return $output;
559         }
560 }
561
562 class Walker_Category extends Walker {
563         var $tree_type = 'category';
564         var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this
565
566         function start_lvl($output, $depth, $args) {
567                 if ( 'list' != $args['style'] )
568                         return $output;
569
570                 $indent = str_repeat("\t", $depth);
571                 $output .= "$indent<ul class='children'>\n";
572                 return $output;
573         }
574
575         function end_lvl($output, $depth, $args) {
576                 if ( 'list' != $args['style'] )
577                         return $output;
578
579                 $indent = str_repeat("\t", $depth);
580                 $output .= "$indent</ul>\n";
581                 return $output;
582         }
583
584         function start_el($output, $category, $depth, $args) {
585                 extract($args);
586
587                 $cat_name = attribute_escape( $category->name);
588                 $cat_name = apply_filters( 'list_cats', $cat_name, $category );
589                 $link = '<a href="' . get_category_link( $category->term_id ) . '" ';
590                 if ( $use_desc_for_title == 0 || empty($category->description) )
591                         $link .= 'title="' . sprintf(__( 'View all posts filed under %s' ), $cat_name) . '"';
592                 else
593                         $link .= 'title="' . attribute_escape( apply_filters( 'category_description', $category->description, $category )) . '"';
594                 $link .= '>';
595                 $link .= $cat_name . '</a>';
596
597                 if ( (! empty($feed_image)) || (! empty($feed)) ) {
598                         $link .= ' ';
599
600                         if ( empty($feed_image) )
601                                 $link .= '(';
602
603                         $link .= '<a href="' . get_category_rss_link( 0, $category->term_id, $category->slug ) . '"';
604
605                         if ( empty($feed) )
606                                 $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
607                         else {
608                                 $title = ' title="' . $feed . '"';
609                                 $alt = ' alt="' . $feed . '"';
610                                 $name = $feed;
611                                 $link .= $title;
612                         }
613
614                         $link .= '>';
615
616                         if ( empty($feed_image) )
617                                 $link .= $name;
618                         else
619                                 $link .= "<img src='$feed_image'$alt$title" . ' />';
620                         $link .= '</a>';
621                         if ( empty($feed_image) )
622                                 $link .= ')';
623                 }
624
625                 if ( isset($show_count) && $show_count )
626                         $link .= ' (' . intval($category->count) . ')';
627
628                 if ( isset($show_date) && $show_date ) {
629                         $link .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp);
630                 }
631
632                 if ( $current_category )
633                         $_current_category = get_category( $current_category );
634
635                 if ( 'list' == $args['style'] ) {
636                         $output .= "\t<li";
637                         $class = 'cat-item cat-item-'.$category->term_id;
638                         if ( $current_category && ($category->term_id == $current_category) )
639                                 $class .=  ' current-cat';
640                         elseif ( $_current_category && ($category->term_id == $_current_category->parent) )
641                                 $class .=  ' current-cat-parent';
642                         $output .=  ' class="'.$class.'"';
643                         $output .= ">$link\n";
644                 } else {
645                         $output .= "\t$link<br />\n";
646                 }
647
648                 return $output;
649         }
650
651         function end_el($output, $page, $depth, $args) {
652                 if ( 'list' != $args['style'] )
653                         return $output;
654
655                 $output .= "</li>\n";
656                 return $output;
657         }
658
659 }
660
661 class Walker_CategoryDropdown extends Walker {
662         var $tree_type = 'category';
663         var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this
664
665         function start_el($output, $category, $depth, $args) {
666                 $pad = str_repeat('&nbsp;', $depth * 3);
667
668                 $cat_name = apply_filters('list_cats', $category->name, $category);
669                 $output .= "\t<option value=\"".$category->term_id."\"";
670                 if ( $category->term_id == $args['selected'] )
671                         $output .= ' selected="selected"';
672                 $output .= '>';
673                 $output .= $pad.$cat_name;
674                 if ( $args['show_count'] )
675                         $output .= '&nbsp;&nbsp;('. $category->count .')';
676                 if ( $args['show_last_update'] ) {
677                         $format = 'Y-m-d';
678                         $output .= '&nbsp;&nbsp;' . gmdate($format, $category->last_update_timestamp);
679                 }
680                 $output .= "</option>\n";
681
682                 return $output;
683         }
684 }
685
686 class WP_Ajax_Response {
687         var $responses = array();
688
689         function WP_Ajax_Response( $args = '' ) {
690                 if ( !empty($args) )
691                         $this->add($args);
692         }
693
694         // a WP_Error object can be passed in 'id' or 'data'
695         function add( $args = '' ) {
696                 $defaults = array(
697                         'what' => 'object', 'action' => false,
698                         'id' => '0', 'old_id' => false,
699                         'data' => '', 'supplemental' => array()
700                 );
701
702                 $r = wp_parse_args( $args, $defaults );
703                 extract( $r, EXTR_SKIP );
704
705                 if ( is_wp_error($id) ) {
706                         $data = $id;
707                         $id = 0;
708                 }
709
710                 $response = '';
711                 if ( is_wp_error($data) )
712                         foreach ( $data->get_error_codes() as $code )
713                                 $response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message($code) . "]]></wp_error>";
714                 else
715                         $response = "<response_data><![CDATA[$data]]></response_data>";
716
717                 $s = '';
718                 if ( (array) $supplemental )
719                         foreach ( $supplemental as $k => $v )
720                                 $s .= "<$k><![CDATA[$v]]></$k>";
721
722                 if ( false === $action )
723                         $action = $_POST['action'];
724
725                 $x = '';
726                 $x .= "<response action='{$action}_$id'>"; // The action attribute in the xml output is formatted like a nonce action
727                 $x .=   "<$what id='$id'" . ( false !== $old_id ? "old_id='$old_id'>" : '>' );
728                 $x .=           $response;
729                 $x .=           $s;
730                 $x .=   "</$what>";
731                 $x .= "</response>";
732
733                 $this->responses[] = $x;
734                 return $x;
735         }
736
737         function send() {
738                 header('Content-Type: text/xml');
739                 echo "<?xml version='1.0' standalone='yes'?><wp_ajax>";
740                 foreach ( $this->responses as $response )
741                         echo $response;
742                 echo '</wp_ajax>';
743                 die();
744         }
745 }
746
747 ?>