[ Index ]

PHP Cross Reference of WordPress

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       * @var string[]
  16       */
  17      public $public_query_vars = array( 'm', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence', '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', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview', 'robots', 'taxonomy', 'term', 'cpage', 'post_type', 'embed' );
  18  
  19      /**
  20       * Private query variables.
  21       *
  22       * Long list of private query variables.
  23       *
  24       * @since 2.0.0
  25       * @var string[]
  26       */
  27      public $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', 'post_parent', 'post_parent__in', 'post_parent__not_in', 'title', 'fields' );
  28  
  29      /**
  30       * Extra query variables set by the user.
  31       *
  32       * @since 2.1.0
  33       * @var array
  34       */
  35      public $extra_query_vars = array();
  36  
  37      /**
  38       * Query variables for setting up the WordPress Query Loop.
  39       *
  40       * @since 2.0.0
  41       * @var array
  42       */
  43      public $query_vars;
  44  
  45      /**
  46       * String parsed to set the query variables.
  47       *
  48       * @since 2.0.0
  49       * @var string
  50       */
  51      public $query_string;
  52  
  53      /**
  54       * The request path, e.g. 2015/05/06.
  55       *
  56       * @since 2.0.0
  57       * @var string
  58       */
  59      public $request;
  60  
  61      /**
  62       * Rewrite rule the request matched.
  63       *
  64       * @since 2.0.0
  65       * @var string
  66       */
  67      public $matched_rule;
  68  
  69      /**
  70       * Rewrite query the request matched.
  71       *
  72       * @since 2.0.0
  73       * @var string
  74       */
  75      public $matched_query;
  76  
  77      /**
  78       * Whether already did the permalink.
  79       *
  80       * @since 2.0.0
  81       * @var bool
  82       */
  83      public $did_permalink = false;
  84  
  85      /**
  86       * Add name to list of public query variables.
  87       *
  88       * @since 2.1.0
  89       *
  90       * @param string $qv Query variable name.
  91       */
  92  	public function add_query_var( $qv ) {
  93          if ( ! in_array( $qv, $this->public_query_vars ) ) {
  94              $this->public_query_vars[] = $qv;
  95          }
  96      }
  97  
  98      /**
  99       * Removes a query variable from a list of public query variables.
 100       *
 101       * @since 4.5.0
 102       *
 103       * @param string $name Query variable name.
 104       */
 105  	public function remove_query_var( $name ) {
 106          $this->public_query_vars = array_diff( $this->public_query_vars, array( $name ) );
 107      }
 108  
 109      /**
 110       * Set the value of a query variable.
 111       *
 112       * @since 2.3.0
 113       *
 114       * @param string $key Query variable name.
 115       * @param mixed $value Query variable value.
 116       */
 117  	public function set_query_var( $key, $value ) {
 118          $this->query_vars[ $key ] = $value;
 119      }
 120  
 121      /**
 122       * Parse request to find correct WordPress query.
 123       *
 124       * Sets up the query variables based on the request. There are also many
 125       * filters and actions that can be used to further manipulate the result.
 126       *
 127       * @since 2.0.0
 128       *
 129       * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 130       *
 131       * @param array|string $extra_query_vars Set the extra query variables.
 132       */
 133  	public function parse_request( $extra_query_vars = '' ) {
 134          global $wp_rewrite;
 135  
 136          /**
 137           * Filters whether to parse the request.
 138           *
 139           * @since 3.5.0
 140           *
 141           * @param bool         $bool             Whether or not to parse the request. Default true.
 142           * @param WP           $this             Current WordPress environment instance.
 143           * @param array|string $extra_query_vars Extra passed query variables.
 144           */
 145          if ( ! apply_filters( 'do_parse_request', true, $this, $extra_query_vars ) ) {
 146              return;
 147          }
 148  
 149          $this->query_vars     = array();
 150          $post_type_query_vars = array();
 151  
 152          if ( is_array( $extra_query_vars ) ) {
 153              $this->extra_query_vars = & $extra_query_vars;
 154          } elseif ( ! empty( $extra_query_vars ) ) {
 155              parse_str( $extra_query_vars, $this->extra_query_vars );
 156          }
 157          // Process PATH_INFO, REQUEST_URI, and 404 for permalinks.
 158  
 159          // Fetch the rewrite rules.
 160          $rewrite = $wp_rewrite->wp_rewrite_rules();
 161  
 162          if ( ! empty( $rewrite ) ) {
 163              // If we match a rewrite rule, this will be cleared.
 164              $error               = '404';
 165              $this->did_permalink = true;
 166  
 167              $pathinfo         = isset( $_SERVER['PATH_INFO'] ) ? $_SERVER['PATH_INFO'] : '';
 168              list( $pathinfo ) = explode( '?', $pathinfo );
 169              $pathinfo         = str_replace( '%', '%25', $pathinfo );
 170  
 171              list( $req_uri ) = explode( '?', $_SERVER['REQUEST_URI'] );
 172              $self            = $_SERVER['PHP_SELF'];
 173              $home_path       = trim( parse_url( home_url(), PHP_URL_PATH ), '/' );
 174              $home_path_regex = sprintf( '|^%s|i', preg_quote( $home_path, '|' ) );
 175  
 176              // Trim path info from the end and the leading home path from the
 177              // front. For path info requests, this leaves us with the requesting
 178              // filename, if any. For 404 requests, this leaves us with the
 179              // requested permalink.
 180              $req_uri  = str_replace( $pathinfo, '', $req_uri );
 181              $req_uri  = trim( $req_uri, '/' );
 182              $req_uri  = preg_replace( $home_path_regex, '', $req_uri );
 183              $req_uri  = trim( $req_uri, '/' );
 184              $pathinfo = trim( $pathinfo, '/' );
 185              $pathinfo = preg_replace( $home_path_regex, '', $pathinfo );
 186              $pathinfo = trim( $pathinfo, '/' );
 187              $self     = trim( $self, '/' );
 188              $self     = preg_replace( $home_path_regex, '', $self );
 189              $self     = trim( $self, '/' );
 190  
 191              // The requested permalink is in $pathinfo for path info requests and
 192              //  $req_uri for other requests.
 193              if ( ! empty( $pathinfo ) && ! preg_match( '|^.*' . $wp_rewrite->index . '$|', $pathinfo ) ) {
 194                  $requested_path = $pathinfo;
 195              } else {
 196                  // If the request uri is the index, blank it out so that we don't try to match it against a rule.
 197                  if ( $req_uri == $wp_rewrite->index ) {
 198                      $req_uri = '';
 199                  }
 200                  $requested_path = $req_uri;
 201              }
 202              $requested_file = $req_uri;
 203  
 204              $this->request = $requested_path;
 205  
 206              // Look for matches.
 207              $request_match = $requested_path;
 208              if ( empty( $request_match ) ) {
 209                  // An empty request could only match against ^$ regex
 210                  if ( isset( $rewrite['$'] ) ) {
 211                      $this->matched_rule = '$';
 212                      $query              = $rewrite['$'];
 213                      $matches            = array( '' );
 214                  }
 215              } else {
 216                  foreach ( (array) $rewrite as $match => $query ) {
 217                      // If the requested file is the anchor of the match, prepend it to the path info.
 218                      if ( ! empty( $requested_file ) && strpos( $match, $requested_file ) === 0 && $requested_file != $requested_path ) {
 219                          $request_match = $requested_file . '/' . $requested_path;
 220                      }
 221  
 222                      if ( preg_match( "#^$match#", $request_match, $matches ) ||
 223                          preg_match( "#^$match#", urldecode( $request_match ), $matches ) ) {
 224  
 225                          if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) {
 226                              // This is a verbose page match, let's check to be sure about it.
 227                              $page = get_page_by_path( $matches[ $varmatch[1] ] );
 228                              if ( ! $page ) {
 229                                  continue;
 230                              }
 231  
 232                              $post_status_obj = get_post_status_object( $page->post_status );
 233                              if ( ! $post_status_obj->public && ! $post_status_obj->protected
 234                                  && ! $post_status_obj->private && $post_status_obj->exclude_from_search ) {
 235                                  continue;
 236                              }
 237                          }
 238  
 239                          // Got a match.
 240                          $this->matched_rule = $match;
 241                          break;
 242                      }
 243                  }
 244              }
 245  
 246              if ( isset( $this->matched_rule ) ) {
 247                  // Trim the query of everything up to the '?'.
 248                  $query = preg_replace( '!^.+\?!', '', $query );
 249  
 250                  // Substitute the substring matches into the query.
 251                  $query = addslashes( WP_MatchesMapRegex::apply( $query, $matches ) );
 252  
 253                  $this->matched_query = $query;
 254  
 255                  // Parse the query.
 256                  parse_str( $query, $perma_query_vars );
 257  
 258                  // If we're processing a 404 request, clear the error var since we found something.
 259                  if ( '404' == $error ) {
 260                      unset( $error, $_GET['error'] );
 261                  }
 262              }
 263  
 264              // If req_uri is empty or if it is a request for ourself, unset error.
 265              if ( empty( $requested_path ) || $requested_file == $self || strpos( $_SERVER['PHP_SELF'], 'wp-admin/' ) !== false ) {
 266                  unset( $error, $_GET['error'] );
 267  
 268                  if ( isset( $perma_query_vars ) && strpos( $_SERVER['PHP_SELF'], 'wp-admin/' ) !== false ) {
 269                      unset( $perma_query_vars );
 270                  }
 271  
 272                  $this->did_permalink = false;
 273              }
 274          }
 275  
 276          /**
 277           * Filters the query variables whitelist before processing.
 278           *
 279           * Allows (publicly allowed) query vars to be added, removed, or changed prior
 280           * to executing the query. Needed to allow custom rewrite rules using your own arguments
 281           * to work, or any other custom query variables you want to be publicly available.
 282           *
 283           * @since 1.5.0
 284           *
 285           * @param string[] $public_query_vars The array of whitelisted query variable names.
 286           */
 287          $this->public_query_vars = apply_filters( 'query_vars', $this->public_query_vars );
 288  
 289          foreach ( get_post_types( array(), 'objects' ) as $post_type => $t ) {
 290              if ( is_post_type_viewable( $t ) && $t->query_var ) {
 291                  $post_type_query_vars[ $t->query_var ] = $post_type;
 292              }
 293          }
 294  
 295          foreach ( $this->public_query_vars as $wpvar ) {
 296              if ( isset( $this->extra_query_vars[ $wpvar ] ) ) {
 297                  $this->query_vars[ $wpvar ] = $this->extra_query_vars[ $wpvar ];
 298              } elseif ( isset( $_GET[ $wpvar ] ) && isset( $_POST[ $wpvar ] ) && $_GET[ $wpvar ] !== $_POST[ $wpvar ] ) {
 299                  wp_die( __( 'A variable mismatch has been detected.' ), __( 'Sorry, you are not allowed to view this item.' ), 400 );
 300              } elseif ( isset( $_POST[ $wpvar ] ) ) {
 301                  $this->query_vars[ $wpvar ] = $_POST[ $wpvar ];
 302              } elseif ( isset( $_GET[ $wpvar ] ) ) {
 303                  $this->query_vars[ $wpvar ] = $_GET[ $wpvar ];
 304              } elseif ( isset( $perma_query_vars[ $wpvar ] ) ) {
 305                  $this->query_vars[ $wpvar ] = $perma_query_vars[ $wpvar ];
 306              }
 307  
 308              if ( ! empty( $this->query_vars[ $wpvar ] ) ) {
 309                  if ( ! is_array( $this->query_vars[ $wpvar ] ) ) {
 310                      $this->query_vars[ $wpvar ] = (string) $this->query_vars[ $wpvar ];
 311                  } else {
 312                      foreach ( $this->query_vars[ $wpvar ] as $vkey => $v ) {
 313                          if ( is_scalar( $v ) ) {
 314                              $this->query_vars[ $wpvar ][ $vkey ] = (string) $v;
 315                          }
 316                      }
 317                  }
 318  
 319                  if ( isset( $post_type_query_vars[ $wpvar ] ) ) {
 320                      $this->query_vars['post_type'] = $post_type_query_vars[ $wpvar ];
 321                      $this->query_vars['name']      = $this->query_vars[ $wpvar ];
 322                  }
 323              }
 324          }
 325  
 326          // Convert urldecoded spaces back into +
 327          foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy => $t ) {
 328              if ( $t->query_var && isset( $this->query_vars[ $t->query_var ] ) ) {
 329                  $this->query_vars[ $t->query_var ] = str_replace( ' ', '+', $this->query_vars[ $t->query_var ] );
 330              }
 331          }
 332  
 333          // Don't allow non-publicly queryable taxonomies to be queried from the front end.
 334          if ( ! is_admin() ) {
 335              foreach ( get_taxonomies( array( 'publicly_queryable' => false ), 'objects' ) as $taxonomy => $t ) {
 336                  /*
 337                   * Disallow when set to the 'taxonomy' query var.
 338                   * Non-publicly queryable taxonomies cannot register custom query vars. See register_taxonomy().
 339                   */
 340                  if ( isset( $this->query_vars['taxonomy'] ) && $taxonomy === $this->query_vars['taxonomy'] ) {
 341                      unset( $this->query_vars['taxonomy'], $this->query_vars['term'] );
 342                  }
 343              }
 344          }
 345  
 346          // Limit publicly queried post_types to those that are publicly_queryable
 347          if ( isset( $this->query_vars['post_type'] ) ) {
 348              $queryable_post_types = get_post_types( array( 'publicly_queryable' => true ) );
 349              if ( ! is_array( $this->query_vars['post_type'] ) ) {
 350                  if ( ! in_array( $this->query_vars['post_type'], $queryable_post_types ) ) {
 351                      unset( $this->query_vars['post_type'] );
 352                  }
 353              } else {
 354                  $this->query_vars['post_type'] = array_intersect( $this->query_vars['post_type'], $queryable_post_types );
 355              }
 356          }
 357  
 358          // Resolve conflicts between posts with numeric slugs and date archive queries.
 359          $this->query_vars = wp_resolve_numeric_slug_conflicts( $this->query_vars );
 360  
 361          foreach ( (array) $this->private_query_vars as $var ) {
 362              if ( isset( $this->extra_query_vars[ $var ] ) ) {
 363                  $this->query_vars[ $var ] = $this->extra_query_vars[ $var ];
 364              }
 365          }
 366  
 367          if ( isset( $error ) ) {
 368              $this->query_vars['error'] = $error;
 369          }
 370  
 371          /**
 372           * Filters the array of parsed query variables.
 373           *
 374           * @since 2.1.0
 375           *
 376           * @param array $query_vars The array of requested query variables.
 377           */
 378          $this->query_vars = apply_filters( 'request', $this->query_vars );
 379  
 380          /**
 381           * Fires once all query variables for the current request have been parsed.
 382           *
 383           * @since 2.1.0
 384           *
 385           * @param WP $this Current WordPress environment instance (passed by reference).
 386           */
 387          do_action_ref_array( 'parse_request', array( &$this ) );
 388      }
 389  
 390      /**
 391       * Sends additional HTTP headers for caching, content type, etc.
 392       *
 393       * Sets the Content-Type header. Sets the 'error' status (if passed) and optionally exits.
 394       * If showing a feed, it will also send Last-Modified, ETag, and 304 status if needed.
 395       *
 396       * @since 2.0.0
 397       * @since 4.4.0 `X-Pingback` header is added conditionally after posts have been queried in handle_404().
 398       */
 399  	public function send_headers() {
 400          $headers       = array();
 401          $status        = null;
 402          $exit_required = false;
 403  
 404          if ( is_user_logged_in() ) {
 405              $headers = array_merge( $headers, wp_get_nocache_headers() );
 406          }
 407          if ( ! empty( $this->query_vars['error'] ) ) {
 408              $status = (int) $this->query_vars['error'];
 409              if ( 404 === $status ) {
 410                  if ( ! is_user_logged_in() ) {
 411                      $headers = array_merge( $headers, wp_get_nocache_headers() );
 412                  }
 413                  $headers['Content-Type'] = get_option( 'html_type' ) . '; charset=' . get_option( 'blog_charset' );
 414              } elseif ( in_array( $status, array( 403, 500, 502, 503 ) ) ) {
 415                  $exit_required = true;
 416              }
 417          } elseif ( empty( $this->query_vars['feed'] ) ) {
 418              $headers['Content-Type'] = get_option( 'html_type' ) . '; charset=' . get_option( 'blog_charset' );
 419          } else {
 420              // Set the correct content type for feeds
 421              $type = $this->query_vars['feed'];
 422              if ( 'feed' == $this->query_vars['feed'] ) {
 423                  $type = get_default_feed();
 424              }
 425              $headers['Content-Type'] = feed_content_type( $type ) . '; charset=' . get_option( 'blog_charset' );
 426  
 427              // We're showing a feed, so WP is indeed the only thing that last changed.
 428              if ( ! empty( $this->query_vars['withcomments'] )
 429                  || false !== strpos( $this->query_vars['feed'], 'comments-' )
 430                  || ( empty( $this->query_vars['withoutcomments'] )
 431                      && ( ! empty( $this->query_vars['p'] )
 432                          || ! empty( $this->query_vars['name'] )
 433                          || ! empty( $this->query_vars['page_id'] )
 434                          || ! empty( $this->query_vars['pagename'] )
 435                          || ! empty( $this->query_vars['attachment'] )
 436                          || ! empty( $this->query_vars['attachment_id'] )
 437                      )
 438                  )
 439              ) {
 440                  $wp_last_modified = mysql2date( 'D, d M Y H:i:s', get_lastcommentmodified( 'GMT' ), false );
 441              } else {
 442                  $wp_last_modified = mysql2date( 'D, d M Y H:i:s', get_lastpostmodified( 'GMT' ), false );
 443              }
 444  
 445              if ( ! $wp_last_modified ) {
 446                  $wp_last_modified = gmdate( 'D, d M Y H:i:s' );
 447              }
 448  
 449              $wp_last_modified .= ' GMT';
 450  
 451              $wp_etag                  = '"' . md5( $wp_last_modified ) . '"';
 452              $headers['Last-Modified'] = $wp_last_modified;
 453              $headers['ETag']          = $wp_etag;
 454  
 455              // Support for Conditional GET
 456              if ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ) {
 457                  $client_etag = wp_unslash( $_SERVER['HTTP_IF_NONE_MATCH'] );
 458              } else {
 459                  $client_etag = false;
 460              }
 461  
 462              $client_last_modified = empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ? '' : trim( $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
 463              // If string is empty, return 0. If not, attempt to parse into a timestamp
 464              $client_modified_timestamp = $client_last_modified ? strtotime( $client_last_modified ) : 0;
 465  
 466              // Make a timestamp for our most recent modification...
 467              $wp_modified_timestamp = strtotime( $wp_last_modified );
 468  
 469              if ( ( $client_last_modified && $client_etag ) ?
 470                      ( ( $client_modified_timestamp >= $wp_modified_timestamp ) && ( $client_etag == $wp_etag ) ) :
 471                      ( ( $client_modified_timestamp >= $wp_modified_timestamp ) || ( $client_etag == $wp_etag ) ) ) {
 472                  $status        = 304;
 473                  $exit_required = true;
 474              }
 475          }
 476  
 477          /**
 478           * Filters the HTTP headers before they're sent to the browser.
 479           *
 480           * @since 2.8.0
 481           *
 482           * @param string[] $headers Associative array of headers to be sent.
 483           * @param WP       $this    Current WordPress environment instance.
 484           */
 485          $headers = apply_filters( 'wp_headers', $headers, $this );
 486  
 487          if ( ! empty( $status ) ) {
 488              status_header( $status );
 489          }
 490  
 491          // If Last-Modified is set to false, it should not be sent (no-cache situation).
 492          if ( isset( $headers['Last-Modified'] ) && false === $headers['Last-Modified'] ) {
 493              unset( $headers['Last-Modified'] );
 494  
 495              if ( ! headers_sent() ) {
 496                  header_remove( 'Last-Modified' );
 497              }
 498          }
 499  
 500          if ( ! headers_sent() ) {
 501              foreach ( (array) $headers as $name => $field_value ) {
 502                  header( "{$name}: {$field_value}" );
 503              }
 504          }
 505  
 506          if ( $exit_required ) {
 507              exit();
 508          }
 509  
 510          /**
 511           * Fires once the requested HTTP headers for caching, content type, etc. have been sent.
 512           *
 513           * @since 2.1.0
 514           *
 515           * @param WP $this Current WordPress environment instance (passed by reference).
 516           */
 517          do_action_ref_array( 'send_headers', array( &$this ) );
 518      }
 519  
 520      /**
 521       * Sets the query string property based off of the query variable property.
 522       *
 523       * The {@see 'query_string'} filter is deprecated, but still works. Plugins should
 524       * use the {@see 'request'} filter instead.
 525       *
 526       * @since 2.0.0
 527       */
 528  	public function build_query_string() {
 529          $this->query_string = '';
 530          foreach ( (array) array_keys( $this->query_vars ) as $wpvar ) {
 531              if ( '' != $this->query_vars[ $wpvar ] ) {
 532                  $this->query_string .= ( strlen( $this->query_string ) < 1 ) ? '' : '&';
 533                  if ( ! is_scalar( $this->query_vars[ $wpvar ] ) ) { // Discard non-scalars.
 534                      continue;
 535                  }
 536                  $this->query_string .= $wpvar . '=' . rawurlencode( $this->query_vars[ $wpvar ] );
 537              }
 538          }
 539  
 540          if ( has_filter( 'query_string' ) ) {  // Don't bother filtering and parsing if no plugins are hooked in.
 541              /**
 542               * Filters the query string before parsing.
 543               *
 544               * @since 1.5.0
 545               * @deprecated 2.1.0 Use 'query_vars' or 'request' filters instead.
 546               *
 547               * @param string $query_string The query string to modify.
 548               */
 549              $this->query_string = apply_filters( 'query_string', $this->query_string );
 550              parse_str( $this->query_string, $this->query_vars );
 551          }
 552      }
 553  
 554      /**
 555       * Set up the WordPress Globals.
 556       *
 557       * The query_vars property will be extracted to the GLOBALS. So care should
 558       * be taken when naming global variables that might interfere with the
 559       * WordPress environment.
 560       *
 561       * @since 2.0.0
 562       *
 563       * @global WP_Query     $wp_query     WordPress Query object.
 564       * @global string       $query_string Query string for the loop.
 565       * @global array        $posts        The found posts.
 566       * @global WP_Post|null $post         The current post, if available.
 567       * @global string       $request      The SQL statement for the request.
 568       * @global int          $more         Only set, if single page or post.
 569       * @global int          $single       If single page or post. Only set, if single page or post.
 570       * @global WP_User      $authordata   Only set, if author archive.
 571       */
 572  	public function register_globals() {
 573          global $wp_query;
 574  
 575          // Extract updated query vars back into global namespace.
 576          foreach ( (array) $wp_query->query_vars as $key => $value ) {
 577              $GLOBALS[ $key ] = $value;
 578          }
 579  
 580          $GLOBALS['query_string'] = $this->query_string;
 581          $GLOBALS['posts']        = & $wp_query->posts;
 582          $GLOBALS['post']         = isset( $wp_query->post ) ? $wp_query->post : null;
 583          $GLOBALS['request']      = $wp_query->request;
 584  
 585          if ( $wp_query->is_single() || $wp_query->is_page() ) {
 586              $GLOBALS['more']   = 1;
 587              $GLOBALS['single'] = 1;
 588          }
 589  
 590          if ( $wp_query->is_author() && isset( $wp_query->post ) ) {
 591              $GLOBALS['authordata'] = get_userdata( $wp_query->post->post_author );
 592          }
 593      }
 594  
 595      /**
 596       * Set up the current user.
 597       *
 598       * @since 2.0.0
 599       */
 600  	public function init() {
 601          wp_get_current_user();
 602      }
 603  
 604      /**
 605       * Set up the Loop based on the query variables.
 606       *
 607       * @since 2.0.0
 608       *
 609       * @global WP_Query $wp_the_query WordPress Query object.
 610       */
 611  	public function query_posts() {
 612          global $wp_the_query;
 613          $this->build_query_string();
 614          $wp_the_query->query( $this->query_vars );
 615      }
 616  
 617      /**
 618       * Set the Headers for 404, if nothing is found for requested URL.
 619       *
 620       * Issue a 404 if a request doesn't match any posts and doesn't match
 621       * any object (e.g. an existing-but-empty category, tag, author) and a 404 was not already
 622       * issued, and if the request was not a search or the homepage.
 623       *
 624       * Otherwise, issue a 200.
 625       *
 626       * This sets headers after posts have been queried. handle_404() really means "handle status."
 627       * By inspecting the result of querying posts, seemingly successful requests can be switched to
 628       * a 404 so that canonical redirection logic can kick in.
 629       *
 630       * @since 2.0.0
 631       *
 632       * @global WP_Query $wp_query WordPress Query object.
 633       */
 634  	public function handle_404() {
 635          global $wp_query;
 636  
 637          /**
 638           * Filters whether to short-circuit default header status handling.
 639           *
 640           * Returning a non-false value from the filter will short-circuit the handling
 641           * and return early.
 642           *
 643           * @since 4.5.0
 644           *
 645           * @param bool     $preempt  Whether to short-circuit default header status handling. Default false.
 646           * @param WP_Query $wp_query WordPress Query object.
 647           */
 648          if ( false !== apply_filters( 'pre_handle_404', false, $wp_query ) ) {
 649              return;
 650          }
 651  
 652          // If we've already issued a 404, bail.
 653          if ( is_404() ) {
 654              return;
 655          }
 656  
 657          // Never 404 for the admin, robots, or if we found posts.
 658          if ( is_admin() || is_robots() || $wp_query->posts ) {
 659  
 660              $success = true;
 661              if ( is_singular() ) {
 662                  $p = false;
 663  
 664                  if ( $wp_query->post instanceof WP_Post ) {
 665                      $p = clone $wp_query->post;
 666                  }
 667  
 668                  // Only set X-Pingback for single posts that allow pings.
 669                  if ( $p && pings_open( $p ) && ! headers_sent() ) {
 670                      header( 'X-Pingback: ' . get_bloginfo( 'pingback_url', 'display' ) );
 671                  }
 672  
 673                  // check for paged content that exceeds the max number of pages
 674                  $next = '<!--nextpage-->';
 675                  if ( $p && false !== strpos( $p->post_content, $next ) && ! empty( $this->query_vars['page'] ) ) {
 676                      $page    = trim( $this->query_vars['page'], '/' );
 677                      $success = (int) $page <= ( substr_count( $p->post_content, $next ) + 1 );
 678                  }
 679              }
 680  
 681              if ( $success ) {
 682                  status_header( 200 );
 683                  return;
 684              }
 685          }
 686  
 687          // We will 404 for paged queries, as no posts were found.
 688          if ( ! is_paged() ) {
 689  
 690              // Don't 404 for authors without posts as long as they matched an author on this site.
 691              $author = get_query_var( 'author' );
 692              if ( is_author() && is_numeric( $author ) && $author > 0 && is_user_member_of_blog( $author ) ) {
 693                  status_header( 200 );
 694                  return;
 695              }
 696  
 697              // Don't 404 for these queries if they matched an object.
 698              if ( ( is_tag() || is_category() || is_tax() || is_post_type_archive() ) && get_queried_object() ) {
 699                  status_header( 200 );
 700                  return;
 701              }
 702  
 703              // Don't 404 for these queries either.
 704              if ( is_home() || is_search() || is_feed() ) {
 705                  status_header( 200 );
 706                  return;
 707              }
 708          }
 709  
 710          // Guess it's time to 404.
 711          $wp_query->set_404();
 712          status_header( 404 );
 713          nocache_headers();
 714      }
 715  
 716      /**
 717       * Sets up all of the variables required by the WordPress environment.
 718       *
 719       * The action {@see 'wp'} has one parameter that references the WP object. It
 720       * allows for accessing the properties and methods to further manipulate the
 721       * object.
 722       *
 723       * @since 2.0.0
 724       *
 725       * @param string|array $query_args Passed to parse_request().
 726       */
 727  	public function main( $query_args = '' ) {
 728          $this->init();
 729          $this->parse_request( $query_args );
 730          $this->send_headers();
 731          $this->query_posts();
 732          $this->handle_404();
 733          $this->register_globals();
 734  
 735          /**
 736           * Fires once the WordPress environment has been set up.
 737           *
 738           * @since 2.1.0
 739           *
 740           * @param WP $this Current WordPress environment instance (passed by reference).
 741           */
 742          do_action_ref_array( 'wp', array( &$this ) );
 743      }
 744  }


Generated: Tue Sep 17 01:00:03 2019 Cross-referenced by PHPXref 0.7.1