/wp-content/plugins/buddypress/bp-forums/bbpress/bb-includes/backpress/class.bp-user.php

https://github.com/jazbek/nycga2 · PHP · 398 lines · 111 code · 37 blank · 250 comment · 16 complexity · 208a6814f765733716d317fca5b8031d MD5 · raw file

  1. <?php
  2. // Last sync [WP11537]
  3. /**
  4. * BackPress User class.
  5. *
  6. * @since 2.0.0
  7. * @package BackPress
  8. * @subpackage User
  9. */
  10. class BP_User {
  11. /**
  12. * User data container.
  13. *
  14. * This will be set as properties of the object.
  15. *
  16. * @since 2.0.0
  17. * @access private
  18. * @var array
  19. */
  20. var $data;
  21. /**
  22. * The user's ID.
  23. *
  24. * @since 2.1.0
  25. * @access public
  26. * @var int
  27. */
  28. var $ID = 0;
  29. /**
  30. * The deprecated user's ID.
  31. *
  32. * @since 2.0.0
  33. * @access public
  34. * @deprecated Use BP_User::$ID
  35. * @see BP_User::$ID
  36. * @var int
  37. */
  38. var $id = 0;
  39. /**
  40. * The individual capabilities the user has been given.
  41. *
  42. * @since 2.0.0
  43. * @access public
  44. * @var array
  45. */
  46. var $caps = array();
  47. /**
  48. * User metadata option name.
  49. *
  50. * @since 2.0.0
  51. * @access public
  52. * @var string
  53. */
  54. var $cap_key;
  55. /**
  56. * The roles the user is part of.
  57. *
  58. * @since 2.0.0
  59. * @access public
  60. * @var array
  61. */
  62. var $roles = array();
  63. /**
  64. * All capabilities the user has, including individual and role based.
  65. *
  66. * @since 2.0.0
  67. * @access public
  68. * @var array
  69. */
  70. var $allcaps = array();
  71. /**
  72. * First name of the user.
  73. *
  74. * Created to prevent notices.
  75. *
  76. * @since 2.7.0
  77. * @access public
  78. * @var string
  79. */
  80. var $first_name = '';
  81. /**
  82. * Last name of the user.
  83. *
  84. * Created to prevent notices.
  85. *
  86. * @since 2.7.0
  87. * @access public
  88. * @var string
  89. */
  90. var $last_name = '';
  91. /**
  92. * PHP4 Constructor - Sets up the object properties.
  93. *
  94. * Retrieves the userdata and then assigns all of the data keys to direct
  95. * properties of the object. Calls {@link BP_User::_init_caps()} after
  96. * setting up the object's user data properties.
  97. *
  98. * @since 2.0.0
  99. * @access public
  100. *
  101. * @param int|string $id User's ID or username
  102. * @param int $name Optional. User's username
  103. * @return BP_User
  104. */
  105. function BP_User( $id, $name = '' ) {
  106. global $wp_users_object;
  107. if ( empty( $id ) && empty( $name ) )
  108. return;
  109. if ( ! is_numeric( $id ) ) {
  110. $name = $id;
  111. $id = 0;
  112. }
  113. if ( ! empty( $id ) )
  114. $this->data = $wp_users_object->get_user( $id );
  115. else
  116. $this->data = $wp_users_object->get_user( $name, array( 'by' => 'login' ) );
  117. if ( empty( $this->data->ID ) )
  118. return;
  119. foreach ( get_object_vars( $this->data ) as $key => $value ) {
  120. $this->{$key} = $value;
  121. }
  122. $this->id = $this->ID;
  123. $this->_init_caps();
  124. }
  125. /**
  126. * Setup capability object properties.
  127. *
  128. * Will set the value for the 'cap_key' property to current database table
  129. * prefix, followed by 'capabilities'. Will then check to see if the
  130. * property matching the 'cap_key' exists and is an array. If so, it will be
  131. * used.
  132. *
  133. * @since 2.1.0
  134. * @access protected
  135. */
  136. function _init_caps() {
  137. global $wp_users_object;
  138. $this->cap_key = $wp_users_object->db->prefix . 'capabilities';
  139. $this->caps = &$this->{$this->cap_key};
  140. if ( ! is_array( $this->caps ) )
  141. $this->caps = array();
  142. $this->get_role_caps();
  143. }
  144. /**
  145. * Retrieve all of the role capabilities and merge with individual capabilities.
  146. *
  147. * All of the capabilities of the roles the user belongs to are merged with
  148. * the users individual roles. This also means that the user can be denied
  149. * specific roles that their role might have, but the specific user isn't
  150. * granted permission to.
  151. *
  152. * @since 2.0.0
  153. * @uses $wp_roles
  154. * @access public
  155. */
  156. function get_role_caps() {
  157. global $wp_roles, $wp_users_object;
  158. if ( ! isset( $wp_roles ) )
  159. $wp_roles = new BP_Roles( $wp_users_object->db );
  160. //Filter out caps that are not role names and assign to $this->roles
  161. if ( is_array( $this->caps ) )
  162. $this->roles = array_filter( array_keys( $this->caps ), array( &$wp_roles, 'is_role' ) );
  163. //Build $allcaps from role caps, overlay user's $caps
  164. $this->allcaps = array();
  165. foreach ( (array) $this->roles as $role ) {
  166. $role = $wp_roles->get_role( $role );
  167. $this->allcaps = array_merge( (array) $this->allcaps, (array) $role->capabilities );
  168. }
  169. $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
  170. }
  171. /**
  172. * Add role to user.
  173. *
  174. * Updates the user's meta data option with capabilities and roles.
  175. *
  176. * @since 2.0.0
  177. * @access public
  178. *
  179. * @param string $role Role name.
  180. */
  181. function add_role( $role ) {
  182. $this->caps[$role] = true;
  183. $this->update_user();
  184. }
  185. /**
  186. * Remove role from user.
  187. *
  188. * @since 2.0.0
  189. * @access public
  190. *
  191. * @param string $role Role name.
  192. */
  193. function remove_role( $role ) {
  194. if ( empty( $this->caps[$role] ) || ( count( $this->caps ) <= 1 ) )
  195. return;
  196. unset( $this->caps[$role] );
  197. $this->update_user();
  198. }
  199. /**
  200. * Set the role of the user.
  201. *
  202. * This will remove the previous roles of the user and assign the user the
  203. * new one. You can set the role to an empty string and it will remove all
  204. * of the roles from the user.
  205. *
  206. * @since 2.0.0
  207. * @access public
  208. *
  209. * @param string $role Role name.
  210. */
  211. function set_role( $role ) {
  212. foreach ( (array) $this->roles as $oldrole )
  213. unset( $this->caps[$oldrole] );
  214. if ( !empty( $role ) ) {
  215. $this->caps[$role] = true;
  216. $this->roles = array( $role => true );
  217. } else {
  218. $this->roles = false;
  219. }
  220. $this->update_user();
  221. }
  222. function update_user() {
  223. global $wp_users_object;
  224. $wp_users_object->update_meta( array( 'id' => $this->ID, 'meta_key' => $this->cap_key, 'meta_value' => $this->caps ) );
  225. $this->get_role_caps();
  226. //$this->update_user_level_from_caps(); // WP
  227. }
  228. /**
  229. * Choose the maximum level the user has.
  230. *
  231. * Will compare the level from the $item parameter against the $max
  232. * parameter. If the item is incorrect, then just the $max parameter value
  233. * will be returned.
  234. *
  235. * Used to get the max level based on the capabilities the user has. This
  236. * is also based on roles, so if the user is assigned the Administrator role
  237. * then the capability 'level_10' will exist and the user will get that
  238. * value.
  239. *
  240. * @since 2.0.0
  241. * @access public
  242. *
  243. * @param int $max Max level of user.
  244. * @param string $item Level capability name.
  245. * @return int Max Level.
  246. */
  247. /*
  248. function level_reduction( $max, $item ) {
  249. if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
  250. $level = intval( $matches[1] );
  251. return max( $max, $level );
  252. } else {
  253. return $max;
  254. }
  255. }
  256. */
  257. /**
  258. * Update the maximum user level for the user.
  259. *
  260. * Updates the 'user_level' user metadata (includes prefix that is the
  261. * database table prefix) with the maximum user level. Gets the value from
  262. * the all of the capabilities that the user has.
  263. *
  264. * @since 2.0.0
  265. * @access public
  266. */
  267. /*
  268. function update_user_level_from_caps() {
  269. global $wp_users_object;
  270. $this->user_level = array_reduce( array_keys( $this->allcaps ), array( &$this, 'level_reduction' ), 0 );
  271. update_usermeta( $this->ID, $wpdb->prefix.'user_level', $this->user_level );
  272. }
  273. */
  274. /*
  275. function translate_level_to_cap($level) {
  276. return 'level_' . $level;
  277. }
  278. */
  279. /**
  280. * Add capability and grant or deny access to capability.
  281. *
  282. * @since 2.0.0
  283. * @access public
  284. *
  285. * @param string $cap Capability name.
  286. * @param bool $grant Whether to grant capability to user.
  287. */
  288. function add_cap( $cap, $grant = true ) {
  289. $this->caps[$cap] = $grant;
  290. $this->update_user();
  291. }
  292. /**
  293. * Remove capability from user.
  294. *
  295. * @since 2.0.0
  296. * @access public
  297. *
  298. * @param string $cap Capability name.
  299. */
  300. function remove_cap( $cap ) {
  301. if ( empty( $this->caps[$cap] ) ) return;
  302. unset( $this->caps[$cap] );
  303. $this->update_user();
  304. }
  305. /**
  306. * Remove all of the capabilities of the user.
  307. *
  308. * @since 2.1.0
  309. * @access public
  310. */
  311. function remove_all_caps() {
  312. global $wp_users_object;
  313. $this->caps = array();
  314. $wp_users_object->delete_meta( $this->ID, $this->cap_key );
  315. $this->get_role_caps();
  316. }
  317. /**
  318. * Whether user has capability or role name.
  319. *
  320. * This is useful for looking up whether the user has a specific role
  321. * assigned to the user. The second optional parameter can also be used to
  322. * check for capabilities against a specfic post.
  323. *
  324. * @since 2.0.0
  325. * @access public
  326. *
  327. * @param string|int $cap Capability or role name to search.
  328. * @param int $post_id Optional. Post ID to check capability against specific post.
  329. * @return bool True, if user has capability; false, if user does not have capability.
  330. */
  331. function has_cap( $cap ) {
  332. global $wp_roles;
  333. if ( is_numeric( $cap ) )
  334. $cap = $this->translate_level_to_cap( $cap );
  335. $args = array_slice( func_get_args(), 1 );
  336. $args = array_merge( array( $cap, $this->ID ), $args );
  337. $caps = call_user_func_array( array(&$wp_roles, 'map_meta_cap'), $args );
  338. // Must have ALL requested caps
  339. $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args );
  340. foreach ( (array) $caps as $cap ) {
  341. //echo "Checking cap $cap<br />";
  342. if ( empty( $capabilities[$cap] ) || !$capabilities[$cap] )
  343. return false;
  344. }
  345. return true;
  346. }
  347. /**
  348. * Convert numeric level to level capability name.
  349. *
  350. * Prepends 'level_' to level number.
  351. *
  352. * @since 2.0.0
  353. * @access public
  354. *
  355. * @param int $level Level number, 1 to 10.
  356. * @return string
  357. */
  358. function translate_level_to_cap( $level ) {
  359. return 'level_' . $level;
  360. }
  361. }