Forked from spivurno/gw-gravity-forms-choice-counter.php
Created
September 9, 2020 12:56
-
-
Save dexit/3e3f4a14db3e858ba1e8d51f1ab88d73 to your computer and use it in GitHub Desktop.
Gravity Wiz // Gravity Forms // Choice Counter
This file contains hidden or 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
| <?php | |
| /** | |
| * Gravity Wiz // Gravity Forms // Choice Counter | |
| * | |
| * Get the total number of checkboxes checked or multi-select options selected. Useful when wanting to apply conditional | |
| * logic based on those totals. | |
| * | |
| * @version 1.0 | |
| * @author David Smith <[email protected]> | |
| * @license GPL-2.0+ | |
| * @link http://gravitywiz.com/ | |
| */ | |
| class GW_Choice_Count { | |
| private static $is_script_output; | |
| function __construct( $args ) { | |
| $this->_args = wp_parse_args( $args, array( | |
| 'form_id' => false, | |
| 'count_field_id' => false, | |
| 'choice_field_id' => null, | |
| 'choice_field_ids' => false | |
| ) ); | |
| if( isset( $this->_args['choice_field_id'] ) ) { | |
| $this->_args['choice_field_ids'] = array( $this->_args['choice_field_id'] ); | |
| } | |
| add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 ); | |
| add_action( 'gform_register_init_scripts', array( $this, 'add_init_script' ) ); | |
| //add_action( 'gform_pre_validation', array( $this, 'override_submitted_value') ); | |
| } | |
| public function load_form_script( $form, $is_ajax_enabled ) { | |
| if( $this->is_applicable_form( $form ) && ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) { | |
| add_action( 'wp_footer', array( $this, 'output_script' ) ); | |
| add_action( 'gform_preview_footer', array( $this, 'output_script' ) ); | |
| } | |
| return $form; | |
| } | |
| function output_script() { | |
| ?> | |
| <script type="text/javascript"> | |
| ( function( $ ) { | |
| window.GWChoiceCount = function( args ) { | |
| var self = this; | |
| // Copy all args to current object: formId, fieldId. | |
| for ( prop in args ) { | |
| if ( args.hasOwnProperty( prop ) ) { | |
| self[ prop ] = args[prop]; | |
| } | |
| } | |
| self.init = function() { | |
| for( var i = 0; i < self.choiceFieldIds.length; i++ ) { | |
| var choiceFieldId = self.choiceFieldIds[ i ], | |
| $choiceField = $( '#input_' + self.formId + '_' + choiceFieldId ); | |
| if ( self.isCheckboxField( $choiceField ) ) { | |
| $choiceField.find( 'input[type="checkbox"]' ).click( function() { | |
| self.updateChoiceCount( self.formId, self.choiceFieldIds, self.countFieldId ); | |
| } ); | |
| } else { | |
| $choiceField.change( function() { | |
| self.updateChoiceCount( self.formId, self.choiceFieldIds, self.countFieldId ); | |
| } ); | |
| } | |
| } | |
| self.updateChoiceCount( self.formId, self.choiceFieldIds, self.countFieldId ); | |
| }; | |
| self.isCheckboxField = function( $field ) { | |
| return Boolean( $field.find( 'input[type="checkbox"]' ).length ); | |
| } | |
| self.updateChoiceCount = function( formId, choiceFieldIds, countFieldId ) { | |
| var countField = $( '#input_' + formId + '_' + countFieldId ), | |
| count = 0; | |
| for ( var i = 0; i < choiceFieldIds.length; i++ ) { | |
| var $choiceField = $( '#input_' + formId + '_' + choiceFieldIds[ i ] ); | |
| if ( self.isCheckboxField( $choiceField ) ) { | |
| count += $choiceField.find( 'input[type="checkbox"]:checked' ).not(' #choice_' + choiceFieldIds[ i ] + '_select_all').length; | |
| } else { | |
| count += $choiceField.find( 'option:selected' ).length; | |
| } | |
| } | |
| if( parseInt( countField.val() ) != parseInt( count ) ) { | |
| countField.val( count ).change(); | |
| } | |
| }; | |
| self.init(); | |
| } | |
| } )( jQuery ); | |
| </script> | |
| <?php | |
| self::$is_script_output = true; | |
| } | |
| function add_init_script( $form ) { | |
| if( ! $this->is_applicable_form( $form['id'] ) ) | |
| return; | |
| $args = array( | |
| 'formId' => $this->_args['form_id'], | |
| 'countFieldId' => $this->_args['count_field_id'], | |
| 'choiceFieldIds' => $this->_args['choice_field_ids'] | |
| ); | |
| $script = 'new GWChoiceCount( ' . json_encode( $args ) . ' );'; | |
| $slug = implode( '_', array( 'gw_choice_count', $this->_args['form_id'], $this->_args['count_field_id'] ) ); | |
| GFFormDisplay::add_init_script( $this->_args['form_id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script ); | |
| return; | |
| } | |
| function override_submitted_value( $form ) { | |
| //$_POST["input_{$this->count_field_id}"] = $day_count; | |
| return $form; | |
| } | |
| 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_ajax_submission( $form_id, $is_ajax_enabled ) { | |
| return isset( GFFormDisplay::$submission[ $form_id ] ) && $is_ajax_enabled; | |
| } | |
| } | |
| # Configuration | |
| new GW_Choice_Count( array( | |
| 'form_id' => 123, // The ID of your form. | |
| 'count_field_id' => 4, // Any Number field on your form in which the number of checked checkboxes should be dynamically populated; you can configure conditional logic based on the value of this field. | |
| 'choice_field_ids' => array( 5, 6 ) // Any array of Checkbox or Multi-select field IDs which should be counted. | |
| ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment