class-acf-location-current-user-role.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( ! class_exists( 'ACF_Location_Current_User_Role' ) ) :
  6. class ACF_Location_Current_User_Role extends ACF_Location {
  7. /**
  8. * Initializes props.
  9. *
  10. * @date 5/03/2014
  11. * @since 5.0.0
  12. *
  13. * @param void
  14. * @return void
  15. */
  16. public function initialize() {
  17. $this->name = 'current_user_role';
  18. $this->label = __( 'Current User Role', 'acf' );
  19. $this->category = 'user';
  20. }
  21. /**
  22. * Matches the provided rule against the screen args returning a bool result.
  23. *
  24. * @date 9/4/20
  25. * @since 5.9.0
  26. *
  27. * @param array $rule The location rule.
  28. * @param array $screen The screen args.
  29. * @param array $field_group The field group settings.
  30. * @return bool
  31. */
  32. public function match( $rule, $screen, $field_group ) {
  33. // Get current user.
  34. $user = wp_get_current_user();
  35. if ( ! $user ) {
  36. return false;
  37. }
  38. // Check super_admin value.
  39. if ( $rule['value'] == 'super_admin' ) {
  40. $result = is_super_admin( $user->ID );
  41. // Check role.
  42. } else {
  43. $result = in_array( $rule['value'], $user->roles );
  44. }
  45. // Reverse result for "!=" operator.
  46. if ( $rule['operator'] === '!=' ) {
  47. return ! $result;
  48. }
  49. return $result;
  50. }
  51. /**
  52. * Returns an array of possible values for this rule type.
  53. *
  54. * @date 9/4/20
  55. * @since 5.9.0
  56. *
  57. * @param array $rule A location rule.
  58. * @return array
  59. */
  60. public function get_values( $rule ) {
  61. $choices = wp_roles()->get_names();
  62. // Prepend Super Admin choice.
  63. if ( is_multisite() ) {
  64. return array_merge(
  65. array(
  66. 'super_admin' => __( 'Super Admin', 'acf' ),
  67. ),
  68. $choices
  69. );
  70. }
  71. return $choices;
  72. }
  73. }
  74. // Register.
  75. acf_register_location_type( 'ACF_Location_Current_User_Role' );
  76. endif; // class_exists check