Skip to:
Content

bbPress.org

Changeset 6302


Ignore:
Timestamp:
02/23/2017 11:24:29 PM (8 years ago)
Author:
johnjamesjacoby
Message:

Common: Introduce bbp_number_not_negative() and use it in the following ways:

  • Register the relevant meta-data keys for posts & users, so updated values can never be invalid
  • Filter return values for existing database values that might be invalid on existing installs
  • Use in place of intval() or (int) casts where negative values should not exist

This has the added benefit of introducing the bbp_register_meta hook, for future meta-data registrations (of which bbPress has much of.) We'll concentrate on counts for 2.6, and integrate IDs and timestamps in future releases.

See #3059.

Location:
trunk/src
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bbpress.php

    r6254 r6302  
    390390            'setup_current_user',       // Setup currently logged in user
    391391            'roles_init',               // User roles init
     392            'register_meta',            // Register meta (forum|topic|reply|user)
    392393            'register_post_types',      // Register post types (forum|topic|reply)
    393394            'register_post_statuses',   // Register post statuses (closed|spam|orphan|hidden)
     
    750751
    751752    /**
     753     * Register bbPress meta-data
     754     *
     755     * Counts added in 2.6.0 to avoid negative values
     756     *
     757     * @since 2.6.0 bbPress (r6300)
     758     */
     759    public function register_meta() {
     760
     761        // Define "count" meta-type array
     762        $count = array(
     763            'type'              => 'integer',
     764            'description'       => esc_html__( 'bbPress Item Count', 'bbpress' ),
     765            'single'            => true,
     766            'sanitize_callback' => 'bbp_number_not_negative',
     767            'show_in_rest'      => true
     768        );
     769
     770        /** Post **************************************************************/
     771
     772        // Counts
     773        register_meta( 'post', '_bbp_topic_count',           $count );
     774        register_meta( 'post', '_bbp_reply_count',           $count );
     775        register_meta( 'post', '_bbp_total_topic_count',     $count );
     776        register_meta( 'post', '_bbp_total_reply_count',     $count );
     777        register_meta( 'post', '_bbp_voice_count',           $count );
     778        register_meta( 'post', '_bbp_anonymous_reply_count', $count );
     779        register_meta( 'post', '_bbp_topic_count_hidden',    $count );
     780        register_meta( 'post', '_bbp_reply_count_hidden',    $count );
     781        register_meta( 'post', '_bbp_forum_subforum_count',  $count );
     782
     783        /* User ***************************************************************/
     784
     785        // Counts
     786        register_meta( 'user', '_bbp_topic_count', $count );
     787        register_meta( 'user', '_bbp_reply_count', $count );
     788    }
     789
     790    /**
    752791     * Setup the currently logged-in user
    753792     *
  • trunk/src/includes/common/formatting.php

    r6298 r6302  
    463463
    464464/**
     465 * Never let a numeric value be less than zero.
     466 *
     467 * @since 2.6.0 bbPress (r6300)
     468 *
     469 * @param int $number
     470 */
     471function bbp_number_not_negative( $number = 0 ) {
     472
     473    // Protect against formatted strings
     474    if ( is_string( $number ) ) {
     475        $number = strip_tags( $number );                    // No HTML
     476        $number = preg_replace( '/[^0-9-]/', '', $number ); // No number-format
     477
     478    // Protect against objects, arrays, scalars, etc...
     479    } elseif ( ! is_numeric( $number ) ) {
     480        $number = 0;
     481    }
     482
     483    // Make the number an integer
     484    $int = intval( $number );
     485
     486    // Pick the maximum value, never less than zero
     487    $not_less_than_zero = max( 0, $int );
     488
     489    // Filter & return
     490    return apply_filters( 'bbp_number_not_negative', $not_less_than_zero, $int, $number );
     491}
     492
     493/**
    465494 * A bbPress specific method of formatting numeric values
    466495 *
  • trunk/src/includes/core/actions.php

    r6118 r6302  
    113113add_action( 'bbp_register', 'bbp_register_views',          8  );
    114114add_action( 'bbp_register', 'bbp_register_shortcodes',     10 );
     115add_action( 'bbp_register', 'bbp_register_meta',           12 );
    115116
    116117// Autoembeds
  • trunk/src/includes/core/filters.php

    r6301 r6302  
    207207add_filter( 'bbp_get_topic_reply_count_hidden', 'bbp_number_format', 10 );
    208208
    209 // Add intval filter to functions expecting absolute values
    210 add_filter( 'bbp_get_user_topic_count_int',         'intval', 10 );
    211 add_filter( 'bbp_get_user_reply_count_int',         'intval', 10 );
    212 add_filter( 'bbp_get_user_post_count_int',          'intval', 10 );
    213 add_filter( 'bbp_get_forum_subforum_count_int',     'intval', 10 );
    214 add_filter( 'bbp_get_forum_topic_count_int',        'intval', 10 );
    215 add_filter( 'bbp_get_forum_reply_count_int',        'intval', 10 );
    216 add_filter( 'bbp_get_forum_post_count_int',         'intval', 10 );
    217 add_filter( 'bbp_get_topic_voice_count_int',        'intval', 10 );
    218 add_filter( 'bbp_get_topic_reply_count_int',        'intval', 10 );
    219 add_filter( 'bbp_get_topic_post_count_int',         'intval', 10 );
    220 add_filter( 'bbp_get_forum_topic_count_hidden_int', 'intval', 10 );
    221 add_filter( 'bbp_get_topic_reply_count_hidden_int', 'intval', 10 );
    222 add_filter( 'bbp_get_topic_revision_count_int',     'intval', 10 );
    223 add_filter( 'bbp_get_reply_revision_count_int',     'intval', 10 );
     209// Add number-not-negative filter to values that can never be negative numbers
     210add_filter( 'bbp_get_user_topic_count_int',         'bbp_number_not_negative', 10 );
     211add_filter( 'bbp_get_user_reply_count_int',         'bbp_number_not_negative', 10 );
     212add_filter( 'bbp_get_user_post_count_int',          'bbp_number_not_negative', 10 );
     213add_filter( 'bbp_get_forum_subforum_count_int',     'bbp_number_not_negative', 10 );
     214add_filter( 'bbp_get_forum_topic_count_int',        'bbp_number_not_negative', 10 );
     215add_filter( 'bbp_get_forum_reply_count_int',        'bbp_number_not_negative', 10 );
     216add_filter( 'bbp_get_forum_post_count_int',         'bbp_number_not_negative', 10 );
     217add_filter( 'bbp_get_topic_voice_count_int',        'bbp_number_not_negative', 10 );
     218add_filter( 'bbp_get_topic_reply_count_int',        'bbp_number_not_negative', 10 );
     219add_filter( 'bbp_get_topic_post_count_int',         'bbp_number_not_negative', 10 );
     220add_filter( 'bbp_get_forum_topic_count_hidden_int', 'bbp_number_not_negative', 10 );
     221add_filter( 'bbp_get_topic_reply_count_hidden_int', 'bbp_number_not_negative', 10 );
     222add_filter( 'bbp_get_topic_revision_count_int',     'bbp_number_not_negative', 10 );
     223add_filter( 'bbp_get_reply_revision_count_int',     'bbp_number_not_negative', 10 );
    224224
    225225// Sanitize displayed user data
  • trunk/src/includes/core/sub-actions.php

    r6106 r6302  
    240240function bbp_register_shortcodes() {
    241241    do_action( 'bbp_register_shortcodes' );
     242}
     243
     244/**
     245 * Register the default bbPress meta-data
     246 *
     247 * @since 2.6.0 bbPress (r46300)
     248 *
     249 * @uses do_action() Calls 'bbp_register_meta'
     250 */
     251function bbp_register_meta() {
     252    do_action( 'bbp_register_meta' );
    242253}
    243254
  • trunk/src/includes/forums/template.php

    r6296 r6302  
    13261326    function bbp_get_forum_subforum_count( $forum_id = 0, $integer = false ) {
    13271327        $forum_id    = bbp_get_forum_id( $forum_id );
    1328         $forum_count = (int) get_post_meta( $forum_id, '_bbp_forum_subforum_count', true );
    1329         $filter      = ( true === $integer ) ? 'bbp_get_forum_subforum_count_int' : 'bbp_get_forum_subforum_count';
     1328        $forum_count = bbp_number_not_negative( get_post_meta( $forum_id, '_bbp_forum_subforum_count', true ) );
     1329        $filter      = ( true === $integer )
     1330            ? 'bbp_get_forum_subforum_count_int'
     1331            : 'bbp_get_forum_subforum_count';
    13301332
    13311333        return apply_filters( $filter, $forum_count, $forum_id );
     
    13631365        $forum_id = bbp_get_forum_id( $forum_id );
    13641366        $meta_key = empty( $total_count ) ? '_bbp_topic_count' : '_bbp_total_topic_count';
    1365         $topics   = (int) get_post_meta( $forum_id, $meta_key, true );
    1366         $filter   = ( true === $integer ) ? 'bbp_get_forum_topic_count_int' : 'bbp_get_forum_topic_count';
     1367        $topics   = bbp_number_not_negative( get_post_meta( $forum_id, $meta_key, true ) );
     1368        $filter   = ( true === $integer )
     1369            ? 'bbp_get_forum_topic_count_int'
     1370            : 'bbp_get_forum_topic_count';
    13671371
    13681372        return apply_filters( $filter, $topics, $forum_id );
     
    14001404        $forum_id = bbp_get_forum_id( $forum_id );
    14011405        $meta_key = empty( $total_count ) ? '_bbp_reply_count' : '_bbp_total_reply_count';
    1402         $replies  = (int) get_post_meta( $forum_id, $meta_key, true );
    1403         $filter   = ( true === $integer ) ? 'bbp_get_forum_reply_count_int' : 'bbp_get_forum_reply_count';
     1406        $replies  = bbp_number_not_negative( get_post_meta( $forum_id, $meta_key, true ) );
     1407        $filter   = ( true === $integer )
     1408            ? 'bbp_get_forum_reply_count_int'
     1409            : 'bbp_get_forum_reply_count';
    14041410
    14051411        return apply_filters( $filter, $replies, $forum_id );
     
    14391445        $topics   = bbp_get_forum_topic_count( $forum_id, $total_count, true );
    14401446        $replies  = bbp_get_forum_reply_count( $forum_id, $total_count, true );
    1441         $retval   = $replies + $topics;
     1447        $retval   = bbp_number_not_negative( $replies + $topics );
    14421448        $filter   = ( true === $integer )
    14431449                ? 'bbp_get_forum_post_count_int'
     
    14761482    function bbp_get_forum_topic_count_hidden( $forum_id = 0, $integer = false ) {
    14771483        $forum_id = bbp_get_forum_id( $forum_id );
    1478         $topics   = (int) get_post_meta( $forum_id, '_bbp_topic_count_hidden', true );
    1479         $filter   = ( true === $integer ) ? 'bbp_get_forum_topic_count_hidden_int' : 'bbp_get_forum_topic_count_hidden';
     1484        $topics   = bbp_number_not_negative( get_post_meta( $forum_id, '_bbp_topic_count_hidden', true ) );
     1485        $filter   = ( true === $integer )
     1486            ? 'bbp_get_forum_topic_count_hidden_int'
     1487            : 'bbp_get_forum_topic_count_hidden';
    14801488
    14811489        return apply_filters( $filter, $topics, $forum_id );
  • trunk/src/includes/replies/template.php

    r6300 r6302  
    878878 */
    879879function bbp_get_reply_revision_count( $reply_id = 0, $integer = false ) {
    880     $count  = (int) count( bbp_get_reply_revisions( $reply_id ) );
    881     $filter = ( true === $integer ) ? 'bbp_get_reply_revision_count_int' : 'bbp_get_reply_revision_count';
     880    $reply_id = bbp_get_reply_id( $reply_id );
     881    $count    = bbp_number_not_negative( count( bbp_get_reply_revisions( $reply_id ) ) );
     882    $filter   = ( true === $integer )
     883        ? 'bbp_get_reply_revision_count_int'
     884        : 'bbp_get_reply_revision_count';
    882885
    883886    return apply_filters( $filter, $count, $reply_id );
     
    922925 */
    923926function bbp_is_reply_published( $reply_id = 0 ) {
    924     $reply_status = bbp_get_reply_status( bbp_get_reply_id( $reply_id ) ) === bbp_get_public_status_id();
     927    $reply_id     = bbp_get_reply_id( $reply_id );
     928    $status       = bbp_get_public_status_id();
     929    $reply_status = bbp_get_reply_status( $reply_id ) === $status;
     930
    925931    return (bool) apply_filters( 'bbp_is_reply_published', (bool) $reply_status, $reply_id );
    926932}
     
    937943 */
    938944function bbp_is_reply_spam( $reply_id = 0 ) {
    939     $reply_status = bbp_get_reply_status( bbp_get_reply_id( $reply_id ) ) === bbp_get_spam_status_id();
     945    $reply_id     = bbp_get_reply_id( $reply_id );
     946    $status       = bbp_get_spam_status_id();
     947    $reply_status = bbp_get_reply_status( $reply_id ) === $status;
     948
    940949    return (bool) apply_filters( 'bbp_is_reply_spam', (bool) $reply_status, $reply_id );
    941950}
     
    952961 */
    953962function bbp_is_reply_trash( $reply_id = 0 ) {
    954     $reply_status = bbp_get_reply_status( bbp_get_reply_id( $reply_id ) ) === bbp_get_trash_status_id();
     963    $reply_id     = bbp_get_reply_id( $reply_id );
     964    $status       = bbp_get_trash_status_id();
     965    $reply_status = bbp_get_reply_status( $reply_id ) === $status;
     966
    955967    return (bool) apply_filters( 'bbp_is_reply_trash', (bool) $reply_status, $reply_id );
    956968}
     
    968980 */
    969981function bbp_is_reply_pending( $reply_id = 0 ) {
    970     $reply_status = bbp_get_reply_status( bbp_get_reply_id( $reply_id ) ) === bbp_get_pending_status_id();
     982    $reply_id     = bbp_get_reply_id( $reply_id );
     983    $status       = bbp_get_pending_status_id();
     984    $reply_status = bbp_get_reply_status( $reply_id ) === $status;
     985
    971986    return (bool) apply_filters( 'bbp_is_reply_pending', (bool) $reply_status, $reply_id );
    972987}
     
    984999 */
    9851000function bbp_is_reply_private( $reply_id = 0 ) {
    986     $reply_status = bbp_get_reply_status( bbp_get_reply_id( $reply_id ) ) === bbp_get_private_status_id();
     1001    $reply_id     = bbp_get_reply_id( $reply_id );
     1002    $status       = bbp_get_private_status_id();
     1003    $reply_status = bbp_get_reply_status( $reply_id ) === $status;
     1004
    9871005    return (bool) apply_filters( 'bbp_is_reply_private', (bool) $reply_status, $reply_id );
    9881006}
  • trunk/src/includes/topics/template.php

    r6291 r6302  
    10701070 */
    10711071function bbp_get_topic_revision_count( $topic_id = 0, $integer = false ) {
    1072     $count  = (int) count( bbp_get_topic_revisions( $topic_id ) );
    1073     $filter = ( true === $integer ) ? 'bbp_get_topic_revision_count_int' : 'bbp_get_topic_revision_count';
     1072    $topic_id = bbp_get_topic_id( $topic_id );
     1073    $count    = bbp_number_not_negative( count( bbp_get_topic_revisions( $topic_id ) ) );
     1074    $filter   = ( true === $integer )
     1075        ? 'bbp_get_topic_revision_count_int'
     1076        : 'bbp_get_topic_revision_count';
    10741077
    10751078    return apply_filters( $filter, $count, $topic_id );
     
    22582261    function bbp_get_topic_reply_count( $topic_id = 0, $integer = false ) {
    22592262        $topic_id = bbp_get_topic_id( $topic_id );
    2260         $replies  = (int) get_post_meta( $topic_id, '_bbp_reply_count', true );
    2261         $filter   = ( true === $integer ) ? 'bbp_get_topic_reply_count_int' : 'bbp_get_topic_reply_count';
     2263        $replies  = bbp_number_not_negative( get_post_meta( $topic_id, '_bbp_reply_count', true ) );
     2264        $filter   = ( true === $integer )
     2265            ? 'bbp_get_topic_reply_count_int'
     2266            : 'bbp_get_topic_reply_count';
    22622267
    22632268        return apply_filters( $filter, $replies, $topic_id );
     
    22912296    function bbp_get_topic_post_count( $topic_id = 0, $integer = false ) {
    22922297        $topic_id = bbp_get_topic_id( $topic_id );
    2293         $replies  = (int) get_post_meta( $topic_id, '_bbp_reply_count', true ) + 1;
    2294         $filter   = ( true === $integer ) ? 'bbp_get_topic_post_count_int' : 'bbp_get_topic_post_count';
     2298        $replies  = bbp_number_not_negative( get_post_meta( $topic_id, '_bbp_reply_count', true ) + 1 );
     2299        $filter   = ( true === $integer )
     2300            ? 'bbp_get_topic_post_count_int'
     2301            : 'bbp_get_topic_post_count';
    22952302
    22962303        return apply_filters( $filter, $replies, $topic_id );
     
    23262333    function bbp_get_topic_reply_count_hidden( $topic_id = 0, $integer = false ) {
    23272334        $topic_id = bbp_get_topic_id( $topic_id );
    2328         $replies  = (int) get_post_meta( $topic_id, '_bbp_reply_count_hidden', true );
    2329         $filter   = ( true === $integer ) ? 'bbp_get_topic_reply_count_hidden_int' : 'bbp_get_topic_reply_count_hidden';
     2335        $replies  = bbp_number_not_negative( get_post_meta( $topic_id, '_bbp_reply_count_hidden', true ) );
     2336        $filter   = ( true === $integer )
     2337            ? 'bbp_get_topic_reply_count_hidden_int'
     2338            : 'bbp_get_topic_reply_count_hidden';
    23302339
    23312340        return apply_filters( $filter, $replies, $topic_id );
     
    23572366    function bbp_get_topic_voice_count( $topic_id = 0, $integer = false ) {
    23582367        $topic_id = bbp_get_topic_id( $topic_id );
    2359         $voices   = (int) get_post_meta( $topic_id, '_bbp_voice_count', true );
    2360         $filter   = ( true === $integer ) ? 'bbp_get_topic_voice_count_int' : 'bbp_get_topic_voice_count';
     2368        $voices   = bbp_number_not_negative( get_post_meta( $topic_id, '_bbp_voice_count', true ) );
     2369        $filter   = ( true === $integer )
     2370            ? 'bbp_get_topic_voice_count_int'
     2371            : 'bbp_get_topic_voice_count';
    23612372
    23622373        return apply_filters( $filter, $voices, $topic_id );
  • trunk/src/includes/users/options.php

    r5951 r6302  
    215215     */
    216216    function bbp_get_user_topic_count( $user_id = 0, $integer = false ) {
    217  
     217
    218218        // Validate user id
    219219        $user_id = bbp_get_user_id( $user_id );
     
    222222        }
    223223
    224         $count  = (int) get_user_option( '_bbp_topic_count', $user_id );
    225         $filter = ( true === $integer ) ? 'bbp_get_user_topic_count_int' : 'bbp_get_user_topic_count';
     224        $count  = bbp_number_not_negative( get_user_option( '_bbp_topic_count', $user_id ) );
     225        $filter = ( true === $integer )
     226            ? 'bbp_get_user_topic_count_int'
     227            : 'bbp_get_user_topic_count';
    226228
    227229        return apply_filters( $filter, $count, $user_id );
     
    261263        }
    262264
    263         $count  = (int) get_user_option( '_bbp_reply_count', $user_id );
    264         $filter = ( true === $integer ) ? 'bbp_get_user_reply_count_int' : 'bbp_get_user_reply_count';
     265        $count  = bbp_number_not_negative( get_user_option( '_bbp_reply_count', $user_id ) );
     266        $filter = ( true === $integer )
     267            ? 'bbp_get_user_reply_count_int'
     268            : 'bbp_get_user_reply_count';
    265269
    266270        return apply_filters( $filter, $count, $user_id );
     
    302306        $topics  = bbp_get_user_topic_count( $user_id, true );
    303307        $replies = bbp_get_user_reply_count( $user_id, true );
    304         $count   = (int) $topics + $replies;
    305         $filter  = ( true === $integer ) ? 'bbp_get_user_post_count_int' : 'bbp_get_user_post_count';
     308        $count   = bbp_number_not_negative( $topics + $replies );
     309        $filter  = ( true === $integer )
     310            ? 'bbp_get_user_post_count_int'
     311            : 'bbp_get_user_post_count';
    306312
    307313        return apply_filters( $filter, $count, $user_id );
Note: See TracChangeset for help on using the changeset viewer.