You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

749 lines
24 KiB

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