Skip to content

gppa-force-lmt-populate-on-edit.php: Added snippet to update Live Merge Tags when editing entries. #1093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions gp-populate-anything/gppa-force-lmt-populate-on-edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Gravity Forms // Populate Anything // Force Dynamic Population When Editing Entry.
* https://gravitywiz.com/documentation/gravity-forms-populate-anything/
*
* Instruction Video: https://www.loom.com/share/6a7f28fc0cde406798d5bad4b386a70a
*
* Use this snippet to force fields to be dynamically repopulated via Populate Anything when they are
* edited via the Gravity Forms edit entry screen.
*/
add_action( 'gform_after_update_entry', function ( $form, $entry_id ) {
$gppa_lmt = GP_Populate_Anything_Live_Merge_Tags::get_instance();
$entry = GFAPI::get_entry( $entry_id );
Comment on lines +11 to +13
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Missing error handling for entry retrieval.

The code retrieves an entry but doesn't validate whether it was successful. If the entry doesn't exist or there's an API error, this could lead to PHP notices or errors.

 add_action( 'gform_after_update_entry', function ( $form, $entry_id ) {
 	$gppa_lmt = GP_Populate_Anything_Live_Merge_Tags::get_instance();
 	$entry    = GFAPI::get_entry( $entry_id );
+	if ( is_wp_error( $entry ) ) {
+		return;
+	}

foreach ( $form['fields'] as $field ) {
// For any field having Live Merge Tags.
if ( $gppa_lmt->has_live_merge_tag( $field->defaultValue ) ) {
$gppa_lmt->populate_lmt_whitelist( $form );
remove_all_filters('gform_pre_replace_merge_tags');
Comment on lines +17 to +18
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Potential side effect when removing filters.

The remove_all_filters('gform_pre_replace_merge_tags') call removes all filters, which could affect other functionality that relies on these filters. Consider using a more targeted approach or restoring filters afterward.


🏁 Script executed:

#!/bin/bash
# Check if other code is using the same filter
rg "add_filter\(\s*['\"]gform_pre_replace_merge_tags['\"]" --type php

Length of output: 1284


🏁 Script executed:

#!/bin/bash
# Display the first 200 lines of the file to understand the context around the remove_all_filters call
sed -n '1,200p' gp-populate-anything/gppa-force-lmt-populate-on-edit.php

Length of output: 1253


Avoid Removing All gform_pre_replace_merge_tags Filters

Removing all filters on gform_pre_replace_merge_tags will clear callbacks added by other plugins and add-ons (e.g. gw-cache-buster, gw-all-fields-template, gw-format-date-merge-tags, gw-merge-tag-tab, gw-multi-file-merge-tag, gppa-page-modifier, gp-advanced-phone-field, etc.), likely breaking their functionality.

Consider one of these targeted approaches:

  • Remove only your own callback instead of all filters:
    remove_filter(
      'gform_pre_replace_merge_tags',
      [ $gppa_lmt, 'your_specific_callback' ],
      $your_callback_priority,
      $your_callback_arg_count
    );
  • Or back up & restore the original filter list around your merge-tag processing:
    $tag              = 'gform_pre_replace_merge_tags';
    $original_filters = $GLOBALS['wp_filter'][ $tag ] ?? [];
    
    // Whitelist and add only necessary WP filters
    $gppa_lmt->populate_lmt_whitelist( $form );
    
    // Process merge tags
    $value = GFCommon::replace_variables( $merge_tag, $form, $entry );
    
    // Restore all other filters
    $GLOBALS['wp_filter'][ $tag ] = $original_filters;

Please update gppa-force-lmt-populate-on-edit.php to use one of these patterns so you don’t unintentionally disable other integrations.


// Process the Live Merge Tags.
$merge_tag = preg_replace( '/@(?=\{)/', '', $field->defaultValue );
$value = GFCommon::replace_variables( $merge_tag, $form, $entry );

// Store updated value on the entry.
GFFormsModel::update_entry_field_value( $form, $entry, $field, '', $field->id, $value );
}
}
}, 15, 2 );
Comment on lines +1 to +28
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider validating plugin dependencies.

The code assumes that GP Populate Anything and its classes are available. Consider adding a check to ensure the required classes exist before executing the main functionality.

<?php
/**
 * Gravity Forms // Populate Anything // Force Dynamic Population When Editing Entry.
 * https://gravitywiz.com/documentation/gravity-forms-populate-anything/
 *
 * Instruction Video: https://www.loom.com/share/6a7f28fc0cde406798d5bad4b386a70a
 *
 * Use this snippet to force fields to be dynamically repopulated via Populate Anything when they are
 * edited via the Gravity Forms edit entry screen.
 */
+// Exit if accessed directly
+if ( ! defined( 'ABSPATH' ) ) {
+	exit;
+}
+
+// Check if required plugins/classes exist
+if ( ! class_exists( 'GP_Populate_Anything_Live_Merge_Tags' ) || ! class_exists( 'GFAPI' ) ) {
+	return;
+}
+
 add_action( 'gform_after_update_entry', function ( $form, $entry_id ) {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<?php
/**
* Gravity Forms // Populate Anything // Force Dynamic Population When Editing Entry.
* https://gravitywiz.com/documentation/gravity-forms-populate-anything/
*
* Instruction Video: https://www.loom.com/share/6a7f28fc0cde406798d5bad4b386a70a
*
* Use this snippet to force fields to be dynamically repopulated via Populate Anything when they are
* edited via the Gravity Forms edit entry screen.
*/
add_action( 'gform_after_update_entry', function ( $form, $entry_id ) {
$gppa_lmt = GP_Populate_Anything_Live_Merge_Tags::get_instance();
$entry = GFAPI::get_entry( $entry_id );
foreach ( $form['fields'] as $field ) {
// For any field having Live Merge Tags.
if ( $gppa_lmt->has_live_merge_tag( $field->defaultValue ) ) {
$gppa_lmt->populate_lmt_whitelist( $form );
remove_all_filters('gform_pre_replace_merge_tags');
// Process the Live Merge Tags.
$merge_tag = preg_replace( '/@(?=\{)/', '', $field->defaultValue );
$value = GFCommon::replace_variables( $merge_tag, $form, $entry );
// Store updated value on the entry.
GFFormsModel::update_entry_field_value( $form, $entry, $field, '', $field->id, $value );
}
}
}, 15, 2 );
<?php
/**
* Gravity Forms // Populate Anything // Force Dynamic Population When Editing Entry.
* https://gravitywiz.com/documentation/gravity-forms-populate-anything/
*
* Instruction Video: https://www.loom.com/share/6a7f28fc0cde406798d5bad4b386a70a
*
* Use this snippet to force fields to be dynamically repopulated via Populate Anything when they are
* edited via the Gravity Forms edit entry screen.
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Check if required plugins/classes exist
if ( ! class_exists( 'GP_Populate_Anything_Live_Merge_Tags' ) || ! class_exists( 'GFAPI' ) ) {
return;
}
add_action( 'gform_after_update_entry', function ( $form, $entry_id ) {
$gppa_lmt = GP_Populate_Anything_Live_Merge_Tags::get_instance();
$entry = GFAPI::get_entry( $entry_id );
foreach ( $form['fields'] as $field ) {
// For any field having Live Merge Tags.
if ( $gppa_lmt->has_live_merge_tag( $field->defaultValue ) ) {
$gppa_lmt->populate_lmt_whitelist( $form );
remove_all_filters( 'gform_pre_replace_merge_tags' );
// Process the Live Merge Tags.
$merge_tag = preg_replace( '/@(?=\{)/', '', $field->defaultValue );
$value = GFCommon::replace_variables( $merge_tag, $form, $entry );
// Store updated value on the entry.
GFFormsModel::update_entry_field_value( $form, $entry, $field, '', $field->id, $value );
}
}
}, 15, 2 );

Loading