class-acf-location-current-user.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( ! class_exists( 'ACF_Location_Current_User' ) ) :
  6. class ACF_Location_Current_User 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';
  18. $this->label = __( 'Current User', '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. switch ( $rule['value'] ) {
  34. case 'logged_in':
  35. $result = is_user_logged_in();
  36. break;
  37. case 'viewing_front':
  38. $result = ! is_admin();
  39. break;
  40. case 'viewing_back':
  41. $result = is_admin();
  42. break;
  43. default:
  44. $result = false;
  45. break;
  46. }
  47. // Reverse result for "!=" operator.
  48. if ( $rule['operator'] === '!=' ) {
  49. return ! $result;
  50. }
  51. return $result;
  52. }
  53. /**
  54. * Returns an array of possible values for this rule type.
  55. *
  56. * @date 9/4/20
  57. * @since 5.9.0
  58. *
  59. * @param array $rule A location rule.
  60. * @return array
  61. */
  62. public function get_values( $rule ) {
  63. return array(
  64. 'logged_in' => __( 'Logged in', 'acf' ),
  65. 'viewing_front' => __( 'Viewing front end', 'acf' ),
  66. 'viewing_back' => __( 'Viewing back end', 'acf' ),
  67. );
  68. }
  69. }
  70. // Register.
  71. acf_register_location_type( 'ACF_Location_Current_User' );
  72. endif; // class_exists check