Skip to content

Commit 4874454

Browse files
authored
gpadvs-enable-add-new-option.php: Added functionality to create new terms on form submission.
1 parent 4bfeb33 commit 4874454

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed

gp-advanced-select/gpadvs-enable-add-new-option.php

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* https://gravitywiz.com/documentation/gravity-forms-advanced-select/
55
*
66
* Enable Advanced Select's "Add New" option that allows users to create new items that aren't in the initial list of options.
7+
* If the options are been populating from a taxonomy term using GPPA, enabling the `insert_new_option` setting will create
8+
* a new taxonomy term.
79
*
810
* Instructions:
911
*
@@ -18,8 +20,9 @@ public function __construct( $args = array() ) {
1820

1921
// set our default arguments, parse against the provided arguments, and store for use throughout the class
2022
$this->_args = wp_parse_args( $args, array(
21-
'form_id' => false,
22-
'field_id' => false,
23+
'form_id' => false,
24+
'field_id' => false,
25+
'insert_new_option' => false,
2326
) );
2427

2528
// do version check in the init to make sure if GF is going to be loaded, it is already loaded
@@ -44,6 +47,8 @@ public function init() {
4447
add_filter( 'gform_pre_render', array( $this, 'disable_state_validation_for_advanced_select_field' ), 10, 1 );
4548

4649
add_filter( 'gform_pre_render', array( $this, 'add_new_option_to_choices' ), 10, 1 );
50+
51+
add_action( 'gform_after_submission', array( $this, 'create_new_option' ), 10, 2 );
4752
}
4853

4954
public function load_form_script( $form, $is_ajax_enabled ) {
@@ -235,6 +240,57 @@ public function disable_state_validation_for_advanced_select_field( $form ) {
235240
return $form;
236241
}
237242

243+
public function create_new_option( $entry, $form ) {
244+
if (
245+
! $this->is_applicable_form( $form ) ||
246+
! $this->_args['insert_new_option'] ||
247+
! class_exists( '\GP_Populate_Anything' )
248+
) {
249+
return;
250+
}
251+
252+
foreach ( $form['fields'] as &$field ) {
253+
if ( ! $this->is_applicable_field( $field ) || $field['gppa-choices-object-type'] != 'term' ) {
254+
continue;
255+
}
256+
257+
$tax_filter = false;
258+
$choice_template_value = $field['gppa-choices-templates']['value'];
259+
$filter_groups = $field['gppa-choices-filter-groups'];
260+
if ( ! empty( $filter_groups ) ) {
261+
foreach ( $filter_groups as $group_filter ) {
262+
$tax_filter = current(
263+
array_filter( $group_filter, function( $item ) {
264+
return $item['property'] == 'taxonomy' && $item['operator'] == 'is';
265+
} )
266+
);
267+
}
268+
}
269+
270+
if ( ! $tax_filter || ! in_array( $choice_template_value, array( 'name', 'term_id', 'slug' ) ) ) {
271+
return;
272+
}
273+
274+
$field_value = rgar( $entry, $field->id );
275+
$taxonomy = $tax_filter['value'];
276+
// check if new option
277+
$term = get_term_by( $choice_template_value, $field_value, $taxonomy );
278+
if ( $term ) {
279+
return;
280+
}
281+
282+
$inserted_term = wp_insert_term( $field_value, $taxonomy );
283+
if ( is_wp_error( $inserted_term ) ) {
284+
return;
285+
}
286+
287+
$term = get_term( $inserted_term['term_id'], $taxonomy );
288+
// Update entry
289+
gform_update_meta( $entry['id'], $field->id, $term->$choice_template_value, $form['id'] );
290+
$gppa_choice_labels = array( $field->id => array( $term->$choice_template_value => $field_value ) );
291+
gform_update_meta( $entry['id'], 'gppa_choices', $gppa_choice_labels, $form['id'] );
292+
}
293+
}
238294
}
239295

240296
# Configuration

0 commit comments

Comments
 (0)