/wp-includes/class-wp-role.php

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