Skip to content

Commit

Permalink
Move helper methods to utility class
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfnl committed Apr 28, 2015
1 parent e5bf0ed commit 516c3b8
Showing 1 changed file with 105 additions and 77 deletions.
182 changes: 105 additions & 77 deletions class-tgm-plugin-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -969,9 +969,9 @@ public function register( $plugin ) {

// Forgive users for using string versions of booleans or floats for version nr
$plugin['version'] = (string) $plugin['version'];
$plugin['required'] = $this->validate_bool( $plugin['required'] );
$plugin['force_activation'] = $this->validate_bool( $plugin['force_activation'] );
$plugin['force_deactivation'] = $this->validate_bool( $plugin['force_deactivation'] );
$plugin['required'] = TGM_Utils::validate_bool( $plugin['required'] );
$plugin['force_activation'] = TGM_Utils::validate_bool( $plugin['force_activation'] );
$plugin['force_deactivation'] = TGM_Utils::validate_bool( $plugin['force_deactivation'] );

// Set the class properties
$this->plugins[ $plugin['slug'] ] = $plugin;
Expand Down Expand Up @@ -1210,79 +1210,6 @@ public function force_deactivation() {

}

/**
* Helper function: Validate a value as boolean
*
* @since 2.5.0
*
* @param mixed $value
*
* @return bool
*/
public function validate_bool( $value ) {
static $has_filters;

if ( ! isset( $has_filters ) ) {
$has_filters = extension_loaded( 'filter' );
}

if ( $has_filters ) {
return filter_var( $value, FILTER_VALIDATE_BOOLEAN );
}
else {
return $this->emulate_filter_bool( $value );
}
}

/**
* Helper function: Cast a value to bool
*
* @since 2.5.0
*
* @param mixed $value Value to cast
*
* @return bool
*/
protected function emulate_filter_bool( $value ) {
static $true = array(
'1',
'true', 'True', 'TRUE',
'y', 'Y',
'yes', 'Yes', 'YES',
'on', 'On', 'On',
);
static $false = array(
'0',
'false', 'False', 'FALSE',
'n', 'N', 'no',
'No', 'NO',
'off', 'Off', 'OFF',
);

if ( is_bool( $value ) ) {
return $value;
}
else if ( is_int( $value ) && ( $value === 0 || $value === 1 ) ) {
return (bool) $value;
}
else if ( ( is_float( $value ) && ! is_nan( $value ) ) && ( $value === (float) 0 || $value === (float) 1 ) ) {
return (bool) $value;
}
else if ( is_string( $value ) ) {
$value = trim( $value );
if ( in_array( $value, $true, true ) ) {
return true;
}
else if ( in_array( $value, $false, true ) ) {
return false;
}
else {
return false;
}
}
return false;
}

/**
* Returns the singleton instance of the class.
*
Expand Down Expand Up @@ -1698,7 +1625,7 @@ public function process_bulk_actions() {
// Received via Filesystem page - unflatten array (WP bug #19643)
$plugins_to_install = explode( ',', $_POST['plugin'] );
}

// Sanitize the received input
$plugins_to_install = array_map( 'sanitize_key', $plugins_to_install );

Expand Down Expand Up @@ -2490,3 +2417,104 @@ public function after_flush_output() {

}
}


if ( ! class_exists( 'TGM_Utils' ) ) {
/**
* Generic utilities for TGMPA.
*
* All methods are static, poor-dev namespacing class wrapper.
*
* @since 2.5.0
*
* @package TGM-Plugin-Activation
* @author Juliette Reinders Folmer
*/
class TGM_Utils {

/**
* @var bool $has_filters Whether the PHP filter extension is enabled
* @static
*/
public static $has_filters;

/**
* Helper function: Validate a value as boolean
*
* @since 2.5.0
*
* @static
*
* @param mixed $value
*
* @return bool
*/
public static function validate_bool( $value ) {
if ( ! isset( self::$has_filters ) ) {
self::$has_filters = extension_loaded( 'filter' );
}

if ( self::$has_filters ) {
return filter_var( $value, FILTER_VALIDATE_BOOLEAN );

} else {
return self::emulate_filter_bool( $value );
}
}

/**
* Helper function: Cast a value to bool
*
* @since 2.5.0
*
* @static
*
* @param mixed $value Value to cast
*
* @return bool
*/
protected static function emulate_filter_bool( $value ) {

static $true = array(
'1',
'true', 'True', 'TRUE',
'y', 'Y',
'yes', 'Yes', 'YES',
'on', 'On', 'On',

This comment has been minimized.

Copy link
@GaryJones

GaryJones Apr 28, 2015

Member

Shouldn't the last one here be 'ON'?

);
static $false = array(
'0',
'false', 'False', 'FALSE',
'n', 'N', 'no',

This comment has been minimized.

Copy link
@GaryJones

GaryJones Apr 28, 2015

Member

Final 'no' here should go on to the start of the next like so it's consistent with the array above.

'No', 'NO',
'off', 'Off', 'OFF',
);

if ( is_bool( $value ) ) {
return $value;

} else if ( is_int( $value ) && ( 0 === $value || 1 === $value ) ) {
return (bool) $value;

} else if ( ( is_float( $value ) && ! is_nan( $value ) ) && ( (float) 0 === $value || (float) 1 === $value ) ) {
return (bool) $value;

} else if ( is_string( $value ) ) {

$value = trim( $value );
if ( in_array( $value, $true, true ) ) {
return true;

} else if ( in_array( $value, $false, true ) ) {
return false;

} else {
return false;
}
}
return false;
}

} // End of class TGM_Utils

} // End of class_exists wrapper

0 comments on commit 516c3b8

Please sign in to comment.