PluginProbe
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions / 110710
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions v110710
260913 260909 260829 260814 260805 110710 110731 110812 110815 110912 110913 110915 110926 110927 111002 111003 111011 111017 111029 111105 111206 111216 111220 120213 120219 All 188 releases
s2member / includes / classes / user-access.inc.php

user-access.inc.php in s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions 110710, at includes/classes/user-access.inc.php

173 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * User access routines.
4 *
5 * Copyright: © 2009-2011
6 * {@link http://www.websharks-inc.com/ WebSharks, Inc.}
7 * ( coded in the USA )
8 *
9 * Released under the terms of the GNU General Public License.
10 * You should have received a copy of the GNU General Public License,
11 * along with this software. In the main directory, see: /licensing/
12 * If not, see: {@link http://www.gnu.org/licenses/}.
13 *
14 * @package s2Member\User_Access
15 * @since 3.5
16 */
17 if (realpath (__FILE__) === realpath ($_SERVER["SCRIPT_FILENAME"]))
18 exit("Do not access this file directly.");
19 /**/
20 if (!class_exists ("c_ws_plugin__s2member_user_access"))
21 {
22 /**
23 * User access routines.
24 *
25 * @package s2Member\User_Access
26 * @since 3.5
27 */
28 class c_ws_plugin__s2member_user_access
29 {
30 /**
31 * Determines the Access Role of a User/Member.
32 *
33 * If ``$user`` is NOT passed in, check the current User/Member.
34 * If ``$user`` IS passed in, this function will check a specific ``$user``.
35 * Returns their Role ID/Name value.
36 *
37 * @package s2Member\User_Access
38 * @since 3.5
39 *
40 * @param obj $user Optional. A `WP_User` object. Defaults to the current User.
41 * In order to check the current User, you must call this function with no arguments/parameters.
42 * @return str Role ID/Name, or an empty string if they have no Role, or if ``$user`` does not exist, or if no User is currently logged-in.
43 */
44 public static function user_access_role ($user = FALSE)
45 {
46 if ((func_num_args () && (!is_object ($user) || !$user->ID)) || (!func_num_args () && !$user && (!is_object ($user = (is_user_logged_in ()) ? wp_get_current_user () : false) || !$user->ID)))
47 {
48 return apply_filters ("ws_plugin__s2member_user_access_role", "", get_defined_vars ());
49 }
50 else /* Else we return the first Role in their array of assigned WordPress® Roles. */
51 return apply_filters ("ws_plugin__s2member_user_access_role", reset ($user->roles), get_defined_vars ());
52 }
53 /**
54 * Determines Custom Capabilities of a User/Member.
55 *
56 * If ``$user`` is NOT passed in, check the current User/Member.
57 * If ``$user`` IS passed in, this function will check a specific ``$user``.
58 * Returns an array of Custom Capabilities.
59 *
60 * @package s2Member\User_Access
61 * @since 3.5
62 *
63 * @param obj $user Optional. A `WP_User` object. Defaults to the current User.
64 * In order to check the current User, you must call this function with no arguments/parameters.
65 * @return array Array of Custom Capabilities, or an empty array if they have no Custom Capabilities, or if ``$user`` does not exist, or if no User is currently logged-in.
66 */
67 public static function user_access_ccaps ($user = FALSE)
68 {
69 if ((func_num_args () && (!is_object ($user) || !$user->ID)) || (!func_num_args () && !$user && (!is_object ($user = (is_user_logged_in ()) ? wp_get_current_user () : false) || !$user->ID)))
70 {
71 return apply_filters ("ws_plugin__s2member_user_access_ccaps", array (), get_defined_vars ());
72 }
73 else /* Otherwise, we DO have the $user object available. */
74 {
75 $ccaps = array (); /* Initializes $ccaps array. */
76 /**/
77 foreach ($user->allcaps as $cap => $cap_enabled)
78 if (preg_match ("/^access_s2member_ccap_/", $cap))
79 $ccaps[] = preg_replace ("/^access_s2member_ccap_/", "", $cap);
80 /**/
81 return apply_filters ("ws_plugin__s2member_user_access_ccaps", $ccaps, get_defined_vars ());
82 }
83 }
84 /**
85 * Determines Access Level of a User/Member.
86 *
87 * If ``$user`` is NOT passed in, check the current User/Member.
88 * If ``$user`` IS passed in, this function will check a specific ``$user``.
89 * Returns `-1` thru number of configured Levels, according to the Access Level#.
90 *
91 * @package s2Member\User_Access
92 * @since 3.5
93 *
94 * @param obj $user Optional. A `WP_User` object. Defaults to the current User.
95 * In order to check the current User, you must call this function with no arguments/parameters.
96 * @return int Access Level#, `-1` if ``$user`` does not exist, or if no User is currently logged-in.
97 */
98 public static function user_access_level ($user = FALSE)
99 {
100 if ((func_num_args () && (!is_object ($user) || !$user->ID)) || (!func_num_args () && !$user && (!is_object ($user = (is_user_logged_in ()) ? wp_get_current_user () : false) || !$user->ID)))
101 {
102 return apply_filters ("ws_plugin__s2member_user_access_level", -1, get_defined_vars ()); /* No $user, or NOT logged in. */
103 }
104 /**/
105 for ($n = $GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["levels"]; $n >= 0; $n--)
106 {
107 if ($user->has_cap ("access_s2member_level" . $n)) /* Testing for Membership Level Access. */
108 {
109 return apply_filters ("ws_plugin__s2member_user_access_level", $n, get_defined_vars ());
110 }
111 }
112 /* Else we assume this is a "User" ( a Free Subscriber with an Access Level of 0. ). */
113 return apply_filters ("ws_plugin__s2member_user_access_level", 0, get_defined_vars ());
114 }
115 /**
116 * Determines Access Level of a specific Role.
117 *
118 * @package s2Member\User_Access
119 * @since 3.5
120 *
121 * @param str $role A WordPress® Role ID/Name.
122 * @return int Access Level#, `-1` if ``$role`` is empty.
123 */
124 public static function user_access_role_to_level ($role = FALSE)
125 {
126 if (!($role = strtolower ($role))) /* No ``$role`` provided. Default value of -1. */
127 {
128 return apply_filters ("ws_plugin__s2member_user_access_role_to_level", -1, get_defined_vars ());
129 }
130 else if (in_array ($role, array ("administrator", "editor", "author", "contributor", "bbp_moderator")))
131 {
132 return apply_filters ("ws_plugin__s2member_user_access_role_to_level", $GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["levels"], get_defined_vars ());
133 }
134 else if (preg_match ("/^s2member_level([0-9]+)$/i", $role, $m) && $m[1] >= 1) /* Test for s2Member Roles. */
135 {
136 return apply_filters ("ws_plugin__s2member_user_access_role_to_level", (int)$m[1], get_defined_vars ());
137 }
138 else if ($role === "subscriber") /* Testing for Free Subscriber Role. */
139 {
140 return apply_filters ("ws_plugin__s2member_user_access_role_to_level", 0, get_defined_vars ());
141 }
142 else /* Else we assume this is a "User" ( a Free Subscriber with an Access Level of 0. ). */
143 return apply_filters ("ws_plugin__s2member_user_access_role_to_level", 0, get_defined_vars ());
144 }
145 /**
146 * Determines Access Label for a User/Member.
147 *
148 * If ``$user`` is NOT passed in, check the current User/Member.
149 * If ``$user`` IS passed in, this function will check a specific ``$user``.
150 *
151 * @package s2Member\User_Access
152 * @since 3.5
153 *
154 * @param obj $user Optional. A `WP_User` object. Defaults to the current User.
155 * In order to check the current User, you must call this function with no arguments/parameters.
156 * @return str Access Level Label, empty string if ``$user`` does not exist, or if no User is currently logged-in.
157 */
158 public static function user_access_label ($user = FALSE)
159 {
160 if ((func_num_args () && (!is_object ($user) || !$user->ID)) || (!func_num_args () && !$user && (!is_object ($user = (is_user_logged_in ()) ? wp_get_current_user () : false) || !$user->ID)))
161 {
162 return apply_filters ("ws_plugin__s2member_user_access_label", "", get_defined_vars ()); /* No $user, or NOT logged in. */
163 }
164 else if (($level = c_ws_plugin__s2member_user_access::user_access_level ($user)) >= 0 && !empty ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["level" . $level . "_label"]))
165 {
166 return apply_filters ("ws_plugin__s2member_user_access_label", $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["level" . $level . "_label"], get_defined_vars ());
167 }
168 else /* Else there is no Label configured for this User/Member. Return empty string. */
169 return apply_filters ("ws_plugin__s2member_user_access_label", "", get_defined_vars ());
170 }
171 }
172 }
173 ?>