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


Generated: Sat Sep 25 01:00:04 2021 Cross-referenced by PHPXref 0.7.1