PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/dkrzos/phc
PHP | 645 lines | 315 code | 82 blank | 248 comment | 97 complexity | e82f06d7c545551897795487eb26539e MD5 | raw file
Possible License(s): GPL-2.0
  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. if ( ! apply_filters( 'do_parse_request', true, $this, $extra_query_vars ) )
  112. return;
  113. $this->query_vars = array();
  114. $post_type_query_vars = array();
  115. if ( is_array($extra_query_vars) )
  116. $this->extra_query_vars = & $extra_query_vars;
  117. else if (! empty($extra_query_vars))
  118. parse_str($extra_query_vars, $this->extra_query_vars);
  119. // Process PATH_INFO, REQUEST_URI, and 404 for permalinks.
  120. // Fetch the rewrite rules.
  121. $rewrite = $wp_rewrite->wp_rewrite_rules();
  122. if ( ! empty($rewrite) ) {
  123. // If we match a rewrite rule, this will be cleared.
  124. $error = '404';
  125. $this->did_permalink = true;
  126. if ( isset($_SERVER['PATH_INFO']) )
  127. $pathinfo = $_SERVER['PATH_INFO'];
  128. else
  129. $pathinfo = '';
  130. $pathinfo_array = explode('?', $pathinfo);
  131. $pathinfo = str_replace("%", "%25", $pathinfo_array[0]);
  132. $req_uri = $_SERVER['REQUEST_URI'];
  133. $req_uri_array = explode('?', $req_uri);
  134. $req_uri = $req_uri_array[0];
  135. $self = $_SERVER['PHP_SELF'];
  136. $home_path = parse_url(home_url());
  137. if ( isset($home_path['path']) )
  138. $home_path = $home_path['path'];
  139. else
  140. $home_path = '';
  141. $home_path = trim($home_path, '/');
  142. // Trim path info from the end and the leading home path from the
  143. // front. For path info requests, this leaves us with the requesting
  144. // filename, if any. For 404 requests, this leaves us with the
  145. // requested permalink.
  146. $req_uri = str_replace($pathinfo, '', $req_uri);
  147. $req_uri = trim($req_uri, '/');
  148. $req_uri = preg_replace("|^$home_path|i", '', $req_uri);
  149. $req_uri = trim($req_uri, '/');
  150. $pathinfo = trim($pathinfo, '/');
  151. $pathinfo = preg_replace("|^$home_path|i", '', $pathinfo);
  152. $pathinfo = trim($pathinfo, '/');
  153. $self = trim($self, '/');
  154. $self = preg_replace("|^$home_path|i", '', $self);
  155. $self = trim($self, '/');
  156. // The requested permalink is in $pathinfo for path info requests and
  157. // $req_uri for other requests.
  158. if ( ! empty($pathinfo) && !preg_match('|^.*' . $wp_rewrite->index . '$|', $pathinfo) ) {
  159. $request = $pathinfo;
  160. } else {
  161. // If the request uri is the index, blank it out so that we don't try to match it against a rule.
  162. if ( $req_uri == $wp_rewrite->index )
  163. $req_uri = '';
  164. $request = $req_uri;
  165. }
  166. $this->request = $request;
  167. // Look for matches.
  168. $request_match = $request;
  169. if ( empty( $request_match ) ) {
  170. // An empty request could only match against ^$ regex
  171. if ( isset( $rewrite['$'] ) ) {
  172. $this->matched_rule = '$';
  173. $query = $rewrite['$'];
  174. $matches = array('');
  175. }
  176. } else {
  177. foreach ( (array) $rewrite as $match => $query ) {
  178. // If the requesting file is the anchor of the match, prepend it to the path info.
  179. if ( ! empty($req_uri) && strpos($match, $req_uri) === 0 && $req_uri != $request )
  180. $request_match = $req_uri . '/' . $request;
  181. if ( preg_match("#^$match#", $request_match, $matches) ||
  182. preg_match("#^$match#", urldecode($request_match), $matches) ) {
  183. if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) {
  184. // this is a verbose page match, lets check to be sure about it
  185. if ( ! get_page_by_path( $matches[ $varmatch[1] ] ) )
  186. continue;
  187. }
  188. // Got a match.
  189. $this->matched_rule = $match;
  190. break;
  191. }
  192. }
  193. }
  194. if ( isset( $this->matched_rule ) ) {
  195. // Trim the query of everything up to the '?'.
  196. $query = preg_replace("!^.+\?!", '', $query);
  197. // Substitute the substring matches into the query.
  198. $query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
  199. $this->matched_query = $query;
  200. // Parse the query.
  201. parse_str($query, $perma_query_vars);
  202. // If we're processing a 404 request, clear the error var since we found something.
  203. if ( '404' == $error )
  204. unset( $error, $_GET['error'] );
  205. }
  206. // If req_uri is empty or if it is a request for ourself, unset error.
  207. if ( empty($request) || $req_uri == $self || strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false ) {
  208. unset( $error, $_GET['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'] ) ) {
  281. $status = (int) $this->query_vars['error'];
  282. if ( 404 === $status ) {
  283. if ( ! is_user_logged_in() )
  284. $headers = array_merge($headers, wp_get_nocache_headers());
  285. $headers['Content-Type'] = get_option('html_type') . '; charset=' . get_option('blog_charset');
  286. } elseif ( in_array( $status, array( 403, 500, 502, 503 ) ) ) {
  287. $exit_required = true;
  288. }
  289. } else if ( empty($this->query_vars['feed']) ) {
  290. $headers['Content-Type'] = get_option('html_type') . '; charset=' . get_option('blog_charset');
  291. } else {
  292. // We're showing a feed, so WP is indeed the only thing that last changed
  293. if ( !empty($this->query_vars['withcomments'])
  294. || ( empty($this->query_vars['withoutcomments'])
  295. && ( !empty($this->query_vars['p'])
  296. || !empty($this->query_vars['name'])
  297. || !empty($this->query_vars['page_id'])
  298. || !empty($this->query_vars['pagename'])
  299. || !empty($this->query_vars['attachment'])
  300. || !empty($this->query_vars['attachment_id'])
  301. )
  302. )
  303. )
  304. $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastcommentmodified('GMT'), 0).' GMT';
  305. else
  306. $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0).' GMT';
  307. $wp_etag = '"' . md5($wp_last_modified) . '"';
  308. $headers['Last-Modified'] = $wp_last_modified;
  309. $headers['ETag'] = $wp_etag;
  310. // Support for Conditional GET
  311. if (isset($_SERVER['HTTP_IF_NONE_MATCH']))
  312. $client_etag = stripslashes(stripslashes($_SERVER['HTTP_IF_NONE_MATCH']));
  313. else $client_etag = false;
  314. $client_last_modified = empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? '' : trim($_SERVER['HTTP_IF_MODIFIED_SINCE']);
  315. // If string is empty, return 0. If not, attempt to parse into a timestamp
  316. $client_modified_timestamp = $client_last_modified ? strtotime($client_last_modified) : 0;
  317. // Make a timestamp for our most recent modification...
  318. $wp_modified_timestamp = strtotime($wp_last_modified);
  319. if ( ($client_last_modified && $client_etag) ?
  320. (($client_modified_timestamp >= $wp_modified_timestamp) && ($client_etag == $wp_etag)) :
  321. (($client_modified_timestamp >= $wp_modified_timestamp) || ($client_etag == $wp_etag)) ) {
  322. $status = 304;
  323. $exit_required = true;
  324. }
  325. }
  326. $headers = apply_filters('wp_headers', $headers, $this);
  327. if ( ! empty( $status ) )
  328. status_header( $status );
  329. // If Last-Modified is set to false, it should not be sent (no-cache situation).
  330. if ( isset( $headers['Last-Modified'] ) && false === $headers['Last-Modified'] ) {
  331. unset( $headers['Last-Modified'] );
  332. // In PHP 5.3+, make sure we are not sending a Last-Modified header.
  333. if ( function_exists( 'header_remove' ) ) {
  334. @header_remove( 'Last-Modified' );
  335. } else {
  336. // In PHP 5.2, send an empty Last-Modified header, but only as a
  337. // last resort to override a header already sent. #WP23021
  338. foreach ( headers_list() as $header ) {
  339. if ( 0 === stripos( $header, 'Last-Modified' ) ) {
  340. $headers['Last-Modified'] = '';
  341. break;
  342. }
  343. }
  344. }
  345. }
  346. foreach( (array) $headers as $name => $field_value )
  347. @header("{$name}: {$field_value}");
  348. if ( $exit_required )
  349. exit();
  350. do_action_ref_array('send_headers', array(&$this));
  351. }
  352. /**
  353. * Sets the query string property based off of the query variable property.
  354. *
  355. * The 'query_string' filter is deprecated, but still works. Plugins should
  356. * use the 'request' filter instead.
  357. *
  358. * @since 2.0.0
  359. */
  360. function build_query_string() {
  361. $this->query_string = '';
  362. foreach ( (array) array_keys($this->query_vars) as $wpvar) {
  363. if ( '' != $this->query_vars[$wpvar] ) {
  364. $this->query_string .= (strlen($this->query_string) < 1) ? '' : '&';
  365. if ( !is_scalar($this->query_vars[$wpvar]) ) // Discard non-scalars.
  366. continue;
  367. $this->query_string .= $wpvar . '=' . rawurlencode($this->query_vars[$wpvar]);
  368. }
  369. }
  370. // query_string filter deprecated. Use request filter instead.
  371. if ( has_filter('query_string') ) { // Don't bother filtering and parsing if no plugins are hooked in.
  372. $this->query_string = apply_filters('query_string', $this->query_string);
  373. parse_str($this->query_string, $this->query_vars);
  374. }
  375. }
  376. /**
  377. * Set up the WordPress Globals.
  378. *
  379. * The query_vars property will be extracted to the GLOBALS. So care should
  380. * be taken when naming global variables that might interfere with the
  381. * WordPress environment.
  382. *
  383. * @global string $query_string Query string for the loop.
  384. * @global int $more Only set, if single page or post.
  385. * @global int $single If single page or post. Only set, if single page or post.
  386. *
  387. * @since 2.0.0
  388. */
  389. function register_globals() {
  390. global $wp_query;
  391. // Extract updated query vars back into global namespace.
  392. foreach ( (array) $wp_query->query_vars as $key => $value) {
  393. $GLOBALS[$key] = $value;
  394. }
  395. $GLOBALS['query_string'] = $this->query_string;
  396. $GLOBALS['posts'] = & $wp_query->posts;
  397. $GLOBALS['post'] = (isset($wp_query->post)) ? $wp_query->post : null;
  398. $GLOBALS['request'] = $wp_query->request;
  399. if ( is_single() || is_page() ) {
  400. $GLOBALS['more'] = 1;
  401. $GLOBALS['single'] = 1;
  402. }
  403. }
  404. /**
  405. * Set up the current user.
  406. *
  407. * @since 2.0.0
  408. */
  409. function init() {
  410. wp_get_current_user();
  411. }
  412. /**
  413. * Set up the Loop based on the query variables.
  414. *
  415. * @uses WP::$query_vars
  416. * @since 2.0.0
  417. */
  418. function query_posts() {
  419. global $wp_the_query;
  420. $this->build_query_string();
  421. $wp_the_query->query($this->query_vars);
  422. }
  423. /**
  424. * Set the Headers for 404, if nothing is found for requested URL.
  425. *
  426. * Issue a 404 if a request doesn't match any posts and doesn't match
  427. * any object (e.g. an existing-but-empty category, tag, author) and a 404 was not already
  428. * issued, and if the request was not a search or the homepage.
  429. *
  430. * Otherwise, issue a 200.
  431. *
  432. * @since 2.0.0
  433. */
  434. function handle_404() {
  435. global $wp_query;
  436. // If we've already issued a 404, bail.
  437. if ( is_404() )
  438. return;
  439. // Never 404 for the admin, robots, or if we found posts.
  440. if ( is_admin() || is_robots() || $wp_query->posts ) {
  441. status_header( 200 );
  442. return;
  443. }
  444. // We will 404 for paged queries, as no posts were found.
  445. if ( ! is_paged() ) {
  446. // Don't 404 for these queries if they matched an object.
  447. if ( ( is_tag() || is_category() || is_tax() || is_author() || is_post_type_archive() ) && $wp_query->get_queried_object() ) {
  448. status_header( 200 );
  449. return;
  450. }
  451. // Don't 404 for these queries either.
  452. if ( is_home() || is_search() ) {
  453. status_header( 200 );
  454. return;
  455. }
  456. }
  457. // Guess it's time to 404.
  458. $wp_query->set_404();
  459. status_header( 404 );
  460. nocache_headers();
  461. }
  462. /**
  463. * Sets up all of the variables required by the WordPress environment.
  464. *
  465. * The action 'wp' has one parameter that references the WP object. It
  466. * allows for accessing the properties and methods to further manipulate the
  467. * object.
  468. *
  469. * @since 2.0.0
  470. *
  471. * @param string|array $query_args Passed to {@link parse_request()}
  472. */
  473. function main($query_args = '') {
  474. $this->init();
  475. $this->parse_request($query_args);
  476. $this->send_headers();
  477. $this->query_posts();
  478. $this->handle_404();
  479. $this->register_globals();
  480. do_action_ref_array('wp', array(&$this));
  481. }
  482. }
  483. /**
  484. * Helper class to remove the need to use eval to replace $matches[] in query strings.
  485. *
  486. * @since 2.9.0
  487. */
  488. class WP_MatchesMapRegex {
  489. /**
  490. * store for matches
  491. *
  492. * @access private
  493. * @var array
  494. */
  495. var $_matches;
  496. /**
  497. * store for mapping result
  498. *
  499. * @access public
  500. * @var string
  501. */
  502. var $output;
  503. /**
  504. * subject to perform mapping on (query string containing $matches[] references
  505. *
  506. * @access private
  507. * @var string
  508. */
  509. var $_subject;
  510. /**
  511. * regexp pattern to match $matches[] references
  512. *
  513. * @var string
  514. */
  515. var $_pattern = '(\$matches\[[1-9]+[0-9]*\])'; // magic number
  516. /**
  517. * constructor
  518. *
  519. * @param string $subject subject if regex
  520. * @param array $matches data to use in map
  521. * @return self
  522. */
  523. function WP_MatchesMapRegex($subject, $matches) {
  524. $this->_subject = $subject;
  525. $this->_matches = $matches;
  526. $this->output = $this->_map();
  527. }
  528. /**
  529. * Substitute substring matches in subject.
  530. *
  531. * static helper function to ease use
  532. *
  533. * @access public
  534. * @param string $subject subject
  535. * @param array $matches data used for substitution
  536. * @return string
  537. */
  538. public static function apply($subject, $matches) {
  539. $oSelf = new WP_MatchesMapRegex($subject, $matches);
  540. return $oSelf->output;
  541. }
  542. /**
  543. * do the actual mapping
  544. *
  545. * @access private
  546. * @return string
  547. */
  548. function _map() {
  549. $callback = array($this, 'callback');
  550. return preg_replace_callback($this->_pattern, $callback, $this->_subject);
  551. }
  552. /**
  553. * preg_replace_callback hook
  554. *
  555. * @access public
  556. * @param array $matches preg_replace regexp matches
  557. * @return string
  558. */
  559. function callback($matches) {
  560. $index = intval(substr($matches[0], 9, -1));
  561. return ( isset( $this->_matches[$index] ) ? urlencode($this->_matches[$index]) : '' );
  562. }
  563. }