class-wp.php 25 KB

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