Skip to content

Commit

Permalink
gpld-conditional-limits.php: Added a new snippet to enable conditio…
Browse files Browse the repository at this point in the history
…nal date limits.
  • Loading branch information
barthc committed Sep 7, 2024
1 parent 8d38d31 commit d8ef370
Showing 1 changed file with 199 additions and 0 deletions.
199 changes: 199 additions & 0 deletions gp-limit-dates/gpld-conditional-limits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
<?php
/**
* Gravity Wiz // Gravity Perks // GP Limit Dates // Conditional Limits
* https://gravitywiz.com/documentation/gravity-forms-limit-dates/
*
* Provide conditional date options for your date fields.
*
*/
class GPLD_Conditional_Limits {

protected static $is_script_output = false;

public function __construct( $args = array() ) {
// set our default arguments, parse against the provided arguments, and store for use throughout the class
$this->_args = wp_parse_args( $args, array(
'form_id' => false,
'field_id' => false,
) );

add_action( 'init', array( $this, 'init' ) );
}

public function init() {
add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 );
add_filter( 'gform_register_init_scripts', array( $this, 'add_init_script' ), 10, 2 );
add_action( 'gform_field_validation', array( $this, 'validate' ), 11, 4 );
}

public function load_form_script( $form, $is_ajax_enabled ) {
if ( $this->is_applicable_form( $form ) && ! self::$is_script_output && ! $this->is_ajax_submission( $form['id'], $is_ajax_enabled ) ) {
$this->output_script();
}

return $form;
}

public function is_ajax_submission( $form_id, $is_ajax_enabled ) {
return isset( GFFormDisplay::$submission[ $form_id ] ) && $is_ajax_enabled;
}

public function output_script() {
?>

<script type="text/javascript">

( function( $ ) {

window.GPLDConditionalLimits = function( args ) {

var self = this;

// copy all args to current object: (list expected props)
for( prop in args ) {
if( args.hasOwnProperty( prop ) )
self[prop] = args[prop];
}

self.init = function() {

gform.addAction( 'gform_input_change', function( elem, formId, fieldId ) {

Check warning on line 61 in gp-limit-dates/gpld-conditional-limits.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Found precision alignment of 1 spaces.
if ( !window.GPLimitDates ) {
return;
};

for( var i = 0; i < self.conditionals.length; i++ ) {
if( gf_get_field_action( self.formId, self.conditionals[ i ].conditionalLogic ) == 'show' ) {
var $input = $( '#input_' + self.formId + '_' + self.fieldId );
$input.datepicker( 'destroy' );

gform.addFilter( 'gpld_datepicker_data', function( data ) {
return {
...data, [args.fieldId]: self.conditionals[ i ].options

Check warning on line 73 in gp-limit-dates/gpld-conditional-limits.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Found precision alignment of 3 spaces.
}
});

window.gformInitSingleDatepicker($input);

break;
}
}
} );
};

self.init();
}

} )( jQuery );

</script>

<?php

self::$is_script_output = true;
}

public function add_init_script( $form ) {
if ( ! $this->is_applicable_form( $form ) ) {
return;
}

$args = array(
'formId' => $this->_args['form_id'],
'fieldId' => $this->_args['field_id'],
'conditionals' => $this->_args['conditionals'],
);

$script = 'new GPLDConditionalLimits( ' . json_encode( $args ) . ' );';
$slug = implode( '_', array( __class__, $this->_args['form_id'], $this->_args['field_id'] ) );

GFFormDisplay::add_init_script( $this->_args['form_id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );
}

public function is_applicable_form( $form ) {
$form_id = isset( $form['id'] ) ? $form['id'] : $form;

return empty( $this->_args['form_id'] ) || $form_id == $this->_args['form_id'];
}

public function is_applicable_field( $field ) {
$field_id = isset( $field['id'] ) ? $field['id'] : $field;

return $field['type'] == 'date' && $field_id == $this->_args['field_id'];

Check failure on line 123 in gp-limit-dates/gpld-conditional-limits.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Expected 1 space before &quot;&amp;&amp;&quot;; 2 found
}

public function validate( $result, $value, $form, $field ) {
if ( ! is_applicable_field( $field ) ) {
return $result;
}

foreach ( $this->_args['conditionals'] as $conditional ) {
if ( GFCommon::evaluate_conditional_logic( $conditional['conditionalLogic'], $form, GFFormsModel::get_current_lead() ) ) {
add_filter( "gpld_limit_dates_options_{$this->_args['field_id']}_{$this->_args['field_id']}", function( $field_options ) {
return $conditional['options'];
} );

break;
}
}

if ( ! rgblank( $value ) && ! gp_limit_dates()->is_valid_date( $value, $field ) ) {
$result['is_valid'] = false;
$result['message'] = __( 'Please enter a valid date.', 'gp-limit-dates' );
}

return $result;
}
}

# Configuration

new GPLD_Conditional_Limits( array(
'form_id' => 30,
'field_id' => 3,
'conditionals' => array(
array(
'options' => array(

Check warning on line 157 in gp-limit-dates/gpld-conditional-limits.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Array double arrow not aligned correctly; expected 10 space(s) between &quot;'options'&quot; and double arrow, but found 11.
'minDate' => '{today}',
'minDateMod' => '+2 days',
'daysOfWeek' => [1],

Check failure on line 160 in gp-limit-dates/gpld-conditional-limits.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Missing space after array opener.

Check failure on line 160 in gp-limit-dates/gpld-conditional-limits.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Short array syntax is not allowed

Check failure on line 160 in gp-limit-dates/gpld-conditional-limits.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Missing space before array closer.
'dateFormat' => 'mdy',
'exceptions' => array( '01/01/2024', '01/17/2024', '05/27/2024' ),
'inlineDatepicker' => false,
),
'conditionalLogic' => array(
'logicType' => 'any',
'actionType' => 'show',
'rules' => array(
array(
'fieldId' => '1',
'operator' => 'is',
'value' => '68462',
),
),
),
),
array(
'options' => array(
'minDate' => '{today}',
'minDateMod' => '+2 days',
'daysOfWeek' => [1, 2],

Check failure on line 181 in gp-limit-dates/gpld-conditional-limits.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Missing space after array opener.

Check failure on line 181 in gp-limit-dates/gpld-conditional-limits.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Short array syntax is not allowed

Check failure on line 181 in gp-limit-dates/gpld-conditional-limits.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Missing space before array closer.
'dateFormat' => 'mdy',
'exceptions' => array( '09/02/2024', '12/25/2024' ),
'inlineDatepicker' => false,
),
'conditionalLogic' => array(
'logicType' => 'any',
'actionType' => 'show',
'rules' => array(
array(
'fieldId' => '1',
'operator' => 'is',
'value' => '68502',
)

Check failure on line 194 in gp-limit-dates/gpld-conditional-limits.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Each array item in a multi-line array declaration must end in a comma
),
),
),
),
) );

0 comments on commit d8ef370

Please sign in to comment.