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


Generated: Mon Jun 21 01:00:04 2021 Cross-referenced by PHPXref 0.7.1