/wp-content/plugins/wordpress-seo/admin/roles/class-role-manager-wp.php

https://bitbucket.org/carloskikea/helpet · PHP · 62 lines · 20 code · 7 blank · 35 comment · 1 complexity · c2e998c539a915709bad98a6e5c0cbba MD5 · raw file

  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Roles
  6. */
  7. /**
  8. * WordPress' default implementation of the Role Manager.
  9. */
  10. final class WPSEO_Role_Manager_WP extends WPSEO_Abstract_Role_Manager {
  11. /**
  12. * Adds a role to the system.
  13. *
  14. * @param string $role Role to add.
  15. * @param string $display_name Name to display for the role.
  16. * @param array $capabilities Capabilities to add to the role.
  17. *
  18. * @return void
  19. */
  20. protected function add_role( $role, $display_name, array $capabilities = array() ) {
  21. $wp_role = get_role( $role );
  22. if ( $wp_role ) {
  23. foreach ( $capabilities as $capability => $grant ) {
  24. $wp_role->add_cap( $capability, $grant );
  25. }
  26. return;
  27. }
  28. // @codingStandardsIgnoreLine
  29. add_role( $role, $display_name, $capabilities );
  30. }
  31. /**
  32. * Removes a role from the system.
  33. *
  34. * @param string $role Role to remove.
  35. *
  36. * @return void
  37. */
  38. protected function remove_role( $role ) {
  39. remove_role( $role );
  40. }
  41. /**
  42. * Formats the capabilities to the required format.
  43. *
  44. * @param array $capabilities Capabilities to format.
  45. * @param bool $enabled Whether these capabilities should be enabled or not.
  46. *
  47. * @return array Formatted capabilities.
  48. */
  49. protected function format_capabilities( array $capabilities, $enabled = true ) {
  50. // Flip keys and values.
  51. $capabilities = array_flip( $capabilities );
  52. // Set all values to $enabled.
  53. return array_fill_keys( array_keys( $capabilities ), $enabled );
  54. }
  55. }