From 43f1a734548f96f2934c89ac73aecc281f66e9fe Mon Sep 17 00:00:00 2001 From: saifsultanc Date: Fri, 2 Dec 2022 08:18:44 +0530 Subject: [PATCH 1/4] `gw-choice-counter.php`: Updated snippet with a values parameter to count checkboxes that match the values given. --- gravity-forms/gw-choice-counter.php | 30 +++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/gravity-forms/gw-choice-counter.php b/gravity-forms/gw-choice-counter.php index 2745213bd..40d3f6447 100644 --- a/gravity-forms/gw-choice-counter.php +++ b/gravity-forms/gw-choice-counter.php @@ -23,6 +23,7 @@ function __construct( $args ) { 'count_field_id' => false, 'choice_field_id' => null, 'choice_field_ids' => false, + 'values' => false, ) ); if ( isset( $this->_args['choice_field_id'] ) ) { @@ -86,7 +87,7 @@ function output_script() { // Event handler for all listeners to avoid DRY and to maintain a pointer reference to the function // which we can use to explicity unbind event handlers self.updateChoiceEventHandler = function() { - self.updateChoiceCount( self.formId, self.choiceFieldIds, self.countFieldId ); + self.updateChoiceCount( self.formId, self.choiceFieldIds, self.countFieldId, self.values ); }; self.init = function() { @@ -107,7 +108,7 @@ function output_script() { return Boolean( $field.find( 'input[type="checkbox"]' ).length ); } - self.updateChoiceCount = function( formId, choiceFieldIds, countFieldId ) { + self.updateChoiceCount = function( formId, choiceFieldIds, countFieldId, values ) { var countField = $( '#input_' + formId + '_' + countFieldId ), count = 0; @@ -115,10 +116,21 @@ function output_script() { 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; + if ( values.length == 0) { + 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; + } } else { - count += $choiceField.find( 'option:selected' ).length; + var selectedValues = []; + $choiceField.find( 'input[type="checkbox"]:checked' ).each( function( k, $selectedChoice ) { + selectedValues.push( $selectedChoice.value ); + }); + var result = values.every( function( val ) { + return selectedValues.indexOf( val ) >= 0; + }); + count += result; } } @@ -151,6 +163,7 @@ function add_init_script( $form ) { 'formId' => $this->_args['form_id'], 'countFieldId' => $this->_args['count_field_id'], 'choiceFieldIds' => $this->_args['choice_field_ids'], + 'values' => $this->_args['values'], ); $script = 'new GWChoiceCount( ' . json_encode( $args ) . ' );'; @@ -182,7 +195,8 @@ public function is_ajax_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. + '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. + 'values' => array( 'None of the above' ), // Array of values, all of which should match. ) ); From 39fc24cf45e339f1e75ee22374c692109376be2a Mon Sep 17 00:00:00 2001 From: saifsultanc Date: Sat, 3 Dec 2022 00:11:41 +0530 Subject: [PATCH 2/4] `gw-choice-counter.php`: Updated snippet with a values parameter to count checkboxes that match the values given. --- gravity-forms/gw-choice-counter.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/gravity-forms/gw-choice-counter.php b/gravity-forms/gw-choice-counter.php index 40d3f6447..d8d336607 100644 --- a/gravity-forms/gw-choice-counter.php +++ b/gravity-forms/gw-choice-counter.php @@ -127,10 +127,9 @@ function output_script() { $choiceField.find( 'input[type="checkbox"]:checked' ).each( function( k, $selectedChoice ) { selectedValues.push( $selectedChoice.value ); }); - var result = values.every( function( val ) { - return selectedValues.indexOf( val ) >= 0; + values.forEach( function( val ) { + count += selectedValues.indexOf( val ) >= 0; }); - count += result; } } From cb833da084871229df0e893038e09a9dfcbbbb49 Mon Sep 17 00:00:00 2001 From: saifsultanc Date: Tue, 6 Dec 2022 00:07:03 +0530 Subject: [PATCH 3/4] `gw-choice-counter.php`: Updated snippet with a values parameter to count checkboxes that match the values given. --- gravity-forms/gw-choice-counter.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gravity-forms/gw-choice-counter.php b/gravity-forms/gw-choice-counter.php index d8d336607..2d927aab2 100644 --- a/gravity-forms/gw-choice-counter.php +++ b/gravity-forms/gw-choice-counter.php @@ -117,12 +117,14 @@ function output_script() { var $choiceField = $( '#input_' + formId + '_' + choiceFieldIds[ i ] ); if ( values.length == 0) { + // If no values provided in the config, just get the number of checkboxes checked. 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; } } else { + // When values are provided, match the values before adding them to count. var selectedValues = []; $choiceField.find( 'input[type="checkbox"]:checked' ).each( function( k, $selectedChoice ) { selectedValues.push( $selectedChoice.value ); From 5df6d2d7ae0c7279dc3b2c59c803a38b8bc9f97e Mon Sep 17 00:00:00 2001 From: saifsultanc Date: Tue, 6 Dec 2022 16:35:08 +0530 Subject: [PATCH 4/4] `gw-choice-counter.php`: Updated snippet with a values parameter to count checkboxes that match the values given. --- gravity-forms/gw-choice-counter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gravity-forms/gw-choice-counter.php b/gravity-forms/gw-choice-counter.php index 2d927aab2..878c640a4 100644 --- a/gravity-forms/gw-choice-counter.php +++ b/gravity-forms/gw-choice-counter.php @@ -116,7 +116,7 @@ function output_script() { for ( var i = 0; i < choiceFieldIds.length; i++ ) { var $choiceField = $( '#input_' + formId + '_' + choiceFieldIds[ i ] ); - if ( values.length == 0) { + if ( ! values ) { // If no values provided in the config, just get the number of checkboxes checked. if ( self.isCheckboxField( $choiceField ) ) { count += $choiceField.find( 'input[type="checkbox"]:checked' ).not(' #choice_' + choiceFieldIds[ i ] + '_select_all').length;