class-register-capabilities.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Capabilities
  6. */
  7. /**
  8. * Capabilities registration class.
  9. */
  10. class WPSEO_Register_Capabilities implements WPSEO_WordPress_Integration {
  11. /**
  12. * Registers the hooks.
  13. *
  14. * @return void
  15. */
  16. public function register_hooks() {
  17. add_action( 'wpseo_register_capabilities', array( $this, 'register' ) );
  18. if ( is_multisite() ) {
  19. add_action( 'user_has_cap', array( $this, 'filter_user_has_wpseo_manage_options_cap' ), 10, 4 );
  20. }
  21. }
  22. /**
  23. * Registers the capabilities.
  24. *
  25. * @return void
  26. */
  27. public function register() {
  28. $manager = WPSEO_Capability_Manager_Factory::get();
  29. $manager->register( 'wpseo_bulk_edit', array( 'editor', 'wpseo_editor', 'wpseo_manager' ) );
  30. $manager->register( 'wpseo_edit_advanced_metadata', array( 'wpseo_editor', 'wpseo_manager' ) );
  31. $manager->register( 'wpseo_manage_options', array( 'administrator', 'wpseo_manager' ) );
  32. }
  33. /**
  34. * Revokes the 'wpseo_manage_options' capability from administrator users if it should only
  35. * only be granted to network administrators.
  36. *
  37. * @param array $allcaps An array of all the user's capabilities.
  38. * @param array $caps Actual capabilities being checked.
  39. * @param array $args Optional parameters passed to has_cap(), typically object ID.
  40. * @param WP_User $user The user object.
  41. *
  42. * @return array Possibly modified array of the user's capabilities.
  43. */
  44. public function filter_user_has_wpseo_manage_options_cap( $allcaps, $caps, $args, $user ) {
  45. // We only need to do something if 'wpseo_manage_options' is being checked.
  46. if ( ! in_array( 'wpseo_manage_options', $caps, true ) ) {
  47. return $allcaps;
  48. }
  49. // If the user does not have 'wpseo_manage_options' anyway, we don't need to revoke access.
  50. if ( empty( $allcaps['wpseo_manage_options'] ) ) {
  51. return $allcaps;
  52. }
  53. // If the user does not have 'delete_users', they are not an administrator.
  54. if ( empty( $allcaps['delete_users'] ) ) {
  55. return $allcaps;
  56. }
  57. $options = WPSEO_Options::get_instance();
  58. if ( $options->get( 'access' ) === 'superadmin' && ! is_super_admin( $user->ID ) ) {
  59. unset( $allcaps['wpseo_manage_options'] );
  60. }
  61. return $allcaps;
  62. }
  63. }