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');
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();
14 var $did_permalink = false;
16 function add_query_var($qv) {
17 $this->public_query_vars[] = $qv;
20 function set_query_var($key, $value) {
21 $this->query_vars[$key] = $value;
24 function parse_request($extra_query_vars = '') {
27 $this->query_vars = array();
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);
34 // Process PATH_INFO, REQUEST_URI, and 404 for permalinks.
36 // Fetch the rewrite rules.
37 $rewrite = $wp_rewrite->wp_rewrite_rules();
39 if (! empty($rewrite)) {
40 // If we match a rewrite rule, this will be cleared.
42 $this->did_permalink = true;
44 if ( isset($_SERVER['PATH_INFO']) )
45 $pathinfo = $_SERVER['PATH_INFO'];
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'];
59 $home_path = trim($home_path, '/');
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, '/');
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) ) {
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 )
88 $this->request = $request;
91 $request_match = $request;
92 foreach ($rewrite as $match => $query) {
93 // If the requesting file is the anchor of the match, prepend it
95 if ((! empty($req_uri)) && (strpos($match, $req_uri) === 0) && ($req_uri != $request)) {
96 $request_match = $req_uri . '/' . $request;
99 if (preg_match("!^$match!", $request_match, $matches) ||
100 preg_match("!^$match!", urldecode($request_match), $matches)) {
102 $this->matched_rule = $match;
104 // Trim the query of everything up to the '?'.
105 $query = preg_replace("!^.+\?!", '', $query);
107 // Substitute the substring matches into the query.
108 eval("\$query = \"$query\";");
109 $this->matched_query = $query;
112 parse_str($query, $perma_query_vars);
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']);
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']);
134 if (isset($perma_query_vars) && strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false)
135 unset($perma_query_vars);
137 $this->did_permalink = false;
141 $this->public_query_vars = apply_filters('query_vars', $this->public_query_vars);
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];
156 if ( !empty( $this->query_vars[$wpvar] ) )
157 $this->query_vars[$wpvar] = (string) $this->query_vars[$wpvar];
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];
168 $this->query_vars['error'] = $error;
170 $this->query_vars = apply_filters('request', $this->query_vars);
172 do_action_ref_array('parse_request', array(&$this));
175 function send_headers() {
176 @header('X-Pingback: '. get_bloginfo('pingback_url'));
177 if ( is_user_logged_in() )
179 if ( !empty($this->query_vars['error']) && '404' == $this->query_vars['error'] ) {
180 status_header( 404 );
181 if ( !is_user_logged_in() )
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'));
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']
199 $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastcommentmodified('GMT'), 0).' GMT';
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");
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;
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;
215 // Make a timestamp for our most recent modification...
216 $wp_modified_timestamp = strtotime($wp_last_modified);
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 );
226 do_action_ref_array('send_headers', array(&$this));
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.
236 $this->query_string .= $wpvar . '=' . rawurlencode($this->query_vars[$wpvar]);
240 // query_string filter deprecated. Use request filter instead.
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);
248 function register_globals() {
250 // Extract updated query vars back into global namespace.
251 foreach ($wp_query->query_vars as $key => $value) {
252 $GLOBALS[$key] = $value;
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;
260 if ( is_single() || is_page() ) {
261 $GLOBALS['more'] = 1;
262 $GLOBALS['single'] = 1;
267 wp_get_current_user();
270 function query_posts() {
271 global $wp_the_query;
272 $this->build_query_string();
273 $wp_the_query->query($this->query_vars);
276 function handle_404() {
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 );
286 } elseif( is_404() != true ) {
287 status_header( 200 );
291 function main($query_args = '') {
293 $this->parse_request($query_args);
294 $this->send_headers();
295 $this->query_posts();
297 $this->register_globals();
298 do_action_ref_array('wp', array(&$this));
307 var $errors = array();
308 var $error_data = array();
310 function WP_Error($code = '', $message = '', $data = '') {
314 $this->errors[$code][] = $message;
316 if ( ! empty($data) )
317 $this->error_data[$code] = $data;
320 function get_error_codes() {
321 if ( empty($this->errors) )
324 return array_keys($this->errors);
327 function get_error_code() {
328 $codes = $this->get_error_codes();
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);
343 return $all_messages;
346 if ( isset($this->errors[$code]) )
347 return $this->errors[$code];
352 function get_error_message($code = '') {
354 $code = $this->get_error_code();
355 $messages = $this->get_error_messages($code);
356 if ( empty($messages) )
361 function get_error_data($code = '') {
363 $code = $this->get_error_code();
365 if ( isset($this->error_data[$code]) )
366 return $this->error_data[$code];
370 function add($code, $message, $data = '') {
371 $this->errors[$code][] = $message;
372 if ( ! empty($data) )
373 $this->error_data[$code] = $data;
376 function add_data($data, $code = '') {
378 $code = $this->get_error_code();
380 $this->error_data[$code] = $data;
384 function is_wp_error($thing) {
385 if ( is_object($thing) && is_a($thing, 'WP_Error') )
391 // A class for displaying various tree-like structures. Extend the Walker class to use it, see examples at the bottom
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; }
403 function walk($elements, $to_depth) {
404 $args = array_slice(func_get_args(), 2); $parents = array(); $depth = 1; $previous_element = ''; $output = '';
407 $last_element->post_parent = 0;
408 $last_element->post_id = 0;
409 $elements[] = $last_element;
411 $id_field = $this->db_fields['id'];
412 $parent_field = $this->db_fields['parent'];
414 $flat = ($to_depth == -1) ? true : false;
416 foreach ( $elements as $element ) {
417 // If flat, start and end the element and skip the level checks.
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);
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);
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);
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);
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);
459 while ( $parent = array_shift($parents) ) {
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);
467 if ( $element->$parent_field == $parents[0]->$id_field ) {
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);
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);
487 $previous_element = $element;
494 class Walker_Page extends Walker {
495 var $tree_type = 'page';
496 var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID'); //TODO: decouple this
498 function start_lvl($output, $depth) {
499 $indent = str_repeat("\t", $depth);
500 $output .= "\n$indent<ul>\n";
504 function end_lvl($output, $depth) {
505 $indent = str_repeat("\t", $depth);
506 $output .= "$indent</ul>\n";
510 function start_el($output, $page, $depth, $current_page, $args) {
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';
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>';
523 if ( !empty($show_date) ) {
524 if ( 'modified' == $show_date )
525 $time = $page->post_modified;
527 $time = $page->post_date;
529 $output .= " " . mysql2date($date_format, $time);
535 function end_el($output, $page, $depth) {
536 $output .= "</li>\n";
543 class Walker_PageDropdown extends Walker {
544 var $tree_type = 'page';
545 var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID'); //TODO: decouple this
547 function start_el($output, $page, $depth, $args) {
548 $pad = str_repeat(' ', $depth * 3);
550 $output .= "\t<option value=\"$page->ID\"";
551 if ( $page->ID == $args['selected'] )
552 $output .= ' selected="selected"';
554 $title = wp_specialchars($page->post_title);
555 $output .= "$pad$title";
556 $output .= "</option>\n";
562 class Walker_Category extends Walker {
563 var $tree_type = 'category';
564 var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this
566 function start_lvl($output, $depth, $args) {
567 if ( 'list' != $args['style'] )
570 $indent = str_repeat("\t", $depth);
571 $output .= "$indent<ul class='children'>\n";
575 function end_lvl($output, $depth, $args) {
576 if ( 'list' != $args['style'] )
579 $indent = str_repeat("\t", $depth);
580 $output .= "$indent</ul>\n";
584 function start_el($output, $category, $depth, $args) {
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) . '"';
593 $link .= 'title="' . attribute_escape( apply_filters( 'category_description', $category->description, $category )) . '"';
595 $link .= $cat_name . '</a>';
597 if ( (! empty($feed_image)) || (! empty($feed)) ) {
600 if ( empty($feed_image) )
603 $link .= '<a href="' . get_category_rss_link( 0, $category->term_id, $category->slug ) . '"';
606 $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
608 $title = ' title="' . $feed . '"';
609 $alt = ' alt="' . $feed . '"';
616 if ( empty($feed_image) )
619 $link .= "<img src='$feed_image'$alt$title" . ' />';
621 if ( empty($feed_image) )
625 if ( isset($show_count) && $show_count )
626 $link .= ' (' . intval($category->count) . ')';
628 if ( isset($show_date) && $show_date ) {
629 $link .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp);
632 if ( $current_category )
633 $_current_category = get_category( $current_category );
635 if ( 'list' == $args['style'] ) {
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";
645 $output .= "\t$link<br />\n";
651 function end_el($output, $page, $depth, $args) {
652 if ( 'list' != $args['style'] )
655 $output .= "</li>\n";
661 class Walker_CategoryDropdown extends Walker {
662 var $tree_type = 'category';
663 var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this
665 function start_el($output, $category, $depth, $args) {
666 $pad = str_repeat(' ', $depth * 3);
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"';
673 $output .= $pad.$cat_name;
674 if ( $args['show_count'] )
675 $output .= ' ('. $category->count .')';
676 if ( $args['show_last_update'] ) {
678 $output .= ' ' . gmdate($format, $category->last_update_timestamp);
680 $output .= "</option>\n";
686 class WP_Ajax_Response {
687 var $responses = array();
689 function WP_Ajax_Response( $args = '' ) {
694 // a WP_Error object can be passed in 'id' or 'data'
695 function add( $args = '' ) {
697 'what' => 'object', 'action' => false,
698 'id' => '0', 'old_id' => false,
699 'data' => '', 'supplemental' => array()
702 $r = wp_parse_args( $args, $defaults );
703 extract( $r, EXTR_SKIP );
705 if ( is_wp_error($id) ) {
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>";
715 $response = "<response_data><![CDATA[$data]]></response_data>";
718 if ( (array) $supplemental )
719 foreach ( $supplemental as $k => $v )
720 $s .= "<$k><![CDATA[$v]]></$k>";
722 if ( false === $action )
723 $action = $_POST['action'];
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'>" : '>' );
733 $this->responses[] = $x;
738 header('Content-Type: text/xml');
739 echo "<?xml version='1.0' standalone='yes'?><wp_ajax>";
740 foreach ( $this->responses as $response )