No Description

class-wp-role.php 2.4KB

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