Skip to content

Commit 1060730

Browse files
committed
gw-update-posts.php: Fixed issue where snippet returned ID instead of value when GPPA dynamically populates post field.
1 parent 00a1386 commit 1060730

File tree

1 file changed

+80
-6
lines changed

1 file changed

+80
-6
lines changed

gravity-forms/gw-update-posts.php

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,27 +140,91 @@ public function update_post_by_entry( $entry, $form ) {
140140
}
141141

142142
if ( $this->_args['terms'] ) {
143-
144143
// Assign custom taxonomies.
145144
$term_fields = is_array( $this->_args['terms'] ) ? $this->_args['terms'] : array( $this->_args['terms'] );
145+
146146
foreach ( $term_fields as $field ) {
147147
$term_field = GFAPI::get_field( $form, $field );
148-
$terms = array_map( 'intval', explode( ',', is_object( $term_field ) ? $term_field->get_value_export( $entry ) : '' ) );
149-
$taxonomy = is_object( $term_field ) ? $term_field['choices'][0]['object']->taxonomy : '';
150148

151-
wp_set_post_terms( $post->ID, $terms, $taxonomy );
149+
$terms = explode( ',', is_object( $term_field ) ? trim( $term_field->get_value_export( $entry ) ) : '' );
150+
151+
if ( ! empty( $terms ) ) {
152+
// Get the taxonomy from the field settings or the first choice object based on the field type.
153+
if ( $term_field instanceof GF_Field_Text ) {
154+
$taxonomy = str_replace( 'taxonomy_', '', rgars( $term_field, 'gppa-values-templates/value', '' ) );
155+
} elseif ( is_object( $term_field ) && ! empty( $term_field['choices'][0]['object']->taxonomy ) ) {
156+
$taxonomy = $term_field['choices'][0]['object']->taxonomy;
157+
} else {
158+
$taxonomy = '';
159+
}
160+
161+
foreach ( $terms as $key => $term ) {
162+
// Check if `$term` is a term name or id. If term name, get the term id.
163+
if ( ! is_numeric( $term ) ) {
164+
$term = term_exists( $term, $taxonomy );
165+
$terms[ $key ] = $term ? $term['term_id'] : 0;
166+
}
167+
}
168+
169+
// If the taxonomy is not hierarchical, we need to get the term names from the term ids.
170+
if( ! $this->is_taxonomy_hierarchical( $taxonomy ) ) {
171+
$terms = $this->get_term_names_by_ids( $terms, $taxonomy );
172+
}
173+
174+
wp_set_post_terms( $post->ID, $terms, $taxonomy );
175+
}
152176
}
153177
}
154178

155179
if ( $this->_args['meta'] ) {
156180
$post->meta_input = $this->prepare_meta_input( $this->_args['meta'], $post->ID, $entry, $form );
157181
}
158182

159-
// ensure the fires after hooks is set to false, so that doesn't override some of the normal rendering - GF confirmation for instance.
183+
// Ensure the fires after hooks is set to false, so that doesn't override some of the normal rendering - GF confirmation for instance.
160184
wp_update_post( $post, false, false );
161185

162186
}
163187

188+
/**
189+
* Check if a taxonomy is hierarchical.
190+
*
191+
* @param $taxonomy
192+
*
193+
* @return bool
194+
*/
195+
function is_taxonomy_hierarchical( $taxonomy ) {
196+
$taxonomy_object = get_taxonomy( $taxonomy );
197+
198+
if ( ! $taxonomy_object ) {
199+
return false;
200+
}
201+
202+
return $taxonomy_object->hierarchical;
203+
}
204+
205+
/**
206+
* Get term names by term IDs.
207+
*
208+
* @param $tag_ids
209+
* @param string $taxonomy
210+
*
211+
* @return array
212+
*/
213+
function get_term_names_by_ids( $tag_ids, $taxonomy = 'post_tag' ) {
214+
$tag_ids = is_array( $tag_ids ) ? $tag_ids : array( $tag_ids );
215+
216+
$tag_names = [];
217+
foreach ( $tag_ids as $tag_id ) {
218+
$tag = get_term( $tag_id, $taxonomy );
219+
if ( ! is_wp_error( $tag ) && $tag ) {
220+
$tag_names[] = $tag->name;
221+
}
222+
}
223+
224+
return $tag_names;
225+
}
226+
227+
164228
/**
165229
* @param $meta
166230
* @param $post_id
@@ -279,6 +343,16 @@ function acf_get_field_object_by_name( $field_name, $group_name = false ) {
279343
* @return mixed
280344
*/
281345
public function return_ids_instead_of_names( $value, $field, $template_name, $populate, $object, $object_type, $objects, $template ) {
346+
// Check if this is for the specific form we want.
347+
if ( $field->formId != $this->_args['form_id'] ) {
348+
return $value;
349+
}
350+
351+
// Don't want to return IDs for post objects used in the populates field dynamically using GPPA.
352+
if ( rgar( $field, 'gppa-values-enabled' ) === true && rgar( $field, 'gppa-values-object-type' ) === 'post' ) {
353+
return $value;
354+
}
355+
282356
if ( strpos( $template, 'taxonomy_' ) === 0 ) {
283357
$taxonomy = preg_replace( '/^taxonomy_/', '', $template );
284358
$terms = wp_get_post_terms( $object->ID, $taxonomy, array( 'fields' => 'ids' ) );
@@ -331,4 +405,4 @@ public function get_post_date( $entry, $form ) {
331405
return gmdate( 'Y-m-d H:i:s', strtotime( sprintf( '%s %s:%s:00', $date, $hour, $min ) ) );
332406
}
333407

334-
}
408+
}

0 commit comments

Comments
 (0)