From d8ef370be4eb7ce2cd7860fc5a7384815721a176 Mon Sep 17 00:00:00 2001 From: barthc Date: Sat, 7 Sep 2024 16:57:46 +0100 Subject: [PATCH] `gpld-conditional-limits.php`: Added a new snippet to enable conditional date limits. --- gp-limit-dates/gpld-conditional-limits.php | 199 +++++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 gp-limit-dates/gpld-conditional-limits.php diff --git a/gp-limit-dates/gpld-conditional-limits.php b/gp-limit-dates/gpld-conditional-limits.php new file mode 100644 index 00000000..37dc1397 --- /dev/null +++ b/gp-limit-dates/gpld-conditional-limits.php @@ -0,0 +1,199 @@ +_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() { + ?> + + + + 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], + '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], + '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', + ) + ), + ), + ), + ), +) );