PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/ninja-forms/includes/Helper.php

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