Skip to:
Content

bbPress.org


Ignore:
Timestamp:
02/23/2017 11:24:29 PM (9 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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 *
Note: See TracChangeset for help on using the changeset viewer.