[ XREF Home ] [ Index ]

PHP Cross Reference of WordPress Trunk

Provided by Yoast

title

Body

[close]

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

   1  <?php
   2  /**
   3   * WordPress environment setup class.
   4   *
   5   * @package WordPress
   6   * @since 2.0.0
   7   */
   8  class WP {
   9      /**
  10       * Public query variables.
  11       *
  12       * Long list of public query variables.
  13       *
  14       * @since 2.0.0
  15       * @access public
  16       * @var array
  17       */
  18      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', 'cpage', 'post_type');
  19  
  20      /**
  21       * Private query variables.
  22       *
  23       * Long list of private query variables.
  24       *
  25       * @since 2.0.0
  26       * @var array
  27       */
  28      var $private_query_vars = array('offset', 'posts_per_page', 'posts_per_archive_page', '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', 'comments_per_page', 'post__in', 'post__not_in');
  29  
  30      /**
  31       * Extra query variables set by the user.
  32       *
  33       * @since 2.1.0
  34       * @var array
  35       */
  36      var $extra_query_vars = array();
  37  
  38      /**
  39       * Query variables for setting up the WordPress Query Loop.
  40       *
  41       * @since 2.0.0
  42       * @var array
  43       */
  44      var $query_vars;
  45  
  46      /**
  47       * String parsed to set the query variables.
  48       *
  49       * @since 2.0.0
  50       * @var string
  51       */
  52      var $query_string;
  53  
  54      /**
  55       * Permalink or requested URI.
  56       *
  57       * @since 2.0.0
  58       * @var string
  59       */
  60      var $request;
  61  
  62      /**
  63       * Rewrite rule the request matched.
  64       *
  65       * @since 2.0.0
  66       * @var string
  67       */
  68      var $matched_rule;
  69  
  70      /**
  71       * Rewrite query the request matched.
  72       *
  73       * @since 2.0.0
  74       * @var string
  75       */
  76      var $matched_query;
  77  
  78      /**
  79       * Whether already did the permalink.
  80       *
  81       * @since 2.0.0
  82       * @var bool
  83       */
  84      var $did_permalink = false;
  85  
  86      /**
  87       * Add name to list of public query variables.
  88       *
  89       * @since 2.1.0
  90       *
  91       * @param string $qv Query variable name.
  92       */
  93  	function add_query_var($qv) {
  94          if ( !in_array($qv, $this->public_query_vars) )
  95              $this->public_query_vars[] = $qv;
  96      }
  97  
  98      /**
  99       * Set the value of a query variable.
 100       *
 101       * @since 2.3.0
 102       *
 103       * @param string $key Query variable name.
 104       * @param mixed $value Query variable value.
 105       */
 106  	function set_query_var($key, $value) {
 107          $this->query_vars[$key] = $value;
 108      }
 109  
 110      /**
 111       * Parse request to find correct WordPress query.
 112       *
 113       * Sets up the query variables based on the request. There are also many
 114       * filters and actions that can be used to further manipulate the result.
 115       *
 116       * @since 2.0.0
 117       *
 118       * @param array|string $extra_query_vars Set the extra query variables.
 119       */
 120  	function parse_request($extra_query_vars = '') {
 121          global $wp_rewrite;
 122  
 123          $this->query_vars = array();
 124          $post_type_query_vars = array();
 125  
 126          if ( is_array($extra_query_vars) )
 127              $this->extra_query_vars = & $extra_query_vars;
 128          else if (! empty($extra_query_vars))
 129              parse_str($extra_query_vars, $this->extra_query_vars);
 130  
 131          // Process PATH_INFO, REQUEST_URI, and 404 for permalinks.
 132  
 133          // Fetch the rewrite rules.
 134          $rewrite = $wp_rewrite->wp_rewrite_rules();
 135  
 136          if ( ! empty($rewrite) ) {
 137              // If we match a rewrite rule, this will be cleared.
 138              $error = '404';
 139              $this->did_permalink = true;
 140  
 141              if ( isset($_SERVER['PATH_INFO']) )
 142                  $pathinfo = $_SERVER['PATH_INFO'];
 143              else
 144                  $pathinfo = '';
 145              $pathinfo_array = explode('?', $pathinfo);
 146              $pathinfo = str_replace("%", "%25", $pathinfo_array[0]);
 147              $req_uri = $_SERVER['REQUEST_URI'];
 148              $req_uri_array = explode('?', $req_uri);
 149              $req_uri = $req_uri_array[0];
 150              $self = $_SERVER['PHP_SELF'];
 151              $home_path = parse_url(home_url());
 152              if ( isset($home_path['path']) )
 153                  $home_path = $home_path['path'];
 154              else
 155                  $home_path = '';
 156              $home_path = trim($home_path, '/');
 157  
 158              // Trim path info from the end and the leading home path from the
 159              // front.  For path info requests, this leaves us with the requesting
 160              // filename, if any.  For 404 requests, this leaves us with the
 161              // requested permalink.
 162              $req_uri = str_replace($pathinfo, '', $req_uri);
 163              $req_uri = trim($req_uri, '/');
 164              $req_uri = preg_replace("|^$home_path|", '', $req_uri);
 165              $req_uri = trim($req_uri, '/');
 166              $pathinfo = trim($pathinfo, '/');
 167              $pathinfo = preg_replace("|^$home_path|", '', $pathinfo);
 168              $pathinfo = trim($pathinfo, '/');
 169              $self = trim($self, '/');
 170              $self = preg_replace("|^$home_path|", '', $self);
 171              $self = trim($self, '/');
 172  
 173              // The requested permalink is in $pathinfo for path info requests and
 174              //  $req_uri for other requests.
 175              if ( ! empty($pathinfo) && !preg_match('|^.*' . $wp_rewrite->index . '$|', $pathinfo) ) {
 176                  $request = $pathinfo;
 177              } else {
 178                  // If the request uri is the index, blank it out so that we don't try to match it against a rule.
 179                  if ( $req_uri == $wp_rewrite->index )
 180                      $req_uri = '';
 181                  $request = $req_uri;
 182              }
 183  
 184              $this->request = $request;
 185  
 186              // Look for matches.
 187              $request_match = $request;
 188              foreach ( (array) $rewrite as $match => $query) {
 189                  // Don't try to match against AtomPub calls
 190                  if ( $req_uri == 'wp-app.php' )
 191                      break;
 192  
 193                  // If the requesting file is the anchor of the match, prepend it
 194                  // to the path info.
 195                  if ( (! empty($req_uri)) && (strpos($match, $req_uri) === 0) && ($req_uri != $request) )
 196                      $request_match = $req_uri . '/' . $request;
 197  
 198                  if ( preg_match("#^$match#", $request_match, $matches) ||
 199                      preg_match("#^$match#", urldecode($request_match), $matches) ) {
 200                      // Got a match.
 201                      $this->matched_rule = $match;
 202  
 203                      // Trim the query of everything up to the '?'.
 204                      $query = preg_replace("!^.+\?!", '', $query);
 205  
 206                      // Substitute the substring matches into the query.
 207                      $query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
 208  
 209                      $this->matched_query = $query;
 210  
 211                      // Parse the query.
 212                      parse_str($query, $perma_query_vars);
 213  
 214                      // If we're processing a 404 request, clear the error var
 215                      // since we found something.
 216                      if ( isset($_GET['error']) )
 217                          unset($_GET['error']);
 218  
 219                      if ( isset($error) )
 220                          unset($error);
 221  
 222                      break;
 223                  }
 224              }
 225  
 226              // If req_uri is empty or if it is a request for ourself, unset error.
 227              if ( empty($request) || $req_uri == $self || strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false ) {
 228                  if ( isset($_GET['error']) )
 229                      unset($_GET['error']);
 230  
 231                  if ( isset($error) )
 232                      unset($error);
 233  
 234                  if ( isset($perma_query_vars) && strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false )
 235                      unset($perma_query_vars);
 236  
 237                  $this->did_permalink = false;
 238              }
 239          }
 240  
 241          $this->public_query_vars = apply_filters('query_vars', $this->public_query_vars);
 242  
 243          foreach ( $GLOBALS['wp_post_types'] as $post_type => $t )
 244              if ( $t->query_var )
 245                  $post_type_query_vars[$t->query_var] = $post_type;
 246  
 247          foreach ( $this->public_query_vars as $wpvar ) {
 248              if ( isset( $this->extra_query_vars[$wpvar] ) )
 249                  $this->query_vars[$wpvar] = $this->extra_query_vars[$wpvar];
 250              elseif ( isset( $_POST[$wpvar] ) )
 251                  $this->query_vars[$wpvar] = $_POST[$wpvar];
 252              elseif ( isset( $_GET[$wpvar] ) )
 253                  $this->query_vars[$wpvar] = $_GET[$wpvar];
 254              elseif ( isset( $perma_query_vars[$wpvar] ) )
 255                  $this->query_vars[$wpvar] = $perma_query_vars[$wpvar];
 256  
 257              if ( !empty( $this->query_vars[$wpvar] ) ) {
 258                  if ( ! is_array( $this->query_vars[$wpvar] ) ) {
 259                      $this->query_vars[$wpvar] = (string) $this->query_vars[$wpvar];
 260                  } else {
 261                      foreach ( $this->query_vars[$wpvar] as $vkey => $v ) {
 262                          if ( !is_object( $v ) ) {
 263                              $this->query_vars[$wpvar][$vkey] = (string) $v;
 264                          }
 265                      }
 266                  }
 267  
 268                  if ( isset($post_type_query_vars[$wpvar] ) ) {
 269                      $this->query_vars['post_type'] = $post_type_query_vars[$wpvar];
 270                      $this->query_vars['name'] = $this->query_vars[$wpvar];
 271                  }
 272              }
 273          }
 274  
 275          // Convert urldecoded spaces back into +
 276          foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t )
 277              if ( $t->query_var && isset( $this->query_vars[$t->query_var] ) )
 278                  $this->query_vars[$t->query_var] = str_replace( ' ', '+', $this->query_vars[$t->query_var] );
 279  
 280          // Limit publicly queried post_types to those that are publicly_queryable
 281          if ( isset( $this->query_vars['post_type']) ) {
 282              $queryable_post_types = get_post_types( array('publicly_queryable' => true) );
 283              if ( ! is_array( $this->query_vars['post_type'] ) ) {
 284                  if ( ! in_array( $this->query_vars['post_type'], $queryable_post_types ) )
 285                      unset( $this->query_vars['post_type'] );
 286              } else {
 287                  $this->query_vars['post_type'] = array_intersect( $this->query_vars['post_type'], $queryable_post_types );
 288              }
 289          }
 290  
 291          foreach ( (array) $this->private_query_vars as $var) {
 292              if ( isset($this->extra_query_vars[$var]) )
 293                  $this->query_vars[$var] = $this->extra_query_vars[$var];
 294          }
 295  
 296          if ( isset($error) )
 297              $this->query_vars['error'] = $error;
 298  
 299          $this->query_vars = apply_filters('request', $this->query_vars);
 300  
 301          do_action_ref_array('parse_request', array(&$this));
 302      }
 303  
 304      /**
 305       * Send additional HTTP headers for caching, content type, etc.
 306       *
 307       * Sets the X-Pingback header, 404 status (if 404), Content-type. If showing
 308       * a feed, it will also send last-modified, etag, and 304 status if needed.
 309       *
 310       * @since 2.0.0
 311       */
 312  	function send_headers() {
 313          $headers = array('X-Pingback' => get_bloginfo('pingback_url'));
 314          $status = null;
 315          $exit_required = false;
 316  
 317          if ( is_user_logged_in() )
 318              $headers = array_merge($headers, wp_get_nocache_headers());
 319          if ( !empty($this->query_vars['error']) && '404' == $this->query_vars['error'] ) {
 320              $status = 404;
 321              if ( !is_user_logged_in() )
 322                  $headers = array_merge($headers, wp_get_nocache_headers());
 323              $headers['Content-Type'] = get_option('html_type') . '; charset=' . get_option('blog_charset');
 324          } else if ( empty($this->query_vars['feed']) ) {
 325              $headers['Content-Type'] = get_option('html_type') . '; charset=' . get_option('blog_charset');
 326          } else {
 327              // We're showing a feed, so WP is indeed the only thing that last changed
 328              if ( !empty($this->query_vars['withcomments'])
 329                  || ( empty($this->query_vars['withoutcomments'])
 330                      && ( !empty($this->query_vars['p'])
 331                          || !empty($this->query_vars['name'])
 332                          || !empty($this->query_vars['page_id'])
 333                          || !empty($this->query_vars['pagename'])
 334                          || !empty($this->query_vars['attachment'])
 335                          || !empty($this->query_vars['attachment_id'])
 336                      )
 337                  )
 338              )
 339                  $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastcommentmodified('GMT'), 0).' GMT';
 340              else
 341                  $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0).' GMT';
 342              $wp_etag = '"' . md5($wp_last_modified) . '"';
 343              $headers['Last-Modified'] = $wp_last_modified;
 344              $headers['ETag'] = $wp_etag;
 345  
 346              // Support for Conditional GET
 347              if (isset($_SERVER['HTTP_IF_NONE_MATCH']))
 348                  $client_etag = stripslashes(stripslashes($_SERVER['HTTP_IF_NONE_MATCH']));
 349              else $client_etag = false;
 350  
 351              $client_last_modified = empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? '' : trim($_SERVER['HTTP_IF_MODIFIED_SINCE']);
 352              // If string is empty, return 0. If not, attempt to parse into a timestamp
 353              $client_modified_timestamp = $client_last_modified ? strtotime($client_last_modified) : 0;
 354  
 355              // Make a timestamp for our most recent modification...
 356              $wp_modified_timestamp = strtotime($wp_last_modified);
 357  
 358              if ( ($client_last_modified && $client_etag) ?
 359                       (($client_modified_timestamp >= $wp_modified_timestamp) && ($client_etag == $wp_etag)) :
 360                       (($client_modified_timestamp >= $wp_modified_timestamp) || ($client_etag == $wp_etag)) ) {
 361                  $status = 304;
 362                  $exit_required = true;
 363              }
 364          }
 365  
 366          $headers = apply_filters('wp_headers', $headers, $this);
 367  
 368          if ( ! empty( $status ) )
 369              status_header( $status );
 370          foreach( (array) $headers as $name => $field_value )
 371              @header("{$name}: {$field_value}");
 372  
 373          if ( $exit_required )
 374              exit();
 375  
 376          do_action_ref_array('send_headers', array(&$this));
 377      }
 378  
 379      /**
 380       * Sets the query string property based off of the query variable property.
 381       *
 382       * The 'query_string' filter is deprecated, but still works. Plugins should
 383       * use the 'request' filter instead.
 384       *
 385       * @since 2.0.0
 386       */
 387  	function build_query_string() {
 388          $this->query_string = '';
 389          foreach ( (array) array_keys($this->query_vars) as $wpvar) {
 390              if ( '' != $this->query_vars[$wpvar] ) {
 391                  $this->query_string .= (strlen($this->query_string) < 1) ? '' : '&';
 392                  if ( !is_scalar($this->query_vars[$wpvar]) ) // Discard non-scalars.
 393                      continue;
 394                  $this->query_string .= $wpvar . '=' . rawurlencode($this->query_vars[$wpvar]);
 395              }
 396          }
 397  
 398          // query_string filter deprecated.  Use request filter instead.
 399          if ( has_filter('query_string') ) {  // Don't bother filtering and parsing if no plugins are hooked in.
 400              $this->query_string = apply_filters('query_string', $this->query_string);
 401              parse_str($this->query_string, $this->query_vars);
 402          }
 403      }
 404  
 405      /**
 406       * Set up the WordPress Globals.
 407       *
 408       * The query_vars property will be extracted to the GLOBALS. So care should
 409       * be taken when naming global variables that might interfere with the
 410       * WordPress environment.
 411       *
 412       * @global string $query_string Query string for the loop.
 413       * @global int $more Only set, if single page or post.
 414       * @global int $single If single page or post. Only set, if single page or post.
 415       *
 416       * @since 2.0.0
 417       */
 418  	function register_globals() {
 419          global $wp_query;
 420          // Extract updated query vars back into global namespace.
 421          foreach ( (array) $wp_query->query_vars as $key => $value) {
 422              $GLOBALS[$key] = $value;
 423          }
 424  
 425          $GLOBALS['query_string'] = $this->query_string;
 426          $GLOBALS['posts'] = & $wp_query->posts;
 427          $GLOBALS['post'] = (isset($wp_query->post)) ? $wp_query->post : null;
 428          $GLOBALS['request'] = $wp_query->request;
 429  
 430          if ( is_single() || is_page() ) {
 431              $GLOBALS['more'] = 1;
 432              $GLOBALS['single'] = 1;
 433          }
 434      }
 435  
 436      /**
 437       * Set up the current user.
 438       *
 439       * @since 2.0.0
 440       */
 441  	function init() {
 442          wp_get_current_user();
 443      }
 444  
 445      /**
 446       * Set up the Loop based on the query variables.
 447       *
 448       * @uses WP::$query_vars
 449       * @since 2.0.0
 450       */
 451  	function query_posts() {
 452          global $wp_the_query;
 453          $this->build_query_string();
 454          $wp_the_query->query($this->query_vars);
 455       }
 456  
 457       /**
 458        * Set the Headers for 404, if nothing is found for requested URL.
 459       *
 460       * Issue a 404 if a request doesn't match any posts and doesn't match
 461       * any object (e.g. an existing-but-empty category, tag, author) and a 404 was not already
 462       * issued, and if the request was not a search or the homepage.
 463       *
 464       * Otherwise, issue a 200.
 465       *
 466       * @since 2.0.0
 467        */
 468  	function handle_404() {
 469          global $wp_query;
 470  
 471          if ( !is_admin() && ( 0 == count( $wp_query->posts ) ) && !is_404() && !is_robots() && !is_search() && !is_home() ) {
 472              // Don't 404 for these queries if they matched an object.
 473              if ( ( is_tag() || is_category() || is_tax() || is_author() ) && $wp_query->get_queried_object() && !is_paged() ) {
 474                  if ( !is_404() )
 475                      status_header( 200 );
 476                  return;
 477              }
 478              $wp_query->set_404();
 479              status_header( 404 );
 480              nocache_headers();
 481          } elseif ( !is_404() ) {
 482              status_header( 200 );
 483          }
 484      }
 485  
 486      /**
 487       * Sets up all of the variables required by the WordPress environment.
 488       *
 489       * The action 'wp' has one parameter that references the WP object. It
 490       * allows for accessing the properties and methods to further manipulate the
 491       * object.
 492       *
 493       * @since 2.0.0
 494       *
 495       * @param string|array $query_args Passed to {@link parse_request()}
 496       */
 497  	function main($query_args = '') {
 498          $this->init();
 499          $this->parse_request($query_args);
 500          $this->send_headers();
 501          $this->query_posts();
 502          $this->handle_404();
 503          $this->register_globals();
 504          do_action_ref_array('wp', array(&$this));
 505      }
 506  
 507  }
 508  
 509  /**
 510   * Helper class to remove the need to use eval to replace $matches[] in query strings.
 511   *
 512   * @since 2.9.0
 513   */
 514  class WP_MatchesMapRegex {
 515      /**
 516       * store for matches
 517       *
 518       * @access private
 519       * @var array
 520       */
 521      var $_matches;
 522  
 523      /**
 524       * store for mapping result
 525       *
 526       * @access public
 527       * @var string
 528       */
 529      var $output;
 530  
 531      /**
 532       * subject to perform mapping on (query string containing $matches[] references
 533       *
 534       * @access private
 535       * @var string
 536       */
 537      var $_subject;
 538  
 539      /**
 540       * regexp pattern to match $matches[] references
 541       *
 542       * @var string
 543       */
 544      var $_pattern = '(\$matches\[[1-9]+[0-9]*\])'; // magic number
 545  
 546      /**
 547       * constructor
 548       *
 549       * @param string $subject subject if regex
 550       * @param array  $matches data to use in map
 551       * @return self
 552       */
 553  	function WP_MatchesMapRegex($subject, $matches) {
 554          $this->_subject = $subject;
 555          $this->_matches = $matches;
 556          $this->output = $this->_map();
 557      }
 558  
 559      /**
 560       * Substitute substring matches in subject.
 561       *
 562       * static helper function to ease use
 563       *
 564       * @access public
 565       * @param string $subject subject
 566       * @param array  $matches data used for subsitution
 567       * @return string
 568       */
 569  	function apply($subject, $matches) {
 570          $oSelf =& new WP_MatchesMapRegex($subject, $matches);
 571          return $oSelf->output;
 572      }
 573  
 574      /**
 575       * do the actual mapping
 576       *
 577       * @access private
 578       * @return string
 579       */
 580  	function _map() {
 581          $callback = array(&$this, 'callback');
 582          return preg_replace_callback($this->_pattern, $callback, $this->_subject);
 583      }
 584  
 585      /**
 586       * preg_replace_callback hook
 587       *
 588       * @access public
 589       * @param  array $matches preg_replace regexp matches
 590       * @return string
 591       */
 592  	function callback($matches) {
 593          $index = intval(substr($matches[0], 9, -1));
 594          return ( isset( $this->_matches[$index] ) ? urlencode($this->_matches[$index]) : '' );
 595      }
 596  
 597  }
 598  
 599  ?>


Generated: Wed Jun 1 08:30:02 2011 Cross-referenced by PHPXref 0.7
Provided by Yoast and awesome WordPress Hosting