Skip to content

Commit 3f30162

Browse files
authored
gpaa-autocomplete-by-city.php: Created a new snippet for autocompleting by city instead of an address.
1 parent 0ad4e4d commit 3f30162

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
/**
3+
* Gravity Perks // Address Autocomplete // Autocomplete By City
4+
* https://gravitywiz.com/documentation/gravity-forms-address-autocomplete/
5+
*
6+
* Autocomplete cities (including state and country) instead of full addresses.
7+
*/
8+
class GPAA_Autocomplete_By_City {
9+
10+
public function __construct( $args = array() ) {
11+
12+
// set our default arguments, parse against the provided arguments, and store for use throughout the class
13+
$this->_args = wp_parse_args( $args, array(
14+
'form_id' => false,
15+
'field_id' => false,
16+
) );
17+
18+
// do version check in the init to make sure if GF is going to be loaded, it is already loaded
19+
add_action( 'init', array( $this, 'init' ) );
20+
21+
}
22+
23+
public function init() {
24+
25+
add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 );
26+
add_filter( 'gform_register_init_scripts', array( $this, 'add_init_script' ), 10, 2 );
27+
28+
add_filter( 'gpaa_init_args', array( $this, 'set_city_input_as_autocomplete_input' ) );
29+
add_filter( 'gform_form_post_get_meta', array( $this, 'disable_unused_inputs' ) );
30+
31+
}
32+
33+
public function load_form_script( $form, $is_ajax_enabled ) {
34+
35+
if ( $this->is_applicable_form( $form ) && ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) {
36+
add_action( 'wp_footer', array( $this, 'output_script' ) );
37+
add_action( 'gform_preview_footer', array( $this, 'output_script' ) );
38+
}
39+
40+
return $form;
41+
}
42+
43+
public function output_script() {
44+
?>
45+
46+
<script type="text/javascript">
47+
48+
( function( $ ) {
49+
50+
window.GPPAAutocompleteByCity = function( args ) {
51+
52+
var self = this;
53+
54+
self.init = function() {
55+
56+
window.gform.addFilter( 'gpaa_autocomplete_options', function( options, gpaa, formId, fieldId ) {
57+
if ( formId == args.formId && fieldId == args.fieldId ) {
58+
options.types = [ '(cities)' ];
59+
}
60+
return options;
61+
} );
62+
63+
};
64+
65+
self.init();
66+
67+
}
68+
69+
} )( jQuery );
70+
71+
</script>
72+
73+
<?php
74+
}
75+
76+
public function add_init_script( $form ) {
77+
78+
if ( ! $this->is_applicable_form( $form ) ) {
79+
return;
80+
}
81+
82+
$args = array(
83+
'formId' => $this->_args['form_id'],
84+
'fieldId' => $this->_args['field_id'],
85+
);
86+
87+
$script = 'new GPPAAutocompleteByCity( ' . json_encode( $args ) . ' );';
88+
$slug = implode( '_', array( 'gw_js_snippet_template', $this->_args['form_id'], $this->_args['field_id'] ) );
89+
90+
GFFormDisplay::add_init_script( $this->_args['form_id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );
91+
92+
}
93+
94+
public function set_city_input_as_autocomplete_input( $args ) {
95+
if ( ! $this->is_applicable_form( $args['formId'] ) || ! $this->is_applicable_field( $args['fieldId'] ) ) {
96+
return $args;
97+
}
98+
$args['inputSelectors']['autocomplete'] = "#input_{$args['formId']}_{$args['fieldId']}_3";
99+
return $args;
100+
}
101+
102+
public function disable_unused_inputs( $form ) {
103+
if ( ! $this->is_applicable_form( $form ) ) {
104+
return $form;
105+
}
106+
foreach( $form['fields'] as &$field ) {
107+
if ( ! $this->is_applicable_field( $field->id ) ) {
108+
continue;
109+
}
110+
$field->inputs[0]['isHidden'] = true;
111+
$field->inputs[1]['isHidden'] = true;
112+
$field->inputs[4]['isHidden'] = true;
113+
}
114+
return $form;
115+
}
116+
117+
public function is_applicable_form( $form ) {
118+
119+
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
120+
121+
return empty( $this->_args['form_id'] ) || (int) $form_id == (int) $this->_args['form_id'];
122+
}
123+
124+
public function is_applicable_field( $field_id ) {
125+
return $this->_args['field_id'] == $field_id;
126+
}
127+
128+
}
129+
130+
# Configuration
131+
132+
new GPAA_Autocomplete_By_City( array(
133+
'form_id' => 123, // Update "123" to your form ID.
134+
'field_id' => 4, // Update "4" to your Address field ID.
135+
) );

0 commit comments

Comments
 (0)