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