<?php if ( ! defined( 'ABSPATH' ) ) exit;
function ninja_forms_return_echo($function_name){
$arguments = func_get_args();
array_shift($arguments); // We need to remove the first arg ($function_name)
call_user_func_array($function_name, $arguments);
$return = ob_get_clean();
function ninja_forms_random_string($length = 10){
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
for ($i = 0; $i < $length; $i++) {
$random_string .= $characters[rand(0, strlen($characters) - 1)];
function ninja_forms_remove_from_array($arr, $key, $val, $within = FALSE) {
foreach ($arr as $i => $array)
if ($within && stripos($array[$key], $val) !== FALSE && (gettype($val) === gettype($array[$key])))
elseif ($array[$key] === $val)
return array_values($arr);
function ninja_forms_letters_to_numbers( $size ) {
$l = substr( $size, -1 );
$ret = substr( $size, 0, -1 );
switch( strtoupper( $l ) ) {
function ninja_forms_subval_sort( $a, $subkey ) {
$b[$k] = strtolower($v[$subkey]);
foreach($b as $key=>$val) {
* Takes a field ID and returns the admin label if it exists and the label if it does not.
function nf_get_field_admin_label( $field_id, $form_id = '' ) {
if ( empty ( $form_id ) ) {
$form = ninja_forms_get_form_by_field_id( $field_id );
$admin_label = isset( Ninja_Forms()->form( $form_id )->fields[ $field_id ]['data']['admin_label'] ) ? Ninja_Forms()->form( $form_id )->fields[ $field_id ]['data']['admin_label'] : '';
$field_label = isset( Ninja_Forms()->form( $form_id )->fields[ $field_id ]['data']['label'] ) ? Ninja_Forms()->form( $form_id )->fields[ $field_id ]['data']['label'] : '';
if ( ! empty( $admin_label ) ) {
* Return the begin date with an added 00:00:00.
* Checks for the current date format setting and tries to respect it.
* @param string $begin_date
* @return string $begin_date
function nf_get_begin_date( $begin_date ) {
$plugin_settings = nf_get_settings();
if ( isset ( $plugin_settings['date_format'] ) ) {
$date_format = $plugin_settings['date_format'];
if ( $date_format == 'd/m/Y' ) {
$begin_date = str_replace( '/', '-', $begin_date );
} else if ( $date_format == 'm-d-Y' ) {
$begin_date = str_replace( '-', '/', $begin_date );
$begin_date .= '00:00:00';
$begin_date = new DateTime( $begin_date );
* Return the end date with an added 23:59:59.
* Checks for the current date format setting and tries to respect it.
* @param string $end_date
* @return string $end_date
function nf_get_end_date( $end_date ) {
$plugin_settings = nf_get_settings();
if ( isset ( $plugin_settings['date_format'] ) ) {
$date_format = $plugin_settings['date_format'];
if ( $date_format == 'd/m/Y' ) {
$end_date = str_replace( '/', '-', $end_date );
} else if ( $date_format == 'm-d-Y' ) {
$end_date = str_replace( '-', '/', $end_date );
$end_date = new DateTime( $end_date );
* Checks whether function is disabled.
* @param string $function Name of the function.
* @return bool Whether or not function is disabled.
if( ! function_exists( 'nf_is_func_disabled' ) ) {
function nf_is_func_disabled($function)
$disabled = explode(',', ini_get('disable_functions'));
return in_array($function, $disabled);
* Acts as a wrapper/alias for nf_get_objects_by_type that is specific to notifications.
* @return array $notifications
function nf_get_all_notifications() {
return nf_get_objects_by_type( 'notification' );
* Acts as a wrapper/alias for nf_get_object_children that is specific to notifications.
* @return array $notifications
function nf_get_notifications_by_form_id( $form_id, $full_data = true ) {
return nf_get_object_children( $form_id, 'notification', $full_data );
* Acts as a wrapper/alias for nf_get_object_meta
* @return array $notification
function nf_get_notification_by_id( $notification_id ) {
return nf_get_object_meta( $notification_id );
* Insert a notification into the database.
* Calls nf_insert_object()
* Calls nf_add_relationship()
* Calls nf_update_object_meta()
function nf_insert_notification( $form_id = '' ) {
if ( empty ( $form_id ) )
$n_id = nf_insert_object( 'notification' );
nf_add_relationship( $n_id, 'notification', $form_id, 'form' );
$date_updated = date( 'Y-m-d', current_time( 'timestamp' ) );
nf_update_object_meta( $n_id, 'date_updated', $date_updated );
* Acts as a wrapper/alias for nf_delete_object
function nf_delete_notification( $n_id ) {
nf_delete_object( $n_id );
* Function that gets a piece of object meta
* @param string $object_id
* @param string $meta_key
* @return var $meta_value
function nf_get_object_meta_value( $object_id, $meta_key ) {
$meta_value = $wpdb->get_row( $wpdb->prepare( 'SELECT meta_value FROM ' . NF_OBJECT_META_TABLE_NAME . ' WHERE object_id = %d AND meta_key = %s', $object_id, $meta_key ), ARRAY_A );
if ( is_array ( $meta_value['meta_value'] ) ) {
$meta_value['meta_value'] = unserialize( $meta_value['meta_value'] );
return $meta_value['meta_value'];
* Function that gets children objects by type and parent id
* @param string $parent_id
* @return array $children
function nf_get_object_children( $object_id, $child_type = '', $full_data = true, $include_forms = true ) {
if ( $child_type != '' ) {
$children = $wpdb->get_results( $wpdb->prepare( "SELECT child_id FROM " . NF_OBJECT_RELATIONSHIPS_TABLE_NAME . " WHERE child_type = %s AND parent_id = %d", $child_type, $object_id ), ARRAY_A);
$children = $wpdb->get_results( $wpdb->prepare( "SELECT child_id FROM " . NF_OBJECT_RELATIONSHIPS_TABLE_NAME . " WHERE parent_id = %d", $object_id ), ARRAY_A);
if ( $child_type != '' ) {
$children = $wpdb->get_results( $wpdb->prepare( "SELECT child_id FROM " . NF_OBJECT_RELATIONSHIPS_TABLE_NAME . " WHERE child_type = %s AND parent_id = %d AND parent_type <> 'form'", $child_type, $object_id ), ARRAY_A);
$children = $wpdb->get_results( $wpdb->prepare( "SELECT child_id FROM " . NF_OBJECT_RELATIONSHIPS_TABLE_NAME . " WHERE parent_id = %d AND parent_type <> 'form'", $object_id ), ARRAY_A);
foreach( $children as $id ) {
$child_id = $id['child_id'];
$settings = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM " . NF_OBJECT_META_TABLE_NAME . " WHERE object_id = %d", $child_id ), ARRAY_A);
if ( ! empty( $settings ) ) {
foreach ( $settings as $s ) {
if ( is_array ( $s['meta_value'] ) ) {
$s['meta_value'] = unserialize( $s['meta_value'] );
$tmp_array[ $child_id ][ $s['meta_key'] ] = $s['meta_value'];
$tmp_array[ $child_id ] = array();
if ( is_array( $children ) ) {
foreach ( $children as $child ) {
$tmp_array[] = $child['child_id'];
* Function that updates a piece of object meta
* @param string $object_id
* @param string $meta_key
* @param string $meta_value
* @return string $meta_id
function nf_update_object_meta( $object_id, $meta_key, $meta_value ) {
if ( is_array( $meta_value ) ) {
$meta_value = serialize( $meta_value );
// Check to see if this meta_key/meta_value pair exist for this object_id.
$found = $wpdb->get_row( $wpdb->prepare( "SELECT id FROM ".NF_OBJECT_META_TABLE_NAME." WHERE object_id = %d AND meta_key = %s", $object_id, $meta_key ), ARRAY_A );
$wpdb->update( NF_OBJECT_META_TABLE_NAME, array( 'meta_value' => $meta_value ), array( 'meta_key' => $meta_key, 'object_id' => $object_id ) );
$wpdb->insert( NF_OBJECT_META_TABLE_NAME, array( 'object_id' => $object_id, 'meta_key' => $meta_key, 'meta_value' => $meta_value ) );
$meta_id = $wpdb->insert_id;
* Function that gets all the meta values attached to a given object.
* @return array $settings
function nf_get_object_meta( $object_id ) {
$settings = $wpdb->get_results( $wpdb->prepare( 'SELECT meta_key, meta_value FROM ' . NF_OBJECT_META_TABLE_NAME . ' WHERE object_id = %d', $object_id ), ARRAY_A);
if ( is_array( $settings ) ) {
foreach( $settings as $setting ) {
$tmp_array[ $setting['meta_key'] ] = $setting['meta_value'] = maybe_unserialize( $setting['meta_value'] );
function nf_insert_object( $type, $id = NULL ) {
$wpdb->insert( NF_OBJECTS_TABLE_NAME, array( 'id' => $id, 'type' => $type ) );
* Delete an object. Also removes all of the objectmeta attached to the object and any references to it in the relationship table.
function nf_delete_object( $object_id ) {
// Check to see if we have any object children.
$children = nf_get_object_children( $object_id, '', false, false );
foreach ( $children as $child_id ) {
nf_delete_object( $child_id );
$wpdb->query( $wpdb->prepare( 'DELETE FROM ' . NF_OBJECTS_TABLE_NAME .' WHERE id = %d', $object_id ) );
// Delete any objectmeta attached to this object.
$wpdb->query( $wpdb->prepare( 'DELETE FROM ' . NF_OBJECT_META_TABLE_NAME .' WHERE object_id = %d', $object_id ) );
// Delete any references to this object in the relationship table
$wpdb->query( $wpdb->prepare( 'DELETE FROM ' . NF_OBJECT_RELATIONSHIPS_TABLE_NAME .' WHERE child_id = %d OR parent_id = %d', $object_id, $object_id ) );
* Create a relationship between two objects
* @param string child_type
* @param string $parent_type
function nf_add_relationship( $child_id, $child_type, $parent_id, $parent_type ) {
// Make sure that our relationship doesn't already exist.
$count = $wpdb->query( $wpdb->prepare( 'SELECT id FROM ' . NF_OBJECT_RELATIONSHIPS_TABLE_NAME .' WHERE child_id = %d AND parent_id = %d', $child_id, $parent_id ), ARRAY_A );
$wpdb->insert( NF_OBJECT_RELATIONSHIPS_TABLE_NAME, array( 'child_id' => $child_id, 'child_type' => $child_type, 'parent_id' => $parent_id, 'parent_type' => $parent_type ) );
function nf_get_object_parent( $child_id ) {
// Check our relationship table for where this ID appears as a child.
$parent = $wpdb->get_row( $wpdb->prepare( 'SELECT parent_id FROM ' . NF_OBJECT_RELATIONSHIPS_TABLE_NAME . ' WHERE child_id = %d', $child_id ), ARRAY_A );
return $parent['parent_id'];
function nf_get_object_type( $object_id ) {
$type = $wpdb->get_row( $wpdb->prepare( 'SELECT type FROM ' . NF_OBJECTS_TABLE_NAME . ' WHERE id = %d', $object_id ), ARRAY_A );
$return = ( isset ( $type['type'] ) ) ? $type['type'] : false;
* Returns the IP address of the current visitor
* @return string $ip User's IP address
if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
//check ip from share internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
//to check ip is pass from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif( ! empty( $_SERVER['REMOTE_ADDR'] ) ) {
$ip = $_SERVER['REMOTE_ADDR'];
return apply_filters( 'nf_get_ip', $ip );
* Function that gets all objects of a given type.
function nf_get_objects_by_type( $object_type ) {
// Bail if we don't have an object type.
if ( $object_type == '' )