Make WordPress Core

Ticket #16603: 16603.2.diff

File 16603.2.diff, 2.6 KB (added by DrewAPicture, 11 years ago)

filter docs

  • src/wp-includes/post.php

     
    20672067}
    20682068
    20692069/**
    2070  * Count number of posts of a post type and is user has permissions to view.
     2070 * Count number of posts of a post type and if user has permissions to view.
    20712071 *
    20722072 * This function provides an efficient method of finding the amount of post's
    20732073 * type a blog has. Another method is to count the amount of items in
     
    20772077 * The $perm parameter checks for 'readable' value and if the user can read
    20782078 * private posts, it will display that for the user that is signed in.
    20792079 *
    2080  * @since 2.5.0
    20812080 * @link http://codex.wordpress.org/Template_Tags/wp_count_posts
    20822081 *
     2082 * @since 2.5.0
     2083 *
    20832084 * @param string $type Optional. Post type to retrieve count
    20842085 * @param string $perm Optional. 'readable' or empty.
    20852086 * @return object Number of posts for each status
     
    20922093
    20932094        $user = wp_get_current_user();
    20942095
    2095         $cache_key = $type;
     2096        $cache_key = 'posts-' . $type;
    20962097
    20972098        $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s";
    20982099        if ( 'readable' == $perm && is_user_logged_in() ) {
     
    21042105        }
    21052106        $query .= ' GROUP BY post_status';
    21062107
    2107         $count = wp_cache_get($cache_key, 'counts');
    2108         if ( false !== $count )
    2109                 return $count;
     2108        $counts = wp_cache_get( $cache_key, 'counts' );
     2109        if ( false !== $counts ) {
     2110                /**
     2111                 * Modify returned post counts by status for the current post type.
     2112                 *
     2113                 * @since 3.7.0
     2114                 *
     2115                 * @param object $counts An object containing the current post_type's post counts by status.
     2116                 * @param string $type   The post type.
     2117                 * @param string $perm   The permission to determine if the posts are 'readable' by the current user.
     2118                 */
     2119                return apply_filters( 'count_posts', $counts, $type, $perm );
     2120        }
    21102121
    2111         $count = $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A );
     2122        $results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A );
    21122123
    2113         $stats = array();
    2114         foreach ( get_post_stati() as $state )
    2115                 $stats[$state] = 0;
     2124        $counts = array_fill_keys( get_post_stati(), 0 );
    21162125
    2117         foreach ( (array) $count as $row )
    2118                 $stats[$row['post_status']] = $row['num_posts'];
     2126        foreach ( $results as $row )
     2127                $counts[ $row['post_status'] ] = $row['num_posts'];
    21192128
    2120         $stats = (object) $stats;
    2121         wp_cache_set($cache_key, $stats, 'counts');
     2129        $counts = (object) $counts;
     2130        wp_cache_set( $cache_key, $counts, 'counts' );
    21222131
    2123         return $stats;
     2132        //duplicate_hook
     2133        return apply_filters( 'count_posts', $counts, $type, $perm );
    21242134}
    21252135
    21262136/**