diff --git a/gp-media-library/gpml-acf-user-image-field.php b/gp-media-library/gpml-acf-user-image-field.php index b13ca2af8..2f5b6b3e0 100644 --- a/gp-media-library/gpml-acf-user-image-field.php +++ b/gp-media-library/gpml-acf-user-image-field.php @@ -7,11 +7,13 @@ * Plugin URI: https://gravitywiz.com/documentation/gravity-forms-media-library/ * Description: Set the user profile image using image uploaded via Gravity Forms. * Author: Gravity Wiz - * Version: 1.2 + * Version: 1.3 * Author URI: https://gravitywiz.com/ */ class GPML_ACF_User_Image_Field { + protected $_args; + public function __construct( $args ) { $this->_args = wp_parse_args( @@ -28,6 +30,7 @@ public function __construct( $args ) { add_action( 'gform_user_registered', array( $this, 'update_user_image_field' ), 10, 3 ); add_action( 'gform_user_updated', array( $this, 'update_user_image_field' ), 10, 3 ); + add_filter( "gform_gravityformsuserregistration_pre_process_feeds_" . $this->_args['form_id'], array( $this, 'disable_gfur_for_metakey' ), 10, 3 ); } @@ -51,6 +54,36 @@ function update_user_image_field( $user_id, $feed, $entry ) { } } + /** + * If using this snippet with GF User Registration, we need to remove the mapping for the metakey to prevent GFUR + * from clearing out the value if an existing file is already set. + */ + function disable_gfur_for_metakey( $feeds, $entry, $form ) { + foreach ( $feeds as &$feed ) { + // If the feed is not set to update, skip it. + if ( rgars( $feed, 'meta/feedType' ) !== 'update' ) { + continue; + } + + $dynamic_field_map_fields = rgars( $feed, 'meta/userMeta' ); + + if ( empty( $dynamic_field_map_fields ) || ! is_array( $dynamic_field_map_fields ) ) { + continue; + } + + // Remove the metakey mapping. + foreach ( $dynamic_field_map_fields as $difm_index => $dynamic_field_map_field ) { + if ( $dynamic_field_map_field['key'] === $this->_args['meta_key'] ) { + unset( $dynamic_field_map_fields[$difm_index] ); + } + } + + $feed['meta']['userMeta'] = $dynamic_field_map_fields; + } + + return $feeds; + } + } # Configuration