PHP Cross Reference of WordPress Subversion HEAD

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]

title

Body

[close]

/wp-includes/ -> classes.php (source)

   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          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. Extend the Walker class to use it, see examples at the bottom
 391  
 392  class Walker {
 393      var $tree_type;
 394      var $db_fields;
 395  
 396      //abstract callbacks
 397  	function start_lvl($output) { return $output; }
 398  	function end_lvl($output)   { return $output; }
 399  	function start_el($output)  { return $output; }
 400  	function end_el($output)    { return $output; }
 401  
 402  	function walk($elements, $to_depth) {
 403          $args = array_slice(func_get_args(), 2); $parents = array(); $depth = 1; $previous_element = ''; $output = '';
 404  
 405          //padding at the end
 406          $last_element->post_parent = 0;
 407          $last_element->post_id = 0;
 408          $elements[] = $last_element;
 409  
 410          $id_field = $this->db_fields['id'];
 411          $parent_field = $this->db_fields['parent'];
 412  
 413          $flat = ($to_depth == -1) ? true : false;
 414  
 415          foreach ( $elements as $element ) {
 416              // If flat, start and end the element and skip the level checks.
 417              if ( $flat) {
 418                  // Start the element.
 419                  if ( isset($element->$id_field) && $element->$id_field != 0 ) {
 420                      $cb_args =