<?php 


add_filter('gform_field_content', 'boot_gf', 10, 5);
function boot_gf ($content, $field, $value, $lead_id, $form_id) {
	if($field["type"] != 'hidden' && $field["type"] != 'list' && $field["type"] != 'multiselect' && $field["type"] != 'checkbox' && $field["type"] != 'fileupload' && $field["type"] != 'date' && $field["type"] != 'html' && $field["type"] != 'address') {
		$content = str_replace('class=\'medium', 'class=\'form-control medium', $content);
	}
	if($field["type"] == 'name' || $field["type"] == 'address') {
		//$content = str_replace('<input ', '<input class=\'form-control\' ', $content);
	}
	if($field["type"] == 'textarea') {
		$content = str_replace('class=\'textarea', 'class=\'form-control medium textarea', $content);
	}
	if($field["type"] == 'checkbox') {
		$content = str_replace('li class=\'', 'li class=\'checkbox ', $content);
		$content = str_replace('<input ', '<input style=\'margin-left:1px;\' ', $content);
	}
	if($field["type"] == 'radio') {
		$content = str_replace('li class=\'', 'li class=\'radio ', $content);
		$content = str_replace('<input ', '<input style=\'margin-left:1px;\' ', $content);
	}
	return $content;
}

add_filter("gform_init_scripts_footer", "init_gscripts");
function init_gscripts() {
	return true;
}

add_action( 'wp_print_scripts', 'dw_deregister_gf_js', 100 );
function dw_deregister_gf_js() {
	if( is_page( 'contact' ) ) {
		//wp_deregister_script( 'gform_conditional_logic' );
		//wp_deregister_script( 'gform_json' );
		//wp_deregister_script( 'gform_datepicker_init' );
		//wp_deregister_script( 'gforms_stripe_frontend' );	
	}
}

add_filter('gform_validation_message', 'change_validation_message', 10, 2);
function change_validation_message($message, $form)
{
    return "<div class='validation_error'><strong>Oops!</strong> Looks like something is missing.</div>";
}

add_filter( 'gform_submit_button', 'form_submit_button', 10, 5 );
function form_submit_button ( $button, $form ){
    $button = str_replace( "input", "button", $button );
    $button = str_replace( "/", "", $button );
    $button .= "{$form['button']['text']}</button>";
    return $button;
}



/*============================================
		 	GF- Date Assist
==============================================*/

class DW_GF_Date {
    public function __construct( $args = array() ) {
        $this->_args = wp_parse_args( $args, array(
            'form_id'         => false,
            'target_field_id' => false,
            'source_field_id' => false,
            'format'          => 'Y-m-d',
            'modifier'        => false
        ) );
        if( ! $this->_args['form_id'] || ! $this->_args['target_field_id'] ) {
            return;
        }
        add_action( 'init', array( $this, 'init' ) );
    }

    public function init() {
        if( $this->_args['source_field_id'] ) {
            add_action( 'gform_pre_submission', array( $this, 'populate_date_on_pre_submission' ) );
        } else {
            add_filter( 'gform_pre_render', array( $this, 'populate_date_on_pre_render' ) );
        }
    }

    public function populate_date_on_pre_render( $form ) {

        foreach( $form['fields'] as &$field ) {
            if( $field['id'] == $this->_args['target_field_id'] ) {
                $key = sprintf( 'gwpd_%d_%d', $form['id'], $field['id'] );
                $value = $this->get_modified_date( $field );
                $field['allowsPrepopulate'] = true;
                $field['inputName'] = $key;
                add_filter("gform_field_value_{$key}", create_function( '', 'return \'' . $value . '\';' ) );
            }
        }
        return $form;
    }

    public function populate_date_on_pre_submission( $form ) {
        foreach( $form['fields'] as &$field ) {
            if( $field['id'] == $this->_args['target_field_id'] ) {
                $timestamp = strtotime( rgpost( 'input_' . $this->_args['source_field_id'] ) );
                $value = $this->get_modified_date( $field, $timestamp );
                $_POST[ "input_{$field['id']}" ] = $value;
            }
        }
    }

    public function get_modified_date( $field, $timestamp = false ) {
        if( ! $timestamp ) {
            $timestamp = current_time( 'timestamp' );
        }
        if( GFFormsModel::get_input_type( $field ) == 'date' ) {
            list( $format, $divider ) = $field['dateFormat'] ? array_pad( explode( '_', $field['dateFormat' ] ), 2, 'slash' ) : array( 'mdy', 'slash' );
            $dividers = array( 'slash' => '/', 'dot' => '.', 'dash' => '-' );
            $format = str_replace( 'y', 'Y', $format );
            $divider = $dividers[$divider];
            $format = implode( $divider, str_split( $format ) );
        } else {
            $format = $this->_args['format'];
        }
        if( $this->_args['modifier'] ) {
            $date = date( $format, strtotime( $this->_args['modifier'], $timestamp ) );
        } else {
            $date = date( $format, $timestamp );
        }
        return $date;
    }
}


new DW_GF_Date( array(
    'form_id' => 1,
    'target_field_id' => 17,
    'modifier' => '+4 months'
) );