From 132ac51a5aca03314a5adab1c675e75198157f76 Mon Sep 17 00:00:00 2001 From: ryan Date: Wed, 9 Apr 2008 20:09:45 +0000 Subject: [PATCH] Use wp_count_posts() to determine availabel statuses for get_available_post_statuses(). fixes #6654 for trunk git-svn-id: http://svn.automattic.com/wordpress/trunk@7638 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/post.php | 5 ++--- wp-includes/post.php | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/wp-admin/includes/post.php b/wp-admin/includes/post.php index ffc5b119e..aa1e833bb 100644 --- a/wp-admin/includes/post.php +++ b/wp-admin/includes/post.php @@ -506,10 +506,9 @@ function _relocate_children( $old_ID, $new_ID ) { } function get_available_post_statuses($type = 'post') { - global $wpdb; + $stati = wp_count_posts($type); - $stati = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_status FROM $wpdb->posts WHERE post_type = %s", $type)); - return $stati; + return array_keys(get_object_vars($stati)); } function wp_edit_posts_query( $q = false ) { diff --git a/wp-includes/post.php b/wp-includes/post.php index 628ea10f1..e43bd1592 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -828,12 +828,23 @@ function wp_count_posts( $type = 'post', $perm = '' ) { $user = wp_get_current_user(); + $cache_key = $type; + if ( !empty($perm) ) + $cache_key .= '_' . $perm; + $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s"; if ( 'readable' == $perm && is_user_logged_in() ) { - if ( !current_user_can("read_private_{$type}s") ) + if ( !current_user_can("read_private_{$type}s") ) { + $cache_key .= '_' . $user->ID; $query .= " AND (post_status != 'private' OR ( post_author = '$user->ID' AND post_status = 'private' ))"; + } } $query .= ' GROUP BY post_status'; + + $count = wp_cache_get($cache_key, 'counts'); + if ( false !== $count ) + return $count; + $count = $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A ); $stats = array( ); @@ -841,9 +852,13 @@ function wp_count_posts( $type = 'post', $perm = '' ) { $stats[$row['post_status']] = $row['num_posts']; } - return (object) $stats; + $stats = (object) $stats; + wp_cache_set($cache_key, $stats, 'counts'); + + return $stats; } + /** * wp_count_attachments() - Count number of attachments *