Skip to:
Content

bbPress.org


Ignore:
Timestamp:
10/19/2012 07:42:49 AM (12 years ago)
Author:
johnjamesjacoby
Message:

Number Formatting:

  • Introduce $integer parameter to template-tags to switch filter for integer usage.
  • Use absint() where appropriate when getting counts.
  • Smarter int type casting.
  • Fixes bug where calculations were being done against formatted strings.
  • Fixes #1974.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bbp-includes/forums/template-tags.php

    r4249 r4258  
    694694
    695695    // Bail if there are no subforums
    696     if ( !bbp_get_forum_subforum_count( $forum_id ) )
     696    if ( !bbp_get_forum_subforum_count( $forum_id, false ) )
    697697        return;
    698698
     
    11171117     */
    11181118    function bbp_get_forum_topics_link( $forum_id = 0 ) {
    1119 
    11201119        $forum    = bbp_get_forum( $forum_id );
    11211120        $forum_id = $forum->ID;
    1122         $topics   = bbp_get_forum_topic_count( $forum_id );
    1123         $topics   = sprintf( _n( '%s topic', '%s topics', $topics, 'bbpress' ), $topics );
     1121        $topics   = sprintf( _n( '%s topic', '%s topics', bbp_get_forum_topic_count( $forum_id, true, false ), 'bbpress' ), bbp_get_forum_topic_count( $forum_id ) );
    11241122        $retval   = '';
    11251123
     
    11571155 * @since bbPress (r2464)
    11581156 *
     1157 * @param int $forum_id Optional. Forum id to check
     1158 * @param boolean $integer Optional. Whether or not to format the result
    11591159 * @uses bbp_get_forum_subforum_count() To get the forum's subforum count
    1160  * @param int $forum_id Optional. Forum id to check
    1161  */
    1162 function bbp_forum_subforum_count( $forum_id = 0 ) {
    1163     echo bbp_get_forum_subforum_count( $forum_id );
     1160 */
     1161function bbp_forum_subforum_count( $forum_id = 0, $integer = false ) {
     1162    echo bbp_get_forum_subforum_count( $forum_id, $integer );
    11641163}
    11651164    /**
     
    11691168     *
    11701169     * @param int $forum_id Optional. Forum id
     1170     * @param boolean $integer Optional. Whether or not to format the result
    11711171     * @uses bbp_get_forum_id() To get the forum id
    11721172     * @uses get_post_meta() To get the subforum count
     
    11751175     * @return int Forum's subforum count
    11761176     */
    1177     function bbp_get_forum_subforum_count( $forum_id = 0 ) {
     1177    function bbp_get_forum_subforum_count( $forum_id = 0, $integer = false ) {
    11781178        $forum_id    = bbp_get_forum_id( $forum_id );
    1179         $forum_count = get_post_meta( $forum_id, '_bbp_forum_subforum_count', true );
    1180 
    1181         return apply_filters( 'bbp_get_forum_subforum_count', (int) $forum_count, $forum_id );
     1179        $forum_count = absint( get_post_meta( $forum_id, '_bbp_forum_subforum_count', true ) );
     1180        $filter      = ( true === $integer ) ? 'bbp_get_forum_subforum_count_int' : 'bbp_get_forum_subforum_count';
     1181
     1182        return apply_filters( $filter, $forum_count, $forum_id );
    11821183    }
    11831184
     
    11891190 * @param int $forum_id Optional. Forum id
    11901191 * @param bool $total_count Optional. To get the total count or normal count?
     1192 * @param boolean $integer Optional. Whether or not to format the result
    11911193 * @uses bbp_get_forum_topic_count() To get the forum topic count
    11921194 */
    1193 function bbp_forum_topic_count( $forum_id = 0, $total_count = true ) {
    1194     echo bbp_get_forum_topic_count( $forum_id, $total_count );
     1195function bbp_forum_topic_count( $forum_id = 0, $total_count = true, $integer = false ) {
     1196    echo bbp_get_forum_topic_count( $forum_id, $total_count, $integer );
    11951197}
    11961198    /**
     
    12021204     * @param bool $total_count Optional. To get the total count or normal
    12031205     *                           count? Defaults to total.
     1206     * @param boolean $integer Optional. Whether or not to format the result
    12041207     * @uses bbp_get_forum_id() To get the forum id
    12051208     * @uses get_post_meta() To get the forum topic count
     
    12081211     * @return int Forum topic count
    12091212     */
    1210     function bbp_get_forum_topic_count( $forum_id = 0, $total_count = true ) {
     1213    function bbp_get_forum_topic_count( $forum_id = 0, $total_count = true, $integer = false ) {
    12111214        $forum_id = bbp_get_forum_id( $forum_id );
    1212         $topics   = get_post_meta( $forum_id, empty( $total_count ) ? '_bbp_topic_count' : '_bbp_total_topic_count', true );
    1213 
    1214         return apply_filters( 'bbp_get_forum_topic_count', (int) $topics, $forum_id );
     1215        $meta_key = empty( $total_count ) ? '_bbp_topic_count' : '_bbp_total_topic_count';
     1216        $topics   = absint( get_post_meta( $forum_id, $meta_key, true ) );
     1217        $filter   = ( true === $integer ) ? 'bbp_get_forum_topic_count_int' : 'bbp_get_forum_topic_count';
     1218
     1219        return apply_filters( $filter, $topics, $forum_id );
    12151220    }
    12161221
     
    12221227 * @param int $forum_id Optional. Forum id
    12231228 * @param bool $total_count Optional. To get the total count or normal count?
     1229 * @param boolean $integer Optional. Whether or not to format the result
    12241230 * @uses bbp_get_forum_reply_count() To get the forum reply count
    12251231 */
    1226 function bbp_forum_reply_count( $forum_id = 0, $total_count = true ) {
    1227     echo bbp_get_forum_reply_count( $forum_id, $total_count );
     1232function bbp_forum_reply_count( $forum_id = 0, $total_count = true, $integer = false ) {
     1233    echo bbp_get_forum_reply_count( $forum_id, $total_count, $integer );
    12281234}
    12291235    /**
     
    12351241     * @param bool $total_count Optional. To get the total count or normal
    12361242     *                           count?
     1243     * @param boolean $integer Optional. Whether or not to format the result
    12371244     * @uses bbp_get_forum_id() To get the forum id
    12381245     * @uses get_post_meta() To get the forum reply count
     
    12411248     * @return int Forum reply count
    12421249     */
    1243     function bbp_get_forum_reply_count( $forum_id = 0, $total_count = true ) {
     1250    function bbp_get_forum_reply_count( $forum_id = 0, $total_count = true, $integer = false ) {
    12441251        $forum_id = bbp_get_forum_id( $forum_id );
    1245         $replies  = get_post_meta( $forum_id, empty( $total_count ) ? '_bbp_reply_count' : '_bbp_total_reply_count', true );
    1246 
    1247         return apply_filters( 'bbp_get_forum_reply_count', (int) $replies, $forum_id );
     1252        $meta_key = empty( $total_count ) ? '_bbp_reply_count' : '_bbp_total_reply_count';
     1253        $replies  = absint( get_post_meta( $forum_id, $meta_key, true ) );
     1254        $filter   = ( true === $integer ) ? 'bbp_get_forum_reply_count_int' : 'bbp_get_forum_reply_count';
     1255
     1256        return apply_filters( $filter, $replies, $forum_id );
    12481257    }
    12491258
     
    12551264 * @param int $forum_id Optional. Forum id
    12561265 * @param bool $total_count Optional. To get the total count or normal count?
     1266 * @param boolean $integer Optional. Whether or not to format the result
    12571267 * @uses bbp_get_forum_post_count() To get the forum post count
    12581268 */
    1259 function bbp_forum_post_count( $forum_id = 0, $total_count = true ) {
    1260     echo bbp_get_forum_post_count( $forum_id, $total_count );
     1269function bbp_forum_post_count( $forum_id = 0, $total_count = true, $integer = false ) {
     1270    echo bbp_get_forum_post_count( $forum_id, $total_count, $integer );
    12611271}
    12621272    /**
     
    12681278     * @param bool $total_count Optional. To get the total count or normal
    12691279     *                           count?
     1280     * @param boolean $integer Optional. Whether or not to format the result
    12701281     * @uses bbp_get_forum_id() To get the forum id
    12711282     * @uses get_post_meta() To get the forum post count
     
    12741285     * @return int Forum post count
    12751286     */
    1276     function bbp_get_forum_post_count( $forum_id = 0, $total_count = true ) {
     1287    function bbp_get_forum_post_count( $forum_id = 0, $total_count = true, $integer = false ) {
    12771288        $forum_id = bbp_get_forum_id( $forum_id );
    1278         $topics   = bbp_get_forum_topic_count( $forum_id, $total_count );
    1279         $replies  = get_post_meta( $forum_id, empty( $total_count ) ? '_bbp_reply_count' : '_bbp_total_reply_count', true );
    1280 
    1281         return apply_filters( 'bbp_get_forum_post_count', (int) $replies + (int) $topics, $forum_id );
     1289        $topics   = bbp_get_forum_topic_count( $forum_id, $total_count, true );
     1290        $meta_key = empty( $total_count ) ? '_bbp_reply_count' : '_bbp_total_reply_count';
     1291        $replies  = absint( get_post_meta( $forum_id, $meta_key, true ) );
     1292        $retval   = $replies + $topics;
     1293        $filter   = ( true === $integer ) ? 'bbp_get_forum_post_count_int' : 'bbp_get_forum_post_count';
     1294
     1295        return apply_filters( $filter, $retval, $forum_id );
    12821296    }
    12831297
     
    12891303 *
    12901304 * @param int $forum_id Optional. Topic id
     1305 * @param boolean $integer Optional. Whether or not to format the result
    12911306 * @uses bbp_get_forum_topic_count_hidden() To get the forum hidden topic count
    12921307 */
    1293 function bbp_forum_topic_count_hidden( $forum_id = 0 ) {
    1294     echo bbp_get_forum_topic_count_hidden( $forum_id );
     1308function bbp_forum_topic_count_hidden( $forum_id = 0, $integer = false ) {
     1309    echo bbp_get_forum_topic_count_hidden( $forum_id, $integer );
    12951310}
    12961311    /**
     
    13011316     *
    13021317     * @param int $forum_id Optional. Topic id
     1318     * @param boolean $integer Optional. Whether or not to format the result
    13031319     * @uses bbp_get_forum_id() To get the forum id
    13041320     * @uses get_post_meta() To get the hidden topic count
     
    13071323     * @return int Topic hidden topic count
    13081324     */
    1309     function bbp_get_forum_topic_count_hidden( $forum_id = 0 ) {
     1325    function bbp_get_forum_topic_count_hidden( $forum_id = 0, $integer = false ) {
    13101326        $forum_id = bbp_get_forum_id( $forum_id );
    1311         $topics   = get_post_meta( $forum_id, '_bbp_topic_count_hidden', true );
    1312 
    1313         return (int) apply_filters( 'bbp_get_forum_topic_count_hidden', (int) $topics, $forum_id );
     1327        $topics   = absint( get_post_meta( $forum_id, '_bbp_topic_count_hidden', true ) );
     1328        $filter   = ( true === $integer ) ? 'bbp_get_forum_topic_count_hidden_int' : 'bbp_get_forum_topic_count_hidden';
     1329
     1330        return apply_filters( $filter, $topics, $forum_id );
    13141331    }
    13151332
     
    18221839
    18231840        // Get some forum data
     1841        $tc_int      = bbp_get_forum_topic_count( $forum_id, false );
     1842        $rc_int      = bbp_get_forum_reply_count( $forum_id, false );
    18241843        $topic_count = bbp_get_forum_topic_count( $forum_id );
    18251844        $reply_count = bbp_get_forum_reply_count( $forum_id );
     
    18281847        // Has replies
    18291848        if ( !empty( $reply_count ) ) {
    1830             $reply_text = sprintf( _n( '%s reply', '%s replies', $reply_count, 'bbpress' ), $reply_count );
     1849            $reply_text = sprintf( _n( '%s reply', '%s replies', $rc_int, 'bbpress' ), $reply_count );
    18311850        }
    18321851
     
    18391858        // Forum has no last active data
    18401859        } else {
    1841             $topic_text      = sprintf( _n( '%s topic', '%s topics', $topic_count, 'bbpress' ), $topic_count );
     1860            $topic_text      = sprintf( _n( '%s topic', '%s topics', $tc_int, 'bbpress' ), $topic_count );
    18421861        }
    18431862
Note: See TracChangeset for help on using the changeset viewer.