Skip to content

Commit 9208e14

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 9208e14

File tree

1 file changed

+95
-6
lines changed

1 file changed

+95
-6
lines changed

gravity-forms/gw-update-posts.php

Lines changed: 95 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,27 +140,105 @@ 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 ) && rgar( $term_field, 'gppa-choices-enabled' ) ) {
152+
$taxonomy = '';
153+
// Get the taxonomy from the field settings or the first choice object based on the field type.
154+
if ( $term_field instanceof GF_Field_Text ) {
155+
$taxonomy = str_replace( 'taxonomy_', '', rgars( $term_field, 'gppa-values-templates/value', '' ) );
156+
} elseif ( is_object( $term_field ) && ! empty( $term_field['choices'][0]['object']->taxonomy ) ) {
157+
$taxonomy = $term_field['choices'][0]['object']->taxonomy;
158+
} elseif ( is_object( $term_field ) && rgars( $term_field, 'gppa-choices-filter-groups/0/0/property' ) === 'taxonomy' ) {
159+
// When GravityView is activated, need to get the taxonomy from the filter groups.
160+
$taxonomy = $term_field['gppa-choices-filter-groups'][0][0]['value'];
161+
}
162+
163+
// Taxonomy not found, skip.
164+
if ( empty( $taxonomy ) ) {
165+
continue;
166+
}
167+
168+
foreach ( $terms as $key => $term ) {
169+
// Check if `$term` is a term name or id. If term name, get the term id.
170+
if ( ! is_numeric( $term ) ) {
171+
$term = term_exists( $term, $taxonomy );
172+
173+
// If the term doesn't exist, remove it from the array.
174+
if ( ! $term ) {
175+
unset( $terms[ $key ] );
176+
continue;
177+
}
178+
179+
$terms[ $key ] = $term['term_id'];
180+
}
181+
}
182+
183+
// If the taxonomy is not hierarchical, we need to get the term names from the term ids.
184+
if( ! $this->is_taxonomy_hierarchical( $taxonomy ) ) {
185+
$terms = $this->get_term_names_by_ids( $terms, $taxonomy );
186+
}
187+
188+
wp_set_post_terms( $post->ID, $terms, $taxonomy );
189+
}
152190
}
153191
}
154192

155193
if ( $this->_args['meta'] ) {
156194
$post->meta_input = $this->prepare_meta_input( $this->_args['meta'], $post->ID, $entry, $form );
157195
}
158196

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

162200
}
163201

202+
/**
203+
* Check if a taxonomy is hierarchical.
204+
*
205+
* @param $taxonomy
206+
*
207+
* @return bool
208+
*/
209+
function is_taxonomy_hierarchical( $taxonomy ) {
210+
$taxonomy_object = get_taxonomy( $taxonomy );
211+
212+
if ( ! $taxonomy_object ) {
213+
return false;
214+
}
215+
216+
return $taxonomy_object->hierarchical;
217+
}
218+
219+
/**
220+
* Get term names by term IDs.
221+
*
222+
* @param $tag_ids
223+
* @param string $taxonomy
224+
*
225+
* @return array
226+
*/
227+
function get_term_names_by_ids( $tag_ids, $taxonomy = 'post_tag' ) {
228+
$tag_ids = is_array( $tag_ids ) ? $tag_ids : array( $tag_ids );
229+
230+
$tag_names = [];
231+
foreach ( $tag_ids as $tag_id ) {
232+
$tag = get_term( $tag_id, $taxonomy );
233+
if ( ! is_wp_error( $tag ) && $tag ) {
234+
$tag_names[] = $tag->name;
235+
}
236+
}
237+
238+
return $tag_names;
239+
}
240+
241+
164242
/**
165243
* @param $meta
166244
* @param $post_id
@@ -279,6 +357,16 @@ function acf_get_field_object_by_name( $field_name, $group_name = false ) {
279357
* @return mixed
280358
*/
281359
public function return_ids_instead_of_names( $value, $field, $template_name, $populate, $object, $object_type, $objects, $template ) {
360+
// Check if this is for the specific form we want.
361+
if ( $field->formId != $this->_args['form_id'] ) {
362+
return $value;
363+
}
364+
365+
// Don't want to return IDs for post objects used in the populates field dynamically using GPPA.
366+
if ( rgar( $field, 'gppa-values-enabled' ) === true && rgar( $field, 'gppa-values-object-type' ) === 'post' ) {
367+
return $value;
368+
}
369+
282370
if ( strpos( $template, 'taxonomy_' ) === 0 ) {
283371
$taxonomy = preg_replace( '/^taxonomy_/', '', $template );
284372
$terms = wp_get_post_terms( $object->ID, $taxonomy, array( 'fields' => 'ids' ) );
@@ -330,5 +418,6 @@ public function get_post_date( $entry, $form ) {
330418

331419
return gmdate( 'Y-m-d H:i:s', strtotime( sprintf( '%s %s:%s:00', $date, $hour, $min ) ) );
332420
}
333-
334421
}
422+
423+

0 commit comments

Comments
 (0)