BP_User

BackPress User class.

Defined (1)

The class is defined in the following location(s).

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