PageRenderTime 34ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/class-wp.php

https://github.com/muskmelon/Greemo
PHP | 599 lines | 283 code | 76 blank | 240 comment | 91 complexity | d397cf2251593fffba8b06c94f3d9f88 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', 'debug', '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. foreach ( (array) $rewrite as $match => $query) {
  168. // Don't try to match against AtomPub calls
  169. if ( $req_uri == 'wp-app.php' )
  170. break;
  171. // If the requesting file is the anchor of the match, prepend it
  172. // to the path info.
  173. if ( (! empty($req_uri)) && (strpos($match, $req_uri) === 0) && ($req_uri != $request) )
  174. $request_match = $req_uri . '/' . $request;
  175. if ( preg_match("#^$match#", $request_match, $matches) ||
  176. preg_match("#^$match#", urldecode($request_match), $matches) ) {
  177. // Got a match.
  178. $this->matched_rule = $match;
  179. // Trim the query of everything up to the '?'.
  180. $query = preg_replace("!^.+\?!", '', $query);
  181. // Substitute the substring matches into the query.
  182. $query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
  183. $this->matched_query = $query;
  184. // Parse the query.
  185. parse_str($query, $perma_query_vars);
  186. // If we're processing a 404 request, clear the error var
  187. // since we found something.
  188. if ( isset($_GET['error']) )
  189. unset($_GET['error']);
  190. if ( isset($error) )
  191. unset($error);
  192. break;
  193. }
  194. }
  195. // If req_uri is empty or if it is a request for ourself, unset error.
  196. if ( empty($request) || $req_uri == $self || strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false ) {
  197. if ( isset($_GET['error']) )
  198. unset($_GET['error']);
  199. if ( isset($error) )
  200. unset($error);
  201. if ( isset($perma_query_vars) && strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false )
  202. unset($perma_query_vars);
  203. $this->did_permalink = false;
  204. }
  205. }
  206. $this->public_query_vars = apply_filters('query_vars', $this->public_query_vars);
  207. foreach ( $GLOBALS['wp_post_types'] as $post_type => $t )
  208. if ( $t->query_var )
  209. $post_type_query_vars[$t->query_var] = $post_type;
  210. foreach ( $this->public_query_vars as $wpvar ) {
  211. if ( isset( $this->extra_query_vars[$wpvar] ) )
  212. $this->query_vars[$wpvar] = $this->extra_query_vars[$wpvar];
  213. elseif ( isset( $_POST[$wpvar] ) )
  214. $this->query_vars[$wpvar] = $_POST[$wpvar];
  215. elseif ( isset( $_GET[$wpvar] ) )
  216. $this->query_vars[$wpvar] = $_GET[$wpvar];
  217. elseif ( isset( $perma_query_vars[$wpvar] ) )
  218. $this->query_vars[$wpvar] = $perma_query_vars[$wpvar];
  219. if ( !empty( $this->query_vars[$wpvar] ) ) {
  220. if ( ! is_array( $this->query_vars[$wpvar] ) ) {
  221. $this->query_vars[$wpvar] = (string) $this->query_vars[$wpvar];
  222. } else {
  223. foreach ( $this->query_vars[$wpvar] as $vkey => $v ) {
  224. if ( !is_object( $v ) ) {
  225. $this->query_vars[$wpvar][$vkey] = (string) $v;
  226. }
  227. }
  228. }
  229. if ( isset($post_type_query_vars[$wpvar] ) ) {
  230. $this->query_vars['post_type'] = $post_type_query_vars[$wpvar];
  231. $this->query_vars['name'] = $this->query_vars[$wpvar];
  232. }
  233. }
  234. }
  235. // Convert urldecoded spaces back into +
  236. foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t )
  237. if ( $t->query_var && isset( $this->query_vars[$t->query_var] ) )
  238. $this->query_vars[$t->query_var] = str_replace( ' ', '+', $this->query_vars[$t->query_var] );
  239. // Limit publicly queried post_types to those that are publicly_queryable
  240. if ( isset( $this->query_vars['post_type']) ) {
  241. $queryable_post_types = get_post_types( array('publicly_queryable' => true) );
  242. if ( ! is_array( $this->query_vars['post_type'] ) ) {
  243. if ( ! in_array( $this->query_vars['post_type'], $queryable_post_types ) )
  244. unset( $this->query_vars['post_type'] );
  245. } else {
  246. $this->query_vars['post_type'] = array_intersect( $this->query_vars['post_type'], $queryable_post_types );
  247. }
  248. }
  249. foreach ( (array) $this->private_query_vars as $var) {
  250. if ( isset($this->extra_query_vars[$var]) )
  251. $this->query_vars[$var] = $this->extra_query_vars[$var];
  252. }
  253. if ( isset($error) )
  254. $this->query_vars['error'] = $error;
  255. $this->query_vars = apply_filters('request', $this->query_vars);
  256. do_action_ref_array('parse_request', array(&$this));
  257. }
  258. /**
  259. * Send additional HTTP headers for caching, content type, etc.
  260. *
  261. * Sets the X-Pingback header, 404 status (if 404), Content-type. If showing
  262. * a feed, it will also send last-modified, etag, and 304 status if needed.
  263. *
  264. * @since 2.0.0
  265. */
  266. function send_headers() {
  267. $headers = array('X-Pingback' => get_bloginfo('pingback_url'));
  268. $status = null;
  269. $exit_required = false;
  270. if ( is_user_logged_in() )
  271. $headers = array_merge($headers, wp_get_nocache_headers());
  272. if ( !empty($this->query_vars['error']) && '404' == $this->query_vars['error'] ) {
  273. $status = 404;
  274. if ( !is_user_logged_in() )
  275. $headers = array_merge($headers, wp_get_nocache_headers());
  276. $headers['Content-Type'] = get_option('html_type') . '; charset=' . get_option('blog_charset');
  277. } else if ( empty($this->query_vars['feed']) ) {
  278. $headers['Content-Type'] = get_option('html_type') . '; charset=' . get_option('blog_charset');
  279. } else {
  280. // We're showing a feed, so WP is indeed the only thing that last changed
  281. if ( !empty($this->query_vars['withcomments'])
  282. || ( empty($this->query_vars['withoutcomments'])
  283. && ( !empty($this->query_vars['p'])
  284. || !empty($this->query_vars['name'])
  285. || !empty($this->query_vars['page_id'])
  286. || !empty($this->query_vars['pagename'])
  287. || !empty($this->query_vars['attachment'])
  288. || !empty($this->query_vars['attachment_id'])
  289. )
  290. )
  291. )
  292. $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastcommentmodified('GMT'), 0).' GMT';
  293. else
  294. $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0).' GMT';
  295. $wp_etag = '"' . md5($wp_last_modified) . '"';
  296. $headers['Last-Modified'] = $wp_last_modified;
  297. $headers['ETag'] = $wp_etag;
  298. // Support for Conditional GET
  299. if (isset($_SERVER['HTTP_IF_NONE_MATCH']))
  300. $client_etag = stripslashes(stripslashes($_SERVER['HTTP_IF_NONE_MATCH']));
  301. else $client_etag = false;
  302. $client_last_modified = empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? '' : trim($_SERVER['HTTP_IF_MODIFIED_SINCE']);
  303. // If string is empty, return 0. If not, attempt to parse into a timestamp
  304. $client_modified_timestamp = $client_last_modified ? strtotime($client_last_modified) : 0;
  305. // Make a timestamp for our most recent modification...
  306. $wp_modified_timestamp = strtotime($wp_last_modified);
  307. if ( ($client_last_modified && $client_etag) ?
  308. (($client_modified_timestamp >= $wp_modified_timestamp) && ($client_etag == $wp_etag)) :
  309. (($client_modified_timestamp >= $wp_modified_timestamp) || ($client_etag == $wp_etag)) ) {
  310. $status = 304;
  311. $exit_required = true;
  312. }
  313. }
  314. $headers = apply_filters('wp_headers', $headers, $this);
  315. if ( ! empty( $status ) )
  316. status_header( $status );
  317. foreach( (array) $headers as $name => $field_value )
  318. @header("{$name}: {$field_value}");
  319. if ( $exit_required )
  320. exit();
  321. do_action_ref_array('send_headers', array(&$this));
  322. }
  323. /**
  324. * Sets the query string property based off of the query variable property.
  325. *
  326. * The 'query_string' filter is deprecated, but still works. Plugins should
  327. * use the 'request' filter instead.
  328. *
  329. * @since 2.0.0
  330. */
  331. function build_query_string() {
  332. $this->query_string = '';
  333. foreach ( (array) array_keys($this->query_vars) as $wpvar) {
  334. if ( '' != $this->query_vars[$wpvar] ) {
  335. $this->query_string .= (strlen($this->query_string) < 1) ? '' : '&';
  336. if ( !is_scalar($this->query_vars[$wpvar]) ) // Discard non-scalars.
  337. continue;
  338. $this->query_string .= $wpvar . '=' . rawurlencode($this->query_vars[$wpvar]);
  339. }
  340. }
  341. // query_string filter deprecated. Use request filter instead.
  342. if ( has_filter('query_string') ) { // Don't bother filtering and parsing if no plugins are hooked in.
  343. $this->query_string = apply_filters('query_string', $this->query_string);
  344. parse_str($this->query_string, $this->query_vars);
  345. }
  346. }
  347. /**
  348. * Set up the WordPress Globals.
  349. *
  350. * The query_vars property will be extracted to the GLOBALS. So care should
  351. * be taken when naming global variables that might interfere with the
  352. * WordPress environment.
  353. *
  354. * @global string $query_string Query string for the loop.
  355. * @global int $more Only set, if single page or post.
  356. * @global int $single If single page or post. Only set, if single page or post.
  357. *
  358. * @since 2.0.0
  359. */
  360. function register_globals() {
  361. global $wp_query;
  362. // Extract updated query vars back into global namespace.
  363. foreach ( (array) $wp_query->query_vars as $key => $value) {
  364. $GLOBALS[$key] = $value;
  365. }
  366. $GLOBALS['query_string'] = $this->query_string;
  367. $GLOBALS['posts'] = & $wp_query->posts;
  368. $GLOBALS['post'] = (isset($wp_query->post)) ? $wp_query->post : null;
  369. $GLOBALS['request'] = $wp_query->request;
  370. if ( is_single() || is_page() ) {
  371. $GLOBALS['more'] = 1;
  372. $GLOBALS['single'] = 1;
  373. }
  374. }
  375. /**
  376. * Set up the current user.
  377. *
  378. * @since 2.0.0
  379. */
  380. function init() {
  381. wp_get_current_user();
  382. }
  383. /**
  384. * Set up the Loop based on the query variables.
  385. *
  386. * @uses WP::$query_vars
  387. * @since 2.0.0
  388. */
  389. function query_posts() {
  390. global $wp_the_query;
  391. $this->build_query_string();
  392. $wp_the_query->query($this->query_vars);
  393. }
  394. /**
  395. * Set the Headers for 404, if nothing is found for requested URL.
  396. *
  397. * Issue a 404 if a request doesn't match any posts and doesn't match
  398. * any object (e.g. an existing-but-empty category, tag, author) and a 404 was not already
  399. * issued, and if the request was not a search or the homepage.
  400. *
  401. * Otherwise, issue a 200.
  402. *
  403. * @since 2.0.0
  404. */
  405. function handle_404() {
  406. global $wp_query;
  407. if ( !is_admin() && ( 0 == count( $wp_query->posts ) ) && !is_404() && !is_robots() && !is_search() && !is_home() ) {
  408. // Don't 404 for these queries if they matched an object.
  409. if ( ( is_tag() || is_category() || is_tax() || is_author() || is_post_type_archive() ) && $wp_query->get_queried_object() && !is_paged() ) {
  410. if ( !is_404() )
  411. status_header( 200 );
  412. return;
  413. }
  414. $wp_query->set_404();
  415. status_header( 404 );
  416. nocache_headers();
  417. } elseif ( !is_404() ) {
  418. status_header( 200 );
  419. }
  420. }
  421. /**
  422. * Sets up all of the variables required by the WordPress environment.
  423. *
  424. * The action 'wp' has one parameter that references the WP object. It
  425. * allows for accessing the properties and methods to further manipulate the
  426. * object.
  427. *
  428. * @since 2.0.0
  429. *
  430. * @param string|array $query_args Passed to {@link parse_request()}
  431. */
  432. function main($query_args = '') {
  433. $this->init();
  434. $this->parse_request($query_args);
  435. $this->send_headers();
  436. $this->query_posts();
  437. $this->handle_404();
  438. $this->register_globals();
  439. do_action_ref_array('wp', array(&$this));
  440. }
  441. }
  442. /**
  443. * Helper class to remove the need to use eval to replace $matches[] in query strings.
  444. *
  445. * @since 2.9.0
  446. */
  447. class WP_MatchesMapRegex {
  448. /**
  449. * store for matches
  450. *
  451. * @access private
  452. * @var array
  453. */
  454. var $_matches;
  455. /**
  456. * store for mapping result
  457. *
  458. * @access public
  459. * @var string
  460. */
  461. var $output;
  462. /**
  463. * subject to perform mapping on (query string containing $matches[] references
  464. *
  465. * @access private
  466. * @var string
  467. */
  468. var $_subject;
  469. /**
  470. * regexp pattern to match $matches[] references
  471. *
  472. * @var string
  473. */
  474. var $_pattern = '(\$matches\[[1-9]+[0-9]*\])'; // magic number
  475. /**
  476. * constructor
  477. *
  478. * @param string $subject subject if regex
  479. * @param array $matches data to use in map
  480. * @return self
  481. */
  482. function WP_MatchesMapRegex($subject, $matches) {
  483. $this->_subject = $subject;
  484. $this->_matches = $matches;
  485. $this->output = $this->_map();
  486. }
  487. /**
  488. * Substitute substring matches in subject.
  489. *
  490. * static helper function to ease use
  491. *
  492. * @access public
  493. * @param string $subject subject
  494. * @param array $matches data used for subsitution
  495. * @return string
  496. */
  497. function apply($subject, $matches) {
  498. $oSelf =& new WP_MatchesMapRegex($subject, $matches);
  499. return $oSelf->output;
  500. }
  501. /**
  502. * do the actual mapping
  503. *
  504. * @access private
  505. * @return string
  506. */
  507. function _map() {
  508. $callback = array(&$this, 'callback');
  509. return preg_replace_callback($this->_pattern, $callback, $this->_subject);
  510. }
  511. /**
  512. * preg_replace_callback hook
  513. *
  514. * @access public
  515. * @param array $matches preg_replace regexp matches
  516. * @return string
  517. */
  518. function callback($matches) {
  519. $index = intval(substr($matches[0], 9, -1));
  520. return ( isset( $this->_matches[$index] ) ? urlencode($this->_matches[$index]) : '' );
  521. }
  522. }
  523. ?>