WP_Query whitespace cleanup, some loop cleanup as well

git-svn-id: https://develop.svn.wordpress.org/trunk@14401 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Dion Hulse 2010-05-03 20:19:13 +00:00
parent 54c73c0c2b
commit aa7c1d8a41
1 changed files with 62 additions and 63 deletions

View File

@ -1204,7 +1204,7 @@ class WP_Query {
, 'sentence' , 'sentence'
); );
foreach ($keys as $key) { foreach ( $keys as $key ) {
if ( !isset($array[$key])) if ( !isset($array[$key]))
$array[$key] = ''; $array[$key] = '';
} }
@ -1227,7 +1227,7 @@ class WP_Query {
* *
* @param string|array $query * @param string|array $query
*/ */
function parse_query($query) { function parse_query($query ) {
if ( !empty($query) || !isset($this->query) ) { if ( !empty($query) || !isset($this->query) ) {
$this->init(); $this->init();
if ( is_array($query) ) if ( is_array($query) )
@ -1301,21 +1301,21 @@ class WP_Query {
} }
if ( $qv['day'] ) { if ( $qv['day'] ) {
if (! $this->is_date) { if ( ! $this->is_date ) {
$this->is_day = true; $this->is_day = true;
$this->is_date = true; $this->is_date = true;
} }
} }
if ( $qv['monthnum'] ) { if ( $qv['monthnum'] ) {
if (! $this->is_date) { if ( ! $this->is_date ) {
$this->is_month = true; $this->is_month = true;
$this->is_date = true; $this->is_date = true;
} }
} }
if ( $qv['year'] ) { if ( $qv['year'] ) {
if (! $this->is_date) { if ( ! $this->is_date ) {
$this->is_year = true; $this->is_year = true;
$this->is_date = true; $this->is_date = true;
} }
@ -1323,25 +1323,25 @@ class WP_Query {
if ( $qv['m'] ) { if ( $qv['m'] ) {
$this->is_date = true; $this->is_date = true;
if (strlen($qv['m']) > 9) { if ( strlen($qv['m']) > 9 ) {
$this->is_time = true; $this->is_time = true;
} else if (strlen($qv['m']) > 7) { } else if ( strlen($qv['m']) > 7 ) {
$this->is_day = true; $this->is_day = true;
} else if (strlen($qv['m']) > 5) { } else if ( strlen($qv['m']) > 5 ) {
$this->is_month = true; $this->is_month = true;
} else { } else {
$this->is_year = true; $this->is_year = true;
} }
} }
if ('' != $qv['w']) { if ( '' != $qv['w'] ) {
$this->is_date = true; $this->is_date = true;
} }
if ( empty($qv['cat']) || ($qv['cat'] == '0') ) { if ( empty($qv['cat']) || ($qv['cat'] == '0') ) {
$this->is_category = false; $this->is_category = false;
} else { } else {
if (strpos($qv['cat'], '-') !== false) { if ( strpos($qv['cat'], '-') !== false ) {
$this->is_category = false; $this->is_category = false;
} else { } else {
$this->is_category = true; $this->is_category = true;
@ -1454,7 +1454,7 @@ class WP_Query {
$this->is_comments_popup = true; $this->is_comments_popup = true;
// if we're previewing inside the write screen // if we're previewing inside the write screen
if ('' != $qv['preview']) if ( '' != $qv['preview'] )
$this->is_preview = true; $this->is_preview = true;
if ( is_admin() ) if ( is_admin() )
@ -1510,8 +1510,8 @@ class WP_Query {
} }
} }
if ( !empty($qv['post_type']) ) { if ( !empty($qv['post_type']) ) {
if(is_array($qv['post_type'])) if ( is_array($qv['post_type']) )
$qv['post_type'] = array_map('sanitize_user', $qv['post_type'], array(true)); $qv['post_type'] = array_map('sanitize_user', $qv['post_type'], array(true));
else else
$qv['post_type'] = sanitize_user($qv['post_type'], true); $qv['post_type'] = sanitize_user($qv['post_type'], true);
@ -1526,7 +1526,7 @@ class WP_Query {
$this->is_singular = $this->is_single || $this->is_page || $this->is_attachment; $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
// Done correcting is_* for page_on_front and page_for_posts // Done correcting is_* for page_on_front and page_for_posts
if ('404' == $qv['error']) if ( '404' == $qv['error'] )
$this->set_404(); $this->set_404();
if ( !empty($query) ) if ( !empty($query) )
@ -1545,7 +1545,7 @@ class WP_Query {
$this->init_query_flags(); $this->init_query_flags();
$this->is_404 = true; $this->is_404 = true;
$this->is_feed = $is_feed; //$this->is_feed = $is_feed;
} }
/** /**
@ -1558,9 +1558,8 @@ class WP_Query {
* @return mixed * @return mixed
*/ */
function get($query_var) { function get($query_var) {
if (isset($this->query_vars[$query_var])) { if ( isset($this->query_vars[$query_var]) )
return $this->query_vars[$query_var]; return $this->query_vars[$query_var];
}
return ''; return '';
} }
@ -1639,7 +1638,7 @@ class WP_Query {
if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) ) if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
$q['posts_per_page'] = $q['posts_per_archive_page']; $q['posts_per_page'] = $q['posts_per_archive_page'];
if ( !isset($q['nopaging']) ) { if ( !isset($q['nopaging']) ) {
if ($q['posts_per_page'] == -1) { if ( $q['posts_per_page'] == -1 ) {
$q['nopaging'] = true; $q['nopaging'] = true;
} else { } else {
$q['nopaging'] = false; $q['nopaging'] = false;
@ -1664,7 +1663,7 @@ class WP_Query {
$q['page_id'] = get_option('page_on_front'); $q['page_id'] = get_option('page_on_front');
} }
if (isset($q['page'])) { if ( isset($q['page']) ) {
$q['page'] = trim($q['page'], '/'); $q['page'] = trim($q['page'], '/');
$q['page'] = absint($q['page']); $q['page'] = absint($q['page']);
} }
@ -1679,15 +1678,15 @@ class WP_Query {
if ( $q['m'] ) { if ( $q['m'] ) {
$q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']); $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
$where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4); $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
if (strlen($q['m'])>5) if ( strlen($q['m']) > 5 )
$where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2); $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
if (strlen($q['m'])>7) if ( strlen($q['m']) > 7 )
$where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2); $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
if (strlen($q['m'])>9) if ( strlen($q['m']) > 9 )
$where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2); $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
if (strlen($q['m'])>11) if ( strlen($q['m']) > 11 )
$where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2); $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
if (strlen($q['m'])>13) if ( strlen($q['m']) > 13 )
$where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2); $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
} }
@ -1716,7 +1715,7 @@ class WP_Query {
if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) ) if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
continue; continue;
if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false) { if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) {
// Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name' // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
$q['name'] = $q[ $ptype_obj->query_var ]; $q['name'] = $q[ $ptype_obj->query_var ];
} else { } else {
@ -1773,7 +1772,7 @@ class WP_Query {
$q['attachment_id'] = $reqpage; $q['attachment_id'] = $reqpage;
} }
} }
} elseif ('' != $q['attachment']) { } elseif ( '' != $q['attachment'] ) {
$q['attachment'] = str_replace('%2F', '/', urlencode(urldecode($q['attachment']))); $q['attachment'] = str_replace('%2F', '/', urlencode(urldecode($q['attachment'])));
$attach_paths = '/' . trim($q['attachment'], '/'); $attach_paths = '/' . trim($q['attachment'], '/');
$q['attachment'] = sanitize_title(basename($attach_paths)); $q['attachment'] = sanitize_title(basename($attach_paths));
@ -1824,7 +1823,7 @@ class WP_Query {
} }
$n = !empty($q['exact']) ? '' : '%'; $n = !empty($q['exact']) ? '' : '%';
$searchand = ''; $searchand = '';
foreach( (array) $q['search_terms'] as $term) { foreach( (array) $q['search_terms'] as $term ) {
$term = addslashes_gpc($term); $term = addslashes_gpc($term);
$search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))"; $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
$searchand = ' AND '; $searchand = ' AND ';
@ -1968,7 +1967,7 @@ class WP_Query {
// Tag and slug intersections. // Tag and slug intersections.
$intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag', 'tag__in' => 'post_tag', 'tag_slug__in' => 'post_tag'); $intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag', 'tag__in' => 'post_tag', 'tag_slug__in' => 'post_tag');
$tagin = array('tag__in', 'tag_slug__in'); // These are used to make some exceptions below $tagin = array('tag__in', 'tag_slug__in'); // These are used to make some exceptions below
foreach ($intersections as $item => $taxonomy) { foreach ( $intersections as $item => $taxonomy ) {
if ( empty($q[$item]) ) continue; if ( empty($q[$item]) ) continue;
if ( in_array($item, $tagin) && empty($q['cat']) ) continue; // We should already have what we need if categories aren't being used if ( in_array($item, $tagin) && empty($q['cat']) ) continue; // We should already have what we need if categories aren't being used
@ -2044,43 +2043,43 @@ class WP_Query {
// Author/user stuff // Author/user stuff
if ( empty($q['author']) || ($q['author'] == '0') ) { if ( empty($q['author']) || ($q['author'] == '0') ) {
$whichauthor=''; $whichauthor = '';
} else { } else {
$q['author'] = ''.urldecode($q['author']).''; $q['author'] = (string)urldecode($q['author']);
$q['author'] = addslashes_gpc($q['author']); $q['author'] = addslashes_gpc($q['author']);
if (strpos($q['author'], '-') !== false) { if ( strpos($q['author'], '-') !== false ) {
$eq = '!='; $eq = '!=';
$andor = 'AND'; $andor = 'AND';
$q['author'] = explode('-', $q['author']); $q['author'] = explode('-', $q['author']);
$q['author'] = '' . absint($q['author'][1]); $q['author'] = (string)absint($q['author'][1]);
} else { } else {
$eq = '='; $eq = '=';
$andor = 'OR'; $andor = 'OR';
} }
$author_array = preg_split('/[,\s]+/', $q['author']); $author_array = preg_split('/[,\s]+/', $q['author']);
$whichauthor .= " AND ($wpdb->posts.post_author ".$eq.' '.absint($author_array[0]); $_author_array = array();
for ($i = 1; $i < (count($author_array)); $i = $i + 1) { foreach ( $author_array as $key => $_author )
$whichauthor .= ' '.$andor." $wpdb->posts.post_author ".$eq.' '.absint($author_array[$i]); $_author_array[] = "$wpdb->posts.post_author " . $eq . ' ' . absint($_author);
} $whichauthor .= ' AND (' . implode(" $andor ", $_author_array) . ')';
$whichauthor .= ')'; unset($author_array, $_author_array);
} }
// Author stuff for nice URLs // Author stuff for nice URLs
if ('' != $q['author_name']) { if ( '' != $q['author_name'] ) {
if (strpos($q['author_name'], '/') !== false) { if ( strpos($q['author_name'], '/') !== false ) {
$q['author_name'] = explode('/',$q['author_name']); $q['author_name'] = explode('/', $q['author_name']);
if ($q['author_name'][count($q['author_name'])-1]) { if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
$q['author_name'] = $q['author_name'][count($q['author_name'])-1];#no trailing slash $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; #no trailing slash
} else { } else {
$q['author_name'] = $q['author_name'][count($q['author_name'])-2];#there was a trailling slash $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; #there was a trailling slash
} }
} }
$q['author_name'] = sanitize_title($q['author_name']); $q['author_name'] = sanitize_title($q['author_name']);
$q['author'] = get_user_by('slug', $q['author_name']); $q['author'] = get_user_by('slug', $q['author_name']);
if ( $q['author'] ) if ( $q['author'] )
$q['author'] = $q['author']->ID; $q['author'] = $q['author']->ID;
$whichauthor .= " AND ($wpdb->posts.post_author = ".absint($q['author']).')'; $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
} }
// MIME-Type stuff for attachment browsing // MIME-Type stuff for attachment browsing
@ -2088,14 +2087,14 @@ class WP_Query {
if ( isset($q['post_mime_type']) && '' != $q['post_mime_type'] ) if ( isset($q['post_mime_type']) && '' != $q['post_mime_type'] )
$whichmimetype = wp_post_mime_type_where($q['post_mime_type']); $whichmimetype = wp_post_mime_type_where($q['post_mime_type']);
$where .= $search.$whichcat.$whichauthor.$whichmimetype; $where .= $search . $whichcat . $whichauthor . $whichmimetype;
if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) ) if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
$q['order'] = 'DESC'; $q['order'] = 'DESC';
// Order by // Order by
if ( empty($q['orderby']) ) { if ( empty($q['orderby']) ) {
$q['orderby'] = "$wpdb->posts.post_date ".$q['order']; $q['orderby'] = "$wpdb->posts.post_date " . $q['order'];
} elseif ( 'none' == $q['orderby'] ) { } elseif ( 'none' == $q['orderby'] ) {
$q['orderby'] = ''; $q['orderby'] = '';
} else { } else {
@ -2116,7 +2115,7 @@ class WP_Query {
if ( ! in_array($orderby, $allowed_keys) ) if ( ! in_array($orderby, $allowed_keys) )
continue; continue;
switch ($orderby) { switch ( $orderby ) {
case 'menu_order': case 'menu_order':
break; break;
case 'ID': case 'ID':
@ -2174,7 +2173,7 @@ class WP_Query {
} elseif ( $this->is_attachment ) { } elseif ( $this->is_attachment ) {
$where .= " AND $wpdb->posts.post_type = 'attachment'"; $where .= " AND $wpdb->posts.post_type = 'attachment'";
$post_type_object = get_post_type_object ( 'attachment' ); $post_type_object = get_post_type_object ( 'attachment' );
} elseif ($this->is_page) { } elseif ( $this->is_page ) {
$where .= " AND $wpdb->posts.post_type = 'page'"; $where .= " AND $wpdb->posts.post_type = 'page'";
$post_type_object = get_post_type_object ( 'page' ); $post_type_object = get_post_type_object ( 'page' );
} else { } else {
@ -2298,7 +2297,7 @@ class WP_Query {
if ( empty($q['offset']) ) { if ( empty($q['offset']) ) {
$pgstrt = ''; $pgstrt = '';
$pgstrt = ($page - 1) * $q['posts_per_page'] . ', '; $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
$limits = 'LIMIT '.$pgstrt.$q['posts_per_page']; $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
} else { // we're ignoring $page and using 'offset' } else { // we're ignoring $page and using 'offset'
$q['offset'] = absint($q['offset']); $q['offset'] = absint($q['offset']);
$pgstrt = $q['offset'] . ', '; $pgstrt = $q['offset'] . ', ';
@ -2333,7 +2332,7 @@ class WP_Query {
$post_ids = array(); $post_ids = array();
foreach ($this->comments as $comment) foreach ( $this->comments as $comment )
$post_ids[] = (int) $comment->comment_post_ID; $post_ids[] = (int) $comment->comment_post_ID;
$post_ids = join(',', $post_ids); $post_ids = join(',', $post_ids);
@ -2421,11 +2420,11 @@ class WP_Query {
} else { } else {
if ( $post_status_obj->protected ) { if ( $post_status_obj->protected ) {
// User must have edit permissions on the draft to preview. // User must have edit permissions on the draft to preview.
if (! current_user_can($edit_cap, $this->posts[0]->ID)) { if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) {
$this->posts = array(); $this->posts = array();
} else { } else {
$this->is_preview = true; $this->is_preview = true;
if ('future' != $status) if ( 'future' != $status )
$this->posts[0]->post_date = current_time('mysql'); $this->posts[0]->post_date = current_time('mysql');
} }
} elseif ( $post_status_obj->private ) { } elseif ( $post_status_obj->private ) {
@ -2497,14 +2496,14 @@ class WP_Query {
$this->post_count = count($this->posts); $this->post_count = count($this->posts);
// Sanitize before caching so it'll only get done once // Sanitize before caching so it'll only get done once
for ($i = 0; $i < $this->post_count; $i++) { for ( $i = 0; $i < $this->post_count; $i++ ) {
$this->posts[$i] = sanitize_post($this->posts[$i], 'raw'); $this->posts[$i] = sanitize_post($this->posts[$i], 'raw');
} }
if ( $q['cache_results'] ) if ( $q['cache_results'] )
update_post_caches($this->posts, $post_type); update_post_caches($this->posts, $post_type);
if ($this->post_count > 0) { if ( $this->post_count > 0 ) {
$this->post = $this->posts[0]; $this->post = $this->posts[0];
} }
@ -2561,9 +2560,9 @@ class WP_Query {
* @return bool True if posts are available, false if end of loop. * @return bool True if posts are available, false if end of loop.
*/ */
function have_posts() { function have_posts() {
if ($this->current_post + 1 < $this->post_count) { if ( $this->current_post + 1 < $this->post_count ) {
return true; return true;
} elseif ($this->current_post + 1 == $this->post_count && $this->post_count > 0) { } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
do_action_ref_array('loop_end', array(&$this)); do_action_ref_array('loop_end', array(&$this));
// Do some cleaning up after the loop // Do some cleaning up after the loop
$this->rewind_posts(); $this->rewind_posts();
@ -2581,7 +2580,7 @@ class WP_Query {
*/ */
function rewind_posts() { function rewind_posts() {
$this->current_post = -1; $this->current_post = -1;
if ($this->post_count > 0) { if ( $this->post_count > 0 ) {
$this->post = $this->posts[0]; $this->post = $this->posts[0];
} }
} }
@ -2614,7 +2613,7 @@ class WP_Query {
$comment = $this->next_comment(); $comment = $this->next_comment();
if ($this->current_comment == 0) { if ( $this->current_comment == 0 ) {
do_action('comment_loop_start'); do_action('comment_loop_start');
} }
} }
@ -2630,9 +2629,9 @@ class WP_Query {
* @return bool True, if more comments. False, if no more posts. * @return bool True, if more comments. False, if no more posts.
*/ */
function have_comments() { function have_comments() {
if ($this->current_comment + 1 < $this->comment_count) { if ( $this->current_comment + 1 < $this->comment_count ) {
return true; return true;
} elseif ($this->current_comment + 1 == $this->comment_count) { } elseif ( $this->current_comment + 1 == $this->comment_count ) {
$this->rewind_comments(); $this->rewind_comments();
} }
@ -2647,7 +2646,7 @@ class WP_Query {
*/ */
function rewind_comments() { function rewind_comments() {
$this->current_comment = -1; $this->current_comment = -1;
if ($this->comment_count > 0) { if ( $this->comment_count > 0 ) {
$this->comment = $this->comments[0]; $this->comment = $this->comments[0];
} }
} }
@ -2738,7 +2737,7 @@ class WP_Query {
function get_queried_object_id() { function get_queried_object_id() {
$this->get_queried_object(); $this->get_queried_object();
if (isset($this->queried_object_id)) { if ( isset($this->queried_object_id) ) {
return $this->queried_object_id; return $this->queried_object_id;
} }
@ -2757,7 +2756,7 @@ class WP_Query {
* @return WP_Query * @return WP_Query
*/ */
function WP_Query($query = '') { function WP_Query($query = '') {
if (! empty($query)) { if ( ! empty($query) ) {
$this->query($query); $this->query($query);
} }
} }