class-wp-role.php 2.6 KB

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