]> scripts.mit.edu Git - autoinstallsdev/wordpress.git/blob - wp-includes/classes.php
Wordpress 2.5.1
[autoinstallsdev/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', 'taxonomy', 'term');
5
6         var $private_query_vars = array('offset', 'posts_per_page', 'posts_per_archive_page', 'what_to_show', 'showposts', 'nopaging', 'post_type', 'post_status', 'category__in', 'category__not_in', 'category__and', 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'tag_id', 'post_mime_type', 'perm');
7         var $extra_query_vars = array();
8
9         var $query_vars;
10         var $query_string;
11         var $request;
12         var $matched_rule;
13         var $matched_query;
14         var $did_permalink = false;
15
16         function add_query_var($qv) {
17                 if ( !in_array($qv, $this->public_query_vars) )
18                         $this->public_query_vars[] = $qv;
19         }
20
21         function set_query_var($key, $value) {
22                 $this->query_vars[$key] = $value;
23         }
24
25         function parse_request($extra_query_vars = '') {
26                 global $wp_rewrite;
27
28                 $this->query_vars = array();
29
30                 if ( is_array($extra_query_vars) )
31                         $this->extra_query_vars = & $extra_query_vars;
32                 else if (! empty($extra_query_vars))
33                         parse_str($extra_query_vars, $this->extra_query_vars);
34
35                 // Process PATH_INFO, REQUEST_URI, and 404 for permalinks.
36
37                 // Fetch the rewrite rules.
38                 $rewrite = $wp_rewrite->wp_rewrite_rules();
39
40                 if (! empty($rewrite)) {
41                         // If we match a rewrite rule, this will be cleared.
42                         $error = '404';
43                         $this->did_permalink = true;
44
45                         if ( isset($_SERVER['PATH_INFO']) )
46                                 $pathinfo = $_SERVER['PATH_INFO'];
47                         else
48                                 $pathinfo = '';
49                         $pathinfo_array = explode('?', $pathinfo);
50                         $pathinfo = str_replace("%", "%25", $pathinfo_array[0]);
51                         $req_uri = $_SERVER['REQUEST_URI'];
52                         $req_uri_array = explode('?', $req_uri);
53                         $req_uri = $req_uri_array[0];
54                         $self = $_SERVER['PHP_SELF'];
55                         $home_path = parse_url(get_option('home'));
56                         if ( isset($home_path['path']) )
57                                 $home_path = $home_path['path'];
58                         else
59                                 $home_path = '';
60                         $home_path = trim($home_path, '/');
61
62                         // Trim path info from the end and the leading home path from the
63                         // front.  For path info requests, this leaves us with the requesting
64                         // filename, if any.  For 404 requests, this leaves us with the
65                         // requested permalink.
66                         $req_uri = str_replace($pathinfo, '', rawurldecode($req_uri));
67                         $req_uri = trim($req_uri, '/');
68                         $req_uri = preg_replace("|^$home_path|", '', $req_uri);
69                         $req_uri = trim($req_uri, '/');
70                         $pathinfo = trim($pathinfo, '/');
71                         $pathinfo = preg_replace("|^$home_path|", '', $pathinfo);
72                         $pathinfo = trim($pathinfo, '/');
73                         $self = trim($self, '/');
74                         $self = preg_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 ( !empty($this->query_vars['withcomments'])
189                                 || ( empty($this->query_vars['withoutcomments'])
190                                         && ( !empty($this->query_vars['p'])
191                                                 || !empty($this->query_vars['name'])
192                                                 || !empty($this->query_vars['page_id'])
193                                                 || !empty($this->query_vars['pagename'])
194                                                 || !empty($this->query_vars['attachment'])
195                                                 || !empty($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 = empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? '' : 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                 if ( has_filter('query_string') ) {  // Don't bother filtering and parsing if no plugins are hooked in.
242                         $this->query_string = apply_filters('query_string', $this->query_string);
243                         parse_str($this->query_string, $this->query_vars);
244                 }
245         }
246
247         function register_globals() {
248                 global $wp_query;
249                 // Extract updated query vars back into global namespace.
250                 foreach ($wp_query->query_vars as $key => $value) {
251                         $GLOBALS[$key] = $value;
252                 }
253
254                 $GLOBALS['query_string'] = & $this->query_string;
255                 $GLOBALS['posts'] = & $wp_query->posts;
256                 $GLOBALS['post'] = & $wp_query->post;
257                 $GLOBALS['request'] = & $wp_query->request;
258
259                 if ( is_single() || is_page() ) {
260                         $GLOBALS['more'] = 1;
261                         $GLOBALS['single'] = 1;
262                 }
263         }
264
265         function init() {
266                 wp_get_current_user();
267         }
268
269         function query_posts() {
270                 global $wp_the_query;
271                 $this->build_query_string();
272                 $wp_the_query->query($this->query_vars);
273         }
274
275         function handle_404() {
276                 global $wp_query;
277                 // Issue a 404 if a permalink request doesn't match any posts.  Don't
278                 // issue a 404 if one was already issued, if the request was a search,
279                 // or if the request was a regular query string request rather than a
280                 // permalink request.
281                 if ( (0 == count($wp_query->posts)) && !is_404() && !is_search() && ( $this->did_permalink || (!empty($_SERVER['QUERY_STRING']) && (false === strpos($_SERVER['REQUEST_URI'], '?'))) ) ) {
282                         $wp_query->set_404();
283                         status_header( 404 );
284                         nocache_headers();
285                 }       elseif( is_404() != true ) {
286                         status_header( 200 );
287                 }
288         }
289
290         function main($query_args = '') {
291                 $this->init();
292                 $this->parse_request($query_args);
293                 $this->send_headers();
294                 $this->query_posts();
295                 $this->handle_404();
296                 $this->register_globals();
297                 do_action_ref_array('wp', array(&$this));
298         }
299
300         function WP() {
301                 // Empty.
302         }
303 }
304
305 class WP_Error {
306         var $errors = array();
307         var $error_data = array();
308
309         function WP_Error($code = '', $message = '', $data = '') {
310                 if ( empty($code) )
311                         return;
312
313                 $this->errors[$code][] = $message;
314
315                 if ( ! empty($data) )
316                         $this->error_data[$code] = $data;
317         }
318
319         function get_error_codes() {
320                 if ( empty($this->errors) )
321                         return array();
322
323                 return array_keys($this->errors);
324         }
325
326         function get_error_code() {
327                 $codes = $this->get_error_codes();
328
329                 if ( empty($codes) )
330                         return '';
331
332                 return $codes[0];
333         }
334
335         function get_error_messages($code = '') {
336                 // Return all messages if no code specified.
337                 if ( empty($code) ) {
338                         $all_messages = array();
339                         foreach ( $this->errors as $code => $messages )
340                                 $all_messages = array_merge($all_messages, $messages);
341
342                         return $all_messages;
343                 }
344
345                 if ( isset($this->errors[$code]) )
346                         return $this->errors[$code];
347                 else
348                         return array();
349         }
350
351         function get_error_message($code = '') {
352                 if ( empty($code) )
353                         $code = $this->get_error_code();
354                 $messages = $this->get_error_messages($code);
355                 if ( empty($messages) )
356                         return '';
357                 return $messages[0];
358         }
359
360         function get_error_data($code = '') {
361                 if ( empty($code) )
362                         $code = $this->get_error_code();
363
364                 if ( isset($this->error_data[$code]) )
365                         return $this->error_data[$code];
366                 return null;
367         }
368
369         function add($code, $message, $data = '') {
370                 $this->errors[$code][] = $message;
371                 if ( ! empty($data) )
372                         $this->error_data[$code] = $data;
373         }
374
375         function add_data($data, $code = '') {
376                 if ( empty($code) )
377                         $code = $this->get_error_code();
378
379                 $this->error_data[$code] = $data;
380         }
381 }
382
383 function is_wp_error($thing) {
384         if ( is_object($thing) && is_a($thing, 'WP_Error') )
385                 return true;
386         return false;
387 }
388
389 /*
390  * A class for displaying various tree-like structures.
391  * 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) {}
399         function end_lvl(&$output)   {}
400         function start_el(&$output)  {}
401         function end_el(&$output)    {}
402
403         /*
404          * display one element if the element doesn't have any children
405          * otherwise, display the element and its children
406          */
407         function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
408
409                 if ( !$element )
410                         return;
411
412                 $id_field = $this->db_fields['id'];
413                 $parent_field = $this->db_fields['parent'];
414
415                 //display this element
416                 $cb_args = array_merge( array(&$output, $element, $depth), $args);
417                 call_user_func_array(array(&$this, 'start_el'), $cb_args);
418
419                 if ( $max_depth == 0 ||
420                      ($max_depth != 0 &&  $max_depth > $depth+1 )) { //whether to descend
421
422                         for ( $i = 0; $i < sizeof( $children_elements ); $i++ ) {
423
424                                 $child = $children_elements[$i];
425                                 if ( $child->$parent_field == $element->$id_field ) {
426
427                                         if ( !isset($newlevel) ) {
428                                                 $newlevel = true;
429                                                 //start the child delimiter
430                                                 $cb_args = array_merge( array(&$output, $depth), $args);
431                                                 call_user_func_array(array(&$this, 'start_lvl'), $cb_args);
432                                         }
433
434                                         array_splice( $children_elements, $i, 1 );
435                                         $this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
436                                         $i = -1;
437                                 }
438                         }
439                 }
440
441                 if ( isset($newlevel) && $newlevel ){
442                         //end the child delimiter
443                         $cb_args = array_merge( array(&$output, $depth), $args);
444                         call_user_func_array(array(&$this, 'end_lvl'), $cb_args);
445                 }
446
447                 //end this element
448                 $cb_args = array_merge( array(&$output, $element, $depth), $args);
449                 call_user_func_array(array(&$this, 'end_el'), $cb_args);
450         }
451
452         /*
453         * displays array of elements hierarchically
454         * it is a generic function which does not assume any existing order of elements
455         * max_depth = -1 means flatly display every element
456         * max_depth = 0  means display all levels
457         * max_depth > 0  specifies the number of display levels.
458         */
459         function walk( $elements, $max_depth) {
460
461                 $args = array_slice(func_get_args(), 2);
462                 $output = '';
463
464                 if ($max_depth < -1) //invalid parameter
465                         return $output;
466
467                 if (empty($elements)) //nothing to walk
468                         return $output;
469
470                 $id_field = $this->db_fields['id'];
471                 $parent_field = $this->db_fields['parent'];
472
473                 // flat display
474                 if ( -1 == $max_depth ) {
475                         $empty_array = array();
476                         foreach ( $elements as $e )
477                                 $this->display_element( $e, $empty_array, 1, 0, $args, $output );
478                         return $output;
479                 }
480
481                 /*
482                  * need to display in hierarchical order
483                  * splice elements into two buckets: those without parent and those with parent
484                  */
485                 $top_level_elements = array();
486                 $children_elements  = array();
487                 foreach ( $elements as $e) {
488                         if ( 0 == $e->$parent_field )
489                                 $top_level_elements[] = $e;
490                         else
491                                 $children_elements[] = $e;
492                 }
493
494                 /*
495                  * none of the elements is top level
496                  * the first one must be root of the sub elements
497                  */
498                 if ( !$top_level_elements ) {
499
500                         $root = $children_elements[0];
501                         for ( $i = 0; $i < sizeof( $children_elements ); $i++ ) {
502
503                                 $child = $children_elements[$i];
504                                 if ($root->$parent_field == $child->$parent_field ) {
505                                         $top_level_elements[] = $child;
506                                         array_splice( $children_elements, $i, 1 );
507                                         $i--;
508                                 }
509                         }
510                 }
511
512                 foreach ( $top_level_elements as $e )
513                         $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
514
515                 /*
516                 * if we are displaying all levels, and remaining children_elements is not empty,
517                 * then we got orphans, which should be displayed regardless
518                 */
519                 if ( ( $max_depth == 0 ) && sizeof( $children_elements ) > 0 ) {
520                         $empty_array = array();
521                         foreach ( $children_elements as $orphan_e )
522                                 $this->display_element( $orphan_e, $empty_array, 1, 0, $args, $output );
523                  }
524                  return $output;
525         }
526 }
527
528 class Walker_Page extends Walker {
529         var $tree_type = 'page';
530         var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID'); //TODO: decouple this
531
532         function start_lvl(&$output, $depth) {
533                 $indent = str_repeat("\t", $depth);
534                 $output .= "\n$indent<ul>\n";
535         }
536
537         function end_lvl(&$output, $depth) {
538                 $indent = str_repeat("\t", $depth);
539                 $output .= "$indent</ul>\n";
540         }
541
542         function start_el(&$output, $page, $depth, $current_page, $args) {
543                 if ( $depth )
544                         $indent = str_repeat("\t", $depth);
545                 else
546                         $indent = '';
547
548                 extract($args, EXTR_SKIP);
549                 $css_class = 'page_item page-item-'.$page->ID;
550                 if ( !empty($current_page) ) {
551                         $_current_page = get_page( $current_page );
552                         if ( in_array($page->ID, (array) $_current_page->ancestors) )
553                                 $css_class .= ' current_page_ancestor';
554                         if ( $page->ID == $current_page )
555                                 $css_class .= ' current_page_item';
556                         elseif ( $_current_page && $page->ID == $_current_page->post_parent )
557                                 $css_class .= ' current_page_parent';
558                 }
559
560                 $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>';
561
562                 if ( !empty($show_date) ) {
563                         if ( 'modified' == $show_date )
564                                 $time = $page->post_modified;
565                         else
566                                 $time = $page->post_date;
567
568                         $output .= " " . mysql2date($date_format, $time);
569                 }
570         }
571
572         function end_el(&$output, $page, $depth) {
573                 $output .= "</li>\n";
574         }
575
576 }
577
578 class Walker_PageDropdown extends Walker {
579         var $tree_type = 'page';
580         var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID'); //TODO: decouple this
581
582         function start_el(&$output, $page, $depth, $args) {
583                 $pad = str_repeat('&nbsp;', $depth * 3);
584
585                 $output .= "\t<option value=\"$page->ID\"";
586                 if ( $page->ID == $args['selected'] )
587                         $output .= ' selected="selected"';
588                 $output .= '>';
589                 $title = wp_specialchars($page->post_title);
590                 $output .= "$pad$title";
591                 $output .= "</option>\n";
592         }
593 }
594
595 class Walker_Category extends Walker {
596         var $tree_type = 'category';
597         var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this
598
599         function start_lvl(&$output, $depth, $args) {
600                 if ( 'list' != $args['style'] )
601                         return;
602
603                 $indent = str_repeat("\t", $depth);
604                 $output .= "$indent<ul class='children'>\n";
605         }
606
607         function end_lvl(&$output, $depth, $args) {
608                 if ( 'list' != $args['style'] )
609                         return;
610
611                 $indent = str_repeat("\t", $depth);
612                 $output .= "$indent</ul>\n";
613         }
614
615         function start_el(&$output, $category, $depth, $args) {
616                 extract($args);
617
618                 $cat_name = attribute_escape( $category->name);
619                 $cat_name = apply_filters( 'list_cats', $cat_name, $category );
620                 $link = '<a href="' . get_category_link( $category->term_id ) . '" ';
621                 if ( $use_desc_for_title == 0 || empty($category->description) )
622                         $link .= 'title="' . sprintf(__( 'View all posts filed under %s' ), $cat_name) . '"';
623                 else
624                         $link .= 'title="' . attribute_escape( apply_filters( 'category_description', $category->description, $category )) . '"';
625                 $link .= '>';
626                 $link .= $cat_name . '</a>';
627
628                 if ( (! empty($feed_image)) || (! empty($feed)) ) {
629                         $link .= ' ';
630
631                         if ( empty($feed_image) )
632                                 $link .= '(';
633
634                         $link .= '<a href="' . get_category_feed_link($category->term_id, $feed_type) . '"';
635
636                         if ( empty($feed) )
637                                 $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
638                         else {
639                                 $title = ' title="' . $feed . '"';
640                                 $alt = ' alt="' . $feed . '"';
641                                 $name = $feed;
642                                 $link .= $title;
643                         }
644
645                         $link .= '>';
646
647                         if ( empty($feed_image) )
648                                 $link .= $name;
649                         else
650                                 $link .= "<img src='$feed_image'$alt$title" . ' />';
651                         $link .= '</a>';
652                         if ( empty($feed_image) )
653                                 $link .= ')';
654                 }
655
656                 if ( isset($show_count) && $show_count )
657                         $link .= ' (' . intval($category->count) . ')';
658
659                 if ( isset($show_date) && $show_date ) {
660                         $link .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp);
661                 }
662
663                 if ( isset($current_category) && $current_category )
664                         $_current_category = get_category( $current_category );
665
666                 if ( 'list' == $args['style'] ) {
667                         $output .= "\t<li";
668                         $class = 'cat-item cat-item-'.$category->term_id;
669                         if ( isset($current_category) && $current_category && ($category->term_id == $current_category) )
670                                 $class .=  ' current-cat';
671                         elseif ( isset($_current_category) && $_current_category && ($category->term_id == $_current_category->parent) )
672                                 $class .=  ' current-cat-parent';
673                         $output .=  ' class="'.$class.'"';
674                         $output .= ">$link\n";
675                 } else {
676                         $output .= "\t$link<br />\n";
677                 }
678         }
679
680         function end_el(&$output, $page, $depth, $args) {
681                 if ( 'list' != $args['style'] )
682                         return;
683
684                 $output .= "</li>\n";
685         }
686
687 }
688
689 class Walker_CategoryDropdown extends Walker {
690         var $tree_type = 'category';
691         var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this
692
693         function start_el(&$output, $category, $depth, $args) {
694                 $pad = str_repeat('&nbsp;', $depth * 3);
695
696                 $cat_name = apply_filters('list_cats', $category->name, $category);
697                 $output .= "\t<option value=\"".$category->term_id."\"";
698                 if ( $category->term_id == $args['selected'] )
699                         $output .= ' selected="selected"';
700                 $output .= '>';
701                 $output .= $pad.$cat_name;
702                 if ( $args['show_count'] )
703                         $output .= '&nbsp;&nbsp;('. $category->count .')';
704                 if ( $args['show_last_update'] ) {
705                         $format = 'Y-m-d';
706                         $output .= '&nbsp;&nbsp;' . gmdate($format, $category->last_update_timestamp);
707                 }
708                 $output .= "</option>\n";
709         }
710 }
711
712 class WP_Ajax_Response {
713         var $responses = array();
714
715         function WP_Ajax_Response( $args = '' ) {
716                 if ( !empty($args) )
717                         $this->add($args);
718         }
719
720         // a WP_Error object can be passed in 'id' or 'data'
721         function add( $args = '' ) {
722                 $defaults = array(
723                         'what' => 'object', 'action' => false,
724                         'id' => '0', 'old_id' => false,
725                         'position' => 1, // -1 = top, 1 = bottom, html ID = after, -html ID = before
726                         'data' => '', 'supplemental' => array()
727                 );
728
729                 $r = wp_parse_args( $args, $defaults );
730                 extract( $r, EXTR_SKIP );
731                 $position = preg_replace( '/[^a-z0-9:_-]/i', '', $position );
732
733                 if ( is_wp_error($id) ) {
734                         $data = $id;
735                         $id = 0;
736                 }
737
738                 $response = '';
739                 if ( is_wp_error($data) ) {
740                         foreach ( $data->get_error_codes() as $code ) {
741                                 $response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message($code) . "]]></wp_error>";
742                                 if ( !$error_data = $data->get_error_data($code) )
743                                         continue;
744                                 $class = '';
745                                 if ( is_object($error_data) ) {
746                                         $class = ' class="' . get_class($error_data) . '"';
747                                         $error_data = get_object_vars($error_data);
748                                 }
749
750                                 $response .= "<wp_error_data code='$code'$class>";
751
752                                 if ( is_scalar($error_data) ) {
753                                         $response .= "<![CDATA[$error_data]]>";
754                                 } elseif ( is_array($error_data) ) {
755                                         foreach ( $error_data as $k => $v )
756                                                 $response .= "<$k><![CDATA[$v]]></$k>";
757                                 }
758
759                                 $response .= "</wp_error_data>";
760                         }
761                 } else {
762                         $response = "<response_data><![CDATA[$data]]></response_data>";
763                 }
764
765                 $s = '';
766                 if ( (array) $supplemental ) {
767                         foreach ( $supplemental as $k => $v )
768                                 $s .= "<$k><![CDATA[$v]]></$k>";
769                         $s = "<supplemental>$s</supplemental>";
770                 }
771
772                 if ( false === $action )
773                         $action = $_POST['action'];
774
775                 $x = '';
776                 $x .= "<response action='{$action}_$id'>"; // The action attribute in the xml output is formatted like a nonce action
777                 $x .=   "<$what id='$id' " . ( false === $old_id ? '' : "old_id='$old_id' " ) . "position='$position'>";
778                 $x .=           $response;
779                 $x .=           $s;
780                 $x .=   "</$what>";
781                 $x .= "</response>";
782
783                 $this->responses[] = $x;
784                 return $x;
785         }
786
787         function send() {
788                 header('Content-Type: text/xml');
789                 echo "<?xml version='1.0' standalone='yes'?><wp_ajax>";
790                 foreach ( $this->responses as $response )
791                         echo $response;
792                 echo '</wp_ajax>';
793                 die();
794         }
795 }
796
797 ?>