/tags/Doduo/WebKitSite/blog/wp-includes/query.php

https://github.com/weissms/owb-mirror · PHP · 1527 lines · 1188 code · 277 blank · 62 comment · 414 complexity · f9d66d9da89322091cf274f3dd7b6cfc MD5 · raw file

  1. <?php
  2. /*
  3. * The Big Query.
  4. */
  5. function get_query_var($var) {
  6. global $wp_query;
  7. return $wp_query->get($var);
  8. }
  9. function set_query_var($var, $value) {
  10. global $wp_query;
  11. return $wp_query->set($var, $value);
  12. }
  13. function &query_posts($query) {
  14. unset($GLOBALS['wp_query']);
  15. $GLOBALS['wp_query'] =& new WP_Query();
  16. return $GLOBALS['wp_query']->query($query);
  17. }
  18. function wp_reset_query() {
  19. unset($GLOBALS['wp_query']);
  20. $GLOBALS['wp_query'] =& $GLOBALS['wp_the_query'];
  21. }
  22. /*
  23. * Query type checks.
  24. */
  25. function is_admin () {
  26. if ( defined('WP_ADMIN') )
  27. return WP_ADMIN;
  28. return false;
  29. }
  30. function is_archive () {
  31. global $wp_query;
  32. return $wp_query->is_archive;
  33. }
  34. function is_attachment () {
  35. global $wp_query;
  36. return $wp_query->is_attachment;
  37. }
  38. function is_author ($author = '') {
  39. global $wp_query;
  40. if ( !$wp_query->is_author )
  41. return false;
  42. if ( empty($author) )
  43. return true;
  44. $author_obj = $wp_query->get_queried_object();
  45. if ( $author == $author_obj->ID )
  46. return true;
  47. elseif ( $author == $author_obj->nickname )
  48. return true;
  49. elseif ( $author == $author_obj->user_nicename )
  50. return true;
  51. return false;
  52. }
  53. function is_category ($category = '') {
  54. global $wp_query;
  55. if ( !$wp_query->is_category )
  56. return false;
  57. if ( empty($category) )
  58. return true;
  59. $cat_obj = $wp_query->get_queried_object();
  60. if ( $category == $cat_obj->term_id )
  61. return true;
  62. else if ( $category == $cat_obj->name )
  63. return true;
  64. elseif ( $category == $cat_obj->slug )
  65. return true;
  66. return false;
  67. }
  68. function is_tag( $slug = '' ) {
  69. global $wp_query;
  70. if ( !$wp_query->is_tag )
  71. return false;
  72. if ( empty( $slug ) )
  73. return true;
  74. $tag_obj = $wp_query->get_queried_object();
  75. if ( $slug == $tag_obj->slug )
  76. return true;
  77. return false;
  78. }
  79. function is_comments_popup () {
  80. global $wp_query;
  81. return $wp_query->is_comments_popup;
  82. }
  83. function is_date () {
  84. global $wp_query;
  85. return $wp_query->is_date;
  86. }
  87. function is_day () {
  88. global $wp_query;
  89. return $wp_query->is_day;
  90. }
  91. function is_feed () {
  92. global $wp_query;
  93. return $wp_query->is_feed;
  94. }
  95. function is_home () {
  96. global $wp_query;
  97. return $wp_query->is_home;
  98. }
  99. function is_month () {
  100. global $wp_query;
  101. return $wp_query->is_month;
  102. }
  103. function is_page ($page = '') {
  104. global $wp_query;
  105. if ( !$wp_query->is_page )
  106. return false;
  107. if ( empty($page) )
  108. return true;
  109. $page_obj = $wp_query->get_queried_object();
  110. if ( $page == $page_obj->ID )
  111. return true;
  112. elseif ( $page == $page_obj->post_title )
  113. return true;
  114. else if ( $page == $page_obj->post_name )
  115. return true;
  116. return false;
  117. }
  118. function is_paged () {
  119. global $wp_query;
  120. return $wp_query->is_paged;
  121. }
  122. function is_plugin_page() {
  123. global $plugin_page;
  124. if ( isset($plugin_page) )
  125. return true;
  126. return false;
  127. }
  128. function is_preview() {
  129. global $wp_query;
  130. return $wp_query->is_preview;
  131. }
  132. function is_robots() {
  133. global $wp_query;
  134. return $wp_query->is_robots;
  135. }
  136. function is_search () {
  137. global $wp_query;
  138. return $wp_query->is_search;
  139. }
  140. function is_single ($post = '') {
  141. global $wp_query;
  142. if ( !$wp_query->is_single )
  143. return false;
  144. if ( empty( $post) )
  145. return true;
  146. $post_obj = $wp_query->get_queried_object();
  147. if ( $post == $post_obj->ID )
  148. return true;
  149. elseif ( $post == $post_obj->post_title )
  150. return true;
  151. elseif ( $post == $post_obj->post_name )
  152. return true;
  153. return false;
  154. }
  155. function is_singular() {
  156. global $wp_query;
  157. return $wp_query->is_singular;
  158. }
  159. function is_time () {
  160. global $wp_query;
  161. return $wp_query->is_time;
  162. }
  163. function is_trackback () {
  164. global $wp_query;
  165. return $wp_query->is_trackback;
  166. }
  167. function is_year () {
  168. global $wp_query;
  169. return $wp_query->is_year;
  170. }
  171. function is_404 () {
  172. global $wp_query;
  173. return $wp_query->is_404;
  174. }
  175. /*
  176. * The Loop. Post loop control.
  177. */
  178. function have_posts() {
  179. global $wp_query;
  180. return $wp_query->have_posts();
  181. }
  182. function in_the_loop() {
  183. global $wp_query;
  184. return $wp_query->in_the_loop;
  185. }
  186. function rewind_posts() {
  187. global $wp_query;
  188. return $wp_query->rewind_posts();
  189. }
  190. function the_post() {
  191. global $wp_query;
  192. $wp_query->the_post();
  193. }
  194. /*
  195. * Comments loop.
  196. */
  197. function have_comments() {
  198. global $wp_query;
  199. return $wp_query->have_comments();
  200. }
  201. function the_comment() {
  202. global $wp_query;
  203. return $wp_query->the_comment();
  204. }
  205. /*
  206. * WP_Query
  207. */
  208. class WP_Query {
  209. var $query;
  210. var $query_vars = array();
  211. var $queried_object;
  212. var $queried_object_id;
  213. var $request;
  214. var $posts;
  215. var $post_count = 0;
  216. var $current_post = -1;
  217. var $in_the_loop = false;
  218. var $post;
  219. var $comments;
  220. var $comment_count = 0;
  221. var $current_comment = -1;
  222. var $comment;
  223. var $found_posts = 0;
  224. var $max_num_pages = 0;
  225. var $is_single = false;
  226. var $is_preview = false;
  227. var $is_page = false;
  228. var $is_archive = false;
  229. var $is_date = false;
  230. var $is_year = false;
  231. var $is_month = false;
  232. var $is_day = false;
  233. var $is_time = false;
  234. var $is_author = false;
  235. var $is_category = false;
  236. var $is_tag = false;
  237. var $is_search = false;
  238. var $is_feed = false;
  239. var $is_comment_feed = false;
  240. var $is_trackback = false;
  241. var $is_home = false;
  242. var $is_404 = false;
  243. var $is_comments_popup = false;
  244. var $is_admin = false;
  245. var $is_attachment = false;
  246. var $is_singular = false;
  247. var $is_robots = false;
  248. var $is_posts_page = false;
  249. function init_query_flags() {
  250. $this->is_single = false;
  251. $this->is_page = false;
  252. $this->is_archive = false;
  253. $this->is_date = false;
  254. $this->is_year = false;
  255. $this->is_month = false;
  256. $this->is_day = false;
  257. $this->is_time = false;
  258. $this->is_author = false;
  259. $this->is_category = false;
  260. $this->is_tag = false;
  261. $this->is_search = false;
  262. $this->is_feed = false;
  263. $this->is_comment_feed = false;
  264. $this->is_trackback = false;
  265. $this->is_home = false;
  266. $this->is_404 = false;
  267. $this->is_paged = false;
  268. $this->is_admin = false;
  269. $this->is_attachment = false;
  270. $this->is_singular = false;
  271. $this->is_robots = false;
  272. $this->is_posts_page = false;
  273. }
  274. function init () {
  275. unset($this->posts);
  276. unset($this->query);
  277. $this->query_vars = array();
  278. unset($this->queried_object);
  279. unset($this->queried_object_id);
  280. $this->post_count = 0;
  281. $this->current_post = -1;
  282. $this->in_the_loop = false;
  283. $this->init_query_flags();
  284. }
  285. // Reparse the query vars.
  286. function parse_query_vars() {
  287. $this->parse_query('');
  288. }
  289. function fill_query_vars($array) {
  290. $keys = array(
  291. 'error'
  292. , 'm'
  293. , 'p'
  294. , 'subpost'
  295. , 'subpost_id'
  296. , 'attachment'
  297. , 'attachment_id'
  298. , 'name'
  299. , 'hour'
  300. , 'static'
  301. , 'pagename'
  302. , 'page_id'
  303. , 'second'
  304. , 'minute'
  305. , 'hour'
  306. , 'day'
  307. , 'monthnum'
  308. , 'year'
  309. , 'w'
  310. , 'category_name'
  311. , 'tag'
  312. , 'tag_id'
  313. , 'author_name'
  314. , 'feed'
  315. , 'tb'
  316. , 'paged'
  317. , 'comments_popup'
  318. , 'preview'
  319. );
  320. foreach ($keys as $key) {
  321. if ( !isset($array[$key]))
  322. $array[$key] = '';
  323. }
  324. $array_keys = array('category__in', 'category__not_in', 'category__and',
  325. 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and');
  326. foreach ( $array_keys as $key ) {
  327. if ( !isset($array[$key]))
  328. $array[$key] = array();
  329. }
  330. return $array;
  331. }
  332. // Parse a query string and set query type booleans.
  333. function parse_query ($query) {
  334. if ( !empty($query) || !isset($this->query) ) {
  335. $this->init();
  336. if ( is_array($query) )
  337. $this->query_vars = $query;
  338. else
  339. parse_str($query, $this->query_vars);
  340. $this->query = $query;
  341. }
  342. $this->query_vars = $this->fill_query_vars($this->query_vars);
  343. $qv = &$this->query_vars;
  344. if ( ! empty($qv['robots']) )
  345. $this->is_robots = true;
  346. $qv['p'] = (int) $qv['p'];
  347. $qv['page_id'] = (int) $qv['page_id'];
  348. $qv['year'] = (int) $qv['year'];
  349. $qv['monthnum'] = (int) $qv['monthnum'];
  350. $qv['day'] = (int) $qv['day'];
  351. $qv['w'] = (int) $qv['w'];
  352. $qv['m'] = (int) $qv['m'];
  353. if ( '' !== $qv['hour'] ) $qv['hour'] = (int) $qv['hour'];
  354. if ( '' !== $qv['minute'] ) $qv['minute'] = (int) $qv['minute'];
  355. if ( '' !== $qv['second'] ) $qv['second'] = (int) $qv['second'];
  356. // Compat. Map subpost to attachment.
  357. if ( '' != $qv['subpost'] )
  358. $qv['attachment'] = $qv['subpost'];
  359. if ( '' != $qv['subpost_id'] )
  360. $qv['attachment_id'] = $qv['subpost_id'];
  361. $qv['attachment_id'] = (int) $qv['attachment_id'];
  362. if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
  363. $this->is_single = true;
  364. $this->is_attachment = true;
  365. } elseif ( '' != $qv['name'] ) {
  366. $this->is_single = true;
  367. } elseif ( $qv['p'] ) {
  368. $this->is_single = true;
  369. } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
  370. // If year, month, day, hour, minute, and second are set, a single
  371. // post is being queried.
  372. $this->is_single = true;
  373. } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
  374. $this->is_page = true;
  375. $this->is_single = false;
  376. } elseif ( !empty($qv['s']) ) {
  377. $this->is_search = true;
  378. } else {
  379. // Look for archive queries. Dates, categories, authors.
  380. if ( '' !== $qv['second'] ) {
  381. $this->is_time = true;
  382. $this->is_date = true;
  383. }
  384. if ( '' !== $qv['minute'] ) {
  385. $this->is_time = true;
  386. $this->is_date = true;
  387. }
  388. if ( '' !== $qv['hour'] ) {
  389. $this->is_time = true;
  390. $this->is_date = true;
  391. }
  392. if ( $qv['day'] ) {
  393. if (! $this->is_date) {
  394. $this->is_day = true;
  395. $this->is_date = true;
  396. }
  397. }
  398. if ( $qv['monthnum'] ) {
  399. if (! $this->is_date) {
  400. $this->is_month = true;
  401. $this->is_date = true;
  402. }
  403. }
  404. if ( $qv['year'] ) {
  405. if (! $this->is_date) {
  406. $this->is_year = true;
  407. $this->is_date = true;
  408. }
  409. }
  410. if ( $qv['m'] ) {
  411. $this->is_date = true;
  412. if (strlen($qv['m']) > 9) {
  413. $this->is_time = true;
  414. } else if (strlen($qv['m']) > 7) {
  415. $this->is_day = true;
  416. } else if (strlen($qv['m']) > 5) {
  417. $this->is_month = true;
  418. } else {
  419. $this->is_year = true;
  420. }
  421. }
  422. if ('' != $qv['w']) {
  423. $this->is_date = true;
  424. }
  425. if ( empty($qv['cat']) || ($qv['cat'] == '0') ) {
  426. $this->is_category = false;
  427. } else {
  428. if (strpos($qv['cat'], '-') !== false) {
  429. $this->is_category = false;
  430. } else {
  431. $this->is_category = true;
  432. }
  433. }
  434. if ( '' != $qv['category_name'] ) {
  435. $this->is_category = true;
  436. }
  437. if ( !is_array($qv['category__in']) || empty($qv['category__in']) ) {
  438. $qv['category__in'] = array();
  439. } else {
  440. $qv['category__in'] = array_map('intval', $qv['category__in']);
  441. $this->is_category = true;
  442. }
  443. if ( !is_array($qv['category___not_in']) || empty($qv['category__not_in']) ) {
  444. $qv['category__not_in'] = array();
  445. } else {
  446. $qv['category__not_in'] = array_map('intval', $qv['category__not_in']);
  447. }
  448. if ( !is_array($qv['category__and']) || empty($qv['category__and']) ) {
  449. $qv['category__and'] = array();
  450. } else {
  451. $qv['category__and'] = array_map('intval', $qv['category__and']);
  452. $this->is_category = true;
  453. }
  454. if ( '' != $qv['tag'] )
  455. $this->is_tag = true;
  456. $qv['tag_id'] = (int) $qv['tag_id'];
  457. if ( !empty($qv['tag_id']) )
  458. $this->is_tag = true;
  459. if ( !is_array($qv['tag__in']) || empty($qv['tag__in']) ) {
  460. $qv['tag__in'] = array();
  461. } else {
  462. $qv['tag__in'] = array_map('intval', $qv['tag__in']);
  463. $this->is_tag = true;
  464. }
  465. if ( !is_array($qv['tag___not_in']) || empty($qv['tag__not_in']) ) {
  466. $qv['tag__not_in'] = array();
  467. } else {
  468. $qv['tag__not_in'] = array_map('intval', $qv['tag__not_in']);
  469. }
  470. if ( !is_array($qv['tag__and']) || empty($qv['tag__and']) ) {
  471. $qv['tag__and'] = array();
  472. } else {
  473. $qv['tag__and'] = array_map('intval', $qv['tag__and']);
  474. $this->is_category = true;
  475. }
  476. if ( !is_array($qv['tag_slug__in']) || empty($qv['tag_slug__in']) ) {
  477. $qv['tag_slug__in'] = array();
  478. } else {
  479. $qv['tag_slug__in'] = array_map('sanitize_title', $qv['tag_slug__in']);
  480. $this->is_tag = true;
  481. }
  482. if ( !is_array($qv['tag_slug__and']) || empty($qv['tag_slug__and']) ) {
  483. $qv['tag_slug__and'] = array();
  484. } else {
  485. $qv['tag_slug__and'] = array_map('sanitize_title', $qv['tag_slug__and']);
  486. $this->is_tag = true;
  487. }
  488. if ( empty($qv['author']) || ($qv['author'] == '0') ) {
  489. $this->is_author = false;
  490. } else {
  491. $this->is_author = true;
  492. }
  493. if ( '' != $qv['author_name'] ) {
  494. $this->is_author = true;
  495. }
  496. if ( ($this->is_date || $this->is_author || $this->is_category || $this->is_tag ) )
  497. $this->is_archive = true;
  498. }
  499. if ( '' != $qv['feed'] )
  500. $this->is_feed = true;
  501. if ( '' != $qv['tb'] )
  502. $this->is_trackback = true;
  503. if ( '' != $qv['paged'] )
  504. $this->is_paged = true;
  505. if ( '' != $qv['comments_popup'] )
  506. $this->is_comments_popup = true;
  507. // if we're previewing inside the write screen
  508. if ('' != $qv['preview'])
  509. $this->is_preview = true;
  510. if ( is_admin() )
  511. $this->is_admin = true;
  512. if ( false !== strpos($qv['feed'], 'comments-') ) {
  513. $qv['feed'] = str_replace('comments-', '', $qv['feed']);
  514. $qv['withcomments'] = 1;
  515. }
  516. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  517. if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
  518. $this->is_comment_feed = true;
  519. if ( !( $this->is_singular || $this->is_archive || $this->is_search || $this->is_feed || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_comments_popup ) )
  520. $this->is_home = true;
  521. // Correct is_* for page_on_front and page_for_posts
  522. if ( $this->is_home && ( empty($this->query) || $qv['preview'] == 'true' ) && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
  523. $this->is_page = true;
  524. $this->is_home = false;
  525. $qv['page_id'] = get_option('page_on_front');
  526. }
  527. if ( '' != $qv['pagename'] ) {
  528. $this->queried_object =& get_page_by_path($qv['pagename']);
  529. if ( !empty($this->queried_object) )
  530. $this->queried_object_id = (int) $this->queried_object->ID;
  531. else
  532. unset($this->queried_object);
  533. if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
  534. $this->is_page = false;
  535. $this->is_home = true;
  536. $this->is_posts_page = true;
  537. }
  538. }
  539. if ( $qv['page_id'] ) {
  540. if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
  541. $this->is_page = false;
  542. $this->is_home = true;
  543. $this->is_posts_page = true;
  544. }
  545. }
  546. if ( !empty($qv['post_type']) )
  547. $qv['post_type'] = sanitize_user($qv['post_type'], true);
  548. if ( !empty($qv['post_status']) )
  549. $qv['post_status'] = sanitize_user($qv['post_status'], true);
  550. if ( $this->is_posts_page && !$qv['withcomments'] )
  551. $this->is_comment_feed = false;
  552. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  553. // Done correcting is_* for page_on_front and page_for_posts
  554. if ('404' == $qv['error'])
  555. $this->set_404();
  556. if ( !empty($query) )
  557. do_action_ref_array('parse_query', array(&$this));
  558. }
  559. function set_404() {
  560. $is_feed = $this->is_feed;
  561. $this->init_query_flags();
  562. $this->is_404 = true;
  563. $this->is_feed = $is_feed;
  564. }
  565. function get($query_var) {
  566. if (isset($this->query_vars[$query_var])) {
  567. return $this->query_vars[$query_var];
  568. }
  569. return '';
  570. }
  571. function set($query_var, $value) {
  572. $this->query_vars[$query_var] = $value;
  573. }
  574. function &get_posts() {
  575. global $wpdb, $pagenow, $user_ID;
  576. do_action_ref_array('pre_get_posts', array(&$this));
  577. // Shorthand.
  578. $q = &$this->query_vars;
  579. $q = $this->fill_query_vars($q);
  580. // First let's clear some variables
  581. $distinct = '';
  582. $whichcat = '';
  583. $whichauthor = '';
  584. $whichpage = '';
  585. $result = '';
  586. $where = '';
  587. $limits = '';
  588. $join = '';
  589. $search = '';
  590. $groupby = '';
  591. if ( !isset($q['post_type']) )
  592. $q['post_type'] = 'post';
  593. $post_type = $q['post_type'];
  594. if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
  595. $q['posts_per_page'] = get_option('posts_per_page');
  596. if ( isset($q['showposts']) && $q['showposts'] ) {
  597. $q['showposts'] = (int) $q['showposts'];
  598. $q['posts_per_page'] = $q['showposts'];
  599. }
  600. if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
  601. $q['posts_per_page'] = $q['posts_per_archive_page'];
  602. if ( !isset($q['nopaging']) ) {
  603. if ($q['posts_per_page'] == -1) {
  604. $q['nopaging'] = true;
  605. } else {
  606. $q['nopaging'] = false;
  607. }
  608. }
  609. if ( $this->is_feed ) {
  610. $q['posts_per_page'] = get_option('posts_per_rss');
  611. $q['nopaging'] = false;
  612. }
  613. $q['posts_per_page'] = (int) $q['posts_per_page'];
  614. if ( $q['posts_per_page'] < -1 )
  615. $q['posts_per_page'] = abs($q['posts_per_page']);
  616. else if ( $q['posts_per_page'] == 0 )
  617. $q['posts_per_page'] = 1;
  618. if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
  619. $this->is_page = true;
  620. $this->is_home = false;
  621. $q['page_id'] = get_option('page_on_front');
  622. }
  623. if (isset($q['page'])) {
  624. $q['page'] = trim($q['page'], '/');
  625. $q['page'] = (int) $q['page'];
  626. $q['page'] = abs($q['page']);
  627. }
  628. $add_hours = intval(get_option('gmt_offset'));
  629. $add_minutes = intval(60 * (get_option('gmt_offset') - $add_hours));
  630. $wp_posts_post_date_field = "post_date"; // "DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)";
  631. // If a month is specified in the querystring, load that month
  632. if ( $q['m'] ) {
  633. $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
  634. $where .= ' AND YEAR(post_date)=' . substr($q['m'], 0, 4);
  635. if (strlen($q['m'])>5)
  636. $where .= ' AND MONTH(post_date)=' . substr($q['m'], 4, 2);
  637. if (strlen($q['m'])>7)
  638. $where .= ' AND DAYOFMONTH(post_date)=' . substr($q['m'], 6, 2);
  639. if (strlen($q['m'])>9)
  640. $where .= ' AND HOUR(post_date)=' . substr($q['m'], 8, 2);
  641. if (strlen($q['m'])>11)
  642. $where .= ' AND MINUTE(post_date)=' . substr($q['m'], 10, 2);
  643. if (strlen($q['m'])>13)
  644. $where .= ' AND SECOND(post_date)=' . substr($q['m'], 12, 2);
  645. }
  646. if ( '' !== $q['hour'] )
  647. $where .= " AND HOUR(post_date)='" . $q['hour'] . "'";
  648. if ( '' !== $q['minute'] )
  649. $where .= " AND MINUTE(post_date)='" . $q['minute'] . "'";
  650. if ( '' !== $q['second'] )
  651. $where .= " AND SECOND(post_date)='" . $q['second'] . "'";
  652. if ( $q['year'] )
  653. $where .= " AND YEAR(post_date)='" . $q['year'] . "'";
  654. if ( $q['monthnum'] )
  655. $where .= " AND MONTH(post_date)='" . $q['monthnum'] . "'";
  656. if ( $q['day'] )
  657. $where .= " AND DAYOFMONTH(post_date)='" . $q['day'] . "'";
  658. if ('' != $q['name']) {
  659. $q['name'] = sanitize_title($q['name']);
  660. $where .= " AND post_name = '" . $q['name'] . "'";
  661. } else if ('' != $q['pagename']) {
  662. if ( isset($this->queried_object_id) )
  663. $reqpage = $this->queried_object_id;
  664. else {
  665. $reqpage = get_page_by_path($q['pagename']);
  666. if ( !empty($reqpage) )
  667. $reqpage = $reqpage->ID;
  668. else
  669. $reqpage = 0;
  670. }
  671. if ( ('page' != get_option('show_on_front') ) || ( $reqpage != get_option('page_for_posts') ) ) {
  672. $q['pagename'] = str_replace('%2F', '/', urlencode(urldecode($q['pagename'])));
  673. $page_paths = '/' . trim($q['pagename'], '/');
  674. $q['pagename'] = sanitize_title(basename($page_paths));
  675. $q['name'] = $q['pagename'];
  676. $where .= " AND (ID = '$reqpage')";
  677. }
  678. } elseif ('' != $q['attachment']) {
  679. $q['attachment'] = str_replace('%2F', '/', urlencode(urldecode($q['attachment'])));
  680. $attach_paths = '/' . trim($q['attachment'], '/');
  681. $q['attachment'] = sanitize_title(basename($attach_paths));
  682. $q['name'] = $q['attachment'];
  683. $where .= " AND post_name = '" . $q['attachment'] . "'";
  684. }
  685. if ( $q['w'] )
  686. $where .= " AND WEEK(post_date, 1)='" . $q['w'] . "'";
  687. if ( intval($q['comments_popup']) )
  688. $q['p'] = intval($q['comments_popup']);
  689. // If an attachment is requested by number, let it supercede any post number.
  690. if ( $q['attachment_id'] )
  691. $q['p'] = $q['attachment_id'];
  692. // If a post number is specified, load that post
  693. if ( $q['p'] )
  694. $where = ' AND ID = ' . $q['p'];
  695. if ( $q['page_id'] ) {
  696. if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
  697. $q['p'] = $q['page_id'];
  698. $where = ' AND ID = ' . $q['page_id'];
  699. }
  700. }
  701. // If a search pattern is specified, load the posts that match
  702. if ( !empty($q['s']) ) {
  703. // added slashes screw with quote grouping when done early, so done later
  704. $q['s'] = stripslashes($q['s']);
  705. if ($q['sentence']) {
  706. $q['search_terms'] = array($q['s']);
  707. }
  708. else {
  709. preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q[s], $matches);
  710. $q['search_terms'] = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]);
  711. }
  712. $n = ($q['exact']) ? '' : '%';
  713. $searchand = '';
  714. foreach((array)$q['search_terms'] as $term) {
  715. $term = addslashes_gpc($term);
  716. $search .= "{$searchand}((post_title LIKE '{$n}{$term}{$n}') OR (post_content LIKE '{$n}{$term}{$n}'))";
  717. $searchand = ' AND ';
  718. }
  719. $term = addslashes_gpc($q['s']);
  720. if (!$q['sentence'] && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] )
  721. $search .= " OR (post_title LIKE '{$n}{$term}{$n}') OR (post_content LIKE '{$n}{$term}{$n}')";
  722. if ( !empty($search) )
  723. $search = " AND ({$search}) ";
  724. }
  725. // Category stuff
  726. if ( empty($q['cat']) || ($q['cat'] == '0') ||
  727. // Bypass cat checks if fetching specific posts
  728. $this->is_singular ) {
  729. $whichcat = '';
  730. } else {
  731. $q['cat'] = ''.urldecode($q['cat']).'';
  732. $q['cat'] = addslashes_gpc($q['cat']);
  733. $cat_array = preg_split('/[,\s]+/', $q['cat']);
  734. foreach ( $cat_array as $cat ) {
  735. $cat = intval($cat);
  736. $in = ($cat > 0);
  737. $cat = abs($cat);
  738. if ( $in ) {
  739. $q['category__in'][] = $cat;
  740. $q['category__in'] = array_merge($q['category__in'], get_term_children($cat, 'category'));
  741. } else {
  742. $q['category__not_in'][] = $cat;
  743. $q['category__not_in'] = array_merge($q['category__not_in'], get_term_children($cat, 'category'));
  744. }
  745. }
  746. }
  747. if ( !empty($q['category__in']) || !empty($q['category__not_in']) || !empty($q['category__and']) ) {
  748. $groupby = "{$wpdb->posts}.ID";
  749. }
  750. if ( !empty($q['category__in']) ) {
  751. $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";
  752. $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
  753. $include_cats = "'" . implode("', '", $q['category__in']) . "'";
  754. $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_cats) ";
  755. }
  756. if ( !empty($q['category__not_in']) ) {
  757. $ids = get_objects_in_term($q['category__not_in'], 'category');
  758. if ( is_wp_error( $ids ) )
  759. return $ids;
  760. if ( is_array($ids) && count($ids > 0) ) {
  761. $out_posts = "'" . implode("', '", $ids) . "'";
  762. $whichcat .= " AND $wpdb->posts.ID NOT IN ($out_posts)";
  763. }
  764. }
  765. // Category stuff for nice URLs
  766. if ( '' != $q['category_name'] ) {
  767. $reqcat = get_category_by_path($q['category_name']);
  768. $q['category_name'] = str_replace('%2F', '/', urlencode(urldecode($q['category_name'])));
  769. $cat_paths = '/' . trim($q['category_name'], '/');
  770. $q['category_name'] = sanitize_title(basename($cat_paths));
  771. $cat_paths = '/' . trim(urldecode($q['category_name']), '/');
  772. $q['category_name'] = sanitize_title(basename($cat_paths));
  773. $cat_paths = explode('/', $cat_paths);
  774. $cat_path = '';
  775. foreach ( (array) $cat_paths as $pathdir )
  776. $cat_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir);
  777. //if we don't match the entire hierarchy fallback on just matching the nicename
  778. if ( empty($reqcat) )
  779. $reqcat = get_category_by_path($q['category_name'], false);
  780. if ( !empty($reqcat) )
  781. $reqcat = $reqcat->term_id;
  782. else
  783. $reqcat = 0;
  784. $q['cat'] = $reqcat;
  785. $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";
  786. $whichcat = " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
  787. $in_cats = array($q['cat']);
  788. $in_cats = array_merge($in_cats, get_term_children($q['cat'], 'category'));
  789. $in_cats = "'" . implode("', '", $in_cats) . "'";
  790. $whichcat .= "AND $wpdb->term_taxonomy.term_id IN ($in_cats)";
  791. $groupby = "{$wpdb->posts}.ID";
  792. }
  793. // Tags
  794. if ( '' != $q['tag'] ) {
  795. if ( strpos($q['tag'], ',') !== false ) {
  796. $tags = preg_split('/[,\s]+/', $q['tag']);
  797. foreach ( (array) $tags as $tag ) {
  798. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  799. $q['tag_slug__in'][] = $tag;
  800. }
  801. } else if ( preg_match('/[+\s]+/', $q['tag']) ) {
  802. $tags = preg_split('/[+\s]+/', $q['tag']);
  803. foreach ( (array) $tags as $tag ) {
  804. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  805. $q['tag_slug__and'][] = $tag;
  806. }
  807. } else {
  808. $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
  809. $reqtag = is_term( $q['tag'], 'post_tag' );
  810. if ( !empty($reqtag) )
  811. $reqtag = $reqtag['term_id'];
  812. else
  813. $reqtag = 0;
  814. $q['tag_id'] = $reqtag;
  815. $q['tag__in'][] = $reqtag;
  816. }
  817. }
  818. if ( !empty($q['tag__in']) || !empty($q['tag__not_in']) || !empty($q['tag__and']) ||
  819. !empty($q['tag_slug__in']) || !empty($q['tag_slug__and']) ) {
  820. $groupby = "{$wpdb->posts}.ID";
  821. }
  822. if ( !empty($q['tag__in']) ) {
  823. $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";
  824. $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
  825. $include_tags = "'" . implode("', '", $q['tag__in']) . "'";
  826. $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_tags) ";
  827. $reqtag = is_term( $q['tag__in'][0], 'post_tag' );
  828. if ( !empty($reqtag) )
  829. $q['tag_id'] = $reqtag['term_id'];
  830. }
  831. if ( !empty($q['tag_slug__in']) ) {
  832. $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) INNER JOIN $wpdb->terms ON ($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id) ";
  833. $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
  834. $include_tags = "'" . implode("', '", $q['tag_slug__in']) . "'";
  835. $whichcat .= " AND $wpdb->terms.slug IN ($include_tags) ";
  836. $reqtag = is_term( $q['tag_slug__in'][0], 'post_tag' );
  837. if ( !empty($reqtag) )
  838. $q['tag_id'] = $reqtag['term_id'];
  839. }
  840. if ( !empty($q['tag__not_in']) ) {
  841. $ids = get_objects_in_term($q['tag__not_in'], 'post_tag');
  842. if ( is_array($ids) && count($ids > 0) ) {
  843. $out_posts = "'" . implode("', '", $ids) . "'";
  844. $whichcat .= " AND $wpdb->posts.ID NOT IN ($out_posts)";
  845. }
  846. }
  847. // Tag and slug intersections.
  848. $intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag');
  849. foreach ($intersections as $item => $taxonomy) {
  850. if ( empty($q[$item]) ) continue;
  851. if ( $item != 'category__and' ) {
  852. $reqtag = is_term( $q[$item][0], 'post_tag' );
  853. if ( !empty($reqtag) )
  854. $q['tag_id'] = $reqtag['term_id'];
  855. }
  856. $taxonomy_field = $item == 'tag_slug__and' ? 'slug' : 'term_id';
  857. $q[$item] = array_unique($q[$item]);
  858. $tsql = "SELECT p.ID FROM $wpdb->posts p INNER JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id) INNER JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) INNER JOIN $wpdb->terms t ON (tt.term_id = t.term_id)";
  859. $tsql .= " WHERE tt.taxonomy = '$taxonomy' AND t.$taxonomy_field IN ('" . implode("', '", $q[$item]) . "')";
  860. $tsql .= " GROUP BY p.ID HAVING count(p.ID) = " . count($q[$item]);
  861. $post_ids = $wpdb->get_col($tsql);
  862. if ( count($post_ids) )
  863. $whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") ";
  864. else {
  865. $whichcat = " AND 0 = 1";
  866. break;
  867. }
  868. }
  869. // Author/user stuff
  870. if ( empty($q['author']) || ($q['author'] == '0') ) {
  871. $whichauthor='';
  872. } else {
  873. $q['author'] = ''.urldecode($q['author']).'';
  874. $q['author'] = addslashes_gpc($q['author']);
  875. if (strpos($q['author'], '-') !== false) {
  876. $eq = '!=';
  877. $andor = 'AND';
  878. $q['author'] = explode('-', $q['author']);
  879. $q['author'] = ''.intval($q['author'][1]);
  880. } else {
  881. $eq = '=';
  882. $andor = 'OR';
  883. }
  884. $author_array = preg_split('/[,\s]+/', $q['author']);
  885. $whichauthor .= ' AND (post_author '.$eq.' '.intval($author_array[0]);
  886. for ($i = 1; $i < (count($author_array)); $i = $i + 1) {
  887. $whichauthor .= ' '.$andor.' post_author '.$eq.' '.intval($author_array[$i]);
  888. }
  889. $whichauthor .= ')';
  890. }
  891. // Author stuff for nice URLs
  892. if ('' != $q['author_name']) {
  893. if (strpos($q['author_name'], '/') !== false) {
  894. $q['author_name'] = explode('/',$q['author_name']);
  895. if ($q['author_name'][count($q['author_name'])-1]) {
  896. $q['author_name'] = $q['author_name'][count($q['author_name'])-1];#no trailing slash
  897. } else {
  898. $q['author_name'] = $q['author_name'][count($q['author_name'])-2];#there was a trailling slash
  899. }
  900. }
  901. $q['author_name'] = sanitize_title($q['author_name']);
  902. $q['author'] = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_nicename='".$q['author_name']."'");
  903. $whichauthor .= ' AND (post_author = '.intval($q['author']).')';
  904. }
  905. $where .= $search.$whichcat.$whichauthor;
  906. if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
  907. $q['order'] = 'DESC';
  908. // Order by
  909. if ( empty($q['orderby']) ) {
  910. $q['orderby'] = 'post_date '.$q['order'];
  911. } else {
  912. // Used to filter values
  913. $allowed_keys = array('author', 'date', 'category', 'title', 'modified', 'menu_order');
  914. $q['orderby'] = urldecode($q['orderby']);
  915. $q['orderby'] = addslashes_gpc($q['orderby']);
  916. $orderby_array = explode(' ',$q['orderby']);
  917. if ( empty($orderby_array) )
  918. $orderby_array[] = $q['orderby'];
  919. $q['orderby'] = '';
  920. for ($i = 0; $i < count($orderby_array); $i++) {
  921. // Only allow certain values for safety
  922. $orderby = $orderby_array[$i];
  923. if ( 'menu_order' != $orderby )
  924. $orderby = 'post_' . $orderby;
  925. if ( in_array($orderby_array[$i], $allowed_keys) )
  926. $q['orderby'] .= (($i == 0) ? '' : ',') . "$orderby {$q['order']}";
  927. }
  928. if ( empty($q['orderby']) )
  929. $q['orderby'] = 'post_date '.$q['order'];
  930. }
  931. if ( $this->is_attachment ) {
  932. $where .= " AND post_type = 'attachment'";
  933. } elseif ($this->is_page) {
  934. $where .= " AND post_type = 'page'";
  935. } elseif ($this->is_single) {
  936. $where .= " AND post_type = 'post'";
  937. } else {
  938. $where .= " AND post_type = '$post_type'";
  939. }
  940. if ( isset($q['post_status']) && '' != $q['post_status'] ) {
  941. $q_status = explode(',', $q['post_status']);
  942. $r_status = array();
  943. if ( in_array( 'draft' , $q_status ) )
  944. $r_status[] = "post_status = 'draft'";
  945. if ( in_array( 'pending', $q_status ) )
  946. $r_status[] = "post_status = 'pending'";
  947. if ( in_array( 'future' , $q_status ) )
  948. $r_status[] = "post_status = 'future'";
  949. if ( in_array( 'inherit' , $q_status ) )
  950. $r_status[] = "post_status = 'inherit'";
  951. if ( in_array( 'private', $q_status ) )
  952. $r_status[] = "post_status = 'private'";
  953. if ( in_array( 'publish', $q_status ) )
  954. $r_status[] = "post_status = 'publish'";
  955. if ( !empty($r_status) )
  956. $where .= " AND (" . join( ' OR ', $r_status ) . ")";
  957. } elseif ( !$this->is_singular ) {
  958. $where .= " AND (post_status = 'publish'";
  959. if ( is_admin() )
  960. $where .= " OR post_status = 'future' OR post_status = 'draft' OR post_status = 'pending'";
  961. if ( is_user_logged_in() ) {
  962. $where .= current_user_can( "read_private_{$post_type}s" ) ? " OR post_status = 'private'" : " OR post_author = $user_ID AND post_status = 'private'";
  963. }
  964. $where .= ')';
  965. }
  966. // Apply filters on where and join prior to paging so that any
  967. // manipulations to them are reflected in the paging by day queries.
  968. $where = apply_filters('posts_where', $where);
  969. $join = apply_filters('posts_join', $join);
  970. // Paging
  971. if ( empty($q['nopaging']) && !$this->is_singular ) {
  972. $page = abs(intval($q['paged']));
  973. if (empty($page)) {
  974. $page = 1;
  975. }
  976. if ( empty($q['offset']) ) {
  977. $pgstrt = '';
  978. $pgstrt = (intval($page) -1) * $q['posts_per_page'] . ', ';
  979. $limits = 'LIMIT '.$pgstrt.$q['posts_per_page'];
  980. } else { // we're ignoring $page and using 'offset'
  981. $q['offset'] = abs(intval($q['offset']));
  982. $pgstrt = $q['offset'] . ', ';
  983. $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
  984. }
  985. }
  986. // Comments feeds
  987. if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
  988. if ( $this->is_archive || $this->is_search ) {
  989. $cjoin = "LEFT JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
  990. $cwhere = "WHERE comment_approved = '1' $where";
  991. $cgroupby = "GROUP BY $wpdb->comments.comment_id";
  992. } else { // Other non singular e.g. front
  993. $cjoin = "LEFT JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
  994. $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
  995. $cgroupby = '';
  996. }
  997. $cjoin = apply_filters('comment_feed_join', $cjoin);
  998. $cwhere = apply_filters('comment_feed_where', $cwhere);
  999. $cgroupby = apply_filters('comment_feed_groupby', $cgroupby);
  1000. $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby ORDER BY comment_date_gmt DESC LIMIT " . get_option('posts_per_rss'));
  1001. $this->comment_count = count($this->comments);
  1002. $post_ids = array();
  1003. foreach ($this->comments as $comment)
  1004. $post_ids[] = (int) $comment->comment_post_ID;
  1005. $post_ids = join(',', $post_ids);
  1006. $join = '';
  1007. if ( $post_ids )
  1008. $where = "AND $wpdb->posts.ID IN ($post_ids) ";
  1009. else
  1010. $where = "AND 0";
  1011. }
  1012. // Apply post-paging filters on where and join. Only plugins that
  1013. // manipulate paging queries should use these hooks.
  1014. // Announce current selection parameters. For use by caching plugins.
  1015. do_action( 'posts_selection', $where . $groupby . $q['orderby'] . $limits . $join );
  1016. $where = apply_filters('posts_where_paged', $where);
  1017. $groupby = apply_filters('posts_groupby', $groupby);
  1018. if ( ! empty($groupby) )
  1019. $groupby = 'GROUP BY ' . $groupby;
  1020. $join = apply_filters('posts_join_paged', $join);
  1021. $orderby = apply_filters('posts_orderby', $q['orderby']);
  1022. if ( !empty( $orderby ) )
  1023. $orderby = 'ORDER BY ' . $orderby;
  1024. $distinct = apply_filters('posts_distinct', $distinct);
  1025. $fields = apply_filters('posts_fields', "$wpdb->posts.*");
  1026. $limits = apply_filters( 'post_limits', $limits );
  1027. $found_rows = '';
  1028. if ( !empty($limits) )
  1029. $found_rows = 'SQL_CALC_FOUND_ROWS';
  1030. $request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
  1031. $this->request = apply_filters('posts_request', $request);
  1032. $this->posts = $wpdb->get_results($this->request);
  1033. // Raw results filter. Prior to status checks.
  1034. $this->posts = apply_filters('posts_results', $this->posts);
  1035. if ( $this->is_comment_feed && $this->is_singular ) {
  1036. $cjoin = apply_filters('comment_feed_join', '');
  1037. $cwhere = apply_filters('comment_feed_where', "WHERE comment_post_ID = {$this->posts[0]->ID} AND comment_approved = '1'");
  1038. $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere ORDER BY comment_date_gmt DESC LIMIT " . get_option('posts_per_rss');
  1039. $this->comments = $wpdb->get_results($comments_request);
  1040. $this->comment_count = count($this->comments);
  1041. }
  1042. if ( !empty($limits) ) {
  1043. $found_posts_query = apply_filters( 'found_posts_query', 'SELECT FOUND_ROWS()' );
  1044. $this->found_posts = $wpdb->get_var( $found_posts_query );
  1045. $this->found_posts = apply_filters( 'found_posts', $this->found_posts );
  1046. $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
  1047. }
  1048. // Check post status to determine if post should be displayed.
  1049. if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
  1050. $status = get_post_status($this->posts[0]);
  1051. //$type = get_post_type($this->posts[0]);
  1052. if ( ('publish' != $status) ) {
  1053. if ( ! is_user_logged_in() ) {
  1054. // User must be logged in to view unpublished posts.
  1055. $this->posts = array();
  1056. } else {
  1057. if (in_array($status, array('draft', 'pending')) ) {
  1058. // User must have edit permissions on the draft to preview.
  1059. if (! current_user_can('edit_post', $this->posts[0]->ID)) {
  1060. $this->posts = array();
  1061. } else {
  1062. $this->is_preview = true;
  1063. $this->posts[0]->post_date = current_time('mysql');
  1064. }
  1065. } else if ('future' == $status) {
  1066. $this->is_preview = true;
  1067. if (!current_user_can('edit_post', $this->posts[0]->ID)) {
  1068. $this->posts = array ( );
  1069. }
  1070. } else {
  1071. if (! current_user_can('read_post', $this->posts[0]->ID))
  1072. $this->posts = array();
  1073. }
  1074. }
  1075. }
  1076. }
  1077. $this->posts = apply_filters('the_posts', $this->posts);
  1078. update_post_caches($this->posts);
  1079. $this->post_count = count($this->posts);
  1080. if ($this->post_count > 0) {
  1081. $this->post = $this->posts[0];
  1082. }
  1083. return $this->posts;
  1084. }
  1085. function next_post() {
  1086. $this->current_post++;
  1087. $this->post = $this->posts[$this->current_post];
  1088. return $this->post;
  1089. }
  1090. function the_post() {
  1091. global $post;
  1092. $this->in_the_loop = true;
  1093. $post = $this->next_post();
  1094. setup_postdata($post);
  1095. if ( $this->current_post == 0 ) // loop has just started
  1096. do_action('loop_start');
  1097. }
  1098. function have_posts() {
  1099. if ($this->current_post + 1 < $this->post_count) {
  1100. return true;
  1101. } elseif ($this->current_post + 1 == $this->post_count) {
  1102. do_action('loop_end');
  1103. // Do some cleaning up after the loop
  1104. $this->rewind_posts();
  1105. }
  1106. $this->in_the_loop = false;
  1107. return false;
  1108. }
  1109. function rewind_posts() {
  1110. $this->current_post = -1;
  1111. if ($this->post_count > 0) {
  1112. $this->post = $this->posts[0];
  1113. }
  1114. }
  1115. function next_comment() {
  1116. $this->current_comment++;
  1117. $this->comment = $this->comments[$this->current_comment];
  1118. return $this->comment;
  1119. }
  1120. function the_comment() {
  1121. global $comment;
  1122. $comment = $this->next_comment();
  1123. if ($this->current_comment == 0) {
  1124. do_action('comment_loop_start');
  1125. }
  1126. }
  1127. function have_comments() {
  1128. if ($this->current_comment + 1 < $this->comment_count) {
  1129. return true;
  1130. } elseif ($this->current_comment + 1 == $this->comment_count) {
  1131. $this->rewind_comments();
  1132. }
  1133. return false;
  1134. }
  1135. function rewind_comments() {
  1136. $this->current_comment = -1;
  1137. if ($this->comment_count > 0) {
  1138. $this->comment = $this->comments[0];
  1139. }
  1140. }
  1141. function &query($query) {
  1142. $this->parse_query($query);
  1143. return $this->get_posts();
  1144. }
  1145. function get_queried_object() {
  1146. if (isset($this->queried_object)) {
  1147. return $this->queried_object;
  1148. }
  1149. $this->queried_object = NULL;
  1150. $this->queried_object_id = 0;
  1151. if ($this->is_category) {
  1152. $cat = $this->get('cat');
  1153. $category = &get_category($cat);
  1154. $this->queried_object = &$category;
  1155. $this->queried_object_id = (int) $cat;
  1156. } else if ($this->is_tag) {
  1157. $tag_id = $this->get('tag_id');
  1158. $tag = &get_term($tag_id, 'post_tag');
  1159. if ( is_wp_error( $tag ) )
  1160. return $tag;
  1161. $this->queried_object = &$tag;
  1162. $this->queried_object_id = (int) $tag_id;
  1163. } else if ($this->is_posts_page) {
  1164. $this->queried_object = & get_page(get_option('page_for_posts'));
  1165. $this->queried_object_id = (int) $this->queried_object->ID;
  1166. } else if ($this->is_single) {
  1167. $this->queried_object = $this->post;
  1168. $this->queried_object_id = (int) $this->post->ID;
  1169. } else if ($this->is_page) {
  1170. $this->queried_object = $this->post;
  1171. $this->queried_object_id = (int) $this->post->ID;
  1172. } else if ($this->is_author) {
  1173. $author_id = (int) $this->get('author');
  1174. $author = get_userdata($author_id);
  1175. $this->queried_object = $author;
  1176. $this->queried_object_id = $author_id;
  1177. }
  1178. return $this->queried_object;
  1179. }
  1180. function get_queried_object_id() {
  1181. $this->get_queried_object();
  1182. if (isset($this->queried_object_id)) {
  1183. return $this->queried_object_id;
  1184. }
  1185. return 0;
  1186. }
  1187. function WP_Query ($query = '') {
  1188. if (! empty($query)) {
  1189. $this->query($query);
  1190. }
  1191. }
  1192. }
  1193. // Redirect old slugs
  1194. function wp_old_slug_redirect () {
  1195. global $wp_query;
  1196. if ( is_404() && '' != $wp_query->query_vars['name'] ) :
  1197. global $wpdb;
  1198. $query = "SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND meta_key = '_wp_old_slug' AND meta_value='" . $wp_query->query_vars['name'] . "'";
  1199. // if year, monthnum, or day have been specified, make our query more precise
  1200. // just in case there are multiple identical _wp_old_slug values
  1201. if ( '' != $wp_query->query_vars['year'] )
  1202. $query .= " AND YEAR(post_date) = '{$wp_query->query_vars['year']}'";
  1203. if ( '' != $wp_query->query_vars['monthnum'] )
  1204. $query .= " AND MONTH(post_date) = '{$wp_query->query_vars['monthnum']}'";
  1205. if ( '' != $wp_query->query_vars['day'] )
  1206. $query .= " AND DAYOFMONTH(post_date) = '{$wp_query->query_vars['day']}'";
  1207. $id = (int) $wpdb->get_var($query);
  1208. if ( !$id )
  1209. return;
  1210. $link = get_permalink($id);
  1211. if ( !$link )
  1212. return;
  1213. wp_redirect($link, '301'); // Permanent redirect
  1214. exit;
  1215. endif;
  1216. }
  1217. //
  1218. // Private helper functions
  1219. //
  1220. // Setup global post data.
  1221. function setup_postdata($post) {
  1222. global $id, $postdata, $authordata, $day, $currentmonth, $page, $pages, $multipage, $more, $numpages, $wp_query;
  1223. global $pagenow;
  1224. $id = (int) $post->ID;
  1225. $authordata = get_userdata($post->post_author);
  1226. $day = mysql2date('d.m.y', $post->post_date);
  1227. $currentmonth = mysql2date('m', $post->post_date);
  1228. $numpages = 1;
  1229. $page = get_query_var('page');
  1230. if ( !$page )
  1231. $page = 1;
  1232. if ( is_single() || is_page() )
  1233. $more = 1;
  1234. $content = $post->post_content;
  1235. if ( preg_match('/<!--nextpage-->/', $content) ) {
  1236. if ( $page > 1 )
  1237. $more = 1;
  1238. $multipage = 1;
  1239. $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
  1240. $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
  1241. $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
  1242. $pages = explode('<!--nextpage-->', $content);
  1243. $numpages = count($pages);
  1244. } else {
  1245. $pages[0] = $post->post_content;
  1246. $multipage = 0;
  1247. }
  1248. return true;
  1249. }
  1250. ?>