[ Index ]

PHP Cross Reference of BackPress

title

Body

[close]

/includes/ -> class.bp-user.php (source)

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


Generated: Thu Dec 26 01:01:01 2024 Cross-referenced by PHPXref 0.7.1