class-wp.php 24 KB

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