Skip to content

Commit 1fbf680

Browse files
authored
gw-update-posts.php: Added support for updating sub fields in an ACF Group field.
1 parent 3602b36 commit 1fbf680

File tree

1 file changed

+63
-29
lines changed

1 file changed

+63
-29
lines changed

gravity-forms/gw-update-posts.php

Lines changed: 63 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* Update existing post title, content, author and custom fields with values from Gravity Forms.
66
*
7-
* @version 0.4.3
7+
* @version 0.5
88
* @author Scott Ryer <scott@gravitywiz.com>
99
* @license GPL-2.0+
1010
* @link http://gravitywiz.com
@@ -134,48 +134,82 @@ public function update_post_by_entry( $entry, $form ) {
134134
}
135135

136136
if ( $this->_args['meta'] ) {
137+
$post->meta_input = $this->prepare_meta_input( $this->_args['meta'], $post->ID, $entry, $form );
138+
}
137139

138-
$meta_input = array();
140+
// ensure the fires after hooks is set to false, so that doesn't override some of the normal rendering - GF confirmation for instance.
141+
wp_update_post( $post, false, false );
139142

140-
// Assign custom fields.
141-
foreach ( $this->_args['meta'] as $key => $value ) {
143+
}
142144

143-
$meta_value = rgar( $entry, $value );
145+
/**
146+
* @param $meta
147+
* @param $post_id
148+
* @param $entry
149+
* @param $form
150+
* @param $meta_input
151+
* @param $group string|null Used to handle populating ACF fields within a group.
152+
*
153+
* @return array|mixed
154+
*/
155+
public function prepare_meta_input( $meta, $post_id, $entry, $form, $meta_input = array(), $group = null ) {
144156

145-
$field = GFAPI::get_field( $form, $value );
157+
foreach ( $meta as $key => $value ) {
146158

147-
// Support mapping all checkboxes of a Checkbox field to a custom field.
148-
if ( $field->get_input_type() === 'checkbox' ) {
149-
$meta_value = $field->get_value_export( $entry );
150-
if ( is_callable( 'acf_get_field' ) ) {
151-
$acf_field = acf_get_field( $key );
152-
if ( $acf_field ) {
153-
$meta_value = array_map( 'trim', explode( ',', $meta_value ) );
154-
}
155-
}
156-
}
159+
if ( is_array( $value ) ) {
160+
$meta_input = $this->prepare_meta_input( $value, $post_id, $entry, $form, $meta_input, $key );
161+
continue;
162+
}
157163

158-
// Check for ACF image-like custom fields. Integration powered by GP Media Library.
159-
$acf_field = is_callable( 'gp_media_library' ) && is_callable( 'acf_get_field' ) ? acf_get_field( $key ) : false;
160-
if ( $acf_field && in_array( $acf_field['type'], array( 'image', 'file', 'gallery' ), true ) ) {
161-
gp_media_library()->acf_update_field( $post->ID, $key, GFAPI::get_field( $form, $value ), $entry );
162-
} else {
163-
// Map all other custom fields generically.
164-
if ( ! rgblank( $meta_value ) ) {
165-
$meta_input[ $key ] = $meta_value;
166-
} elseif ( $this->_args['delete_if_empty'] ) {
167-
delete_post_meta( $post->ID, $key );
164+
$meta_value = rgar( $entry, $value );
165+
166+
$field = GFAPI::get_field( $form, $value );
167+
168+
// Support mapping all checkboxes of a Checkbox field to a custom field.
169+
if ( $field->get_input_type() === 'checkbox' ) {
170+
$meta_value = $field->get_value_export( $entry );
171+
if ( is_callable( 'acf_get_field' ) ) {
172+
$acf_field = acf_get_field( $key );
173+
if ( $acf_field ) {
174+
$meta_value = array_map( 'trim', explode( ',', $meta_value ) );
168175
}
169176
}
170177
}
171178

172-
$post->meta_input = $meta_input;
179+
// Check for ACF image-like custom fields. Integration powered by GP Media Library. We use `acf_maybe_get_field()`
180+
// here which supports fetching fields within a group by combined key (e.g. "group_name_field_name" );
181+
$acf_field = is_callable( 'gp_media_library' ) ? $this->acf_get_field_object_by_name( $key, $group ) : false;
182+
if ( $acf_field && in_array( $acf_field['type'], array( 'image', 'file', 'gallery' ), true ) ) {
183+
$meta_value = gp_media_library()->acf_get_field_value( 'id', $entry, GFAPI::get_field( $form, $value ) );
184+
}
185+
186+
if ( $group ) {
187+
$key = sprintf( '%s_%s', $group, $key );
188+
}
173189

190+
if ( ! rgblank( $meta_value ) ) {
191+
$meta_input[ $key ] = $meta_value;
192+
} elseif ( $this->_args['delete_if_empty'] ) {
193+
delete_post_meta( $post_id, $key );
194+
}
174195
}
175196

176-
// ensure the fires after hooks is set to false, so that doesn't override some of the normal rendering - GF confirmation for instance.
177-
wp_update_post( $post, false, false );
197+
return $meta_input;
198+
}
199+
200+
function acf_get_field_object_by_name( $field_name, $group_name = false ) {
201+
202+
if ( ! is_callable( 'acf_get_field' ) ) {
203+
return null;
204+
}
205+
206+
if ( ! $group_name ) {
207+
return acf_get_field( $field_name );
208+
}
209+
210+
$group = acf_get_field( $group_name );
178211

212+
return acf_get_field( $field_name, $group['ID'] );
179213
}
180214

181215
/**

0 commit comments

Comments
 (0)