Source Code Documentation


s2member\includes\classes\utils-users.inc.php


  1. <?php
  2. /**
  3. * User utilities.
  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\Utilities
  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_utils_users"))
  21. {
  22. /**
  23. * User utilities.
  24. *
  25. * @package s2Member\Utilities
  26. * @since 3.5
  27. */
  28. class c_ws_plugin__s2member_utils_users
  29. {
  30. /**
  31. * Determines the total Users/Members in the database.
  32. *
  33. * @package s2Member\Utilities
  34. * @since 3.5
  35. *
  36. * @return int Number of Users in the database, total.
  37. */
  38. public static function users_in_database ()
  39. {
  40. global /* Global database object reference. */ $wpdb;
  41.  
  42. $q1 = mysql_query ("SELECT SQL_CALC_FOUND_ROWS `" . $wpdb->users . "`.`ID` FROM `" . $wpdb->users . "`, `" . $wpdb->usermeta . "` WHERE `" . $wpdb->users . "`.`ID` = `" . $wpdb->usermeta . "`.`user_id` AND `" . $wpdb->usermeta . "`.`meta_key` = '" . esc_sql ($wpdb->prefix . "capabilities") . "' LIMIT 1", $wpdb->dbh);
  43. $q2 = mysql_query ("SELECT FOUND_ROWS()", $wpdb->dbh);
  44.  
  45. $users = (int)mysql_result ($q2, 0);
  46.  
  47. mysql_free_result ($q2);
  48. mysql_free_result ($q1);
  49.  
  50. return $users;
  51. }
  52. /**
  53. * Obtains Custom String for an existing Member, referenced by a Subscr. or Transaction ID.
  54. *
  55. * A second lookup parameter can be provided as well *(optional)*.
  56. *
  57. * @package s2Member\Utilities
  58. * @since 3.5
  59. *
  60. * @param str $subscr_or_txn_id Either a Paid Subscr. ID, or a Paid Transaction ID.
  61. * @param str $os0 Optional. A second lookup parameter, usually the `os0` value for PayPal® integrations.
  62. * @return str|bool The Custom String value on success, else false on failure.
  63. */
  64. public static function get_user_custom_with ($subscr_or_txn_id = FALSE, $os0 = FALSE)
  65. {
  66. global /* Need global DB obj. */ $wpdb;
  67.  
  68. if /* This case includes some additional routines that can use the ``$os0`` value. */ ($subscr_or_txn_id && $os0)
  69. {
  70. if (($q = $wpdb->get_row ("SELECT `user_id` FROM `" . $wpdb->usermeta . "` WHERE (`meta_key` = '" . $wpdb->prefix . "s2member_subscr_id' OR `meta_key` = '" . $wpdb->prefix . "s2member_first_payment_txn_id') AND (`meta_value` = '" . $wpdb->escape ($subscr_or_txn_id) . "' OR `meta_value` = '" . $wpdb->escape ($os0) . "') LIMIT 1"))
  71. || ($q = $wpdb->get_row ("SELECT `ID` AS `user_id` FROM `" . $wpdb->users . "` WHERE `ID` = '" . $wpdb->escape ($os0) . "' LIMIT 1")))
  72. if (($custom = get_user_option ("s2member_custom", $q->user_id)))
  73. return $custom;
  74. }
  75. else if /* Otherwise, if all we have is a Subscr./Txn. ID value. */ ($subscr_or_txn_id)
  76. {
  77. if (($q = $wpdb->get_row ("SELECT `user_id` FROM `" . $wpdb->usermeta . "` WHERE (`meta_key` = '" . $wpdb->prefix . "s2member_subscr_id' OR `meta_key` = '" . $wpdb->prefix . "s2member_first_payment_txn_id') AND `meta_value` = '" . $wpdb->escape ($subscr_or_txn_id) . "' LIMIT 1")))
  78. if (($custom = get_user_option ("s2member_custom", $q->user_id)))
  79. return $custom;
  80. }
  81. return /* Otherwise, return false. */ false;
  82. }
  83. /**
  84. * Obtains the User ID for an existing Member, referenced by a Subscr. or Transaction ID.
  85. *
  86. * A second lookup parameter can be provided as well *(optional)*.
  87. *
  88. * @package s2Member\Utilities
  89. * @since 3.5
  90. *
  91. * @param str $subscr_or_txn_id Either a Paid Subscr. ID, or a Paid Transaction ID.
  92. * @param str $os0 Optional. A second lookup parameter, usually the `os0` value for PayPal® integrations.
  93. * @return int|bool A WordPress® User ID on success, else false on failure.
  94. */
  95. public static function get_user_id_with ($subscr_or_txn_id = FALSE, $os0 = FALSE)
  96. {
  97. global /* Need global DB obj. */ $wpdb;
  98.  
  99. if /* This case includes some additional routines that can use the ``$os0`` value. */($subscr_or_txn_id && $os0)
  100. {
  101. if (($q = $wpdb->get_row ("SELECT `user_id` FROM `" . $wpdb->usermeta . "` WHERE (`meta_key` = '" . $wpdb->prefix . "s2member_subscr_id' OR `meta_key` = '" . $wpdb->prefix . "s2member_first_payment_txn_id') AND (`meta_value` = '" . $wpdb->escape ($subscr_or_txn_id) . "' OR `meta_value` = '" . $wpdb->escape ($os0) . "') LIMIT 1"))
  102. || ($q = $wpdb->get_row ("SELECT `ID` AS `user_id` FROM `" . $wpdb->users . "` WHERE `ID` = '" . $wpdb->escape ($os0) . "' LIMIT 1")))
  103. return $q->user_id;
  104. }
  105. else if /* Otherwise, if all we have is a Subscr./Txn. ID value. */ ($subscr_or_txn_id)
  106. {
  107. if (($q = $wpdb->get_row ("SELECT `user_id` FROM `" . $wpdb->usermeta . "` WHERE (`meta_key` = '" . $wpdb->prefix . "s2member_subscr_id' OR `meta_key` = '" . $wpdb->prefix . "s2member_first_payment_txn_id') AND `meta_value` = '" . $wpdb->escape ($subscr_or_txn_id) . "' LIMIT 1")))
  108. return $q->user_id;
  109. }
  110. return /* Otherwise, return false. */ false;
  111. }
  112. /**
  113. * Obtains the Email Address for an existing Member, referenced by a Subscr. or Transaction ID.
  114. *
  115. * A second lookup parameter can be provided as well *(optional)*.
  116. *
  117. * @package s2Member\Utilities
  118. * @since 3.5
  119. *
  120. * @param str $subscr_or_txn_id Either a Paid Subscr. ID, or a Paid Transaction ID.
  121. * @param str $os0 Optional. A second lookup parameter, usually the `os0` value for PayPal® integrations.
  122. * @return int|bool A User's Email Address on success, else false on failure.
  123. */
  124. public static function get_user_email_with ($subscr_or_txn_id = FALSE, $os0 = FALSE)
  125. {
  126. global /* Need global DB obj. */ $wpdb;
  127.  
  128. if /* This case includes some additional routines that can use the ``$os0`` value. */($subscr_or_txn_id && $os0)
  129. {
  130. if (($q = $wpdb->get_row ("SELECT `user_id` FROM `" . $wpdb->usermeta . "` WHERE (`meta_key` = '" . $wpdb->prefix . "s2member_subscr_id' OR `meta_key` = '" . $wpdb->prefix . "s2member_first_payment_txn_id') AND (`meta_value` = '" . $wpdb->escape ($subscr_or_txn_id) . "' OR `meta_value` = '" . $wpdb->escape ($os0) . "') LIMIT 1"))
  131. || ($q = $wpdb->get_row ("SELECT `ID` AS `user_id` FROM `" . $wpdb->users . "` WHERE `ID` = '" . $wpdb->escape ($os0) . "' LIMIT 1")))
  132. if (is_object ($user = new WP_User ($q->user_id)) && !empty ($user->ID) && ($email = $user->user_email))
  133. return $email;
  134. }
  135. else if /* Otherwise, if all we have is a Subscr./Txn. ID value. */($subscr_or_txn_id)
  136. {
  137. if (($q = $wpdb->get_row ("SELECT `user_id` FROM `" . $wpdb->usermeta . "` WHERE (`meta_key` = '" . $wpdb->prefix . "s2member_subscr_id' OR `meta_key` = '" . $wpdb->prefix . "s2member_first_payment_txn_id') AND `meta_value` = '" . $wpdb->escape ($subscr_or_txn_id) . "' LIMIT 1")))
  138. if (is_object ($user = new WP_User ($q->user_id)) && !empty ($user->ID) && ($email = $user->user_email))
  139. return $email;
  140. }
  141. return /* Otherwise, return false. */ false;
  142. }
  143. /**
  144. * Retrieves IPN Signup Vars & validates their Subscription ID.
  145. *
  146. * The ``$user_id`` can be passed in directly; or a lookup can be performed with ``$subscr_id``.
  147. *
  148. * @package s2Member\Utilities
  149. * @since 3.5
  150. *
  151. * @param int|str $user_id Optional. A numeric WordPress® User ID.
  152. * @param str $subscr_id Optional. Can be used instead of passing in a ``$user_id``.
  153. * If ``$subscr_id`` is passed in, it has to match the one found inside the resulting IPN Signup Vars collected by this routine.
  154. * If neither of these parameters are passed in, the current User is assumed instead, obtained through ``wp_get_current_user()``.
  155. * @return array|bool A User's IPN Signup Vars on success, else false on failure.
  156. */
  157. public static function get_user_ipn_signup_vars ($user_id = FALSE, $subscr_id = FALSE)
  158. {
  159. if ($user_id || ($subscr_id && ($user_id = c_ws_plugin__s2member_utils_users::get_user_id_with ($subscr_id))) || (!$user_id && !$subscr_id && is_object ($user = wp_get_current_user ()) && !empty ($user->ID) && ($user_id = $user->ID)))
  160. {
  161. if (($_subscr_id = get_user_option ("s2member_subscr_id", $user_id)) && (!$subscr_id || $subscr_id === $_subscr_id) && ($subscr_id = $_subscr_id))
  162. if (is_array ($ipn_signup_vars = get_user_option ("s2member_ipn_signup_vars", $user_id)))
  163. if ($ipn_signup_vars["subscr_id"] === $subscr_id)
  164. return $ipn_signup_vars;
  165. }
  166. return /* Otherwise, return false. */ false;
  167. }
  168. /**
  169. * Retrieves IPN Signup Var & validates their Subscription ID.
  170. *
  171. * The ``$user_id`` can be passed in directly; or a lookup can be performed with ``$subscr_id``.
  172. *
  173. * @package s2Member\Utilities
  174. * @since 110912
  175. *
  176. * @param str $var Required. The requested Signup Var.
  177. * @param int|str $user_id Optional. A numeric WordPress® User ID.
  178. * @param str $subscr_id Optional. Can be used instead of passing in a ``$user_id``.
  179. * If ``$subscr_id`` is passed in, it has to match the one found inside the resulting IPN Signup Vars collected by this routine.
  180. * If neither of these parameters are passed in, the current User is assumed instead, obtained through ``wp_get_current_user()``.
  181. * @return mixed|bool A User's IPN Signup Var on success, else false on failure.
  182. */
  183. public static function get_user_ipn_signup_var ($var = FALSE, $user_id = FALSE, $subscr_id = FALSE)
  184. {
  185. if (!empty ($var) && is_array ($user_ipn_signup_vars = c_ws_plugin__s2member_utils_users::get_user_ipn_signup_vars ($user_id, $subscr_id)))
  186. {
  187. if /* Available? */(isset ($user_ipn_signup_vars[$var]))
  188. return $user_ipn_signup_vars[$var];
  189. }
  190. return /* Otherwise, return false. */ false;
  191. }
  192. /**
  193. * Obtains a User's Paid Subscr. ID *(if available)*; otherwise their WP User ID.
  194. *
  195. * If ``$user`` IS passed in, this function will return data from a specific ``$user``, or fail if not possible.
  196. * If ``$user`` is NOT passed in, check the current User/Member.
  197. *
  198. * @package s2Member\Utilities
  199. * @since 3.5
  200. *
  201. * @param obj $user Optional. A `WP_User` object.
  202. * In order to check the current User, you must call this function with no arguments/parameters.
  203. * @return int|str|bool If possible, the User's Paid Subscr. ID, else their WordPress® User ID, else false.
  204. */
  205. public static function get_user_subscr_or_wp_id ($user = FALSE)
  206. {
  207. if ((func_num_args () && (!is_object ($user) || empty ($user->ID))) || (!func_num_args () && (!is_object ($user = (is_user_logged_in ()) ? wp_get_current_user () : false) || empty ($user->ID))))
  208. {
  209. return /* The ``$user`` was passed in but is NOT an object; or nobody is logged in. */ false;
  210. }
  211. else // Else return Paid Subscr. ID (if available), otherwise return their WP database User ID.
  212. return ($subscr_id = get_user_option ("s2member_subscr_id", $user->ID)) ? $subscr_id : $user->ID;
  213. }
  214. /**
  215. * Determines whether or not a Username/Email is already in the database.
  216. *
  217. * Returns the WordPress® User ID if they exist.
  218. *
  219. * @package s2Member\Utilities
  220. * @since 3.5
  221. *
  222. * @param str $user_login A User's Username.
  223. * @param str $user_email A User's Email Address.
  224. * @return int|bool If exists, a WordPress® User ID, else false.
  225. */
  226. public static function user_login_email_exists ($user_login = FALSE, $user_email = FALSE)
  227. {
  228. global /* Global database object reference. */ $wpdb;
  229.  
  230. if /* Only if we have both of these. */ ($user_login && $user_email)
  231. if (($user_id = $wpdb->get_var ("SELECT `ID` FROM `" . $wpdb->users . "` WHERE `user_login` LIKE '" . esc_sql (like_escape ($user_login)) . "' AND `user_email` LIKE '" . esc_sql (like_escape ($user_email)) . "' LIMIT 1")))
  232. return /* Return the associated WordPress® ID. */$user_id;
  233.  
  234. return /* Else return false. */false;
  235. }
  236. /**
  237. * Determines whether or not a Username/Email is already in the database for this Blog.
  238. *
  239. * Returns the WordPress® User ID if they exist.
  240. *
  241. * @package s2Member\Utilities
  242. * @since 3.5
  243. *
  244. * @param str $user_login A User's Username.
  245. * @param str $user_email A User's Email Address.
  246. * @param int|str $blog_id A numeric WordPress® Blog ID.
  247. * @return int|bool If exists *(but not on Blog)*, a WordPress® User ID, else false.
  248. */
  249. public static function ms_user_login_email_exists_but_not_on_blog ($user_login = FALSE, $user_email = FALSE, $blog_id = FALSE)
  250. {
  251. if /* Only if we have both of these. */ ($user_login && $user_email)
  252. if (is_multisite () && ($user_id = c_ws_plugin__s2member_utils_users::user_login_email_exists ($user_login, $user_email)) && !is_user_member_of_blog ($user_id, $blog_id))
  253. return $user_id;
  254.  
  255. return /* Else return false. */ false;
  256. }
  257. /**
  258. * Determines whether or not a Username/Email is already in the database for this Blog.
  259. *
  260. * This is an alias for: `c_ws_plugin__s2member_utils_users::ms_user_login_email_exists_but_not_on_blog()`.
  261. *
  262. * Returns the WordPress® User ID if they exist.
  263. *
  264. * @package s2Member\Utilities
  265. * @since 3.5
  266. *
  267. * @param str $user_login A User's Username.
  268. * @param str $user_email A User's Email Address.
  269. * @param int|str $blog_id A numeric WordPress® Blog ID.
  270. * @return int|bool If exists *(but not on Blog)*, a WordPress® User ID, else false.
  271. */
  272. public static function ms_user_login_email_can_join_blog ($user_login = FALSE, $user_email = FALSE, $blog_id = FALSE)
  273. {
  274. return c_ws_plugin__s2member_utils_users::ms_user_login_email_exists_but_not_on_blog ($user_login, $user_email, $blog_id);
  275. }
  276. /**
  277. * Retrieves a field value. Also supports Custom Fields.
  278. *
  279. * @package s2Member\Utilities
  280. * @since 3.5
  281. *
  282. * @param str $field_id Required. A unique Custom Registration/Profile Field ID, that you configured with s2Member.
  283. * Or, this could be set to any property that exists on the WP_User object for a particular User;
  284. * ( i.e. `id`, `ID`, `user_login`, `user_email`, `first_name`, `last_name`, `display_name`, `ip`, `IP`,
  285. * `s2member_registration_ip`, `s2member_custom`, `s2member_subscr_id`, `s2member_subscr_or_wp_id`,
  286. * `s2member_subscr_gateway`, `s2member_custom_fields`, `s2member_file_download_access_[log|arc]`,
  287. * `s2member_auto_eot_time`, `s2member_last_payment_time`, `s2member_paid_registration_times`,
  288. * `s2member_access_role`, `s2member_access_level`, `s2member_access_label`,
  289. * `s2member_access_ccaps`, etc, etc. ).
  290. * @param int|str $user_id Optional. Defaults to the current User's ID.
  291. * @return mixed The value of the requested field, or false if the field does not exist.
  292. */
  293. public static function get_user_field ($field_id = FALSE, $user_id = FALSE) // Very powerful function here.
  294. {
  295. global /* Global database object reference. */ $wpdb;
  296.  
  297. $current_user = /* Current User's object (used when/if `$user_id` is empty). */ wp_get_current_user ();
  298.  
  299. if (is_object ($user = ($user_id) ? new WP_User ($user_id) : $current_user) && !empty ($user->ID) && ($user_id = $user->ID))
  300. {
  301. if /* Immediate User object property? (most likely) */(isset ($user->$field_id))
  302. return $user->$field_id;
  303.  
  304. else if /* Also try the data object property. */ (isset ($user->data->$field_id))
  305. return $user->data->$field_id;
  306.  
  307. else if /* Immediate prefixed? */ (isset ($user->{$wpdb->prefix . $field_id}))
  308. return $user->{$wpdb->prefix . $field_id};
  309.  
  310. else if /* Data prefixed? */ (isset ($user->data->{$wpdb->prefix . $field_id}))
  311. return $user->data->{$wpdb->prefix . $field_id};
  312.  
  313. else if /* First/last full name? */ (strcasecmp ($field_id, "full_name") === 0)
  314. return trim ($user->first_name . " " . $user->last_name);
  315.  
  316. else if /* Email address? */ (preg_match ("/^(email|user_email)$/i", $field_id))
  317. return $user->user_email;
  318.  
  319. else if /* Username / login? */ (preg_match ("/^(login|user_login)$/i", $field_id))
  320. return $user->user_login;
  321.  
  322. else if /* Role name/ID? */ (strcasecmp ($field_id, "s2member_access_role") === 0)
  323. return c_ws_plugin__s2member_user_access::user_access_role ($user);
  324.  
  325. else if /* Access Level? */ (strcasecmp ($field_id, "s2member_access_level") === 0)
  326. return c_ws_plugin__s2member_user_access::user_access_level ($user);
  327.  
  328. else if /* Access Label? */ (strcasecmp ($field_id, "s2member_access_label") === 0)
  329. return c_ws_plugin__s2member_user_access::user_access_label ($user);
  330.  
  331. else if /* Custom Caps? */ (strcasecmp ($field_id, "s2member_access_ccaps") === 0)
  332. return c_ws_plugin__s2member_user_access::user_access_ccaps ($user);
  333.  
  334. else if (strcasecmp ($field_id, "ip") === 0 && is_object ($current_user) && !empty ($current_user->ID) && $current_user->ID === ($user_id = $user->ID))
  335. return /* The current User's IP address, right now. */ $_SERVER["REMOTE_ADDR"];
  336.  
  337. else if (strcasecmp ($field_id, "s2member_registration_ip") === 0 || strcasecmp ($field_id, "reg_ip") === 0 || strcasecmp ($field_id, "ip") === 0)
  338. return get_user_option ("s2member_registration_ip", $user_id);
  339.  
  340. else if (strcasecmp ($field_id, "s2member_subscr_or_wp_id") === 0)
  341. return ($subscr_id = get_user_option ("s2member_subscr_id", $user_id)) ? $subscr_id : $user_id;
  342.  
  343. else if (is_array ($fields = get_user_option ("s2member_custom_fields", $user_id)))
  344. if (isset ($fields[preg_replace ("/[^a-z0-9]/i", "_", strtolower ($field_id))]))
  345. return $fields[preg_replace ("/[^a-z0-9]/i", "_", strtolower ($field_id))];
  346. }
  347. return /* Default, return false. */ false;
  348. }
  349. }
  350. }
  351. ?>

s2member\includes\classes\utils-users.inc.php

Source Code Documentation