class-acf-location-user-role.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( ! class_exists( 'ACF_Location_User_Role' ) ) :
  6. class ACF_Location_User_Role extends ACF_Location {
  7. /**
  8. * initialize
  9. *
  10. * Sets up the class functionality.
  11. *
  12. * @date 5/03/2014
  13. * @since 5.0.0
  14. *
  15. * @param void
  16. * @return void
  17. */
  18. function initialize() {
  19. $this->name = 'user_role';
  20. $this->label = __( 'User Role', 'acf' );
  21. $this->category = 'user';
  22. $this->object_type = 'user';
  23. }
  24. /**
  25. * Matches the provided rule against the screen args returning a bool result.
  26. *
  27. * @date 9/4/20
  28. * @since 5.9.0
  29. *
  30. * @param array $rule The location rule.
  31. * @param array $screen The screen args.
  32. * @param array $field_group The field group settings.
  33. * @return bool
  34. */
  35. public function match( $rule, $screen, $field_group ) {
  36. // Check screen args.
  37. if ( isset( $screen['user_role'] ) ) {
  38. $user_role = $screen['user_role'];
  39. } elseif ( isset( $screen['user_id'] ) ) {
  40. $user_id = $screen['user_id'];
  41. $user_role = '';
  42. // Determine $user_role from $user_id.
  43. if ( $user_id === 'new' ) {
  44. $user_role = get_option( 'default_role' );
  45. // Check if user can, and if so, set the value allowing them to match.
  46. } elseif ( user_can( $user_id, $rule['value'] ) ) {
  47. $user_role = $rule['value'];
  48. }
  49. } else {
  50. return false;
  51. }
  52. // Compare rule against $user_role.
  53. return $this->compare_to_rule( $user_role, $rule );
  54. }
  55. /**
  56. * Returns an array of possible values for this rule type.
  57. *
  58. * @date 9/4/20
  59. * @since 5.9.0
  60. *
  61. * @param array $rule A location rule.
  62. * @return array
  63. */
  64. public function get_values( $rule ) {
  65. global $wp_roles;
  66. return array_merge(
  67. array(
  68. 'all' => __( 'All', 'acf' ),
  69. ),
  70. $wp_roles->get_names()
  71. );
  72. }
  73. }
  74. // initialize
  75. acf_register_location_type( 'ACF_Location_User_Role' );
  76. endif; // class_exists check