Changeset 25554
- Timestamp:
- 09/21/2013 05:54:36 PM (11 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/post.php
r25553 r25554 2071 2071 2072 2072 /** 2073 * Count number of posts of a post type and i suser has permissions to view.2073 * Count number of posts of a post type and if user has permissions to view. 2074 2074 * 2075 2075 * This function provides an efficient method of finding the amount of post's … … 2081 2081 * private posts, it will display that for the user that is signed in. 2082 2082 * 2083 * @link http://codex.wordpress.org/Template_Tags/wp_count_posts 2084 * 2083 2085 * @since 2.5.0 2084 * @link http://codex.wordpress.org/Template_Tags/wp_count_posts2085 2086 * 2086 2087 * @param string $type Optional. Post type to retrieve count … … 2096 2097 $user = wp_get_current_user(); 2097 2098 2098 $cache_key = $type;2099 $cache_key = 'posts-' . $type; 2099 2100 2100 2101 $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s"; … … 2108 2109 $query .= ' GROUP BY post_status'; 2109 2110 2110 $count = wp_cache_get($cache_key, 'counts'); 2111 if ( false !== $count ) 2112 return $count; 2113 2114 $count = $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A ); 2115 2116 $stats = array(); 2117 foreach ( get_post_stati() as $state ) 2118 $stats[$state] = 0; 2119 2120 foreach ( (array) $count as $row ) 2121 $stats[$row['post_status']] = $row['num_posts']; 2122 2123 $stats = (object) $stats; 2124 wp_cache_set($cache_key, $stats, 'counts'); 2125 2126 return $stats; 2111 $counts = wp_cache_get( $cache_key, 'counts' ); 2112 if ( false !== $counts ) { 2113 /** 2114 * Modify returned post counts by status for the current post type. 2115 * 2116 * @since 3.7.0 2117 * 2118 * @param object $counts An object containing the current post_type's post counts by status. 2119 * @param string $type The post type. 2120 * @param string $perm The permission to determine if the posts are 'readable' by the current user. 2121 */ 2122 return apply_filters( 'count_posts', $counts, $type, $perm ); 2123 } 2124 2125 $results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A ); 2126 2127 $counts = array_fill_keys( get_post_stati(), 0 ); 2128 2129 foreach ( $results as $row ) 2130 $counts[ $row['post_status'] ] = $row['num_posts']; 2131 2132 $counts = (object) $counts; 2133 wp_cache_set( $cache_key, $counts, 'counts' ); 2134 2135 //duplicate_hook 2136 return apply_filters( 'count_posts', $counts, $type, $perm ); 2127 2137 } 2128 2138 -
trunk/tests/phpunit/tests/post.php
r25265 r25554 809 809 } 810 810 811 function test_wp_count_posts_filtered() { 812 $post_type = rand_str(20); 813 register_post_type( $post_type ); 814 $this->factory->post->create_many( 10, array( 815 'post_type' => $post_type, 816 'post_author' => $this->author_id 817 ) ); 818 $count1 = wp_count_posts( $post_type, 'readable' ); 819 $this->assertEquals( 10, $count1->publish ); 820 add_filter( 'count_posts', array( $this, 'filter_wp_count_posts' ) ); 821 822 $count2 = wp_count_posts( $post_type, 'readable' ); 823 $this->assertEquals( 7, $count2->publish ); 824 825 remove_filter( 'count_posts', array( $this, 'filter_wp_count_posts' ) ); 826 } 827 828 function filter_wp_count_posts( $counts ) { 829 $counts->publish = 7; 830 return $counts; 831 } 832 811 833 /** 812 834 * @ticket 22074
Note: See TracChangeset
for help on using the changeset viewer.