From f861363e344422942b65c324193df705d88511e4 Mon Sep 17 00:00:00 2001 From: omarkasem Date: Mon, 30 Sep 2024 21:44:43 +0300 Subject: [PATCH 1/5] Fixes reversing issue --- templates/fields/field-survey-html.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/templates/fields/field-survey-html.php b/templates/fields/field-survey-html.php index 2a4a3738e..ae9ff99a8 100644 --- a/templates/fields/field-survey-html.php +++ b/templates/fields/field-survey-html.php @@ -143,6 +143,12 @@ } $choices = $field->field->choices; + + // If the choices are reversed, reverse them back. + if( !empty($choices) && $choices[0]['text'] === 'Excellent' ){ + $choices = array_reverse( $choices ); + } + $choice_values = wp_list_pluck( $choices, 'value', $gravityview->value ); $starred_index = array_search( $gravityview->value, $choice_values ); $star_a11y_label = $starred_index !== false From f2f7a7338be792ee4f907ffb3c6d8ea821f90337 Mon Sep 17 00:00:00 2001 From: omarkasem Date: Mon, 30 Sep 2024 21:48:58 +0300 Subject: [PATCH 2/5] Fixes spacing --- templates/fields/field-survey-html.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/fields/field-survey-html.php b/templates/fields/field-survey-html.php index ae9ff99a8..99be95377 100644 --- a/templates/fields/field-survey-html.php +++ b/templates/fields/field-survey-html.php @@ -145,7 +145,7 @@ $choices = $field->field->choices; // If the choices are reversed, reverse them back. - if( !empty($choices) && $choices[0]['text'] === 'Excellent' ){ + if ( ! empty( $choices ) && $choices[0]['text'] === 'Excellent' ) { $choices = array_reverse( $choices ); } From 0cd996de8b7e9be9e5be1e59a56ba2c32b3e006e Mon Sep 17 00:00:00 2001 From: omarkasem Date: Wed, 2 Oct 2024 21:26:07 +0300 Subject: [PATCH 3/5] Gets choices directly from database to avoid reversal issues --- .../fields/class-gravityview-field-survey.php | 30 +++++++++++++++++++ templates/fields/field-survey-html.php | 8 ++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/includes/fields/class-gravityview-field-survey.php b/includes/fields/class-gravityview-field-survey.php index 1bb286600..0a25c6ff0 100644 --- a/includes/fields/class-gravityview-field-survey.php +++ b/includes/fields/class-gravityview-field-survey.php @@ -59,6 +59,36 @@ public static function get_choice_score( $field, $value, $input_id = 0 ) { return is_array( $value ) ? '' : $value; } + /** + * Get field choices directly from the database (To avoid issues where choices are reversed by the survey plugin). + * + * @since TBD + * + * @param int $form_id The ID of the form. + * @param int $field_id The ID of the field. + * + * @return array The choices for the field. + */ + public static function get_field_choices($form_id, $field_id) { + global $wpdb; + $table_name = GFFormsModel::get_meta_table_name(); + $form_row = $wpdb->get_row( $wpdb->prepare( "SELECT display_meta FROM {$table_name} WHERE form_id=%d", $form_id ), ARRAY_A ); + $form_meta = GFFormsModel::unserialize( rgar( $form_row, 'display_meta' ) ); + + $field_choices = []; + if ( $form_meta ) { + foreach ( $form_meta['fields'] as $form_field ) { + if ( $form_field['id'] == $field_id ) { + $field_choices = $form_field['choices']; + break; + } + } + } + + return $field_choices; + } + + public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) { unset( $field_options['search_filter'] ); diff --git a/templates/fields/field-survey-html.php b/templates/fields/field-survey-html.php index 99be95377..f3b52c432 100644 --- a/templates/fields/field-survey-html.php +++ b/templates/fields/field-survey-html.php @@ -15,6 +15,7 @@ $field = $gravityview->field; $display_value = $gravityview->display_value; $input_id = gravityview_get_input_id_from_id( $field->ID ); +$form_id = $gravityview->view->form->ID; // Used in filters below. $return_true = function () { @@ -142,12 +143,7 @@ return; } - $choices = $field->field->choices; - - // If the choices are reversed, reverse them back. - if ( ! empty( $choices ) && $choices[0]['text'] === 'Excellent' ) { - $choices = array_reverse( $choices ); - } + $choices = GravityView_Field_Survey::get_field_choices( $form_id, $field->ID ); $choice_values = wp_list_pluck( $choices, 'value', $gravityview->value ); $starred_index = array_search( $gravityview->value, $choice_values ); From 9e98a5f8cf5e68ce52e8b3252e1ae42c1c81764d Mon Sep 17 00:00:00 2001 From: omarkasem Date: Wed, 9 Oct 2024 17:49:26 +0300 Subject: [PATCH 4/5] Makes code more readable --- .../fields/class-gravityview-field-survey.php | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/includes/fields/class-gravityview-field-survey.php b/includes/fields/class-gravityview-field-survey.php index 0a25c6ff0..75055aecb 100644 --- a/includes/fields/class-gravityview-field-survey.php +++ b/includes/fields/class-gravityview-field-survey.php @@ -60,7 +60,7 @@ public static function get_choice_score( $field, $value, $input_id = 0 ) { } /** - * Get field choices directly from the database (To avoid issues where choices are reversed by the survey plugin). + * Gets field choices directly from the database (To avoid issues where choices are reversed by the survey plugin). * * @since TBD * @@ -73,19 +73,19 @@ public static function get_field_choices($form_id, $field_id) { global $wpdb; $table_name = GFFormsModel::get_meta_table_name(); $form_row = $wpdb->get_row( $wpdb->prepare( "SELECT display_meta FROM {$table_name} WHERE form_id=%d", $form_id ), ARRAY_A ); - $form_meta = GFFormsModel::unserialize( rgar( $form_row, 'display_meta' ) ); - - $field_choices = []; - if ( $form_meta ) { - foreach ( $form_meta['fields'] as $form_field ) { - if ( $form_field['id'] == $field_id ) { - $field_choices = $form_field['choices']; - break; - } + $form_meta = GFFormsModel::unserialize( rgar( $form_row, 'display_meta' ) ); + + if ( ! $form_meta ) { + return []; + } + + foreach ( $form_meta['fields'] as $form_field ) { + if ( $form_field['id'] == $field_id ) { + return $form_field['choices']; } } - return $field_choices; + return []; } From 209c90f51fa7543c69b3681e4599579d5daa312f Mon Sep 17 00:00:00 2001 From: Vlad Date: Mon, 14 Oct 2024 12:54:14 -0400 Subject: [PATCH 5/5] Update changelog [ci skip] --- readme.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/readme.txt b/readme.txt index 0b9d96ddf..0fbae5e09 100644 --- a/readme.txt +++ b/readme.txt @@ -31,6 +31,7 @@ Beautifully display your Gravity Forms entries. Learn more on [gravitykit.com](h * Fixed: Resending notifications from the Entries screen did not work when sending to all entries filtered by approval status. * Fixed: Conflict with the Wordfence plugin caused a fatal error when redirecting users after deleting an entry. * Fixed: Fatal error when rendering a GravityView View field with a non-existent View ID. +* Fixed: Survey field (Rating type) values displaying in reverse order when a View is embedded inside another View. = 2.29 on October 1, 2024 =