-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gpld-conditional-limits.php
: Added a new snippet to enable conditio…
…nal date limits.
- Loading branch information
Showing
1 changed file
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
<?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() ) { | ||
if ( ! function_exists( 'gp_limit_dates' ) ) { | ||
return; | ||
} | ||
|
||
// 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 ) { | ||
|
||
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 | ||
} | ||
}); | ||
|
||
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']; | ||
} | ||
|
||
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( | ||
'minDate' => '{today}', | ||
'minDateMod' => '+2 days', | ||
'daysOfWeek' => [1], | ||
Check failure on line 164 in gp-limit-dates/gpld-conditional-limits.php GitHub Actions / PHPCS (Files Changed)
Check failure on line 164 in gp-limit-dates/gpld-conditional-limits.php GitHub Actions / PHPCS (Files Changed)
|
||
'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 185 in gp-limit-dates/gpld-conditional-limits.php GitHub Actions / PHPCS (Files Changed)
Check failure on line 185 in gp-limit-dates/gpld-conditional-limits.php GitHub Actions / PHPCS (Files Changed)
|
||
'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', | ||
) | ||
), | ||
), | ||
), | ||
), | ||
) ); |