Last active
September 25, 2025 13:49
-
-
Save geekontheroad/cdfa3a416887dd80de87550832b99fa7 to your computer and use it in GitHub Desktop.
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 | |
| /** | |
| * Class to automatically add fields to the Gravity Forms live summary. | |
| * It supports adding all supported field types or selected field types. | |
| * This class only runs when saving the applicable form through the GF form Editor (press save) | |
| * | |
| * How to use this? | |
| * 1. Copy the entire snippet except the opening PHP tag. | |
| * 2. Paste it into your theme's functions.php file or a custom plugin. | |
| * 3. Modify the configuration at the bottom of the snippet | |
| * | |
| * To define selected types to syn, you'll have to use the official naming in the GF plugin. | |
| * Here is a list of current field types that can be shown in the summary: | |
| * text, radio, checkbox, select, number, email, textarea, date, phone, website, name, address, multiselect, | |
| * username, product, time, shipping, subtotal, tax, discount, gotrgf_eu_vat_field, coupon, section, hidden, | |
| * image_choice, multi_choice | |
| * | |
| * @version 0.1 | |
| * @author Geek on the Road | |
| */ | |
| class GOTR_Summary_Auto_Add_Fields { | |
| /** | |
| * Arguments for the instance. | |
| * @since 0.1 | |
| * @var array | |
| */ | |
| private $_args = array(); | |
| /** | |
| * Field IDs to sync. | |
| * @since 0.1 | |
| * @var array | |
| */ | |
| private $_field_ids_to_sync = array(); | |
| /** | |
| * Supported field types grabbed from the LS summary instance. | |
| * @since 0.1 | |
| * @var array | |
| */ | |
| private $_supported_field_types = array(); | |
| /** | |
| * Live Summary instance. | |
| * @since 0.1 | |
| * @var object | |
| */ | |
| private $_summary_instance = null; | |
| /** | |
| * Form instance. | |
| * @since 0.1 | |
| * @var object | |
| */ | |
| private $_form = null; | |
| /** | |
| * Constructor. | |
| */ | |
| public function __construct( $args = array() ) { | |
| // 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, | |
| 'side_summary' => false, | |
| 'summary_fields' => array(), | |
| 'field_types_to_load' => array( 'product' ) | |
| ) ); | |
| // do version check in the init to make sure if GF is going to be loaded, it is already loaded | |
| add_action( 'init', array( $this, 'init' ) ); | |
| } | |
| /** | |
| * Initialize the class. | |
| * | |
| * @since 0.1 | |
| * | |
| * @return void | |
| */ | |
| public function init() { | |
| if( ! class_exists('GFAPI') || ! function_exists('gotrls_get_instance') ) { | |
| return; | |
| } | |
| $this->_summary_instance = gotrls_get_instance(); | |
| $this->_supported_field_types = $this->_summary_instance->supported_fields; | |
| add_action( 'gform_after_save_form', array( $this, 'sync_selected_summary_fields' ), 10, 2 ); | |
| } | |
| /** | |
| * Sync selected summary fields when the applicable form is saved. | |
| * | |
| * @since 0.1 | |
| * | |
| * @return void | |
| */ | |
| public function sync_selected_summary_fields( $form, $is_new ) { | |
| if( ! $this->is_applicable_form( $form ) ) { | |
| return; | |
| } | |
| $this->_form = $form; | |
| $this->store_field_ids_to_sync(); | |
| if ( $this->_args['side_summary'] ) { | |
| $this->sync_side_summary(); | |
| } | |
| if ( is_array( $this->_args['summary_fields'] ) && ! empty( $this->_args['summary_fields'] ) ) { | |
| foreach ( $this->_args['summary_fields'] as $field_id ) { | |
| $this->sync_summary_field( $field_id); | |
| } | |
| } | |
| GFAPI::update_form( $this->_form ); | |
| } | |
| /** | |
| * Sync side summary fields. | |
| * | |
| * @since 0.1 | |
| * | |
| * @return void | |
| */ | |
| public function sync_side_summary() { | |
| $current_selected_fields = $this->_form['gravitysummary']['gotrgf_summary_fields_selector_selectedFields'] ?? array(); | |
| $current_selected_fields = explode( ',', $current_selected_fields ); | |
| $merge = array_unique( array_merge( $current_selected_fields, $this->_field_ids_to_sync ) ); | |
| $this->_form['gravitysummary']['gotrgf_summary_fields_selector_selectedFields'] = implode( ',', $merge ); | |
| } | |
| /** | |
| * Sync summary field. | |
| * | |
| * @since 0.1 | |
| * | |
| * @return void | |
| */ | |
| public function sync_summary_field( $field_id) { | |
| $field = GFAPI::get_field( $this->_form['id'], $field_id ); | |
| if ( property_exists( $field, 'gotr_use_side_summary_fields' ) && $field->gotr_use_side_summary_fields ) { | |
| $this->sync_side_summary(); | |
| return; | |
| } | |
| if ( property_exists( $field, 'selectedSummaries' ) && $field->selectedSummaries != '' ) { | |
| $current_selected_fields = explode( ',', $field->selectedSummaries ); | |
| $merge = array_unique( array_merge( $current_selected_fields, $this->_field_ids_to_sync ) ); | |
| foreach ( $this->_form['fields'] as &$form_field ) { | |
| if ( (int) $form_field->id === (int) $field_id ) { | |
| $form_field->selectedSummaries = implode( ',', $merge ); | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| /** | |
| * Store field IDs to sync. | |
| * | |
| * @since 0.1 | |
| * | |
| * @return void | |
| */ | |
| public function store_field_ids_to_sync() { | |
| $field_ids = array(); | |
| $types = $this->_args['field_types_to_load']; | |
| $fields = array(); | |
| if ( is_array( $types ) && ! empty( $types ) ) { | |
| $fields = GFAPI::get_fields_by_type( $this->_form, $types ); | |
| } else if ( is_array( $types ) && empty( $types ) ) { | |
| $all_fields = $this->_form['fields']; | |
| $fields = array_filter( $all_fields, function( $field ) { | |
| return in_array( $field->type, $this->_supported_field_types ); | |
| } ); | |
| } | |
| foreach ( $fields as $field ) { | |
| $field_ids[] = $field->id; | |
| } | |
| $this->_field_ids_to_sync = $field_ids; | |
| } | |
| /** | |
| * Check if the form is applicable for this instance. | |
| * | |
| * @since 0.1 | |
| * | |
| * @param array $form Form data. | |
| * @return bool | |
| */ | |
| public function is_applicable_form( $form ) { | |
| $form_id = isset( $form['id'] ) ? $form['id'] : $form; | |
| return empty( $this->_args['form_id'] ) || (int) $form_id === (int) $this->_args['form_id']; | |
| } | |
| } | |
| # Configuration | |
| $args = array( | |
| 'form_id' => 34, // Form Id this instance should run for | |
| 'side_summary' => true, // Activate for side summary | |
| 'summary_fields' => array(), // Provide comma seperated list between the array brackets of summary field ids to activate auto loading. Example: array(2,3,5) | |
| 'field_types_to_load' => array() // leave the array empty to auto load all fields. Otherwise define a list of GF field types. Example: array('text', 'radio') | |
| ); | |
| new GOTR_Summary_Auto_Add_Fields( $args ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment