home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-includes / class-wp.php < prev    next >
Encoding:
PHP Script  |  2017-10-02  |  23.2 KB  |  729 lines

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