WPN_Helper

Class WPN_Helper.

Defined (1)

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

/includes/Helper.php  
  1. final class WPN_Helper 
  2. /** 
  3. * @param $value 
  4. * @return array|string 
  5. */ 
  6. public static function addslashes( $value ) 
  7. $value = is_array($value) ? 
  8. array_map(array( 'self', 'addslashes' ), $value) : 
  9. addslashes($value); 
  10. return $value; 
  11.  
  12. /** 
  13. * @param $input 
  14. * @return array|string 
  15. */ 
  16. public static function utf8_encode( $input ) { 
  17. if ( is_array( $input ) ) { 
  18. return array_map( array( 'self', 'utf8_encode' ), $input ); 
  19. }else{ 
  20. return utf8_encode( $input ); 
  21.  
  22. /** 
  23. * @param $input 
  24. * @return array|string 
  25. */ 
  26. public static function utf8_decode( $input ) { 
  27. if ( is_array( $input ) ) { 
  28. return array_map( array( 'self', 'utf8_decode' ), $input ); 
  29. }else{ 
  30. return utf8_decode( $input ); 
  31.  
  32. /** 
  33. * @param $search 
  34. * @param $replace 
  35. * @param $subject 
  36. * @return mixed 
  37. */ 
  38. public static function str_replace( $search, $replace, $subject ) { 
  39. if( is_array( $subject ) ) { 
  40. foreach( $subject as &$oneSubject ) 
  41. $oneSubject = WPN_Helper::str_replace($search, $replace, $oneSubject); 
  42. unset($oneSubject); 
  43. return $subject; 
  44. } else { 
  45. return str_replace($search, $replace, $subject); 
  46.  
  47. /** 
  48. * @param $value 
  49. * @param int $flag 
  50. * @return array|string 
  51. */ 
  52. public static function html_entity_decode( $value, $flag = ENT_COMPAT ) { 
  53. $value = is_array($value) ? 
  54. array_map( array( 'self', 'html_entity_decode' ), $value) : 
  55. html_entity_decode( $value, $flag ); 
  56. return $value; 
  57.  
  58. /** 
  59. * @param $value 
  60. * @return array|string 
  61. */ 
  62. public static function htmlspecialchars( $value ) { 
  63. $value = is_array($value) ? 
  64. array_map( array( 'self', 'htmlspecialchars' ), $value) : 
  65. htmlspecialchars( $value ); 
  66. return $value; 
  67.  
  68. /** 
  69. * @param $value 
  70. * @return array|string 
  71. */ 
  72. public static function stripslashes( $value ) { 
  73. $value = is_array($value) ? 
  74. array_map( array( 'self', 'stripslashes' ), $value) : 
  75. stripslashes($value); 
  76. return $value; 
  77.  
  78. /** 
  79. * @param $value 
  80. * @return array|string 
  81. */ 
  82. public static function esc_html( $value ) 
  83. $value = is_array($value) ? 
  84. array_map( array( 'self', 'esc_html' ), $value) : 
  85. esc_html($value); 
  86. return $value; 
  87.  
  88. /** 
  89. * @param $value 
  90. * @return array|string 
  91. */ 
  92. public static function kses_post( $value ) 
  93. $value = is_array( $value ) ? 
  94. array_map( array( 'self', 'kses_post' ), $value ) : 
  95. wp_kses_post($value); 
  96. return $value; 
  97.  
  98. /** 
  99. * @param $value 
  100. * @return array|string 
  101. */ 
  102. public static function strip_tags( $value ) 
  103. $value = is_array( $value ) ? 
  104. array_map( array( 'self', 'strip_tags' ), $value ) : 
  105. strip_tags( $value ); 
  106. return $value; 
  107.  
  108. /** 
  109. * String to Bytes 
  110. * Converts PHP settings from a string to bytes. 
  111. * @param $size 
  112. * @return float 
  113. */ 
  114. public static function string_to_bytes( $size ) 
  115. // Remove the non-unit characters from the size. 
  116. $unit = preg_replace('/[^bkmgtpezy]/i', '', $size); 
  117.  
  118. // Remove the non-numeric characters from the size. 
  119. $size = preg_replace('/[^0-9\.]/', '', $size); 
  120.  
  121. if ( $unit && is_array( $unit ) ) { 
  122. // Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by. 
  123. $size *= pow( 1024, stripos( 'bkmgtpezy', $unit[0] ) ); 
  124.  
  125. return round($size); 
  126.  
  127. public static function str_putcsv( $array, $delimiter = ', ', $enclosure = '"', $terminator = "\n" ) { 
  128. // First convert associative array to numeric indexed array 
  129. $workArray = array(); 
  130. foreach ($array as $key => $value) { 
  131. $workArray[] = $value; 
  132.  
  133. $returnString = ''; # Initialize return string 
  134. $arraySize = count( $workArray ); # Get size of array 
  135.  
  136. for ( $i=0; $i<$arraySize; $i++ ) { 
  137. // Nested array, process nest item 
  138. if ( is_array( $workArray[$i] ) ) { 
  139. $returnString .= self::str_putcsv( $workArray[$i], $delimiter, $enclosure, $terminator ); 
  140. } else { 
  141. switch ( gettype( $workArray[$i] ) ) { 
  142. // Manually set some strings 
  143. case "NULL": $_spFormat = ''; break; 
  144. case "boolean": $_spFormat = ($workArray[$i] == true) ? 'true': 'false'; break; 
  145. // Make sure sprintf has a good datatype to work with 
  146. case "integer": $_spFormat = '%i'; break; 
  147. case "double": $_spFormat = '%0.2f'; break; 
  148. case "string": $_spFormat = '%s'; $workArray[$i] = str_replace("$enclosure", "$enclosure$enclosure", $workArray[$i]); break; 
  149. // Unknown or invalid items for a csv - note: the datatype of array is already handled above, assuming the data is nested 
  150. case "object": 
  151. case "resource": 
  152. default: $_spFormat = ''; break; 
  153. $returnString .= sprintf('%2$s'.$_spFormat.'%2$s', $workArray[$i], $enclosure); 
  154. $returnString .= ($i < ($arraySize-1)) ? $delimiter : $terminator; 
  155. // Done the workload, return the output information 
  156. return $returnString; 
  157.  
  158. public static function get_query_string( $key, $default = FALSE ) 
  159. if( ! isset( $_GET[ $key ] ) ) return $default; 
  160.  
  161. $value = self::htmlspecialchars( $_GET[ $key ] ); 
  162.  
  163. if( is_array( $value ) ) $value = $value[ 0 ]; 
  164.  
  165. return $value; 
  166.  
  167. public static function sanitize_text_field( $data ) 
  168. if( is_array( $data ) ) { 
  169. return array_map( array( 'self', 'sanitize_text_field' ), $data ); 
  170. return sanitize_text_field( $data ); 
  171.  
  172. public static function get_plugin_version( $plugin ) 
  173. $plugins = get_plugins(); 
  174.  
  175. if( ! isset( $plugins[ $plugin ] ) ) return false; 
  176.  
  177. return $plugins[ $plugin ][ 'Version' ]; 
  178.  
  179. public static function is_func_disabled( $function ) 
  180. if( ! function_exists( $function ) ) return true; 
  181. $disabled = explode( ', ', ini_get( 'disable_functions' ) ); 
  182. return in_array( $function, $disabled ); 
  183.  
  184. public static function maybe_unserialize( $original ) 
  185. // Repalcement for https://codex.wordpress.org/Function_Reference/maybe_unserialize 
  186. if ( is_serialized( $original ) ) { 
  187. // Ported with php5.2 support from https://magp.ie/2014/08/13/php-unserialize-string-after-non-utf8-characters-stripped-out/ 
  188. $parsed = preg_replace_callback( '!s:(\d+):"(.*?)";!s', array( 'self', 'parse_utf8_serialized' ), $original ); 
  189. $parsed = @unserialize( $parsed ); 
  190.  
  191. return ( $parsed ) ? $parsed : unserialize( $original ); // Fallback if parse error. 
  192. return $original; 
  193.  
  194.  
  195. /** 
  196. * Function to get this installation's TLS version 
  197. *  
  198. * Since 3.0 
  199. *  
  200. * @return float OR false 
  201. */ 
  202. public static function get_tls() 
  203. $php_ver = phpversion(); 
  204. // If we have a php version lower than 5.6, bail. 
  205. if( version_compare( $php_ver, '5.6.0', '<' ) ) return false; 
  206. // Get the user's TLS version. 
  207.  
  208. // If we have a php version of 7.0 or higher... 
  209. if( version_compare( $php_ver, '7.0.0', '>=' ) ) { 
  210. $meta = stream_get_meta_data( fopen( 'https://ninjaforms.com/', 'r' ) ); 
  211. $tls = $meta[ 'crypto' ][ 'protocol' ];  
  212. // Otherwise (php version between 5.6 and 7.0)... 
  213. else { 
  214. $ctx = stream_context_create( array( 'ssl' => array( 
  215. 'capture_session_meta' => TRUE 
  216. ) ) ); 
  217. $html = file_get_contents( 'https://ninjaforms.com/', FALSE, $ctx ); 
  218. $meta = stream_context_get_options( $ctx ); 
  219. $tls = $meta[ 'ssl' ][ 'session_meta' ][ 'protocol' ]; 
  220. unset( $ctx ); 
  221. // If we got a TLS version number... 
  222. if( false !== strpos( $tls, 'TLSv' ) ) { 
  223. $ver = substr( $tls, strpos( $tls, 'TLSv' ) + 4 ); 
  224. return floatval( $ver ); 
  225. } else { 
  226. return false; 
  227.  
  228. private static function parse_utf8_serialized( $matches ) 
  229. if ( isset( $matches[2] ) ) { 
  230. return 's:'.strlen($matches[2]).':"'.$matches[2].'";'; 
  231.  
  232. } // End Class WPN_Helper