Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

class-wp-role.php 2.7 KiB

3 anni fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. * @access public
  20. * @var string
  21. */
  22. public $name;
  23. /**
  24. * List of capabilities the role contains.
  25. *
  26. * @since 2.0.0
  27. * @access public
  28. * @var array
  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. * @access public
  39. *
  40. * @param string $role Role name.
  41. * @param array $capabilities List of capabilities.
  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. * @access public
  52. *
  53. * @param string $cap Capability name.
  54. * @param bool $grant Whether role has capability privilege.
  55. */
  56. public function add_cap( $cap, $grant = true ) {
  57. $this->capabilities[$cap] = $grant;
  58. wp_roles()->add_cap( $this->name, $cap, $grant );
  59. }
  60. /**
  61. * Removes a capability from a role.
  62. *
  63. * This is a container for WP_Roles::remove_cap() to remove the
  64. * capability from the role. That is to say, that WP_Roles::remove_cap()
  65. * implements the functionality, but it also makes sense to use this class,
  66. * because you don't need to enter the role name.
  67. *
  68. * @since 2.0.0
  69. * @access public
  70. *
  71. * @param string $cap Capability name.
  72. */
  73. public function remove_cap( $cap ) {
  74. unset( $this->capabilities[$cap] );
  75. wp_roles()->remove_cap( $this->name, $cap );
  76. }
  77. /**
  78. * Determines whether the role has the given capability.
  79. *
  80. * The capabilities is passed through the {@see 'role_has_cap'} filter.
  81. * The first parameter for the hook is the list of capabilities the class
  82. * has assigned. The second parameter is the capability name to look for.
  83. * The third and final parameter for the hook is the role name.
  84. *
  85. * @since 2.0.0
  86. * @access public
  87. *
  88. * @param string $cap Capability name.
  89. * @return bool True if the role has the given capability. False otherwise.
  90. */
  91. public function has_cap( $cap ) {
  92. /**
  93. * Filters which capabilities a role has.
  94. *
  95. * @since 2.0.0
  96. *
  97. * @param array $capabilities Array of role capabilities.
  98. * @param string $cap Capability name.
  99. * @param string $name Role name.
  100. */
  101. $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
  102. if ( !empty( $capabilities[$cap] ) )
  103. return $capabilities[$cap];
  104. else
  105. return false;
  106. }
  107. }