class-abstract-role-manager.php 3.4 KB

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