暫無描述

class-wp.php 25KB

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