Restrict States in Address Fields

Restrict the states that can be selected for Address fields. Either restrict specific fields or restrict all Address fields on the site.

See https://gravitywiz.com/documentation/how-do-i-install-a-snippet/ for details on how to install this snippet.

Instructions

See “Where do I put snippets?” in our documentation for installation instructions.

Code

Filename: gw-restrict-states-in-address-fields.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
 * Gravity Wiz // Gravity Forms // Restrict States in Address Fields
 * https://gravitywiz.com/
 *
 * Restrict the states that can be selected for Address fields. Either restrict specific fields or restrict all Address
 * fields on the site.
 *
 * See https://gravitywiz.com/documentation/how-do-i-install-a-snippet/ for details on how to install this snippet.
 *
 * @version 1.1
 * @license GPL-2.0+
 * @link    https://gravitywiz.com
 */
class GW_Restrict_States_In_Address_Field {

	private $_args = array();

	public function __construct( $args = array() ) {
		// set our default arguments, parse against the provided arguments, and store for use throughout the class
		$this->_args = wp_parse_args(
			$args,
			array(
				'form_id'            => false,
				'field_id'           => false,
				'allowed_states'     => null,
				'validation_message' => 'We\'re sorry, we only offer our services in the following states: %s',
			)
		);

		if ( ! $this->_args['allowed_states'] ) {
			return;
		}

		add_filter( 'gform_validation', array( $this, 'validate' ) );
		add_filter( 'gform_field_input', array( $this, 'register_us_states_filter' ), 10, 2 );
		add_filter( 'gform_field_content', array( $this, 'unregister_us_states_filter' ) );

	}


	/**
	 * @param array $result Gravity Forms validation result.
	 */
	public function validate( $result ) {
		$form = $result['form'];

		// Do not validate the states unless the form ID matches or if no form ID was supplied which means we validate
		// for all forms.
		if ( ! $this->is_applicable_form( $form ) ) {
			return $result;
		}

		foreach ( $form['fields'] as &$field ) {

			if ( ! $this->is_applicable_field( $field ) ) {
				continue;
			}

			$field_value    = GFFormsModel::get_field_value( $field );
			$selected_state = rgar( $field_value, "{$field->id}.4" );

			if ( ! $selected_state ) {
				continue;
			}

			if (
				in_array( $selected_state, $this->_args['allowed_states'], true )
				|| array_key_exists( $selected_state, $this->_args['allowed_states'] )
			) {
				continue;
			}

			$allowed_states_list = join( ', ', $this->_args['allowed_states'] );

			$field['failed_validation']  = true;
			$field['validation_message'] = sprintf( $this->_args['validation_message'], $allowed_states_list );
			$result['is_valid']          = false;
		}

		$result['form'] = $form;

		return $result;
	}

	/**
	 * Register our states filter immediately before the field's input markup is generated.
	 *
	 * @param $return
	 * @param $field
	 *
	 * @return mixed
	 */
	public function register_us_states_filter( $return, $field ) {
		if ( $this->is_applicable_field( $field ) ) {
			add_filter( 'gform_us_states', array( $this, 'filter_states' ) );
		}
		return $return;
	}

	/**
	 * Unregister our states filter after the field's input markup has been generated.
	 *
	 * @param $return
	 *
	 * @return mixed
	 */
	public function unregister_us_states_filter( $return ) {
		remove_filter( 'gform_us_states', array( $this, 'filter_states' ) );
		return $return;
	}

	public function filter_states( $states ) {
		return array_intersect( $this->_args['allowed_states'], $states );
	}

	public function is_applicable_form( $form ) {

		$form_id = isset( $form['id'] ) ? $form['id'] : $form;

		return empty( $this->_args['form_id'] ) || (int) $form_id === (int) $this->_args['form_id'];
	}

	public function is_applicable_field( $field ) {

		if ( ! $this->is_applicable_form( $field->formId ) ) {
			return false;
		}

		// Check if this is our specified field if set.
		if ( isset( $this->_args['field_id'] ) && ! empty( $this->_args['field_id'] ) ) {
			return (int) $field->id === (int) $this->_args['field_id'];
		}

		// Otherwise, all Address fields are applicable.
		return $field->get_input_type() === 'address';
	}

}

// Restrict states for all Address field.
//new GW_Restrict_States_In_Address_Field( array(
//	'allowed_states' => array(
//		'California',
//		'Iowa',
//	),
//) );
// Or when value is different, like using gform_us_states
// (2 letter state values, full state name label).
//new GW_Restrict_States_In_Address_Field( array(
//	'allowed_states' => array(
//		'CA' => 'California',
//		'IA' => 'Iowa',
//	),
//) );

// Restrict states for specific Address field.
new GW_Restrict_States_In_Address_Field( array(
	'form_id'        => 123,
	'field_id'       => 4,
	'allowed_states' => array(
		'California',
		'Iowa',
	),
) );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.