| 2 | | |
|---|
| 3 | | class WP_Query { |
|---|
| 4 | | var $query; |
|---|
| 5 | | var $query_vars; |
|---|
| 6 | | var $queried_object; |
|---|
| 7 | | var $queried_object_id; |
|---|
| 8 | | var $request; |
|---|
| 9 | | |
|---|
| 10 | | var $posts; |
|---|
| 11 | | var $post_count = 0; |
|---|
| 12 | | var $current_post = -1; |
|---|
| 13 | | var $in_the_loop = false; |
|---|
| 14 | | var $post; |
|---|
| 15 | | |
|---|
| 16 | | var $is_single = false; |
|---|
| 17 | | var $is_preview = false; |
|---|
| 18 | | var $is_page = false; |
|---|
| 19 | | var $is_archive = false; |
|---|
| 20 | | var $is_date = false; |
|---|
| 21 | | var $is_year = false; |
|---|
| 22 | | var $is_month = false; |
|---|
| 23 | | var $is_day = false; |
|---|
| 24 | | var $is_time = false; |
|---|
| 25 | | var $is_author = false; |
|---|
| 26 | | var $is_category = false; |
|---|
| 27 | | var $is_search = false; |
|---|
| 28 | | var $is_feed = false; |
|---|
| 29 | | var $is_trackback = false; |
|---|
| 30 | | var $is_home = false; |
|---|
| 31 | | var $is_404 = false; |
|---|
| 32 | | var $is_comments_popup = false; |
|---|
| 33 | | var $is_admin = false; |
|---|
| 34 | | var $is_attachment = false; |
|---|
| 35 | | |
|---|
| 36 | | function init_query_flags() { |
|---|
| 37 | | $this->is_single = false; |
|---|
| 38 | | $this->is_page = false; |
|---|
| 39 | | $this->is_archive = false; |
|---|
| 40 | | $this->is_date = false; |
|---|
| 41 | | $this->is_year = false; |
|---|
| 42 | | $this->is_month = false; |
|---|
| 43 | | $this->is_day = false; |
|---|
| 44 | | $this->is_time = false; |
|---|
| 45 | | $this->is_author = false; |
|---|
| 46 | | $this->is_category = false; |
|---|
| 47 | | $this->is_search = false; |
|---|
| 48 | | $this->is_feed = false; |
|---|
| 49 | | $this->is_trackback = false; |
|---|
| 50 | | $this->is_home = false; |
|---|
| 51 | | $this->is_404 = false; |
|---|
| 52 | | $this->is_paged = false; |
|---|
| 53 | | $this->is_admin = false; |
|---|
| 54 | | $this->is_attachment = false; |
|---|
| 55 | | } |
|---|
| 56 | | |
|---|
| 57 | | function init () { |
|---|
| 58 | | unset($this->posts); |
|---|
| 59 | | unset($this->query); |
|---|
| 60 | | unset($this->query_vars); |
|---|
| 61 | | unset($this->queried_object); |
|---|
| 62 | | unset($this->queried_object_id); |
|---|
| 63 | | $this->post_count = 0; |
|---|
| 64 | | $this->current_post = -1; |
|---|
| 65 | | $this->in_the_loop = false; |
|---|
| 66 | | |
|---|
| 67 | | $this->init_query_flags(); |
|---|
| 68 | | } |
|---|
| 69 | | |
|---|
| 70 | | // Reparse the query vars. |
|---|
| 71 | | function parse_query_vars() { |
|---|
| 72 | | $this->parse_query(''); |
|---|
| 73 | | } |
|---|
| 74 | | |
|---|
| 75 | | // Parse a query string and set query type booleans. |
|---|
| 76 | | function parse_query ($query) { |
|---|
| 77 | | if ( !empty($query) || !isset($this->query) ) { |
|---|
| 78 | | $this->init(); |
|---|
| 79 | | parse_str($query, $qv); |
|---|
| 80 | | $this->query = $query; |
|---|
| 81 | | $this->query_vars = $qv; |
|---|
| 82 | | } |
|---|
| 83 | | |
|---|
| 84 | | if ('404' == $qv['error']) { |
|---|
| 85 | | $this->is_404 = true; |
|---|
| 86 | | if ( !empty($query) ) { |
|---|
| 87 | | do_action('parse_query', array(&$this)); |
|---|
| 88 | | } |
|---|
| 89 | | return; |
|---|
| 90 | | } |
|---|
| 91 | | |
|---|
| 92 | | $qv['m'] = (int) $qv['m']; |
|---|
| 93 | | $qv['p'] = (int) $qv['p']; |
|---|
| 94 | | |
|---|
| 95 | | // Compat. Map subpost to attachment. |
|---|
| 96 | | if ( '' != $qv['subpost'] ) |
|---|
| 97 | | $qv['attachment'] = $qv['subpost']; |
|---|
| 98 | | if ( '' != $qv['subpost_id'] ) |
|---|
| 99 | | $qv['attachment_id'] = $qv['subpost_id']; |
|---|
| 100 | | |
|---|
| 101 | | if ( ('' != $qv['attachment']) || (int) $qv['attachment_id'] ) { |
|---|
| 102 | | $this->is_single = true; |
|---|
| 103 | | $this->is_attachment = true; |
|---|
| 104 | | } elseif ('' != $qv['name']) { |
|---|
| 105 | | $this->is_single = true; |
|---|
| 106 | | } elseif ( $qv['p'] ) { |
|---|
| 107 | | $this->is_single = true; |
|---|
| 108 | | } elseif (('' != $qv['hour']) && ('' != $qv['minute']) &&('' != $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day'])) { |
|---|
| 109 | | // If year, month, day, hour, minute, and second are set, a single |
|---|
| 110 | | // post is being queried. |
|---|
| 111 | | $this->is_single = true; |
|---|
| 112 | | } elseif ('' != $qv['static'] || '' != $qv['pagename'] || '' != $qv['page_id']) { |
|---|
| 113 | | $this->is_page = true; |
|---|
| 114 | | $this->is_single = false; |
|---|
| 115 | | } elseif (!empty($qv['s'])) { |
|---|
| 116 | | $this->is_search = true; |
|---|
| 117 | | } else { |
|---|
| 118 | | // Look for archive queries. Dates, categories, authors. |
|---|
| 119 | | |
|---|
| 120 | | if ( (int) $qv['second']) { |
|---|
| 121 | | $this->is_time = true; |
|---|
| 122 | | $this->is_date = true; |
|---|
| 123 | | } |
|---|
| 124 | | |
|---|
| 125 | | if ( (int) $qv['minute']) { |
|---|
| 126 | | $this->is_time = true; |
|---|
| 127 | | $this->is_date = true; |
|---|
| 128 | | } |
|---|
| 129 | | |
|---|
| 130 | | if ( (int) $qv['hour']) { |
|---|
| 131 | | $this->is_time = true; |
|---|
| 132 | | $this->is_date = true; |
|---|
| 133 | | } |
|---|
| 134 | | |
|---|
| 135 | | if ( (int) $qv['day']) { |
|---|
| 136 | | if (! $this->is_date) { |
|---|
| 137 | | $this->is_day = true; |
|---|
| 138 | | $this->is_date = true; |
|---|
| 139 | | } |
|---|
| 140 | | } |
|---|
| 141 | | |
|---|
| 142 | | if ( (int) $qv['monthnum']) { |
|---|
| 143 | | if (! $this->is_date) { |
|---|
| 144 | | $this->is_month = true; |
|---|
| 145 | | $this->is_date = true; |
|---|
| 146 | | } |
|---|
| 147 | | } |
|---|
| 148 | | |
|---|
| 149 | | if ( (int) $qv['year']) { |
|---|
| 150 | | if (! $this->is_date) { |
|---|
| 151 | | $this->is_year = true; |
|---|
| 152 | | $this->is_date = true; |
|---|
| 153 | | } |
|---|
| 154 | | } |
|---|
| 155 | | |
|---|
| 156 | | if ( (int) $qv['m']) { |
|---|
| 157 | | $this->is_date = true; |
|---|
| 158 | | if (strlen($qv['m']) > 9) { |
|---|
| 159 | | $this->is_time = true; |
|---|
| 160 | | } else if (strlen($qv['m']) > 7) { |
|---|
| 161 | | $this->is_day = true; |
|---|
| 162 | | } else if (strlen($qv['m']) > 5) { |
|---|
| 163 | | $this->is_month = true; |
|---|
| 164 | | } else { |
|---|
| 165 | | $this->is_year = true; |
|---|
| 166 | | } |
|---|
| 167 | | } |
|---|
| 168 | | |
|---|
| 169 | | if ('' != $qv['w']) { |
|---|
| 170 | | $this->is_date = true; |
|---|
| 171 | | } |
|---|
| 172 | | |
|---|
| 173 | | if (empty($qv['cat']) || ($qv['cat'] == '0')) { |
|---|
| 174 | | $this->is_category = false; |
|---|
| 175 | | } else { |
|---|
| 176 | | if (stristr($qv['cat'],'-')) { |
|---|
| 177 | | $this->is_category = false; |
|---|
| 178 | | } else { |
|---|
| 179 | | $this->is_category = true; |
|---|
| 180 | | } |
|---|
| 181 | | } |
|---|
| 182 | | |
|---|
| 183 | | if ('' != $qv['category_name']) { |
|---|
| 184 | | $this->is_category = true; |
|---|
| 185 | | } |
|---|
| 186 | | |
|---|
| 187 | | if ((empty($qv['author'])) || ($qv['author'] == '0')) { |
|---|
| 188 | | $this->is_author = false; |
|---|
| 189 | | } else { |
|---|
| 190 | | $this->is_author = true; |
|---|
| 191 | | } |
|---|
| 192 | | |
|---|
| 193 | | if ('' != $qv['author_name']) { |
|---|
| 194 | | $this->is_author = true; |
|---|
| 195 | | } |
|---|
| 196 | | |
|---|
| 197 | | if ( ($this->is_date || $this->is_author || $this->is_category)) { |
|---|
| 198 | | $this->is_archive = true; |
|---|
| 199 | | } |
|---|
| 200 | | } |
|---|
| 201 | | |
|---|
| 202 | | if ('' != $qv['feed']) { |
|---|
| 203 | | $this->is_feed = true; |
|---|
| 204 | | } |
|---|
| 205 | | |
|---|
| 206 | | if ('' != $qv['tb']) { |
|---|
| 207 | | $this->is_trackback = true; |
|---|
| 208 | | } |
|---|
| 209 | | |
|---|
| 210 | | if ('' != $qv['paged']) { |
|---|
| 211 | | $this->is_paged = true; |
|---|
| 212 | | } |
|---|
| 213 | | |
|---|
| 214 | | if ('' != $qv['comments_popup']) { |
|---|
| 215 | | $this->is_comments_popup = true; |
|---|
| 216 | | } |
|---|
| 217 | | |
|---|
| 218 | | //if we're previewing inside the write screen |
|---|
| 219 | | if ('' != $qv['preview']) { |
|---|
| 220 | | $this->is_preview = true; |
|---|
| 221 | | } |
|---|
| 222 | | |
|---|
| 223 | | if (strstr($_SERVER['PHP_SELF'], 'wp-admin/')) { |
|---|
| 224 | | $this->is_admin = true; |
|---|
| 225 | | } |
|---|
| 226 | | |
|---|
| 227 | | if ( ! ($this->is_attachment || $this->is_archive || $this->is_single || $this->is_page || $this->is_search || $this->is_feed || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_comments_popup)) { |
|---|
| 228 | | $this->is_home = true; |
|---|
| 229 | | } |
|---|
| 230 | | |
|---|
| 231 | | if ( !empty($query) ) { |
|---|
| 232 | | do_action('parse_query', array(&$this)); |
|---|
| 233 | | } |
|---|
| 234 | | } |
|---|
| 235 | | |
|---|
| 236 | | function set_404() { |
|---|
| 237 | | $this->init_query_flags(); |
|---|
| 238 | | $this->is_404 = true; |
|---|
| 239 | | } |
|---|
| 240 | | |
|---|
| 241 | | function get($query_var) { |
|---|
| 242 | | if (isset($this->query_vars[$query_var])) { |
|---|
| 243 | | return $this->query_vars[$query_var]; |
|---|
| 244 | | } |
|---|
| 245 | | |
|---|
| 246 | | return ''; |
|---|
| 247 | | } |
|---|
| 248 | | |
|---|
| 249 | | function set($query_var, $value) { |
|---|
| 250 | | $this->query_vars[$query_var] = $value; |
|---|
| 251 | | } |
|---|
| 252 | | |
|---|
| 253 | | function &get_posts() { |
|---|
| 254 | | global $wpdb, $pagenow, $user_ID; |
|---|
| 255 | | |
|---|
| 256 | | do_action('pre_get_posts', array(&$this)); |
|---|
| 257 | | |
|---|
| 258 | | // Shorthand. |
|---|
| 259 | | $q = &$this->query_vars; |
|---|
| 260 | | |
|---|
| 261 | | // First let's clear some variables |
|---|
| 262 | | $whichcat = ''; |
|---|
| 263 | | $whichauthor = ''; |
|---|
| 264 | | $whichpage = ''; |
|---|
| 265 | | $result = ''; |
|---|
| 266 | | $where = ''; |
|---|
| 267 | | $limits = ''; |
|---|
| 268 | | $distinct = ''; |
|---|
| 269 | | $join = ''; |
|---|
| 270 | | |
|---|
| 271 | | if ( !isset($q['post_type']) ) |
|---|
| 272 | | $q['post_type'] = 'post'; |
|---|
| 273 | | $post_type = $q['post_type']; |
|---|
| 274 | | if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 ) |
|---|
| 275 | | $q['posts_per_page'] = get_settings('posts_per_page'); |
|---|
| 276 | | if ( !isset($q['what_to_show']) ) |
|---|
| 277 | | $q['what_to_show'] = get_settings('what_to_show'); |
|---|
| 278 | | if ( isset($q['showposts']) && $q['showposts'] ) { |
|---|
| 279 | | $q['showposts'] = (int) $q['showposts']; |
|---|
| 280 | | $q['posts_per_page'] = $q['showposts']; |
|---|
| 281 | | } |
|---|
| 282 | | if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) ) |
|---|
| 283 | | $q['posts_per_page'] = $q['posts_per_archive_page']; |
|---|
| 284 | | if ( !isset($q['nopaging']) ) { |
|---|
| 285 | | if ($q['posts_per_page'] == -1) { |
|---|
| 286 | | $q['nopaging'] = true; |
|---|
| 287 | | } else { |
|---|
| 288 | | $q['nopaging'] = false; |
|---|
| 289 | | } |
|---|
| 290 | | } |
|---|
| 291 | | if ( $this->is_feed ) { |
|---|
| 292 | | $q['posts_per_page'] = get_settings('posts_per_rss'); |
|---|
| 293 | | $q['what_to_show'] = 'posts'; |
|---|
| 294 | | } |
|---|
| 295 | | |
|---|
| 296 | | if ( $this->is_home && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) { |
|---|
| 297 | | $this->is_page = true; |
|---|
| 298 | | $this->is_home = false; |
|---|
| 299 | | $q['page_id'] = get_option('page_on_front'); |
|---|
| 300 | | } |
|---|
| 301 | | |
|---|
| 302 | | if (isset($q['page'])) { |
|---|
| 303 | | $q['page'] = trim($q['page'], '/'); |
|---|
| 304 | | $q['page'] = (int) $q['page']; |
|---|
| 305 | | } |
|---|
| 306 | | |
|---|
| 307 | | $add_hours = intval(get_settings('gmt_offset')); |
|---|
| 308 | | $add_minutes = intval(60 * (get_settings('gmt_offset') - $add_hours)); |
|---|
| 309 | | $wp_posts_post_date_field = "post_date"; // "DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)"; |
|---|
| 310 | | |
|---|
| 311 | | // If a month is specified in the querystring, load that month |
|---|
| 312 | | if ( (int) $q['m'] ) { |
|---|
| 313 | | $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']); |
|---|
| 314 | | $where .= ' AND YEAR(post_date)=' . substr($q['m'], 0, 4); |
|---|
| 315 | | if (strlen($q['m'])>5) |
|---|
| 316 | | $where .= ' AND MONTH(post_date)=' . substr($q['m'], 4, 2); |
|---|
| 317 | | if (strlen($q['m'])>7) |
|---|
| 318 | | $where .= ' AND DAYOFMONTH(post_date)=' . substr($q['m'], 6, 2); |
|---|
| 319 | | if (strlen($q['m'])>9) |
|---|
| 320 | | $where .= ' AND HOUR(post_date)=' . substr($q['m'], 8, 2); |
|---|
| 321 | | if (strlen($q['m'])>11) |
|---|
| 322 | | $where .= ' AND MINUTE(post_date)=' . substr($q['m'], 10, 2); |
|---|
| 323 | | if (strlen($q['m'])>13) |
|---|
| 324 | | $where .= ' AND SECOND(post_date)=' . substr($q['m'], 12, 2); |
|---|
| 325 | | } |
|---|
| 326 | | |
|---|
| 327 | | if ( (int) $q['hour'] ) { |
|---|
| 328 | | $q['hour'] = '' . intval($q['hour']); |
|---|
| 329 | | $where .= " AND HOUR(post_date)='" . $q['hour'] . "'"; |
|---|
| 330 | | } |
|---|
| 331 | | |
|---|
| 332 | | if ( (int) $q['minute'] ) { |
|---|
| 333 | | $q['minute'] = '' . intval($q['minute']); |
|---|
| 334 | | $where .= " AND MINUTE(post_date)='" . $q['minute'] . "'"; |
|---|
| 335 | | } |
|---|
| 336 | | |
|---|
| 337 | | if ( (int) $q['second'] ) { |
|---|
| 338 | | $q['second'] = '' . intval($q['second']); |
|---|
| 339 | | $where .= " AND SECOND(post_date)='" . $q['second'] . "'"; |
|---|
| 340 | | } |
|---|
| 341 | | |
|---|
| 342 | | if ( (int) $q['year'] ) { |
|---|
| 343 | | $q['year'] = '' . intval($q['year']); |
|---|
| 344 | | $where .= " AND YEAR(post_date)='" . $q['year'] . "'"; |
|---|
| 345 | | } |
|---|
| 346 | | |
|---|
| 347 | | if ( (int) $q['monthnum'] ) { |
|---|
| 348 | | $q['monthnum'] = '' . intval($q['monthnum']); |
|---|
| 349 | | $where .= " AND MONTH(post_date)='" . $q['monthnum'] . "'"; |
|---|
| 350 | | } |
|---|
| 351 | | |
|---|
| 352 | | if ( (int) $q['day'] ) { |
|---|
| 353 | | $q['day'] = '' . intval($q['day']); |
|---|
| 354 | | $where .= " AND DAYOFMONTH(post_date)='" . $q['day'] . "'"; |
|---|
| 355 | | } |
|---|
| 356 | | |
|---|
| 357 | | // Compat. Map subpost to attachment. |
|---|
| 358 | | if ( '' != $q['subpost'] ) |
|---|
| 359 | | $q['attachment'] = $q['subpost']; |
|---|
| 360 | | if ( '' != $q['subpost_id'] ) |
|---|
| 361 | | $q['attachment_id'] = $q['subpost_id']; |
|---|
| 362 | | |
|---|
| 363 | | if ('' != $q['name']) { |
|---|
| 364 | | $q['name'] = sanitize_title($q['name']); |
|---|
| 365 | | $where .= " AND post_name = '" . $q['name'] . "'"; |
|---|
| 366 | | } else if ('' != $q['pagename']) { |
|---|
| 367 | | $reqpage = get_page_by_path($q['pagename']); |
|---|
| 368 | | if ( !empty($reqpage) ) |
|---|
| 369 | | $reqpage = $reqpage->ID; |
|---|
| 370 | | else |
|---|
| 371 | | $reqpage = 0; |
|---|
| 372 | | |
|---|
| 373 | | if ( ('page' == get_option('show_on_front') ) && ( $reqpage == get_option('page_for_posts') ) ) { |
|---|
| 374 | | $this->is_page = false; |
|---|
| 375 | | $this->is_home = true; |
|---|
| 376 | | } else { |
|---|
| 377 | | $q['pagename'] = str_replace('%2F', '/', urlencode(urldecode($q['pagename']))); |
|---|
| 378 | | $page_paths = '/' . trim($q['pagename'], '/'); |
|---|
| 379 | | $q['pagename'] = sanitize_title(basename($page_paths)); |
|---|
| 380 | | $q['name'] = $q['pagename']; |
|---|
| 381 | | $where .= " AND (ID = '$reqpage')"; |
|---|
| 382 | | } |
|---|
| 383 | | } elseif ('' != $q['attachment']) { |
|---|
| 384 | | $q['attachment'] = str_replace('%2F', '/', urlencode(urldecode($q['attachment']))); |
|---|
| 385 | | $attach_paths = '/' . trim($q['attachment'], '/'); |
|---|
| 386 | | $q['attachment'] = sanitize_title(basename($attach_paths)); |
|---|
| 387 | | $q['name'] = $q['attachment']; |
|---|
| 388 | | $where .= " AND post_name = '" . $q['attachment'] . "'"; |
|---|
| 389 | | } |
|---|
| 390 | | |
|---|
| 391 | | if ( (int) $q['w'] ) { |
|---|
| 392 | | $q['w'] = ''.intval($q['w']); |
|---|
| 393 | | $where .= " AND WEEK(post_date, 1)='" . $q['w'] . "'"; |
|---|
| 394 | | } |
|---|
| 395 | | |
|---|
| 396 | | if ( intval($q['comments_popup']) ) |
|---|
| 397 | | $q['p'] = intval($q['comments_popup']); |
|---|
| 398 | | |
|---|
| 399 | | // If a attachment is requested by number, let it supercede any post number. |
|---|
| 400 | | if ( ($q['attachment_id'] != '') && (intval($q['attachment_id']) != 0) ) |
|---|
| 401 | | $q['p'] = (int) $q['attachment_id']; |
|---|
| 402 | | |
|---|
| 403 | | // If a post number is specified, load that post |
|---|
| 404 | | if (($q['p'] != '') && intval($q['p']) != 0) { |
|---|
| 405 | | $q['p'] = (int) $q['p']; |
|---|
| 406 | | $where = ' AND ID = ' . $q['p']; |
|---|
| 407 | | } |
|---|
| 408 | | |
|---|
| 409 | | if (($q['page_id'] != '') && (intval($q['page_id']) != 0)) { |
|---|
| 410 | | $q['page_id'] = intval($q['page_id']); |
|---|
| 411 | | if ( ('page' == get_option('show_on_front') ) && ( $q['page_id'] == get_option('page_for_posts') ) ) { |
|---|
| 412 | | $this->is_page = false; |
|---|
| 413 | | $this->is_home = true; |
|---|
| 414 | | } else { |
|---|
| 415 | | $q['p'] = $q['page_id']; |
|---|
| 416 | | $where = ' AND ID = '.$q['page_id']; |
|---|
| 417 | | } |
|---|
| 418 | | } |
|---|
| 419 | | |
|---|
| 420 | | // If a search pattern is specified, load the posts that match |
|---|
| 421 | | if (!empty($q['s'])) { |
|---|
| 422 | | $q['s'] = addslashes_gpc($q['s']); |
|---|
| 423 | | $search = ' AND ('; |
|---|
| 424 | | $q['s'] = preg_replace('/, +/', ' ', $q['s']); |
|---|
| 425 | | $q['s'] = str_replace(',', ' ', $q['s']); |
|---|
| 426 | | $q['s'] = str_replace('"', ' ', $q['s']); |
|---|
| 427 | | $q['s'] = trim($q['s']); |
|---|
| 428 | | if ($q['exact']) { |
|---|
| 429 | | $n = ''; |
|---|
| 430 | | } else { |
|---|
| 431 | | $n = '%'; |
|---|
| 432 | | } |
|---|
| 433 | | if (!$q['sentence']) { |
|---|
| 434 | | $s_array = explode(' ',$q['s']); |
|---|
| 435 | | $q['search_terms'] = $s_array; |
|---|
| 436 | | $search .= '((post_title LIKE \''.$n.$s_array[0].$n.'\') OR (post_content LIKE \''.$n.$s_array[0].$n.'\'))'; |
|---|
| 437 | | for ( $i = 1; $i < count($s_array); $i = $i + 1) { |
|---|
| 438 | | $search .= ' AND ((post_title LIKE \''.$n.$s_array[$i].$n.'\') OR (post_content LIKE \''.$n.$s_array[$i].$n.'\'))'; |
|---|
| 439 | | } |
|---|
| 440 | | $search .= ' OR (post_title LIKE \''.$n.$q['s'].$n.'\') OR (post_content LIKE \''.$n.$q['s'].$n.'\')'; |
|---|
| 441 | | $search .= ')'; |
|---|
| 442 | | } else { |
|---|
| 443 | | $search = ' AND ((post_title LIKE \''.$n.$q['s'].$n.'\') OR (post_content LIKE \''.$n.$q['s'].$n.'\'))'; |
|---|
| 444 | | } |
|---|
| 445 | | } |
|---|
| 446 | | |
|---|
| 447 | | // Category stuff |
|---|
| 448 | | |
|---|
| 449 | | if ((empty($q['cat'])) || ($q['cat'] == '0') || |
|---|
| 450 | | // Bypass cat checks if fetching specific posts |
|---|
| 451 | | ( $this->is_single || $this->is_page )) { |
|---|
| 452 | | $whichcat=''; |
|---|
| 453 | | } else { |
|---|
| 454 | | $q['cat'] = ''.urldecode($q['cat']).''; |
|---|
| 455 | | $q['cat'] = addslashes_gpc($q['cat']); |
|---|
| 456 | | if (stristr($q['cat'],'-')) { |
|---|
| 457 | | // Note: if we have a negative, we ignore all the positives. It must |
|---|
| 458 | | // always mean 'everything /except/ this one'. We should be able to do |
|---|
| 459 | | // multiple negatives but we don't :-( |
|---|
| 460 | | $eq = '!='; |
|---|
| 461 | | $andor = 'AND'; |
|---|
| 462 | | $q['cat'] = explode('-',$q['cat']); |
|---|
| 463 | | $q['cat'] = intval($q['cat'][1]); |
|---|
| 464 | | } else { |
|---|
| 465 | | $eq = '='; |
|---|
| 466 | | $andor = 'OR'; |
|---|
| 467 | | } |
|---|
| 468 | | $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) "; |
|---|
| 469 | | $cat_array = preg_split('/[,\s]+/', $q['cat']); |
|---|
| 470 | | $whichcat .= ' AND (category_id '.$eq.' '.intval($cat_array[0]); |
|---|
| 471 | | $whichcat .= get_category_children($cat_array[0], ' '.$andor.' category_id '.$eq.' '); |
|---|
| 472 | | for ($i = 1; $i < (count($cat_array)); $i = $i + 1) { |
|---|
| 473 | | $whichcat .= ' '.$andor.' category_id '.$eq.' '.intval($cat_array[$i]); |
|---|
| 474 | | $whichcat .= get_category_children($cat_array[$i], ' '.$andor.' category_id '.$eq.' '); |
|---|
| 475 | | } |
|---|
| 476 | | $whichcat .= ')'; |
|---|
| 477 | | if ($eq == '!=') { |
|---|
| 478 | | $q['cat'] = '-'.$q['cat']; // Put back the knowledge that we are excluding a category. |
|---|
| 479 | | } |
|---|
| 480 | | } |
|---|
| 481 | | |
|---|
| 482 | | // Category stuff for nice URIs |
|---|
| 483 | | |
|---|
| 484 | | global $cache_categories; |
|---|
| 485 | | if ('' != $q['category_name']) { |
|---|
| 486 | | $reqcat = get_category_by_path($q['category_name']); |
|---|
| 487 | | $q['category_name'] = str_replace('%2F', '/', urlencode(urldecode($q['category_name']))); |
|---|
| 488 | | $cat_paths = '/' . trim($q['category_name'], '/'); |
|---|
| 489 | | $q['category_name'] = sanitize_title(basename($cat_paths)); |
|---|
| 490 | | |
|---|
| 491 | | $cat_paths = '/' . trim(urldecode($q['category_name']), '/'); |
|---|
| 492 | | $q['category_name'] = sanitize_title(basename($cat_paths)); |
|---|
| 493 | | $cat_paths = explode('/', $cat_paths); |
|---|
| 494 | | foreach($cat_paths as $pathdir) |
|---|
| 495 | | $cat_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir); |
|---|
| 496 | | |
|---|
| 497 | | //if we don't match the entire hierarchy fallback on just matching the nicename |
|---|
| 498 | | if ( empty($reqcat) ) |
|---|
| 499 | | $reqcat = get_category_by_path($q['category_name'], false); |
|---|
| 500 | | |
|---|
| 501 | | if ( !empty($reqcat) ) |
|---|
| 502 | | $reqcat = $reqcat->cat_ID; |
|---|
| 503 | | else |
|---|
| 504 | | $reqcat = 0; |
|---|
| 505 | | |
|---|
| 506 | | $q['cat'] = $reqcat; |
|---|
| 507 | | |
|---|
| 508 | | $tables = ", $wpdb->post2cat, $wpdb->categories"; |
|---|
| 509 | | $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) LEFT JOIN $wpdb->categories ON ($wpdb->post2cat.category_id = $wpdb->categories.cat_ID) "; |
|---|
| 510 | | $whichcat = " AND (category_id = '" . $q['cat'] . "'"; |
|---|
| 511 | | $whichcat .= get_category_children($q['cat'], " OR category_id = "); |
|---|
| 512 | | $whichcat .= ")"; |
|---|
| 513 | | } |
|---|
| 514 | | |
|---|
| 515 | | // Author/user stuff |
|---|
| 516 | | |
|---|
| 517 | | if ((empty($q['author'])) || ($q['author'] == '0')) { |
|---|
| 518 | | $whichauthor=''; |
|---|
| 519 | | } else { |
|---|
| 520 | | $q['author'] = ''.urldecode($q['author']).''; |
|---|
| 521 | | $q['author'] = addslashes_gpc($q['author']); |
|---|
| 522 | | if (stristr($q['author'], '-')) { |
|---|
| 523 | | $eq = '!='; |
|---|
| 524 | | $andor = 'AND'; |
|---|
| 525 | | $q['author'] = explode('-', $q['author']); |
|---|
| 526 | | $q['author'] = ''.intval($q['author'][1]); |
|---|
| 527 | | } else { |
|---|
| 528 | | $eq = '='; |
|---|
| 529 | | $andor = 'OR'; |
|---|
| 530 | | } |
|---|
| 531 | | $author_array = preg_split('/[,\s]+/', $q['author']); |
|---|
| 532 | | $whichauthor .= ' AND (post_author '.$eq.' '.intval($author_array[0]); |
|---|
| 533 | | for ($i = 1; $i < (count($author_array)); $i = $i + 1) { |
|---|
| 534 | | $whichauthor .= ' '.$andor.' post_author '.$eq.' '.intval($author_array[$i]); |
|---|
| 535 | | } |
|---|
| 536 | | $whichauthor .= ')'; |
|---|
| 537 | | } |
|---|
| 538 | | |
|---|
| 539 | | // Author stuff for nice URIs |
|---|
| 540 | | |
|---|
| 541 | | if ('' != $q['author_name']) { |
|---|
| 542 | | if (stristr($q['author_name'],'/')) { |
|---|
| 543 | | $q['author_name'] = explode('/',$q['author_name']); |
|---|
| 544 | | if ($q['author_name'][count($q['author_name'])-1]) { |
|---|
| 545 | | $q['author_name'] = $q['author_name'][count($q['author_name'])-1];#no trailing slash |
|---|
| 546 | | } else { |
|---|
| 547 | | $q['author_name'] = $q['author_name'][count($q['author_name'])-2];#there was a trailling slash |
|---|
| 548 | | } |
|---|
| 549 | | } |
|---|
| 550 | | $q['author_name'] = sanitize_title($q['author_name']); |
|---|
| 551 | | $q['author'] = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_nicename='".$q['author_name']."'"); |
|---|
| 552 | | $whichauthor .= ' AND (post_author = '.intval($q['author']).')'; |
|---|
| 553 | | } |
|---|
| 554 | | |
|---|
| 555 | | $where .= $search.$whichcat.$whichauthor; |
|---|
| 556 | | |
|---|
| 557 | | if ((empty($q['order'])) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC'))) { |
|---|
| 558 | | $q['order']='DESC'; |
|---|
| 559 | | } |
|---|
| 560 | | |
|---|
| 561 | | // Order by |
|---|
| 562 | | if (empty($q['orderby'])) { |
|---|
| 563 | | $q['orderby'] = 'post_date '.$q['order']; |
|---|
| 564 | | } else { |
|---|
| 565 | | // Used to filter values |
|---|
| 566 | | $allowed_keys = array('author', 'date', 'category', 'title', 'modified', 'menu_order'); |
|---|
| 567 | | $q['orderby'] = urldecode($q['orderby']); |
|---|
| 568 | | $q['orderby'] = addslashes_gpc($q['orderby']); |
|---|
| 569 | | $orderby_array = explode(' ',$q['orderby']); |
|---|
| 570 | | if ( empty($orderby_array) ) |
|---|
| 571 | | $orderby_array[] = $q['orderby']; |
|---|
| 572 | | $q['orderby'] = ''; |
|---|
| 573 | | for ($i = 0; $i < count($orderby_array); $i++) { |
|---|
| 574 | | // Only allow certain values for safety |
|---|
| 575 | | $orderby = $orderby_array[$i]; |
|---|
| 576 | | if ( 'menu_order' != $orderby ) |
|---|
| 577 | | $orderby = 'post_' . $orderby; |
|---|
| 578 | | if ( in_array($orderby_array[$i], $allowed_keys) ) |
|---|
| 579 | | $q['orderby'] .= (($i == 0) ? '' : ',') . "$orderby {$q['order']}"; |
|---|
| 580 | | } |
|---|
| 581 | | if ( empty($q['orderby']) ) |
|---|
| 582 | | $q['orderby'] = 'post_date '.$q['order']; |
|---|
| 583 | | } |
|---|
| 584 | | |
|---|
| 585 | | //$now = gmdate('Y-m-d H:i:59'); |
|---|
| 586 | | |
|---|
| 587 | | //only select past-dated posts, except if a logged in user is viewing a single: then, if they |
|---|
| 588 | | //can edit the post, we let them through |
|---|
| 589 | | //if ($pagenow != 'post.php' && $pagenow != 'edit.php' && !($this->is_single && $user_ID)) { |
|---|
| 590 | | // $where .= " AND post_date_gmt <= '$now'"; |
|---|
| 591 | | // $distinct = 'DISTINCT'; |
|---|
| 592 | | //} |
|---|
| 593 | | |
|---|
| 594 | | if ( $this->is_attachment ) { |
|---|
| 595 | | $where .= ' AND (post_type = "attachment")'; |
|---|
| 596 | | } elseif ($this->is_page) { |
|---|
| 597 | | $where .= ' AND (post_type = "page")'; |
|---|
| 598 | | } elseif ($this->is_single) { |
|---|
| 599 | | $where .= ' AND (post_type = "post")'; |
|---|
| 600 | | } else { |
|---|
| 601 | | $where .= " AND (post_type = '$post_type' AND (post_status = 'publish'"; |
|---|
| 602 | | |
|---|
| 603 | | if ( is_admin() ) |
|---|
| 604 | | $where .= " OR post_status = 'future' OR post_status = 'draft'"; |
|---|
| 605 | | else |
|---|
| 606 | | $distinct = 'DISTINCT'; |
|---|
| 607 | | |
|---|
| 608 | | if ( is_user_logged_in() ) |
|---|
| 609 | | $where .= " OR post_author = $user_ID AND post_status = 'private'))"; |
|---|
| 610 | | else |
|---|
| 611 | | $where .= '))'; |
|---|
| 612 | | } |
|---|
| 613 | | |
|---|
| 614 | | // Apply filters on where and join prior to paging so that any |
|---|
| 615 | | // manipulations to them are reflected in the paging by day queries. |
|---|
| 616 | | $where = apply_filters('posts_where', $where); |
|---|
| 617 | | $join = apply_filters('posts_join', $join); |
|---|
| 618 | | |
|---|
| 619 | | // Paging |
|---|
| 620 | | if (empty($q['nopaging']) && ! $this->is_single) { |
|---|
| 621 | | $page = $q['paged']; |
|---|
| 622 | | if (empty($page)) { |
|---|
| 623 | | $page = 1; |
|---|
| 624 | | } |
|---|
| 625 | | |
|---|
| 626 | | if (($q['what_to_show'] == 'posts')) { |
|---|
| 627 | | $pgstrt = ''; |
|---|
| 628 | | $pgstrt = (intval($page) -1) * $q['posts_per_page'] . ', '; |
|---|
| 629 | | $limits = 'LIMIT '.$pgstrt.$q['posts_per_page']; |
|---|
| 630 | | } elseif ($q['what_to_show'] == 'days') { |
|---|
| 631 | | $startrow = $q['posts_per_page'] * (intval($page)-1); |
|---|
| 632 | | $start_date = $wpdb->get_var("SELECT max(post_date) FROM $wpdb->posts $join WHERE (1=1) $where GROUP BY year(post_date), month(post_date), dayofmonth(post_date) ORDER BY post_date DESC LIMIT $startrow,1"); |
|---|
| 633 | | $endrow = $startrow + $q['posts_per_page'] - 1; |
|---|
| 634 | | $end_date = $wpdb->get_var("SELECT min(post_date) FROM $wpdb->posts $join WHERE (1=1) $where GROUP BY year(post_date), month(post_date), dayofmonth(post_date) ORDER BY post_date DESC LIMIT $endrow,1"); |
|---|
| 635 | | |
|---|
| 636 | | if ($page > 1) { |
|---|
| 637 | | $where .= " AND post_date >= '$end_date' AND post_date <= '$start_date'"; |
|---|
| 638 | | } else { |
|---|
| 639 | | $where .= " AND post_date >= '$end_date'"; |
|---|
| 640 | | } |
|---|
| 641 | | } |
|---|
| 642 | | } |
|---|
| 643 | | |
|---|
| 644 | | // Apply post-paging filters on where and join. Only plugins that |
|---|
| 645 | | // manipulate paging queries should use these hooks. |
|---|
| 646 | | $where = apply_filters('posts_where_paged', $where); |
|---|
| 647 | | $groupby = " $wpdb->posts.ID "; |
|---|
| 648 | | $groupby = apply_filters('posts_groupby', $groupby); |
|---|
| 649 | | $join = apply_filters('posts_join_paged', $join); |
|---|
| 650 | | $orderby = apply_filters('posts_orderby', $q['orderby']); |
|---|
| 651 | | $request = " SELECT $distinct * FROM $wpdb->posts $join WHERE 1=1" . $where . " GROUP BY " . $groupby . " ORDER BY " . $orderby . " $limits"; |
|---|
| 652 | | $this->request = apply_filters('posts_request', $request); |
|---|
| 653 | | |
|---|
| 654 | | $this->posts = $wpdb->get_results($this->request); |
|---|
| 655 | | |
|---|
| 656 | | // Check post status to determine if post should be displayed. |
|---|
| 657 | | if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) { |
|---|
| 658 | | $status = get_post_status($this->posts[0]); |
|---|
| 659 | | //$type = get_post_type($this->posts[0]); |
|---|
| 660 | | if ( ('publish' != $status) ) { |
|---|
| 661 | | if ( ! is_user_logged_in() ) { |
|---|
| 662 | | // User must be logged in to view unpublished posts. |
|---|
| 663 | | $this->posts = array(); |
|---|
| 664 | | } else { |
|---|
| 665 | | if ('draft' == $status) { |
|---|
| 666 | | // User must have edit permissions on the draft to preview. |
|---|
| 667 | | if (! current_user_can('edit_post', $this->posts[0]->ID)) { |
|---|
| 668 | | $this->posts = array(); |
|---|
| 669 | | } else { |
|---|
| 670 | | $this->is_preview = true; |
|---|
| 671 | | $this->posts[0]->post_date = current_time('mysql'); |
|---|
| 672 | | } |
|---|
| 673 | | } else if ('future' == $status) { |
|---|
| 674 | | $this->is_preview = true; |
|---|
| 675 | | if (!current_user_can('edit_post', $this->posts[0]->ID)) { |
|---|
| 676 | | $this->posts = array ( ); |
|---|
| 677 | | } |
|---|
| 678 | | } else { |
|---|
| 679 | | if (! current_user_can('read_post', $this->posts[0]->ID)) |
|---|
| 680 | | $this->posts = array(); |
|---|
| 681 | | } |
|---|
| 682 | | } |
|---|
| 683 | | } |
|---|
| 684 | | } |
|---|
| 685 | | |
|---|
| 686 | | update_post_caches($this->posts); |
|---|
| 687 | | |
|---|
| 688 | | $this->posts = apply_filters('the_posts', $this->posts); |
|---|
| 689 | | $this->post_count = count($this->posts); |
|---|
| 690 | | if ($this->post_count > 0) { |
|---|
| 691 | | $this->post = $this->posts[0]; |
|---|
| 692 | | } |
|---|
| 693 | | |
|---|
| 694 | | return $this->posts; |
|---|
| 695 | | } |
|---|
| 696 | | |
|---|
| 697 | | function next_post() { |
|---|
| 698 | | |
|---|
| 699 | | $this->current_post++; |
|---|
| 700 | | |
|---|
| 701 | | $this->post = $this->posts[$this->current_post]; |
|---|
| 702 | | return $this->post; |
|---|
| 703 | | } |
|---|
| 704 | | |
|---|
| 705 | | function the_post() { |
|---|
| 706 | | global $post; |
|---|
| 707 | | $this->in_the_loop = true; |
|---|
| 708 | | $post = $this->next_post(); |
|---|
| 709 | | setup_postdata($post); |
|---|
| 710 | | |
|---|
| 711 | | if ( $this->current_post == 0 ) // loop has just started |
|---|
| 712 | | do_action('loop_start'); |
|---|
| 713 | | } |
|---|
| 714 | | |
|---|
| 715 | | function have_posts() { |
|---|
| 716 | | if ($this->current_post + 1 < $this->post_count) { |
|---|
| 717 | | return true; |
|---|
| 718 | | } elseif ($this->current_post + 1 == $this->post_count) { |
|---|
| 719 | | do_action('loop_end'); |
|---|
| 720 | | // Do some cleaning up after the loop |
|---|
| 721 | | $this->rewind_posts(); |
|---|
| 722 | | } |
|---|
| 723 | | |
|---|
| 724 | | $this->in_the_loop = false; |
|---|
| 725 | | return false; |
|---|
| 726 | | } |
|---|
| 727 | | |
|---|
| 728 | | function rewind_posts() { |
|---|
| 729 | | $this->current_post = -1; |
|---|
| 730 | | if ($this->post_count > 0) { |
|---|
| 731 | | $this->post = $this->posts[0]; |
|---|
| 732 | | } |
|---|
| 733 | | } |
|---|
| 734 | | |
|---|
| 735 | | function &query($query) { |
|---|
| 736 | | $this->parse_query($query); |
|---|
| 737 | | return $this->get_posts(); |
|---|
| 738 | | } |
|---|
| 739 | | |
|---|
| 740 | | function get_queried_object() { |
|---|
| 741 | | if (isset($this->queried_object)) { |
|---|
| 742 | | return $this->queried_object; |
|---|
| 743 | | } |
|---|
| 744 | | |
|---|
| 745 | | $this->queried_object = NULL; |
|---|
| 746 | | $this->queried_object_id = 0; |
|---|
| 747 | | |
|---|
| 748 | | if ($this->is_category) { |
|---|
| 749 | | $cat = $this->get('cat'); |
|---|
| 750 | | $category = &get_category($cat); |
|---|
| 751 | | $this->queried_object = &$category; |
|---|
| 752 | | $this->queried_object_id = $cat; |
|---|
| 753 | | } else if ($this->is_single) { |
|---|
| 754 | | $this->queried_object = $this->post; |
|---|
| 755 | | $this->queried_object_id = $this->post->ID; |
|---|
| 756 | | } else if ($this->is_page) { |
|---|
| 757 | | $this->queried_object = $this->post; |
|---|
| 758 | | $this->queried_object_id = $this->post->ID; |
|---|
| 759 | | } else if ($this->is_author) { |
|---|
| 760 | | $author_id = $this->get('author'); |
|---|
| 761 | | $author = get_userdata($author_id); |
|---|
| 762 | | $this->queried_object = $author; |
|---|
| 763 | | $this->queried_object_id = $author_id; |
|---|
| 764 | | } |
|---|
| 765 | | |
|---|
| 766 | | return $this->queried_object; |
|---|
| 767 | | } |
|---|
| 768 | | |
|---|
| 769 | | function get_queried_object_id() { |
|---|
| 770 | | $this->get_queried_object(); |
|---|
| 771 | | |
|---|
| 772 | | if (isset($this->queried_object_id)) { |
|---|
| 773 | | return $this->queried_object_id; |
|---|
| 774 | | } |
|---|
| 775 | | |
|---|
| 776 | | return 0; |
|---|
| 777 | | } |
|---|
| 778 | | |
|---|
| 779 | | function WP_Query ($query = '') { |
|---|
| 780 | | if (! empty($query)) { |
|---|
| 781 | | $this->query($query); |
|---|
| 782 | | } |
|---|
| 783 | | } |
|---|
| 784 | | } |
|---|