class-capability-manager-wp.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Capabilities
  6. */
  7. /**
  8. * Default WordPress capability manager implementation.
  9. */
  10. final class WPSEO_Capability_Manager_WP extends WPSEO_Abstract_Capability_Manager {
  11. /**
  12. * Adds the capabilities to the roles.
  13. *
  14. * @return void
  15. */
  16. public function add() {
  17. foreach ( $this->capabilities as $capability => $roles ) {
  18. $filtered_roles = $this->filter_roles( $capability, $roles );
  19. $wp_roles = $this->get_wp_roles( $filtered_roles );
  20. foreach ( $wp_roles as $wp_role ) {
  21. $wp_role->add_cap( $capability );
  22. }
  23. }
  24. }
  25. /**
  26. * Unregisters the capabilities from the system.
  27. *
  28. * @return void
  29. */
  30. public function remove() {
  31. // Remove from any roles it has been added to.
  32. $roles = wp_roles()->get_names();
  33. $roles = array_keys( $roles );
  34. foreach ( $this->capabilities as $capability => $_roles ) {
  35. $registered_roles = array_unique( array_merge( $roles, $this->capabilities[ $capability ] ) );
  36. // Allow filtering of roles.
  37. $filtered_roles = $this->filter_roles( $capability, $registered_roles );
  38. $wp_roles = $this->get_wp_roles( $filtered_roles );
  39. foreach ( $wp_roles as $wp_role ) {
  40. $wp_role->remove_cap( $capability );
  41. }
  42. }
  43. }
  44. }