PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/wp-includes/class-wp.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 754 lines | 370 code | 90 blank | 294 comment | 123 complexity | 8e7416a8b662b969c78c337b5b2ca21b MD5 | raw file
  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. * 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. /*
  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 whitelist 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 whitelisted 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 ) ) {
  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 one minute via the moderation hash.
  359. $headers['Expires'] = gmdate( 'D, d M Y H:i:s', time() + MINUTE_IN_SECONDS );
  360. $headers['Cache-Control'] = 'max-age=60, must-revalidate';
  361. }
  362. if ( ! empty( $this->query_vars['error'] ) ) {
  363. $status = (int) $this->query_vars['error'];
  364. if ( 404 === $status ) {
  365. if ( ! is_user_logged_in() ) {
  366. $headers = array_merge( $headers, wp_get_nocache_headers() );
  367. }
  368. $headers['Content-Type'] = get_option( 'html_type' ) . '; charset=' . get_option( 'blog_charset' );
  369. } elseif ( in_array( $status, array( 403, 500, 502, 503 ) ) ) {
  370. $exit_required = true;
  371. }
  372. } elseif ( empty( $this->query_vars['feed'] ) ) {
  373. $headers['Content-Type'] = get_option( 'html_type' ) . '; charset=' . get_option( 'blog_charset' );
  374. } else {
  375. // Set the correct content type for feeds.
  376. $type = $this->query_vars['feed'];
  377. if ( 'feed' == $this->query_vars['feed'] ) {
  378. $type = get_default_feed();
  379. }
  380. $headers['Content-Type'] = feed_content_type( $type ) . '; charset=' . get_option( 'blog_charset' );
  381. // We're showing a feed, so WP is indeed the only thing that last changed.
  382. if ( ! empty( $this->query_vars['withcomments'] )
  383. || false !== strpos( $this->query_vars['feed'], 'comments-' )
  384. || ( empty( $this->query_vars['withoutcomments'] )
  385. && ( ! empty( $this->query_vars['p'] )
  386. || ! empty( $this->query_vars['name'] )
  387. || ! empty( $this->query_vars['page_id'] )
  388. || ! empty( $this->query_vars['pagename'] )
  389. || ! empty( $this->query_vars['attachment'] )
  390. || ! empty( $this->query_vars['attachment_id'] )
  391. )
  392. )
  393. ) {
  394. $wp_last_modified = mysql2date( 'D, d M Y H:i:s', get_lastcommentmodified( 'GMT' ), false );
  395. } else {
  396. $wp_last_modified = mysql2date( 'D, d M Y H:i:s', get_lastpostmodified( 'GMT' ), false );
  397. }
  398. if ( ! $wp_last_modified ) {
  399. $wp_last_modified = gmdate( 'D, d M Y H:i:s' );
  400. }
  401. $wp_last_modified .= ' GMT';
  402. $wp_etag = '"' . md5( $wp_last_modified ) . '"';
  403. $headers['Last-Modified'] = $wp_last_modified;
  404. $headers['ETag'] = $wp_etag;
  405. // Support for conditional GET.
  406. if ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ) {
  407. $client_etag = wp_unslash( $_SERVER['HTTP_IF_NONE_MATCH'] );
  408. } else {
  409. $client_etag = false;
  410. }
  411. $client_last_modified = empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ? '' : trim( $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
  412. // If string is empty, return 0. If not, attempt to parse into a timestamp.
  413. $client_modified_timestamp = $client_last_modified ? strtotime( $client_last_modified ) : 0;
  414. // Make a timestamp for our most recent modification..
  415. $wp_modified_timestamp = strtotime( $wp_last_modified );
  416. if ( ( $client_last_modified && $client_etag ) ?
  417. ( ( $client_modified_timestamp >= $wp_modified_timestamp ) && ( $client_etag == $wp_etag ) ) :
  418. ( ( $client_modified_timestamp >= $wp_modified_timestamp ) || ( $client_etag == $wp_etag ) ) ) {
  419. $status = 304;
  420. $exit_required = true;
  421. }
  422. }
  423. /**
  424. * Filters the HTTP headers before they're sent to the browser.
  425. *
  426. * @since 2.8.0
  427. *
  428. * @param string[] $headers Associative array of headers to be sent.
  429. * @param WP $this Current WordPress environment instance.
  430. */
  431. $headers = apply_filters( 'wp_headers', $headers, $this );
  432. if ( ! empty( $status ) ) {
  433. status_header( $status );
  434. }
  435. // If Last-Modified is set to false, it should not be sent (no-cache situation).
  436. if ( isset( $headers['Last-Modified'] ) && false === $headers['Last-Modified'] ) {
  437. unset( $headers['Last-Modified'] );
  438. if ( ! headers_sent() ) {
  439. header_remove( 'Last-Modified' );
  440. }
  441. }
  442. if ( ! headers_sent() ) {
  443. foreach ( (array) $headers as $name => $field_value ) {
  444. header( "{$name}: {$field_value}" );
  445. }
  446. }
  447. if ( $exit_required ) {
  448. exit();
  449. }
  450. /**
  451. * Fires once the requested HTTP headers for caching, content type, etc. have been sent.
  452. *
  453. * @since 2.1.0
  454. *
  455. * @param WP $this Current WordPress environment instance (passed by reference).
  456. */
  457. do_action_ref_array( 'send_headers', array( &$this ) );
  458. }
  459. /**
  460. * Sets the query string property based off of the query variable property.
  461. *
  462. * The {@see 'query_string'} filter is deprecated, but still works. Plugins should
  463. * use the {@see 'request'} filter instead.
  464. *
  465. * @since 2.0.0
  466. */
  467. public function build_query_string() {
  468. $this->query_string = '';
  469. foreach ( (array) array_keys( $this->query_vars ) as $wpvar ) {
  470. if ( '' != $this->query_vars[ $wpvar ] ) {
  471. $this->query_string .= ( strlen( $this->query_string ) < 1 ) ? '' : '&';
  472. if ( ! is_scalar( $this->query_vars[ $wpvar ] ) ) { // Discard non-scalars.
  473. continue;
  474. }
  475. $this->query_string .= $wpvar . '=' . rawurlencode( $this->query_vars[ $wpvar ] );
  476. }
  477. }
  478. if ( has_filter( 'query_string' ) ) { // Don't bother filtering and parsing if no plugins are hooked in.
  479. /**
  480. * Filters the query string before parsing.
  481. *
  482. * @since 1.5.0
  483. * @deprecated 2.1.0 Use {@see 'query_vars'} or {@see 'request'} filters instead.
  484. *
  485. * @param string $query_string The query string to modify.
  486. */
  487. $this->query_string = apply_filters_deprecated(
  488. 'query_string',
  489. array( $this->query_string ),
  490. '2.1.0',
  491. 'query_vars, request'
  492. );
  493. parse_str( $this->query_string, $this->query_vars );
  494. }
  495. }
  496. /**
  497. * Set up the WordPress Globals.
  498. *
  499. * The query_vars property will be extracted to the GLOBALS. So care should
  500. * be taken when naming global variables that might interfere with the
  501. * WordPress environment.
  502. *
  503. * @since 2.0.0
  504. *
  505. * @global WP_Query $wp_query WordPress Query object.
  506. * @global string $query_string Query string for the loop.
  507. * @global array $posts The found posts.
  508. * @global WP_Post|null $post The current post, if available.
  509. * @global string $request The SQL statement for the request.
  510. * @global int $more Only set, if single page or post.
  511. * @global int $single If single page or post. Only set, if single page or post.
  512. * @global WP_User $authordata Only set, if author archive.
  513. */
  514. public function register_globals() {
  515. global $wp_query;
  516. // Extract updated query vars back into global namespace.
  517. foreach ( (array) $wp_query->query_vars as $key => $value ) {
  518. $GLOBALS[ $key ] = $value;
  519. }
  520. $GLOBALS['query_string'] = $this->query_string;
  521. $GLOBALS['posts'] = & $wp_query->posts;
  522. $GLOBALS['post'] = isset( $wp_query->post ) ? $wp_query->post : null;
  523. $GLOBALS['request'] = $wp_query->request;
  524. if ( $wp_query->is_single() || $wp_query->is_page() ) {
  525. $GLOBALS['more'] = 1;
  526. $GLOBALS['single'] = 1;
  527. }
  528. if ( $wp_query->is_author() && isset( $wp_query->post ) ) {
  529. $GLOBALS['authordata'] = get_userdata( $wp_query->post->post_author );
  530. }
  531. }
  532. /**
  533. * Set up the current user.
  534. *
  535. * @since 2.0.0
  536. */
  537. public function init() {
  538. wp_get_current_user();
  539. }
  540. /**
  541. * Set up the Loop based on the query variables.
  542. *
  543. * @since 2.0.0
  544. *
  545. * @global WP_Query $wp_the_query WordPress Query object.
  546. */
  547. public function query_posts() {
  548. global $wp_the_query;
  549. $this->build_query_string();
  550. $wp_the_query->query( $this->query_vars );
  551. }
  552. /**
  553. * Set the Headers for 404, if nothing is found for requested URL.
  554. *
  555. * Issue a 404 if a request doesn't match any posts and doesn't match
  556. * any object (e.g. an existing-but-empty category, tag, author) and a 404 was not already
  557. * issued, and if the request was not a search or the homepage.
  558. *
  559. * Otherwise, issue a 200.
  560. *
  561. * This sets headers after posts have been queried. handle_404() really means "handle status."
  562. * By inspecting the result of querying posts, seemingly successful requests can be switched to
  563. * a 404 so that canonical redirection logic can kick in.
  564. *
  565. * @since 2.0.0
  566. *
  567. * @global WP_Query $wp_query WordPress Query object.
  568. */
  569. public function handle_404() {
  570. global $wp_query;
  571. /**
  572. * Filters whether to short-circuit default header status handling.
  573. *
  574. * Returning a non-false value from the filter will short-circuit the handling
  575. * and return early.
  576. *
  577. * @since 4.5.0
  578. *
  579. * @param bool $preempt Whether to short-circuit default header status handling. Default false.
  580. * @param WP_Query $wp_query WordPress Query object.
  581. */
  582. if ( false !== apply_filters( 'pre_handle_404', false, $wp_query ) ) {
  583. return;
  584. }
  585. // If we've already issued a 404, bail.
  586. if ( is_404() ) {
  587. return;
  588. }
  589. // Never 404 for the admin, robots, favicon, or if we found posts.
  590. if ( is_admin() || is_robots() || is_favicon() || $wp_query->posts ) {
  591. $success = true;
  592. if ( is_singular() ) {
  593. $p = false;
  594. if ( $wp_query->post instanceof WP_Post ) {
  595. $p = clone $wp_query->post;
  596. }
  597. // Only set X-Pingback for single posts that allow pings.
  598. if ( $p && pings_open( $p ) && ! headers_sent() ) {
  599. header( 'X-Pingback: ' . get_bloginfo( 'pingback_url', 'display' ) );
  600. }
  601. // Check for paged content that exceeds the max number of pages.
  602. $next = '<!--nextpage-->';
  603. if ( $p && false !== strpos( $p->post_content, $next ) && ! empty( $this->query_vars['page'] ) ) {
  604. $page = trim( $this->query_vars['page'], '/' );
  605. $success = (int) $page <= ( substr_count( $p->post_content, $next ) + 1 );
  606. }
  607. }
  608. if ( $success ) {
  609. status_header( 200 );
  610. return;
  611. }
  612. }
  613. // We will 404 for paged queries, as no posts were found.
  614. if ( ! is_paged() ) {
  615. // Don't 404 for authors without posts as long as they matched an author on this site.
  616. $author = get_query_var( 'author' );
  617. if ( is_author() && is_numeric( $author ) && $author > 0 && is_user_member_of_blog( $author ) ) {
  618. status_header( 200 );
  619. return;
  620. }
  621. // Don't 404 for these queries if they matched an object.
  622. if ( ( is_tag() || is_category() || is_tax() || is_post_type_archive() ) && get_queried_object() ) {
  623. status_header( 200 );
  624. return;
  625. }
  626. // Don't 404 for these queries either.
  627. if ( is_home() || is_search() || is_feed() ) {
  628. status_header( 200 );
  629. return;
  630. }
  631. }
  632. // Guess it's time to 404.
  633. $wp_query->set_404();
  634. status_header( 404 );
  635. nocache_headers();
  636. }
  637. /**
  638. * Sets up all of the variables required by the WordPress environment.
  639. *
  640. * The action {@see 'wp'} has one parameter that references the WP object. It
  641. * allows for accessing the properties and methods to further manipulate the
  642. * object.
  643. *
  644. * @since 2.0.0
  645. *
  646. * @param string|array $query_args Passed to parse_request().
  647. */
  648. public function main( $query_args = '' ) {
  649. $this->init();
  650. $this->parse_request( $query_args );
  651. $this->send_headers();
  652. $this->query_posts();
  653. $this->handle_404();
  654. $this->register_globals();
  655. /**
  656. * Fires once the WordPress environment has been set up.
  657. *
  658. * @since 2.1.0
  659. *
  660. * @param WP $this Current WordPress environment instance (passed by reference).
  661. */
  662. do_action_ref_array( 'wp', array( &$this ) );
  663. }
  664. }