TGMPA_Utils

Generic utilities for TGMPA.

Defined (1)

The class is defined in the following location(s).

/class-tgm-plugin-activation.php  
  1. class TGMPA_Utils { 
  2. /** 
  3. * Whether the PHP filter extension is enabled. 
  4. * @see http://php.net/book.filter 
  5. * @since 2.5.0 
  6. * @static 
  7. * @var bool $has_filters True is the extension is enabled. 
  8. */ 
  9. public static $has_filters; 
  10.  
  11. /** 
  12. * Wrap an arbitrary string in <em> tags. Meant to be used in combination with array_map(). 
  13. * @since 2.5.0 
  14. * @static 
  15. * @param string $string Text to be wrapped. 
  16. * @return string 
  17. */ 
  18. public static function wrap_in_em( $string ) { 
  19. return '<em>' . wp_kses_post( $string ) . '</em>'; 
  20.  
  21. /** 
  22. * Wrap an arbitrary string in <strong> tags. Meant to be used in combination with array_map(). 
  23. * @since 2.5.0 
  24. * @static 
  25. * @param string $string Text to be wrapped. 
  26. * @return string 
  27. */ 
  28. public static function wrap_in_strong( $string ) { 
  29. return '<strong>' . wp_kses_post( $string ) . '</strong>'; 
  30.  
  31. /** 
  32. * Helper function: Validate a value as boolean 
  33. * @since 2.5.0 
  34. * @static 
  35. * @param mixed $value Arbitrary value. 
  36. * @return bool 
  37. */ 
  38. public static function validate_bool( $value ) { 
  39. if ( ! isset( self::$has_filters ) ) { 
  40. self::$has_filters = extension_loaded( 'filter' ); 
  41.  
  42. if ( self::$has_filters ) { 
  43. return filter_var( $value, FILTER_VALIDATE_BOOLEAN ); 
  44. } else { 
  45. return self::emulate_filter_bool( $value ); 
  46.  
  47. /** 
  48. * Helper function: Cast a value to bool 
  49. * @since 2.5.0 
  50. * @static 
  51. * @param mixed $value Value to cast. 
  52. * @return bool 
  53. */ 
  54. protected static function emulate_filter_bool( $value ) { 
  55. // @codingStandardsIgnoreStart 
  56. static $true = array( 
  57. '1',  
  58. 'true', 'True', 'TRUE',  
  59. 'y', 'Y',  
  60. 'yes', 'Yes', 'YES',  
  61. 'on', 'On', 'ON',  
  62. ); 
  63. static $false = array( 
  64. '0',  
  65. 'false', 'False', 'FALSE',  
  66. 'n', 'N',  
  67. 'no', 'No', 'NO',  
  68. 'off', 'Off', 'OFF',  
  69. ); 
  70. // @codingStandardsIgnoreEnd 
  71.  
  72. if ( is_bool( $value ) ) { 
  73. return $value; 
  74. } elseif ( is_int( $value ) && ( 0 === $value || 1 === $value ) ) { 
  75. return (bool) $value; 
  76. } elseif ( ( is_float( $value ) && ! is_nan( $value ) ) && ( (float) 0 === $value || (float) 1 === $value ) ) { 
  77. return (bool) $value; 
  78. } elseif ( is_string( $value ) ) { 
  79. $value = trim( $value ); 
  80. if ( in_array( $value, $true, true ) ) { 
  81. return true; 
  82. } elseif ( in_array( $value, $false, true ) ) { 
  83. return false; 
  84. } else { 
  85. return false; 
  86.  
  87. return false; 
  88. } // End of class TGMPA_Utils