-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move helper methods to utility class
- Loading branch information
Showing
1 changed file
with
105 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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. | ||
* | ||
|
@@ -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 ); | ||
|
||
|
@@ -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.
Sorry, something went wrong. |
||
); | ||
static $false = array( | ||
'0', | ||
'false', 'False', 'FALSE', | ||
'n', 'N', 'no', | ||
This comment has been minimized.
Sorry, something went wrong.
GaryJones
Member
|
||
'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 |
Shouldn't the last one here be
'ON'
?