Completed
Push — develop ( 7592b8...3eb7c5 )
by Zack
14:44
created

fields/class-gravityview-field-address.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * @file class-gravityview-field-address.php
4
 * @package GravityView
5
 * @subpackage includes\fields
6
 */
7
8
/**
9
 * Add custom options for address fields
10
 */
11
class GravityView_Field_Address extends GravityView_Field {
12
13
	var $name = 'address';
14
15
	var $group = 'advanced';
0 ignored issues
show
The visibility should be declared for property $group.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
16
17
	var $is_numeric = false;
18
19
	var $is_searchable = true;
0 ignored issues
show
The visibility should be declared for property $is_searchable.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
20
21
	var $search_operators = array( 'is', 'isnot', 'contains' );
22
23
	var $_gf_field_class_name = 'GF_Field_Address';
24
25
	public function __construct() {
26
		$this->label = esc_html__( 'Address', 'gravityview' );
27
28
		$this->add_hooks();
29
30
		parent::__construct();
31
	}
32
33
	/**
34
	 * Add filters for this field type
35
	 *
36
	 * @since 1.19.2
37
	 *
38
	 * @return void
39
	 */
40
	private function add_hooks() {
41
		add_filter( 'gravityview/extension/search/input_type', array( $this, 'search_bar_input_type' ), 10, 3 );
42
		add_filter( 'gravityview/search/input_types', array( $this, 'input_types' ) );
43
		add_filter( 'gravityview_widget_search_filters', array( $this, 'search_field_filter' ), 10, 3 );
44
	}
45
46
	/**
47
	 * Dynamically add choices to the address field dropdowns, if any
48
	 *
49
	 * @since 1.19.2
50
	 *
51
	 * @param array $search_fields Array of search filters with `key`, `label`, `value`, `type` keys
52
	 * @param GravityView_Widget_Search $widget Current widget object
53
	 * @param array $widget_args Args passed to this method. {@since 1.8}
54
	 *
55
	 * @return array If the search field GF Field type is `address`, and there are choices to add, adds them and changes the input type. Otherwise, sets the input to text.
56
	 */
57
	public function search_field_filter( $search_fields, $widget, $widget_args ) {
58
59
		foreach ( $search_fields as & $search_field ) {
60
61
			if ( 'address' === rgar( $search_field, 'type' ) ) {
62
63
				$field_id = intval( floor( $search_field['key'] ) );
64
				$input_id = gravityview_get_input_id_from_id( $search_field['key'] );
65
				$form = GravityView_View::getInstance()->getForm();
66
67
				/** @var GF_Field_Address $address_field */
68
				$address_field = GFFormsModel::get_field( $form, $field_id );
69
70
				$choices = array();
71
72
				$method_name = 'get_choices_' . self::get_input_type_from_input_id( $input_id );
73
				if( method_exists( $this, $method_name ) ) {
74
					/**
75
					 * @uses GravityView_Field_Address::get_choices_country()
76
					 * @uses GravityView_Field_Address::get_choices_state()
77
					 */
78
					$choices = $this->{$method_name}( $address_field );
79
				}
80
81
				if( ! empty( $choices ) ) {
82
					$search_field['choices'] = $choices;
83
					$search_field['type'] = rgar( $search_field, 'input');
84
				} else {
85
					$search_field['type'] = 'text';
86
					$search_field['input'] = 'input_text';
87
				}
88
			}
89
		}
90
91
		return $search_fields;
92
	}
93
94
	/**
95
	 * Get array of countries to use for the search choices
96
	 *
97
	 * @since 1.19.2
98
	 *
99
	 * @see GF_Field_Address::get_countries()
100
	 *
101
	 * @param GF_Field_Address $address_field
102
	 *
103
	 * @return array Array of countries with `value` and `text` keys as the name of the country
104
	 */
105
	private function get_choices_country( $address_field ) {
106
107
		$countries = $address_field->get_countries();
108
109
		$country_choices = array();
110
111
		foreach ( $countries as $key => $country ) {
112
			$country_choices[] = array(
113
				'value' => $country,
114
				'text' => $country,
115
			);
116
		}
117
118
		return $country_choices;
119
	}
120
121
	/**
122
	 * Get array of states to use for the search choices
123
	 *
124
	 * @since 1.19.2
125
	 *
126
	 * @uses GF_Field_Address::get_us_states()
127
	 * @uses GF_Field_Address::get_us_state_code()
128
	 * @uses GF_Field_Address::get_canadian_provinces()
129
	 *
130
	 * @param GF_Field_Address $address_field
131
	 *
132
	 * @return array Array of countries with `value` and `text` keys as the name of the country
133
	 */
134
	private function get_choices_state( $address_field ) {
135
136
		$address_type = empty( $address_field->addressType ) ? $address_field->get_default_address_type( $form['id'] ) : $address_field->addressType;
137
138
		$state_choices = array();
139
140
		switch ( $address_type ) {
141
			case 'us':
142
				$states = GFCommon::get_us_states();
143
				break;
144
			case 'canadian':
145
				$states = GFCommon::get_canadian_provinces();
146
				break;
147
			default:
148
				$states = empty( $address_types[ $address_type ]['states'] ) ? array() : $address_types[ $address_type ]['states'];
149
				break;
150
		}
151
152
		foreach ( $states as $key => $state ) {
153
			$state_choices[] = array(
154
				'value' => $state,
155
				'text' => $state,
156
			);
157
		}
158
159
		return $state_choices;
160
	}
161
162
	/**
163
	 * Add the input types available for each custom search field type
164
	 *
165
	 * @since 1.19.2
166
	 *
167
	 * @param array $input_types Array of input types as the keys (`select`, `radio`, `multiselect`, `input_text`) with a string or array of supported inputs as values
168
	 *
169
	 * @return array $input_types array, but
170
	 */
171
	public function input_types( $input_types ) {
172
173
		// Use the same inputs as the "text" input type allows
174
		$text_inputs = rgar( $input_types, 'text' );
175
176
		$input_types['street'] = $text_inputs;
177
		$input_types['street2'] = $text_inputs;
178
		$input_types['city'] = $text_inputs;
179
180
		$input_types['state'] = array( 'select', 'radio', 'link' ) + $text_inputs;
181
		$input_types['zip'] = array( 'input_text' );
182
		$input_types['country'] = array( 'select', 'radio', 'link' ) + $text_inputs;
183
184
		return $input_types;
185
	}
186
187
	/**
188
	 * Converts the custom input type (address) into the selected type
189
	 *
190
	 * @since 1.19.2
191
	 *
192
	 * @param string $input_type Assign an input type according to the form field type. Defaults: `boolean`, `multi`, `select`, `date`, `text`
193
	 * @param string $field_type Gravity Forms field type (also the `name` parameter of GravityView_Field classes)
194
	 * @param string|int|float $field_id ID of the field being processed
195
	 *
196
	 * @return string If the field ID matches an address field input, return those options {@see GravityView_Field_Address::input_types() }. Otherwise, original value is used.
197
	 */
198
	public function search_bar_input_type( $input_type, $field_type, $field_id ) {
199
200
		// Is this search field for an input (eg: 4.2) or the whole address field (eg: 4)?
201
		$input_id = gravityview_get_input_id_from_id( $field_id );
202
203
		if( 'address' === $field_type && $input_id ) {
204
205
			// If the input ID matches an expected address input, set to that. Otherwise, keep existing input type.
206
			if( $address_field_name = self::get_input_type_from_input_id( $input_id ) ) {
207
				$input_type = $address_field_name;
208
			}
209
		}
210
211
		return $input_type;
212
	}
213
214
	/**
215
	 * Get a name for the input based on the input ID
216
	 *
217
	 * @since 1.19.2
218
	 *
219
	 * @param int $input_id ID of the specific input for the address field
220
	 *
221
	 * @return false|string If the input ID matches a known address field input, returns a name for that input ("city", or "country"). Otherwise, returns false.
222
	 */
223
	private static function get_input_type_from_input_id( $input_id ) {
224
225
		$input_type = false;
226
227
		switch ( $input_id ) {
228
			case 1:
229
				$input_type = 'street';
230
				break;
231
			case 2:
232
				$input_type = 'street2';
233
				break;
234
			case 3:
235
				$input_type = 'city';
236
				break;
237
				break;
238
			case 4:
239
				$input_type = 'state';
240
				break;
241
			case 5:
242
				$input_type = 'zip';
243
				break;
244
			case 6:
245
				$input_type = 'country';
246
				break;
247
		}
248
249
		return $input_type;
250
	}
251
252
	function field_options( $field_options, $template_id = '', $field_id = '', $context = '', $input_type = '' ) {
253
254
		// If this is NOT the full address field, return default options.
255
		if( floor( $field_id ) !== floatval( $field_id ) ) {
256
			return $field_options;
257
		}
258
259
		if( 'edit' === $context ) {
260
			return $field_options;
261
		}
262
263
		$add_options = array();
264
265
		$add_options['show_map_link'] = array(
266
			'type' => 'checkbox',
267
			'label' => __( 'Show Map Link:', 'gravityview' ),
268
			'desc' => __('Display a "Map It" link below the address', 'gravityview'),
269
			'value' => true,
270
			'merge_tags' => false,
271
		);
272
273
		return $add_options + $field_options;
274
	}
275
276
}
277
278
new GravityView_Field_Address;
279