Helper.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. } elseif ( function_exists( 'utf8_encode' ) ) {
  30. return utf8_encode( $input );
  31. } else {
  32. return $input;
  33. }
  34. }
  35. /**
  36. * @param $input
  37. * @return array|string
  38. */
  39. public static function utf8_decode( $input ){
  40. if ( is_array( $input ) ) {
  41. return array_map( array( 'self', 'utf8_decode' ), $input );
  42. } elseif ( function_exists( 'utf8_decode' ) ) {
  43. return utf8_decode( $input );
  44. } else {
  45. return $input;
  46. }
  47. }
  48. /**
  49. * Function to clean json data before json_decode.
  50. * @since 3.2
  51. * @param $input String
  52. * @return String
  53. */
  54. public static function json_cleanup( $input ) {
  55. /*
  56. * Remove any unwated (corrupted?) characters from either side of our object.
  57. */
  58. $l_trim = strpos( $input, '{' );
  59. $r_trim = strrpos( $input, '}' ) - $l_trim + 1;
  60. return substr( $input, $l_trim, $r_trim );
  61. }
  62. /**
  63. * @param $search
  64. * @param $replace
  65. * @param $subject
  66. * @return mixed
  67. */
  68. public static function str_replace( $search, $replace, $subject ){
  69. if( is_array( $subject ) ){
  70. foreach( $subject as &$oneSubject )
  71. $oneSubject = WPN_Helper::str_replace($search, $replace, $oneSubject);
  72. unset($oneSubject);
  73. return $subject;
  74. } else {
  75. return str_replace($search, $replace, $subject);
  76. }
  77. }
  78. /**
  79. * @param $value
  80. * @param int $flag
  81. * @return array|string
  82. */
  83. public static function html_entity_decode( $value, $flag = ENT_COMPAT ){
  84. $value = is_array($value) ?
  85. array_map( array( 'self', 'html_entity_decode' ), $value) :
  86. html_entity_decode( $value, $flag );
  87. return $value;
  88. }
  89. /**
  90. * @param $value
  91. * @return array|string
  92. */
  93. public static function htmlspecialchars( $value ){
  94. $value = is_array($value) ?
  95. array_map( array( 'self', 'htmlspecialchars' ), $value) :
  96. htmlspecialchars( $value );
  97. return $value;
  98. }
  99. /**
  100. * @param $value
  101. * @return array|string
  102. */
  103. public static function stripslashes( $value ){
  104. $value = is_array($value) ?
  105. array_map( array( 'self', 'stripslashes' ), $value) :
  106. stripslashes($value);
  107. return $value;
  108. }
  109. /**
  110. * @param $value
  111. * @return array|string
  112. */
  113. public static function esc_html( $value )
  114. {
  115. $value = is_array($value) ?
  116. array_map( array( 'self', 'esc_html' ), $value) :
  117. esc_html($value);
  118. return $value;
  119. }
  120. /**
  121. * @param $value
  122. * @return array|string
  123. */
  124. public static function kses_post( $value )
  125. {
  126. $value = is_array( $value ) ?
  127. array_map( array( 'self', 'kses_post' ), $value ) :
  128. wp_kses_post($value);
  129. return $value;
  130. }
  131. /**
  132. * @param $value
  133. * @return array|string
  134. */
  135. public static function strip_tags( $value )
  136. {
  137. $value = is_array( $value ) ?
  138. array_map( array( 'self', 'strip_tags' ), $value ) :
  139. strip_tags( $value );
  140. return $value;
  141. }
  142. /**
  143. * String to Bytes
  144. *
  145. * Converts PHP settings from a string to bytes.
  146. *
  147. * @param $size
  148. * @return float
  149. */
  150. public static function string_to_bytes( $size )
  151. {
  152. // Remove the non-unit characters from the size.
  153. $unit = preg_replace('/[^bkmgtpezy]/i', '', $size);
  154. // Remove the non-numeric characters from the size.
  155. $size = preg_replace('/[^0-9\.]/', '', $size);
  156. if ( $unit && is_array( $unit ) ) {
  157. // Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by.
  158. $size *= pow( 1024, stripos( 'bkmgtpezy', $unit[0] ) );
  159. }
  160. return round($size);
  161. }
  162. public static function str_putcsv( $array, $delimiter = ',', $enclosure = '"', $terminator = "\n" ) {
  163. // First convert associative array to numeric indexed array
  164. $workArray = array();
  165. foreach ($array as $key => $value) {
  166. $workArray[] = $value;
  167. }
  168. $returnString = ''; # Initialize return string
  169. $arraySize = count( $workArray ); # Get size of array
  170. for ( $i=0; $i<$arraySize; $i++ ) {
  171. // Nested array, process nest item
  172. if ( is_array( $workArray[$i] ) ) {
  173. $returnString .= self::str_putcsv( $workArray[$i], $delimiter, $enclosure, $terminator );
  174. } else {
  175. switch ( gettype( $workArray[$i] ) ) {
  176. // Manually set some strings
  177. case "NULL": $_spFormat = ''; break;
  178. case "boolean": $_spFormat = ($workArray[$i] == true) ? 'true': 'false'; break;
  179. // Make sure sprintf has a good datatype to work with
  180. case "integer": $_spFormat = '%i'; break;
  181. case "double": $_spFormat = '%0.2f'; break;
  182. case "string": $_spFormat = '%s'; $workArray[$i] = str_replace("$enclosure", "$enclosure$enclosure", $workArray[$i]); break;
  183. // Unknown or invalid items for a csv - note: the datatype of array is already handled above, assuming the data is nested
  184. case "object":
  185. case "resource":
  186. default: $_spFormat = ''; break;
  187. }
  188. $returnString .= sprintf('%2$s'.$_spFormat.'%2$s', $workArray[$i], $enclosure);
  189. $returnString .= ($i < ($arraySize-1)) ? $delimiter : $terminator;
  190. }
  191. }
  192. // Done the workload, return the output information
  193. return $returnString;
  194. }
  195. public static function get_query_string( $key, $default = FALSE )
  196. {
  197. if( ! isset( $_GET[ $key ] ) ) return $default;
  198. $value = self::htmlspecialchars( $_GET[ $key ] );
  199. if( is_array( $value ) ) $value = reset( $value );
  200. return $value;
  201. }
  202. public static function sanitize_text_field( $data )
  203. {
  204. if( is_array( $data ) ){
  205. return array_map( array( 'self', 'sanitize_text_field' ), $data );
  206. }
  207. return sanitize_text_field( $data );
  208. }
  209. public static function get_plugin_version( $plugin )
  210. {
  211. $plugins = get_plugins();
  212. if( ! isset( $plugins[ $plugin ] ) ) return false;
  213. return $plugins[ $plugin ][ 'Version' ];
  214. }
  215. public static function is_func_disabled( $function )
  216. {
  217. if( ! function_exists( $function ) ) return true;
  218. $disabled = explode( ',', ini_get( 'disable_functions' ) );
  219. return in_array( $function, $disabled );
  220. }
  221. public static function maybe_unserialize( $original )
  222. {
  223. // Repalcement for https://codex.wordpress.org/Function_Reference/maybe_unserialize
  224. if ( is_serialized( $original ) ){
  225. // Ported with php5.2 support from https://magp.ie/2014/08/13/php-unserialize-string-after-non-utf8-characters-stripped-out/
  226. $parsed = preg_replace_callback( '!s:(\d+):"(.*?)";!s', array( 'self', 'parse_utf8_serialized' ), $original );
  227. $parsed = @unserialize( $parsed );
  228. return ( $parsed ) ? $parsed : unserialize( $original ); // Fallback if parse error.
  229. }
  230. return $original;
  231. }
  232. /**
  233. * Function to fetch our cache from the upgrades table (if it exists there).
  234. *
  235. * @param $id (int) The form ID.
  236. *
  237. * @since 3.3.7
  238. */
  239. public static function get_nf_cache( $id ) {
  240. // See if we have the data in our table already.
  241. global $wpdb;
  242. $sql = "SELECT cache FROM `{$wpdb->prefix}nf3_upgrades` WHERE id = " . intval( $id );
  243. $result = $wpdb->get_results( $sql, 'ARRAY_A' );
  244. // If so...
  245. if ( ! empty( $result ) ) {
  246. // Unserialize the result.
  247. $value = WPN_Helper::maybe_unserialize( $result[ 0 ][ 'cache' ] );
  248. // Return it.
  249. return $value;
  250. } // Otherwise... (We don't have the data.)
  251. else {
  252. // Get it from the options table.
  253. return get_option( 'nf_form_' . $id );
  254. }
  255. }
  256. /**
  257. * Function to insert or update our cache in the upgrades table (if it exists).
  258. *
  259. * @param $id (int) The form ID.
  260. * @param $data (string) The form cache.
  261. *
  262. * @since 3.3.7
  263. */
  264. public static function update_nf_cache( $id, $data ) {
  265. // Define our current stage here for use as we run various upgrades.
  266. $CURRENT_STAGE = 1;
  267. // Serialize our data.
  268. $cache = serialize( $data );
  269. global $wpdb;
  270. // See if we've already got a record.
  271. $sql = "SELECT id FROM `{$wpdb->prefix}nf3_upgrades` WHERE id = " . intval( $id );
  272. $result = $wpdb->get_results( $sql, 'ARRAY_A' );
  273. // If we don't already have the data...
  274. if ( empty( $result ) ) {
  275. // Insert it.
  276. $sql = $wpdb->prepare( "INSERT INTO `{$wpdb->prefix}nf3_upgrades` (id, cache, stage) VALUES (%d, %s, %s)", intval( $id ), $cache, $CURRENT_STAGE);
  277. } // Otherwise... (We do have the data.)
  278. else {
  279. // Update the existing record.
  280. $sql = $wpdb->prepare( "UPDATE `{$wpdb->prefix}nf3_upgrades` SET cache = %s WHERE id = %d", $cache, intval( $id ) ) ;
  281. }
  282. $wpdb->query( $sql );
  283. }
  284. /**
  285. * Function to delete our cache.
  286. *
  287. * @param $id (int) The form ID.
  288. *
  289. * @since 3.3.7
  290. */
  291. public static function delete_nf_cache( $id ) {
  292. global $wpdb;
  293. $sql = "DELETE FROM `{$wpdb->prefix}nf3_upgrades` WHERE id = " . intval( $id );
  294. $wpdb->query( $sql );
  295. delete_option( 'nf_form_' . intval( $id ) );
  296. }
  297. private static function parse_utf8_serialized( $matches )
  298. {
  299. if ( isset( $matches[2] ) ){
  300. return 's:'.strlen($matches[2]).':"'.$matches[2].'";';
  301. }
  302. }
  303. } // End Class WPN_Helper