/wp-content/plugins/wordpress-seo/admin/capabilities/class-abstract-capability-manager.php

https://bitbucket.org/carloskikea/helpet · PHP · 84 lines · 26 code · 11 blank · 47 comment · 3 complexity · d4812460c1b33900643ea8b1f2b985ac MD5 · raw file

  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Capabilities
  6. */
  7. /**
  8. * Abstract Capability Manager shared code.
  9. */
  10. abstract class WPSEO_Abstract_Capability_Manager implements WPSEO_Capability_Manager {
  11. /** @var array Registered capabilities */
  12. protected $capabilities = array();
  13. /**
  14. * Registers a capability.
  15. *
  16. * @param string $capability Capability to register.
  17. * @param array $roles Roles to add the capability to.
  18. * @param bool $overwrite Optional. Use add or overwrite as registration method.
  19. */
  20. public function register( $capability, array $roles, $overwrite = false ) {
  21. if ( $overwrite || ! isset( $this->capabilities[ $capability ] ) ) {
  22. $this->capabilities[ $capability ] = $roles;
  23. return;
  24. }
  25. // Combine configurations.
  26. $this->capabilities[ $capability ] = array_merge( $roles, $this->capabilities[ $capability ] );
  27. // Remove doubles.
  28. $this->capabilities[ $capability ] = array_unique( $this->capabilities[ $capability ] );
  29. }
  30. /**
  31. * Returns the list of registered capabilitities.
  32. *
  33. * @return string[] Registered capabilities.
  34. */
  35. public function get_capabilities() {
  36. return array_keys( $this->capabilities );
  37. }
  38. /**
  39. * Returns a list of WP_Role roles.
  40. *
  41. * The string array of role names are converted to actual WP_Role objects.
  42. * These are needed to be able to use the API on them.
  43. *
  44. * @param array $roles Roles to retrieve the objects for.
  45. *
  46. * @return WP_Role[] List of WP_Role objects.
  47. */
  48. protected function get_wp_roles( array $roles ) {
  49. $wp_roles = array_map( 'get_role', $roles );
  50. return array_filter( $wp_roles );
  51. }
  52. /**
  53. * Filter capability roles.
  54. *
  55. * @param string $capability Capability to filter roles for.
  56. * @param array $roles List of roles which can be filtered.
  57. *
  58. * @return array Filtered list of roles for the capability.
  59. */
  60. protected function filter_roles( $capability, array $roles ) {
  61. /**
  62. * Filter: Allow changing roles that a capability is added to.
  63. *
  64. * @api array $roles The default roles to be filtered.
  65. */
  66. $filtered = apply_filters( $capability . '_roles', $roles );
  67. // Make sure we have the expected type.
  68. if ( ! is_array( $filtered ) ) {
  69. return array();
  70. }
  71. return $filtered;
  72. }
  73. }