WP_Role

Core class used to extend the user roles API.

Defined (1)

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

/wp-includes/class-wp-role.php  
  1. class WP_Role { 
  2. /** 
  3. * Role name. 
  4. * @since 2.0.0 
  5. * @access public 
  6. * @var string 
  7. */ 
  8. public $name; 
  9.  
  10. /** 
  11. * List of capabilities the role contains. 
  12. * @since 2.0.0 
  13. * @access public 
  14. * @var array 
  15. */ 
  16. public $capabilities; 
  17.  
  18. /** 
  19. * Constructor - Set up object properties. 
  20. * The list of capabilities, must have the key as the name of the capability 
  21. * and the value a boolean of whether it is granted to the role. 
  22. * @since 2.0.0 
  23. * @access public 
  24. * @param string $role Role name. 
  25. * @param array $capabilities List of capabilities. 
  26. */ 
  27. public function __construct( $role, $capabilities ) { 
  28. $this->name = $role; 
  29. $this->capabilities = $capabilities; 
  30.  
  31. /** 
  32. * Assign role a capability. 
  33. * @since 2.0.0 
  34. * @access public 
  35. * @param string $cap Capability name. 
  36. * @param bool $grant Whether role has capability privilege. 
  37. */ 
  38. public function add_cap( $cap, $grant = true ) { 
  39. $this->capabilities[$cap] = $grant; 
  40. wp_roles()->add_cap( $this->name, $cap, $grant ); 
  41.  
  42. /** 
  43. * Removes a capability from a role. 
  44. * This is a container for WP_Roles::remove_cap() to remove the 
  45. * capability from the role. That is to say, that WP_Roles::remove_cap() 
  46. * implements the functionality, but it also makes sense to use this class,  
  47. * because you don't need to enter the role name. 
  48. * @since 2.0.0 
  49. * @access public 
  50. * @param string $cap Capability name. 
  51. */ 
  52. public function remove_cap( $cap ) { 
  53. unset( $this->capabilities[$cap] ); 
  54. wp_roles()->remove_cap( $this->name, $cap ); 
  55.  
  56. /** 
  57. * Determines whether the role has the given capability. 
  58. * The capabilities is passed through the {@see 'role_has_cap'} filter. 
  59. * The first parameter for the hook is the list of capabilities the class 
  60. * has assigned. The second parameter is the capability name to look for. 
  61. * The third and final parameter for the hook is the role name. 
  62. * @since 2.0.0 
  63. * @access public 
  64. * @param string $cap Capability name. 
  65. * @return bool True if the role has the given capability. False otherwise. 
  66. */ 
  67. public function has_cap( $cap ) { 
  68. /** 
  69. * Filters which capabilities a role has. 
  70. * @since 2.0.0 
  71. * @param array $capabilities Array of role capabilities. 
  72. * @param string $cap Capability name. 
  73. * @param string $name Role name. 
  74. */ 
  75. $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name ); 
  76.  
  77. if ( !empty( $capabilities[$cap] ) ) 
  78. return $capabilities[$cap]; 
  79. else 
  80. return false; 
  81.