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-users.php

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

1,321 lines 39.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WP-Members User 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 * Checks if a user exists.
18 *
19 * @since 3.2.5
20 *
21 * @param $user_id
22 * @return boolean
23 */
24 function wpmem_is_user( $user_id ) {
25 $user = get_userdata( $user_id );
26 return ( $user ) ? true : false;
27 }
28
29 /**
30 * Returns the current user's current role.
31 *
32 * Note that users may have more than one role. This returns
33 * whatever the internal pointer is set to. Usually, this will
34 * be the first element in the array, but not always.
35 * @see: https://www.php.net/manual/en/function.current.php
36 *
37 * @since 3.3.0
38 *
39 * @param int $user_id
40 * @param boolean $all If true, all roles as an array; if false, just the current role.
41 * @return mixed If the user is set and has roles, the current user role, otherwise false.
42 */
43 function wpmem_get_user_role( $user_id = false, $all = false ) {
44 $user = ( $user_id ) ? get_userdata( $user_id ) : wp_get_current_user();
45 $role = ( ! $all ) ? current( $user->roles ) : $user->roles;
46 return ( $user ) ? $role : false;
47 }
48
49 /**
50 * Checks if user has a particular role.
51 *
52 * Utility function to check if a given user has a specific role. Users can
53 * have multiple roles assigned, so it checks the role array rather than using
54 * the incorrect method of current_user_can( 'role_name' ). The function can
55 * check the role of the current user (default) or a specific user (if $user_id
56 * is passed).
57 *
58 * @since 3.1.1
59 * @since 3.1.6 Include accepting an array of roles to check.
60 * @since 3.1.9 Return false if user is not logged in.
61 * @since 3.2.0 Change return false to not logged in AND no user id.
62 * @since 3.4.5 $current_user no longer necessary.
63 * @since 3.4.6 $wpmem global is no longer necessary.
64 *
65 * @param string|array $role Slug or array of slugs of the role being checked.
66 * @param int $user_id ID of the user being checked (optional).
67 * @return boolean $has_role True if user has the role, otherwise false.
68 */
69 function wpmem_user_has_role( $role, $user_id = false ) {
70 if ( ! is_user_logged_in() && ! $user_id ) {
71 return false;
72 }
73 $has_role = false;
74 $user = ( $user_id ) ? get_userdata( $user_id ) : wp_get_current_user();
75 if ( is_array( $role ) ) {
76 foreach ( $role as $r ) {
77 if ( in_array( $r, $user->roles ) ) {
78 return true;
79 }
80 }
81 } else {
82 return ( in_array( $role, $user->roles ) ) ? true : $has_role;
83 }
84 }
85
86 /**
87 * Gets user meta.
88 *
89 * It may seem like WP already has this feature. And it does. But most user meta
90 * are single, and WP's get_user_meta() defaults to "false" for the $single
91 * argument. This function provides a shorthand that assumes a string value for
92 * the meta result and drops the $single argument.
93 *
94 * @since 3.3.0
95 * @since 3.5.2 Include the possibility of serialized data with WooCommerce.
96 *
97 * @param int $user_id
98 * @param string $meta_key
99 * @return string $result
100 */
101 function wpmem_get_user_meta( $user_id, $meta_key ) {
102 $value = get_user_meta( $user_id, $meta_key, true );
103 // Check if it is serialized (WooCommerce)
104 if ( is_array( $value ) ) {
105 // Does it have a delimiter?
106 $fields = wpmem_fields('all');
107 $value = implode( $fields[ $meta_key ]['delimiter'], $value );
108 }
109 /**
110 * Filter the result returned from wpmem_get_user_meta()
111 *
112 * @since 3.5.2
113 *
114 * @param $meta_value
115 * @param $user_id
116 * @param $meta_key
117 * @return $meta_value
118 */
119 return apply_filters( 'wpmem_get_user_meta', $value, $user_id, $meta_key );
120 }
121
122 /**
123 * Get a user meta checkbox label as value if checked.
124 *
125 * @since 3.4.2
126 *
127 * @param int $user_id
128 * @param string $meta_key
129 * @return string $result
130 */
131 function wpmem_get_user_meta_checkbox( $user_id, $meta_key ) {
132 $value = wpmem_get_user_meta( $user_id, $meta_key );
133 return wpmem_checkbox_field_display( $value );
134 }
135
136 /**
137 * Get a user meta select (dropdown) value.
138 *
139 * @since 3.4.2
140 *
141 * @param int $user_id
142 * @param string $meta_key
143 * @return string $result
144 */
145 function wpmem_get_user_meta_select( $user_id, $meta_key ) {
146 $value = wpmem_get_user_meta( $user_id, $meta_key );
147 return wpmem_select_field_display( $meta_key, $value );
148 }
149
150 /**
151 * Get a user meta radio value.
152 * Alias of wpmem_get_user_meta_select() for radio field type.
153 *
154 * @since 3.4.2
155 *
156 * @param int $user_id
157 * @param string $meta_key
158 * @return string $result
159 */
160 function wpmem_get_user_meta_radio( $user_id, $meta_key ) {
161 return wpmem_get_user_meta_select( $user_id, $meta_key );
162 }
163
164 /**
165 * Returns the meta value for a requested mutli-checkbox or multi-select
166 * field for a requested user ID.
167 *
168 * @since 3.4.2
169 *
170 * @param int $user_id
171 * @param string $field_meta
172 * @param string $return_type (string|array default:string)
173 * @return string
174 */
175 function wpmem_get_user_meta_multi( $user_id, $field_meta, $return_type = "string" ) {
176 $fields = wpmem_fields();
177 $value = wpmem_get_user_meta( $user_id, $field_meta );
178 $array = ( '' != $value && false != $value ) ? explode( $fields[ $field_meta ]['delimiter'], $value ) : array();
179 if ( ! empty( $array ) && count( $array ) > 1 ) {
180 foreach ( $array as $val ) {
181 $display_values[] = wpmem_field_display_value( $field_meta, $val );
182 }
183 $return_value = ( "array" == $return_type ) ? $display_values : implode( ", ", $display_values );
184 } else {
185 $display_value = wpmem_field_display_value( $field_meta, $value );
186 $return_value = ( "array" == $return_type ) ? array( $display_value ) : $display_value;
187 }
188 return $return_value;
189 }
190
191 /**
192 * Checks if a user has a given meta value.
193 *
194 * @since 3.1.8
195 * @since 3.3.0 Added wpmem_user_has_meta filter.
196 * @since 3.3.0 Added array check for multi-value fields (multicheckbox and multiselect).
197 *
198 * @global object $wpmem WP_Members object.
199 *
200 * @param string $meta Meta key being checked.
201 * @param string $value Value the meta key should have (optional).
202 * @param int $user_id ID of the user being checked (optional).
203 * @return boolean $has_meta True if user has the meta value, otherwise false.
204 */
205 function wpmem_user_has_meta( $meta, $value = false, $user_id = false ) {
206
207 global $wpmem;
208
209 // Get the user ID.
210 $user_id = ( $user_id ) ? $user_id : get_current_user_id();
211
212 // Get field type.
213 $fields = wpmem_fields();
214 $multi = ( ( isset( $fields[ $meta ] ) ) && ( 'multicheckbox' == $fields[ $meta ]['type'] || 'multiselect' == $fields[ $meta ]['type'] ) ) ? true : false;
215
216 // Get meta.
217 $has_meta = false;
218 $user_value = get_user_meta( $user_id, $meta, true );
219
220 // Check meta.
221 if ( $value ) {
222 if ( $multi ) {
223 // Check array of values.
224 $user_value = explode( $fields[ $meta ]['delimiter'], $user_value );
225 $has_meta = ( in_array( $value, $user_value ) ) ? true : $has_meta;
226 } else {
227 // Straight comparison.
228 $has_meta = ( $user_value == $value ) ? true : $has_meta;
229 }
230 } else {
231 // Check if the user has any meta value (regardless of actual value).
232 $has_meta = ( $user_value ) ? true : $has_meta;
233 }
234
235 /**
236 * Filter the user has meta result.
237 *
238 * @since 3.3.0
239 *
240 * @param bool $has_meta True if the user has the value, otherwise false.
241 * @param int $user_id The user ID being checked.
242 * @param string $user_value The user's stored meta value (false if none).
243 */
244 return apply_filters( 'wpmem_user_has_meta', $has_meta, $user_id, $user_value );
245 }
246
247 /**
248 * Gets a user by meta key.
249 *
250 * @since 3.4.6
251 *
252 * @global object $wpdb
253 * @param string $meta_key
254 * @param string $meta_value
255 * @return WP_User
256 */
257 function wpmem_get_user_by_meta( $meta_key, $meta_value ) {
258 global $wpdb;
259 $sql = 'SELECT u1.ID, m1.meta_value
260 FROM ' . $wpdb->users . ' u1
261 JOIN ' . $wpdb->usermeta . ' m1 ON (m1.user_id = u1.ID AND m1.meta_key = "' . esc_sql( $meta_key ) . '")
262 WHERE m1.meta_value = "' . esc_sql( $meta_value ) . '";';
263 $user = $wpdb->get_row( $sql );
264 return $user;
265 }
266
267 /**
268 * Checks if a user is activated.
269 *
270 * @since 3.1.7
271 * @since 3.2.3 Now an alias for WP_Members_Users::is_user_activated().
272 *
273 * @global object $wpmem
274 * @param int $user_id
275 * @return bool
276 */
277 function wpmem_is_user_activated( $user_id = false ) {
278 global $wpmem;
279 return $wpmem->user->is_user_activated( $user_id );
280 }
281
282 /**
283 * Checks if a user is specifically deactivated.
284 * Note: this is not the same as not activated.
285 *
286 * @since 3.5.5
287 *
288 * @param int $user_id
289 * @return bool
290 */
291 function wpmem_is_user_deactivated( $user_id ) {
292 $user_id = ( ! $user_id ) ? get_current_user_id() : $user_id;
293 $status = get_user_meta( $user_id, 'active', true );
294 return ( 2 == $status ) ? true : false;
295 }
296
297 /**
298 * Gets an array of the user's registration data.
299 *
300 * Returns an array keyed by meta keys of the user's registration data for
301 * all fields in the WP-Members Fields. Returns the current user unless
302 * a user ID is specified.
303 *
304 * @since 3.2.0
305 *
306 * @global object $wpmem
307 * @param integer $user_id
308 * @param bool $all
309 * @return array $user_fields
310 */
311 function wpmem_user_data( $user_id = false, $all = false ) {
312 global $wpmem;
313 return $wpmem->user->user_data( $user_id, $all );
314 }
315
316 /**
317 * Updates a user's role.
318 *
319 * This is an alias for $wpmem->update_user_role(). It can add a role to a
320 * user, change or remove the user's role. If no action is specified it will
321 * change the role.
322 *
323 * @since 3.2.0
324 *
325 * @global object $wpmem
326 * @param integer $user_id (required)
327 * @param string $role (required)
328 * @param string $action (optional add|remove|set default:set)
329 */
330 function wpmem_update_user_role( $user_id, $role, $action = 'set' ) {
331 global $wpmem;
332 $wpmem->user->update_user_role( $user_id, $role, $action );
333 }
334
335 /**
336 * A function for checking user access criteria.
337 *
338 * @since 3.2.0
339 * @since 3.2.3 Reversed order of arguments.
340 *
341 * @param mixed $membership Accepts a single membership slug/meta, or an array of multiple memberships.
342 * @param integer $user_id User ID (optional|default: false).
343 * @return boolean $access True if user has access, otherwise false.
344 */
345 function wpmem_user_has_access( $membership, $user_id = false ) {
346 global $wpmem;
347 $user_id = ( false == $user_id ) ? get_current_user_id() : $user_id;
348 return $wpmem->user->has_access( $membership, $user_id );
349 }
350
351 /**
352 * Checks if user expiration is current.
353 *
354 * Similar to wpmem_user_has_access(), but specifically checks the
355 * expiration date for a specified membership (must be expiration product).
356 *
357 * Must be named _user_is_current() as _is_user_current() exists in PayPal extension.
358 *
359 * @since 3.4.0
360 *
361 * @param string $membership The meta key of the
362 * @param integer $user_id The user ID to check. Optional. If not passed, the currently logged in user will be checked.
363 * @return boolean True if user is current (i.e. not expired), otherwise false.
364 */
365 function wpmem_user_is_current( $membership, $user_id = false ) {
366 global $wpmem;
367 $user_id = ( false === $user_id ) ? get_current_user_id() : $user_id;
368 $memberships = wpmem_get_user_memberships( $user_id );
369 return ( isset( $memberships[ $membership ] ) && $wpmem->user->is_current( $memberships[ $membership ] ) ) ? true : false;
370 }
371
372 /**
373 * An alias for wpmem_set_user_product().
374 *
375 * @since 3.4.2
376 */
377 function wpmem_set_user_membership( $membership, $user_id = false, $date = false ) {
378 return wpmem_set_user_product( $membership, $user_id, $date );
379 }
380
381 /**
382 * An alias for wpmem_remove_user_product().
383 *
384 * @since 3.4.2
385 */
386 function wpmem_remove_user_membership( $membership, $user_id = false ) {
387 return wpmem_remove_user_product( $membership, $user_id );
388 }
389
390 /**
391 * Gets memberships a user has.
392 *
393 * @since 3.4.2
394 */
395 function wpmem_get_user_memberships( $user_id = false ) {
396 global $wpmem;
397 return ( $user_id ) ? $wpmem->user->get_user_products( $user_id ) : $wpmem->user->access;
398 }
399
400 /**
401 * Sets product access for a user.
402 *
403 * @since 3.2.3
404 * @since 3.2.6 Added $date to set a specific expiration date.
405 * @since 3.4.2 Use wpmem_set_user_membership() instead.
406 *
407 * @global object $wpmem
408 * @param string $product The meta key of the product.
409 * @param int $user_id
410 * @param string $date Expiration date (optional) format: MySQL timestamp
411 * @return bool $result
412 */
413 function wpmem_set_user_product( $product, $user_id = false, $date = false ) {
414 global $wpmem;
415 return $wpmem->user->set_user_product( $product, $user_id, $date );
416 }
417
418 /**
419 * Removes product access for a user.
420 *
421 * @since 3.2.3
422 * @since 3.4.2 Use wpmem_remove_user_membership() instead.
423 *
424 * @global object $wpmem
425 * @param string $product
426 * @param int $user_id
427 */
428 function wpmem_remove_user_product( $product, $user_id = false ) {
429 global $wpmem;
430 $wpmem->user->remove_user_product( $product, $user_id );
431 return;
432 }
433
434 /**
435 * Deprecated alias for wpmem_get_user_memberships().
436 *
437 * @since 3.3.0
438 * @since 3.4.2 Use wpmem_get_user_memberships() instead.
439 *
440 * @global stdClass $wpmem
441 * @param int $user_id
442 * @return array
443 */
444 function wpmem_get_user_products( $user_id = false ) {
445 return wpmem_get_user_memberships( $user_id );
446 }
447
448 /**
449 * Get user expiration date
450 *
451 * @since 3.4.2
452 *
453 * @param $product_key The membership slug being requested (optional: defaults to first membership in the array).
454 * @param $user_id The user ID (optional: defaults to current user).
455 * @param $format The date format to return (optional: defaults to raw epoch timestamp).
456 * @return $exp_date The expiration date unformatted (i.e. unix/epoch timestamp) (false if no date or membership does not exist for user).
457 */
458 function wpmem_get_user_expiration( $product_key = false, $user_id = false, $format = false ) {
459 $user_id = ( false === $user_id ) ? get_current_user_id() : $user_id;
460 $memberships = wpmem_get_user_memberships( $user_id );
461 $product_key = ( false == $product_key ) ? key( $memberships ) : $product_key;
462 $exp_date = ( is_numeric( $memberships[ $product_key ] ) ) ? $memberships[ $product_key ] : strtotime( $memberships[ $product_key ] );
463 $exp_date = ( $format ) ? wpmem_format_date( array( 'date'=>$exp_date, 'date_format'=>$format ) ) : $exp_date;
464 return $exp_date;
465 }
466
467 function wpmem_get_user_time_remaining( $product_key = false, $user_id = false, $interval = 'days' ) {
468 $user_id = ( false === $user_id ) ? get_current_user_id() : $user_id;
469 $expires = wpmem_get_user_expiration( $product_key, $user_id, 'Y-m-d' );
470 $target_date = new DateTime( $expires );
471 $current_date = new DateTime();
472 $date_diff = $current_date->diff( $target_date );
473 switch( $interval ) {
474 case 'months':
475 return $date_diff->months;
476 break;
477 default:
478 return $date_diff->days;
479 break;
480 }
481 }
482
483 function wpmem_prorate_membership( $args ) {
484
485 }
486
487 /**
488 * Sets a user as logged in.
489 *
490 * @since 3.2.3
491 *
492 * @global object $wpmem
493 * @param int $user_id
494 */
495 function wpmem_set_as_logged_in( $user_id ) {
496 global $wpmem;
497 $wpmem->user->set_as_logged_in( $user_id );
498 }
499
500 if ( ! function_exists( 'wpmem_login' ) ):
501 /**
502 * Logs in the user.
503 *
504 * Logs in the the user using wp_signon (since 2.5.2). If login is
505 * successful, it will set a cookie using wp_set_auth_cookie (since 2.7.7),
506 * then it redirects and exits; otherwise "loginfailed" is returned.
507 *
508 * @since 0.1.0
509 * @since 2.5.2 Now uses wp_signon().
510 * @since 2.7.7 Sets cookie using wp_set_auth_cookie().
511 * @since 3.0.0 Removed wp_set_auth_cookie(), this already happens in wp_signon().
512 * @since 3.1.7 Now an alias for login() in WP_Members_Users Class.
513 * @since 3.2.4 Moved to user API (could be deprecated).
514 *
515 * @global object $wpmem
516 * @return string Returns "loginfailed" if the login fails.
517 */
518 function wpmem_login() {
519 global $wpmem;
520 return $wpmem->user->login();
521 } // End of login function.
522 endif;
523
524 if ( ! function_exists( 'wpmem_logout' ) ):
525 /**
526 * Logs the user out then redirects.
527 *
528 * @since 2.0.0
529 * @since 3.1.6 Added wp_destroy_current_session(), removed nocache_headers().
530 * @since 3.1.7 Now an alias for logout() in WP_Members_Users Class.
531 * @since 3.2.4 Moved to user API (could be deprecated).
532 *
533 * @global object $wpmem
534 * @param string $redirect_to The URL to redirect to at logout.
535 */
536 function wpmem_logout( $redirect_to = false ) {
537 global $wpmem;
538 $wpmem->user->logout( $redirect_to );
539 }
540 endif;
541
542 if ( ! function_exists( 'wpmem_change_password' ) ):
543 /**
544 * Handles user password change (not reset).
545 *
546 * @since 2.1.0
547 * @since 3.1.7 Now an alias for password_update() in WP_Members_Users Class.
548 * @since 3.2.4 Moved to user API (could be deprecated).
549 *
550 * @global int $user_ID The WordPress user ID.
551 *
552 * @return string The value for $wpmem->regchk
553 */
554 function wpmem_change_password() {
555 global $wpmem;
556 return $wpmem->user->password_update( 'change' );
557 }
558 endif;
559
560 if ( ! function_exists( 'wpmem_reset_password' ) ):
561 /**
562 * Resets a forgotten password.
563 *
564 * @since 2.1.0
565 * @since 3.1.7 Now an alias for password_update() in WP_Members_Users Class.
566 * @since 3.2.4 Moved to user API (could be deprecated).
567 *
568 * @global object $wpmem The WP-Members object class.
569 *
570 * @return string The value for $wpmem->regchk
571 */
572 function wpmem_reset_password() {
573 global $wpmem;
574 return $wpmem->user->password_update( 'reset' );
575 }
576 endif;
577
578 /**
579 * Handles retrieving a forgotten username.
580 *
581 * @since 3.0.8
582 * @since 3.1.6 Dependencies now loaded by object.
583 * @since 3.1.8 Now an alias for $wpmem->retrieve_username() in WP_Members_Users Class.
584 * @since 3.2.4 Moved to user API (could be deprecated).
585 *
586 * @global object $wpmem The WP-Members object class.
587 *
588 * @return string $regchk The regchk value.
589 */
590 function wpmem_retrieve_username() {
591 global $wpmem;
592 return $wpmem->user->retrieve_username();
593 }
594
595 /**
596 * Creates a membership number.
597 *
598 * @since 3.1.1
599 * @since 3.2.0 Changed "lead" to "pad".
600 *
601 * @param array $args {
602 * @type string $option The wp_options name for the counter setting (required).
603 * @type string $meta_key The field's meta key (required).
604 * @type int $start Number to start with (optional, default 0).
605 * @type int $increment Number to increment by (optional, default 1).
606 * @type int $digits Number of digits for the number (optional).
607 * @type boolen $pad Pad leading zeros (optional, default true).
608 * }
609 * @return string $membersip_number
610 */
611 function wpmem_create_membership_number( $args ) {
612 global $wpmem;
613 return $wpmem->api->generate_membership_number( $args );
614 }
615
616 /**
617 * Activates a user.
618 *
619 * If registration is moderated, sets the activated flag
620 * in the usermeta. Flag prevents login when $wpmem->mod_reg
621 * is true (1). Function is fired from bulk user edit or
622 * user profile update.
623 *
624 * @uses $wpdb WordPress Database object.
625 *
626 * @since 2.4
627 * @since 3.1.6 Dependencies now loaded by object.
628 * @since 3.2.4 Renamed from wpmem_a_activate_user().
629 * @since 3.3.0 Moved to user API.
630 * @since 3.3.5 Added $notify argument.
631 * @since 3.4.0 Added $set_pwd argument.
632 *
633 * @param int $user_id
634 * @param bool $notify Send notification to user (optional, default: true).
635 */
636 function wpmem_activate_user( $user_id, $notify = true, $set_pwd = false ) {
637
638 global $wpmem;
639
640 // Define new_pass.
641 $new_pass = '';
642
643 // If passwords are user defined skip this.
644 if ( true == $set_pwd || ! wpmem_user_sets_password() ) {
645 $new_pass = wp_generate_password();
646 wp_set_password( $new_pass, $user_id );
647 }
648
649 // @todo this should be taken out, use the wpmem_user_activated hook instead.
650 // If subscriptions can expire, and the user has no expiration date, set one.
651 if ( $wpmem->use_exp == 1 && ! get_user_meta( $user_id, 'expires', true ) ) {
652 if ( function_exists( 'wpmem_set_exp' ) ) {
653 wpmem_set_exp( $user_id );
654 }
655 }
656
657 // Generate and send user approved email to user.
658 if ( true === $notify ) {
659 wpmem_email_to_user( array( 'user_id'=>$user_id, 'password'=>$new_pass, 'tag'=>'appmod' ) );
660 }
661
662 // Set the active flag in usermeta.
663 update_user_meta( $user_id, 'active', 1 );
664
665 /**
666 * Fires after the user activation process is complete.
667 *
668 * @since 2.8.2
669 *
670 * @param int $user_id The user's ID.
671 */
672 do_action( 'wpmem_user_activated', $user_id );
673
674 return;
675 }
676
677 /**
678 * Deactivates a user.
679 *
680 * Reverses the active flag from the activation process
681 * preventing login when registration is moderated.
682 *
683 * @since 2.7.1
684 * @since 3.2.4 Renamed from wpmem_a_deactivate_user().
685 * @since 3.3.0 Moved to user API.
686 *
687 * @param int $user_id
688 */
689 function wpmem_deactivate_user( $user_id ) {
690 update_user_meta( $user_id, 'active', 0 );
691
692 // Destroy sessions.
693 $sessions = WP_Session_Tokens::get_instance( $user_id );
694 $sessions->destroy_all();
695
696 /**
697 * Fires after the user deactivation process is complete.
698 *
699 * @since 2.9.9
700 *
701 * @param int $user_id The user's ID.
702 */
703 do_action( 'wpmem_user_deactivated', $user_id );
704 }
705
706 /**
707 * Updates the user_status value in the wp_users table.
708 *
709 * @since Unknown
710 * @since 3.3.0 Moved to User API.
711 *
712 * @global object $wpdb
713 *
714 * @param int $user_id
715 * @param string $status
716 */
717 function wpmem_set_user_status( $user_id, $status ) {
718 global $wpdb;
719 $wpdb->update( $wpdb->users, array( 'user_status' => $status ), array( 'ID' => $user_id ) );
720 return;
721 }
722
723 /**
724 * Register function.
725 *
726 * Handles registering new users and updating existing users.
727 *
728 * @since 2.2.1
729 * @since 2.7.2 Added pre/post process actions.
730 * @since 2.8.2 Added validation and data filters.
731 * @since 2.9.3 Added validation for multisite.
732 * @since 3.0.0 Moved from wp-members-register.php to /inc/register.php.
733 * @since 3.3.0 Ported from wpmem_registration in /inc/register.php (now deprecated).
734 * @since 3.5.0 $tag is optional, does registration by default.
735 *
736 * @todo Review what should be in the API function and what should be moved to object classes.
737 *
738 * @global int $user_ID
739 * @global object $wpmem
740 * @global string $wpmem_themsg
741 * @global array $userdata
742 *
743 * @param string $tag Identifies 'register' or 'update'. Optional, default: register
744 * @return string $wpmem_themsg|success|editsuccess
745 */
746 function wpmem_user_register( $tag = "register" ) {
747
748 // Get the globals.
749 global $user_ID, $wpmem, $wpmem_themsg, $userdata;
750
751 $wpmem->user->register_validate( $tag );
752
753 // @todo Added as a fix for legacy versions of security extension and any wpmem_pre_register_data action that might null $wpmem_themsg.
754 if ( $wpmem_themsg ) {
755 return $wpmem_themsg;
756 }
757
758 switch ( $tag ) {
759
760 case "register":
761
762 /**
763 * Filter registration data after validation before data insertion.
764 *
765 * @since 2.8.2
766 *
767 * @param array $wpmem->user->post_data An array of the registration field data.
768 * @param string $tag A switch to indicate the action (new|edit).
769 */
770 $wpmem->user->post_data = apply_filters( 'wpmem_register_data', $wpmem->user->post_data, 'new' );
771
772 /**
773 * Fires before any insertion/emails.
774 *
775 * This action is the final step in pre registering a user. This
776 * can be used for attaching custom validation to the registration
777 * process. It cannot be used for changing any user registration
778 * data. Use the wpmem_register_data filter for that.
779 *
780 * @since 2.7.2
781 *
782 * @param array $wpmem->user->post_data The user's submitted registration data.
783 */
784 do_action( 'wpmem_pre_register_data', $wpmem->user->post_data );
785
786 // If the _pre_register_data hook sends back an error message.
787 if ( $wpmem_themsg ) {
788 return $wpmem_themsg;
789 }
790
791 // Main new user fields are ready.
792 $new_user_fields = array (
793 'user_pass' => $wpmem->user->post_data['password'],
794 'user_login' => $wpmem->user->post_data['username'],
795 'user_nicename' => $wpmem->user->post_data['user_nicename'],
796 'user_email' => $wpmem->user->post_data['user_email'],
797 'display_name' => $wpmem->user->post_data['display_name'],
798 'nickname' => $wpmem->user->post_data['nickname'],
799 'user_registered' => $wpmem->user->post_data['user_registered'],
800 'role' => $wpmem->user->post_data['user_role']
801 );
802
803 // Get any excluded meta fields.
804 $wpmem->excluded_meta = wpmem_get_excluded_meta( 'register' );
805
806 // Fields for wp_insert_user: user_url, first_name, last_name, description.
807 $new_user_fields_meta = array( 'user_url', 'first_name', 'last_name', 'description' );
808 foreach ( $wpmem->fields as $meta_key => $field ) {
809 if ( in_array( $meta_key, $new_user_fields_meta ) ) {
810 if ( $field['register'] && ! in_array( $meta_key, $wpmem->excluded_meta ) ) {
811 $new_user_fields[ $meta_key ] = $wpmem->user->post_data[ $meta_key ];
812 }
813 }
814 }
815
816 // Inserts to wp_users table.
817 $user_id = wp_insert_user( $new_user_fields );
818
819 /**
820 * Fires after registration is complete.
821 *
822 * @since 2.7.1
823 * @since 3.1.0 Added $fields
824 * @since 3.1.7 Changed $fields to $this->post_data
825 * @since 3.3.0 Moved to registration function.
826 * @since 3.3.8 Added $user parameter.
827 *
828 * @param array $wpmem->user->post_data The user's submitted registration data.
829 * @param int $user_id
830 */
831 do_action( 'wpmem_register_redirect', $wpmem->user->post_data, $user_id );
832
833 // successful registration message
834 return "success";
835 break;
836
837 case "update":
838
839 if ( $wpmem_themsg ) {
840 return "updaterr";
841 exit();
842 }
843
844 /*
845 * Doing a check for existing email is not the same as a new reg. check first to
846 * see if it's different, then check if it is a valid address and it exists.
847 */
848 global $current_user; wp_get_current_user();
849 if ( isset( $wpmem->user->post_data['user_email'] ) ) {
850 if ( $wpmem->user->post_data['user_email'] != $current_user->user_email ) {
851 if ( email_exists( $wpmem->user->post_data['user_email'] ) ) {
852 return "email";
853 exit();
854 }
855 if ( in_array( 'user_email', $wpmem->fields ) && ! is_email( $wpmem->user->post_data['user_email']) ) {
856 $wpmem_themsg = wpmem_get_text( 'reg_valid_email' );
857 return "updaterr";
858 exit();
859 }
860 }
861 }
862
863 // If form includes email confirmation, validate that they match.
864 if ( array_key_exists( 'confirm_email', $wpmem->user->post_data ) && $wpmem->user->post_data['confirm_email'] != $wpmem->user->post_data ['user_email'] ) {
865 $wpmem_themsg = wpmem_get_text( 'reg_email_match' );
866 return "updaterr";
867 exit();
868 }
869
870 // Add the user_ID to the fields array.
871 $wpmem->user->post_data['ID'] = $user_ID;
872
873 /** This filter is documented in register.php */
874 $wpmem->user->post_data = apply_filters( 'wpmem_register_data', $wpmem->user->post_data, 'edit' );
875
876 /**
877 * Fires before data insertion.
878 *
879 * This action is the final step in pre updating a user. This
880 * can be used for attaching custom validation to the update
881 * process. It cannot be used for changing any user update
882 * data. Use the wpmem_register_data filter for that.
883 *
884 * @since 2.7.2
885 *
886 * @param array $wpmem->user->post_data The user's submitted update data.
887 */
888 do_action( 'wpmem_pre_update_data', $wpmem->user->post_data );
889
890 // If the _pre_update_data hook sends back an error message.
891 if ( $wpmem_themsg ){
892 return "updaterr";
893 }
894
895 // A list of fields that can be updated by wp_update_user.
896 $native_fields = array(
897 'user_nicename',
898 'user_url',
899 'user_email',
900 'display_name',
901 'nickname',
902 'first_name',
903 'last_name',
904 'description',
905 'role',
906 );
907 $native_update = array( 'ID' => $wpmem->user->post_data['ID'] );
908
909 foreach ( $wpmem->fields as $meta_key => $field ) {
910 // If the field is not excluded, update accordingly.
911 if ( ! in_array( $meta_key, wpmem_get_excluded_meta( 'update' ) ) ) {
912 if ( 'file' != $field['type'] && 'image' != $field['type'] ) {
913 switch ( $meta_key ) {
914
915 // If the field can be updated by wp_update_user.
916 case( in_array( $meta_key, $native_fields ) ):
917 if ( 1 == $field['profile'] ) {
918 // Prev value in prev_data array.
919 $wpmem->user->prev_data[ $meta_key ] = $current_user->{$meta_key};
920 // Add to post_data array.
921 $wpmem->user->post_data[ $meta_key ] = ( isset( $wpmem->user->post_data[ $meta_key ] ) ) ? $wpmem->user->post_data[ $meta_key ] : '';
922 // Add to native update for settings.
923 $native_update[ $meta_key ] = $wpmem->user->post_data[ $meta_key ];
924 }
925 break;
926
927 // If the field is password.
928 case( 'password' ):
929 // Do nothing.
930 break;
931
932 // Everything else goes into wp_usermeta.
933 default:
934 if ( ( 'register' == $tag && true == $field['register'] ) || ( 'update' == $tag && true == $field['profile'] ) ) {
935 // Get existing value for prev_data array.
936 $wpmem->user->prev_data[ $meta_key ] = get_user_meta( $wpmem->user->post_data['ID'], $meta_key, true );
937 // Update the value.
938 update_user_meta( $wpmem->user->post_data['ID'], $meta_key, $wpmem->user->post_data[ $meta_key ] );
939 }
940 break;
941 }
942 }
943 }
944 }
945
946 // Handle file uploads, if any.
947 if ( ! empty( $_FILES ) ) {
948 $wpmem->user->upload_user_files( $wpmem->user->post_data['ID'], $wpmem->fields );
949 }
950
951 // Update wp_update_user fields.
952 wp_update_user( $native_update );
953
954 /**
955 * Fires at the end of user update data insertion.
956 *
957 * @since 2.7.2
958 *
959 * @param array $wpmem->user->post_data The user's submitted registration data.
960 * @param int $user_id
961 * @param array $wpmem->user->prev_data The data for the user prior to update (does not support file and image fields).
962 */
963 do_action( 'wpmem_post_update_data', $wpmem->user->post_data, $wpmem->user->post_data['ID'], $wpmem->user->prev_data );
964
965 return "editsuccess"; exit();
966 break;
967 }
968 } // End registration function.
969
970 /**
971 * Get user IP address.
972 *
973 * From Pippin.
974 * @link https://gist.github.com/pippinsplugins/9641841
975 *
976 * @since 3.3.0
977 * @since 3.4.0 Now an alias for rktgk_get_user_ip();
978 *
979 * @return string $ip.
980 */
981 function wpmem_get_user_ip() {
982 /**
983 * Filter the IP result.
984 *
985 * @since 3.3.0
986 *
987 * @param string $ip
988 */
989 return apply_filters( 'wpmem_get_ip', rktgk_get_user_ip() );
990 }
991
992 /**
993 * Export all or selected users
994 *
995 * @since 2.9.7
996 * @since 3.2.0 Updated to use fputcsv.
997 * @since 3.2.1 Added user data filters.
998 * @since 3.3.0 Call object class static method.
999 * @since 3.3.4 Moved into general API.
1000 *
1001 * @todo Move object class file to main /includes/
1002 *
1003 * @global object $wpmem
1004 *
1005 * @param array $args array {
1006 * Array of defaults for export.
1007 *
1008 * @type string $export The type of export (all|selected)
1009 * @type string $filename
1010 * @type array $fields {
1011 * The array of export fields is keyed as 'meta_key' => 'heading value'.
1012 * The array can include fields in the Fields tab, plus the following:
1013 *
1014 * @type int $ID ID from wp_users
1015 * @type string $username user_login from wp_users
1016 * @type string $user_nicename user_nicename
1017 * @type string $user_url user_url
1018 * @type string $display_name display_name
1019 * @type int $active Whether the user is active/deactivated.
1020 * @type string $exp_type If the PayPal extension is installed pending|subscrption (optional)
1021 * @type string $expires If the PayPal extension is installed MM/DD/YYYY (optional)
1022 * @type string $user_registered user_registered
1023 * @type string $user_ip The IP of the user when they registered.
1024 * @type string $role The user's role (or roles, if multiple).
1025 * }
1026 * @type array $exclude_fields @deprecated 3.4.0
1027 * @type boolean $entity_decode Whether HTML entities should be decoded (default: false)
1028 * @type string $date_format A PHP readable date format (default: Y-m-d which results in YYYY-MM-DD)
1029 * }
1030 * @param array $users Array of user IDs to export.
1031 */
1032 function wpmem_export_users( $args = array(), $users = array() ) {
1033 global $wpmem;
1034 include_once( $wpmem->path . 'includes/class-wp-members-user-export.php' );
1035 WP_Members_User_Export::export_users( $args, $users );
1036 }
1037
1038 /**
1039 * Gets user ID based on request.
1040 *
1041 * @since 3.3.5
1042 *
1043 * @param mixed $user
1044 * @return mixed
1045 */
1046 function wpmem_get_user_id( $user ) {
1047 $user_obj = wpmem_get_user_obj( $user );
1048 return ( is_object( $user_obj ) ) ? $user_obj->ID : false;
1049 }
1050
1051 /**
1052 * Gets user object based on request.
1053 *
1054 * @since 3.3.5
1055 *
1056 * @param mixed $user
1057 * @return mixed
1058 */
1059 function wpmem_get_user_obj( $user ) {
1060 if ( is_numeric( $user ) ) {
1061 $user_obj = get_userdata( $user );
1062 if ( $user_obj ) {
1063 return $user_obj;
1064 }
1065 }
1066 if ( strpos( $user, '@' ) ) {
1067 $user_obj = get_user_by( 'email', $user );
1068 if ( $user_obj ) {
1069 return $user_obj;
1070 }
1071 }
1072 if ( is_string( $user ) ) {
1073 $user_obj = get_user_by( 'login', $user );
1074 if ( $user_obj ) {
1075 return $user_obj;
1076 }
1077 }
1078 return false;
1079 }
1080
1081 /**
1082 * Get all users by a meta value.
1083 *
1084 * @since 3.3.5
1085 *
1086 * @param string $meta The meta key to search for.
1087 * @param string $value The meta value to search for (defaul:false).
1088 * @return array $users An array of user IDs who have the requested meta.
1089 */
1090 function wpmem_get_users_by_meta( $meta, $value = "EXISTS" ) {
1091 $args = array( 'fields' => array( 'ID' ), 'meta_key' => esc_sql( $meta ) );
1092 if ( FALSE === $value ) { // needs to be explicit as $value may sometimes be 0 (not false).
1093 $args['meta_value'] = '';
1094 $args['meta_compare'] = 'NOT EXISTS';
1095 } elseif ( "EXISTS" == $value ) {
1096 $args['meta_value'] = '';
1097 $args['meta_compare'] = '>';
1098 } else {
1099 $args['meta_value'] = esc_sql( $value );
1100 }
1101 $results = get_users( $args );
1102 if ( $results ) {
1103 foreach( $results as $result ) {
1104 $users[] = intval( $result->ID );
1105 }
1106 return $users;
1107 } else {
1108 return array();
1109 }
1110 }
1111
1112 /**
1113 * Gets a list of all pending users.
1114 *
1115 * @since 3.3.5
1116 *
1117 * @return array $users An array of user IDs where meta key "active" does not exist.
1118 */
1119 function wpmem_get_pending_users() {
1120 return wpmem_get_users_by_meta( 'active', false );
1121 }
1122
1123 /**
1124 * Gets a list of all activated users.
1125 *
1126 * @since 3.3.5
1127 *
1128 * @return array $users An array of user IDs who have the meta key active=1
1129 */
1130 function wpmem_get_activated_users() {
1131 return wpmem_get_users_by_meta( 'active', 1 );
1132 }
1133
1134 /**
1135 * Gets a list of all deactivated users.
1136 *
1137 * @since 3.3.5
1138 *
1139 * @return array $users An array of users IDs who have the meta key active=0
1140 */
1141 function wpmem_get_deactivated_users() {
1142 return wpmem_get_users_by_meta( 'active', 0 );
1143 }
1144
1145 /**
1146 * Gets a list of all confirmed users.
1147 *
1148 * @since 3.5.4
1149 *
1150 * @todo meta is a timestamp, so could add a "get all since date" process by looping
1151 * through the results and checking the date.
1152 *
1153 * @return array $users An array of users IDs who have the meta key active=0
1154 */
1155 function wpmem_get_confirmed_users() {
1156 return wpmem_get_users_by_meta( '_wpmem_user_confirmed', 'EXISTS' );
1157 }
1158
1159 /**
1160 * Sets a user as validated.
1161 *
1162 * @since 3.3.5
1163 *
1164 * @param int $user_id
1165 * @return void
1166 */
1167 function wpmem_set_user_as_confirmed( $user_id ) {
1168 global $wpmem;
1169 $wpmem->act_newreg->set_as_confirmed( $user_id );
1170 }
1171
1172 /**
1173 * Sets user as unconfirmed (not validated).
1174 *
1175 * @since 3.3.8
1176 *
1177 * @param int $user_id
1178 * @return void
1179 */
1180 function wpmem_set_user_as_unconfirmed( $user_id ) {
1181 global $wpmem;
1182 $wpmem->act_newreg->set_as_unconfirmed( $user_id );
1183 }
1184
1185 /**
1186 * Checks if a user is confirmed.
1187 *
1188 * @since 3.3.8
1189 *
1190 * @global object $wpmem
1191 * @param int $user_id
1192 * @return bool
1193 */
1194 function wpmem_is_user_confirmed( $user_id = false ) {
1195 global $wpmem;
1196 $user_id = ( false === $user_id ) ? get_current_user_id() : $user_id;
1197 return ( get_user_meta( $user_id, $wpmem->act_newreg->validation_confirm, true ) ) ? true : false;
1198 }
1199
1200 /**
1201 * Adds WP-Members custom fields to the WP Add New User form.
1202 *
1203 * @since 2.9.1
1204 * @since 3.4.0 Moved from admin.php
1205 *
1206 * @global stdClass $wpmem
1207 */
1208 function wpmem_admin_add_new_user() {
1209 global $wpmem;
1210 // Output the custom registration fields.
1211 echo $wpmem->forms->wp_newuser_form();
1212 return;
1213 }
1214
1215 /**
1216 * Creates a username "placeholder" for the db from
1217 * the user's email.
1218 *
1219 * @since 3.4.6
1220 *
1221 * @param string $email
1222 * @param array $fields Array of fields
1223 * @return string $username
1224 */
1225 function wpmem_create_username_from_email( $email, $fields = array() ) {
1226 // If the WooCommerce function exists, use that.
1227 if ( function_exists( 'wc_create_new_customer_username' ) ) {
1228 return wc_create_new_customer_username( $email, $fields );
1229 }
1230 if ( ! is_email( $email ) ) {
1231 return false;
1232 }
1233 // Extract beginning of email for username
1234 $parts = explode( "@", $email );
1235 $temp_user = $parts[0];
1236
1237 // Make sure it's a unique value. If not, add a number and retest.
1238 if ( username_exists( $temp_user ) ) {
1239 $i = 0;
1240 do {
1241 $i++;
1242 $temp_user = $temp_user . $i;
1243 } while ( username_exists( $temp_user ) );
1244 }
1245
1246 return $temp_user;
1247 }
1248
1249 /**
1250 * Utility to get all users. Essentially a wrapper for WP_User_Query
1251 *
1252 * @since 3.5.0
1253 *
1254 * @todo Add query args for getting users by membership (and by not membership)
1255 */
1256 function wpmem_get_users( $args = array( 'fields' => 'ID' ), $type = 'array' ) {
1257 $type = ( ! is_array( $args ) ) ? $args : $type;
1258 $args['count_total'] = false;
1259 $user_search = new WP_User_Query( $args );
1260 $result = $user_search->get_results();
1261 return ( 'object' == $type ) ? (object)$result : $result;
1262 }
1263
1264 /**
1265 * Gets a count of users.
1266 *
1267 * @since 3.4.5
1268 * @since 3.5.3 Now in the main API (previously admin API)
1269 * @since 3.5.4 Revamped.
1270 *
1271 * @param mixed $args
1272 */
1273 function wpmem_user_count( $args = 'all' ) {
1274 if ( is_array( $args ) && isset( $args['meta_key'] ) ) {
1275 // This is a meta query.
1276 $compare = ( isset( $args['compare'] ) ) ? $args['compare'] : '=';
1277 return wpmem_get_user_count_by_meta( $args['meta_key'], $args['meta_value'], $compare );
1278 } else {
1279 switch ( $args ) {
1280 case 'active':
1281 return count( wpmem_get_activated_users() );
1282 break;
1283
1284 case 'deactivated':
1285 return count( wpmem_get_deactivated_users() );
1286 break;
1287
1288 case 'pending':
1289 return count( wpmem_get_pending_users() );
1290 break;
1291
1292 case 'confirmed':
1293 return wpmem_get_user_count_by_meta( '_wpmem_user_confirmed', '0', '>' );
1294 break;
1295
1296 case 'notconfirmed':
1297 return ( count_users() ) - ( wpmem_get_user_count_by_meta( '_wpmem_user_confirmed', '0', '>' ) );
1298 break;
1299
1300 default:
1301 return wpmem_get_user_count_by_role( $args );
1302 break;
1303 }
1304 }
1305 }
1306
1307 function wpmem_get_user_count_by_meta( $meta_key, $meta_value, $compare = '=' ) {
1308 global $wpdb;
1309 return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM " . $wpdb->usermeta . " WHERE meta_key=%s AND meta_value " . esc_sql( $compare ) . " \"%s\"", $meta_key, $meta_value ) );
1310 }
1311
1312 /**
1313 * Gets a count of users by role.
1314 *
1315 * @since 3.5.4
1316 */
1317 function wpmem_get_user_count_by_role( $role ) {
1318 $users = count_users();
1319 return ( 'all' == $role ) ? $users['total_users'] : $users['avail_roles'][ $role ];
1320 }
1321 // End of file.