: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
<?php if ( ! defined( 'ABSPATH' ) ) exit;
* The WP Ninjas Static Helper Class
* Provides additional helper functionality to WordPress helper functions.
public static function addslashes( $value )
$value = is_array($value) ?
array_map('WPN_Helper::addslashes' , $value) :
public static function utf8_encode( $input ){
if ( is_array( $input ) ) {
return array_map( 'WPN_Helper::utf8_encode' , $input );
} elseif ( function_exists( 'utf8_encode' ) ) {
return static::iso8859_1_to_utf8( $input );
* Replace utf8_encode with mimicked functionaliy
* Deprecated in PHP8 and removed in PHP9
* Replacement credit: https://php.watch/versions/8.2/utf8_encode-utf8_decode-deprecated
* and https://github.com/symfony/polyfill-php72/blob/v1.26.0/Php72.php#L32-39
public static function iso8859_1_to_utf8( $s) {
for ($i = $len >> 1, $j = 0; $i < $len; ++$i, ++$j) {
case $s[$i] < "\x80": $s[$j] = $s[$i]; break;
case $s[$i] < "\xC0": $s[$j] = "\xC2"; $s[++$j] = $s[$i]; break;
default: $s[$j] = "\xC3"; $s[++$j] = \chr(\ord($s[$i]) - 64); break;
return substr($s, 0, $j);
public static function utf8_decode( $input ){
if ( is_array( $input ) ) {
return array_map( 'WPN_Helper::utf8_decode' , $input );
} elseif ( function_exists( 'utf8_decode' ) ) {
return self::utf8_to_iso8859_1( $input );
* Replace utf8_decode with mimicked functionaliy
* Deprecated in PHP8 and removed in PHP9
* Replacement credit: https://php.watch/versions/8.2/utf8_encode-utf8_decode-deprecated
* and https://github.com/symfony/polyfill-php72/blob/v1.26.0/Php72.php#L40-69
public static function utf8_to_iso8859_1( $string)
for ($i = 0, $j = 0; $i < $len; ++$i, ++$j) {
switch ($s[$i] & "\xF0") {
$c = (\ord($s[$i] & "\x1F") << 6) | \ord($s[++$i] & "\x3F");
$s[$j] = $c < 256 ? \chr($c) : '?';
return substr($s, 0, $j);
* Function to clean json data before json_decode.
public static function json_cleanup( $input ) {
* Remove any unwated (corrupted?) characters from either side of our object.
$l_trim = strpos( $input, '{' );
$r_trim = strrpos( $input, '}' ) - $l_trim + 1;
return substr( $input, $l_trim, $r_trim );
public static function str_replace( $search, $replace, $subject ){
if( is_array( $subject ) ){
foreach( $subject as &$oneSubject )
$oneSubject = WPN_Helper::str_replace($search, $replace, $oneSubject);
return str_replace($search, $replace, $subject);
public static function html_entity_decode( $value, $flag = ENT_COMPAT ){
$value = is_array($value) ?
array_map( 'WPN_Helper::html_entity_decode' , $value) :
html_entity_decode( $value, $flag );
public static function htmlspecialchars( $value ){
$value = is_array($value) ?
array_map( 'WPN_Helper::htmlspecialchars' , $value) :
htmlspecialchars( $value );
public static function stripslashes( $value ){
$value = is_array($value) ?
array_map( 'WPN_Helper::stripslashes' , $value) :
public static function esc_html( $value )
$value = is_array($value) ?
array_map( 'WPN_Helper::esc_html' , $value) :
public static function kses_post( $value )
$value = is_array( $value ) ?
array_map( 'WPN_Helper::kses_post' , $value ) :
public static function strip_tags( $value )
$value = is_array( $value ) ?
array_map( 'WPN_Helper::strip_tags' , $value ) :
* Converts PHP settings from a string to bytes.
public static function string_to_bytes( $size )
// Remove the non-unit characters from the size.
$unit = preg_replace('/[^bkmgtpezy]/i', '', $size);
// Remove the non-numeric characters from the size.
$size = preg_replace('/[^0-9\.]/', '', $size);
if ( $unit && is_array( $unit ) ) {
// Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by.
$size *= pow( 1024, stripos( 'bkmgtpezy', $unit[0] ) );
public static function str_putcsv( $array, $delimiter = ',', $enclosure = '"', $terminator = "\n" ) {
// First convert associative array to numeric indexed array
foreach ($array as $key => $value) {
$returnString = ''; # Initialize return string
$arraySize = count( $workArray ); # Get size of array
for ( $i=0; $i<$arraySize; $i++ ) {
// Nested array, process nest item
if ( is_array( $workArray[$i] ) ) {
$returnString .= self::str_putcsv( $workArray[$i], $delimiter, $enclosure, $terminator );
switch ( gettype( $workArray[$i] ) ) {
// Manually set some strings
case "NULL": $_spFormat = ''; break;
case "boolean": $_spFormat = ($workArray[$i] == true) ? 'true': 'false'; break;
// Make sure sprintf has a good datatype to work with
case "integer": $_spFormat = '%i'; break;
case "double": $_spFormat = '%0.2f'; break;
case "string": $_spFormat = '%s'; $workArray[$i] = str_replace("$enclosure", "$enclosure$enclosure", $workArray[$i]); break;
// Unknown or invalid items for a csv - note: the datatype of array is already handled above, assuming the data is nested
default: $_spFormat = ''; break;
$returnString .= sprintf('%2$s'.$_spFormat.'%2$s', $workArray[$i], $enclosure);
$returnString .= ($i < ($arraySize-1)) ? $delimiter : $terminator;
// Done the workload, return the output information
public static function get_query_string( $key, $default = FALSE )
if( ! isset( $_GET[ $key ] ) ) return $default;
$value = self::htmlspecialchars( $_GET[ $key ] );
if( is_array( $value ) ) $value = reset( $value );
public static function sanitize_text_field( $data )
return array_map( 'WPN_Helper::sanitize_text_field' , $data );
return sanitize_text_field( $data );
public static function get_plugin_version( $plugin )
$plugins = get_plugins();
if( ! isset( $plugins[ $plugin ] ) ) return false;
return $plugins[ $plugin ][ 'Version' ];
public static function is_func_disabled( $function )
if( ! function_exists( $function ) ) return true;
$disabled = explode( ',', ini_get( 'disable_functions' ) );
return in_array( $function, $disabled );
public static function maybe_unserialize( $original )
// Repalcement for https://codex.wordpress.org/Function_Reference/maybe_unserialize
if ( is_serialized( $original ) ){
// Ported with php5.2 support from https://magp.ie/2014/08/13/php-unserialize-string-after-non-utf8-characters-stripped-out/
$parsed = preg_replace_callback( '!s:(\d+):"(.*?)";!s', 'WPN_Helper::parse_utf8_serialized' , $original );
$parsed = @unserialize( $parsed );
return ( $parsed ) ? $parsed : unserialize( $original ); // Fallback if parse error.
* Function to fetch our cache from the upgrades table (if it exists there).
* @param $id (int) The form ID.
public static function get_nf_cache( $id ) {
// See if we have the data in our table already.
$sql = "SELECT cache FROM `{$wpdb->prefix}nf3_upgrades` WHERE id = " . intval( $id );
$result = $wpdb->get_results( $sql, 'ARRAY_A' );
if ( ! empty( $result ) ) {
// Unserialize the result.
$value = WPN_Helper::maybe_unserialize( $result[ 0 ][ 'cache' ] );
} // Otherwise... (We don't have the data.)
// Get it from the options table.
return get_option( 'nf_form_' . $id );
* Function to insert or update our cache in the upgrades table (if it exists).
* @param $id (int) The form ID.
* @param $data (string) The form cache.
* @param $stage (int) The target stage of this update. Default to the current max stage.
public static function update_nf_cache( $id, $data, $stage = 0 ) {
$stage = ( $stage ) ? $stage : WPN_Helper::get_stage();
$cache = serialize( $data );
// See if we've already got a record.
$sql = "SELECT id FROM `{$wpdb->prefix}nf3_upgrades` WHERE id = " . intval( $id );
$result = $wpdb->get_results( $sql, 'ARRAY_A' );
// If we don't already have the data...
if ( empty( $result ) ) {
$sql = $wpdb->prepare( "INSERT INTO `{$wpdb->prefix}nf3_upgrades` (id, cache, stage) VALUES (%d, %s, %s)", intval( $id ), $cache, intval( $stage ) );
} // Otherwise... (We do have the data.)
// Update the existing record.
$sql = $wpdb->prepare( "UPDATE `{$wpdb->prefix}nf3_upgrades` SET cache = %s, stage = %d WHERE id = %d", $cache, intval( $stage ), intval( $id ) );
* Function to retrieve our upgrade stage.
* Remove this after the cache has been resolved.
public static function get_stage() {
$ver = Ninja_Forms::$db_version;
$stack = explode( '.', $ver );
return intval( array_pop( $stack ) );
* Function to build our form cache from the table.
* @param $id (int) The form ID.
* @return $form_cache Array of form data.
public static function build_nf_cache( $id ) {
$form = Ninja_Forms()->form( $id )->get();
'settings' => $form->get_settings(),
$fields = Ninja_Forms()->form( $id )->get_fields();
foreach( $fields as $field ){
if ( ! is_null( $field ) && ! empty( $field ) ) {
array_push( $form_cache[ 'fields' ], array( 'settings' => $field->get_settings(), 'id' => $field->get_id() ) );
$actions = Ninja_Forms()->form( $id )->get_actions();
foreach( $actions as $action ){
if ( ! is_null( $action ) && ! empty( $action ) ) {
array_push( $form_cache[ 'actions' ], array( 'settings' => $action->get_settings(), 'id' => $action->get_id() ) );
WPN_Helper::update_nf_cache( $id, $form_cache );
* Function to delete our cache.
* @param $id (int) The form ID.
public static function delete_nf_cache( $id ) {
$sql = "DELETE FROM `{$wpdb->prefix}nf3_upgrades` WHERE id = " . intval( $id );
delete_option( 'nf_form_' . intval( $id ) );
private static function parse_utf8_serialized( $matches )
if ( isset( $matches[2] ) ){
return 's:'.strlen($matches[2]).':"'.$matches[2].'";';
* This funtion gets/creates the Ninja Forms gate keeper( a random integer
* between 1 and 100 ). We will use this number when deciding whether a
* particular install is eligible for an upgrade or whatever else we decide
public static function get_zuul() {
$zuul = get_option( 'ninja_forms_zuul', -1 );
update_option( 'ninja_forms_zuul', $zuul, false );
* This function will return true/false based on an option( ninja_forms_zuul )
* and a threshold that we set. We can use this to limit updates