PluginProbe
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions / trunk
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions vtrunk
260909 260829 260814 260805 110710 110731 110812 110815 110912 110913 110915 110926 110927 111002 111003 111011 111017 111029 111105 111206 111216 111220 120213 120219 120301 All 187 releases
s2member / src / includes / classes / custom-reg-fields.inc.php

custom-reg-fields.inc.php in s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions trunk, at src/includes/classes/custom-reg-fields.inc.php

847 lines 50.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // @codingStandardsIgnoreFile
3 /**
4 * Custom Registration/Profile Fields for s2Member.
5 *
6 * Copyright: © 2009-2011
7 * {@link http://websharks-inc.com/ WebSharks, Inc.}
8 * (coded in the USA)
9 *
10 * Released under the terms of the GNU General Public License.
11 * You should have received a copy of the GNU General Public License,
12 * along with this software. In the main directory, see: /licensing/
13 * If not, see: {@link http://www.gnu.org/licenses/}.
14 *
15 * @package s2Member\Custom_Reg_Fields
16 * @since 3.5
17 */
18 if(!defined('WPINC')) // MUST have WordPress.
19 exit("Do not access this file directly.");
20
21 if(!class_exists("c_ws_plugin__s2member_custom_reg_fields"))
22 {
23 /**
24 * Custom Registration/Profile Fields for s2Member.
25 *
26 * @package s2Member\Custom_Reg_Fields
27 * @since 3.5
28 */
29 class c_ws_plugin__s2member_custom_reg_fields
30 {
31 /**
32 * Generates all Custom Fields.
33 *
34 * @package s2Member\Custom_Reg_Fields
35 * @since 3.5
36 *
37 * @param string $_function Function calling upon this routine.
38 * @param array $_field The Field array of configuration options.
39 * @param string $_name_prefix The `name=""` attribute prefix.
40 * @param string $_id_prefix The `id=""` attribute prefix.
41 * @param string $_classes Optional. String of space separated classes that will go inside the Field's `class=""` attribute.
42 * @param string $_styles Optional. String of CSS styles that will go inside the Field's `style=""` attribute.
43 * @param string|int $_tabindex. Optional numeric tabindex for the `tabindex=""` attribute.
44 * @param string $_attrs Optional. Some additional Field attributes and values.
45 * @param array $_submission Optional. But should be passed in with any submission data related to this Field. For instance, you might pass in ``$_POST``.
46 * @param string|array $_value Optional. The value of this Field, either by default, or from the ``$_submission`` array.
47 * @param string $_editable_context Optional. One of `profile|profile-view|registration`.
48 * @return string The resulting Custom Field, in HTML format.
49 */
50 public static function custom_field_gen($_function = FALSE, $_field = FALSE, $_name_prefix = FALSE, $_id_prefix = FALSE, $_classes = FALSE, $_styles = FALSE, $_tabindex = FALSE, $_attrs = FALSE, $_submission = FALSE, $_value = FALSE, $_editable_context = FALSE)
51 {
52 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
53 do_action("ws_plugin__s2member_before_custom_field_gen", get_defined_vars());
54 unset($__refs, $__v);
55
56 if(!($gen = "") && $_function && is_array($field = $_field) && !empty($field["type"]) && !empty($field["id"]) && $_name_prefix && $_id_prefix)
57 {
58 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
59 do_action("ws_plugin__s2member_during_custom_field_gen_before", get_defined_vars());
60 unset($__refs, $__v);
61
62 $field_var = preg_replace("/[^a-z0-9]/i", "_", strtolower($field["id"]));
63 $field_id_class = preg_replace("/_/", "-", $field_var);
64
65 $name_suffix = (preg_match("/\[$/", $_name_prefix)) ? ']' : '';
66 $field_name = trim($_name_prefix.$field_var.$name_suffix);
67
68 if(in_array($field["type"], array("text", "textarea", "select"), TRUE))
69 $_classes = trim($_classes." form-control"); // Bootstrap.
70
71 $common = /* Common attributes. */ '';
72 $common .= ' name="'.esc_attr($field_name).'"';
73 $common .= ' id="'.esc_attr($_id_prefix.$field_id_class).'"';
74 $common .= ((!empty($field["required"]) && $field["required"] === "yes") ? ' aria-required="true"' : '');
75 $common .= ((strlen($_tabindex)) ? ' tabindex="'.esc_attr($_tabindex).'"' : /* No tabindex if empty. */ '');
76 $common .= (( /* Certain data expected? */!empty($field["expected"])) ? ' data-expected="'.esc_attr($field["expected"]).'"' : '');
77 $common .= (($_editable_context === "profile-view" || ($_editable_context === "profile" && !empty($field["editable"]) && strpos($field["editable"], "no") === 0)) ? ' disabled="disabled"' : '');
78 $common .= (($_classes || !empty($field["classes"])) ? ' class="'.esc_attr(trim($_classes.((!empty($field["classes"])) ? ' '.$field["classes"] : ''))).'"' : '');
79 $common .= (($_styles || !empty($field["styles"])) ? ' style="'.esc_attr(trim($_styles.((!empty($field["styles"])) ? ' '.$field["styles"] : ''))).'"' : '');
80 $common .= (($_attrs || !empty($field["attrs"])) ? ' '.trim($_attrs.((!empty($field["attrs"])) ? ' '.$field["attrs"] : '')) : '');
81
82 if($field["type"] === "text")
83 {
84 if($_editable_context === "profile-view")
85 $gen = esc_html((string)$_value);
86
87 else // Else handle normally.
88 {
89 $gen = '<input type="text" maxlength="100" autocomplete="off"';
90 $gen .= ' value="'.format_to_edit((!$_submission && isset($field["deflt"]) && strlen((string)$field["deflt"])) ? (string)$field["deflt"] : (string)$_value).'"';
91 $gen .= $common.' />';
92 }
93 }
94 else if($field["type"] === "textarea")
95 {
96 if($_editable_context === "profile-view")
97 $gen = nl2br(esc_html((string)$_value));
98
99 else // Else handle normally.
100 {
101 $gen = '<textarea rows="3"'.$common.'>';
102 $gen .= format_to_edit((!$_submission && isset($field["deflt"]) && strlen((string)$field["deflt"])) ? (string)$field["deflt"] : (string)$_value);
103 $gen .= '</textarea>';
104 }
105 }
106 else if($field["type"] === "select" && !empty($field["options"]))
107 {
108 if($_editable_context === "profile-view")
109 {
110 foreach(preg_split("/[\r\n\t]+/", $field["options"]) as $option_line)
111 {
112 $option_value = $option_label = $option_default = "";
113 @list($option_value, $option_label, $option_default) = c_ws_plugin__s2member_utils_strings::trim_deep(preg_split("/\|/", trim($option_line)));
114 if($option_value === (string)$_value)
115 {
116 $gen = $option_label;
117 break;
118 }
119 }
120 }
121 else // Else handle normally.
122 {
123 $gen = '<select'.$common.'>';
124 $selected_default_option = false;
125 foreach(preg_split("/[\r\n\t]+/", $field["options"]) as $option_line)
126 {
127 $option_value = $option_label = $option_default = "";
128 @list($option_value, $option_label, $option_default) = c_ws_plugin__s2member_utils_strings::trim_deep(preg_split("/\|/", trim($option_line)));
129 $gen .= '<option value="'.esc_attr($option_value).'"'.(((($option_default && !$_submission) || ($option_value === (string)$_value && !$selected_default_option)) && ($selected_default_option = true)) ? ' selected="selected"' : '').'>'.$option_label.'</option>';
130 }
131 $gen .= '</select>';
132 }
133 }
134 else if($field["type"] === "selects" && !empty($field["options"]))
135 {
136 if($_editable_context === "profile-view")
137 {
138 foreach(preg_split("/[\r\n\t]+/", $field["options"]) as $option_line)
139 {
140 $option_value = $option_label = $option_default = "";
141 @list($option_value, $option_label, $option_default) = c_ws_plugin__s2member_utils_strings::trim_deep(preg_split("/\|/", trim($option_line)));
142 if(in_array($option_value, (array)$_value))
143 $gen .= $option_label.", ";
144 }
145 $gen = c_ws_plugin__s2member_utils_strings::trim($gen, 0, ",");
146 }
147 else // Else handle normally.
148 {
149 $common = preg_replace('/ name\="(.+?)"/', ' name="$1[]"', $common);
150 $common = preg_replace('/ style\="(.+?)"/', ' style="height:auto; $1"', $common);
151
152 $gen = '<select multiple="multiple" size="3"'.$common.'>';
153 foreach(preg_split("/[\r\n\t]+/", $field["options"]) as $option_line)
154 {
155 $option_value = $option_label = $option_default = "";
156 @list($option_value, $option_label, $option_default) = c_ws_plugin__s2member_utils_strings::trim_deep(preg_split("/\|/", trim($option_line)));
157 $gen .= '<option value="'.esc_attr($option_value).'"'.((($option_default && !$_submission) || in_array($option_value, (array)$_value)) ? ' selected="selected"' : '').'>'.$option_label.'</option>';
158 }
159 $gen .= '</select>';
160 }
161 }
162 else if($field["type"] === "checkbox" && !empty($field["label"]))
163 {
164 if($_editable_context === "profile-view")
165 $gen = ((string)$_value) ? "yes" : "no";
166
167 else // Else handle normally.
168 {
169 $gen = '<input type="checkbox" value="1"';
170 $gen .= (((string)$_value) ? ' checked="checked"' : '');
171 $gen .= $common.' /> <label for="'.esc_attr($_id_prefix.$field_id_class).'" style="display:inline !important; margin:0 !important;">'.$field["label"].'</label>';
172 }
173 }
174 else if($field["type"] === "pre_checkbox" && !empty($field["label"]))
175 {
176 if($_editable_context === "profile-view")
177 $gen = ((string)$_value) ? "yes" : "no";
178
179 else // Else handle normally.
180 {
181 $gen = '<input type="checkbox" value="1"';
182 $gen .= ((!$_submission || (string)$_value) ? ' checked="checked"' : '');
183 $gen .= $common.' /> <label for="'.esc_attr($_id_prefix.$field_id_class).'" style="display:inline !important; margin:0 !important;">'.$field["label"].'</label>';
184 }
185 }
186 else if($field["type"] === "checkboxes" && !empty($field["options"]))
187 {
188 if($_editable_context === "profile-view")
189 {
190 foreach(preg_split("/[\r\n\t]+/", $field["options"]) as $i => $option_line)
191 {
192 $option_value = $option_label = $option_default = "";
193 @list($option_value, $option_label, $option_default) = c_ws_plugin__s2member_utils_strings::trim_deep(preg_split("/\|/", trim($option_line)));
194 if(in_array($option_value, (array)$_value))
195 $gen .= $option_label.", ";
196 }
197 $gen = c_ws_plugin__s2member_utils_strings::trim($gen, 0, ",");
198 }
199 else // Else handle normally.
200 {
201 $common = preg_replace('/ name\="(.+?)"/', ' name="$1[]"', $common);
202
203 $sep = apply_filters("ws_plugin__s2member_custom_field_gen_checkboxes_sep", "&nbsp;&nbsp;", get_defined_vars());
204 $opl = apply_filters("ws_plugin__s2member_custom_field_gen_checkboxes_opl", "ws-plugin--s2member-custom-reg-field-op-l", get_defined_vars());
205
206 foreach(preg_split("/[\r\n\t]+/", $field["options"]) as $i => $option_line)
207 {
208 $common_i = preg_replace('/ id\="(.+?)"/', ' id="$1-'.($i).'"', $common); //200226 Fix label for/id mismatch (checkbox options).
209
210 $option_value = $option_label = $option_default = "";
211 @list($option_value, $option_label, $option_default) = c_ws_plugin__s2member_utils_strings::trim_deep(preg_split("/\|/", trim($option_line)));
212
213 $gen .= ($i > 0) ? $sep : ''; // Separators can be filtered above.
214 $gen .= '<input type="checkbox" value="'.esc_attr($option_value).'"';
215 $gen .= ((($option_default && !$_submission) || in_array($option_value, (array)$_value)) ? ' checked="checked"' : '');
216 $gen .= $common_i.' /> <label for="'.esc_attr($_id_prefix.$field_id_class."-".$i).'" class="'.esc_attr($opl).'" style="display:inline !important; margin:0 !important;">'.$option_label.'</label>';
217 }
218 }
219 }
220 else if($field["type"] === "radios" && !empty($field["options"]))
221 {
222 if($_editable_context === "profile-view")
223 {
224 foreach(preg_split("/[\r\n\t]+/", $field["options"]) as $i => $option_line)
225 {
226 $option_value = $option_label = $option_default = "";
227 @list($option_value, $option_label, $option_default) = c_ws_plugin__s2member_utils_strings::trim_deep(preg_split("/\|/", trim($option_line)));
228 if($option_value === (string)$_value)
229 {
230 $gen = $option_label;
231 break;
232 }
233 }
234 }
235 else // Else handle normally.
236 {
237 $sep = apply_filters("ws_plugin__s2member_custom_field_gen_radios_sep", "&nbsp;&nbsp;", get_defined_vars());
238 $opl = apply_filters("ws_plugin__s2member_custom_field_gen_radios_opl", "ws-plugin--s2member-custom-reg-field-op-l", get_defined_vars());
239
240 foreach(preg_split("/[\r\n\t]+/", $field["options"]) as $i => $option_line)
241 {
242 $common_i = preg_replace('/ id\="(.+?)"/', ' id="$1-'.($i).'"', $common); //200226 Fix label for/id mismatch (radio options).
243
244 $option_value = $option_label = $option_default = "";
245 @list($option_value, $option_label, $option_default) = c_ws_plugin__s2member_utils_strings::trim_deep(preg_split("/\|/", trim($option_line)));
246
247 $gen .= ($i > 0) ? $sep : ''; // Separators can be filtered above.
248 $gen .= '<input type="radio" value="'.esc_attr($option_value).'"';
249 $gen .= ((($option_default && !$_submission) || $option_value === (string)$_value) ? ' checked="checked"' : '');
250 $gen .= $common_i.' /> <label for="'.esc_attr($_id_prefix.$field_id_class."-".$i).'" class="'.esc_attr($opl).'" style="display:inline !important; margin:0 !important;">'.$option_label.'</label>';
251 }
252 }
253 }
254 else // Default to a text field input type when nothing matches.
255 {
256 if($_editable_context === "profile-view")
257 $gen = esc_html((string)$_value);
258
259 else // Else handle normally.
260 {
261 $gen = '<input type="text" maxlength="100" autocomplete="off"';
262 $gen .= ' value="'.format_to_edit((!$_submission && isset($field["deflt"]) && strlen((string)$field["deflt"])) ? (string)$field["deflt"] : (string)$_value).'"';
263 $gen .= $common.' />';
264 }
265 }
266 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
267 do_action("ws_plugin__s2member_during_custom_field_gen_after", get_defined_vars());
268 unset($__refs, $__v);
269 }
270 return apply_filters("ws_plugin__s2member_custom_field_gen", $gen, get_defined_vars());
271 }
272 /**
273 * Determines which Custom Fields apply to a specific Level.
274 *
275 * @package s2Member\Custom_Reg_Fields
276 * @since 3.5
277 *
278 * @param string|int $_level Optional. Defaults to the current User's Access Level number.
279 * You can either pass in a numeric Level number, or the string `auto-detection`.
280 * @param string $_editable_context Optional. One of `profile|profile-view|registration`.
281 * @param boolean $full_config Optional. Defaults to a `FALSE` value. `TRUE` to get a full array for each field configuration.
282 * @return array Array of Custom Field IDs applicable.
283 */
284 public static function custom_fields_configured_at_level($_level = "auto-detection", $_editable_context = FALSE, $full_config = FALSE)
285 {
286 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
287 do_action("ws_plugin__s2member_before_custom_fields_configured_at_level", get_defined_vars());
288 unset($__refs, $__v);
289
290 $level = ($_level === "auto-detection") ? c_ws_plugin__s2member_user_access::user_access_level() : $_level;
291 if($_level === "auto-detection" && $level < 0 && ($reg_cookies = c_ws_plugin__s2member_register_access::reg_cookies_ok()) && extract($reg_cookies) && preg_match($GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["membership_item_number_w_level_regex"], $item_number, $m) && !empty($m[1]) && is_numeric($m[1]))
292 $level = /* A numeric Membership Level # . */ $m[1];
293
294 $level = ($level !== "any" && (!is_numeric($level) || $level < 0)) ? 0 : /* Default. */ $level;
295
296 if(($level === "any" || (is_numeric($level) && $level >= 0)) && $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_fields"])
297 {
298 foreach(json_decode($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_fields"], true) as $field)
299 if($level === "any" || $field["levels"] === "all" || in_array($level, preg_split("/[;,]+/", preg_replace("/[^0-9;,]/", "", $field["levels"]))))
300 if(empty($_editable_context) || $_editable_context === "administrative" || ($_editable_context === "registration" && $field["editable"] !== "no-always-invisible" && $field["editable"] !== "yes-invisible") || (($_editable_context === "profile" || $_editable_context === "profile-view") && $field["editable"] !== "no-invisible" && $field["editable"] !== "no-always-invisible"))
301 $configured[] = /* Add this to the array. */ $full_config ? $field : $field["id"];
302 }
303 return apply_filters("ws_plugin__s2member_custom_fields_configured_at_level", ((!empty($configured)) ? $configured : array()), get_defined_vars());
304 }
305 /**
306 * Adds Custom Fields to: `/wp-signup.php`.
307 *
308 * For Multisite Blog Farms.
309 *
310 * @package s2Member\Custom_Reg_Fields
311 * @since 3.5
312 *
313 * @attaches-to ``add_action("signup_extra_fields");``
314 *
315 * @todo Optimize with ``empty()``.
316 */
317 public static function ms_custom_registration_fields()
318 {
319 do_action("ws_plugin__s2member_before_ms_custom_registration_fields", get_defined_vars());
320
321 if(is_multisite() && is_main_site())
322 {
323 $_p = (!empty($_POST)) ? c_ws_plugin__s2member_utils_strings::trim_deep(stripslashes_deep($_POST)) : array();
324
325 echo '<input type="hidden" name="ws_plugin__s2member_registration" value="'.esc_attr(wp_create_nonce("ws-plugin--s2member-registration")).'" />'."\n";
326
327 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
328 do_action("ws_plugin__s2member_during_ms_custom_registration_fields_before", get_defined_vars());
329 unset($__refs, $__v);
330
331 if($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_names"])
332 {
333 echo '<div class="ws-plugin--s2member-custom-reg-field-divider-section"></div>'."\n";
334
335 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
336 do_action("ws_plugin__s2member_during_ms_custom_registration_fields_before_first_name", get_defined_vars());
337 unset($__refs, $__v);
338
339 echo '<label for="ws-plugin--s2member-custom-reg-field-first-name">'._x("First Name", "s2member-front", "s2member").' *</label>'."\n";
340 echo '<input type="text" aria-required="true" maxlength="100" autocomplete="off" name="ws_plugin__s2member_custom_reg_field_first_name" id="ws-plugin--s2member-custom-reg-field-first-name" class="ws-plugin--s2member-custom-reg-field form-control" value="'.esc_attr((string)(@$_p["ws_plugin__s2member_custom_reg_field_first_name"])).'" />'."\n";
341 echo '<br />'."\n";
342
343 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
344 do_action("ws_plugin__s2member_during_ms_custom_registration_fields_after_first_name", get_defined_vars());
345 unset($__refs, $__v);
346
347 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
348 do_action("ws_plugin__s2member_during_ms_custom_registration_fields_before_last_name", get_defined_vars());
349 unset($__refs, $__v);
350
351 echo '<label for="ws-plugin--s2member-custom-reg-field-last-name">'._x("Last Name", "s2member-front", "s2member").' *</label>'."\n";
352 echo '<input type="text" aria-required="true" maxlength="100" autocomplete="off" name="ws_plugin__s2member_custom_reg_field_last_name" id="ws-plugin--s2member-custom-reg-field-last-name" class="ws-plugin--s2member-custom-reg-field form-control" value="'.esc_attr((string)(@$_p["ws_plugin__s2member_custom_reg_field_last_name"])).'" />'."\n";
353 echo '<br />'."\n";
354
355 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
356 do_action("ws_plugin__s2member_during_ms_custom_registration_fields_after_last_name", get_defined_vars());
357 unset($__refs, $__v);
358 }
359 if($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_fields"])
360 if($fields_applicable = c_ws_plugin__s2member_custom_reg_fields::custom_fields_configured_at_level("auto-detection", "registration"))
361 foreach(json_decode($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_fields"], true) as $field)
362 {
363 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
364 do_action("ws_plugin__s2member_during_ms_custom_registration_fields_before_custom_fields", get_defined_vars());
365 unset($__refs, $__v);
366
367 if(in_array($field["id"], $fields_applicable))
368 {
369 $field_var = preg_replace("/[^a-z0-9]/i", "_", strtolower($field["id"]));
370 $field_id_class = preg_replace("/_/", "-", $field_var);
371
372 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
373 if(apply_filters("ws_plugin__s2member_during_ms_custom_registration_fields_during_custom_fields_display", true, get_defined_vars()))
374 {
375 if(!empty($field["section"]) && $field["section"] === "yes")
376 echo '<div class="ws-plugin--s2member-custom-reg-field-divider-section'.((!empty($field["sectitle"])) ? '-title' : '').'">'.((!empty($field["sectitle"])) ? $field["sectitle"] : '').'</div>';
377
378 echo '<label for="ws-plugin--s2member-custom-reg-field-'.esc_attr($field_id_class).'"'.((preg_match("/^(checkbox|pre_checkbox)$/", $field["type"])) ? ' style="display:none;"' : '').'>'.$field["label"].(($field["required"] === "yes") ? ' *' : '').'</label>'.((preg_match("/^(checkbox|pre_checkbox)$/", $field["type"])) ? '<br />' : '')."\n";
379 echo c_ws_plugin__s2member_custom_reg_fields::custom_field_gen(__FUNCTION__, $field, "ws_plugin__s2member_custom_reg_field_", "ws-plugin--s2member-custom-reg-field-", "ws-plugin--s2member-custom-reg-field", "", "", "", $_p, @$_p["ws_plugin__s2member_custom_reg_field_".$field_var], "registration");
380 echo '<br />'."\n";
381 }
382 unset($__refs, $__v);
383 }
384 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
385 do_action("ws_plugin__s2member_during_ms_custom_registration_fields_after_custom_fields", get_defined_vars());
386 unset($__refs, $__v);
387 }
388 if($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_opt_in"] && c_ws_plugin__s2member_list_servers::list_servers_integrated())
389 {
390 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
391 do_action("ws_plugin__s2member_during_ms_custom_registration_fields_before_opt_in", get_defined_vars());
392 unset($__refs, $__v);
393
394 echo '<label for="ws-plugin--s2member-custom-reg-field-opt-in">'."\n";
395 echo '<input type="checkbox" name="ws_plugin__s2member_custom_reg_field_opt_in" id="ws-plugin--s2member-custom-reg-field-opt-in" class="ws-plugin--s2member-custom-reg-field" value="1"'.(((empty($_p) && $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_opt_in"] == 1) || @$_p["ws_plugin__s2member_custom_reg_field_opt_in"]) ? ' checked="checked"' : '').' />'."\n";
396 echo $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_opt_in_label"]."\n";
397 echo '</label>'."\n";
398 echo '<br />'."\n";
399
400 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
401 do_action("ws_plugin__s2member_during_ms_custom_registration_fields_after_opt_in", get_defined_vars());
402 unset($__refs, $__v);
403 }
404 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
405 do_action("ws_plugin__s2member_during_ms_custom_registration_fields_after", get_defined_vars());
406 unset($__refs, $__v);
407 }
408 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
409 do_action("ws_plugin__s2member_after_ms_custom_registration_fields", get_defined_vars());
410 unset($__refs, $__v);
411 }
412 /**
413 * Adds Custom Fields to: `/wp-login.php?action=register`.
414 *
415 * @package s2Member\Custom_Reg_Fields
416 * @since 3.5
417 *
418 * @attaches-to ``add_action("register_form");``
419 *
420 * @todo Optimize with ``empty()``.
421 */
422 public static function custom_registration_fields()
423 {
424 do_action("ws_plugin__s2member_before_custom_registration_fields", get_defined_vars());
425
426 $_p = (!empty($_POST)) ? c_ws_plugin__s2member_utils_strings::trim_deep(stripslashes_deep($_POST)) : array();
427
428 echo '<input type="hidden" name="ws_plugin__s2member_registration" value="'.esc_attr(wp_create_nonce("ws-plugin--s2member-registration")).'" />'."\n";
429
430 $tabindex = 20; // Incremented tabindex starting with 20.
431
432 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
433 do_action("ws_plugin__s2member_during_custom_registration_fields_before", get_defined_vars());
434 unset($__refs, $__v);
435
436 if($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_password"])
437 {
438 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
439 do_action("ws_plugin__s2member_during_custom_registration_fields_before_user_pass", get_defined_vars());
440 unset($__refs, $__v);
441
442 echo '<p>'."\n";
443
444 echo '<label for="ws-plugin--s2member-custom-reg-field-user-pass1" title="'.esc_attr(_x("Please type your Password twice to confirm.", "s2member-front", "s2member")).'">'."\n";
445 echo '<span>'._x("Password (please type it twice)", "s2member-front", "s2member").' *</span><br />'."\n";
446 echo '<input type="password" aria-required="true" maxlength="100" autocomplete="off" name="ws_plugin__s2member_custom_reg_field_user_pass1" id="ws-plugin--s2member-custom-reg-field-user-pass1" class="ws-plugin--s2member-custom-reg-field form-control" value="'.format_to_edit((string)(@$_p["ws_plugin__s2member_custom_reg_field_user_pass1"])).'" tabindex="'.esc_attr(($tabindex = $tabindex + 10)).'" />'."\n";
447 echo '</label>'."\n";
448
449 echo '<label for="ws-plugin--s2member-custom-reg-field-user-pass2" title="'.esc_attr(_x("Please type your Password twice to confirm.", "s2member-front", "s2member")).'">'."\n";
450 echo '<input type="password" maxlength="100" autocomplete="off" name="ws_plugin__s2member_custom_reg_field_user_pass2" id="ws-plugin--s2member-custom-reg-field-user-pass2" class="ws-plugin--s2member-custom-reg-field form-control" value="'.format_to_edit((string)(@$_p["ws_plugin__s2member_custom_reg_field_user_pass2"])).'" tabindex="'.esc_attr(($tabindex = $tabindex + 10)).'" />'."\n";
451 echo '</label>'."\n";
452
453 echo '<div id="ws-plugin--s2member-custom-reg-field-user-pass-strength" class="ws-plugin--s2member-password-strength"><em>'._x("password strength indicator", "s2member-front", "s2member").'</em></div>'."\n";
454
455 echo '</p>'."\n";
456
457 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
458 do_action("ws_plugin__s2member_during_custom_registration_fields_after_user_pass", get_defined_vars());
459 unset($__refs, $__v);
460 }
461 if($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_names"])
462 {
463 echo '<div class="ws-plugin--s2member-custom-reg-field-divider-section"></div>'."\n";
464
465 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
466 do_action("ws_plugin__s2member_during_custom_registration_fields_before_first_name", get_defined_vars());
467 unset($__refs, $__v);
468
469 echo '<p>'."\n";
470 echo '<label for="ws-plugin--s2member-custom-reg-field-first-name">'."\n";
471 echo '<span>'._x("First Name", "s2member-front", "s2member").' *</span><br />'."\n";
472 echo '<input type="text" aria-required="true" maxlength="100" autocomplete="off" name="ws_plugin__s2member_custom_reg_field_first_name" id="ws-plugin--s2member-custom-reg-field-first-name" class="ws-plugin--s2member-custom-reg-field form-control" value="'.esc_attr((string)(@$_p["ws_plugin__s2member_custom_reg_field_first_name"])).'" tabindex="'.esc_attr(($tabindex = $tabindex + 10)).'" />'."\n";
473 echo '</label>'."\n";
474 echo '</p>'."\n";
475
476 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
477 do_action("ws_plugin__s2member_during_custom_registration_fields_after_first_name", get_defined_vars());
478 unset($__refs, $__v);
479
480 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
481 do_action("ws_plugin__s2member_during_custom_registration_fields_before_last_name", get_defined_vars());
482 unset($__refs, $__v);
483
484 echo '<p>'."\n";
485 echo '<label for="ws-plugin--s2member-custom-reg-field-last-name">'."\n";
486 echo '<span>'._x("Last Name", "s2member-front", "s2member").' *</span><br />'."\n";
487 echo '<input type="text" aria-required="true" maxlength="100" autocomplete="off" name="ws_plugin__s2member_custom_reg_field_last_name" id="ws-plugin--s2member-custom-reg-field-last-name" class="ws-plugin--s2member-custom-reg-field form-control" value="'.esc_attr((string)(@$_p["ws_plugin__s2member_custom_reg_field_last_name"])).'" tabindex="'.esc_attr(($tabindex = $tabindex + 10)).'" />'."\n";
488 echo '</label>'."\n";
489 echo '</p>'."\n";
490
491 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
492 do_action("ws_plugin__s2member_during_custom_registration_fields_after_last_name", get_defined_vars());
493 unset($__refs, $__v);
494 }
495 if($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_fields"])
496 if($fields_applicable = c_ws_plugin__s2member_custom_reg_fields::custom_fields_configured_at_level("auto-detection", "registration"))
497 {
498 $tabindex = /* Start tabindex at +9 ( +1 below ). */ $tabindex + 9;
499
500 foreach(json_decode($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_fields"], true) as $field)
501 {
502 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
503 do_action("ws_plugin__s2member_during_custom_registration_fields_before_custom_fields", get_defined_vars());
504 unset($__refs, $__v);
505
506 if /* Field applicable? */(in_array($field["id"], $fields_applicable))
507 {
508 $field_var = preg_replace("/[^a-z0-9]/i", "_", strtolower($field["id"]));
509 $field_id_class = preg_replace("/_/", "-", $field_var);
510
511 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
512 if(apply_filters("ws_plugin__s2member_during_custom_registration_fields_during_custom_fields_display", true, get_defined_vars()))
513 {
514 if /* Starts a new section? */(!empty($field["section"]) && $field["section"] === "yes")
515 echo '<div class="ws-plugin--s2member-custom-reg-field-divider-section'.((!empty($field["sectitle"])) ? '-title' : '').'">'.((!empty($field["sectitle"])) ? $field["sectitle"] : '').'</div>';
516
517 echo '<p>'."\n";
518 echo '<label for="ws-plugin--s2member-custom-reg-field-'.esc_attr($field_id_class).'">'."\n";
519 echo '<span'.((preg_match("/^(checkbox|pre_checkbox)$/", $field["type"])) ? ' style="display:none;"' : '').'>'.$field["label"].(($field["required"] === "yes") ? ' *' : '').'</span></label>'.((preg_match("/^(checkbox|pre_checkbox)$/", $field["type"])) ? '' : '<br />')."\n";
520 echo c_ws_plugin__s2member_custom_reg_fields::custom_field_gen(__FUNCTION__, $field, "ws_plugin__s2member_custom_reg_field_", "ws-plugin--s2member-custom-reg-field-", "ws-plugin--s2member-custom-reg-field", "", ($tabindex = $tabindex + 1), "", $_p, @$_p["ws_plugin__s2member_custom_reg_field_".$field_var], "registration");
521 echo '</p>'."\n";
522 }
523 unset($__refs, $__v);
524 }
525 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
526 do_action("ws_plugin__s2member_during_custom_registration_fields_after_custom_fields", get_defined_vars());
527 unset($__refs, $__v);
528 }
529 }
530 if($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_opt_in"] && c_ws_plugin__s2member_list_servers::list_servers_integrated())
531 {
532 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
533 do_action("ws_plugin__s2member_during_custom_registration_fields_before_opt_in", get_defined_vars());
534 unset($__refs, $__v);
535
536 echo '<p>'."\n";
537 echo '<label for="ws-plugin--s2member-custom-reg-field-opt-in">'."\n";
538 echo '<input type="checkbox" name="ws_plugin__s2member_custom_reg_field_opt_in" id="ws-plugin--s2member-custom-reg-field-opt-in" class="ws-plugin--s2member-custom-reg-field" value="1"'.(((empty($_p) && $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_opt_in"] == 1) || @$_p["ws_plugin__s2member_custom_reg_field_opt_in"]) ? ' checked="checked"' : '').' tabindex="'.esc_attr(($tabindex = $tabindex + 10)).'" />'."\n";
539 echo $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_opt_in_label"]."\n";
540 echo '</label>'."\n";
541 echo '</p>'."\n";
542
543 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
544 do_action("ws_plugin__s2member_during_custom_registration_fields_after_opt_in", get_defined_vars());
545 unset($__refs, $__v);
546 }
547 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
548 do_action("ws_plugin__s2member_during_custom_registration_fields_after", get_defined_vars());
549 unset($__refs, $__v);
550
551 foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
552 do_action("ws_plugin__s2member_after_custom_registration_fields", get_defined_vars());
553 unset($__refs, $__v);
554 }
555
556 /**
557 * Validates custom registration/profile fields.
558 *
559 * @param array $input Array of data input by a user.
560 * Array keys should match custom registration field variable names.
561 *
562 * @param array $fields_to_validate An array of custom registration/profile field configurations.
563 * Only include the configurations for those fields that need to be validated against the `$input`.
564 *
565 * @return array An array of errors; else an empty array.
566 * If there are errors, the array keys are the custom registration field variable names.
567 * Each array value is an error message with basic HTML markup.
568 */
569 public static function validation_errors($input, $fields_to_validate)
570 {
571 $input = (array)$input;
572 $fields_to_validate = (array)$fields_to_validate;
573
574 $errors = array(); // Initialize the array of errors.
575 $force_personal_emails = isset($GLOBALS['WS_PLUGIN__']['s2member']['o']['custom_reg_force_personal_emails'][0]) ? TRUE : FALSE;
576 $non_personal_email_users = '/^(?:'.implode('|', preg_split('/[\s;,]+/', preg_quote($GLOBALS['WS_PLUGIN__']['s2member']['o']['custom_reg_force_personal_emails'], '/'))).')@/i';
577
578 foreach($fields_to_validate as $_field)
579 {
580 if(empty($_field['id'])) continue; // Not applicable.
581 $_field_type = !empty($_field['type']) ? $_field['type'] : '';
582 $_field_var = preg_replace('/[^a-z0-9]/i', '_', strtolower($_field['id']));
583 $_field_id_class = preg_replace('/_/', '-', $_field_var);
584
585 $_field_required = !empty($_field['required']) && $_field['required'] === 'yes';
586 $_field_expects = !empty($_field['expected']) ? $_field['expected'] : '';
587 $_field_label = !empty($_field['label']) ? $_field['label'] : ucwords(str_replace('_', ' ', $_field_var));
588
589 switch($_field_type)
590 {
591 case 'text':
592 case 'textarea':
593 case 'checkbox':
594 case 'pre_checkbox':
595 case 'radios':
596 case 'select':
597 if(isset($input[$_field_var]) && !is_string($input[$_field_var]))
598 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Invalid data type. Expecting a string.', 's2member-front', 's2member').'</em>';
599 break;
600
601 case 'checkboxes':
602 case 'selects':
603 if(isset($input[$_field_var]) && !is_array($input[$_field_var]))
604 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Invalid data type. Expecting an array.', 's2member-front', 's2member').'</em>';
605 break;
606
607 default: // Default case handler for best security.
608 if(isset($input[$_field_var]) && !is_string($input[$_field_var]))
609 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Invalid data type. Expecting a string.', 's2member-front', 's2member').'</em>';
610 break;
611 }
612 if(empty($errors[$_field_var]) && $_field_required) switch($_field_type)
613 {
614 case 'text':
615 case 'textarea':
616 if(!isset($input[$_field_var]) || !is_string($input[$_field_var]) || !isset($input[$_field_var][0]))
617 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('This is a required field, please try again.', 's2member-front', 's2member').'</em>';
618 break;
619
620 case 'checkbox':
621 case 'pre_checkbox':
622 if(!isset($input[$_field_var]) || !is_string($input[$_field_var]) || !isset($input[$_field_var][0]))
623 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Required. This box must be checked.', 's2member-front', 's2member').'</em>';
624 break;
625
626 case 'checkboxes':
627 if(!isset($input[$_field_var]) || !is_array($input[$_field_var]) || empty($input[$_field_var]))
628 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Please check at least one of the boxes.', 's2member-front', 's2member').'</em>';
629 break;
630
631 case 'radios':
632 if(!isset($input[$_field_var]) || !is_string($input[$_field_var]))
633 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Please select one of the options.', 's2member-front', 's2member').'</em>';
634 break;
635
636 case 'select':
637 if(!isset($input[$_field_var]) || !is_string($input[$_field_var]) || !isset($input[$_field_var][0]))
638 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Please select one of the options.', 's2member-front', 's2member').'</em>';
639 break;
640
641 case 'selects':
642 if(!isset($input[$_field_var]) || !is_array($input[$_field_var]) || empty($input[$_field_var]))
643 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Please select at least one of the options.', 's2member-front', 's2member').'</em>';
644 break;
645
646 default: // Default case handler for best security.
647 if(!isset($input[$_field_var]) || !is_string($input[$_field_var]) || !isset($input[$_field_var][0]))
648 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('This is a required field, please try again.', 's2member-front', 's2member').'</em>';
649 break;
650 }
651 if(empty($errors[$_field_var]) && $_field_expects) switch($_field_type)
652 {
653 case 'text':
654 case 'textarea':
655 if(isset($input[$_field_var]) && is_string($input[$_field_var]) && isset($input[$_field_var][0])) switch($_field_expects)
656 {
657 case 'numeric-wp-commas':
658 if(!preg_match('/^[0-9][0-9\.,]*$/', $input[$_field_var]))
659 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Must be numeric (with or without decimals, commas allowed).', 's2member-front', 's2member').'</em>';
660 break;
661
662 case 'numeric':
663 if(!preg_match('/^[0-9][0-9\.]*$/', $input[$_field_var]))
664 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Must be numeric (with or without decimals, no commas).', 's2member-front', 's2member').'</em>';
665 break;
666
667 case 'integer':
668 if(!preg_match('/^[0-9]+$/', $input[$_field_var]))
669 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Must be an integer (a whole number, without any decimals).', 's2member-front', 's2member').'</em>';
670 break;
671
672 case 'integer-gt-0':
673 if(!preg_match('/^[0-9]+$/', $input[$_field_var]) || $input[$_field_var] <= 0)
674 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Must be an integer &gt; 0 (whole number, no decimals, greater than 0).', 's2member-front', 's2member').'</em>';
675 break;
676
677 case 'float':
678 if(!preg_match('/^[0-9\.]+$/', $input[$_field_var]) || !preg_match('/[0-9]/', $input[$_field_var]) || !preg_match('/\./', $input[$_field_var]))
679 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Must be a float (floating point number, decimals required).', 's2member-front', 's2member').'</em>';
680 break;
681
682 case 'float-gt-0':
683 if(!preg_match('/^[0-9\.]+$/', $input[$_field_var]) || !preg_match('/[0-9]/', $input[$_field_var]) || !preg_match('/\./', $input[$_field_var]) || $input[$_field_var] <= 0)
684 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Must be a float &gt; 0 (floating point number, decimals required, greater than 0).', 's2member-front', 's2member').'</em>';
685 break;
686
687 case 'date':
688 if(!preg_match('/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/', $input[$_field_var]))
689 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Must be a date (required date format: dd/mm/yyyy).', 's2member-front', 's2member').'</em>';
690 break;
691
692 case 'email':
693 if(!is_email($input[$_field_var]))
694 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Must be a valid email address.', 's2member-front', 's2member').'</em>';
695
696 else if($force_personal_emails && $non_personal_email_users && preg_match($non_personal_email_users, $input[$_field_var]))
697 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'.sprintf(_x('Please use a personal email address. Addresses like &lt;%s@&gt; are problematic.', 's2member-front', 's2member'), substr($input[$_field_var], 0, strpos($input[$_field_var], '@'))).'</em>';
698 break;
699
700 case 'url':
701 if(!preg_match('/^https?\:\/\/.+$/i', $input[$_field_var]))
702 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Must be a full URL (starting with http or https).', 's2member-front', 's2member').'</em>';
703 break;
704
705 case 'domain':
706 if(!preg_match('~^[0-9a-z][0-9a-z-]{0,61}[0-9a-z](\.[a-z]{2,}){1,2}$~i', $input[$_field_var]))
707 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Must be a domain name (domain name only, without http).', 's2member-front', 's2member').'</em>';
708 break;
709
710 case 'phone':
711 if(!preg_match('/^[0-9 ()\-]+$/', $input[$_field_var]) || strlen(preg_replace('/[^0-9]+/', '', $input[$_field_var])) !== 10)
712 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Must be a phone # (10 digits w/possible hyphens, spaces, brackets).', 's2member-front', 's2member').'</em>';
713 break;
714
715 case 'uszip':
716 if(!preg_match('/^[0-9]{5}(?:\-[0-9]{4})?$/', $input[$_field_var]))
717 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Must be a US zipcode (5-9 digits w/ possible hyphen).', 's2member-front', 's2member').'</em>';
718 break;
719
720 case 'cazip':
721 if(!preg_match('/^[0-9A-Z]{3} ?[0-9A-Z]{3}$/i', $input[$_field_var]))
722 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Must be a Canadian zipcode (6 alpha-numerics w/ possible space).', 's2member-front', 's2member').'</em>';
723 break;
724
725 case 'uczip':
726 if(!preg_match('/^[0-9]{5}(?:\-[0-9]{4})?$/', $input[$_field_var]) && !preg_match('/^[0-9A-Z]{3} ?[0-9A-Z]{3}$/i', $input[$_field_var]))
727 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Must be a zipcode (either a US or Canadian zipcode).', 's2member-front', 's2member').'</em>';
728 break;
729
730 case 'ukpostcode':
731 if(!preg_match('/^(([a-zA-Z]{1,2}[0-9][a-zA-Z0-9]?|ASCN|STHL|TDCU|BBND|[BFS]IQQ|PCRN|TKCA) ?[0-9][a-zA-Z]{2}|BFPO ?[0-9]{1,4}|(KY[0-9]|MSR|VG|AI)[ -]?[0-9]{4}|[a-zA-Z]{2} ?[0-9]{2}|GE ?CX|GIR ?0A{2}|SAN ?TA1)$/', $input[$_field_var]))
732 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Must be a valid UK postcode.', 's2member-front', 's2member').'</em>';
733 break;
734
735 default: // Handle others dynamically here.
736
737 if(preg_match('/^alphanumerics\-spaces\-punctuation\-[0-9]+(?:\-e)?$/', $_field_expects) && !preg_match('/^[a-z 0-9\/\\\\,.?:;"\'{}[\]\^|+=_()*&%$#@!`~\-]+$/i', $input[$_field_var]))
738 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Please use alphanumerics, spaces &amp; punctuation only.', 's2member-front', 's2member').'</em>';
739
740 else if(preg_match('/^alphanumerics\-spaces\-[0-9]+(?:\-e)?$/', $_field_expects) && !preg_match('/^[a-z 0-9]+$/i', $input[$_field_var]))
741 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Please use alphanumerics &amp; spaces only.', 's2member-front', 's2member').'</em>';
742
743 else if(preg_match('/^alphanumerics\-punctuation\-[0-9]+(?:\-e)?$/', $_field_expects) && !preg_match('/^[a-z0-9\/\\\\,.?:;"\'{}[\]\^|+=_()*&%$#@!`~\-]+$/i', $input[$_field_var]))
744 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Please use alphanumerics &amp; punctuation only (no spaces).', 's2member-front', 's2member').'</em>';
745
746 else if(preg_match('/^alphanumerics\-[0-9]+(?:\-e)?$/', $_field_expects) && !preg_match('/^[a-z0-9]+$/i', $input[$_field_var]))
747 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Please use alphanumerics only (no spaces/punctuation).', 's2member-front', 's2member').'</em>';
748
749 else if(preg_match('/^alphabetics\-[0-9]+(?:\-e)?$/', $_field_expects) && !preg_match('/^[a-z]+$/i', $input[$_field_var]))
750 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Please use alphabetics only (no digits/spaces/punctuation).', 's2member-front', 's2member').'</em>';
751
752 else if(preg_match('/^numerics\-[0-9]+(?:\-e)?$/', $_field_expects) && !preg_match('/^[0-9]+$/i', $input[$_field_var]))
753 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Please use numeric digits only.', 's2member-front', 's2member').'</em>';
754
755 else if(preg_match('/^(?:any|alphanumerics\-spaces\-punctuation|alphanumerics\-spaces|alphanumerics\-punctuation|alphanumerics|alphabetics|numerics)\-[0-9]+(?:\-e)?$/', $_field_expects))
756 {
757 $_field_expects_split = explode('-', $_field_expects);
758 $_field_expects_length = (int)$_field_expects_split[1];
759 $_field_expects_exact_length = !empty($_field_expects_split[2]) && $_field_expects_split[2] === 'e';
760
761 if($_field_expects_exact_length && strlen($input[$_field_var]) !== $_field_expects_length)
762 {
763 if($_field_expects_split[0] === 'numerics')
764 {
765 if($_field_expects_length === 1)
766 $_field_expects_digits_chars = _x('digit', 's2member-front', 's2member');
767 else $_field_expects_digits_chars = _x('digits', 's2member-front', 's2member');
768 }
769 else if($_field_expects_length === 1)
770 $_field_expects_digits_chars = _x('character', 's2member-front', 's2member');
771 else $_field_expects_digits_chars = _x('characters', 's2member-front', 's2member');
772
773 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'.sprintf(_x('Must be exactly %s %s.', 's2member-front', 's2member'), $_field_expects_length, $_field_expects_digits_chars).'</em>';
774 }
775 else if(strlen($input[$_field_var]) < $_field_expects_length)
776 {
777 if($_field_expects_split[0] === 'numerics')
778 {
779 if($_field_expects_length === 1)
780 $_field_expects_digits_chars = _x('digit', 's2member-front', 's2member');
781 else $_field_expects_digits_chars = _x('digits', 's2member-front', 's2member');
782 }
783 else if($_field_expects_length === 1)
784 $_field_expects_digits_chars = _x('character', 's2member-front', 's2member');
785 else $_field_expects_digits_chars = _x('characters', 's2member-front', 's2member');
786
787 $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'.sprintf(_x('Must be at least %s %s.', 's2member-front', 's2member'), $_field_expects_length, $_field_expects_digits_chars).'</em>';
788 }
789 }
790 break;
791 }
792 break;
793 }
794 }
795 unset($_field, $_field_type, $_field_var, $_field_id_class, $_field_required, $_field_label, $_field_expects, $_field_expects_split, $_field_expects_length, $_field_expects_exact_length, $_field_expects_digits_chars);
796
797 return apply_filters('c_ws_plugin__s2member_custom_reg_field_validation_errors', $errors, get_defined_vars());
798 }
799
800 /**
801 * Adds filters to `get_user_option` for Custom Field mapping
802 */
803 public static function add_filters_get_user_option()
804 {
805 if(!($fields = json_decode($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_fields"], TRUE)))
806 return; // Nothing to do in this case.
807
808 foreach($fields as $_field => $_config)
809 {
810 add_filter('get_user_option_' . $_field, 'c_ws_plugin__s2member_custom_reg_fields::do_filter_get_user_option', 20, 3);
811 add_filter('get_user_option_s2_' . $_field, 'c_ws_plugin__s2member_custom_reg_fields::do_filter_get_user_option', 20, 3);
812 }
813 unset($_field, $_config); // Housekeeping.
814 }
815
816 /**
817 * Filter for `get_user_option` queries. For Custom Field mapping.
818 *
819 * @param bool|string $what_wp_says The current field value, if WordPress has found one. Otherwise, FALSE.
820 * @param string $option_name Field slug/name being queried
821 * @param WP_User $user WP_User object related to the `get_user_option` query
822 *
823 * @return mixed
824 */
825 public static function do_filter_get_user_option($what_wp_says, $option_name, $user)
826 {
827 if($what_wp_says !== FALSE)
828 return $what_wp_says;
829
830 if(!($user instanceof WP_User) || !$user->exists())
831 return $what_wp_says;
832
833 if(!($user_fields = get_user_option("s2member_custom_fields", $user->ID)))
834 return $what_wp_says;
835
836 if(isset($user_fields[$option_name]))
837 return $user_fields[$option_name];
838
839 if(stripos($option_name, 's2_') === 0 && ($real_option_name = preg_replace('/^s2_/i', '', $option_name)))
840 if(isset($user_fields[$real_option_name]))
841 return $user_fields[$real_option_name];
842
843 return $what_wp_says;
844 }
845 }
846 }
847