class-abstract-role-manager.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Roles
  6. */
  7. /**
  8. * Abstract Role Manager template.
  9. */
  10. abstract class WPSEO_Abstract_Role_Manager implements WPSEO_Role_Manager {
  11. /**
  12. * Registered roles.
  13. *
  14. * @var array
  15. */
  16. protected $roles = [];
  17. /**
  18. * Registers a role.
  19. *
  20. * @param string $role Role to register.
  21. * @param string $display_name Display name to use.
  22. * @param null|string $template Optional. Role to base the new role on.
  23. *
  24. * @return void
  25. */
  26. public function register( $role, $display_name, $template = null ) {
  27. $this->roles[ $role ] =
  28. (object) [
  29. 'display_name' => $display_name,
  30. 'template' => $template,
  31. ];
  32. }
  33. /**
  34. * Returns the list of registered roles.
  35. *
  36. * @return string[] List or registered roles.
  37. */
  38. public function get_roles() {
  39. return array_keys( $this->roles );
  40. }
  41. /**
  42. * Adds the registered roles.
  43. *
  44. * @return void
  45. */
  46. public function add() {
  47. foreach ( $this->roles as $role => $data ) {
  48. $capabilities = $this->get_capabilities( $data->template );
  49. $capabilities = $this->filter_existing_capabilties( $role, $capabilities );
  50. $this->add_role( $role, $data->display_name, $capabilities );
  51. }
  52. }
  53. /**
  54. * Removes the registered roles.
  55. *
  56. * @return void
  57. */
  58. public function remove() {
  59. $roles = array_keys( $this->roles );
  60. array_map( [ $this, 'remove_role' ], $roles );
  61. }
  62. /**
  63. * Returns the capabilities for the specified role.
  64. *
  65. * @param string $role Role to fetch capabilities from.
  66. *
  67. * @return array List of capabilities.
  68. */
  69. protected function get_capabilities( $role ) {
  70. if ( ! is_string( $role ) || empty( $role ) ) {
  71. return [];
  72. }
  73. $wp_role = get_role( $role );
  74. if ( ! $wp_role ) {
  75. return [];
  76. }
  77. return $wp_role->capabilities;
  78. }
  79. /**
  80. * Returns true if the capability exists on the role.
  81. *
  82. * @param WP_Role $role Role to check capability against.
  83. * @param string $capability Capability to check.
  84. *
  85. * @return bool True if the capability is defined for the role.
  86. */
  87. protected function capability_exists( WP_Role $role, $capability ) {
  88. return ! array_key_exists( $capability, $role->capabilities );
  89. }
  90. /**
  91. * Filters out capabilities that are already set for the role.
  92. *
  93. * This makes sure we don't override configurations that have been previously set.
  94. *
  95. * @param string $role The role to check against.
  96. * @param array $capabilities The capabilities that should be set.
  97. *
  98. * @return array Capabilties that can be safely set.
  99. */
  100. protected function filter_existing_capabilties( $role, array $capabilities ) {
  101. if ( $capabilities === [] ) {
  102. return $capabilities;
  103. }
  104. $wp_role = get_role( $role );
  105. if ( ! $wp_role ) {
  106. return $capabilities;
  107. }
  108. foreach ( $capabilities as $capability => $grant ) {
  109. if ( $this->capability_exists( $wp_role, $capability ) ) {
  110. unset( $capabilities[ $capability ] );
  111. }
  112. }
  113. return $capabilities;
  114. }
  115. /**
  116. * Adds a role to the system.
  117. *
  118. * @param string $role Role to add.
  119. * @param string $display_name Name to display for the role.
  120. * @param array $capabilities Capabilities to add to the role.
  121. *
  122. * @return void
  123. */
  124. abstract protected function add_role( $role, $display_name, array $capabilities = [] );
  125. /**
  126. * Removes a role from the system.
  127. *
  128. * @param string $role Role to remove.
  129. *
  130. * @return void
  131. */
  132. abstract protected function remove_role( $role );
  133. }