Last active
September 4, 2025 05:45
-
-
Save saifsultanc/e1bdd99bf9e434a9c665da88e82627e3 to your computer and use it in GitHub Desktop.
87744-snippet.php
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 GPPA_Populate_Child_Entries { | |
| public function __construct( $args = array() ) { | |
| // 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' ) ); | |
| } | |
| public function init() { | |
| if ( ! is_callable( 'gp_nested_forms' ) ) { | |
| return; | |
| } | |
| add_filter( 'gform_field_value_gpnf_parent_entry_id', array( $this, 'populate_parent_entry_id' ), 10, 2 ); | |
| add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 ); | |
| // Priority 11 so that it will initialize *after* Nested Forms. | |
| add_filter( 'gform_register_init_scripts', array( $this, 'add_init_script' ), 11, 2 ); | |
| add_action( 'gravityview/edit-entry/render/before', array( $this, 'modify_gv_entry' ) ); | |
| add_action( 'gpeb_before_render_block', array( $this, 'modify_gpeb_entry' ) ); | |
| } | |
| public function populate_parent_entry_id( $value, $field ) { | |
| if ( is_callable( 'gravityview_get_context' ) && gravityview_get_context() === 'edit' ) { | |
| $value = GravityView_frontend::is_single_entry(); | |
| } elseif ( rgget( 'edit_entry' ) ) { | |
| $value = rgget( 'edit_entry' ); | |
| } else { | |
| $session = new GPNF_Session( $field->formId ); | |
| $cookie = $session->get_cookie(); | |
| $value = rgar( $cookie, 'hash', '' ); | |
| } | |
| return $value; | |
| } | |
| /** | |
| * GV passes the full parent entry as field values when rendering the entry for editing. Populate Anything honors the | |
| * submitted entry values rather than using the prepopulated values. To avoid this, let's unset those values from the | |
| * entry before GV attempts to render the form. | |
| * | |
| * @param $gv_edit_entry_renderer | |
| */ | |
| public function modify_gv_entry( $gv_edit_entry_renderer ) { | |
| $parent_entry_id_field_id = $this->get_parent_entry_id_field( $gv_edit_entry_renderer->form ); | |
| if ( $parent_entry_id_field_id ) { | |
| unset( $gv_edit_entry_renderer->entry[ $parent_entry_id_field_id ] ); | |
| } | |
| } | |
| /** | |
| * Modify the Gravity Forms Entry Block before rendering. | |
| * | |
| * @param $block | |
| */ | |
| public function modify_gpeb_entry( $block ) { | |
| $form = GFAPI::get_form( $block->context['gp-entry-blocks/formId'] ); | |
| $entry = GFAPI::get_entry( rgget( 'edit_entry' ) ); | |
| $parent_entry_id_field_id = $this->get_parent_entry_id_field( $form ); | |
| if ( $entry && ! is_wp_error( $entry ) && $parent_entry_id_field_id ) { | |
| unset( $entry[ $parent_entry_id_field_id ] ); | |
| $entry[ $parent_entry_id_field_id ] = rgget( 'edit_entry' ); | |
| GFAPI::update_entry( $entry ); | |
| } | |
| } | |
| 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; | |
| } | |
| public function output_script() { | |
| ?> | |
| <script type="text/javascript"> | |
| ( function( $ ) { | |
| window.GPPAPopulateChildEntries = function( args ) { | |
| var self = this; | |
| // copy all args to current object: (list expected props) | |
| for( var prop in args ) { | |
| if( args.hasOwnProperty( prop ) ) { | |
| self[ prop ] = args[ prop ]; | |
| } | |
| } | |
| self.init = function() { | |
| self.$peidField = $( '#input_{0}_{1}'.gformFormat( self.formId, self.fieldId ) ); | |
| // Make sure cookies are set. | |
| gform.addAction( 'gpnf_session_initialized', function() { | |
| self.setupPeidField(); | |
| } ); | |
| }; | |
| self.setupPeidField = function() { | |
| var gpnfCookie = $.parseJSON( self.getCookie( self.cookieName )); | |
| if ( ! self.$peidField.val() && gpnfCookie ) { | |
| self.$peidField | |
| .val( gpnfCookie.hash ) | |
| .change(); | |
| } | |
| for ( var i = 0; i < self.nestedFormFieldIds.length; i++ ) { | |
| window[ 'GPNestedForms_{0}_{1}'.gformFormat( self.formId, self.nestedFormFieldIds[ i ] ) ].viewModel.entries.subscribe( function( entries ) { | |
| self.$peidField.data( 'lastValue', '' ).change(); | |
| } ); | |
| } | |
| } | |
| self.getCookie = function( name ) { | |
| var cookieArr = document.cookie.split( ';' ); | |
| for( var i = 0; i < cookieArr.length; i++ ) { | |
| var cookiePair = cookieArr[ i ].split( '=' ); | |
| if ( name == cookiePair[0].trim() ) { | |
| return decodeURIComponent( cookiePair[1] ); | |
| } | |
| } | |
| return null; | |
| } | |
| self.init(); | |
| } | |
| } )( jQuery ); | |
| </script> | |
| <?php | |
| } | |
| public function add_init_script( $form ) { | |
| if ( ! $this->is_applicable_form( $form ) ) { | |
| return; | |
| } | |
| $args = array( | |
| 'formId' => $form['id'], | |
| 'fieldId' => $this->get_parent_entry_id_field( $form ), | |
| 'nestedFormFieldIds' => wp_list_pluck( $this->get_nested_form_fields( $form ), 'id' ), | |
| 'cookieName' => ( new GPNF_Session( $form['id'] ) )->get_cookie_name(), | |
| ); | |
| $script = 'new GPPAPopulateChildEntries( ' . json_encode( $args ) . ' );'; | |
| $slug = implode( '_', array( 'gppa_populate_child_entries', $args['formId'], $args['fieldId'] ) ); | |
| GFFormDisplay::add_init_script( $args['formId'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script ); | |
| } | |
| public function is_applicable_form( $form ) { | |
| return $this->get_parent_entry_id_field( $form ) !== false && ! rgempty( $this->get_nested_form_fields( $form ) ); | |
| } | |
| public function get_parent_entry_id_field( $form ) { | |
| foreach ( $form['fields'] as $field ) { | |
| if ( $field->inputName === 'gpnf_parent_entry_id' ) { | |
| return $field->id; | |
| } | |
| } | |
| return false; | |
| } | |
| public function get_nested_form_fields( $form ) { | |
| return GFAPI::get_fields_by_type( $form, 'form', false ); | |
| } | |
| } | |
| # Configuration | |
| new GPPA_Populate_Child_Entries(); | |
| // EXTRA SNIPPET | |
| add_filter( 'gpnf_set_parent_entry_id', function( $parent_entry_id ) { | |
| if ( rgget( 'edit_entry' ) ) { | |
| $parent_entry_id = rgget( 'edit_entry' ); | |
| } | |
| return $parent_entry_id; | |
| } ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment