PluginProbe
WP-Members Membership Plugin / trunk
WP-Members Membership Plugin vtrunk
trunk 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.4.2 3.4.5 3.4.6 3.4.7 3.4.8 3.4.9 3.4.9.1 3.4.9.2 3.4.9.3 3.4.9.4 3.4.9.5 3.4.9.6 3.4.9.7 3.5.0 3.5.1 3.5.2 3.5.3 3.5.4 All 34 releases
wp-members / includes / api / api-forms.php

api-forms.php in WP-Members Membership Plugin trunk, at includes/api/api-forms.php

882 lines 27.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WP-Members API Functions
4 *
5 * This file is part of the WP-Members plugin by Chad Butler
6 * You can find out more about this plugin at https://rocketgeek.com
7 * Copyright (c) 2006-2026 Chad Butler
8 * WP-Members(tm) is a trademark of butlerblog.com
9 *
10 * @package WP-Members
11 * @subpackage WP-Members API Functions
12 * @author Chad Butler
13 * @copyright 2006-2026
14 */
15
16 /**
17 * Gets the present state of a form.
18 *
19 * Currently uses the value contained in $wpmem->regchk
20 *
21 * @since 3.4.8.
22 */
23 function wpmem_get_form_state() {
24 global $wpmem;
25 return $wpmem->regchk;
26 }
27
28 if ( ! function_exists( 'wpmem_login_form' ) ):
29 /**
30 * Invokes a login form.
31 *
32 * Note: The original pluggable version of this function used a $page param
33 * and an array. This function should (1) no longer be considered pluggable
34 * and (2) should pass all arguments in the form if a single array. The previous
35 * methods are maintained for legacy reasons, but should be updated to apply
36 * to the current function documentation.
37 *
38 * @since 2.5.1
39 * @since 3.1.7 Now an alias for $wpmem->forms->login_form()
40 * @since 3.3.0 Added to API.
41 * @since 3.4.0 Main API function for displaying login form.
42 *
43 * @global object $wpmem
44 * @param array $args {
45 * Possible arguments for creating the form.
46 *
47 * @type string $id
48 * @type string $tag
49 * @type string $form
50 * @type string $redirect_to
51 * }
52 * @param array $arr {
53 * Maintained only for legacy reasons.
54 * The elements needed to generate the form (login|reset password|forgotten password).
55 *
56 * @type string $heading Form heading text.
57 * @type string $action The form action (login|pwdchange|pwdreset).
58 * @type string $button_text Form submit button text.
59 * @type array $inputs {
60 * The form input values.
61 *
62 * @type array {
63 *
64 * @type string $name The field label.
65 * @type string $type Input type.
66 * @type string $tag Input tag name.
67 * @type string $class Input tag class.
68 * @type string $div Div wrapper class.
69 * }
70 * }
71 * @type string $redirect_to Optional. URL to redirect to.
72 * }
73 * @return string $form The HTML for the form as a string.
74 */
75 function wpmem_login_form( $args = array(), $arr = false ) {
76 global $wpmem;
77
78 /*
79 // Convert legacy values.
80 if ( ( ! is_array( $args ) && is_array( $arr ) ) || ( is_array( $args ) && empty( $args ) ) ) {
81 $page = $args;
82 $args = $arr;
83 $args['page'] = $page;
84 }
85 $args['form'] = ( isset( $args['form'] ) ) ? $args['form'] : 'login';
86 // @todo Work on making this $wpmem->forms->do_login_form( $args );
87 return $wpmem->forms->login_form( $args );
88 */
89
90 return $wpmem->forms->do_shortform( 'login', $args );
91 }
92 endif;
93
94 /**
95 * Use the WP login form.
96 *
97 * @since 3.3.2
98 * @since 3.4.2 echo set to false by default.
99 *
100 * @global stdClass $wpmem
101 * @param array $args
102 */
103 function wpmem_wp_login_form( $args ) {
104 global $wpmem;
105 $args['echo'] = ( ! isset( $args['echo'] ) ) ? false : $args['echo'];
106 return $wpmem->forms->wp_login_form( $args );
107 }
108
109 /**
110 * Invokes a registration or user profile update form.
111 *
112 * @since 3.2.0
113 *
114 * @global object $wpmem
115 * @param array $args {
116 * Possible arguments for creating the form.
117 *
118 * @type string id
119 * @type string tag
120 * @type string form
121 * @type string product
122 * @type string include_fields
123 * @type string exclude_fields
124 * @type string redirect_to
125 * @type string heading
126 * }
127 * @return string $html
128 */
129 function wpmem_register_form( $args = 'new' ) {
130 global $wpmem;
131 return $wpmem->forms->register_form( $args );
132 }
133
134 /**
135 * Change Password Form.
136 *
137 * @since 3.3.0 Replaces wpmem_inc_changepassword().
138 * @since 3.3.0 Added $action argument.
139 *
140 * @global stdClass $wpmem The WP_Members object.
141 *
142 * @param string $action Determine if it is password change or reset.
143 * @return string $str The generated html for the change password form.
144 */
145 function wpmem_change_password_form() {
146 global $wpmem;
147 return $wpmem->forms->do_shortform( 'changepassword' );
148 }
149
150 /**
151 * Reset Password Form.
152 *
153 * @since 3.3.0 Replaced wpmem_inc_resetpassword().
154 *
155 * @global object $wpmem The WP_Members object.
156 * @return string $str The generated html fo the reset password form.
157 */
158 function wpmem_reset_password_form() {
159 global $wpmem;
160 return $wpmem->forms->do_shortform( 'resetpassword' );
161 }
162
163 /**
164 * Forgot Username Form.
165 *
166 * @since 3.3.0 Replaced wpmem_inc_forgotusername().
167 *
168 * @global object $wpmem The WP_Members object class.
169 * @return string $str The generated html for the forgot username form.
170 */
171 function wpmem_forgot_username_form() {
172 global $wpmem;
173 return $wpmem->forms->do_shortform( 'forgotusername' );
174 }
175
176 /**
177 * Resend confirmation link form.
178 *
179 * @since 3.5.0
180 *
181 * @global object $wpmem The WP_Members object class.
182 * @return string $str The generated html for the forgot username form.
183 */
184 function wpmem_resend_confirmation_form() {
185 global $wpmem;
186 return $wpmem->forms->do_shortform( 'reconfirm' );
187 }
188
189 /**
190 * Add registration fields to the native WP registration.
191 *
192 * @since 2.8.3
193 * @since 3.1.8 Added $process argument.
194 * @since 3.3.0 Moved to forms API.
195 *
196 * @global stdClass $wpmem
197 * @param string $process
198 */
199 function wpmem_wp_register_form( $process ) {
200 global $wpmem;
201 $process = ( ! $process || '' == $process ) ? 'register_wp' : $process;
202 $wpmem->forms->wp_register_form( $process );
203 }
204
205 /**
206 * Add registration fields to WooCommerce registration.
207 *
208 * As of WooCommerce 3.0, the WC registration process no longer includes the
209 * WP register_form action hook. It only includes woocommerce_register_form.
210 * In previous versions, WP-Members hooked to register_form for both WP and
211 * WC registration. To provide backward compatibility with users who may
212 * continue to use updated WP-Members with pre-3.0 WooCommerce, this function
213 * checks for WC version and if it is older than 3.0 it will ignore adding
214 * the WP-Members form fields as they would have already been added when the
215 * register_form action hook fired.
216 *
217 * @since 3.1.8
218 * @since 3.3.0 Moved to forms API.
219 *
220 * @global stdClass $woocommerce
221 */
222 function wpmem_woo_register_form() {
223 if ( class_exists( 'WooCommerce' ) ) {
224 global $woocommerce;
225 if ( version_compare( $woocommerce->version, '3.0', ">=" ) ) {
226 wpmem_wp_register_form( 'woo' );
227 }
228 }
229 }
230
231 /**
232 * Alias for $wpmem->create_form_field().
233 *
234 * @since 3.1.2
235 * @since 3.2.0 Accepts wpmem_create_formfield() arguments.
236 *
237 * @global object $wpmem The WP_Members object class.
238 * @param string|array $args {
239 * @type string $name (required) The field meta key.
240 * @type string $type (required) The field HTML type (url, email, image, file, checkbox, text, textarea, password, hidden, select, multiselect, multicheckbox, radio).
241 * @type string $value (optional) The field's value (can be a null value).
242 * @type string $compare (optional) Compare value.
243 * @type string $class (optional) Class identifier for the field.
244 * @type boolean $required (optional) If a value is required default: true).
245 * @type string $delimiter (optional) The field delimiter (pipe or comma, default: | ).
246 * @type string $placeholder (optional) Defines the placeholder attribute.
247 * @type string $pattern (optional) Adds a regex pattern to the field (HTML5).
248 * @type string $title (optional) Defines the title attribute.
249 * @type string $min (optional) Adds a min attribute (HTML5).
250 * @type string $max (optional) Adds a max attribute (HTML5).
251 * @type string $rows (optional) Adds rows attribute to textarea.
252 * @type string $cols (optional) Adds cols attribute to textarea.
253 * }
254 * @param string $type The field type.
255 * @param string $value The default value for the field.
256 * @param string $valtochk Optional for comparing the default value of the field.
257 * @param string $class Optional for setting a specific CSS class for the field.
258 * @return string The HTML of the form field.
259 */
260 //function wpmem_form_field( $args ) {
261 function wpmem_form_field( $name, $type=null, $value=null, $valtochk=null, $class='textbox' ) {
262 global $wpmem;
263 if ( is_array( $name ) ) {
264 $args = $name;
265 } else {
266 $args = array(
267 'name' => $name,
268 'type' => $type,
269 'value' => $value,
270 'compare' => $valtochk,
271 'class' => $class,
272 );
273 }
274 return $wpmem->forms->create_form_field( $args );
275 }
276
277 /**
278 * Wrapper for $wpmem->create_form_label().
279 *
280 * @since 3.1.7
281 *
282 * @global object $wpmem
283 * @param array $args {
284 * @type string $meta_key
285 * @type string $label
286 * @type string $type
287 * @type string $id (optional)
288 * @type string $class (optional)
289 * @type string $required (optional)
290 * @type string $req_mark (optional)
291 * }
292 * @return string The HTML of the form label.
293 */
294 function wpmem_form_label( $args ) {
295 global $wpmem;
296 return $wpmem->forms->create_form_label( $args );
297 }
298
299 /**
300 * Wrapper to get form fields.
301 *
302 * @since 3.1.1
303 * @since 3.1.5 Checks if fields array is set or empty before returning.
304 * @since 3.1.7 Added wpmem_form_fields filter.
305 * @since 3.3.9 load_fields() moved to forms object class.
306 * @since 3.5.0 Add default for $tag to avoid PHP 8.2 issues.
307 *
308 * @global object $wpmem The WP_Members object.
309 * @param string $tag The action being used (default: null).
310 * @param string $form The form being generated.
311 * @return array $fields The form fields.
312 */
313 function wpmem_fields( $tag = 'all', $form = 'default' ) {
314 global $wpmem;
315
316 // @todo Review for removal.
317 $tag = $wpmem->convert_tag( $tag );
318
319 // Change in 3.5.0, always load fields here, regardless of whether they are loaded or not. That way it resets for the requested instance.
320 $wpmem->forms->load_fields( $tag );
321
322 /**
323 * Filters the fields array.
324 *
325 * @since 3.1.7
326 * @since 3.3.2 Change object var and return.
327 * @since 3.4.2 Added $form parameter.
328 *
329 * @param array $wpmem->fields
330 * @param string $tag (optional)
331 * @param string $form (optional)
332 */
333 $wpmem->fields = apply_filters( 'wpmem_fields', $wpmem->fields, $tag, $form );
334
335 return $wpmem->fields;
336 }
337
338 /**
339 * Sanitizes classes passed to the WP-Members form building functions.
340 *
341 * This generally uses just sanitize_html_class() but allows for
342 * whitespace so multiple classes can be passed (such as "regular-text code").
343 * This is an API wrapper for WP_Members_Forms::sanitize_class().
344 *
345 * @since 3.2.9
346 * @since 3.4.0 Now an alias for rktgk_sanitize_class().
347 *
348 * @global object $wpmem
349 *
350 * @param string $class
351 * @return string sanitized_class
352 */
353 function wpmem_sanitize_class( $class ) {
354 return rktgk_sanitize_class( $class );
355 }
356
357 /**
358 * Sanitizes the text in an array.
359 *
360 * This is an API wrapper for WP_Members_Forms::sanitize_array().
361 *
362 * @since 3.2.9
363 * @since 3.3.7 Added optional $type
364 * @since 3.4.0 Now an alias for rktgk_sanitize_array().
365 *
366 * @global object $wpmem
367 *
368 * @param array $data
369 * @param string $type The data type integer|int (default: false)
370 * @return array $data
371 */
372 function wpmem_sanitize_array( $data, $type = false ) {
373 return rktgk_sanitize_array( $data, $type );
374 }
375
376 /**
377 * A multi use sanitization function.
378 *
379 * @since 3.3.0
380 * @since 3.4.2 Added text, url, array, class as accepted $type
381 *
382 * @global object $wpmem
383 *
384 * @param string $data
385 * @param string $type (text|array|multiselect|multicheckbox|textarea|email|file|image|int|integer|number|url|class) Default:text
386 * @return string $sanitized_data
387 */
388 function wpmem_sanitize_field( $data, $type = 'text' ) {
389 return rktgk_sanitize_field( $data, $type );
390 }
391
392 /**
393 * Generate a form nonce.
394 *
395 * @since 3.3.0
396 *
397 * @param string $nonce
398 * @param boolean $echo
399 * @return string The nonce.
400 */
401 function wpmem_form_nonce( $nonce, $echo = false ) {
402 $form = ( 'update' == $nonce || 'register' == $nonce ) ? 'longform' : 'shortform';
403 return wp_nonce_field( 'wpmem_' . $form . '_nonce', '_wpmem_' . $nonce . '_nonce', true, $echo );
404 }
405
406 // @todo Experimental
407 /**
408 * Create WP-Members fields set for woo checkout.
409 *
410 * @since 3.3.4
411 *
412 * @param array $checkout_fields
413 */
414 function wpmem_woo_checkout_fields( $checkout_fields = false ) {
415
416 $fields = wpmem_fields();
417
418 if ( ! $checkout_fields ) {
419 $checkout_fields = WC()->checkout()->checkout_fields;
420 }
421
422 // Get saved checkout field settings.
423 $wpmembers_wcchkout = get_option( 'wpmembers_wcchkout_fields' );
424
425 foreach ( $fields as $meta_key => $field ) {
426
427 if ( $wpmembers_wcchkout ) {
428
429 if ( ! in_array( $meta_key, $wpmembers_wcchkout ) ) {
430 unset( $fields[ $meta_key ] );
431 }
432
433 } else {
434
435 if ( 1 != $fields[ $meta_key ]['register'] ) {
436 unset( $fields[ $meta_key ] );
437 } else {
438 if ( isset( $checkout_fields['billing'][ $meta_key ] ) ) {
439 unset( $fields[ $meta_key ] );
440 }
441 if ( isset( $checkout_fields['shipping'][ $meta_key ] ) ) {
442 unset( $fields[ $meta_key ] );
443 }
444 if ( isset( $checkout_fields['account'][ $meta_key ] ) ) {
445 unset( $fields[ $meta_key ] );
446 }
447 if ( isset( $checkout_fields['order'][ $meta_key ] ) ) {
448 unset( $fields[ $meta_key ] );
449 }
450 }
451
452 // @todo For now, remove any unsupported field types.
453 if ( 'hidden' == $field['type'] || 'image' == $field['type'] || 'file' == $field['type'] || 'membership' == $field['type'] ) {
454 unset( $fields[ $meta_key ] );
455 }
456 }
457 }
458 unset( $fields['username'] );
459 unset( $fields['password'] );
460 unset( $fields['confirm_password'] );
461 unset( $fields['confirm_email'] );
462 unset( $fields['user_email'] );
463 unset( $fields['first_name'] );
464 unset( $fields['last_name'] );
465
466 return $fields;
467 }
468
469 /**
470 * Adds WP-Members custom fields to woo checkout.
471 *
472 * @since 3.3.4
473 *
474 * @param array $checkout_fields
475 */
476 function wpmem_woo_checkout_form( $checkout_fields ) {
477 global $wpmem;
478 $fields = wpmem_woo_checkout_fields( $checkout_fields );
479
480 /**
481 * Filters the initial WC priority of the WP-members added fields.
482 *
483 * @since 3.3.7
484 *
485 * @param int
486 */
487 $priority = apply_filters( 'wpmem_wc_checkout_field_priority_seed', 10 );
488
489 foreach ( $fields as $meta_key => $field ) {
490
491 // All field types have these.
492 $checkout_fields['order'][ $meta_key ] = array(
493 'type' => $fields[ $meta_key ]['type'],
494 'label' => ( 'tos' == $meta_key ) ? $wpmem->forms->get_tos_link( $field, 'woo' ) : wpmem_get_field_label( $meta_key ),
495 'required' => $fields[ $meta_key ]['required'],
496 'priority' => $priority,
497 );
498
499 // If there is a placeholder.
500 if ( isset( $fields[ $meta_key ]['placeholder'] ) ) {
501 $checkout_fields['order'][ $meta_key ]['placeholder'] = $fields[ $meta_key ]['placeholder'];
502 }
503
504 if ( 'select' == wpmem_get_field_type( $meta_key ) ) {
505 $checkout_fields['order'][ $meta_key ]['options'] = wpmem_get_field_options( $meta_key );
506 }
507
508 // Increment priority
509 $priority = $priority + 10;
510 }
511 return $checkout_fields;
512 }
513
514 /**
515 * Saves WP-Members custom fields for woo checkout.
516 *
517 * @since 3.3.4
518 *
519 * @param int $order_id
520 */
521 function wpmem_woo_checkout_update_meta( $order_id ) {
522
523 // Get user id from order.
524 $order = wc_get_order( $order_id );
525 $user_id = $order->get_user_id();
526
527 $checkout_fields = WC()->checkout()->checkout_fields;
528 $fields = wpmem_fields();
529 foreach ( $fields as $meta_key => $field ) {
530 if ( isset( $checkout_fields['order'][ $meta_key ] ) && isset( $_POST[ $meta_key ] ) ) {
531 switch ( $fields[ $meta_key ]['type'] ) {
532 case 'checkbox':
533 update_user_meta( $user_id, $meta_key, $field['checked_value'] );
534 break;
535 case 'textarea':
536 update_user_meta( $user_id, $meta_key, sanitize_textarea_field( $_POST[ $meta_key ] ) );
537 break;
538 case 'multicheckbox':
539 case 'multiselect':
540 update_user_meta( $user_id, $meta_key, wpmem_sanitize_array( $_POST[ $meta_key ] ) );
541 break;
542 case 'membership':
543 wpmem_set_user_product( wpmem_sanitize_array( $_POST[ $meta_key ] ), $user_id );
544 break;
545 default:
546 if ( 'user_url' == $meta_key ) {
547 wp_update_user( array( 'ID' => $user_id, 'user_url' => sanitize_text_field( $_POST[ $meta_key ] ) ) );
548 } else {
549 update_user_meta( $user_id, $meta_key, sanitize_text_field( $_POST[ $meta_key ] ) );
550 }
551 break;
552 }
553 }
554 }
555 }
556
557 /**
558 * Adds WP-Members custom fields to woo profile update.
559 *
560 * @since 3.5.0
561 *
562 * @param array $checkout_fields
563 */
564 function wpmem_woo_edit_account_form() {
565 $fields = wpmem_woo_edit_account_fields();
566 foreach ( $fields as $meta_key => $field ) {
567 $args = array(
568 'type' => wpmem_get_field_type( $meta_key ),
569 'label' => wpmem_get_field_label( $meta_key ),
570 'required' => wpmem_is_field_required( $meta_key )
571 );
572 if ( 'checkbox' == wpmem_get_field_type( $meta_key ) ) {
573 $args['checked_value'] = $field['checked_value'];
574 }
575 if ( 'select' == wpmem_get_field_type( $meta_key ) || 'radio' == wpmem_get_field_type( $meta_key ) ) {
576 $args['options'] = wpmem_get_field_options( $meta_key );
577 }
578 $value = esc_attr( wpmem_get_user_meta( get_current_user_id(), $meta_key ) );
579 woocommerce_form_field( $meta_key, $args, $value );
580 }
581 }
582
583 function wpmem_woo_edit_account_fields() {
584 $fields = wpmem_fields();
585 // Get saved checkout field settings.
586 $wpmembers_wcupdate = get_option( 'wpmembers_wcupdate_fields' );
587 if ( $wpmembers_wcupdate ) {
588 foreach ( $fields as $meta => $field ) {
589 if ( in_array( $meta, $wpmembers_wcupdate ) ) {
590 $return_fields[ $meta ] = $field;
591 }
592 }
593 return $return_fields;
594 }
595 }
596
597 /**
598 * Sets required fields for WooCommerce Edit Account form.
599 *
600 * @since 3.5.1
601 */
602 function wpmem_woo_edit_account_required( $required_fields ) {
603 $fields = wpmem_fields();
604 // Get saved checkout field settings.
605 $wpmembers_wcupdate = get_option( 'wpmembers_wcupdate_fields' );
606 if ( $wpmembers_wcupdate ) {
607 foreach ( $fields as $meta => $field ) {
608 if ( in_array( $meta, $wpmembers_wcupdate ) && wpmem_is_field_required( $meta ) ) {
609 $required_fields[ $meta ] = wpmem_get_field_label( $meta );
610 }
611 }
612 }
613 return $required_fields;
614 }
615
616 /**
617 * Saves WooCommerce Edit Account custom field values on update.
618 *
619 * @since 3.5.1
620 */
621 function wpmem_woo_edit_account_save( $user_id ) {
622 $fields = wpmem_fields();
623 // Get saved checkout field settings.
624 $wpmembers_wcupdate = get_option( 'wpmembers_wcupdate_fields' );
625 if ( $wpmembers_wcupdate ) {
626 foreach ( $fields as $meta => $field ) {
627 if ( in_array( $meta, $wpmembers_wcupdate ) ) {
628 $field_type = wpmem_get_field_type( $meta );
629 if ( 'multiselect' == $field_type || 'multicheckbox' == $field_type ) {
630 $value = wpmem_get( $meta, false );
631 $value = implode( $field['delimiter'], $value );
632 $sanitized_value = sanitize_text_field( $value );
633 } else {
634 $sanitized_value = wpmem_get_sanitized( $meta, 'post', $field_type );
635 }
636 update_user_meta( $user_id, $meta, $sanitized_value );
637 }
638 }
639 }
640 }
641
642 /**
643 * Handle custom fields for WooCommerce.
644 *
645 * @since Unknown
646 *
647 * @param string $field
648 * @param string $key The field's meta key.
649 * @param array $args
650 * @param string $value
651 * @param string $field
652 */
653 function wpmem_form_field_wc_custom_field_types( $field, $key, $args, $value ) {
654
655 $wpmem_fields = wpmem_fields();
656 /**
657 * @type string $name (required) The field meta key.
658 * @type string $type (required) The field HTML type (url, email, image, file, checkbox, text, textarea, password, hidden, select, multiselect, multicheckbox, radio).
659 * @type string $value (optional) The field's value (can be a null value).
660 * @type string $compare (optional) Compare value.
661 * @type string $class (optional) Class identifier for the field.
662 * @type boolean $required (optional) If a value is required default: true).
663 * @type string $delimiter (optional) The field delimiter (pipe or comma, default: | ).
664 * @type string $placeholder (optional) Defines the placeholder attribute.
665 * @type string $pattern (optional) Adds a regex pattern to the field (HTML5).
666 * @type string $title (optional) Defines the title attribute.
667 * @type string $min (optional) Adds a min attribute (HTML5).
668 * @type string $max (optional) Adds a max attribute (HTML5).
669 * @type string $rows (optional) Adds rows attribute to textarea.
670 * @type string $cols (optional) Adds cols attribute to textarea.
671 */
672
673 // Let's only mess with WP-Members fields (in case another checkout fields plugin is used).
674 if ( array_key_exists( $key, $wpmem_fields ) ) {
675
676 switch ( $wpmem_fields[ $key ]['type'] ) {
677
678 case 'checkbox':
679 // If it is a checkbox that should be checked by default.
680 if ( $wpmem_fields[ $key ]['checked_default'] && ! is_user_logged_in() && ! $_POST ) {
681 $field = str_replace( '<input type="checkbox"', '<input type="checkbox" checked ', $field );
682 }
683 break;
684
685 case 'radio':
686 case 'select':
687 case 'multiselect':
688 case 'multicheckbox':
689 $field_args = array(
690 'name' => $key,
691 'type' => $wpmem_fields[ $key ]['type'],
692 'required' => $wpmem_fields[ $key ]['required'],
693 'delimiter' => $wpmem_fields[ $key ]['delimiter'],
694 'value' => $wpmem_fields[ $key ]['values'],
695 );
696 $value = wpmem_get_user_meta( get_current_user_id(), $key );
697
698 // Two possibilities: delimited string (WP-Members) or serialized (WooCommerce)
699 if ( is_array( $value ) ) {
700 $value = implode( $wpmem_fields[ $key ]['delimiter'], $value );
701 }
702
703 if ( is_user_logged_in() ) {
704 $field_args['compare'] = esc_attr( $value );
705 }
706
707 $field_html = wpmem_form_field( $field_args );
708 $field_html = str_replace( 'class="' . $wpmem_fields[ $key ]['type'] . '"', 'class="' . esc_attr( $wpmem_fields[ $key ]['type'] ) . '" style="display:initial;"', $field_html );
709 $field = '<p class="form-row ' . implode( ' ', $args['class'] ) .'" id="' . esc_attr( $key ) . '_field">
710 <label for="' . esc_attr( $key ) . '" class="' . implode( ' ', $args['label_class'] ) .'">' . esc_html( $args['label'] ) . ( ( 1 == $wpmem_fields[ $key ]['required'] ) ? '&nbsp;<abbr class="required" title="required">*</abbr>' : '' ) . '</label>';
711 $field .= $field_html;
712 $field .= '</p>';
713 break;
714 }
715 }
716 return $field;
717 }
718
719 function wpmem_woo_reg_validate( $username, $email, $errors ) {
720
721 $fields = wpmem_woo_checkout_fields();
722
723 unset( $fields['username'] );
724 unset( $fields['password'] );
725 unset( $fields['confirm_password'] );
726 unset( $fields['user_email'] );
727 unset( $fields['first_name'] );
728 unset( $fields['last_name'] );
729
730 foreach ( $fields as $key => $field_args ) {
731 if ( 1 == $field_args['required'] && empty( $_POST[ $key ] ) ) {
732 $message = sprintf( wpmem_get_text( 'woo_reg_required_field' ), '<strong>' . esc_html( $field_args['label'] ) . '</strong>' );
733 $errors->add( $key, $message );
734 }
735 }
736 return $errors;
737 }
738
739 function wpmem_is_reg_form_showing() {
740 global $wpmem;
741 return ( true == $wpmem->forms->is_reg_form_showing() ) ? true : false;
742 }
743
744 /**
745 * Returns the display value of certain field types.
746 *
747 * Some WP-Members field types may have a different "saved" value and
748 * "displayed" value. This function provides a simple way to get the displayed
749 * value for a field based on the saved value.
750 *
751 * @since 3.4.0
752 * @since 3.4.2 Cleanup, completed support for all field types.
753 *
754 * @param string $field_meta The meta key for the requested field.
755 * @param string $value The value to convert (empty for checkbox).
756 * @param boolean $echo Whether to echo or return the value (default: false).
757 * @return string
758 */
759 function wpmem_field_display_value( $field_meta, $value = '', $echo = false ) {
760 $fields = wpmem_fields();
761 $type = $fields[ $field_meta ]['type'];
762
763 $display_value = ( 'checkbox' == $type ) ? $fields[ $field_meta ]['label'] : $fields[ $field_meta ]['options'][ $value ];
764
765 /**
766 * Filter the value.
767 *
768 * @since 3.4.0
769 *
770 * @param string $display_value
771 * @param string $field_meta
772 * @param string $type
773 */
774 $display_value = apply_filters( 'wpmem_' . $type . '_field_display', $display_value, $field_meta, $type );
775 if ( $echo ) {
776 echo $display_value;
777 } else {
778 return $display_value;
779 }
780 }
781
782 function wpmem_checkbox_field_display( $field_meta, $echo = false ) {
783 return wpmem_field_display_value( $field_meta, '', $echo );
784 }
785
786 function wpmem_select_field_display( $field_meta, $value, $echo = false ) {
787 return wpmem_field_display_value( $field_meta, $value, $echo );
788 }
789
790 /**
791 * Returns the URL for a file field.
792 *
793 * @since 3.5.1
794 */
795 function wpmem_get_file_field_url( $field_meta, $user_id ) {
796 if ( wpmem_is_file_field( $field_meta ) ) {
797 return wp_get_attachment_url( get_user_meta( $user_id, $field_meta, true ) );
798 } else {
799 return false;
800 }
801 }
802
803 /**
804 * Returns the field type.
805 *
806 * @since 3.5.1
807 */
808 function wpmem_get_field_type( $field_meta ) {
809 $fields = wpmem_fields();
810 return $fields[ $field_meta ]['type'];
811 }
812
813 /**
814 * Returns true if file type field
815 *
816 * @since 3.5.1
817 */
818 function wpmem_is_file_field( $field_meta ) {
819 $field_type = wpmem_get_field_type( $field_meta );
820 return ( 'file' == $field_type || 'image' == $field_type ) ? true : false;
821 }
822
823 /**
824 * Returns the field label.
825 *
826 * @since 3.5.1
827 * @since 3.5.4 Supports label_href and returns sanitized result.
828 */
829 function wpmem_get_field_label( $meta_key ) {
830 $fields = wpmem_fields();
831
832 $args = $fields[ $meta_key ];
833
834 // Adjust placeholders.
835
836 if ( isset( $args['label_href'] ) ) {
837
838 $label_text = str_replace( '%', '%s', esc_html__( $args['label'], 'wp-members' ) );
839 /**
840 * Filters the anchor tag.
841 *
842 * @since 3.5.3
843 *
844 * @param array $atts An array of attributes for the <a> tag.
845 * @param string $meta_key Meta key of the field.
846 */
847 $tag_atts = apply_filters( 'wpmem_form_label_link', array (
848 'href' => $args['label_href'],
849 'target' => '_blank',
850 ), $meta_key );
851
852 $anchor_tag = "<a ";
853 foreach ( $tag_atts as $attribute => $value ) {
854 $anchor_tag .= ( '' != $value ) ? esc_attr( $attribute ) . '="' . esc_attr( $value ) . '" ' : esc_attr( $attribute ) . ' ';
855 }
856 $anchor_tag .= ">";
857
858 return sprintf( $label_text, $anchor_tag, '</a>' );
859 } else {
860 return esc_html__( $args['label'], 'wp-members' );
861 }
862 }
863
864 /**
865 * Checks if field is required.
866 *
867 * @since 3.5.1
868 */
869 function wpmem_is_field_required( $meta_key ) {
870 $fields = wpmem_fields();
871 return ( $fields[ $meta_key ]['required'] ) ? true : false;
872 }
873
874 /**
875 * Gets select field options.
876 *
877 * @since 3.5.1
878 */
879 function wpmem_get_field_options( $meta_key ) {
880 $fields = wpmem_fields();
881 return $fields[ $meta_key ]['options'];
882 }