Make WordPress Core


Ignore:
Timestamp:
09/21/2013 05:54:36 PM (11 years ago)
Author:
wonderboymusic
Message:

Add hooks to wp_count_posts(). Adds filter docs. Adds unit test to test count_posts filter.

Props nacin, DrewAPicture.
Fixes #16603.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r25553 r25554  
    20712071
    20722072/**
    2073  * Count number of posts of a post type and is user has permissions to view.
     2073 * Count number of posts of a post type and if user has permissions to view.
    20742074 *
    20752075 * This function provides an efficient method of finding the amount of post's
     
    20812081 * private posts, it will display that for the user that is signed in.
    20822082 *
     2083 * @link http://codex.wordpress.org/Template_Tags/wp_count_posts
     2084 *
    20832085 * @since 2.5.0
    2084  * @link http://codex.wordpress.org/Template_Tags/wp_count_posts
    20852086 *
    20862087 * @param string $type Optional. Post type to retrieve count
     
    20962097    $user = wp_get_current_user();
    20972098
    2098     $cache_key = $type;
     2099    $cache_key = 'posts-' . $type;
    20992100
    21002101    $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s";
     
    21082109    $query .= ' GROUP BY post_status';
    21092110
    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 );
    21272137}
    21282138
Note: See TracChangeset for help on using the changeset viewer.