@@ -19,7 +19,7 @@ |
||
19 | 19 | */ |
20 | 20 | public function __construct($validation_error_message = null) |
21 | 21 | { |
22 | - if (! $validation_error_message) { |
|
22 | + if ( ! $validation_error_message) { |
|
23 | 23 | $validation_error_message = esc_html__("HTML tags are not permitted in this field", "event_espresso"); |
24 | 24 | } |
25 | 25 | parent::__construct($validation_error_message); |
@@ -13,27 +13,27 @@ |
||
13 | 13 | */ |
14 | 14 | class EE_Plaintext_Validation_Strategy extends EE_Validation_Strategy_Base |
15 | 15 | { |
16 | - /** |
|
17 | - * @param null $validation_error_message |
|
18 | - */ |
|
19 | - public function __construct($validation_error_message = null) |
|
20 | - { |
|
21 | - if (! $validation_error_message) { |
|
22 | - $validation_error_message = esc_html__("HTML tags are not permitted in this field", "event_espresso"); |
|
23 | - } |
|
24 | - parent::__construct($validation_error_message); |
|
25 | - } |
|
16 | + /** |
|
17 | + * @param null $validation_error_message |
|
18 | + */ |
|
19 | + public function __construct($validation_error_message = null) |
|
20 | + { |
|
21 | + if (! $validation_error_message) { |
|
22 | + $validation_error_message = esc_html__("HTML tags are not permitted in this field", "event_espresso"); |
|
23 | + } |
|
24 | + parent::__construct($validation_error_message); |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * @param $normalized_value |
|
29 | - * @throws \EE_Validation_Error |
|
30 | - */ |
|
31 | - public function validate($normalized_value) |
|
32 | - { |
|
33 | - $no_tags = wp_strip_all_tags($normalized_value); |
|
34 | - if (strlen($no_tags) < strlen(trim($normalized_value))) { |
|
35 | - throw new EE_Validation_Error($this->get_validation_error_message(), 'no_html_tags'); |
|
36 | - } |
|
37 | - parent::validate($normalized_value); |
|
38 | - } |
|
27 | + /** |
|
28 | + * @param $normalized_value |
|
29 | + * @throws \EE_Validation_Error |
|
30 | + */ |
|
31 | + public function validate($normalized_value) |
|
32 | + { |
|
33 | + $no_tags = wp_strip_all_tags($normalized_value); |
|
34 | + if (strlen($no_tags) < strlen(trim($normalized_value))) { |
|
35 | + throw new EE_Validation_Error($this->get_validation_error_message(), 'no_html_tags'); |
|
36 | + } |
|
37 | + parent::validate($normalized_value); |
|
38 | + } |
|
39 | 39 | } |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | */ |
29 | 29 | public function __construct($validation_error_message = null, $requirement_conditions = array()) |
30 | 30 | { |
31 | - if (! $validation_error_message) { |
|
31 | + if ( ! $validation_error_message) { |
|
32 | 32 | $validation_error_message = esc_html__("This field is required.", "event_espresso"); |
33 | 33 | } |
34 | 34 | $this->set_requirement_conditions($requirement_conditions); |
@@ -116,7 +116,7 @@ discard block |
||
116 | 116 | { |
117 | 117 | $requirement_value = ''; |
118 | 118 | $conditions = $this->get_requirement_conditions(); |
119 | - if (! is_array($conditions)) { |
|
119 | + if ( ! is_array($conditions)) { |
|
120 | 120 | EE_Error::throw_exception_if_debugging( |
121 | 121 | sprintf( |
122 | 122 | esc_html__('Input requirement conditions must be an array. You provided %1$s', 'event_espresso'), |
@@ -141,7 +141,7 @@ discard block |
||
141 | 141 | } |
142 | 142 | foreach ($conditions as $input_path => $op_and_value) { |
143 | 143 | $input = $this->_input->find_section_from_path($input_path); |
144 | - if (! $input instanceof EE_Form_Input_Base) { |
|
144 | + if ( ! $input instanceof EE_Form_Input_Base) { |
|
145 | 145 | EE_Error::throw_exception_if_debugging( |
146 | 146 | sprintf( |
147 | 147 | esc_html__('Error encountered while setting requirement condition for input %1$s. The path %2$s does not correspond to a valid input', 'event_espresso'), |
@@ -154,9 +154,9 @@ discard block |
||
154 | 154 | ); |
155 | 155 | return false; |
156 | 156 | } |
157 | - list( $op, $value ) = $this->_validate_op_and_value($op_and_value); |
|
157 | + list($op, $value) = $this->_validate_op_and_value($op_and_value); |
|
158 | 158 | // ok now the jquery dependency expression depends on the input's display strategy. |
159 | - if (! $input->get_display_strategy() instanceof EE_Select_Display_Strategy) { |
|
159 | + if ( ! $input->get_display_strategy() instanceof EE_Select_Display_Strategy) { |
|
160 | 160 | EE_Error::throw_exception_if_debugging( |
161 | 161 | sprintf( |
162 | 162 | esc_html__('Required Validation Strategy can only depend on another input which uses the EE_Select_Display_Strategy, but you specified a field "%1$s" that uses display strategy "%2$s". If you need others, please add support for it! The related input is %3$s', 'event_espresso'), |
@@ -169,7 +169,7 @@ discard block |
||
169 | 169 | __LINE__ |
170 | 170 | ); |
171 | 171 | } |
172 | - $requirement_value = $input->html_id(true) . ' option[value="' . $value . '"]:selected'; |
|
172 | + $requirement_value = $input->html_id(true).' option[value="'.$value.'"]:selected'; |
|
173 | 173 | } |
174 | 174 | return $requirement_value; |
175 | 175 | } |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | $conditions = $this->get_requirement_conditions(); |
191 | 191 | foreach ($conditions as $input_path => $op_and_value) { |
192 | 192 | $input = $this->_input->find_section_from_path($input_path); |
193 | - if (! $input instanceof EE_Form_Input_Base) { |
|
193 | + if ( ! $input instanceof EE_Form_Input_Base) { |
|
194 | 194 | EE_Error::throw_exception_if_debugging( |
195 | 195 | sprintf( |
196 | 196 | esc_html__('Error encountered while setting requirement condition for input %1$s. The path %2$s does not correspond to a valid input', 'event_espresso'), |
@@ -203,13 +203,13 @@ discard block |
||
203 | 203 | ); |
204 | 204 | return false; |
205 | 205 | } |
206 | - list( $op, $value ) = $this->_validate_op_and_value($op_and_value); |
|
206 | + list($op, $value) = $this->_validate_op_and_value($op_and_value); |
|
207 | 207 | switch ($op) { |
208 | 208 | case '=': |
209 | 209 | default: |
210 | 210 | $meets_all_requirements = $input->normalized_value() === $value; |
211 | 211 | } |
212 | - if (! $meets_all_requirements) { |
|
212 | + if ( ! $meets_all_requirements) { |
|
213 | 213 | break; |
214 | 214 | } |
215 | 215 | } |
@@ -228,7 +228,7 @@ discard block |
||
228 | 228 | */ |
229 | 229 | protected function _validate_op_and_value($op_and_value) |
230 | 230 | { |
231 | - if (! isset($op_and_value[0], $op_and_value[1])) { |
|
231 | + if ( ! isset($op_and_value[0], $op_and_value[1])) { |
|
232 | 232 | EE_Error::throw_exception_if_debugging( |
233 | 233 | sprintf( |
234 | 234 | esc_html__('Required Validation Strategy conditions array\'s value must be an array with two elements: an operator, and a value. It didn\'t. The related input is %1$s', 'event_espresso'), |
@@ -252,6 +252,6 @@ discard block |
||
252 | 252 | __LINE__ |
253 | 253 | ); |
254 | 254 | } |
255 | - return array( $operator, $value ); |
|
255 | + return array($operator, $value); |
|
256 | 256 | } |
257 | 257 | } |
@@ -11,246 +11,246 @@ |
||
11 | 11 | */ |
12 | 12 | class EE_Conditionally_Required_Validation_Strategy extends EE_Validation_Strategy_Base |
13 | 13 | { |
14 | - /** |
|
15 | - * Array describing conditions necessary to make the input required. |
|
16 | - * This is used to derive a jquery dependency expression (see http://jqueryvalidation.org/required-method) |
|
17 | - * or jquery callback; and server-side logic to determine if the field is necessary. |
|
18 | - * @var array |
|
19 | - */ |
|
20 | - protected $requirement_conditions; |
|
14 | + /** |
|
15 | + * Array describing conditions necessary to make the input required. |
|
16 | + * This is used to derive a jquery dependency expression (see http://jqueryvalidation.org/required-method) |
|
17 | + * or jquery callback; and server-side logic to determine if the field is necessary. |
|
18 | + * @var array |
|
19 | + */ |
|
20 | + protected $requirement_conditions; |
|
21 | 21 | |
22 | 22 | |
23 | 23 | |
24 | - /** |
|
25 | - * @param string $validation_error_message |
|
26 | - * @param array $requirement_conditions |
|
27 | - */ |
|
28 | - public function __construct($validation_error_message = null, $requirement_conditions = array()) |
|
29 | - { |
|
30 | - if (! $validation_error_message) { |
|
31 | - $validation_error_message = esc_html__("This field is required.", "event_espresso"); |
|
32 | - } |
|
33 | - $this->set_requirement_conditions($requirement_conditions); |
|
34 | - parent::__construct($validation_error_message); |
|
35 | - } |
|
24 | + /** |
|
25 | + * @param string $validation_error_message |
|
26 | + * @param array $requirement_conditions |
|
27 | + */ |
|
28 | + public function __construct($validation_error_message = null, $requirement_conditions = array()) |
|
29 | + { |
|
30 | + if (! $validation_error_message) { |
|
31 | + $validation_error_message = esc_html__("This field is required.", "event_espresso"); |
|
32 | + } |
|
33 | + $this->set_requirement_conditions($requirement_conditions); |
|
34 | + parent::__construct($validation_error_message); |
|
35 | + } |
|
36 | 36 | |
37 | 37 | |
38 | 38 | |
39 | - /** |
|
40 | - * just checks the field isn't blank, provided the requirement conditions |
|
41 | - * indicate this input is still required |
|
42 | - * |
|
43 | - * @param $normalized_value |
|
44 | - * @return bool |
|
45 | - * @throws \EE_Error |
|
46 | - * @throws \EE_Validation_Error |
|
47 | - */ |
|
48 | - public function validate($normalized_value) |
|
49 | - { |
|
50 | - if ( |
|
51 | - ( |
|
52 | - $normalized_value === '' |
|
53 | - || $normalized_value === null |
|
54 | - || $normalized_value === array() |
|
55 | - ) |
|
56 | - && $this->_input_is_required_server_side() |
|
57 | - ) { |
|
58 | - throw new EE_Validation_Error($this->get_validation_error_message(), 'required'); |
|
59 | - } else { |
|
60 | - return true; |
|
61 | - } |
|
62 | - } |
|
39 | + /** |
|
40 | + * just checks the field isn't blank, provided the requirement conditions |
|
41 | + * indicate this input is still required |
|
42 | + * |
|
43 | + * @param $normalized_value |
|
44 | + * @return bool |
|
45 | + * @throws \EE_Error |
|
46 | + * @throws \EE_Validation_Error |
|
47 | + */ |
|
48 | + public function validate($normalized_value) |
|
49 | + { |
|
50 | + if ( |
|
51 | + ( |
|
52 | + $normalized_value === '' |
|
53 | + || $normalized_value === null |
|
54 | + || $normalized_value === array() |
|
55 | + ) |
|
56 | + && $this->_input_is_required_server_side() |
|
57 | + ) { |
|
58 | + throw new EE_Validation_Error($this->get_validation_error_message(), 'required'); |
|
59 | + } else { |
|
60 | + return true; |
|
61 | + } |
|
62 | + } |
|
63 | 63 | |
64 | 64 | |
65 | 65 | |
66 | - /** |
|
67 | - * @return array |
|
68 | - * @throws \EE_Error |
|
69 | - */ |
|
70 | - public function get_jquery_validation_rule_array() |
|
71 | - { |
|
72 | - return array( |
|
73 | - 'required' => $this->_get_jquery_requirement_value(), |
|
74 | - 'messages' => array( |
|
75 | - 'required' => $this->get_validation_error_message() |
|
76 | - ) |
|
77 | - ); |
|
78 | - } |
|
66 | + /** |
|
67 | + * @return array |
|
68 | + * @throws \EE_Error |
|
69 | + */ |
|
70 | + public function get_jquery_validation_rule_array() |
|
71 | + { |
|
72 | + return array( |
|
73 | + 'required' => $this->_get_jquery_requirement_value(), |
|
74 | + 'messages' => array( |
|
75 | + 'required' => $this->get_validation_error_message() |
|
76 | + ) |
|
77 | + ); |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * Sets the "required conditions". This should be an array, its top-level key |
|
82 | - * is the name of a field, its value is an array. This 2nd level array has two items: |
|
83 | - * the first is the operator (for now only '=' is accepted), and teh 2nd argument is the |
|
84 | - * the value the field should be in order to make the field required. |
|
85 | - * Eg array( 'payment_type' => array( '=', 'credit_card' ). |
|
86 | - * |
|
87 | - * @param array $requirement_conditions |
|
88 | - */ |
|
89 | - public function set_requirement_conditions($requirement_conditions) |
|
90 | - { |
|
91 | - $this->requirement_conditions = (array) $requirement_conditions; |
|
92 | - } |
|
80 | + /** |
|
81 | + * Sets the "required conditions". This should be an array, its top-level key |
|
82 | + * is the name of a field, its value is an array. This 2nd level array has two items: |
|
83 | + * the first is the operator (for now only '=' is accepted), and teh 2nd argument is the |
|
84 | + * the value the field should be in order to make the field required. |
|
85 | + * Eg array( 'payment_type' => array( '=', 'credit_card' ). |
|
86 | + * |
|
87 | + * @param array $requirement_conditions |
|
88 | + */ |
|
89 | + public function set_requirement_conditions($requirement_conditions) |
|
90 | + { |
|
91 | + $this->requirement_conditions = (array) $requirement_conditions; |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * Gets the array that describes when the related input should be required. |
|
96 | - * see set_requirement_conditions for a description of how it should be formatted |
|
97 | - * @return array |
|
98 | - */ |
|
99 | - public function get_requirement_conditions() |
|
100 | - { |
|
101 | - return $this->requirement_conditions; |
|
102 | - } |
|
94 | + /** |
|
95 | + * Gets the array that describes when the related input should be required. |
|
96 | + * see set_requirement_conditions for a description of how it should be formatted |
|
97 | + * @return array |
|
98 | + */ |
|
99 | + public function get_requirement_conditions() |
|
100 | + { |
|
101 | + return $this->requirement_conditions; |
|
102 | + } |
|
103 | 103 | |
104 | 104 | |
105 | 105 | |
106 | - /** |
|
107 | - * gets jQuery dependency expression used for client-side validation |
|
108 | - * Its possible this could also return a javascript callback used for determining |
|
109 | - * if the input is required or not. That is not yet implemented, however. |
|
110 | - * |
|
111 | - * @return string see http://jqueryvalidation.org/required-method for format |
|
112 | - * @throws \EE_Error |
|
113 | - */ |
|
114 | - protected function _get_jquery_requirement_value() |
|
115 | - { |
|
116 | - $requirement_value = ''; |
|
117 | - $conditions = $this->get_requirement_conditions(); |
|
118 | - if (! is_array($conditions)) { |
|
119 | - EE_Error::throw_exception_if_debugging( |
|
120 | - sprintf( |
|
121 | - esc_html__('Input requirement conditions must be an array. You provided %1$s', 'event_espresso'), |
|
122 | - $this->_input->name() |
|
123 | - ), |
|
124 | - __FILE__, |
|
125 | - __FUNCTION__, |
|
126 | - __LINE__ |
|
127 | - ); |
|
128 | - return true; |
|
129 | - } |
|
130 | - if (count($conditions) > 1) { |
|
131 | - EE_Error::throw_exception_if_debugging( |
|
132 | - sprintf( |
|
133 | - esc_html__('Required Validation Strategy does not yet support multiple conditions. You should add it! The related input is %1$s', 'event_espresso'), |
|
134 | - $this->_input->name() |
|
135 | - ), |
|
136 | - __FILE__, |
|
137 | - __FUNCTION__, |
|
138 | - __LINE__ |
|
139 | - ); |
|
140 | - } |
|
141 | - foreach ($conditions as $input_path => $op_and_value) { |
|
142 | - $input = $this->_input->find_section_from_path($input_path); |
|
143 | - if (! $input instanceof EE_Form_Input_Base) { |
|
144 | - EE_Error::throw_exception_if_debugging( |
|
145 | - sprintf( |
|
146 | - esc_html__('Error encountered while setting requirement condition for input %1$s. The path %2$s does not correspond to a valid input', 'event_espresso'), |
|
147 | - $this->_input->name(), |
|
148 | - $input_path |
|
149 | - ), |
|
150 | - __FILE__, |
|
151 | - __FUNCTION__, |
|
152 | - __LINE__ |
|
153 | - ); |
|
154 | - return false; |
|
155 | - } |
|
156 | - list( $op, $value ) = $this->_validate_op_and_value($op_and_value); |
|
157 | - // ok now the jquery dependency expression depends on the input's display strategy. |
|
158 | - if (! $input->get_display_strategy() instanceof EE_Select_Display_Strategy) { |
|
159 | - EE_Error::throw_exception_if_debugging( |
|
160 | - sprintf( |
|
161 | - esc_html__('Required Validation Strategy can only depend on another input which uses the EE_Select_Display_Strategy, but you specified a field "%1$s" that uses display strategy "%2$s". If you need others, please add support for it! The related input is %3$s', 'event_espresso'), |
|
162 | - $input->name(), |
|
163 | - get_class($input->get_display_strategy()), |
|
164 | - $this->_input->name() |
|
165 | - ), |
|
166 | - __FILE__, |
|
167 | - __FUNCTION__, |
|
168 | - __LINE__ |
|
169 | - ); |
|
170 | - } |
|
171 | - $requirement_value = $input->html_id(true) . ' option[value="' . $value . '"]:selected'; |
|
172 | - } |
|
173 | - return $requirement_value; |
|
174 | - } |
|
106 | + /** |
|
107 | + * gets jQuery dependency expression used for client-side validation |
|
108 | + * Its possible this could also return a javascript callback used for determining |
|
109 | + * if the input is required or not. That is not yet implemented, however. |
|
110 | + * |
|
111 | + * @return string see http://jqueryvalidation.org/required-method for format |
|
112 | + * @throws \EE_Error |
|
113 | + */ |
|
114 | + protected function _get_jquery_requirement_value() |
|
115 | + { |
|
116 | + $requirement_value = ''; |
|
117 | + $conditions = $this->get_requirement_conditions(); |
|
118 | + if (! is_array($conditions)) { |
|
119 | + EE_Error::throw_exception_if_debugging( |
|
120 | + sprintf( |
|
121 | + esc_html__('Input requirement conditions must be an array. You provided %1$s', 'event_espresso'), |
|
122 | + $this->_input->name() |
|
123 | + ), |
|
124 | + __FILE__, |
|
125 | + __FUNCTION__, |
|
126 | + __LINE__ |
|
127 | + ); |
|
128 | + return true; |
|
129 | + } |
|
130 | + if (count($conditions) > 1) { |
|
131 | + EE_Error::throw_exception_if_debugging( |
|
132 | + sprintf( |
|
133 | + esc_html__('Required Validation Strategy does not yet support multiple conditions. You should add it! The related input is %1$s', 'event_espresso'), |
|
134 | + $this->_input->name() |
|
135 | + ), |
|
136 | + __FILE__, |
|
137 | + __FUNCTION__, |
|
138 | + __LINE__ |
|
139 | + ); |
|
140 | + } |
|
141 | + foreach ($conditions as $input_path => $op_and_value) { |
|
142 | + $input = $this->_input->find_section_from_path($input_path); |
|
143 | + if (! $input instanceof EE_Form_Input_Base) { |
|
144 | + EE_Error::throw_exception_if_debugging( |
|
145 | + sprintf( |
|
146 | + esc_html__('Error encountered while setting requirement condition for input %1$s. The path %2$s does not correspond to a valid input', 'event_espresso'), |
|
147 | + $this->_input->name(), |
|
148 | + $input_path |
|
149 | + ), |
|
150 | + __FILE__, |
|
151 | + __FUNCTION__, |
|
152 | + __LINE__ |
|
153 | + ); |
|
154 | + return false; |
|
155 | + } |
|
156 | + list( $op, $value ) = $this->_validate_op_and_value($op_and_value); |
|
157 | + // ok now the jquery dependency expression depends on the input's display strategy. |
|
158 | + if (! $input->get_display_strategy() instanceof EE_Select_Display_Strategy) { |
|
159 | + EE_Error::throw_exception_if_debugging( |
|
160 | + sprintf( |
|
161 | + esc_html__('Required Validation Strategy can only depend on another input which uses the EE_Select_Display_Strategy, but you specified a field "%1$s" that uses display strategy "%2$s". If you need others, please add support for it! The related input is %3$s', 'event_espresso'), |
|
162 | + $input->name(), |
|
163 | + get_class($input->get_display_strategy()), |
|
164 | + $this->_input->name() |
|
165 | + ), |
|
166 | + __FILE__, |
|
167 | + __FUNCTION__, |
|
168 | + __LINE__ |
|
169 | + ); |
|
170 | + } |
|
171 | + $requirement_value = $input->html_id(true) . ' option[value="' . $value . '"]:selected'; |
|
172 | + } |
|
173 | + return $requirement_value; |
|
174 | + } |
|
175 | 175 | |
176 | 176 | |
177 | 177 | |
178 | - /** |
|
179 | - * Returns whether or not this input is required based on the _requirement_conditions |
|
180 | - * (not whether or not the input passes validation. That's for the validate method |
|
181 | - * to decide) |
|
182 | - * |
|
183 | - * @return boolean |
|
184 | - * @throws \EE_Error |
|
185 | - */ |
|
186 | - protected function _input_is_required_server_side() |
|
187 | - { |
|
188 | - $meets_all_requirements = true; |
|
189 | - $conditions = $this->get_requirement_conditions(); |
|
190 | - foreach ($conditions as $input_path => $op_and_value) { |
|
191 | - $input = $this->_input->find_section_from_path($input_path); |
|
192 | - if (! $input instanceof EE_Form_Input_Base) { |
|
193 | - EE_Error::throw_exception_if_debugging( |
|
194 | - sprintf( |
|
195 | - esc_html__('Error encountered while setting requirement condition for input %1$s. The path %2$s does not correspond to a valid input', 'event_espresso'), |
|
196 | - $this->_input->name(), |
|
197 | - $input_path |
|
198 | - ), |
|
199 | - __FILE__, |
|
200 | - __FUNCTION__, |
|
201 | - __LINE__ |
|
202 | - ); |
|
203 | - return false; |
|
204 | - } |
|
205 | - list( $op, $value ) = $this->_validate_op_and_value($op_and_value); |
|
206 | - switch ($op) { |
|
207 | - case '=': |
|
208 | - default: |
|
209 | - $meets_all_requirements = $input->normalized_value() === $value; |
|
210 | - } |
|
211 | - if (! $meets_all_requirements) { |
|
212 | - break; |
|
213 | - } |
|
214 | - } |
|
215 | - return $meets_all_requirements; |
|
216 | - } |
|
178 | + /** |
|
179 | + * Returns whether or not this input is required based on the _requirement_conditions |
|
180 | + * (not whether or not the input passes validation. That's for the validate method |
|
181 | + * to decide) |
|
182 | + * |
|
183 | + * @return boolean |
|
184 | + * @throws \EE_Error |
|
185 | + */ |
|
186 | + protected function _input_is_required_server_side() |
|
187 | + { |
|
188 | + $meets_all_requirements = true; |
|
189 | + $conditions = $this->get_requirement_conditions(); |
|
190 | + foreach ($conditions as $input_path => $op_and_value) { |
|
191 | + $input = $this->_input->find_section_from_path($input_path); |
|
192 | + if (! $input instanceof EE_Form_Input_Base) { |
|
193 | + EE_Error::throw_exception_if_debugging( |
|
194 | + sprintf( |
|
195 | + esc_html__('Error encountered while setting requirement condition for input %1$s. The path %2$s does not correspond to a valid input', 'event_espresso'), |
|
196 | + $this->_input->name(), |
|
197 | + $input_path |
|
198 | + ), |
|
199 | + __FILE__, |
|
200 | + __FUNCTION__, |
|
201 | + __LINE__ |
|
202 | + ); |
|
203 | + return false; |
|
204 | + } |
|
205 | + list( $op, $value ) = $this->_validate_op_and_value($op_and_value); |
|
206 | + switch ($op) { |
|
207 | + case '=': |
|
208 | + default: |
|
209 | + $meets_all_requirements = $input->normalized_value() === $value; |
|
210 | + } |
|
211 | + if (! $meets_all_requirements) { |
|
212 | + break; |
|
213 | + } |
|
214 | + } |
|
215 | + return $meets_all_requirements; |
|
216 | + } |
|
217 | 217 | |
218 | 218 | |
219 | 219 | |
220 | - /** |
|
221 | - * Verifies this is an array with keys 0 and 1, where key 0 is a usable |
|
222 | - * operator (initially just '=') and key 1 is something that can be cast to a string |
|
223 | - * |
|
224 | - * @param array $op_and_value |
|
225 | - * @return array |
|
226 | - * @throws \EE_Error |
|
227 | - */ |
|
228 | - protected function _validate_op_and_value($op_and_value) |
|
229 | - { |
|
230 | - if (! isset($op_and_value[0], $op_and_value[1])) { |
|
231 | - EE_Error::throw_exception_if_debugging( |
|
232 | - sprintf( |
|
233 | - esc_html__('Required Validation Strategy conditions array\'s value must be an array with two elements: an operator, and a value. It didn\'t. The related input is %1$s', 'event_espresso'), |
|
234 | - $this->_input->name() |
|
235 | - ), |
|
236 | - __FILE__, |
|
237 | - __FUNCTION__, |
|
238 | - __LINE__ |
|
239 | - ); |
|
240 | - } |
|
241 | - $operator = $op_and_value[0]; |
|
242 | - $value = (string) $op_and_value[1]; |
|
243 | - if ($operator !== '=') { |
|
244 | - EE_Error::throw_exception_if_debugging( |
|
245 | - sprintf( |
|
246 | - esc_html__('Required Validation Strategy conditions can currently only use the equals operator. If you need others, please add support for it! The related input is %1$s', 'event_espresso'), |
|
247 | - $this->_input->name() |
|
248 | - ), |
|
249 | - __FILE__, |
|
250 | - __FUNCTION__, |
|
251 | - __LINE__ |
|
252 | - ); |
|
253 | - } |
|
254 | - return array( $operator, $value ); |
|
255 | - } |
|
220 | + /** |
|
221 | + * Verifies this is an array with keys 0 and 1, where key 0 is a usable |
|
222 | + * operator (initially just '=') and key 1 is something that can be cast to a string |
|
223 | + * |
|
224 | + * @param array $op_and_value |
|
225 | + * @return array |
|
226 | + * @throws \EE_Error |
|
227 | + */ |
|
228 | + protected function _validate_op_and_value($op_and_value) |
|
229 | + { |
|
230 | + if (! isset($op_and_value[0], $op_and_value[1])) { |
|
231 | + EE_Error::throw_exception_if_debugging( |
|
232 | + sprintf( |
|
233 | + esc_html__('Required Validation Strategy conditions array\'s value must be an array with two elements: an operator, and a value. It didn\'t. The related input is %1$s', 'event_espresso'), |
|
234 | + $this->_input->name() |
|
235 | + ), |
|
236 | + __FILE__, |
|
237 | + __FUNCTION__, |
|
238 | + __LINE__ |
|
239 | + ); |
|
240 | + } |
|
241 | + $operator = $op_and_value[0]; |
|
242 | + $value = (string) $op_and_value[1]; |
|
243 | + if ($operator !== '=') { |
|
244 | + EE_Error::throw_exception_if_debugging( |
|
245 | + sprintf( |
|
246 | + esc_html__('Required Validation Strategy conditions can currently only use the equals operator. If you need others, please add support for it! The related input is %1$s', 'event_espresso'), |
|
247 | + $this->_input->name() |
|
248 | + ), |
|
249 | + __FILE__, |
|
250 | + __FUNCTION__, |
|
251 | + __LINE__ |
|
252 | + ); |
|
253 | + } |
|
254 | + return array( $operator, $value ); |
|
255 | + } |
|
256 | 256 | } |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | */ |
18 | 18 | public function __construct($validation_error_message = null) |
19 | 19 | { |
20 | - if (! $validation_error_message) { |
|
20 | + if ( ! $validation_error_message) { |
|
21 | 21 | $validation_error_message = sprintf(esc_html__("Only numeric characters, commas, periods, and spaces, please!", "event_espresso")); |
22 | 22 | } |
23 | 23 | parent::__construct($validation_error_message); |
@@ -42,6 +42,6 @@ discard block |
||
42 | 42 | */ |
43 | 43 | public function get_jquery_validation_rule_array() |
44 | 44 | { |
45 | - return array('number' => true, 'messages' => array( 'number' => $this->get_validation_error_message() ) ); |
|
45 | + return array('number' => true, 'messages' => array('number' => $this->get_validation_error_message())); |
|
46 | 46 | } |
47 | 47 | } |
@@ -11,36 +11,36 @@ |
||
11 | 11 | */ |
12 | 12 | class EE_Float_Validation_Strategy extends EE_Validation_Strategy_Base |
13 | 13 | { |
14 | - /** |
|
15 | - * @param null $validation_error_message |
|
16 | - */ |
|
17 | - public function __construct($validation_error_message = null) |
|
18 | - { |
|
19 | - if (! $validation_error_message) { |
|
20 | - $validation_error_message = sprintf(esc_html__("Only numeric characters, commas, periods, and spaces, please!", "event_espresso")); |
|
21 | - } |
|
22 | - parent::__construct($validation_error_message); |
|
23 | - } |
|
14 | + /** |
|
15 | + * @param null $validation_error_message |
|
16 | + */ |
|
17 | + public function __construct($validation_error_message = null) |
|
18 | + { |
|
19 | + if (! $validation_error_message) { |
|
20 | + $validation_error_message = sprintf(esc_html__("Only numeric characters, commas, periods, and spaces, please!", "event_espresso")); |
|
21 | + } |
|
22 | + parent::__construct($validation_error_message); |
|
23 | + } |
|
24 | 24 | |
25 | 25 | |
26 | 26 | |
27 | - /** |
|
28 | - * |
|
29 | - * @param $normalized_value |
|
30 | - * @return bool |
|
31 | - */ |
|
32 | - public function validate($normalized_value) |
|
33 | - { |
|
34 | - // errors should have been detected by the normalization strategy |
|
35 | - } |
|
27 | + /** |
|
28 | + * |
|
29 | + * @param $normalized_value |
|
30 | + * @return bool |
|
31 | + */ |
|
32 | + public function validate($normalized_value) |
|
33 | + { |
|
34 | + // errors should have been detected by the normalization strategy |
|
35 | + } |
|
36 | 36 | |
37 | 37 | |
38 | 38 | |
39 | - /** |
|
40 | - * @return array |
|
41 | - */ |
|
42 | - public function get_jquery_validation_rule_array() |
|
43 | - { |
|
44 | - return array('number' => true, 'messages' => array( 'number' => $this->get_validation_error_message() ) ); |
|
45 | - } |
|
39 | + /** |
|
40 | + * @return array |
|
41 | + */ |
|
42 | + public function get_jquery_validation_rule_array() |
|
43 | + { |
|
44 | + return array('number' => true, 'messages' => array( 'number' => $this->get_validation_error_message() ) ); |
|
45 | + } |
|
46 | 46 | } |
@@ -19,7 +19,7 @@ discard block |
||
19 | 19 | */ |
20 | 20 | public function __construct($validation_error_message = null) |
21 | 21 | { |
22 | - if (! $validation_error_message) { |
|
22 | + if ( ! $validation_error_message) { |
|
23 | 23 | $allowedtags = $this->_get_allowed_tags(); |
24 | 24 | $validation_error_message = sprintf(esc_html__("Only simple HTML tags are allowed. Eg, %s", "event_espresso"), implode(",", array_keys($allowedtags))); |
25 | 25 | } |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | { |
63 | 63 | $allowedtags = $this->_get_allowed_tags(); |
64 | 64 | parent::validate($normalized_value); |
65 | - $normalized_value_sans_tags = wp_kses("$normalized_value", $allowedtags); |
|
65 | + $normalized_value_sans_tags = wp_kses("$normalized_value", $allowedtags); |
|
66 | 66 | if (strlen($normalized_value) > strlen($normalized_value_sans_tags)) { |
67 | 67 | throw new EE_Validation_Error($this->get_validation_error_message(), 'complex_html_tags'); |
68 | 68 | } |
@@ -13,57 +13,57 @@ |
||
13 | 13 | */ |
14 | 14 | class EE_Simple_HTML_Validation_Strategy extends EE_Validation_Strategy_Base |
15 | 15 | { |
16 | - /** |
|
17 | - * @param null $validation_error_message |
|
18 | - */ |
|
19 | - public function __construct($validation_error_message = null) |
|
20 | - { |
|
21 | - if (! $validation_error_message) { |
|
22 | - $allowedtags = $this->_get_allowed_tags(); |
|
23 | - $validation_error_message = sprintf(esc_html__("Only simple HTML tags are allowed. Eg, %s", "event_espresso"), implode(",", array_keys($allowedtags))); |
|
24 | - } |
|
25 | - parent::__construct($validation_error_message); |
|
26 | - } |
|
16 | + /** |
|
17 | + * @param null $validation_error_message |
|
18 | + */ |
|
19 | + public function __construct($validation_error_message = null) |
|
20 | + { |
|
21 | + if (! $validation_error_message) { |
|
22 | + $allowedtags = $this->_get_allowed_tags(); |
|
23 | + $validation_error_message = sprintf(esc_html__("Only simple HTML tags are allowed. Eg, %s", "event_espresso"), implode(",", array_keys($allowedtags))); |
|
24 | + } |
|
25 | + parent::__construct($validation_error_message); |
|
26 | + } |
|
27 | 27 | |
28 | 28 | |
29 | 29 | |
30 | - /** |
|
31 | - * get tags allowed |
|
32 | - */ |
|
33 | - protected function _get_allowed_tags() |
|
34 | - { |
|
35 | - return EEH_HTML::get_simple_tags(); |
|
36 | - } |
|
30 | + /** |
|
31 | + * get tags allowed |
|
32 | + */ |
|
33 | + protected function _get_allowed_tags() |
|
34 | + { |
|
35 | + return EEH_HTML::get_simple_tags(); |
|
36 | + } |
|
37 | 37 | |
38 | 38 | |
39 | 39 | |
40 | - /** |
|
41 | - * add_more_tags |
|
42 | - * |
|
43 | - * generates and returns a string that lists the top-level HTML tags that are allowable for this input |
|
44 | - * |
|
45 | - * @return string |
|
46 | - */ |
|
47 | - public function get_list_of_allowed_tags() |
|
48 | - { |
|
49 | - $allowed_tags = $this->_get_allowed_tags(); |
|
50 | - ksort($allowed_tags); |
|
51 | - return implode(', ', array_keys($allowed_tags)); |
|
52 | - } |
|
40 | + /** |
|
41 | + * add_more_tags |
|
42 | + * |
|
43 | + * generates and returns a string that lists the top-level HTML tags that are allowable for this input |
|
44 | + * |
|
45 | + * @return string |
|
46 | + */ |
|
47 | + public function get_list_of_allowed_tags() |
|
48 | + { |
|
49 | + $allowed_tags = $this->_get_allowed_tags(); |
|
50 | + ksort($allowed_tags); |
|
51 | + return implode(', ', array_keys($allowed_tags)); |
|
52 | + } |
|
53 | 53 | |
54 | 54 | |
55 | 55 | |
56 | - /** |
|
57 | - * @param $normalized_value |
|
58 | - * @throws \EE_Validation_Error |
|
59 | - */ |
|
60 | - public function validate($normalized_value) |
|
61 | - { |
|
62 | - $allowedtags = $this->_get_allowed_tags(); |
|
63 | - parent::validate($normalized_value); |
|
64 | - $normalized_value_sans_tags = wp_kses("$normalized_value", $allowedtags); |
|
65 | - if (strlen($normalized_value) > strlen($normalized_value_sans_tags)) { |
|
66 | - throw new EE_Validation_Error($this->get_validation_error_message(), 'complex_html_tags'); |
|
67 | - } |
|
68 | - } |
|
56 | + /** |
|
57 | + * @param $normalized_value |
|
58 | + * @throws \EE_Validation_Error |
|
59 | + */ |
|
60 | + public function validate($normalized_value) |
|
61 | + { |
|
62 | + $allowedtags = $this->_get_allowed_tags(); |
|
63 | + parent::validate($normalized_value); |
|
64 | + $normalized_value_sans_tags = wp_kses("$normalized_value", $allowedtags); |
|
65 | + if (strlen($normalized_value) > strlen($normalized_value_sans_tags)) { |
|
66 | + throw new EE_Validation_Error($this->get_validation_error_message(), 'complex_html_tags'); |
|
67 | + } |
|
68 | + } |
|
69 | 69 | } |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | if (is_float($value_to_normalize) || is_int($value_to_normalize)) { |
34 | 34 | return (float) $value_to_normalize; |
35 | 35 | } |
36 | - if (! is_string($value_to_normalize)) { |
|
36 | + if ( ! is_string($value_to_normalize)) { |
|
37 | 37 | throw new EE_Validation_Error( |
38 | 38 | sprintf( |
39 | 39 | esc_html__('The value "%s" must be a string submitted for normalization, it was %s', 'event_espresso'), |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | } |
70 | 70 | // this really shouldn't ever happen because fields with a float normalization strategy |
71 | 71 | // should also have a float validation strategy, but in case it doesn't use the default |
72 | - if (! $validation_error_message) { |
|
72 | + if ( ! $validation_error_message) { |
|
73 | 73 | $default_validation_strategy = new EE_Float_Validation_Strategy(); |
74 | 74 | $validation_error_message = $default_validation_strategy->get_validation_error_message(); |
75 | 75 | } |
@@ -10,84 +10,84 @@ |
||
10 | 10 | */ |
11 | 11 | class EE_Float_Normalization extends EE_Normalization_Strategy_Base |
12 | 12 | { |
13 | - /* |
|
13 | + /* |
|
14 | 14 | * regex pattern that matches for the following: |
15 | 15 | * * optional negative sign |
16 | 16 | * * one or more digits or decimals |
17 | 17 | */ |
18 | - const REGEX = '/^(-?)([\d.]+)$/'; |
|
18 | + const REGEX = '/^(-?)([\d.]+)$/'; |
|
19 | 19 | |
20 | 20 | |
21 | 21 | |
22 | - /** |
|
23 | - * @param string $value_to_normalize |
|
24 | - * @return float |
|
25 | - * @throws \EE_Validation_Error |
|
26 | - */ |
|
27 | - public function normalize($value_to_normalize) |
|
28 | - { |
|
29 | - if ($value_to_normalize === null) { |
|
30 | - return null; |
|
31 | - } |
|
32 | - if (is_float($value_to_normalize) || is_int($value_to_normalize)) { |
|
33 | - return (float) $value_to_normalize; |
|
34 | - } |
|
35 | - if (! is_string($value_to_normalize)) { |
|
36 | - throw new EE_Validation_Error( |
|
37 | - sprintf( |
|
38 | - esc_html__('The value "%s" must be a string submitted for normalization, it was %s', 'event_espresso'), |
|
39 | - print_r($value_to_normalize, true), |
|
40 | - gettype($value_to_normalize) |
|
41 | - ) |
|
42 | - ); |
|
43 | - } |
|
44 | - $normalized_value = filter_var( |
|
45 | - $value_to_normalize, |
|
46 | - FILTER_SANITIZE_NUMBER_FLOAT, |
|
47 | - FILTER_FLAG_ALLOW_FRACTION |
|
48 | - ); |
|
49 | - if ($normalized_value === '') { |
|
50 | - return null; |
|
51 | - } |
|
52 | - if (preg_match(EE_Float_Normalization::REGEX, $normalized_value, $matches)) { |
|
53 | - if (count($matches) === 3) { |
|
54 | - // if first match is the negative sign, |
|
55 | - // then the number needs to be multiplied by -1 to remain negative |
|
56 | - return $matches[1] === '-' |
|
57 | - ? (float) $matches[2] * -1 |
|
58 | - : (float) $matches[2]; |
|
59 | - } |
|
60 | - } |
|
61 | - // find if this input has a float validation strategy |
|
62 | - // in which case, use its message |
|
63 | - $validation_error_message = null; |
|
64 | - foreach ($this->_input->get_validation_strategies() as $validation_strategy) { |
|
65 | - if ($validation_strategy instanceof EE_Float_Validation_Strategy) { |
|
66 | - $validation_error_message = $validation_strategy->get_validation_error_message(); |
|
67 | - } |
|
68 | - } |
|
69 | - // this really shouldn't ever happen because fields with a float normalization strategy |
|
70 | - // should also have a float validation strategy, but in case it doesn't use the default |
|
71 | - if (! $validation_error_message) { |
|
72 | - $default_validation_strategy = new EE_Float_Validation_Strategy(); |
|
73 | - $validation_error_message = $default_validation_strategy->get_validation_error_message(); |
|
74 | - } |
|
75 | - throw new EE_Validation_Error($validation_error_message, 'float_only'); |
|
76 | - } |
|
22 | + /** |
|
23 | + * @param string $value_to_normalize |
|
24 | + * @return float |
|
25 | + * @throws \EE_Validation_Error |
|
26 | + */ |
|
27 | + public function normalize($value_to_normalize) |
|
28 | + { |
|
29 | + if ($value_to_normalize === null) { |
|
30 | + return null; |
|
31 | + } |
|
32 | + if (is_float($value_to_normalize) || is_int($value_to_normalize)) { |
|
33 | + return (float) $value_to_normalize; |
|
34 | + } |
|
35 | + if (! is_string($value_to_normalize)) { |
|
36 | + throw new EE_Validation_Error( |
|
37 | + sprintf( |
|
38 | + esc_html__('The value "%s" must be a string submitted for normalization, it was %s', 'event_espresso'), |
|
39 | + print_r($value_to_normalize, true), |
|
40 | + gettype($value_to_normalize) |
|
41 | + ) |
|
42 | + ); |
|
43 | + } |
|
44 | + $normalized_value = filter_var( |
|
45 | + $value_to_normalize, |
|
46 | + FILTER_SANITIZE_NUMBER_FLOAT, |
|
47 | + FILTER_FLAG_ALLOW_FRACTION |
|
48 | + ); |
|
49 | + if ($normalized_value === '') { |
|
50 | + return null; |
|
51 | + } |
|
52 | + if (preg_match(EE_Float_Normalization::REGEX, $normalized_value, $matches)) { |
|
53 | + if (count($matches) === 3) { |
|
54 | + // if first match is the negative sign, |
|
55 | + // then the number needs to be multiplied by -1 to remain negative |
|
56 | + return $matches[1] === '-' |
|
57 | + ? (float) $matches[2] * -1 |
|
58 | + : (float) $matches[2]; |
|
59 | + } |
|
60 | + } |
|
61 | + // find if this input has a float validation strategy |
|
62 | + // in which case, use its message |
|
63 | + $validation_error_message = null; |
|
64 | + foreach ($this->_input->get_validation_strategies() as $validation_strategy) { |
|
65 | + if ($validation_strategy instanceof EE_Float_Validation_Strategy) { |
|
66 | + $validation_error_message = $validation_strategy->get_validation_error_message(); |
|
67 | + } |
|
68 | + } |
|
69 | + // this really shouldn't ever happen because fields with a float normalization strategy |
|
70 | + // should also have a float validation strategy, but in case it doesn't use the default |
|
71 | + if (! $validation_error_message) { |
|
72 | + $default_validation_strategy = new EE_Float_Validation_Strategy(); |
|
73 | + $validation_error_message = $default_validation_strategy->get_validation_error_message(); |
|
74 | + } |
|
75 | + throw new EE_Validation_Error($validation_error_message, 'float_only'); |
|
76 | + } |
|
77 | 77 | |
78 | 78 | |
79 | 79 | |
80 | - /** |
|
81 | - * Converts a float into a string |
|
82 | - * |
|
83 | - * @param float $normalized_value |
|
84 | - * @return string |
|
85 | - */ |
|
86 | - public function unnormalize($normalized_value) |
|
87 | - { |
|
88 | - if (empty($normalized_value)) { |
|
89 | - return '0.00'; |
|
90 | - } |
|
91 | - return "{$normalized_value}"; |
|
92 | - } |
|
80 | + /** |
|
81 | + * Converts a float into a string |
|
82 | + * |
|
83 | + * @param float $normalized_value |
|
84 | + * @return string |
|
85 | + */ |
|
86 | + public function unnormalize($normalized_value) |
|
87 | + { |
|
88 | + if (empty($normalized_value)) { |
|
89 | + return '0.00'; |
|
90 | + } |
|
91 | + return "{$normalized_value}"; |
|
92 | + } |
|
93 | 93 | } |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | if (is_int($value_to_normalize) || is_float($value_to_normalize)) { |
34 | 34 | return (int) $value_to_normalize; |
35 | 35 | } |
36 | - if (! is_string($value_to_normalize)) { |
|
36 | + if ( ! is_string($value_to_normalize)) { |
|
37 | 37 | throw new EE_Validation_Error( |
38 | 38 | sprintf( |
39 | 39 | esc_html__('The value "%s" must be a string submitted for normalization, it was %s', 'event_espresso'), |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | } |
71 | 71 | // this really shouldn't ever happen because fields with a int normalization strategy |
72 | 72 | // should also have a int validation strategy, but in case it doesn't use the default |
73 | - if (! $validation_error_message) { |
|
73 | + if ( ! $validation_error_message) { |
|
74 | 74 | $default_validation_strategy = new EE_Int_Validation_Strategy(); |
75 | 75 | $validation_error_message = $default_validation_strategy->get_validation_error_message(); |
76 | 76 | } |
@@ -10,88 +10,88 @@ |
||
10 | 10 | */ |
11 | 11 | class EE_Int_Normalization extends EE_Normalization_Strategy_Base |
12 | 12 | { |
13 | - /* |
|
13 | + /* |
|
14 | 14 | * regex pattern that matches for the following: |
15 | 15 | * * optional negative sign |
16 | 16 | * * one or more digits |
17 | 17 | */ |
18 | - const REGEX = '/^(-?)(\d+)(?:\.0+)?$/'; |
|
18 | + const REGEX = '/^(-?)(\d+)(?:\.0+)?$/'; |
|
19 | 19 | |
20 | 20 | |
21 | 21 | |
22 | - /** |
|
23 | - * @param string $value_to_normalize |
|
24 | - * @return int|mixed|string |
|
25 | - * @throws \EE_Validation_Error |
|
26 | - */ |
|
27 | - public function normalize($value_to_normalize) |
|
28 | - { |
|
29 | - if ($value_to_normalize === null) { |
|
30 | - return null; |
|
31 | - } |
|
32 | - if (is_int($value_to_normalize) || is_float($value_to_normalize)) { |
|
33 | - return (int) $value_to_normalize; |
|
34 | - } |
|
35 | - if (! is_string($value_to_normalize)) { |
|
36 | - throw new EE_Validation_Error( |
|
37 | - sprintf( |
|
38 | - esc_html__('The value "%s" must be a string submitted for normalization, it was %s', 'event_espresso'), |
|
39 | - print_r($value_to_normalize, true), |
|
40 | - gettype($value_to_normalize) |
|
41 | - ) |
|
42 | - ); |
|
43 | - } |
|
44 | - $value_to_normalize = filter_var( |
|
45 | - $value_to_normalize, |
|
46 | - FILTER_SANITIZE_NUMBER_FLOAT, |
|
47 | - FILTER_FLAG_ALLOW_FRACTION |
|
48 | - ); |
|
49 | - if ($value_to_normalize === '') { |
|
50 | - return null; |
|
51 | - } |
|
52 | - $matches = array(); |
|
53 | - if (preg_match(EE_Int_Normalization::REGEX, $value_to_normalize, $matches)) { |
|
54 | - if (count($matches) === 3) { |
|
55 | - // if first match is the negative sign, |
|
56 | - // then the number needs to be multiplied by -1 to remain negative |
|
57 | - return $matches[1] === '-' |
|
58 | - ? (int) $matches[2] * -1 |
|
59 | - : (int) $matches[2]; |
|
60 | - } |
|
61 | - } |
|
62 | - // find if this input has a int validation strategy |
|
63 | - // in which case, use its message |
|
64 | - $validation_error_message = null; |
|
65 | - foreach ($this->_input->get_validation_strategies() as $validation_strategy) { |
|
66 | - if ($validation_strategy instanceof EE_Int_Validation_Strategy) { |
|
67 | - $validation_error_message = $validation_strategy->get_validation_error_message(); |
|
68 | - } |
|
69 | - } |
|
70 | - // this really shouldn't ever happen because fields with a int normalization strategy |
|
71 | - // should also have a int validation strategy, but in case it doesn't use the default |
|
72 | - if (! $validation_error_message) { |
|
73 | - $default_validation_strategy = new EE_Int_Validation_Strategy(); |
|
74 | - $validation_error_message = $default_validation_strategy->get_validation_error_message(); |
|
75 | - } |
|
76 | - throw new EE_Validation_Error($validation_error_message, 'numeric_only'); |
|
77 | - } |
|
22 | + /** |
|
23 | + * @param string $value_to_normalize |
|
24 | + * @return int|mixed|string |
|
25 | + * @throws \EE_Validation_Error |
|
26 | + */ |
|
27 | + public function normalize($value_to_normalize) |
|
28 | + { |
|
29 | + if ($value_to_normalize === null) { |
|
30 | + return null; |
|
31 | + } |
|
32 | + if (is_int($value_to_normalize) || is_float($value_to_normalize)) { |
|
33 | + return (int) $value_to_normalize; |
|
34 | + } |
|
35 | + if (! is_string($value_to_normalize)) { |
|
36 | + throw new EE_Validation_Error( |
|
37 | + sprintf( |
|
38 | + esc_html__('The value "%s" must be a string submitted for normalization, it was %s', 'event_espresso'), |
|
39 | + print_r($value_to_normalize, true), |
|
40 | + gettype($value_to_normalize) |
|
41 | + ) |
|
42 | + ); |
|
43 | + } |
|
44 | + $value_to_normalize = filter_var( |
|
45 | + $value_to_normalize, |
|
46 | + FILTER_SANITIZE_NUMBER_FLOAT, |
|
47 | + FILTER_FLAG_ALLOW_FRACTION |
|
48 | + ); |
|
49 | + if ($value_to_normalize === '') { |
|
50 | + return null; |
|
51 | + } |
|
52 | + $matches = array(); |
|
53 | + if (preg_match(EE_Int_Normalization::REGEX, $value_to_normalize, $matches)) { |
|
54 | + if (count($matches) === 3) { |
|
55 | + // if first match is the negative sign, |
|
56 | + // then the number needs to be multiplied by -1 to remain negative |
|
57 | + return $matches[1] === '-' |
|
58 | + ? (int) $matches[2] * -1 |
|
59 | + : (int) $matches[2]; |
|
60 | + } |
|
61 | + } |
|
62 | + // find if this input has a int validation strategy |
|
63 | + // in which case, use its message |
|
64 | + $validation_error_message = null; |
|
65 | + foreach ($this->_input->get_validation_strategies() as $validation_strategy) { |
|
66 | + if ($validation_strategy instanceof EE_Int_Validation_Strategy) { |
|
67 | + $validation_error_message = $validation_strategy->get_validation_error_message(); |
|
68 | + } |
|
69 | + } |
|
70 | + // this really shouldn't ever happen because fields with a int normalization strategy |
|
71 | + // should also have a int validation strategy, but in case it doesn't use the default |
|
72 | + if (! $validation_error_message) { |
|
73 | + $default_validation_strategy = new EE_Int_Validation_Strategy(); |
|
74 | + $validation_error_message = $default_validation_strategy->get_validation_error_message(); |
|
75 | + } |
|
76 | + throw new EE_Validation_Error($validation_error_message, 'numeric_only'); |
|
77 | + } |
|
78 | 78 | |
79 | 79 | |
80 | 80 | |
81 | - /** |
|
82 | - * Converts the int into a string for use in teh html form |
|
83 | - * |
|
84 | - * @param int $normalized_value |
|
85 | - * @return string |
|
86 | - */ |
|
87 | - public function unnormalize($normalized_value) |
|
88 | - { |
|
89 | - if ($normalized_value === null || $normalized_value === '') { |
|
90 | - return ''; |
|
91 | - } |
|
92 | - if (empty($normalized_value)) { |
|
93 | - return '0'; |
|
94 | - } |
|
95 | - return "$normalized_value"; |
|
96 | - } |
|
81 | + /** |
|
82 | + * Converts the int into a string for use in teh html form |
|
83 | + * |
|
84 | + * @param int $normalized_value |
|
85 | + * @return string |
|
86 | + */ |
|
87 | + public function unnormalize($normalized_value) |
|
88 | + { |
|
89 | + if ($normalized_value === null || $normalized_value === '') { |
|
90 | + return ''; |
|
91 | + } |
|
92 | + if (empty($normalized_value)) { |
|
93 | + return '0'; |
|
94 | + } |
|
95 | + return "$normalized_value"; |
|
96 | + } |
|
97 | 97 | } |
@@ -199,7 +199,7 @@ discard block |
||
199 | 199 | if (isset($input_args['validation_strategies'])) { |
200 | 200 | foreach ((array) $input_args['validation_strategies'] as $validation_strategy) { |
201 | 201 | if ($validation_strategy instanceof EE_Validation_Strategy_Base && empty($input_args['ignore_input'])) { |
202 | - $this->_validation_strategies[ get_class($validation_strategy) ] = $validation_strategy; |
|
202 | + $this->_validation_strategies[get_class($validation_strategy)] = $validation_strategy; |
|
203 | 203 | } |
204 | 204 | } |
205 | 205 | unset($input_args['validation_strategies']); |
@@ -210,7 +210,7 @@ discard block |
||
210 | 210 | // loop thru incoming options |
211 | 211 | foreach ($input_args as $key => $value) { |
212 | 212 | // add underscore to $key to match property names |
213 | - $_key = '_' . $key; |
|
213 | + $_key = '_'.$key; |
|
214 | 214 | if (property_exists($this, $_key)) { |
215 | 215 | $this->{$_key} = $value; |
216 | 216 | } |
@@ -230,7 +230,7 @@ discard block |
||
230 | 230 | if (isset($input_args['ignore_input'])) { |
231 | 231 | $this->_normalization_strategy = new EE_Null_Normalization(); |
232 | 232 | } |
233 | - if (! $this->_normalization_strategy) { |
|
233 | + if ( ! $this->_normalization_strategy) { |
|
234 | 234 | $this->_normalization_strategy = new EE_Text_Normalization(); |
235 | 235 | } |
236 | 236 | $this->_normalization_strategy->_construct_finalize($this); |
@@ -239,7 +239,7 @@ discard block |
||
239 | 239 | $this->set_default($input_args['default']); |
240 | 240 | unset($input_args['default']); |
241 | 241 | } |
242 | - if (! $this->_sensitive_data_removal_strategy) { |
|
242 | + if ( ! $this->_sensitive_data_removal_strategy) { |
|
243 | 243 | $this->_sensitive_data_removal_strategy = new EE_No_Sensitive_Data_Removal(); |
244 | 244 | } |
245 | 245 | $this->_sensitive_data_removal_strategy->_construct_finalize($this); |
@@ -256,10 +256,10 @@ discard block |
||
256 | 256 | */ |
257 | 257 | protected function _set_default_html_name_if_empty() |
258 | 258 | { |
259 | - if (! $this->_html_name) { |
|
259 | + if ( ! $this->_html_name) { |
|
260 | 260 | $this->_html_name = $this->name(); |
261 | 261 | if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
262 | - $this->_html_name = $this->_parent_section->html_name_prefix() . "[{$this->name()}]"; |
|
262 | + $this->_html_name = $this->_parent_section->html_name_prefix()."[{$this->name()}]"; |
|
263 | 263 | } |
264 | 264 | } |
265 | 265 | } |
@@ -291,7 +291,7 @@ discard block |
||
291 | 291 | protected function _get_display_strategy() |
292 | 292 | { |
293 | 293 | $this->ensure_construct_finalized_called(); |
294 | - if (! $this->_display_strategy || ! $this->_display_strategy instanceof EE_Display_Strategy_Base) { |
|
294 | + if ( ! $this->_display_strategy || ! $this->_display_strategy instanceof EE_Display_Strategy_Base) { |
|
295 | 295 | throw new EE_Error( |
296 | 296 | sprintf( |
297 | 297 | esc_html__( |
@@ -459,7 +459,7 @@ discard block |
||
459 | 459 | $validation_strategy instanceof $validation_strategy_classname |
460 | 460 | || is_subclass_of($validation_strategy, $validation_strategy_classname) |
461 | 461 | ) { |
462 | - unset($this->_validation_strategies[ $key ]); |
|
462 | + unset($this->_validation_strategies[$key]); |
|
463 | 463 | } |
464 | 464 | } |
465 | 465 | } |
@@ -670,7 +670,7 @@ discard block |
||
670 | 670 | if (is_array($raw_input)) { |
671 | 671 | $raw_value = array(); |
672 | 672 | foreach ($raw_input as $key => $value) { |
673 | - $raw_value[ $key ] = $this->_sanitize($value); |
|
673 | + $raw_value[$key] = $this->_sanitize($value); |
|
674 | 674 | } |
675 | 675 | $this->_set_raw_value($raw_value); |
676 | 676 | } else { |
@@ -703,7 +703,7 @@ discard block |
||
703 | 703 | */ |
704 | 704 | public function html_label_id() |
705 | 705 | { |
706 | - return ! empty($this->_html_label_id) ? $this->_html_label_id : $this->html_id() . '-lbl'; |
|
706 | + return ! empty($this->_html_label_id) ? $this->_html_label_id : $this->html_id().'-lbl'; |
|
707 | 707 | } |
708 | 708 | |
709 | 709 | |
@@ -853,9 +853,9 @@ discard block |
||
853 | 853 | $validation_strategy->get_jquery_validation_rule_array() |
854 | 854 | ); |
855 | 855 | } |
856 | - if (! empty($jquery_validation_rules)) { |
|
856 | + if ( ! empty($jquery_validation_rules)) { |
|
857 | 857 | foreach ($this->get_display_strategy()->get_html_input_ids(true) as $html_id_with_pound_sign) { |
858 | - $jquery_validation_js[ $html_id_with_pound_sign ] = $jquery_validation_rules; |
|
858 | + $jquery_validation_js[$html_id_with_pound_sign] = $jquery_validation_rules; |
|
859 | 859 | } |
860 | 860 | } |
861 | 861 | return $jquery_validation_js; |
@@ -977,7 +977,7 @@ discard block |
||
977 | 977 | public function html_class($add_required = false) |
978 | 978 | { |
979 | 979 | return $add_required && $this->required() |
980 | - ? $this->required_css_class() . ' ' . $this->_html_class |
|
980 | + ? $this->required_css_class().' '.$this->_html_class |
|
981 | 981 | : $this->_html_class; |
982 | 982 | } |
983 | 983 | |
@@ -1052,7 +1052,7 @@ discard block |
||
1052 | 1052 | $button_css_attributes .= ''; |
1053 | 1053 | } |
1054 | 1054 | $this->_button_css_attributes .= ! empty($other_attributes) |
1055 | - ? $button_css_attributes . ' ' . $other_attributes |
|
1055 | + ? $button_css_attributes.' '.$other_attributes |
|
1056 | 1056 | : $button_css_attributes; |
1057 | 1057 | } |
1058 | 1058 | |
@@ -1090,8 +1090,8 @@ discard block |
||
1090 | 1090 | // now get the value for the input |
1091 | 1091 | $value = $this->findRequestForSectionUsingNameParts($name_parts, $req_data); |
1092 | 1092 | // check if this thing's name is at the TOP level of the request data |
1093 | - if ($value === null && isset($req_data[ $this->name() ])) { |
|
1094 | - $value = $req_data[ $this->name() ]; |
|
1093 | + if ($value === null && isset($req_data[$this->name()])) { |
|
1094 | + $value = $req_data[$this->name()]; |
|
1095 | 1095 | } |
1096 | 1096 | return $value; |
1097 | 1097 | } |
@@ -1133,13 +1133,13 @@ discard block |
||
1133 | 1133 | public function findRequestForSectionUsingNameParts($html_name_parts, $req_data) |
1134 | 1134 | { |
1135 | 1135 | $first_part_to_consider = array_shift($html_name_parts); |
1136 | - if (isset($req_data[ $first_part_to_consider ])) { |
|
1136 | + if (isset($req_data[$first_part_to_consider])) { |
|
1137 | 1137 | if (empty($html_name_parts)) { |
1138 | - return $req_data[ $first_part_to_consider ]; |
|
1138 | + return $req_data[$first_part_to_consider]; |
|
1139 | 1139 | } else { |
1140 | 1140 | return $this->findRequestForSectionUsingNameParts( |
1141 | 1141 | $html_name_parts, |
1142 | - $req_data[ $first_part_to_consider ] |
|
1142 | + $req_data[$first_part_to_consider] |
|
1143 | 1143 | ); |
1144 | 1144 | } |
1145 | 1145 | } else { |
@@ -14,1261 +14,1261 @@ |
||
14 | 14 | */ |
15 | 15 | abstract class EE_Form_Input_Base extends EE_Form_Section_Validatable |
16 | 16 | { |
17 | - /** |
|
18 | - * the input's name attribute |
|
19 | - * |
|
20 | - * @var string |
|
21 | - */ |
|
22 | - protected $_html_name; |
|
23 | - |
|
24 | - /** |
|
25 | - * id for the html label tag |
|
26 | - * |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - protected $_html_label_id; |
|
30 | - |
|
31 | - /** |
|
32 | - * class for teh html label tag |
|
33 | - * |
|
34 | - * @var string |
|
35 | - */ |
|
36 | - protected $_html_label_class; |
|
37 | - |
|
38 | - /** |
|
39 | - * style for teh html label tag |
|
40 | - * |
|
41 | - * @var string |
|
42 | - */ |
|
43 | - protected $_html_label_style; |
|
44 | - |
|
45 | - /** |
|
46 | - * text to be placed in the html label |
|
47 | - * |
|
48 | - * @var string |
|
49 | - */ |
|
50 | - protected $_html_label_text; |
|
51 | - |
|
52 | - /** |
|
53 | - * the full html label. If used, all other html_label_* properties are invalid |
|
54 | - * |
|
55 | - * @var string |
|
56 | - */ |
|
57 | - protected $_html_label; |
|
58 | - |
|
59 | - /** |
|
60 | - * HTML to use for help text (normally placed below form input), in a span which normally |
|
61 | - * has a class of 'description' |
|
62 | - * |
|
63 | - * @var string |
|
64 | - */ |
|
65 | - protected $_html_help_text; |
|
66 | - |
|
67 | - /** |
|
68 | - * CSS classes for displaying the help span |
|
69 | - * |
|
70 | - * @var string |
|
71 | - */ |
|
72 | - protected $_html_help_class = 'description'; |
|
73 | - |
|
74 | - /** |
|
75 | - * CSS to put in the style attribute on the help span |
|
76 | - * |
|
77 | - * @var string |
|
78 | - */ |
|
79 | - protected $_html_help_style; |
|
80 | - |
|
81 | - /** |
|
82 | - * Stores whether or not this input's response is required. |
|
83 | - * Because certain styling elements may also want to know that this |
|
84 | - * input is required etc. |
|
85 | - * |
|
86 | - * @var boolean |
|
87 | - */ |
|
88 | - protected $_required; |
|
89 | - |
|
90 | - /** |
|
91 | - * css class added to required inputs |
|
92 | - * |
|
93 | - * @var string |
|
94 | - */ |
|
95 | - protected $_required_css_class = 'ee-required'; |
|
96 | - |
|
97 | - /** |
|
98 | - * css styles applied to button type inputs |
|
99 | - * |
|
100 | - * @var string |
|
101 | - */ |
|
102 | - protected $_button_css_attributes; |
|
103 | - |
|
104 | - /** |
|
105 | - * The raw post data submitted for this |
|
106 | - * Generally unsafe for usage in client code |
|
107 | - * |
|
108 | - * @var mixed string or array |
|
109 | - */ |
|
110 | - protected $_raw_value; |
|
111 | - |
|
112 | - /** |
|
113 | - * Value normalized according to the input's normalization strategy. |
|
114 | - * The normalization strategy dictates whether this is a string, int, float, |
|
115 | - * boolean, or array of any of those. |
|
116 | - * |
|
117 | - * @var mixed |
|
118 | - */ |
|
119 | - protected $_normalized_value; |
|
120 | - |
|
121 | - |
|
122 | - /** |
|
123 | - * Normalized default value either initially set on the input, or provided by calling |
|
124 | - * set_default(). |
|
125 | - * @var mixed |
|
126 | - */ |
|
127 | - protected $_default; |
|
128 | - |
|
129 | - /** |
|
130 | - * Strategy used for displaying this field. |
|
131 | - * Child classes must use _get_display_strategy to access it. |
|
132 | - * |
|
133 | - * @var EE_Display_Strategy_Base |
|
134 | - */ |
|
135 | - private $_display_strategy; |
|
136 | - |
|
137 | - /** |
|
138 | - * Gets all the validation strategies used on this field |
|
139 | - * |
|
140 | - * @var EE_Validation_Strategy_Base[] |
|
141 | - */ |
|
142 | - private $_validation_strategies = array(); |
|
143 | - |
|
144 | - /** |
|
145 | - * The normalization strategy for this field |
|
146 | - * |
|
147 | - * @var EE_Normalization_Strategy_Base |
|
148 | - */ |
|
149 | - private $_normalization_strategy; |
|
150 | - |
|
151 | - /** |
|
152 | - * Strategy for removing sensitive data after we're done with the form input |
|
153 | - * |
|
154 | - * @var EE_Sensitive_Data_Removal_Base |
|
155 | - */ |
|
156 | - protected $_sensitive_data_removal_strategy; |
|
157 | - |
|
158 | - /** |
|
159 | - * Whether this input has been disabled or not. |
|
160 | - * If it's disabled while rendering, an extra hidden input is added that indicates it has been knowingly disabled. |
|
161 | - * (Client-side code that wants to dynamically disable it must also add this hidden input). |
|
162 | - * When the form is submitted, if the input is disabled in the PHP form section, then input is ignored. |
|
163 | - * If the input is missing from the request data but the hidden input indicating the input is disabled, then the input is again ignored. |
|
164 | - * |
|
165 | - * @var boolean |
|
166 | - */ |
|
167 | - protected $disabled = false; |
|
168 | - |
|
169 | - |
|
170 | - |
|
171 | - /** |
|
172 | - * @param array $input_args { |
|
173 | - * @type string $html_name the html name for the input |
|
174 | - * @type string $html_label_id the id attribute to give to the html label tag |
|
175 | - * @type string $html_label_class the class attribute to give to the html label tag |
|
176 | - * @type string $html_label_style the style attribute to give ot teh label tag |
|
177 | - * @type string $html_label_text the text to put in the label tag |
|
178 | - * @type string $html_label the full html label. If used, |
|
179 | - * all other html_label_* args are invalid |
|
180 | - * @type string $html_help_text text to put in help element |
|
181 | - * @type string $html_help_style style attribute to give to teh help element |
|
182 | - * @type string $html_help_class class attribute to give to the help element |
|
183 | - * @type string $default default value NORMALIZED (eg, if providing the default |
|
184 | - * for a Yes_No_Input, you should provide TRUE or FALSE, not '1' or '0') |
|
185 | - * @type EE_Display_Strategy_Base $display strategy |
|
186 | - * @type EE_Normalization_Strategy_Base $normalization_strategy |
|
187 | - * @type EE_Validation_Strategy_Base[] $validation_strategies |
|
188 | - * @type boolean $ignore_input special argument which can be used to avoid adding any validation strategies, |
|
189 | - * and sets the normalization strategy to the Null normalization. This is good |
|
190 | - * when you want the input to be totally ignored server-side (like when using |
|
191 | - * React.js form inputs) |
|
192 | - * } |
|
193 | - */ |
|
194 | - public function __construct($input_args = array()) |
|
195 | - { |
|
196 | - $input_args = (array) apply_filters('FHEE__EE_Form_Input_Base___construct__input_args', $input_args, $this); |
|
197 | - // the following properties must be cast as arrays |
|
198 | - if (isset($input_args['validation_strategies'])) { |
|
199 | - foreach ((array) $input_args['validation_strategies'] as $validation_strategy) { |
|
200 | - if ($validation_strategy instanceof EE_Validation_Strategy_Base && empty($input_args['ignore_input'])) { |
|
201 | - $this->_validation_strategies[ get_class($validation_strategy) ] = $validation_strategy; |
|
202 | - } |
|
203 | - } |
|
204 | - unset($input_args['validation_strategies']); |
|
205 | - } |
|
206 | - if (isset($input_args['ignore_input'])) { |
|
207 | - $this->_validation_strategies = array(); |
|
208 | - } |
|
209 | - // loop thru incoming options |
|
210 | - foreach ($input_args as $key => $value) { |
|
211 | - // add underscore to $key to match property names |
|
212 | - $_key = '_' . $key; |
|
213 | - if (property_exists($this, $_key)) { |
|
214 | - $this->{$_key} = $value; |
|
215 | - } |
|
216 | - } |
|
217 | - // ensure that "required" is set correctly |
|
218 | - $this->set_required( |
|
219 | - $this->_required, |
|
220 | - isset($input_args['required_validation_error_message']) |
|
221 | - ? $input_args['required_validation_error_message'] |
|
222 | - : null |
|
223 | - ); |
|
224 | - // $this->_html_name_specified = isset( $input_args['html_name'] ) ? TRUE : FALSE; |
|
225 | - $this->_display_strategy->_construct_finalize($this); |
|
226 | - foreach ($this->_validation_strategies as $validation_strategy) { |
|
227 | - $validation_strategy->_construct_finalize($this); |
|
228 | - } |
|
229 | - if (isset($input_args['ignore_input'])) { |
|
230 | - $this->_normalization_strategy = new EE_Null_Normalization(); |
|
231 | - } |
|
232 | - if (! $this->_normalization_strategy) { |
|
233 | - $this->_normalization_strategy = new EE_Text_Normalization(); |
|
234 | - } |
|
235 | - $this->_normalization_strategy->_construct_finalize($this); |
|
236 | - // at least we can use the normalization strategy to populate the default |
|
237 | - if (isset($input_args['default'])) { |
|
238 | - $this->set_default($input_args['default']); |
|
239 | - unset($input_args['default']); |
|
240 | - } |
|
241 | - if (! $this->_sensitive_data_removal_strategy) { |
|
242 | - $this->_sensitive_data_removal_strategy = new EE_No_Sensitive_Data_Removal(); |
|
243 | - } |
|
244 | - $this->_sensitive_data_removal_strategy->_construct_finalize($this); |
|
245 | - parent::__construct($input_args); |
|
246 | - } |
|
247 | - |
|
248 | - |
|
249 | - |
|
250 | - /** |
|
251 | - * Sets the html_name to its default value, if none was specified in teh constructor. |
|
252 | - * Calculation involves using the name and the parent's html_name |
|
253 | - * |
|
254 | - * @throws EE_Error |
|
255 | - */ |
|
256 | - protected function _set_default_html_name_if_empty() |
|
257 | - { |
|
258 | - if (! $this->_html_name) { |
|
259 | - $this->_html_name = $this->name(); |
|
260 | - if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
|
261 | - $this->_html_name = $this->_parent_section->html_name_prefix() . "[{$this->name()}]"; |
|
262 | - } |
|
263 | - } |
|
264 | - } |
|
265 | - |
|
266 | - |
|
267 | - |
|
268 | - /** |
|
269 | - * @param $parent_form_section |
|
270 | - * @param $name |
|
271 | - * @throws EE_Error |
|
272 | - */ |
|
273 | - public function _construct_finalize($parent_form_section, $name) |
|
274 | - { |
|
275 | - parent::_construct_finalize($parent_form_section, $name); |
|
276 | - if ($this->_html_label === null && $this->_html_label_text === null) { |
|
277 | - $this->_html_label_text = ucwords(str_replace("_", " ", $name)); |
|
278 | - } |
|
279 | - do_action('AHEE__EE_Form_Input_Base___construct_finalize__end', $this, $parent_form_section, $name); |
|
280 | - } |
|
281 | - |
|
282 | - |
|
283 | - |
|
284 | - /** |
|
285 | - * Returns the strategy for displaying this form input. If none is set, throws an exception. |
|
286 | - * |
|
287 | - * @return EE_Display_Strategy_Base |
|
288 | - * @throws EE_Error |
|
289 | - */ |
|
290 | - protected function _get_display_strategy() |
|
291 | - { |
|
292 | - $this->ensure_construct_finalized_called(); |
|
293 | - if (! $this->_display_strategy || ! $this->_display_strategy instanceof EE_Display_Strategy_Base) { |
|
294 | - throw new EE_Error( |
|
295 | - sprintf( |
|
296 | - esc_html__( |
|
297 | - "Cannot get display strategy for form input with name %s and id %s, because it has not been set in the constructor", |
|
298 | - "event_espresso" |
|
299 | - ), |
|
300 | - $this->html_name(), |
|
301 | - $this->html_id() |
|
302 | - ) |
|
303 | - ); |
|
304 | - } else { |
|
305 | - return $this->_display_strategy; |
|
306 | - } |
|
307 | - } |
|
308 | - |
|
309 | - |
|
310 | - |
|
311 | - /** |
|
312 | - * Sets the display strategy. |
|
313 | - * |
|
314 | - * @param EE_Display_Strategy_Base $strategy |
|
315 | - */ |
|
316 | - protected function _set_display_strategy(EE_Display_Strategy_Base $strategy) |
|
317 | - { |
|
318 | - $this->_display_strategy = $strategy; |
|
319 | - } |
|
320 | - |
|
321 | - |
|
322 | - |
|
323 | - /** |
|
324 | - * Sets the sanitization strategy |
|
325 | - * |
|
326 | - * @param EE_Normalization_Strategy_Base $strategy |
|
327 | - */ |
|
328 | - protected function _set_normalization_strategy(EE_Normalization_Strategy_Base $strategy) |
|
329 | - { |
|
330 | - $this->_normalization_strategy = $strategy; |
|
331 | - } |
|
332 | - |
|
333 | - |
|
334 | - |
|
335 | - /** |
|
336 | - * Gets sensitive_data_removal_strategy |
|
337 | - * |
|
338 | - * @return EE_Sensitive_Data_Removal_Base |
|
339 | - */ |
|
340 | - public function get_sensitive_data_removal_strategy() |
|
341 | - { |
|
342 | - return $this->_sensitive_data_removal_strategy; |
|
343 | - } |
|
344 | - |
|
345 | - |
|
346 | - |
|
347 | - /** |
|
348 | - * Sets sensitive_data_removal_strategy |
|
349 | - * |
|
350 | - * @param EE_Sensitive_Data_Removal_Base $sensitive_data_removal_strategy |
|
351 | - * @return void |
|
352 | - */ |
|
353 | - public function set_sensitive_data_removal_strategy($sensitive_data_removal_strategy) |
|
354 | - { |
|
355 | - $this->_sensitive_data_removal_strategy = $sensitive_data_removal_strategy; |
|
356 | - } |
|
357 | - |
|
358 | - |
|
359 | - |
|
360 | - /** |
|
361 | - * Gets the display strategy for this input |
|
362 | - * |
|
363 | - * @return EE_Display_Strategy_Base |
|
364 | - */ |
|
365 | - public function get_display_strategy() |
|
366 | - { |
|
367 | - return $this->_display_strategy; |
|
368 | - } |
|
369 | - |
|
370 | - |
|
371 | - |
|
372 | - /** |
|
373 | - * Overwrites the display strategy |
|
374 | - * |
|
375 | - * @param EE_Display_Strategy_Base $display_strategy |
|
376 | - */ |
|
377 | - public function set_display_strategy($display_strategy) |
|
378 | - { |
|
379 | - $this->_display_strategy = $display_strategy; |
|
380 | - $this->_display_strategy->_construct_finalize($this); |
|
381 | - } |
|
382 | - |
|
383 | - |
|
384 | - |
|
385 | - /** |
|
386 | - * Gets the normalization strategy set on this input |
|
387 | - * |
|
388 | - * @return EE_Normalization_Strategy_Base |
|
389 | - */ |
|
390 | - public function get_normalization_strategy() |
|
391 | - { |
|
392 | - return $this->_normalization_strategy; |
|
393 | - } |
|
394 | - |
|
395 | - |
|
396 | - |
|
397 | - /** |
|
398 | - * Overwrites the normalization strategy |
|
399 | - * |
|
400 | - * @param EE_Normalization_Strategy_Base $normalization_strategy |
|
401 | - */ |
|
402 | - public function set_normalization_strategy($normalization_strategy) |
|
403 | - { |
|
404 | - $this->_normalization_strategy = $normalization_strategy; |
|
405 | - $this->_normalization_strategy->_construct_finalize($this); |
|
406 | - } |
|
407 | - |
|
408 | - |
|
409 | - |
|
410 | - /** |
|
411 | - * Returns all teh validation strategies which apply to this field, numerically indexed |
|
412 | - * |
|
413 | - * @return EE_Validation_Strategy_Base[] |
|
414 | - */ |
|
415 | - public function get_validation_strategies() |
|
416 | - { |
|
417 | - return $this->_validation_strategies; |
|
418 | - } |
|
419 | - |
|
420 | - |
|
421 | - |
|
422 | - /** |
|
423 | - * Adds this strategy to the field so it will be used in both JS validation and server-side validation |
|
424 | - * |
|
425 | - * @param EE_Validation_Strategy_Base $validation_strategy |
|
426 | - * @return void |
|
427 | - */ |
|
428 | - protected function _add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy) |
|
429 | - { |
|
430 | - $validation_strategy->_construct_finalize($this); |
|
431 | - $this->_validation_strategies[] = $validation_strategy; |
|
432 | - } |
|
433 | - |
|
434 | - |
|
435 | - |
|
436 | - /** |
|
437 | - * Adds a new validation strategy onto the form input |
|
438 | - * |
|
439 | - * @param EE_Validation_Strategy_Base $validation_strategy |
|
440 | - * @return void |
|
441 | - */ |
|
442 | - public function add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy) |
|
443 | - { |
|
444 | - $this->_add_validation_strategy($validation_strategy); |
|
445 | - } |
|
446 | - |
|
447 | - |
|
448 | - |
|
449 | - /** |
|
450 | - * The classname of the validation strategy to remove |
|
451 | - * |
|
452 | - * @param string $validation_strategy_classname |
|
453 | - */ |
|
454 | - public function remove_validation_strategy($validation_strategy_classname) |
|
455 | - { |
|
456 | - foreach ($this->_validation_strategies as $key => $validation_strategy) { |
|
457 | - if ( |
|
458 | - $validation_strategy instanceof $validation_strategy_classname |
|
459 | - || is_subclass_of($validation_strategy, $validation_strategy_classname) |
|
460 | - ) { |
|
461 | - unset($this->_validation_strategies[ $key ]); |
|
462 | - } |
|
463 | - } |
|
464 | - } |
|
465 | - |
|
466 | - |
|
467 | - |
|
468 | - /** |
|
469 | - * returns true if input employs any of the validation strategy defined by the supplied array of classnames |
|
470 | - * |
|
471 | - * @param array $validation_strategy_classnames |
|
472 | - * @return bool |
|
473 | - */ |
|
474 | - public function has_validation_strategy($validation_strategy_classnames) |
|
475 | - { |
|
476 | - $validation_strategy_classnames = is_array($validation_strategy_classnames) |
|
477 | - ? $validation_strategy_classnames |
|
478 | - : array($validation_strategy_classnames); |
|
479 | - foreach ($this->_validation_strategies as $key => $validation_strategy) { |
|
480 | - if (in_array($key, $validation_strategy_classnames)) { |
|
481 | - return true; |
|
482 | - } |
|
483 | - } |
|
484 | - return false; |
|
485 | - } |
|
486 | - |
|
487 | - |
|
488 | - |
|
489 | - /** |
|
490 | - * Gets the HTML |
|
491 | - * |
|
492 | - * @return string |
|
493 | - */ |
|
494 | - public function get_html() |
|
495 | - { |
|
496 | - return $this->_parent_section->get_html_for_input($this); |
|
497 | - } |
|
498 | - |
|
499 | - |
|
500 | - |
|
501 | - /** |
|
502 | - * Gets the HTML for the input itself (no label or errors) according to the |
|
503 | - * input's display strategy |
|
504 | - * Makes sure the JS and CSS are enqueued for it |
|
505 | - * |
|
506 | - * @return string |
|
507 | - * @throws EE_Error |
|
508 | - */ |
|
509 | - public function get_html_for_input() |
|
510 | - { |
|
511 | - return $this->_form_html_filter |
|
512 | - ? $this->_form_html_filter->filterHtml( |
|
513 | - $this->_get_display_strategy()->display(), |
|
514 | - $this |
|
515 | - ) |
|
516 | - : $this->_get_display_strategy()->display(); |
|
517 | - } |
|
518 | - |
|
519 | - |
|
520 | - |
|
521 | - /** |
|
522 | - * @return string |
|
523 | - */ |
|
524 | - public function html_other_attributes() |
|
525 | - { |
|
526 | - EE_Error::doing_it_wrong( |
|
527 | - __METHOD__, |
|
528 | - sprintf( |
|
529 | - esc_html__( |
|
530 | - 'This method is no longer in use. You should replace it by %s', |
|
531 | - 'event_espresso' |
|
532 | - ), |
|
533 | - 'EE_Form_Section_Base::other_html_attributes()' |
|
534 | - ), |
|
535 | - '4.10.2.p' |
|
536 | - ); |
|
537 | - |
|
538 | - return $this->other_html_attributes(); |
|
539 | - } |
|
540 | - |
|
541 | - |
|
542 | - |
|
543 | - /** |
|
544 | - * @param string $html_other_attributes |
|
545 | - */ |
|
546 | - public function set_html_other_attributes($html_other_attributes) |
|
547 | - { |
|
548 | - EE_Error::doing_it_wrong( |
|
549 | - __METHOD__, |
|
550 | - sprintf( |
|
551 | - esc_html__( |
|
552 | - 'This method is no longer in use. You should replace it by %s', |
|
553 | - 'event_espresso' |
|
554 | - ), |
|
555 | - 'EE_Form_Section_Base::set_other_html_attributes()' |
|
556 | - ), |
|
557 | - '4.10.2.p' |
|
558 | - ); |
|
559 | - |
|
560 | - $this->set_other_html_attributes($html_other_attributes); |
|
561 | - } |
|
562 | - |
|
563 | - |
|
564 | - |
|
565 | - /** |
|
566 | - * Gets the HTML for displaying the label for this form input |
|
567 | - * according to the form section's layout strategy |
|
568 | - * |
|
569 | - * @return string |
|
570 | - */ |
|
571 | - public function get_html_for_label() |
|
572 | - { |
|
573 | - return $this->_parent_section->get_layout_strategy()->display_label($this); |
|
574 | - } |
|
575 | - |
|
576 | - |
|
577 | - |
|
578 | - /** |
|
579 | - * Gets the HTML for displaying the errors section for this form input |
|
580 | - * according to the form section's layout strategy |
|
581 | - * |
|
582 | - * @return string |
|
583 | - */ |
|
584 | - public function get_html_for_errors() |
|
585 | - { |
|
586 | - return $this->_parent_section->get_layout_strategy()->display_errors($this); |
|
587 | - } |
|
588 | - |
|
589 | - |
|
590 | - |
|
591 | - /** |
|
592 | - * Gets the HTML for displaying the help text for this form input |
|
593 | - * according to the form section's layout strategy |
|
594 | - * |
|
595 | - * @return string |
|
596 | - */ |
|
597 | - public function get_html_for_help() |
|
598 | - { |
|
599 | - return $this->_parent_section->get_layout_strategy()->display_help_text($this); |
|
600 | - } |
|
601 | - |
|
602 | - |
|
603 | - |
|
604 | - /** |
|
605 | - * Validates the input's sanitized value (assumes _sanitize() has already been called) |
|
606 | - * and returns whether or not the form input's submitted value is value |
|
607 | - * |
|
608 | - * @return boolean |
|
609 | - */ |
|
610 | - protected function _validate() |
|
611 | - { |
|
612 | - if ($this->isDisabled()) { |
|
613 | - return true; |
|
614 | - } |
|
615 | - foreach ($this->_validation_strategies as $validation_strategy) { |
|
616 | - if ($validation_strategy instanceof EE_Validation_Strategy_Base) { |
|
617 | - try { |
|
618 | - $validation_strategy->validate($this->normalized_value()); |
|
619 | - } catch (EE_Validation_Error $e) { |
|
620 | - $this->add_validation_error($e); |
|
621 | - } |
|
622 | - } |
|
623 | - } |
|
624 | - if ($this->get_validation_errors()) { |
|
625 | - return false; |
|
626 | - } else { |
|
627 | - return true; |
|
628 | - } |
|
629 | - } |
|
630 | - |
|
631 | - |
|
632 | - |
|
633 | - /** |
|
634 | - * Performs basic sanitization on this value. But what sanitization can be performed anyways? |
|
635 | - * This value MIGHT be allowed to have tags, so we can't really remove them. |
|
636 | - * |
|
637 | - * @param string $value |
|
638 | - * @return null|string |
|
639 | - */ |
|
640 | - protected function _sanitize($value) |
|
641 | - { |
|
642 | - return $value !== null ? stripslashes(html_entity_decode(trim($value))) : null; |
|
643 | - } |
|
644 | - |
|
645 | - |
|
646 | - |
|
647 | - /** |
|
648 | - * Picks out the form value that relates to this form input, |
|
649 | - * and stores it as the sanitized value on the form input, and sets the normalized value. |
|
650 | - * Returns whether or not any validation errors occurred |
|
651 | - * |
|
652 | - * @param array $req_data |
|
653 | - * @return boolean whether or not there was an error |
|
654 | - * @throws EE_Error |
|
655 | - */ |
|
656 | - protected function _normalize($req_data) |
|
657 | - { |
|
658 | - // any existing validation errors don't apply so clear them |
|
659 | - $this->_validation_errors = array(); |
|
660 | - // if the input is disabled, ignore whatever input was sent in |
|
661 | - if ($this->isDisabled()) { |
|
662 | - $this->_set_raw_value(null); |
|
663 | - $this->_set_normalized_value($this->get_default()); |
|
664 | - return false; |
|
665 | - } |
|
666 | - try { |
|
667 | - $raw_input = $this->find_form_data_for_this_section($req_data); |
|
668 | - // super simple sanitization for now |
|
669 | - if (is_array($raw_input)) { |
|
670 | - $raw_value = array(); |
|
671 | - foreach ($raw_input as $key => $value) { |
|
672 | - $raw_value[ $key ] = $this->_sanitize($value); |
|
673 | - } |
|
674 | - $this->_set_raw_value($raw_value); |
|
675 | - } else { |
|
676 | - $this->_set_raw_value($this->_sanitize($raw_input)); |
|
677 | - } |
|
678 | - // we want to mostly leave the input alone in case we need to re-display it to the user |
|
679 | - $this->_set_normalized_value($this->_normalization_strategy->normalize($this->raw_value())); |
|
680 | - return false; |
|
681 | - } catch (EE_Validation_Error $e) { |
|
682 | - $this->add_validation_error($e); |
|
683 | - return true; |
|
684 | - } |
|
685 | - } |
|
686 | - |
|
687 | - |
|
688 | - /** |
|
689 | - * @return string |
|
690 | - * @throws EE_Error |
|
691 | - */ |
|
692 | - public function html_name() |
|
693 | - { |
|
694 | - $this->_set_default_html_name_if_empty(); |
|
695 | - return $this->_html_name; |
|
696 | - } |
|
697 | - |
|
698 | - |
|
699 | - /** |
|
700 | - * @return string |
|
701 | - * @throws EE_Error |
|
702 | - */ |
|
703 | - public function html_label_id() |
|
704 | - { |
|
705 | - return ! empty($this->_html_label_id) ? $this->_html_label_id : $this->html_id() . '-lbl'; |
|
706 | - } |
|
707 | - |
|
708 | - |
|
709 | - |
|
710 | - /** |
|
711 | - * @return string |
|
712 | - */ |
|
713 | - public function html_label_class() |
|
714 | - { |
|
715 | - return $this->_html_label_class; |
|
716 | - } |
|
717 | - |
|
718 | - |
|
719 | - |
|
720 | - /** |
|
721 | - * @return string |
|
722 | - */ |
|
723 | - public function html_label_style() |
|
724 | - { |
|
725 | - return $this->_html_label_style; |
|
726 | - } |
|
727 | - |
|
728 | - |
|
729 | - |
|
730 | - /** |
|
731 | - * @return string |
|
732 | - */ |
|
733 | - public function html_label_text() |
|
734 | - { |
|
735 | - return $this->_html_label_text; |
|
736 | - } |
|
737 | - |
|
738 | - |
|
739 | - |
|
740 | - /** |
|
741 | - * @return string |
|
742 | - */ |
|
743 | - public function html_help_text() |
|
744 | - { |
|
745 | - return $this->_html_help_text; |
|
746 | - } |
|
747 | - |
|
748 | - |
|
749 | - |
|
750 | - /** |
|
751 | - * @return string |
|
752 | - */ |
|
753 | - public function html_help_class() |
|
754 | - { |
|
755 | - return $this->_html_help_class; |
|
756 | - } |
|
757 | - |
|
758 | - |
|
759 | - |
|
760 | - /** |
|
761 | - * @return string |
|
762 | - */ |
|
763 | - public function html_help_style() |
|
764 | - { |
|
765 | - return $this->_html_style; |
|
766 | - } |
|
767 | - |
|
768 | - |
|
769 | - |
|
770 | - /** |
|
771 | - * returns the raw, UNSAFE, input, almost exactly as the user submitted it. |
|
772 | - * Please note that almost all client code should instead use the normalized_value; |
|
773 | - * or possibly raw_value_in_form (which prepares the string for displaying in an HTML attribute on a tag, |
|
774 | - * mostly by escaping quotes) |
|
775 | - * Note, we do not store the exact original value sent in the user's request because |
|
776 | - * it may have malicious content, and we MIGHT want to store the form input in a transient or something... |
|
777 | - * in which case, we would have stored the malicious content to our database. |
|
778 | - * |
|
779 | - * @return string |
|
780 | - */ |
|
781 | - public function raw_value() |
|
782 | - { |
|
783 | - return $this->_raw_value; |
|
784 | - } |
|
785 | - |
|
786 | - |
|
787 | - |
|
788 | - /** |
|
789 | - * Returns a string safe to usage in form inputs when displaying, because |
|
790 | - * it escapes all html entities |
|
791 | - * |
|
792 | - * @return string |
|
793 | - */ |
|
794 | - public function raw_value_in_form() |
|
795 | - { |
|
796 | - return htmlentities($this->raw_value(), ENT_QUOTES, 'UTF-8'); |
|
797 | - } |
|
798 | - |
|
799 | - |
|
800 | - |
|
801 | - /** |
|
802 | - * returns the value after it's been sanitized, and then converted into it's proper type |
|
803 | - * in PHP. Eg, a string, an int, an array, |
|
804 | - * |
|
805 | - * @return mixed |
|
806 | - */ |
|
807 | - public function normalized_value() |
|
808 | - { |
|
809 | - return $this->_normalized_value; |
|
810 | - } |
|
811 | - |
|
812 | - |
|
813 | - |
|
814 | - /** |
|
815 | - * Returns the normalized value is a presentable way. By default this is just |
|
816 | - * the normalized value by itself, but it can be overridden for when that's not |
|
817 | - * the best thing to display |
|
818 | - * |
|
819 | - * @return string |
|
820 | - */ |
|
821 | - public function pretty_value() |
|
822 | - { |
|
823 | - return $this->_normalized_value; |
|
824 | - } |
|
825 | - |
|
826 | - |
|
827 | - |
|
828 | - /** |
|
829 | - * When generating the JS for the jquery validation rules like<br> |
|
830 | - * <code>$( "#myform" ).validate({ |
|
831 | - * rules: { |
|
832 | - * password: "required", |
|
833 | - * password_again: { |
|
834 | - * equalTo: "#password" |
|
835 | - * } |
|
836 | - * } |
|
837 | - * });</code> |
|
838 | - * if this field had the name 'password_again', it should return |
|
839 | - * <br><code>password_again: { |
|
840 | - * equalTo: "#password" |
|
841 | - * }</code> |
|
842 | - * |
|
843 | - * @return array |
|
844 | - */ |
|
845 | - public function get_jquery_validation_rules() |
|
846 | - { |
|
847 | - $jquery_validation_js = array(); |
|
848 | - $jquery_validation_rules = array(); |
|
849 | - foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
850 | - $jquery_validation_rules = array_replace_recursive( |
|
851 | - $jquery_validation_rules, |
|
852 | - $validation_strategy->get_jquery_validation_rule_array() |
|
853 | - ); |
|
854 | - } |
|
855 | - if (! empty($jquery_validation_rules)) { |
|
856 | - foreach ($this->get_display_strategy()->get_html_input_ids(true) as $html_id_with_pound_sign) { |
|
857 | - $jquery_validation_js[ $html_id_with_pound_sign ] = $jquery_validation_rules; |
|
858 | - } |
|
859 | - } |
|
860 | - return $jquery_validation_js; |
|
861 | - } |
|
862 | - |
|
863 | - |
|
864 | - |
|
865 | - /** |
|
866 | - * Sets the input's default value for use in displaying in the form. Note: value should be |
|
867 | - * normalized (Eg, if providing a default of ra Yes_NO_Input you would provide TRUE or FALSE, not '1' or '0') |
|
868 | - * |
|
869 | - * @param mixed $value |
|
870 | - * @return void |
|
871 | - */ |
|
872 | - public function set_default($value) |
|
873 | - { |
|
874 | - $this->_default = $value; |
|
875 | - $this->_set_normalized_value($value); |
|
876 | - $this->_set_raw_value($value); |
|
877 | - } |
|
878 | - |
|
879 | - |
|
880 | - |
|
881 | - /** |
|
882 | - * Sets the normalized value on this input |
|
883 | - * |
|
884 | - * @param mixed $value |
|
885 | - */ |
|
886 | - protected function _set_normalized_value($value) |
|
887 | - { |
|
888 | - $this->_normalized_value = $value; |
|
889 | - } |
|
890 | - |
|
891 | - |
|
892 | - |
|
893 | - /** |
|
894 | - * Sets the raw value on this input (ie, exactly as the user submitted it) |
|
895 | - * |
|
896 | - * @param mixed $value |
|
897 | - */ |
|
898 | - protected function _set_raw_value($value) |
|
899 | - { |
|
900 | - $this->_raw_value = $this->_normalization_strategy->unnormalize($value); |
|
901 | - } |
|
902 | - |
|
903 | - |
|
904 | - |
|
905 | - /** |
|
906 | - * Sets the HTML label text after it has already been defined |
|
907 | - * |
|
908 | - * @param string $label |
|
909 | - * @return void |
|
910 | - */ |
|
911 | - public function set_html_label_text($label) |
|
912 | - { |
|
913 | - $this->_html_label_text = $label; |
|
914 | - } |
|
915 | - |
|
916 | - |
|
917 | - |
|
918 | - /** |
|
919 | - * Sets whether or not this field is required, and adjusts the validation strategy. |
|
920 | - * If you want to use the EE_Conditionally_Required_Validation_Strategy, |
|
921 | - * please add it as a validation strategy using add_validation_strategy as normal |
|
922 | - * |
|
923 | - * @param boolean $required boolean |
|
924 | - * @param null $required_text |
|
925 | - */ |
|
926 | - public function set_required($required = true, $required_text = null) |
|
927 | - { |
|
928 | - $required = filter_var($required, FILTER_VALIDATE_BOOLEAN); |
|
929 | - // whether $required is a string or a boolean, we want to add a required validation strategy |
|
930 | - if ($required) { |
|
931 | - $this->_add_validation_strategy(new EE_Required_Validation_Strategy($required_text)); |
|
932 | - } else { |
|
933 | - $this->remove_validation_strategy('EE_Required_Validation_Strategy'); |
|
934 | - } |
|
935 | - $this->_required = $required; |
|
936 | - } |
|
937 | - |
|
938 | - |
|
939 | - |
|
940 | - /** |
|
941 | - * Returns whether or not this field is required |
|
942 | - * |
|
943 | - * @return boolean |
|
944 | - */ |
|
945 | - public function required() |
|
946 | - { |
|
947 | - return $this->_required; |
|
948 | - } |
|
949 | - |
|
950 | - |
|
951 | - |
|
952 | - /** |
|
953 | - * @param string $required_css_class |
|
954 | - */ |
|
955 | - public function set_required_css_class($required_css_class) |
|
956 | - { |
|
957 | - $this->_required_css_class = $required_css_class; |
|
958 | - } |
|
959 | - |
|
960 | - |
|
961 | - |
|
962 | - /** |
|
963 | - * @return string |
|
964 | - */ |
|
965 | - public function required_css_class() |
|
966 | - { |
|
967 | - return $this->_required_css_class; |
|
968 | - } |
|
969 | - |
|
970 | - |
|
971 | - |
|
972 | - /** |
|
973 | - * @param bool $add_required |
|
974 | - * @return string |
|
975 | - */ |
|
976 | - public function html_class($add_required = false) |
|
977 | - { |
|
978 | - return $add_required && $this->required() |
|
979 | - ? $this->required_css_class() . ' ' . $this->_html_class |
|
980 | - : $this->_html_class; |
|
981 | - } |
|
982 | - |
|
983 | - |
|
984 | - /** |
|
985 | - * Sets the help text, in case |
|
986 | - * |
|
987 | - * @param string $text |
|
988 | - */ |
|
989 | - public function set_html_help_text($text) |
|
990 | - { |
|
991 | - $this->_html_help_text = $text; |
|
992 | - } |
|
993 | - |
|
994 | - |
|
995 | - |
|
996 | - /** |
|
997 | - * Uses the sensitive data removal strategy to remove the sensitive data from this |
|
998 | - * input. If there is any kind of sensitive data removal on this input, we clear |
|
999 | - * out the raw value completely |
|
1000 | - * |
|
1001 | - * @return void |
|
1002 | - */ |
|
1003 | - public function clean_sensitive_data() |
|
1004 | - { |
|
1005 | - // if we do ANY kind of sensitive data removal on this, then just clear out the raw value |
|
1006 | - // if we need more logic than this we'll make a strategy for it |
|
1007 | - if ( |
|
1008 | - $this->_sensitive_data_removal_strategy |
|
1009 | - && ! $this->_sensitive_data_removal_strategy instanceof EE_No_Sensitive_Data_Removal |
|
1010 | - ) { |
|
1011 | - $this->_set_raw_value(null); |
|
1012 | - } |
|
1013 | - // and clean the normalized value according to the appropriate strategy |
|
1014 | - $this->_set_normalized_value( |
|
1015 | - $this->get_sensitive_data_removal_strategy()->remove_sensitive_data( |
|
1016 | - $this->_normalized_value |
|
1017 | - ) |
|
1018 | - ); |
|
1019 | - } |
|
1020 | - |
|
1021 | - |
|
1022 | - |
|
1023 | - /** |
|
1024 | - * @param bool $primary |
|
1025 | - * @param string $button_size |
|
1026 | - * @param string $other_attributes |
|
1027 | - */ |
|
1028 | - public function set_button_css_attributes($primary = true, $button_size = '', $other_attributes = '') |
|
1029 | - { |
|
1030 | - $button_css_attributes = 'button'; |
|
1031 | - $button_css_attributes .= $primary === true ? ' button-primary' : ' button-secondary'; |
|
1032 | - switch ($button_size) { |
|
1033 | - case 'xs': |
|
1034 | - case 'extra-small': |
|
1035 | - $button_css_attributes .= ' button-xs'; |
|
1036 | - break; |
|
1037 | - case 'sm': |
|
1038 | - case 'small': |
|
1039 | - $button_css_attributes .= ' button-sm'; |
|
1040 | - break; |
|
1041 | - case 'lg': |
|
1042 | - case 'large': |
|
1043 | - $button_css_attributes .= ' button-lg'; |
|
1044 | - break; |
|
1045 | - case 'block': |
|
1046 | - $button_css_attributes .= ' button-block'; |
|
1047 | - break; |
|
1048 | - case 'md': |
|
1049 | - case 'medium': |
|
1050 | - default: |
|
1051 | - $button_css_attributes .= ''; |
|
1052 | - } |
|
1053 | - $this->_button_css_attributes .= ! empty($other_attributes) |
|
1054 | - ? $button_css_attributes . ' ' . $other_attributes |
|
1055 | - : $button_css_attributes; |
|
1056 | - } |
|
1057 | - |
|
1058 | - |
|
1059 | - |
|
1060 | - /** |
|
1061 | - * @return string |
|
1062 | - */ |
|
1063 | - public function button_css_attributes() |
|
1064 | - { |
|
1065 | - if (empty($this->_button_css_attributes)) { |
|
1066 | - $this->set_button_css_attributes(); |
|
1067 | - } |
|
1068 | - return $this->_button_css_attributes; |
|
1069 | - } |
|
1070 | - |
|
1071 | - |
|
1072 | - |
|
1073 | - /** |
|
1074 | - * find_form_data_for_this_section |
|
1075 | - * using this section's name and its parents, finds the value of the form data that corresponds to it. |
|
1076 | - * For example, if this form section's HTML name is my_form[subform][form_input_1], |
|
1077 | - * then it's value should be in request at request['my_form']['subform']['form_input_1']. |
|
1078 | - * (If that doesn't exist, we also check for this subsection's name |
|
1079 | - * at the TOP LEVEL of the request data. Eg request['form_input_1'].) |
|
1080 | - * This function finds its value in the form. |
|
1081 | - * |
|
1082 | - * @param array $req_data |
|
1083 | - * @return mixed whatever the raw value of this form section is in the request data |
|
1084 | - * @throws EE_Error |
|
1085 | - */ |
|
1086 | - public function find_form_data_for_this_section($req_data) |
|
1087 | - { |
|
1088 | - $name_parts = $this->getInputNameParts(); |
|
1089 | - // now get the value for the input |
|
1090 | - $value = $this->findRequestForSectionUsingNameParts($name_parts, $req_data); |
|
1091 | - // check if this thing's name is at the TOP level of the request data |
|
1092 | - if ($value === null && isset($req_data[ $this->name() ])) { |
|
1093 | - $value = $req_data[ $this->name() ]; |
|
1094 | - } |
|
1095 | - return $value; |
|
1096 | - } |
|
1097 | - |
|
1098 | - |
|
1099 | - /** |
|
1100 | - * If this input's name is something like "foo[bar][baz]" |
|
1101 | - * returns an array like `array('foo','bar',baz')` |
|
1102 | - * |
|
1103 | - * @return array |
|
1104 | - * @throws EE_Error |
|
1105 | - */ |
|
1106 | - protected function getInputNameParts() |
|
1107 | - { |
|
1108 | - // break up the html name by "[]" |
|
1109 | - if (strpos($this->html_name(), '[') !== false) { |
|
1110 | - $before_any_brackets = substr($this->html_name(), 0, strpos($this->html_name(), '[')); |
|
1111 | - } else { |
|
1112 | - $before_any_brackets = $this->html_name(); |
|
1113 | - } |
|
1114 | - // grab all of the segments |
|
1115 | - preg_match_all('~\[([^]]*)\]~', $this->html_name(), $matches); |
|
1116 | - if (isset($matches[1]) && is_array($matches[1])) { |
|
1117 | - $name_parts = $matches[1]; |
|
1118 | - array_unshift($name_parts, $before_any_brackets); |
|
1119 | - } else { |
|
1120 | - $name_parts = array($before_any_brackets); |
|
1121 | - } |
|
1122 | - return $name_parts; |
|
1123 | - } |
|
1124 | - |
|
1125 | - |
|
1126 | - |
|
1127 | - /** |
|
1128 | - * @param array $html_name_parts |
|
1129 | - * @param array $req_data |
|
1130 | - * @return array | NULL |
|
1131 | - */ |
|
1132 | - public function findRequestForSectionUsingNameParts($html_name_parts, $req_data) |
|
1133 | - { |
|
1134 | - $first_part_to_consider = array_shift($html_name_parts); |
|
1135 | - if (isset($req_data[ $first_part_to_consider ])) { |
|
1136 | - if (empty($html_name_parts)) { |
|
1137 | - return $req_data[ $first_part_to_consider ]; |
|
1138 | - } else { |
|
1139 | - return $this->findRequestForSectionUsingNameParts( |
|
1140 | - $html_name_parts, |
|
1141 | - $req_data[ $first_part_to_consider ] |
|
1142 | - ); |
|
1143 | - } |
|
1144 | - } else { |
|
1145 | - return null; |
|
1146 | - } |
|
1147 | - } |
|
1148 | - |
|
1149 | - |
|
1150 | - |
|
1151 | - /** |
|
1152 | - * Checks if this form input's data is in the request data |
|
1153 | - * |
|
1154 | - * @param array $req_data |
|
1155 | - * @return boolean |
|
1156 | - * @throws EE_Error |
|
1157 | - */ |
|
1158 | - public function form_data_present_in($req_data = null) |
|
1159 | - { |
|
1160 | - if ($req_data === null) { |
|
1161 | - /** @var RequestInterface $request */ |
|
1162 | - $request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
1163 | - $req_data = $request->postParams(); |
|
1164 | - } |
|
1165 | - $checked_value = $this->find_form_data_for_this_section($req_data); |
|
1166 | - if ($checked_value !== null) { |
|
1167 | - return true; |
|
1168 | - } else { |
|
1169 | - return false; |
|
1170 | - } |
|
1171 | - } |
|
1172 | - |
|
1173 | - |
|
1174 | - |
|
1175 | - /** |
|
1176 | - * Overrides parent to add js data from validation and display strategies |
|
1177 | - * |
|
1178 | - * @param array $form_other_js_data |
|
1179 | - * @return array |
|
1180 | - */ |
|
1181 | - public function get_other_js_data($form_other_js_data = array()) |
|
1182 | - { |
|
1183 | - return $this->get_other_js_data_from_strategies($form_other_js_data); |
|
1184 | - } |
|
1185 | - |
|
1186 | - |
|
1187 | - |
|
1188 | - /** |
|
1189 | - * Gets other JS data for localization from this input's strategies, like |
|
1190 | - * the validation strategies and the display strategy |
|
1191 | - * |
|
1192 | - * @param array $form_other_js_data |
|
1193 | - * @return array |
|
1194 | - */ |
|
1195 | - public function get_other_js_data_from_strategies($form_other_js_data = array()) |
|
1196 | - { |
|
1197 | - $form_other_js_data = $this->get_display_strategy()->get_other_js_data($form_other_js_data); |
|
1198 | - foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
1199 | - $form_other_js_data = $validation_strategy->get_other_js_data($form_other_js_data); |
|
1200 | - } |
|
1201 | - return $form_other_js_data; |
|
1202 | - } |
|
1203 | - |
|
1204 | - |
|
1205 | - |
|
1206 | - /** |
|
1207 | - * Override parent because we want to give our strategies an opportunity to enqueue some js and css |
|
1208 | - * |
|
1209 | - * @return void |
|
1210 | - */ |
|
1211 | - public function enqueue_js() |
|
1212 | - { |
|
1213 | - // ask our display strategy and validation strategies if they have js to enqueue |
|
1214 | - $this->enqueue_js_from_strategies(); |
|
1215 | - } |
|
1216 | - |
|
1217 | - |
|
1218 | - |
|
1219 | - /** |
|
1220 | - * Tells strategies when its ok to enqueue their js and css |
|
1221 | - * |
|
1222 | - * @return void |
|
1223 | - */ |
|
1224 | - public function enqueue_js_from_strategies() |
|
1225 | - { |
|
1226 | - $this->get_display_strategy()->enqueue_js(); |
|
1227 | - foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
1228 | - $validation_strategy->enqueue_js(); |
|
1229 | - } |
|
1230 | - } |
|
1231 | - |
|
1232 | - |
|
1233 | - |
|
1234 | - /** |
|
1235 | - * Gets the default value set on the input (not the current value, which may have been |
|
1236 | - * changed because of a form submission). If no default was set, this us null. |
|
1237 | - * @return mixed |
|
1238 | - */ |
|
1239 | - public function get_default() |
|
1240 | - { |
|
1241 | - return $this->_default; |
|
1242 | - } |
|
1243 | - |
|
1244 | - |
|
1245 | - |
|
1246 | - /** |
|
1247 | - * Makes this input disabled. That means it will have the HTML attribute 'disabled="disabled"', |
|
1248 | - * and server-side if any input was received it will be ignored |
|
1249 | - */ |
|
1250 | - public function disable($disable = true) |
|
1251 | - { |
|
1252 | - $disabled_attribute = ' disabled="disabled"'; |
|
1253 | - $this->disabled = filter_var($disable, FILTER_VALIDATE_BOOLEAN); |
|
1254 | - if ($this->disabled) { |
|
1255 | - if (strpos($this->_other_html_attributes, $disabled_attribute) === false) { |
|
1256 | - $this->_other_html_attributes .= $disabled_attribute; |
|
1257 | - } |
|
1258 | - $this->_set_normalized_value($this->get_default()); |
|
1259 | - } else { |
|
1260 | - $this->_other_html_attributes = str_replace($disabled_attribute, '', $this->_other_html_attributes); |
|
1261 | - } |
|
1262 | - } |
|
1263 | - |
|
1264 | - |
|
1265 | - |
|
1266 | - /** |
|
1267 | - * Returns whether or not this input is currently disabled. |
|
1268 | - * @return bool |
|
1269 | - */ |
|
1270 | - public function isDisabled() |
|
1271 | - { |
|
1272 | - return $this->disabled; |
|
1273 | - } |
|
17 | + /** |
|
18 | + * the input's name attribute |
|
19 | + * |
|
20 | + * @var string |
|
21 | + */ |
|
22 | + protected $_html_name; |
|
23 | + |
|
24 | + /** |
|
25 | + * id for the html label tag |
|
26 | + * |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + protected $_html_label_id; |
|
30 | + |
|
31 | + /** |
|
32 | + * class for teh html label tag |
|
33 | + * |
|
34 | + * @var string |
|
35 | + */ |
|
36 | + protected $_html_label_class; |
|
37 | + |
|
38 | + /** |
|
39 | + * style for teh html label tag |
|
40 | + * |
|
41 | + * @var string |
|
42 | + */ |
|
43 | + protected $_html_label_style; |
|
44 | + |
|
45 | + /** |
|
46 | + * text to be placed in the html label |
|
47 | + * |
|
48 | + * @var string |
|
49 | + */ |
|
50 | + protected $_html_label_text; |
|
51 | + |
|
52 | + /** |
|
53 | + * the full html label. If used, all other html_label_* properties are invalid |
|
54 | + * |
|
55 | + * @var string |
|
56 | + */ |
|
57 | + protected $_html_label; |
|
58 | + |
|
59 | + /** |
|
60 | + * HTML to use for help text (normally placed below form input), in a span which normally |
|
61 | + * has a class of 'description' |
|
62 | + * |
|
63 | + * @var string |
|
64 | + */ |
|
65 | + protected $_html_help_text; |
|
66 | + |
|
67 | + /** |
|
68 | + * CSS classes for displaying the help span |
|
69 | + * |
|
70 | + * @var string |
|
71 | + */ |
|
72 | + protected $_html_help_class = 'description'; |
|
73 | + |
|
74 | + /** |
|
75 | + * CSS to put in the style attribute on the help span |
|
76 | + * |
|
77 | + * @var string |
|
78 | + */ |
|
79 | + protected $_html_help_style; |
|
80 | + |
|
81 | + /** |
|
82 | + * Stores whether or not this input's response is required. |
|
83 | + * Because certain styling elements may also want to know that this |
|
84 | + * input is required etc. |
|
85 | + * |
|
86 | + * @var boolean |
|
87 | + */ |
|
88 | + protected $_required; |
|
89 | + |
|
90 | + /** |
|
91 | + * css class added to required inputs |
|
92 | + * |
|
93 | + * @var string |
|
94 | + */ |
|
95 | + protected $_required_css_class = 'ee-required'; |
|
96 | + |
|
97 | + /** |
|
98 | + * css styles applied to button type inputs |
|
99 | + * |
|
100 | + * @var string |
|
101 | + */ |
|
102 | + protected $_button_css_attributes; |
|
103 | + |
|
104 | + /** |
|
105 | + * The raw post data submitted for this |
|
106 | + * Generally unsafe for usage in client code |
|
107 | + * |
|
108 | + * @var mixed string or array |
|
109 | + */ |
|
110 | + protected $_raw_value; |
|
111 | + |
|
112 | + /** |
|
113 | + * Value normalized according to the input's normalization strategy. |
|
114 | + * The normalization strategy dictates whether this is a string, int, float, |
|
115 | + * boolean, or array of any of those. |
|
116 | + * |
|
117 | + * @var mixed |
|
118 | + */ |
|
119 | + protected $_normalized_value; |
|
120 | + |
|
121 | + |
|
122 | + /** |
|
123 | + * Normalized default value either initially set on the input, or provided by calling |
|
124 | + * set_default(). |
|
125 | + * @var mixed |
|
126 | + */ |
|
127 | + protected $_default; |
|
128 | + |
|
129 | + /** |
|
130 | + * Strategy used for displaying this field. |
|
131 | + * Child classes must use _get_display_strategy to access it. |
|
132 | + * |
|
133 | + * @var EE_Display_Strategy_Base |
|
134 | + */ |
|
135 | + private $_display_strategy; |
|
136 | + |
|
137 | + /** |
|
138 | + * Gets all the validation strategies used on this field |
|
139 | + * |
|
140 | + * @var EE_Validation_Strategy_Base[] |
|
141 | + */ |
|
142 | + private $_validation_strategies = array(); |
|
143 | + |
|
144 | + /** |
|
145 | + * The normalization strategy for this field |
|
146 | + * |
|
147 | + * @var EE_Normalization_Strategy_Base |
|
148 | + */ |
|
149 | + private $_normalization_strategy; |
|
150 | + |
|
151 | + /** |
|
152 | + * Strategy for removing sensitive data after we're done with the form input |
|
153 | + * |
|
154 | + * @var EE_Sensitive_Data_Removal_Base |
|
155 | + */ |
|
156 | + protected $_sensitive_data_removal_strategy; |
|
157 | + |
|
158 | + /** |
|
159 | + * Whether this input has been disabled or not. |
|
160 | + * If it's disabled while rendering, an extra hidden input is added that indicates it has been knowingly disabled. |
|
161 | + * (Client-side code that wants to dynamically disable it must also add this hidden input). |
|
162 | + * When the form is submitted, if the input is disabled in the PHP form section, then input is ignored. |
|
163 | + * If the input is missing from the request data but the hidden input indicating the input is disabled, then the input is again ignored. |
|
164 | + * |
|
165 | + * @var boolean |
|
166 | + */ |
|
167 | + protected $disabled = false; |
|
168 | + |
|
169 | + |
|
170 | + |
|
171 | + /** |
|
172 | + * @param array $input_args { |
|
173 | + * @type string $html_name the html name for the input |
|
174 | + * @type string $html_label_id the id attribute to give to the html label tag |
|
175 | + * @type string $html_label_class the class attribute to give to the html label tag |
|
176 | + * @type string $html_label_style the style attribute to give ot teh label tag |
|
177 | + * @type string $html_label_text the text to put in the label tag |
|
178 | + * @type string $html_label the full html label. If used, |
|
179 | + * all other html_label_* args are invalid |
|
180 | + * @type string $html_help_text text to put in help element |
|
181 | + * @type string $html_help_style style attribute to give to teh help element |
|
182 | + * @type string $html_help_class class attribute to give to the help element |
|
183 | + * @type string $default default value NORMALIZED (eg, if providing the default |
|
184 | + * for a Yes_No_Input, you should provide TRUE or FALSE, not '1' or '0') |
|
185 | + * @type EE_Display_Strategy_Base $display strategy |
|
186 | + * @type EE_Normalization_Strategy_Base $normalization_strategy |
|
187 | + * @type EE_Validation_Strategy_Base[] $validation_strategies |
|
188 | + * @type boolean $ignore_input special argument which can be used to avoid adding any validation strategies, |
|
189 | + * and sets the normalization strategy to the Null normalization. This is good |
|
190 | + * when you want the input to be totally ignored server-side (like when using |
|
191 | + * React.js form inputs) |
|
192 | + * } |
|
193 | + */ |
|
194 | + public function __construct($input_args = array()) |
|
195 | + { |
|
196 | + $input_args = (array) apply_filters('FHEE__EE_Form_Input_Base___construct__input_args', $input_args, $this); |
|
197 | + // the following properties must be cast as arrays |
|
198 | + if (isset($input_args['validation_strategies'])) { |
|
199 | + foreach ((array) $input_args['validation_strategies'] as $validation_strategy) { |
|
200 | + if ($validation_strategy instanceof EE_Validation_Strategy_Base && empty($input_args['ignore_input'])) { |
|
201 | + $this->_validation_strategies[ get_class($validation_strategy) ] = $validation_strategy; |
|
202 | + } |
|
203 | + } |
|
204 | + unset($input_args['validation_strategies']); |
|
205 | + } |
|
206 | + if (isset($input_args['ignore_input'])) { |
|
207 | + $this->_validation_strategies = array(); |
|
208 | + } |
|
209 | + // loop thru incoming options |
|
210 | + foreach ($input_args as $key => $value) { |
|
211 | + // add underscore to $key to match property names |
|
212 | + $_key = '_' . $key; |
|
213 | + if (property_exists($this, $_key)) { |
|
214 | + $this->{$_key} = $value; |
|
215 | + } |
|
216 | + } |
|
217 | + // ensure that "required" is set correctly |
|
218 | + $this->set_required( |
|
219 | + $this->_required, |
|
220 | + isset($input_args['required_validation_error_message']) |
|
221 | + ? $input_args['required_validation_error_message'] |
|
222 | + : null |
|
223 | + ); |
|
224 | + // $this->_html_name_specified = isset( $input_args['html_name'] ) ? TRUE : FALSE; |
|
225 | + $this->_display_strategy->_construct_finalize($this); |
|
226 | + foreach ($this->_validation_strategies as $validation_strategy) { |
|
227 | + $validation_strategy->_construct_finalize($this); |
|
228 | + } |
|
229 | + if (isset($input_args['ignore_input'])) { |
|
230 | + $this->_normalization_strategy = new EE_Null_Normalization(); |
|
231 | + } |
|
232 | + if (! $this->_normalization_strategy) { |
|
233 | + $this->_normalization_strategy = new EE_Text_Normalization(); |
|
234 | + } |
|
235 | + $this->_normalization_strategy->_construct_finalize($this); |
|
236 | + // at least we can use the normalization strategy to populate the default |
|
237 | + if (isset($input_args['default'])) { |
|
238 | + $this->set_default($input_args['default']); |
|
239 | + unset($input_args['default']); |
|
240 | + } |
|
241 | + if (! $this->_sensitive_data_removal_strategy) { |
|
242 | + $this->_sensitive_data_removal_strategy = new EE_No_Sensitive_Data_Removal(); |
|
243 | + } |
|
244 | + $this->_sensitive_data_removal_strategy->_construct_finalize($this); |
|
245 | + parent::__construct($input_args); |
|
246 | + } |
|
247 | + |
|
248 | + |
|
249 | + |
|
250 | + /** |
|
251 | + * Sets the html_name to its default value, if none was specified in teh constructor. |
|
252 | + * Calculation involves using the name and the parent's html_name |
|
253 | + * |
|
254 | + * @throws EE_Error |
|
255 | + */ |
|
256 | + protected function _set_default_html_name_if_empty() |
|
257 | + { |
|
258 | + if (! $this->_html_name) { |
|
259 | + $this->_html_name = $this->name(); |
|
260 | + if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
|
261 | + $this->_html_name = $this->_parent_section->html_name_prefix() . "[{$this->name()}]"; |
|
262 | + } |
|
263 | + } |
|
264 | + } |
|
265 | + |
|
266 | + |
|
267 | + |
|
268 | + /** |
|
269 | + * @param $parent_form_section |
|
270 | + * @param $name |
|
271 | + * @throws EE_Error |
|
272 | + */ |
|
273 | + public function _construct_finalize($parent_form_section, $name) |
|
274 | + { |
|
275 | + parent::_construct_finalize($parent_form_section, $name); |
|
276 | + if ($this->_html_label === null && $this->_html_label_text === null) { |
|
277 | + $this->_html_label_text = ucwords(str_replace("_", " ", $name)); |
|
278 | + } |
|
279 | + do_action('AHEE__EE_Form_Input_Base___construct_finalize__end', $this, $parent_form_section, $name); |
|
280 | + } |
|
281 | + |
|
282 | + |
|
283 | + |
|
284 | + /** |
|
285 | + * Returns the strategy for displaying this form input. If none is set, throws an exception. |
|
286 | + * |
|
287 | + * @return EE_Display_Strategy_Base |
|
288 | + * @throws EE_Error |
|
289 | + */ |
|
290 | + protected function _get_display_strategy() |
|
291 | + { |
|
292 | + $this->ensure_construct_finalized_called(); |
|
293 | + if (! $this->_display_strategy || ! $this->_display_strategy instanceof EE_Display_Strategy_Base) { |
|
294 | + throw new EE_Error( |
|
295 | + sprintf( |
|
296 | + esc_html__( |
|
297 | + "Cannot get display strategy for form input with name %s and id %s, because it has not been set in the constructor", |
|
298 | + "event_espresso" |
|
299 | + ), |
|
300 | + $this->html_name(), |
|
301 | + $this->html_id() |
|
302 | + ) |
|
303 | + ); |
|
304 | + } else { |
|
305 | + return $this->_display_strategy; |
|
306 | + } |
|
307 | + } |
|
308 | + |
|
309 | + |
|
310 | + |
|
311 | + /** |
|
312 | + * Sets the display strategy. |
|
313 | + * |
|
314 | + * @param EE_Display_Strategy_Base $strategy |
|
315 | + */ |
|
316 | + protected function _set_display_strategy(EE_Display_Strategy_Base $strategy) |
|
317 | + { |
|
318 | + $this->_display_strategy = $strategy; |
|
319 | + } |
|
320 | + |
|
321 | + |
|
322 | + |
|
323 | + /** |
|
324 | + * Sets the sanitization strategy |
|
325 | + * |
|
326 | + * @param EE_Normalization_Strategy_Base $strategy |
|
327 | + */ |
|
328 | + protected function _set_normalization_strategy(EE_Normalization_Strategy_Base $strategy) |
|
329 | + { |
|
330 | + $this->_normalization_strategy = $strategy; |
|
331 | + } |
|
332 | + |
|
333 | + |
|
334 | + |
|
335 | + /** |
|
336 | + * Gets sensitive_data_removal_strategy |
|
337 | + * |
|
338 | + * @return EE_Sensitive_Data_Removal_Base |
|
339 | + */ |
|
340 | + public function get_sensitive_data_removal_strategy() |
|
341 | + { |
|
342 | + return $this->_sensitive_data_removal_strategy; |
|
343 | + } |
|
344 | + |
|
345 | + |
|
346 | + |
|
347 | + /** |
|
348 | + * Sets sensitive_data_removal_strategy |
|
349 | + * |
|
350 | + * @param EE_Sensitive_Data_Removal_Base $sensitive_data_removal_strategy |
|
351 | + * @return void |
|
352 | + */ |
|
353 | + public function set_sensitive_data_removal_strategy($sensitive_data_removal_strategy) |
|
354 | + { |
|
355 | + $this->_sensitive_data_removal_strategy = $sensitive_data_removal_strategy; |
|
356 | + } |
|
357 | + |
|
358 | + |
|
359 | + |
|
360 | + /** |
|
361 | + * Gets the display strategy for this input |
|
362 | + * |
|
363 | + * @return EE_Display_Strategy_Base |
|
364 | + */ |
|
365 | + public function get_display_strategy() |
|
366 | + { |
|
367 | + return $this->_display_strategy; |
|
368 | + } |
|
369 | + |
|
370 | + |
|
371 | + |
|
372 | + /** |
|
373 | + * Overwrites the display strategy |
|
374 | + * |
|
375 | + * @param EE_Display_Strategy_Base $display_strategy |
|
376 | + */ |
|
377 | + public function set_display_strategy($display_strategy) |
|
378 | + { |
|
379 | + $this->_display_strategy = $display_strategy; |
|
380 | + $this->_display_strategy->_construct_finalize($this); |
|
381 | + } |
|
382 | + |
|
383 | + |
|
384 | + |
|
385 | + /** |
|
386 | + * Gets the normalization strategy set on this input |
|
387 | + * |
|
388 | + * @return EE_Normalization_Strategy_Base |
|
389 | + */ |
|
390 | + public function get_normalization_strategy() |
|
391 | + { |
|
392 | + return $this->_normalization_strategy; |
|
393 | + } |
|
394 | + |
|
395 | + |
|
396 | + |
|
397 | + /** |
|
398 | + * Overwrites the normalization strategy |
|
399 | + * |
|
400 | + * @param EE_Normalization_Strategy_Base $normalization_strategy |
|
401 | + */ |
|
402 | + public function set_normalization_strategy($normalization_strategy) |
|
403 | + { |
|
404 | + $this->_normalization_strategy = $normalization_strategy; |
|
405 | + $this->_normalization_strategy->_construct_finalize($this); |
|
406 | + } |
|
407 | + |
|
408 | + |
|
409 | + |
|
410 | + /** |
|
411 | + * Returns all teh validation strategies which apply to this field, numerically indexed |
|
412 | + * |
|
413 | + * @return EE_Validation_Strategy_Base[] |
|
414 | + */ |
|
415 | + public function get_validation_strategies() |
|
416 | + { |
|
417 | + return $this->_validation_strategies; |
|
418 | + } |
|
419 | + |
|
420 | + |
|
421 | + |
|
422 | + /** |
|
423 | + * Adds this strategy to the field so it will be used in both JS validation and server-side validation |
|
424 | + * |
|
425 | + * @param EE_Validation_Strategy_Base $validation_strategy |
|
426 | + * @return void |
|
427 | + */ |
|
428 | + protected function _add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy) |
|
429 | + { |
|
430 | + $validation_strategy->_construct_finalize($this); |
|
431 | + $this->_validation_strategies[] = $validation_strategy; |
|
432 | + } |
|
433 | + |
|
434 | + |
|
435 | + |
|
436 | + /** |
|
437 | + * Adds a new validation strategy onto the form input |
|
438 | + * |
|
439 | + * @param EE_Validation_Strategy_Base $validation_strategy |
|
440 | + * @return void |
|
441 | + */ |
|
442 | + public function add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy) |
|
443 | + { |
|
444 | + $this->_add_validation_strategy($validation_strategy); |
|
445 | + } |
|
446 | + |
|
447 | + |
|
448 | + |
|
449 | + /** |
|
450 | + * The classname of the validation strategy to remove |
|
451 | + * |
|
452 | + * @param string $validation_strategy_classname |
|
453 | + */ |
|
454 | + public function remove_validation_strategy($validation_strategy_classname) |
|
455 | + { |
|
456 | + foreach ($this->_validation_strategies as $key => $validation_strategy) { |
|
457 | + if ( |
|
458 | + $validation_strategy instanceof $validation_strategy_classname |
|
459 | + || is_subclass_of($validation_strategy, $validation_strategy_classname) |
|
460 | + ) { |
|
461 | + unset($this->_validation_strategies[ $key ]); |
|
462 | + } |
|
463 | + } |
|
464 | + } |
|
465 | + |
|
466 | + |
|
467 | + |
|
468 | + /** |
|
469 | + * returns true if input employs any of the validation strategy defined by the supplied array of classnames |
|
470 | + * |
|
471 | + * @param array $validation_strategy_classnames |
|
472 | + * @return bool |
|
473 | + */ |
|
474 | + public function has_validation_strategy($validation_strategy_classnames) |
|
475 | + { |
|
476 | + $validation_strategy_classnames = is_array($validation_strategy_classnames) |
|
477 | + ? $validation_strategy_classnames |
|
478 | + : array($validation_strategy_classnames); |
|
479 | + foreach ($this->_validation_strategies as $key => $validation_strategy) { |
|
480 | + if (in_array($key, $validation_strategy_classnames)) { |
|
481 | + return true; |
|
482 | + } |
|
483 | + } |
|
484 | + return false; |
|
485 | + } |
|
486 | + |
|
487 | + |
|
488 | + |
|
489 | + /** |
|
490 | + * Gets the HTML |
|
491 | + * |
|
492 | + * @return string |
|
493 | + */ |
|
494 | + public function get_html() |
|
495 | + { |
|
496 | + return $this->_parent_section->get_html_for_input($this); |
|
497 | + } |
|
498 | + |
|
499 | + |
|
500 | + |
|
501 | + /** |
|
502 | + * Gets the HTML for the input itself (no label or errors) according to the |
|
503 | + * input's display strategy |
|
504 | + * Makes sure the JS and CSS are enqueued for it |
|
505 | + * |
|
506 | + * @return string |
|
507 | + * @throws EE_Error |
|
508 | + */ |
|
509 | + public function get_html_for_input() |
|
510 | + { |
|
511 | + return $this->_form_html_filter |
|
512 | + ? $this->_form_html_filter->filterHtml( |
|
513 | + $this->_get_display_strategy()->display(), |
|
514 | + $this |
|
515 | + ) |
|
516 | + : $this->_get_display_strategy()->display(); |
|
517 | + } |
|
518 | + |
|
519 | + |
|
520 | + |
|
521 | + /** |
|
522 | + * @return string |
|
523 | + */ |
|
524 | + public function html_other_attributes() |
|
525 | + { |
|
526 | + EE_Error::doing_it_wrong( |
|
527 | + __METHOD__, |
|
528 | + sprintf( |
|
529 | + esc_html__( |
|
530 | + 'This method is no longer in use. You should replace it by %s', |
|
531 | + 'event_espresso' |
|
532 | + ), |
|
533 | + 'EE_Form_Section_Base::other_html_attributes()' |
|
534 | + ), |
|
535 | + '4.10.2.p' |
|
536 | + ); |
|
537 | + |
|
538 | + return $this->other_html_attributes(); |
|
539 | + } |
|
540 | + |
|
541 | + |
|
542 | + |
|
543 | + /** |
|
544 | + * @param string $html_other_attributes |
|
545 | + */ |
|
546 | + public function set_html_other_attributes($html_other_attributes) |
|
547 | + { |
|
548 | + EE_Error::doing_it_wrong( |
|
549 | + __METHOD__, |
|
550 | + sprintf( |
|
551 | + esc_html__( |
|
552 | + 'This method is no longer in use. You should replace it by %s', |
|
553 | + 'event_espresso' |
|
554 | + ), |
|
555 | + 'EE_Form_Section_Base::set_other_html_attributes()' |
|
556 | + ), |
|
557 | + '4.10.2.p' |
|
558 | + ); |
|
559 | + |
|
560 | + $this->set_other_html_attributes($html_other_attributes); |
|
561 | + } |
|
562 | + |
|
563 | + |
|
564 | + |
|
565 | + /** |
|
566 | + * Gets the HTML for displaying the label for this form input |
|
567 | + * according to the form section's layout strategy |
|
568 | + * |
|
569 | + * @return string |
|
570 | + */ |
|
571 | + public function get_html_for_label() |
|
572 | + { |
|
573 | + return $this->_parent_section->get_layout_strategy()->display_label($this); |
|
574 | + } |
|
575 | + |
|
576 | + |
|
577 | + |
|
578 | + /** |
|
579 | + * Gets the HTML for displaying the errors section for this form input |
|
580 | + * according to the form section's layout strategy |
|
581 | + * |
|
582 | + * @return string |
|
583 | + */ |
|
584 | + public function get_html_for_errors() |
|
585 | + { |
|
586 | + return $this->_parent_section->get_layout_strategy()->display_errors($this); |
|
587 | + } |
|
588 | + |
|
589 | + |
|
590 | + |
|
591 | + /** |
|
592 | + * Gets the HTML for displaying the help text for this form input |
|
593 | + * according to the form section's layout strategy |
|
594 | + * |
|
595 | + * @return string |
|
596 | + */ |
|
597 | + public function get_html_for_help() |
|
598 | + { |
|
599 | + return $this->_parent_section->get_layout_strategy()->display_help_text($this); |
|
600 | + } |
|
601 | + |
|
602 | + |
|
603 | + |
|
604 | + /** |
|
605 | + * Validates the input's sanitized value (assumes _sanitize() has already been called) |
|
606 | + * and returns whether or not the form input's submitted value is value |
|
607 | + * |
|
608 | + * @return boolean |
|
609 | + */ |
|
610 | + protected function _validate() |
|
611 | + { |
|
612 | + if ($this->isDisabled()) { |
|
613 | + return true; |
|
614 | + } |
|
615 | + foreach ($this->_validation_strategies as $validation_strategy) { |
|
616 | + if ($validation_strategy instanceof EE_Validation_Strategy_Base) { |
|
617 | + try { |
|
618 | + $validation_strategy->validate($this->normalized_value()); |
|
619 | + } catch (EE_Validation_Error $e) { |
|
620 | + $this->add_validation_error($e); |
|
621 | + } |
|
622 | + } |
|
623 | + } |
|
624 | + if ($this->get_validation_errors()) { |
|
625 | + return false; |
|
626 | + } else { |
|
627 | + return true; |
|
628 | + } |
|
629 | + } |
|
630 | + |
|
631 | + |
|
632 | + |
|
633 | + /** |
|
634 | + * Performs basic sanitization on this value. But what sanitization can be performed anyways? |
|
635 | + * This value MIGHT be allowed to have tags, so we can't really remove them. |
|
636 | + * |
|
637 | + * @param string $value |
|
638 | + * @return null|string |
|
639 | + */ |
|
640 | + protected function _sanitize($value) |
|
641 | + { |
|
642 | + return $value !== null ? stripslashes(html_entity_decode(trim($value))) : null; |
|
643 | + } |
|
644 | + |
|
645 | + |
|
646 | + |
|
647 | + /** |
|
648 | + * Picks out the form value that relates to this form input, |
|
649 | + * and stores it as the sanitized value on the form input, and sets the normalized value. |
|
650 | + * Returns whether or not any validation errors occurred |
|
651 | + * |
|
652 | + * @param array $req_data |
|
653 | + * @return boolean whether or not there was an error |
|
654 | + * @throws EE_Error |
|
655 | + */ |
|
656 | + protected function _normalize($req_data) |
|
657 | + { |
|
658 | + // any existing validation errors don't apply so clear them |
|
659 | + $this->_validation_errors = array(); |
|
660 | + // if the input is disabled, ignore whatever input was sent in |
|
661 | + if ($this->isDisabled()) { |
|
662 | + $this->_set_raw_value(null); |
|
663 | + $this->_set_normalized_value($this->get_default()); |
|
664 | + return false; |
|
665 | + } |
|
666 | + try { |
|
667 | + $raw_input = $this->find_form_data_for_this_section($req_data); |
|
668 | + // super simple sanitization for now |
|
669 | + if (is_array($raw_input)) { |
|
670 | + $raw_value = array(); |
|
671 | + foreach ($raw_input as $key => $value) { |
|
672 | + $raw_value[ $key ] = $this->_sanitize($value); |
|
673 | + } |
|
674 | + $this->_set_raw_value($raw_value); |
|
675 | + } else { |
|
676 | + $this->_set_raw_value($this->_sanitize($raw_input)); |
|
677 | + } |
|
678 | + // we want to mostly leave the input alone in case we need to re-display it to the user |
|
679 | + $this->_set_normalized_value($this->_normalization_strategy->normalize($this->raw_value())); |
|
680 | + return false; |
|
681 | + } catch (EE_Validation_Error $e) { |
|
682 | + $this->add_validation_error($e); |
|
683 | + return true; |
|
684 | + } |
|
685 | + } |
|
686 | + |
|
687 | + |
|
688 | + /** |
|
689 | + * @return string |
|
690 | + * @throws EE_Error |
|
691 | + */ |
|
692 | + public function html_name() |
|
693 | + { |
|
694 | + $this->_set_default_html_name_if_empty(); |
|
695 | + return $this->_html_name; |
|
696 | + } |
|
697 | + |
|
698 | + |
|
699 | + /** |
|
700 | + * @return string |
|
701 | + * @throws EE_Error |
|
702 | + */ |
|
703 | + public function html_label_id() |
|
704 | + { |
|
705 | + return ! empty($this->_html_label_id) ? $this->_html_label_id : $this->html_id() . '-lbl'; |
|
706 | + } |
|
707 | + |
|
708 | + |
|
709 | + |
|
710 | + /** |
|
711 | + * @return string |
|
712 | + */ |
|
713 | + public function html_label_class() |
|
714 | + { |
|
715 | + return $this->_html_label_class; |
|
716 | + } |
|
717 | + |
|
718 | + |
|
719 | + |
|
720 | + /** |
|
721 | + * @return string |
|
722 | + */ |
|
723 | + public function html_label_style() |
|
724 | + { |
|
725 | + return $this->_html_label_style; |
|
726 | + } |
|
727 | + |
|
728 | + |
|
729 | + |
|
730 | + /** |
|
731 | + * @return string |
|
732 | + */ |
|
733 | + public function html_label_text() |
|
734 | + { |
|
735 | + return $this->_html_label_text; |
|
736 | + } |
|
737 | + |
|
738 | + |
|
739 | + |
|
740 | + /** |
|
741 | + * @return string |
|
742 | + */ |
|
743 | + public function html_help_text() |
|
744 | + { |
|
745 | + return $this->_html_help_text; |
|
746 | + } |
|
747 | + |
|
748 | + |
|
749 | + |
|
750 | + /** |
|
751 | + * @return string |
|
752 | + */ |
|
753 | + public function html_help_class() |
|
754 | + { |
|
755 | + return $this->_html_help_class; |
|
756 | + } |
|
757 | + |
|
758 | + |
|
759 | + |
|
760 | + /** |
|
761 | + * @return string |
|
762 | + */ |
|
763 | + public function html_help_style() |
|
764 | + { |
|
765 | + return $this->_html_style; |
|
766 | + } |
|
767 | + |
|
768 | + |
|
769 | + |
|
770 | + /** |
|
771 | + * returns the raw, UNSAFE, input, almost exactly as the user submitted it. |
|
772 | + * Please note that almost all client code should instead use the normalized_value; |
|
773 | + * or possibly raw_value_in_form (which prepares the string for displaying in an HTML attribute on a tag, |
|
774 | + * mostly by escaping quotes) |
|
775 | + * Note, we do not store the exact original value sent in the user's request because |
|
776 | + * it may have malicious content, and we MIGHT want to store the form input in a transient or something... |
|
777 | + * in which case, we would have stored the malicious content to our database. |
|
778 | + * |
|
779 | + * @return string |
|
780 | + */ |
|
781 | + public function raw_value() |
|
782 | + { |
|
783 | + return $this->_raw_value; |
|
784 | + } |
|
785 | + |
|
786 | + |
|
787 | + |
|
788 | + /** |
|
789 | + * Returns a string safe to usage in form inputs when displaying, because |
|
790 | + * it escapes all html entities |
|
791 | + * |
|
792 | + * @return string |
|
793 | + */ |
|
794 | + public function raw_value_in_form() |
|
795 | + { |
|
796 | + return htmlentities($this->raw_value(), ENT_QUOTES, 'UTF-8'); |
|
797 | + } |
|
798 | + |
|
799 | + |
|
800 | + |
|
801 | + /** |
|
802 | + * returns the value after it's been sanitized, and then converted into it's proper type |
|
803 | + * in PHP. Eg, a string, an int, an array, |
|
804 | + * |
|
805 | + * @return mixed |
|
806 | + */ |
|
807 | + public function normalized_value() |
|
808 | + { |
|
809 | + return $this->_normalized_value; |
|
810 | + } |
|
811 | + |
|
812 | + |
|
813 | + |
|
814 | + /** |
|
815 | + * Returns the normalized value is a presentable way. By default this is just |
|
816 | + * the normalized value by itself, but it can be overridden for when that's not |
|
817 | + * the best thing to display |
|
818 | + * |
|
819 | + * @return string |
|
820 | + */ |
|
821 | + public function pretty_value() |
|
822 | + { |
|
823 | + return $this->_normalized_value; |
|
824 | + } |
|
825 | + |
|
826 | + |
|
827 | + |
|
828 | + /** |
|
829 | + * When generating the JS for the jquery validation rules like<br> |
|
830 | + * <code>$( "#myform" ).validate({ |
|
831 | + * rules: { |
|
832 | + * password: "required", |
|
833 | + * password_again: { |
|
834 | + * equalTo: "#password" |
|
835 | + * } |
|
836 | + * } |
|
837 | + * });</code> |
|
838 | + * if this field had the name 'password_again', it should return |
|
839 | + * <br><code>password_again: { |
|
840 | + * equalTo: "#password" |
|
841 | + * }</code> |
|
842 | + * |
|
843 | + * @return array |
|
844 | + */ |
|
845 | + public function get_jquery_validation_rules() |
|
846 | + { |
|
847 | + $jquery_validation_js = array(); |
|
848 | + $jquery_validation_rules = array(); |
|
849 | + foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
850 | + $jquery_validation_rules = array_replace_recursive( |
|
851 | + $jquery_validation_rules, |
|
852 | + $validation_strategy->get_jquery_validation_rule_array() |
|
853 | + ); |
|
854 | + } |
|
855 | + if (! empty($jquery_validation_rules)) { |
|
856 | + foreach ($this->get_display_strategy()->get_html_input_ids(true) as $html_id_with_pound_sign) { |
|
857 | + $jquery_validation_js[ $html_id_with_pound_sign ] = $jquery_validation_rules; |
|
858 | + } |
|
859 | + } |
|
860 | + return $jquery_validation_js; |
|
861 | + } |
|
862 | + |
|
863 | + |
|
864 | + |
|
865 | + /** |
|
866 | + * Sets the input's default value for use in displaying in the form. Note: value should be |
|
867 | + * normalized (Eg, if providing a default of ra Yes_NO_Input you would provide TRUE or FALSE, not '1' or '0') |
|
868 | + * |
|
869 | + * @param mixed $value |
|
870 | + * @return void |
|
871 | + */ |
|
872 | + public function set_default($value) |
|
873 | + { |
|
874 | + $this->_default = $value; |
|
875 | + $this->_set_normalized_value($value); |
|
876 | + $this->_set_raw_value($value); |
|
877 | + } |
|
878 | + |
|
879 | + |
|
880 | + |
|
881 | + /** |
|
882 | + * Sets the normalized value on this input |
|
883 | + * |
|
884 | + * @param mixed $value |
|
885 | + */ |
|
886 | + protected function _set_normalized_value($value) |
|
887 | + { |
|
888 | + $this->_normalized_value = $value; |
|
889 | + } |
|
890 | + |
|
891 | + |
|
892 | + |
|
893 | + /** |
|
894 | + * Sets the raw value on this input (ie, exactly as the user submitted it) |
|
895 | + * |
|
896 | + * @param mixed $value |
|
897 | + */ |
|
898 | + protected function _set_raw_value($value) |
|
899 | + { |
|
900 | + $this->_raw_value = $this->_normalization_strategy->unnormalize($value); |
|
901 | + } |
|
902 | + |
|
903 | + |
|
904 | + |
|
905 | + /** |
|
906 | + * Sets the HTML label text after it has already been defined |
|
907 | + * |
|
908 | + * @param string $label |
|
909 | + * @return void |
|
910 | + */ |
|
911 | + public function set_html_label_text($label) |
|
912 | + { |
|
913 | + $this->_html_label_text = $label; |
|
914 | + } |
|
915 | + |
|
916 | + |
|
917 | + |
|
918 | + /** |
|
919 | + * Sets whether or not this field is required, and adjusts the validation strategy. |
|
920 | + * If you want to use the EE_Conditionally_Required_Validation_Strategy, |
|
921 | + * please add it as a validation strategy using add_validation_strategy as normal |
|
922 | + * |
|
923 | + * @param boolean $required boolean |
|
924 | + * @param null $required_text |
|
925 | + */ |
|
926 | + public function set_required($required = true, $required_text = null) |
|
927 | + { |
|
928 | + $required = filter_var($required, FILTER_VALIDATE_BOOLEAN); |
|
929 | + // whether $required is a string or a boolean, we want to add a required validation strategy |
|
930 | + if ($required) { |
|
931 | + $this->_add_validation_strategy(new EE_Required_Validation_Strategy($required_text)); |
|
932 | + } else { |
|
933 | + $this->remove_validation_strategy('EE_Required_Validation_Strategy'); |
|
934 | + } |
|
935 | + $this->_required = $required; |
|
936 | + } |
|
937 | + |
|
938 | + |
|
939 | + |
|
940 | + /** |
|
941 | + * Returns whether or not this field is required |
|
942 | + * |
|
943 | + * @return boolean |
|
944 | + */ |
|
945 | + public function required() |
|
946 | + { |
|
947 | + return $this->_required; |
|
948 | + } |
|
949 | + |
|
950 | + |
|
951 | + |
|
952 | + /** |
|
953 | + * @param string $required_css_class |
|
954 | + */ |
|
955 | + public function set_required_css_class($required_css_class) |
|
956 | + { |
|
957 | + $this->_required_css_class = $required_css_class; |
|
958 | + } |
|
959 | + |
|
960 | + |
|
961 | + |
|
962 | + /** |
|
963 | + * @return string |
|
964 | + */ |
|
965 | + public function required_css_class() |
|
966 | + { |
|
967 | + return $this->_required_css_class; |
|
968 | + } |
|
969 | + |
|
970 | + |
|
971 | + |
|
972 | + /** |
|
973 | + * @param bool $add_required |
|
974 | + * @return string |
|
975 | + */ |
|
976 | + public function html_class($add_required = false) |
|
977 | + { |
|
978 | + return $add_required && $this->required() |
|
979 | + ? $this->required_css_class() . ' ' . $this->_html_class |
|
980 | + : $this->_html_class; |
|
981 | + } |
|
982 | + |
|
983 | + |
|
984 | + /** |
|
985 | + * Sets the help text, in case |
|
986 | + * |
|
987 | + * @param string $text |
|
988 | + */ |
|
989 | + public function set_html_help_text($text) |
|
990 | + { |
|
991 | + $this->_html_help_text = $text; |
|
992 | + } |
|
993 | + |
|
994 | + |
|
995 | + |
|
996 | + /** |
|
997 | + * Uses the sensitive data removal strategy to remove the sensitive data from this |
|
998 | + * input. If there is any kind of sensitive data removal on this input, we clear |
|
999 | + * out the raw value completely |
|
1000 | + * |
|
1001 | + * @return void |
|
1002 | + */ |
|
1003 | + public function clean_sensitive_data() |
|
1004 | + { |
|
1005 | + // if we do ANY kind of sensitive data removal on this, then just clear out the raw value |
|
1006 | + // if we need more logic than this we'll make a strategy for it |
|
1007 | + if ( |
|
1008 | + $this->_sensitive_data_removal_strategy |
|
1009 | + && ! $this->_sensitive_data_removal_strategy instanceof EE_No_Sensitive_Data_Removal |
|
1010 | + ) { |
|
1011 | + $this->_set_raw_value(null); |
|
1012 | + } |
|
1013 | + // and clean the normalized value according to the appropriate strategy |
|
1014 | + $this->_set_normalized_value( |
|
1015 | + $this->get_sensitive_data_removal_strategy()->remove_sensitive_data( |
|
1016 | + $this->_normalized_value |
|
1017 | + ) |
|
1018 | + ); |
|
1019 | + } |
|
1020 | + |
|
1021 | + |
|
1022 | + |
|
1023 | + /** |
|
1024 | + * @param bool $primary |
|
1025 | + * @param string $button_size |
|
1026 | + * @param string $other_attributes |
|
1027 | + */ |
|
1028 | + public function set_button_css_attributes($primary = true, $button_size = '', $other_attributes = '') |
|
1029 | + { |
|
1030 | + $button_css_attributes = 'button'; |
|
1031 | + $button_css_attributes .= $primary === true ? ' button-primary' : ' button-secondary'; |
|
1032 | + switch ($button_size) { |
|
1033 | + case 'xs': |
|
1034 | + case 'extra-small': |
|
1035 | + $button_css_attributes .= ' button-xs'; |
|
1036 | + break; |
|
1037 | + case 'sm': |
|
1038 | + case 'small': |
|
1039 | + $button_css_attributes .= ' button-sm'; |
|
1040 | + break; |
|
1041 | + case 'lg': |
|
1042 | + case 'large': |
|
1043 | + $button_css_attributes .= ' button-lg'; |
|
1044 | + break; |
|
1045 | + case 'block': |
|
1046 | + $button_css_attributes .= ' button-block'; |
|
1047 | + break; |
|
1048 | + case 'md': |
|
1049 | + case 'medium': |
|
1050 | + default: |
|
1051 | + $button_css_attributes .= ''; |
|
1052 | + } |
|
1053 | + $this->_button_css_attributes .= ! empty($other_attributes) |
|
1054 | + ? $button_css_attributes . ' ' . $other_attributes |
|
1055 | + : $button_css_attributes; |
|
1056 | + } |
|
1057 | + |
|
1058 | + |
|
1059 | + |
|
1060 | + /** |
|
1061 | + * @return string |
|
1062 | + */ |
|
1063 | + public function button_css_attributes() |
|
1064 | + { |
|
1065 | + if (empty($this->_button_css_attributes)) { |
|
1066 | + $this->set_button_css_attributes(); |
|
1067 | + } |
|
1068 | + return $this->_button_css_attributes; |
|
1069 | + } |
|
1070 | + |
|
1071 | + |
|
1072 | + |
|
1073 | + /** |
|
1074 | + * find_form_data_for_this_section |
|
1075 | + * using this section's name and its parents, finds the value of the form data that corresponds to it. |
|
1076 | + * For example, if this form section's HTML name is my_form[subform][form_input_1], |
|
1077 | + * then it's value should be in request at request['my_form']['subform']['form_input_1']. |
|
1078 | + * (If that doesn't exist, we also check for this subsection's name |
|
1079 | + * at the TOP LEVEL of the request data. Eg request['form_input_1'].) |
|
1080 | + * This function finds its value in the form. |
|
1081 | + * |
|
1082 | + * @param array $req_data |
|
1083 | + * @return mixed whatever the raw value of this form section is in the request data |
|
1084 | + * @throws EE_Error |
|
1085 | + */ |
|
1086 | + public function find_form_data_for_this_section($req_data) |
|
1087 | + { |
|
1088 | + $name_parts = $this->getInputNameParts(); |
|
1089 | + // now get the value for the input |
|
1090 | + $value = $this->findRequestForSectionUsingNameParts($name_parts, $req_data); |
|
1091 | + // check if this thing's name is at the TOP level of the request data |
|
1092 | + if ($value === null && isset($req_data[ $this->name() ])) { |
|
1093 | + $value = $req_data[ $this->name() ]; |
|
1094 | + } |
|
1095 | + return $value; |
|
1096 | + } |
|
1097 | + |
|
1098 | + |
|
1099 | + /** |
|
1100 | + * If this input's name is something like "foo[bar][baz]" |
|
1101 | + * returns an array like `array('foo','bar',baz')` |
|
1102 | + * |
|
1103 | + * @return array |
|
1104 | + * @throws EE_Error |
|
1105 | + */ |
|
1106 | + protected function getInputNameParts() |
|
1107 | + { |
|
1108 | + // break up the html name by "[]" |
|
1109 | + if (strpos($this->html_name(), '[') !== false) { |
|
1110 | + $before_any_brackets = substr($this->html_name(), 0, strpos($this->html_name(), '[')); |
|
1111 | + } else { |
|
1112 | + $before_any_brackets = $this->html_name(); |
|
1113 | + } |
|
1114 | + // grab all of the segments |
|
1115 | + preg_match_all('~\[([^]]*)\]~', $this->html_name(), $matches); |
|
1116 | + if (isset($matches[1]) && is_array($matches[1])) { |
|
1117 | + $name_parts = $matches[1]; |
|
1118 | + array_unshift($name_parts, $before_any_brackets); |
|
1119 | + } else { |
|
1120 | + $name_parts = array($before_any_brackets); |
|
1121 | + } |
|
1122 | + return $name_parts; |
|
1123 | + } |
|
1124 | + |
|
1125 | + |
|
1126 | + |
|
1127 | + /** |
|
1128 | + * @param array $html_name_parts |
|
1129 | + * @param array $req_data |
|
1130 | + * @return array | NULL |
|
1131 | + */ |
|
1132 | + public function findRequestForSectionUsingNameParts($html_name_parts, $req_data) |
|
1133 | + { |
|
1134 | + $first_part_to_consider = array_shift($html_name_parts); |
|
1135 | + if (isset($req_data[ $first_part_to_consider ])) { |
|
1136 | + if (empty($html_name_parts)) { |
|
1137 | + return $req_data[ $first_part_to_consider ]; |
|
1138 | + } else { |
|
1139 | + return $this->findRequestForSectionUsingNameParts( |
|
1140 | + $html_name_parts, |
|
1141 | + $req_data[ $first_part_to_consider ] |
|
1142 | + ); |
|
1143 | + } |
|
1144 | + } else { |
|
1145 | + return null; |
|
1146 | + } |
|
1147 | + } |
|
1148 | + |
|
1149 | + |
|
1150 | + |
|
1151 | + /** |
|
1152 | + * Checks if this form input's data is in the request data |
|
1153 | + * |
|
1154 | + * @param array $req_data |
|
1155 | + * @return boolean |
|
1156 | + * @throws EE_Error |
|
1157 | + */ |
|
1158 | + public function form_data_present_in($req_data = null) |
|
1159 | + { |
|
1160 | + if ($req_data === null) { |
|
1161 | + /** @var RequestInterface $request */ |
|
1162 | + $request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
1163 | + $req_data = $request->postParams(); |
|
1164 | + } |
|
1165 | + $checked_value = $this->find_form_data_for_this_section($req_data); |
|
1166 | + if ($checked_value !== null) { |
|
1167 | + return true; |
|
1168 | + } else { |
|
1169 | + return false; |
|
1170 | + } |
|
1171 | + } |
|
1172 | + |
|
1173 | + |
|
1174 | + |
|
1175 | + /** |
|
1176 | + * Overrides parent to add js data from validation and display strategies |
|
1177 | + * |
|
1178 | + * @param array $form_other_js_data |
|
1179 | + * @return array |
|
1180 | + */ |
|
1181 | + public function get_other_js_data($form_other_js_data = array()) |
|
1182 | + { |
|
1183 | + return $this->get_other_js_data_from_strategies($form_other_js_data); |
|
1184 | + } |
|
1185 | + |
|
1186 | + |
|
1187 | + |
|
1188 | + /** |
|
1189 | + * Gets other JS data for localization from this input's strategies, like |
|
1190 | + * the validation strategies and the display strategy |
|
1191 | + * |
|
1192 | + * @param array $form_other_js_data |
|
1193 | + * @return array |
|
1194 | + */ |
|
1195 | + public function get_other_js_data_from_strategies($form_other_js_data = array()) |
|
1196 | + { |
|
1197 | + $form_other_js_data = $this->get_display_strategy()->get_other_js_data($form_other_js_data); |
|
1198 | + foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
1199 | + $form_other_js_data = $validation_strategy->get_other_js_data($form_other_js_data); |
|
1200 | + } |
|
1201 | + return $form_other_js_data; |
|
1202 | + } |
|
1203 | + |
|
1204 | + |
|
1205 | + |
|
1206 | + /** |
|
1207 | + * Override parent because we want to give our strategies an opportunity to enqueue some js and css |
|
1208 | + * |
|
1209 | + * @return void |
|
1210 | + */ |
|
1211 | + public function enqueue_js() |
|
1212 | + { |
|
1213 | + // ask our display strategy and validation strategies if they have js to enqueue |
|
1214 | + $this->enqueue_js_from_strategies(); |
|
1215 | + } |
|
1216 | + |
|
1217 | + |
|
1218 | + |
|
1219 | + /** |
|
1220 | + * Tells strategies when its ok to enqueue their js and css |
|
1221 | + * |
|
1222 | + * @return void |
|
1223 | + */ |
|
1224 | + public function enqueue_js_from_strategies() |
|
1225 | + { |
|
1226 | + $this->get_display_strategy()->enqueue_js(); |
|
1227 | + foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
1228 | + $validation_strategy->enqueue_js(); |
|
1229 | + } |
|
1230 | + } |
|
1231 | + |
|
1232 | + |
|
1233 | + |
|
1234 | + /** |
|
1235 | + * Gets the default value set on the input (not the current value, which may have been |
|
1236 | + * changed because of a form submission). If no default was set, this us null. |
|
1237 | + * @return mixed |
|
1238 | + */ |
|
1239 | + public function get_default() |
|
1240 | + { |
|
1241 | + return $this->_default; |
|
1242 | + } |
|
1243 | + |
|
1244 | + |
|
1245 | + |
|
1246 | + /** |
|
1247 | + * Makes this input disabled. That means it will have the HTML attribute 'disabled="disabled"', |
|
1248 | + * and server-side if any input was received it will be ignored |
|
1249 | + */ |
|
1250 | + public function disable($disable = true) |
|
1251 | + { |
|
1252 | + $disabled_attribute = ' disabled="disabled"'; |
|
1253 | + $this->disabled = filter_var($disable, FILTER_VALIDATE_BOOLEAN); |
|
1254 | + if ($this->disabled) { |
|
1255 | + if (strpos($this->_other_html_attributes, $disabled_attribute) === false) { |
|
1256 | + $this->_other_html_attributes .= $disabled_attribute; |
|
1257 | + } |
|
1258 | + $this->_set_normalized_value($this->get_default()); |
|
1259 | + } else { |
|
1260 | + $this->_other_html_attributes = str_replace($disabled_attribute, '', $this->_other_html_attributes); |
|
1261 | + } |
|
1262 | + } |
|
1263 | + |
|
1264 | + |
|
1265 | + |
|
1266 | + /** |
|
1267 | + * Returns whether or not this input is currently disabled. |
|
1268 | + * @return bool |
|
1269 | + */ |
|
1270 | + public function isDisabled() |
|
1271 | + { |
|
1272 | + return $this->disabled; |
|
1273 | + } |
|
1274 | 1274 | } |
@@ -15,7 +15,7 @@ |
||
15 | 15 | */ |
16 | 16 | public function __construct($options = array()) |
17 | 17 | { |
18 | - $select_options = array(true => esc_html__("Yes", "event_espresso"),false => esc_html__("No", "event_espresso")); |
|
18 | + $select_options = array(true => esc_html__("Yes", "event_espresso"), false => esc_html__("No", "event_espresso")); |
|
19 | 19 | |
20 | 20 | parent::__construct($select_options, $options); |
21 | 21 | } |
@@ -9,13 +9,13 @@ |
||
9 | 9 | */ |
10 | 10 | class EE_Yes_No_Input extends EE_Select_Input |
11 | 11 | { |
12 | - /** |
|
13 | - * @param array $options |
|
14 | - */ |
|
15 | - public function __construct($options = array()) |
|
16 | - { |
|
17 | - $select_options = array(true => esc_html__("Yes", "event_espresso"),false => esc_html__("No", "event_espresso")); |
|
12 | + /** |
|
13 | + * @param array $options |
|
14 | + */ |
|
15 | + public function __construct($options = array()) |
|
16 | + { |
|
17 | + $select_options = array(true => esc_html__("Yes", "event_espresso"),false => esc_html__("No", "event_espresso")); |
|
18 | 18 | |
19 | - parent::__construct($select_options, $options); |
|
20 | - } |
|
19 | + parent::__construct($select_options, $options); |
|
20 | + } |
|
21 | 21 | } |
@@ -14,17 +14,17 @@ |
||
14 | 14 | */ |
15 | 15 | class EE_Phone_Input extends EE_Text_Input |
16 | 16 | { |
17 | - /** |
|
18 | - * @param array $options |
|
19 | - */ |
|
20 | - public function __construct($options = array()) |
|
21 | - { |
|
22 | - $this->_add_validation_strategy( |
|
23 | - new EE_Text_Validation_Strategy( |
|
24 | - esc_html__('Please enter a valid phone number. Eg 123-456-7890 or 1234567890', 'event_espresso'), |
|
25 | - '~^(([\d]{10})|(^[\d]{3}-[\d]{3}-[\d]{4}))$~' |
|
26 | - ) |
|
27 | - ); |
|
28 | - parent::__construct($options); |
|
29 | - } |
|
17 | + /** |
|
18 | + * @param array $options |
|
19 | + */ |
|
20 | + public function __construct($options = array()) |
|
21 | + { |
|
22 | + $this->_add_validation_strategy( |
|
23 | + new EE_Text_Validation_Strategy( |
|
24 | + esc_html__('Please enter a valid phone number. Eg 123-456-7890 or 1234567890', 'event_espresso'), |
|
25 | + '~^(([\d]{10})|(^[\d]{3}-[\d]{3}-[\d]{4}))$~' |
|
26 | + ) |
|
27 | + ); |
|
28 | + parent::__construct($options); |
|
29 | + } |
|
30 | 30 | } |