class-wp-role.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #[AllowDynamicProperties]
  15. class WP_Role {
  16. /**
  17. * Role name.
  18. *
  19. * @since 2.0.0
  20. * @var string
  21. */
  22. public $name;
  23. /**
  24. * List of capabilities the role contains.
  25. *
  26. * @since 2.0.0
  27. * @var bool[] Array of key/value pairs where keys represent a capability name and boolean values
  28. * represent whether the role has that capability.
  29. */
  30. public $capabilities;
  31. /**
  32. * Constructor - Set up object properties.
  33. *
  34. * The list of capabilities must have the key as the name of the capability
  35. * and the value a boolean of whether it is granted to the role.
  36. *
  37. * @since 2.0.0
  38. *
  39. * @param string $role Role name.
  40. * @param bool[] $capabilities Array of key/value pairs where keys represent a capability name and boolean values
  41. * represent whether the role has that capability.
  42. */
  43. public function __construct( $role, $capabilities ) {
  44. $this->name = $role;
  45. $this->capabilities = $capabilities;
  46. }
  47. /**
  48. * Assign role a capability.
  49. *
  50. * @since 2.0.0
  51. *
  52. * @param string $cap Capability name.
  53. * @param bool $grant Whether role has capability privilege.
  54. */
  55. public function add_cap( $cap, $grant = true ) {
  56. $this->capabilities[ $cap ] = $grant;
  57. wp_roles()->add_cap( $this->name, $cap, $grant );
  58. }
  59. /**
  60. * Removes a capability from a role.
  61. *
  62. * @since 2.0.0
  63. *
  64. * @param string $cap Capability name.
  65. */
  66. public function remove_cap( $cap ) {
  67. unset( $this->capabilities[ $cap ] );
  68. wp_roles()->remove_cap( $this->name, $cap );
  69. }
  70. /**
  71. * Determines whether the role has the given capability.
  72. *
  73. * @since 2.0.0
  74. *
  75. * @param string $cap Capability name.
  76. * @return bool Whether the role has the given capability.
  77. */
  78. public function has_cap( $cap ) {
  79. /**
  80. * Filters which capabilities a role has.
  81. *
  82. * @since 2.0.0
  83. *
  84. * @param bool[] $capabilities Array of key/value pairs where keys represent a capability name and boolean values
  85. * represent whether the role has that capability.
  86. * @param string $cap Capability name.
  87. * @param string $name Role name.
  88. */
  89. $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
  90. if ( ! empty( $capabilities[ $cap ] ) ) {
  91. return $capabilities[ $cap ];
  92. } else {
  93. return false;
  94. }
  95. }
  96. }