PageRenderTime 63ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/class-wp.php

https://bitbucket.org/space87/wordpressstart
PHP | 620 lines | 297 code | 78 blank | 245 comment | 92 complexity | 2c10551b00bce2f0b2d79e184dbf7074 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. * @access public
  16. * @var array
  17. */
  18. var $public_query_vars = array('m', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'tag', 'feed', 'author_name', 'static', 'pagename', 'page_id', 'error', 'comments_popup', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview', 'robots', 'taxonomy', 'term', 'cpage', 'post_type');
  19. /**
  20. * Private query variables.
  21. *
  22. * Long list of private query variables.
  23. *
  24. * @since 2.0.0
  25. * @var array
  26. */
  27. var $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');
  28. /**
  29. * Extra query variables set by the user.
  30. *
  31. * @since 2.1.0
  32. * @var array
  33. */
  34. var $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. var $query_vars;
  42. /**
  43. * String parsed to set the query variables.
  44. *
  45. * @since 2.0.0
  46. * @var string
  47. */
  48. var $query_string;
  49. /**
  50. * Permalink or requested URI.
  51. *
  52. * @since 2.0.0
  53. * @var string
  54. */
  55. var $request;
  56. /**
  57. * Rewrite rule the request matched.
  58. *
  59. * @since 2.0.0
  60. * @var string
  61. */
  62. var $matched_rule;
  63. /**
  64. * Rewrite query the request matched.
  65. *
  66. * @since 2.0.0
  67. * @var string
  68. */
  69. var $matched_query;
  70. /**
  71. * Whether already did the permalink.
  72. *
  73. * @since 2.0.0
  74. * @var bool
  75. */
  76. var $did_permalink = false;
  77. /**
  78. * Add name to list of public query variables.
  79. *
  80. * @since 2.1.0
  81. *
  82. * @param string $qv Query variable name.
  83. */
  84. function add_query_var($qv) {
  85. if ( !in_array($qv, $this->public_query_vars) )
  86. $this->public_query_vars[] = $qv;
  87. }
  88. /**
  89. * Set the value of a query variable.
  90. *
  91. * @since 2.3.0
  92. *
  93. * @param string $key Query variable name.
  94. * @param mixed $value Query variable value.
  95. */
  96. function set_query_var($key, $value) {
  97. $this->query_vars[$key] = $value;
  98. }
  99. /**
  100. * Parse request to find correct WordPress query.
  101. *
  102. * Sets up the query variables based on the request. There are also many
  103. * filters and actions that can be used to further manipulate the result.
  104. *
  105. * @since 2.0.0
  106. *
  107. * @param array|string $extra_query_vars Set the extra query variables.
  108. */
  109. function parse_request($extra_query_vars = '') {
  110. global $wp_rewrite;
  111. $this->query_vars = array();
  112. $post_type_query_vars = array();
  113. if ( is_array($extra_query_vars) )
  114. $this->extra_query_vars = & $extra_query_vars;
  115. else if (! empty($extra_query_vars))
  116. parse_str($extra_query_vars, $this->extra_query_vars);
  117. // Process PATH_INFO, REQUEST_URI, and 404 for permalinks.
  118. // Fetch the rewrite rules.
  119. $rewrite = $wp_rewrite->wp_rewrite_rules();
  120. if ( ! empty($rewrite) ) {
  121. // If we match a rewrite rule, this will be cleared.
  122. $error = '404';
  123. $this->did_permalink = true;
  124. if ( isset($_SERVER['PATH_INFO']) )
  125. $pathinfo = $_SERVER['PATH_INFO'];
  126. else
  127. $pathinfo = '';
  128. $pathinfo_array = explode('?', $pathinfo);
  129. $pathinfo = str_replace("%", "%25", $pathinfo_array[0]);
  130. $req_uri = $_SERVER['REQUEST_URI'];
  131. $req_uri_array = explode('?', $req_uri);
  132. $req_uri = $req_uri_array[0];
  133. $self = $_SERVER['PHP_SELF'];
  134. $home_path = parse_url(home_url());
  135. if ( isset($home_path['path']) )
  136. $home_path = $home_path['path'];
  137. else
  138. $home_path = '';
  139. $home_path = trim($home_path, '/');
  140. // Trim path info from the end and the leading home path from the
  141. // front. For path info requests, this leaves us with the requesting
  142. // filename, if any. For 404 requests, this leaves us with the
  143. // requested permalink.
  144. $req_uri = str_replace($pathinfo, '', $req_uri);
  145. $req_uri = trim($req_uri, '/');
  146. $req_uri = preg_replace("|^$home_path|", '', $req_uri);
  147. $req_uri = trim($req_uri, '/');
  148. $pathinfo = trim($pathinfo, '/');
  149. $pathinfo = preg_replace("|^$home_path|", '', $pathinfo);
  150. $pathinfo = trim($pathinfo, '/');
  151. $self = trim($self, '/');
  152. $self = preg_replace("|^$home_path|", '', $self);
  153. $self = trim($self, '/');
  154. // The requested permalink is in $pathinfo for path info requests and
  155. // $req_uri for other requests.
  156. if ( ! empty($pathinfo) && !preg_match('|^.*' . $wp_rewrite->index . '$|', $pathinfo) ) {
  157. $request = $pathinfo;
  158. } else {
  159. // If the request uri is the index, blank it out so that we don't try to match it against a rule.
  160. if ( $req_uri == $wp_rewrite->index )
  161. $req_uri = '';
  162. $request = $req_uri;
  163. }
  164. $this->request = $request;
  165. // Look for matches.
  166. $request_match = $request;
  167. if ( empty( $request_match ) ) {
  168. // An empty request could only match against ^$ regex
  169. if ( isset( $rewrite['$'] ) ) {
  170. $this->matched_rule = '$';
  171. $query = $rewrite['$'];
  172. $matches = array('');
  173. }
  174. } else if ( $req_uri != 'wp-app.php' ) {
  175. foreach ( (array) $rewrite as $match => $query ) {
  176. // If the requesting file is the anchor of the match, prepend it to the path info.
  177. if ( ! empty($req_uri) && strpos($match, $req_uri) === 0 && $req_uri != $request )
  178. $request_match = $req_uri . '/' . $request;
  179. if ( preg_match("#^$match#", $request_match, $matches) ||
  180. preg_match("#^$match#", urldecode($request_match), $matches) ) {
  181. if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) {
  182. // this is a verbose page match, lets check to be sure about it
  183. if ( ! get_page_by_path( $matches[ $varmatch[1] ] ) )
  184. continue;
  185. }
  186. // Got a match.
  187. $this->matched_rule = $match;
  188. break;
  189. }
  190. }
  191. }
  192. if ( isset( $this->matched_rule ) ) {
  193. // Trim the query of everything up to the '?'.
  194. $query = preg_replace("!^.+\?!", '', $query);
  195. // Substitute the substring matches into the query.
  196. $query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
  197. $this->matched_query = $query;
  198. // Parse the query.
  199. parse_str($query, $perma_query_vars);
  200. // If we're processing a 404 request, clear the error var
  201. // since we found something.
  202. unset( $_GET['error'] );
  203. unset( $error );
  204. }
  205. // If req_uri is empty or if it is a request for ourself, unset error.
  206. if ( empty($request) || $req_uri == $self || strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false ) {
  207. unset( $_GET['error'] );
  208. unset( $error );
  209. if ( isset($perma_query_vars) && strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false )
  210. unset( $perma_query_vars );
  211. $this->did_permalink = false;
  212. }
  213. }
  214. $this->public_query_vars = apply_filters('query_vars', $this->public_query_vars);
  215. foreach ( $GLOBALS['wp_post_types'] as $post_type => $t )
  216. if ( $t->query_var )
  217. $post_type_query_vars[$t->query_var] = $post_type;
  218. foreach ( $this->public_query_vars as $wpvar ) {
  219. if ( isset( $this->extra_query_vars[$wpvar] ) )
  220. $this->query_vars[$wpvar] = $this->extra_query_vars[$wpvar];
  221. elseif ( isset( $_POST[$wpvar] ) )
  222. $this->query_vars[$wpvar] = $_POST[$wpvar];
  223. elseif ( isset( $_GET[$wpvar] ) )
  224. $this->query_vars[$wpvar] = $_GET[$wpvar];
  225. elseif ( isset( $perma_query_vars[$wpvar] ) )
  226. $this->query_vars[$wpvar] = $perma_query_vars[$wpvar];
  227. if ( !empty( $this->query_vars[$wpvar] ) ) {
  228. if ( ! is_array( $this->query_vars[$wpvar] ) ) {
  229. $this->query_vars[$wpvar] = (string) $this->query_vars[$wpvar];
  230. } else {
  231. foreach ( $this->query_vars[$wpvar] as $vkey => $v ) {
  232. if ( !is_object( $v ) ) {
  233. $this->query_vars[$wpvar][$vkey] = (string) $v;
  234. }
  235. }
  236. }
  237. if ( isset($post_type_query_vars[$wpvar] ) ) {
  238. $this->query_vars['post_type'] = $post_type_query_vars[$wpvar];
  239. $this->query_vars['name'] = $this->query_vars[$wpvar];
  240. }
  241. }
  242. }
  243. // Convert urldecoded spaces back into +
  244. foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t )
  245. if ( $t->query_var && isset( $this->query_vars[$t->query_var] ) )
  246. $this->query_vars[$t->query_var] = str_replace( ' ', '+', $this->query_vars[$t->query_var] );
  247. // Limit publicly queried post_types to those that are publicly_queryable
  248. if ( isset( $this->query_vars['post_type']) ) {
  249. $queryable_post_types = get_post_types( array('publicly_queryable' => true) );
  250. if ( ! is_array( $this->query_vars['post_type'] ) ) {
  251. if ( ! in_array( $this->query_vars['post_type'], $queryable_post_types ) )
  252. unset( $this->query_vars['post_type'] );
  253. } else {
  254. $this->query_vars['post_type'] = array_intersect( $this->query_vars['post_type'], $queryable_post_types );
  255. }
  256. }
  257. foreach ( (array) $this->private_query_vars as $var) {
  258. if ( isset($this->extra_query_vars[$var]) )
  259. $this->query_vars[$var] = $this->extra_query_vars[$var];
  260. }
  261. if ( isset($error) )
  262. $this->query_vars['error'] = $error;
  263. $this->query_vars = apply_filters('request', $this->query_vars);
  264. do_action_ref_array('parse_request', array(&$this));
  265. }
  266. /**
  267. * Send additional HTTP headers for caching, content type, etc.
  268. *
  269. * Sets the X-Pingback header, 404 status (if 404), Content-type. If showing
  270. * a feed, it will also send last-modified, etag, and 304 status if needed.
  271. *
  272. * @since 2.0.0
  273. */
  274. function send_headers() {
  275. $headers = array('X-Pingback' => get_bloginfo('pingback_url'));
  276. $status = null;
  277. $exit_required = false;
  278. if ( is_user_logged_in() )
  279. $headers = array_merge($headers, wp_get_nocache_headers());
  280. if ( !empty($this->query_vars['error']) && '404' == $this->query_vars['error'] ) {
  281. $status = 404;
  282. if ( !is_user_logged_in() )
  283. $headers = array_merge($headers, wp_get_nocache_headers());
  284. $headers['Content-Type'] = get_option('html_type') . '; charset=' . get_option('blog_charset');
  285. } else if ( empty($this->query_vars['feed']) ) {
  286. $headers['Content-Type'] = get_option('html_type') . '; charset=' . get_option('blog_charset');
  287. } else {
  288. // We're showing a feed, so WP is indeed the only thing that last changed
  289. if ( !empty($this->query_vars['withcomments'])
  290. || ( empty($this->query_vars['withoutcomments'])
  291. && ( !empty($this->query_vars['p'])
  292. || !empty($this->query_vars['name'])
  293. || !empty($this->query_vars['page_id'])
  294. || !empty($this->query_vars['pagename'])
  295. || !empty($this->query_vars['attachment'])
  296. || !empty($this->query_vars['attachment_id'])
  297. )
  298. )
  299. )
  300. $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastcommentmodified('GMT'), 0).' GMT';
  301. else
  302. $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0).' GMT';
  303. $wp_etag = '"' . md5($wp_last_modified) . '"';
  304. $headers['Last-Modified'] = $wp_last_modified;
  305. $headers['ETag'] = $wp_etag;
  306. // Support for Conditional GET
  307. if (isset($_SERVER['HTTP_IF_NONE_MATCH']))
  308. $client_etag = stripslashes(stripslashes($_SERVER['HTTP_IF_NONE_MATCH']));
  309. else $client_etag = false;
  310. $client_last_modified = empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? '' : trim($_SERVER['HTTP_IF_MODIFIED_SINCE']);
  311. // If string is empty, return 0. If not, attempt to parse into a timestamp
  312. $client_modified_timestamp = $client_last_modified ? strtotime($client_last_modified) : 0;
  313. // Make a timestamp for our most recent modification...
  314. $wp_modified_timestamp = strtotime($wp_last_modified);
  315. if ( ($client_last_modified && $client_etag) ?
  316. (($client_modified_timestamp >= $wp_modified_timestamp) && ($client_etag == $wp_etag)) :
  317. (($client_modified_timestamp >= $wp_modified_timestamp) || ($client_etag == $wp_etag)) ) {
  318. $status = 304;
  319. $exit_required = true;
  320. }
  321. }
  322. $headers = apply_filters('wp_headers', $headers, $this);
  323. if ( ! empty( $status ) )
  324. status_header( $status );
  325. foreach( (array) $headers as $name => $field_value )
  326. @header("{$name}: {$field_value}");
  327. if ( $exit_required )
  328. exit();
  329. do_action_ref_array('send_headers', array(&$this));
  330. }
  331. /**
  332. * Sets the query string property based off of the query variable property.
  333. *
  334. * The 'query_string' filter is deprecated, but still works. Plugins should
  335. * use the 'request' filter instead.
  336. *
  337. * @since 2.0.0
  338. */
  339. function build_query_string() {
  340. $this->query_string = '';
  341. foreach ( (array) array_keys($this->query_vars) as $wpvar) {
  342. if ( '' != $this->query_vars[$wpvar] ) {
  343. $this->query_string .= (strlen($this->query_string) < 1) ? '' : '&';
  344. if ( !is_scalar($this->query_vars[$wpvar]) ) // Discard non-scalars.
  345. continue;
  346. $this->query_string .= $wpvar . '=' . rawurlencode($this->query_vars[$wpvar]);
  347. }
  348. }
  349. // query_string filter deprecated. Use request filter instead.
  350. if ( has_filter('query_string') ) { // Don't bother filtering and parsing if no plugins are hooked in.
  351. $this->query_string = apply_filters('query_string', $this->query_string);
  352. parse_str($this->query_string, $this->query_vars);
  353. }
  354. }
  355. /**
  356. * Set up the WordPress Globals.
  357. *
  358. * The query_vars property will be extracted to the GLOBALS. So care should
  359. * be taken when naming global variables that might interfere with the
  360. * WordPress environment.
  361. *
  362. * @global string $query_string Query string for the loop.
  363. * @global int $more Only set, if single page or post.
  364. * @global int $single If single page or post. Only set, if single page or post.
  365. *
  366. * @since 2.0.0
  367. */
  368. function register_globals() {
  369. global $wp_query;
  370. // Extract updated query vars back into global namespace.
  371. foreach ( (array) $wp_query->query_vars as $key => $value) {
  372. $GLOBALS[$key] = $value;
  373. }
  374. $GLOBALS['query_string'] = $this->query_string;
  375. $GLOBALS['posts'] = & $wp_query->posts;
  376. $GLOBALS['post'] = (isset($wp_query->post)) ? $wp_query->post : null;
  377. $GLOBALS['request'] = $wp_query->request;
  378. if ( is_single() || is_page() ) {
  379. $GLOBALS['more'] = 1;
  380. $GLOBALS['single'] = 1;
  381. }
  382. }
  383. /**
  384. * Set up the current user.
  385. *
  386. * @since 2.0.0
  387. */
  388. function init() {
  389. wp_get_current_user();
  390. }
  391. /**
  392. * Set up the Loop based on the query variables.
  393. *
  394. * @uses WP::$query_vars
  395. * @since 2.0.0
  396. */
  397. function query_posts() {
  398. global $wp_the_query;
  399. $this->build_query_string();
  400. $wp_the_query->query($this->query_vars);
  401. }
  402. /**
  403. * Set the Headers for 404, if nothing is found for requested URL.
  404. *
  405. * Issue a 404 if a request doesn't match any posts and doesn't match
  406. * any object (e.g. an existing-but-empty category, tag, author) and a 404 was not already
  407. * issued, and if the request was not a search or the homepage.
  408. *
  409. * Otherwise, issue a 200.
  410. *
  411. * @since 2.0.0
  412. */
  413. function handle_404() {
  414. global $wp_query;
  415. // If we've already issued a 404, bail.
  416. if ( is_404() )
  417. return;
  418. // Never 404 for the admin, robots, or if we found posts.
  419. if ( is_admin() || is_robots() || $wp_query->posts ) {
  420. status_header( 200 );
  421. return;
  422. }
  423. // We will 404 for paged queries, as no posts were found.
  424. if ( ! is_paged() ) {
  425. // Don't 404 for these queries if they matched an object.
  426. if ( ( is_tag() || is_category() || is_tax() || is_author() || is_post_type_archive() ) && $wp_query->get_queried_object() ) {
  427. status_header( 200 );
  428. return;
  429. }
  430. // Don't 404 for these queries either.
  431. if ( is_home() || is_search() ) {
  432. status_header( 200 );
  433. return;
  434. }
  435. }
  436. // Guess it's time to 404.
  437. $wp_query->set_404();
  438. status_header( 404 );
  439. nocache_headers();
  440. }
  441. /**
  442. * Sets up all of the variables required by the WordPress environment.
  443. *
  444. * The action 'wp' has one parameter that references the WP object. It
  445. * allows for accessing the properties and methods to further manipulate the
  446. * object.
  447. *
  448. * @since 2.0.0
  449. *
  450. * @param string|array $query_args Passed to {@link parse_request()}
  451. */
  452. function main($query_args = '') {
  453. $this->init();
  454. $this->parse_request($query_args);
  455. $this->send_headers();
  456. $this->query_posts();
  457. $this->handle_404();
  458. $this->register_globals();
  459. do_action_ref_array('wp', array(&$this));
  460. }
  461. }
  462. /**
  463. * Helper class to remove the need to use eval to replace $matches[] in query strings.
  464. *
  465. * @since 2.9.0
  466. */
  467. class WP_MatchesMapRegex {
  468. /**
  469. * store for matches
  470. *
  471. * @access private
  472. * @var array
  473. */
  474. var $_matches;
  475. /**
  476. * store for mapping result
  477. *
  478. * @access public
  479. * @var string
  480. */
  481. var $output;
  482. /**
  483. * subject to perform mapping on (query string containing $matches[] references
  484. *
  485. * @access private
  486. * @var string
  487. */
  488. var $_subject;
  489. /**
  490. * regexp pattern to match $matches[] references
  491. *
  492. * @var string
  493. */
  494. var $_pattern = '(\$matches\[[1-9]+[0-9]*\])'; // magic number
  495. /**
  496. * constructor
  497. *
  498. * @param string $subject subject if regex
  499. * @param array $matches data to use in map
  500. * @return self
  501. */
  502. function WP_MatchesMapRegex($subject, $matches) {
  503. $this->_subject = $subject;
  504. $this->_matches = $matches;
  505. $this->output = $this->_map();
  506. }
  507. /**
  508. * Substitute substring matches in subject.
  509. *
  510. * static helper function to ease use
  511. *
  512. * @access public
  513. * @param string $subject subject
  514. * @param array $matches data used for substitution
  515. * @return string
  516. */
  517. public static function apply($subject, $matches) {
  518. $oSelf = new WP_MatchesMapRegex($subject, $matches);
  519. return $oSelf->output;
  520. }
  521. /**
  522. * do the actual mapping
  523. *
  524. * @access private
  525. * @return string
  526. */
  527. function _map() {
  528. $callback = array(&$this, 'callback');
  529. return preg_replace_callback($this->_pattern, $callback, $this->_subject);
  530. }
  531. /**
  532. * preg_replace_callback hook
  533. *
  534. * @access public
  535. * @param array $matches preg_replace regexp matches
  536. * @return string
  537. */
  538. function callback($matches) {
  539. $index = intval(substr($matches[0], 9, -1));
  540. return ( isset( $this->_matches[$index] ) ? urlencode($this->_matches[$index]) : '' );
  541. }
  542. }