Skip to content

Commit b57f52d

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 b57f52d

File tree

1 file changed

+87
-6
lines changed

1 file changed

+87
-6
lines changed

gravity-forms/gw-update-posts.php

Lines changed: 87 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,27 +140,98 @@ 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 ) && ! empty( $term_field['gppa-choices-filter-groups'][0][0]['property'] ) && $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+
$terms[ $key ] = $term ? $term['term_id'] : 0;
173+
}
174+
}
175+
176+
// If the taxonomy is not hierarchical, we need to get the term names from the term ids.
177+
if( ! $this->is_taxonomy_hierarchical( $taxonomy ) ) {
178+
$terms = $this->get_term_names_by_ids( $terms, $taxonomy );
179+
}
180+
181+
wp_set_post_terms( $post->ID, $terms, $taxonomy );
182+
}
152183
}
153184
}
154185

155186
if ( $this->_args['meta'] ) {
156187
$post->meta_input = $this->prepare_meta_input( $this->_args['meta'], $post->ID, $entry, $form );
157188
}
158189

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

162193
}
163194

195+
/**
196+
* Check if a taxonomy is hierarchical.
197+
*
198+
* @param $taxonomy
199+
*
200+
* @return bool
201+
*/
202+
function is_taxonomy_hierarchical( $taxonomy ) {
203+
$taxonomy_object = get_taxonomy( $taxonomy );
204+
205+
if ( ! $taxonomy_object ) {
206+
return false;
207+
}
208+
209+
return $taxonomy_object->hierarchical;
210+
}
211+
212+
/**
213+
* Get term names by term IDs.
214+
*
215+
* @param $tag_ids
216+
* @param string $taxonomy
217+
*
218+
* @return array
219+
*/
220+
function get_term_names_by_ids( $tag_ids, $taxonomy = 'post_tag' ) {
221+
$tag_ids = is_array( $tag_ids ) ? $tag_ids : array( $tag_ids );
222+
223+
$tag_names = [];
224+
foreach ( $tag_ids as $tag_id ) {
225+
$tag = get_term( $tag_id, $taxonomy );
226+
if ( ! is_wp_error( $tag ) && $tag ) {
227+
$tag_names[] = $tag->name;
228+
}
229+
}
230+
231+
return $tag_names;
232+
}
233+
234+
164235
/**
165236
* @param $meta
166237
* @param $post_id
@@ -279,6 +350,16 @@ function acf_get_field_object_by_name( $field_name, $group_name = false ) {
279350
* @return mixed
280351
*/
281352
public function return_ids_instead_of_names( $value, $field, $template_name, $populate, $object, $object_type, $objects, $template ) {
353+
// Check if this is for the specific form we want.
354+
if ( $field->formId != $this->_args['form_id'] ) {
355+
return $value;
356+
}
357+
358+
// Don't want to return IDs for post objects used in the populates field dynamically using GPPA.
359+
if ( rgar( $field, 'gppa-values-enabled' ) === true && rgar( $field, 'gppa-values-object-type' ) === 'post' ) {
360+
return $value;
361+
}
362+
282363
if ( strpos( $template, 'taxonomy_' ) === 0 ) {
283364
$taxonomy = preg_replace( '/^taxonomy_/', '', $template );
284365
$terms = wp_get_post_terms( $object->ID, $taxonomy, array( 'fields' => 'ids' ) );
@@ -331,4 +412,4 @@ public function get_post_date( $entry, $form ) {
331412
return gmdate( 'Y-m-d H:i:s', strtotime( sprintf( '%s %s:%s:00', $date, $hour, $min ) ) );
332413
}
333414

334-
}
415+
}

0 commit comments

Comments
 (0)