capabilities.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Capabilities class.
  4. *
  5. * @access public
  6. * @since 6.0.0
  7. *
  8. * @package MonsterInsights
  9. * @subpackage Capabilities
  10. * @author Chris Christoff
  11. */
  12. // Exit if accessed directly
  13. if ( ! defined( 'ABSPATH' ) ) {
  14. exit;
  15. }
  16. /**
  17. * Map MonsterInsights Capabilities.
  18. *
  19. * Using meta caps, we're creating virtual capabilities that are
  20. * for backwards compatibility reasons given to users with manage_options, and to
  21. * users who have at least of the roles selected in the options on the permissions
  22. * tab of the MonsterInsights settings.
  23. *
  24. * @access public
  25. * @since 6.0.0
  26. *
  27. * @param array $caps Array of capabilities the user has.
  28. * @param string $cap The current cap being filtered.
  29. * @param int $user_id User to check permissions for.
  30. * @param array $args Extra parameters. Unused.
  31. * @return array Array of caps needed to have this meta cap. If returned array is empty, user has the capability.
  32. */
  33. function monsterinsights_add_capabilities( $caps, $cap, $user_id, $args ) {
  34. switch( $cap ) {
  35. case 'monsterinsights_view_dashboard' :
  36. $roles = monsterinsights_get_option( 'view_reports', array() );
  37. $user_can_via_settings = false;
  38. if ( ! empty( $roles ) && is_array( $roles ) ) {
  39. foreach ( $roles as $role ) {
  40. if ( is_string( $role ) ) {
  41. if ( user_can( $user_id, $role ) ) {
  42. $user_can_via_settings = true;
  43. break;
  44. }
  45. }
  46. }
  47. } else if ( ! empty( $roles ) && is_string( $roles ) ) {
  48. if ( user_can( $user_id, $roles ) ) {
  49. $user_can_via_settings = true;
  50. }
  51. }
  52. if ( user_can( $user_id, 'manage_options' ) || $user_can_via_settings ) {
  53. $caps = array();
  54. }
  55. break;
  56. case 'monsterinsights_save_settings' :
  57. $roles = monsterinsights_get_option( 'save_settings', array() );
  58. $user_can_via_settings = false;
  59. if ( ! empty( $roles ) && is_array( $roles ) ) {
  60. foreach ( $roles as $role ) {
  61. if ( is_string( $role ) ) {
  62. if ( user_can( $user_id, $role ) ) {
  63. $user_can_via_settings = true;
  64. break;
  65. }
  66. }
  67. }
  68. } else if ( ! empty( $roles ) && is_string( $roles ) ) {
  69. if ( user_can( $user_id, $roles ) ) {
  70. $user_can_via_settings = true;
  71. }
  72. }
  73. if ( user_can( $user_id, 'manage_options' ) || $user_can_via_settings ) {
  74. $caps = array();
  75. }
  76. break;
  77. }
  78. return $caps;
  79. }
  80. add_filter( 'map_meta_cap','monsterinsights_add_capabilities', 10, 4 );