wp/wp-includes/class-wp.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
    38 	 * Query variables for setting up the WordPress Query Loop.
    38 	 * Query variables for setting up the WordPress Query Loop.
    39 	 *
    39 	 *
    40 	 * @since 2.0.0
    40 	 * @since 2.0.0
    41 	 * @var array
    41 	 * @var array
    42 	 */
    42 	 */
    43 	public $query_vars;
    43 	public $query_vars = array();
    44 
    44 
    45 	/**
    45 	/**
    46 	 * String parsed to set the query variables.
    46 	 * String parsed to set the query variables.
    47 	 *
    47 	 *
    48 	 * @since 2.0.0
    48 	 * @since 2.0.0
    49 	 * @var string
    49 	 * @var string
    50 	 */
    50 	 */
    51 	public $query_string;
    51 	public $query_string = '';
    52 
    52 
    53 	/**
    53 	/**
    54 	 * The request path, e.g. 2015/05/06.
    54 	 * The request path, e.g. 2015/05/06.
    55 	 *
    55 	 *
    56 	 * @since 2.0.0
    56 	 * @since 2.0.0
    57 	 * @var string
    57 	 * @var string
    58 	 */
    58 	 */
    59 	public $request;
    59 	public $request = '';
    60 
    60 
    61 	/**
    61 	/**
    62 	 * Rewrite rule the request matched.
    62 	 * Rewrite rule the request matched.
    63 	 *
    63 	 *
    64 	 * @since 2.0.0
    64 	 * @since 2.0.0
    65 	 * @var string
    65 	 * @var string
    66 	 */
    66 	 */
    67 	public $matched_rule;
    67 	public $matched_rule = '';
    68 
    68 
    69 	/**
    69 	/**
    70 	 * Rewrite query the request matched.
    70 	 * Rewrite query the request matched.
    71 	 *
    71 	 *
    72 	 * @since 2.0.0
    72 	 * @since 2.0.0
    73 	 * @var string
    73 	 * @var string
    74 	 */
    74 	 */
    75 	public $matched_query;
    75 	public $matched_query = '';
    76 
    76 
    77 	/**
    77 	/**
    78 	 * Whether already did the permalink.
    78 	 * Whether already did the permalink.
    79 	 *
    79 	 *
    80 	 * @since 2.0.0
    80 	 * @since 2.0.0
   123 	 *
   123 	 *
   124 	 * Sets up the query variables based on the request. There are also many
   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.
   125 	 * filters and actions that can be used to further manipulate the result.
   126 	 *
   126 	 *
   127 	 * @since 2.0.0
   127 	 * @since 2.0.0
       
   128 	 * @since 6.0.0 A return value was added.
   128 	 *
   129 	 *
   129 	 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
   130 	 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
   130 	 *
   131 	 *
   131 	 * @param array|string $extra_query_vars Set the extra query variables.
   132 	 * @param array|string $extra_query_vars Set the extra query variables.
       
   133 	 * @return bool Whether the request was parsed.
   132 	 */
   134 	 */
   133 	public function parse_request( $extra_query_vars = '' ) {
   135 	public function parse_request( $extra_query_vars = '' ) {
   134 		global $wp_rewrite;
   136 		global $wp_rewrite;
   135 
   137 
   136 		/**
   138 		/**
   137 		 * Filters whether to parse the request.
   139 		 * Filters whether to parse the request.
   138 		 *
   140 		 *
   139 		 * @since 3.5.0
   141 		 * @since 3.5.0
   140 		 *
   142 		 *
   141 		 * @param bool         $bool             Whether or not to parse the request. Default true.
   143 		 * @param bool         $bool             Whether or not to parse the request. Default true.
   142 		 * @param WP           $this             Current WordPress environment instance.
   144 		 * @param WP           $wp               Current WordPress environment instance.
   143 		 * @param array|string $extra_query_vars Extra passed query variables.
   145 		 * @param array|string $extra_query_vars Extra passed query variables.
   144 		 */
   146 		 */
   145 		if ( ! apply_filters( 'do_parse_request', true, $this, $extra_query_vars ) ) {
   147 		if ( ! apply_filters( 'do_parse_request', true, $this, $extra_query_vars ) ) {
   146 			return;
   148 			return false;
   147 		}
   149 		}
   148 
   150 
   149 		$this->query_vars     = array();
   151 		$this->query_vars     = array();
   150 		$post_type_query_vars = array();
   152 		$post_type_query_vars = array();
   151 
   153 
   168 			list( $pathinfo ) = explode( '?', $pathinfo );
   170 			list( $pathinfo ) = explode( '?', $pathinfo );
   169 			$pathinfo         = str_replace( '%', '%25', $pathinfo );
   171 			$pathinfo         = str_replace( '%', '%25', $pathinfo );
   170 
   172 
   171 			list( $req_uri ) = explode( '?', $_SERVER['REQUEST_URI'] );
   173 			list( $req_uri ) = explode( '?', $_SERVER['REQUEST_URI'] );
   172 			$self            = $_SERVER['PHP_SELF'];
   174 			$self            = $_SERVER['PHP_SELF'];
   173 			$home_path       = trim( parse_url( home_url(), PHP_URL_PATH ), '/' );
   175 
   174 			$home_path_regex = sprintf( '|^%s|i', preg_quote( $home_path, '|' ) );
   176 			$home_path       = parse_url( home_url(), PHP_URL_PATH );
       
   177 			$home_path_regex = '';
       
   178 			if ( is_string( $home_path ) && '' !== $home_path ) {
       
   179 				$home_path       = trim( $home_path, '/' );
       
   180 				$home_path_regex = sprintf( '|^%s|i', preg_quote( $home_path, '|' ) );
       
   181 			}
   175 
   182 
   176 			/*
   183 			/*
   177 			 * Trim path info from the end and the leading home path from the front.
   184 			 * 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.
   185 			 * For path info requests, this leaves us with the requesting filename, if any.
   179 			 * For 404 requests, this leaves us with the requested permalink.
   186 			 * For 404 requests, this leaves us with the requested permalink.
   180 			 */
   187 			 */
   181 			$req_uri  = str_replace( $pathinfo, '', $req_uri );
   188 			$req_uri  = str_replace( $pathinfo, '', $req_uri );
   182 			$req_uri  = trim( $req_uri, '/' );
   189 			$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, '/' );
   190 			$pathinfo = trim( $pathinfo, '/' );
   188 			$self     = trim( $self, '/' );
   191 			$self     = trim( $self, '/' );
   189 			$self     = preg_replace( $home_path_regex, '', $self );
   192 
   190 			$self     = trim( $self, '/' );
   193 			if ( ! empty( $home_path_regex ) ) {
       
   194 				$req_uri  = preg_replace( $home_path_regex, '', $req_uri );
       
   195 				$req_uri  = trim( $req_uri, '/' );
       
   196 				$pathinfo = preg_replace( $home_path_regex, '', $pathinfo );
       
   197 				$pathinfo = trim( $pathinfo, '/' );
       
   198 				$self     = preg_replace( $home_path_regex, '', $self );
       
   199 				$self     = trim( $self, '/' );
       
   200 			}
   191 
   201 
   192 			// The requested permalink is in $pathinfo for path info requests and
   202 			// The requested permalink is in $pathinfo for path info requests and
   193 			// $req_uri for other requests.
   203 			// $req_uri for other requests.
   194 			if ( ! empty( $pathinfo ) && ! preg_match( '|^.*' . $wp_rewrite->index . '$|', $pathinfo ) ) {
   204 			if ( ! empty( $pathinfo ) && ! preg_match( '|^.*' . $wp_rewrite->index . '$|', $pathinfo ) ) {
   195 				$requested_path = $pathinfo;
   205 				$requested_path = $pathinfo;
   242 						break;
   252 						break;
   243 					}
   253 					}
   244 				}
   254 				}
   245 			}
   255 			}
   246 
   256 
   247 			if ( isset( $this->matched_rule ) ) {
   257 			if ( ! empty( $this->matched_rule ) ) {
   248 				// Trim the query of everything up to the '?'.
   258 				// Trim the query of everything up to the '?'.
   249 				$query = preg_replace( '!^.+\?!', '', $query );
   259 				$query = preg_replace( '!^.+\?!', '', $query );
   250 
   260 
   251 				// Substitute the substring matches into the query.
   261 				// Substitute the substring matches into the query.
   252 				$query = addslashes( WP_MatchesMapRegex::apply( $query, $matches ) );
   262 				$query = addslashes( WP_MatchesMapRegex::apply( $query, $matches ) );
   381 		/**
   391 		/**
   382 		 * Fires once all query variables for the current request have been parsed.
   392 		 * Fires once all query variables for the current request have been parsed.
   383 		 *
   393 		 *
   384 		 * @since 2.1.0
   394 		 * @since 2.1.0
   385 		 *
   395 		 *
   386 		 * @param WP $this Current WordPress environment instance (passed by reference).
   396 		 * @param WP $wp Current WordPress environment instance (passed by reference).
   387 		 */
   397 		 */
   388 		do_action_ref_array( 'parse_request', array( &$this ) );
   398 		do_action_ref_array( 'parse_request', array( &$this ) );
       
   399 
       
   400 		return true;
   389 	}
   401 	}
   390 
   402 
   391 	/**
   403 	/**
   392 	 * Sends additional HTTP headers for caching, content type, etc.
   404 	 * Sends additional HTTP headers for caching, content type, etc.
   393 	 *
   405 	 *
   399 	 */
   411 	 */
   400 	public function send_headers() {
   412 	public function send_headers() {
   401 		$headers       = array();
   413 		$headers       = array();
   402 		$status        = null;
   414 		$status        = null;
   403 		$exit_required = false;
   415 		$exit_required = false;
       
   416 		$date_format   = 'D, d M Y H:i:s';
   404 
   417 
   405 		if ( is_user_logged_in() ) {
   418 		if ( is_user_logged_in() ) {
   406 			$headers = array_merge( $headers, wp_get_nocache_headers() );
   419 			$headers = array_merge( $headers, wp_get_nocache_headers() );
   407 		} elseif ( ! empty( $_GET['unapproved'] ) && ! empty( $_GET['moderation-hash'] ) ) {
   420 		} elseif ( ! empty( $_GET['unapproved'] ) && ! empty( $_GET['moderation-hash'] ) ) {
   408 			// Unmoderated comments are only visible for 10 minutes via the moderation hash.
   421 			// Unmoderated comments are only visible for 10 minutes via the moderation hash.
   409 			$expires = 10 * MINUTE_IN_SECONDS;
   422 			$expires = 10 * MINUTE_IN_SECONDS;
   410 
   423 
   411 			$headers['Expires']       = gmdate( 'D, d M Y H:i:s', time() + $expires );
   424 			$headers['Expires']       = gmdate( $date_format, time() + $expires );
   412 			$headers['Cache-Control'] = sprintf(
   425 			$headers['Cache-Control'] = sprintf(
   413 				'max-age=%d, must-revalidate',
   426 				'max-age=%d, must-revalidate',
   414 				$expires
   427 				$expires
   415 			);
   428 			);
   416 		}
   429 		}
   445 						|| ! empty( $this->query_vars['attachment'] )
   458 						|| ! empty( $this->query_vars['attachment'] )
   446 						|| ! empty( $this->query_vars['attachment_id'] )
   459 						|| ! empty( $this->query_vars['attachment_id'] )
   447 					)
   460 					)
   448 				)
   461 				)
   449 			) {
   462 			) {
   450 				$wp_last_modified = mysql2date( 'D, d M Y H:i:s', get_lastcommentmodified( 'GMT' ), false );
   463 				$wp_last_modified_post    = mysql2date( $date_format, get_lastpostmodified( 'GMT' ), false );
       
   464 				$wp_last_modified_comment = mysql2date( $date_format, get_lastcommentmodified( 'GMT' ), false );
       
   465 				if ( strtotime( $wp_last_modified_post ) > strtotime( $wp_last_modified_comment ) ) {
       
   466 					$wp_last_modified = $wp_last_modified_post;
       
   467 				} else {
       
   468 					$wp_last_modified = $wp_last_modified_comment;
       
   469 				}
   451 			} else {
   470 			} else {
   452 				$wp_last_modified = mysql2date( 'D, d M Y H:i:s', get_lastpostmodified( 'GMT' ), false );
   471 				$wp_last_modified = mysql2date( $date_format, get_lastpostmodified( 'GMT' ), false );
   453 			}
   472 			}
   454 
   473 
   455 			if ( ! $wp_last_modified ) {
   474 			if ( ! $wp_last_modified ) {
   456 				$wp_last_modified = gmdate( 'D, d M Y H:i:s' );
   475 				$wp_last_modified = gmdate( $date_format );
   457 			}
   476 			}
   458 
   477 
   459 			$wp_last_modified .= ' GMT';
   478 			$wp_last_modified .= ' GMT';
   460 
   479 
   461 			$wp_etag                  = '"' . md5( $wp_last_modified ) . '"';
   480 			$wp_etag                  = '"' . md5( $wp_last_modified ) . '"';
   520 		/**
   539 		/**
   521 		 * Fires once the requested HTTP headers for caching, content type, etc. have been sent.
   540 		 * Fires once the requested HTTP headers for caching, content type, etc. have been sent.
   522 		 *
   541 		 *
   523 		 * @since 2.1.0
   542 		 * @since 2.1.0
   524 		 *
   543 		 *
   525 		 * @param WP $this Current WordPress environment instance (passed by reference).
   544 		 * @param WP $wp Current WordPress environment instance (passed by reference).
   526 		 */
   545 		 */
   527 		do_action_ref_array( 'send_headers', array( &$this ) );
   546 		do_action_ref_array( 'send_headers', array( &$this ) );
   528 	}
   547 	}
   529 
   548 
   530 	/**
   549 	/**
   745 	 *
   764 	 *
   746 	 * @param string|array $query_args Passed to parse_request().
   765 	 * @param string|array $query_args Passed to parse_request().
   747 	 */
   766 	 */
   748 	public function main( $query_args = '' ) {
   767 	public function main( $query_args = '' ) {
   749 		$this->init();
   768 		$this->init();
   750 		$this->parse_request( $query_args );
   769 
       
   770 		$parsed = $this->parse_request( $query_args );
       
   771 
   751 		$this->send_headers();
   772 		$this->send_headers();
   752 		$this->query_posts();
   773 
   753 		$this->handle_404();
   774 		if ( $parsed ) {
   754 		$this->register_globals();
   775 			$this->query_posts();
       
   776 			$this->handle_404();
       
   777 			$this->register_globals();
       
   778 		}
   755 
   779 
   756 		/**
   780 		/**
   757 		 * Fires once the WordPress environment has been set up.
   781 		 * Fires once the WordPress environment has been set up.
   758 		 *
   782 		 *
   759 		 * @since 2.1.0
   783 		 * @since 2.1.0
   760 		 *
   784 		 *
   761 		 * @param WP $this Current WordPress environment instance (passed by reference).
   785 		 * @param WP $wp Current WordPress environment instance (passed by reference).
   762 		 */
   786 		 */
   763 		do_action_ref_array( 'wp', array( &$this ) );
   787 		do_action_ref_array( 'wp', array( &$this ) );
   764 	}
   788 	}
   765 }
   789 }