PluginProbe
Members – Membership & User Role Editor Plugin / 3.2.24
Members – Membership & User Role Editor Plugin v3.2.24
3.2.25 3.2.26 3.2.24 3.2.23 3.2.22 3.2.21 trunk 0.1 0.1.1 0.2 0.2.1 0.2.2 0.2.3 0.2.4 0.2.5 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 2.0.0 2.0.1 2.0.2 All 66 releases
members / inc / functions-users.php

functions-users.php in Members – Membership & User Role Editor Plugin 3.2.24, at inc/functions-users.php

167 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * User-related functions and filters.
4 *
5 * @package Members
6 * @subpackage Includes
7 * @author The MemberPress Team
8 * @copyright Copyright (c) 2009 - 2018, The MemberPress Team
9 * @link https://members-plugin.com/
10 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
11 */
12 if (!defined('ABSPATH')) {
13 die('You are not allowed to call this page directly.');
14 }
15
16 // Filter `user_has_cap` if denied caps should take precedence.
17 if ( members_explicitly_deny_caps() ) {
18 add_filter( 'user_has_cap', 'members_user_has_cap_filter', 10, 4 );
19 }
20
21 /**
22 * Filter on `user_has_cap` to explicitly deny caps if there are conflicting caps when a
23 * user has multiple roles. WordPress doesn't consistently handle two or more roles that
24 * have the same capability but a conflict between being granted or denied. Core WP
25 * merges the role caps so that the last role the user has will take precedence. This
26 * has the potential for granting permission for things that a user shouldn't have
27 * permission to do.
28 *
29 * @since 1.0.0
30 * @access public
31 * @param array $allcaps
32 * @param array $caps
33 * @param array $args
34 * @param object $user
35 * @return array
36 */
37 function members_user_has_cap_filter( $allcaps, $caps, $args, $user ) {
38
39 // If the user doesn't have more than one role, bail.
40 if ( 1 >= count( (array) $user->roles ) )
41 return $allcaps;
42
43 // Get the denied caps.
44 $denied_caps = array_keys( (array) $allcaps, false );
45
46 // Loop through the user's roles and find any denied caps.
47 foreach ( (array) $user->roles as $role ) {
48
49 // Get the role object.
50 $role_obj = get_role( $role );
51
52 // If we have an object, merge it's denied caps.
53 if ( ! is_null( $role_obj ) ) {
54 $denied_caps = array_merge(
55 (array) $denied_caps,
56 array_keys( (array) $role_obj->capabilities, false )
57 );
58 }
59 }
60
61 // If there are any denied caps, make sure they take precedence.
62 if ( $denied_caps ) {
63
64 foreach ( $denied_caps as $denied_cap ) {
65 $allcaps[ $denied_cap ] = false;
66 }
67 }
68
69 // Return all the user caps.
70 return $allcaps;
71 }
72
73 /**
74 * Conditional tag to check whether a user has a specific role.
75 *
76 * @since 1.0.0
77 * @access public
78 * @param int $user_id
79 * @param string|array $roles
80 * @return bool
81 */
82 function members_user_has_role( $user_id, $roles ) {
83
84 $user = new WP_User( $user_id );
85
86 foreach ( (array) $roles as $role ) {
87
88 if ( in_array( $role, (array) $user->roles ) )
89 return true;
90 }
91
92 return false;
93 }
94
95 /**
96 * Conditional tag to check whether the currently logged-in user has a specific role.
97 *
98 * @since 1.0.0
99 * @access public
100 * @param string|array $roles
101 * @return bool
102 */
103 function members_current_user_has_role( $roles ) {
104
105 return is_user_logged_in() ? members_user_has_role( get_current_user_id(), $roles ) : false;
106 }
107
108 /**
109 * Wrapper for `current_user_can()` that checks if the user can perform any action.
110 * Accepts an array of caps instead of a single cap.
111 *
112 * @since 2.0.0
113 * @access public
114 * @param array $caps
115 * @return bool
116 */
117 function members_current_user_can_any( $caps = array() ) {
118
119 foreach ( $caps as $cap ) {
120
121 if ( current_user_can( $cap ) )
122 return true;
123 }
124
125 return false;
126 }
127
128 /**
129 * Wrapper for `current_user_can()` that checks if the user can perform all actions.
130 * Accepts an array of caps instead of a single cap.
131 *
132 * @since 2.0.0
133 * @access public
134 * @param array $caps
135 * @return bool
136 */
137 function members_current_user_can_all( $caps = array() ) {
138
139 foreach ( $caps as $cap ) {
140
141 if ( ! current_user_can( $cap ) )
142 return false;
143 }
144
145 return true;
146 }
147
148 /**
149 * Returns an array of the role names a user has.
150 *
151 * @since 1.0.0
152 * @access public
153 * @param int $user_id
154 * @return array
155 */
156 function members_get_user_role_names( $user_id ) {
157
158 $user = new WP_User( $user_id );
159
160 $names = array();
161
162 foreach ( $user->roles as $role )
163 $names[ $role ] = members_get_role( $role )->get( 'label' );
164
165 return $names;
166 }
167