Make WordPress Core


Ignore:
Timestamp:
05/22/2014 06:33:34 PM (12 years ago)
Author:
wonderboymusic
Message:

Because PHP can be configured without --filter, it is not 100% safe to use filter_var(). This is problematic for casting "false" to false, as PHP always casts it to true. FILTER_VALIDATE_BOOLEAN fixes this, but it may not be available.

Add a new function, wp_validate_boolean(), to replace filter_var( $var, FILTER_VALIDATE_BOOLEAN ).

Fixes #28170.

File:
1 edited

Legend:

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

    r28426 r28542  
    44464446    mbstring_binary_safe_encoding( true );
    44474447}
     4448
     4449/**
     4450 * Alternative to filter_var( $var, FILTER_VALIDATE_BOOLEAN )
     4451 *
     4452 * @since 4.0.0
     4453 *
     4454 * @param mixed $var
     4455 * @return boolean
     4456 */
     4457function wp_validate_boolean( $var ) {
     4458    if ( is_bool( $var ) ) {
     4459        return $var;
     4460    }
     4461
     4462    if ( 'false' === $var ) {
     4463        return false;
     4464    }
     4465
     4466    return (bool) $var;
     4467}
Note: See TracChangeset for help on using the changeset viewer.