[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> class-wp-roles.php (source)

   1  <?php
   2  /**
   3   * User API: WP_Roles class
   4   *
   5   * @package WordPress
   6   * @subpackage Users
   7   * @since 4.4.0
   8   */
   9  
  10  /**
  11   * Core class used to implement a user roles API.
  12   *
  13   * The role option is simple, the structure is organized by role name that store
  14   * the name in value of the 'name' key. The capabilities are stored as an array
  15   * in the value of the 'capability' key.
  16   *
  17   *     array (
  18   *          'rolename' => array (
  19   *              'name' => 'rolename',
  20   *              'capabilities' => array()
  21   *          )
  22   *     )
  23   *
  24   * @since 2.0.0
  25   */
  26  #[AllowDynamicProperties]
  27  class WP_Roles {
  28      /**
  29       * List of roles and capabilities.
  30       *
  31       * @since 2.0.0
  32       * @var array[]
  33       */
  34      public $roles;
  35  
  36      /**
  37       * List of the role objects.
  38       *
  39       * @since 2.0.0
  40       * @var WP_Role[]
  41       */
  42      public $role_objects = array();
  43  
  44      /**
  45       * List of role names.
  46       *
  47       * @since 2.0.0
  48       * @var string[]
  49       */
  50      public $role_names = array();
  51  
  52      /**
  53       * Option name for storing role list.
  54       *
  55       * @since 2.0.0
  56       * @var string
  57       */
  58      public $role_key;
  59  
  60      /**
  61       * Whether to use the database for retrieval and storage.
  62       *
  63       * @since 2.1.0
  64       * @var bool
  65       */
  66      public $use_db = true;
  67  
  68      /**
  69       * The site ID the roles are initialized for.
  70       *
  71       * @since 4.9.0
  72       * @var int
  73       */
  74      protected $site_id = 0;
  75  
  76      /**
  77       * Constructor.
  78       *
  79       * @since 2.0.0
  80       * @since 4.9.0 The `$site_id` argument was added.
  81       *
  82       * @global array $wp_user_roles Used to set the 'roles' property value.
  83       *
  84       * @param int $site_id Site ID to initialize roles for. Default is the current site.
  85       */
  86  	public function __construct( $site_id = null ) {
  87          global $wp_user_roles;
  88  
  89          $this->use_db = empty( $wp_user_roles );
  90  
  91          $this->for_site( $site_id );
  92      }
  93  
  94      /**
  95       * Makes private/protected methods readable for backward compatibility.
  96       *
  97       * @since 4.0.0
  98       *
  99       * @param string $name      Method to call.
 100       * @param array  $arguments Arguments to pass when calling.
 101       * @return mixed|false Return value of the callback, false otherwise.
 102       */
 103  	public function __call( $name, $arguments ) {
 104          if ( '_init' === $name ) {
 105              return $this->_init( ...$arguments );
 106          }
 107          return false;
 108      }
 109  
 110      /**
 111       * Sets up the object properties.
 112       *
 113       * The role key is set to the current prefix for the $wpdb object with
 114       * 'user_roles' appended. If the $wp_user_roles global is set, then it will
 115       * be used and the role option will not be updated or used.
 116       *
 117       * @since 2.1.0
 118       * @deprecated 4.9.0 Use WP_Roles::for_site()
 119       */
 120  	protected function _init() {
 121          _deprecated_function( __METHOD__, '4.9.0', 'WP_Roles::for_site()' );
 122  
 123          $this->for_site();
 124      }
 125  
 126      /**
 127       * Reinitializes the object.
 128       *
 129       * Recreates the role objects. This is typically called only by switch_to_blog()
 130       * after switching wpdb to a new site ID.
 131       *
 132       * @since 3.5.0
 133       * @deprecated 4.7.0 Use WP_Roles::for_site()
 134       */
 135  	public function reinit() {
 136          _deprecated_function( __METHOD__, '4.7.0', 'WP_Roles::for_site()' );
 137  
 138          $this->for_site();
 139      }
 140  
 141      /**
 142       * Adds a role name with capabilities to the list.
 143       *
 144       * Updates the list of roles, if the role doesn't already exist.
 145       *
 146       * The capabilities are defined in the following format: `array( 'read' => true )`.
 147       * To explicitly deny the role a capability, set the value for that capability to false.
 148       *
 149       * @since 2.0.0
 150       *
 151       * @param string $role         Role name.
 152       * @param string $display_name Role display name.
 153       * @param bool[] $capabilities Optional. List of capabilities keyed by the capability name,
 154       *                             e.g. `array( 'edit_posts' => true, 'delete_posts' => false )`.
 155       *                             Default empty array.
 156       * @return WP_Role|void WP_Role object, if the role is added.
 157       */
 158  	public function add_role( $role, $display_name, $capabilities = array() ) {
 159          if ( empty( $role ) || isset( $this->roles[ $role ] ) ) {
 160              return;
 161          }
 162  
 163          $this->roles[ $role ] = array(
 164              'name'         => $display_name,
 165              'capabilities' => $capabilities,
 166          );
 167          if ( $this->use_db ) {
 168              update_option( $this->role_key, $this->roles );
 169          }
 170          $this->role_objects[ $role ] = new WP_Role( $role, $capabilities );
 171          $this->role_names[ $role ]   = $display_name;
 172          return $this->role_objects[ $role ];
 173      }
 174  
 175      /**
 176       * Removes a role by name.
 177       *
 178       * @since 2.0.0
 179       *
 180       * @param string $role Role name.
 181       */
 182  	public function remove_role( $role ) {
 183          if ( ! isset( $this->role_objects[ $role ] ) ) {
 184              return;
 185          }
 186  
 187          unset( $this->role_objects[ $role ] );
 188          unset( $this->role_names[ $role ] );
 189          unset( $this->roles[ $role ] );
 190  
 191          if ( $this->use_db ) {
 192              update_option( $this->role_key, $this->roles );
 193          }
 194  
 195          if ( get_option( 'default_role' ) === $role ) {
 196              update_option( 'default_role', 'subscriber' );
 197          }
 198      }
 199  
 200      /**
 201       * Adds a capability to role.
 202       *
 203       * @since 2.0.0
 204       *
 205       * @param string $role  Role name.
 206       * @param string $cap   Capability name.
 207       * @param bool   $grant Optional. Whether role is capable of performing capability.
 208       *                      Default true.
 209       */
 210  	public function add_cap( $role, $cap, $grant = true ) {
 211          if ( ! isset( $this->roles[ $role ] ) ) {
 212              return;
 213          }
 214  
 215          $this->roles[ $role ]['capabilities'][ $cap ] = $grant;
 216          if ( $this->use_db ) {
 217              update_option( $this->role_key, $this->roles );
 218          }
 219      }
 220  
 221      /**
 222       * Removes a capability from role.
 223       *
 224       * @since 2.0.0
 225       *
 226       * @param string $role Role name.
 227       * @param string $cap  Capability name.
 228       */
 229  	public function remove_cap( $role, $cap ) {
 230          if ( ! isset( $this->roles[ $role ] ) ) {
 231              return;
 232          }
 233  
 234          unset( $this->roles[ $role ]['capabilities'][ $cap ] );
 235          if ( $this->use_db ) {
 236              update_option( $this->role_key, $this->roles );
 237          }
 238      }
 239  
 240      /**
 241       * Retrieves a role object by name.
 242       *
 243       * @since 2.0.0
 244       *
 245       * @param string $role Role name.
 246       * @return WP_Role|null WP_Role object if found, null if the role does not exist.
 247       */
 248  	public function get_role( $role ) {
 249          if ( isset( $this->role_objects[ $role ] ) ) {
 250              return $this->role_objects[ $role ];
 251          } else {
 252              return null;
 253          }
 254      }
 255  
 256      /**
 257       * Retrieves a list of role names.
 258       *
 259       * @since 2.0.0
 260       *
 261       * @return string[] List of role names.
 262       */
 263  	public function get_names() {
 264          return $this->role_names;
 265      }
 266  
 267      /**
 268       * Determines whether a role name is currently in the list of available roles.
 269       *
 270       * @since 2.0.0
 271       *
 272       * @param string $role Role name to look up.
 273       * @return bool
 274       */
 275  	public function is_role( $role ) {
 276          return isset( $this->role_names[ $role ] );
 277      }
 278  
 279      /**
 280       * Initializes all of the available roles.
 281       *
 282       * @since 4.9.0
 283       */
 284  	public function init_roles() {
 285          if ( empty( $this->roles ) ) {
 286              return;
 287          }
 288  
 289          $this->role_objects = array();
 290          $this->role_names   = array();
 291          foreach ( array_keys( $this->roles ) as $role ) {
 292              $this->role_objects[ $role ] = new WP_Role( $role, $this->roles[ $role ]['capabilities'] );
 293              $this->role_names[ $role ]   = $this->roles[ $role ]['name'];
 294          }
 295  
 296          /**
 297           * Fires after the roles have been initialized, allowing plugins to add their own roles.
 298           *
 299           * @since 4.7.0
 300           *
 301           * @param WP_Roles $wp_roles A reference to the WP_Roles object.
 302           */
 303          do_action( 'wp_roles_init', $this );
 304      }
 305  
 306      /**
 307       * Sets the site to operate on. Defaults to the current site.
 308       *
 309       * @since 4.9.0
 310       *
 311       * @global wpdb $wpdb WordPress database abstraction object.
 312       *
 313       * @param int $site_id Site ID to initialize roles for. Default is the current site.
 314       */
 315  	public function for_site( $site_id = null ) {
 316          global $wpdb;
 317  
 318          if ( ! empty( $site_id ) ) {
 319              $this->site_id = absint( $site_id );
 320          } else {
 321              $this->site_id = get_current_blog_id();
 322          }
 323  
 324          $this->role_key = $wpdb->get_blog_prefix( $this->site_id ) . 'user_roles';
 325  
 326          if ( ! empty( $this->roles ) && ! $this->use_db ) {
 327              return;
 328          }
 329  
 330          $this->roles = $this->get_roles_data();
 331  
 332          $this->init_roles();
 333      }
 334  
 335      /**
 336       * Gets the ID of the site for which roles are currently initialized.
 337       *
 338       * @since 4.9.0
 339       *
 340       * @return int Site ID.
 341       */
 342  	public function get_site_id() {
 343          return $this->site_id;
 344      }
 345  
 346      /**
 347       * Gets the available roles data.
 348       *
 349       * @since 4.9.0
 350       *
 351       * @global array $wp_user_roles Used to set the 'roles' property value.
 352       *
 353       * @return array Roles array.
 354       */
 355  	protected function get_roles_data() {
 356          global $wp_user_roles;
 357  
 358          if ( ! empty( $wp_user_roles ) ) {
 359              return $wp_user_roles;
 360          }
 361  
 362          if ( is_multisite() && get_current_blog_id() !== $this->site_id ) {
 363              remove_action( 'switch_blog', 'wp_switch_roles_and_user', 1 );
 364  
 365              $roles = get_blog_option( $this->site_id, $this->role_key, array() );
 366  
 367              add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 );
 368  
 369              return $roles;
 370          }
 371  
 372          return get_option( $this->role_key, array() );
 373      }
 374  }


Generated : Fri Apr 19 08:20:01 2024 Cross-referenced by PHPXref