abstract-acf-location.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( ! class_exists( 'ACF_Location' ) ) :
  6. abstract class ACF_Location extends ACF_Legacy_Location {
  7. /**
  8. * The location rule name.
  9. *
  10. * @since 5.9.0
  11. * @var string
  12. */
  13. public $name = '';
  14. /**
  15. * The location rule label.
  16. *
  17. * @since 5.9.0
  18. * @var string
  19. */
  20. public $label = '';
  21. /**
  22. * The location rule category.
  23. *
  24. * Accepts "post", "page", "user", "forms" or a custom label.
  25. *
  26. * @since 5.9.0
  27. * @var string
  28. */
  29. public $category = 'post';
  30. /**
  31. * Whether or not the location rule is publicly accessible.
  32. *
  33. * @since 5.0.0
  34. * @var bool
  35. */
  36. public $public = true;
  37. /**
  38. * The object type related to this location rule.
  39. *
  40. * Accepts an object type discoverable by `acf_get_object_type()`.
  41. *
  42. * @since 5.9.0
  43. * @var string
  44. */
  45. public $object_type = '';
  46. /**
  47. * The object subtype related to this location rule.
  48. *
  49. * Accepts a custom post type or custom taxonomy.
  50. *
  51. * @since 5.9.0
  52. * @var string
  53. */
  54. public $object_subtype = '';
  55. /**
  56. * Constructor.
  57. *
  58. * @date 8/4/20
  59. * @since 5.9.0
  60. *
  61. * @param void
  62. * @return void
  63. */
  64. public function __construct() {
  65. $this->initialize();
  66. // Call legacy constructor.
  67. parent::__construct();
  68. }
  69. /**
  70. * Initializes props.
  71. *
  72. * @date 5/03/2014
  73. * @since 5.0.0
  74. *
  75. * @param void
  76. * @return void
  77. */
  78. public function initialize() {
  79. // Set props here.
  80. }
  81. /**
  82. * Returns an array of operators for this location.
  83. *
  84. * @date 9/4/20
  85. * @since 5.9.0
  86. *
  87. * @param array $rule A location rule.
  88. * @return array
  89. */
  90. public static function get_operators( $rule ) {
  91. return array(
  92. '==' => __( 'is equal to', 'acf' ),
  93. '!=' => __( 'is not equal to', 'acf' ),
  94. );
  95. }
  96. /**
  97. * Returns an array of possible values for this location.
  98. *
  99. * @date 9/4/20
  100. * @since 5.9.0
  101. *
  102. * @param array $rule A location rule.
  103. * @return array
  104. */
  105. public function get_values( $rule ) {
  106. return array();
  107. }
  108. /**
  109. * Returns the object_type connected to this location.
  110. *
  111. * @date 1/4/20
  112. * @since 5.9.0
  113. *
  114. * @param array $rule A location rule.
  115. * @return string
  116. */
  117. public function get_object_type( $rule ) {
  118. return $this->object_type;
  119. }
  120. /**
  121. * Returns the object_subtype connected to this location.
  122. *
  123. * @date 1/4/20
  124. * @since 5.9.0
  125. *
  126. * @param array $rule A location rule.
  127. * @return string|array
  128. */
  129. public function get_object_subtype( $rule ) {
  130. return $this->object_subtype;
  131. }
  132. /**
  133. * Matches the provided rule against the screen args returning a bool result.
  134. *
  135. * @date 9/4/20
  136. * @since 5.9.0
  137. *
  138. * @param array $rule The location rule.
  139. * @param array $screen The screen args.
  140. * @param array $field_group The field group settings.
  141. * @return bool
  142. */
  143. public function match( $rule, $screen, $field_group ) {
  144. return false;
  145. }
  146. /**
  147. * Compares the given value and rule params returning true when they match.
  148. *
  149. * @date 17/9/19
  150. * @since 5.8.1
  151. *
  152. * @param array $rule The location rule data.
  153. * @param mixed $value The value to compare against.
  154. * @return bool
  155. */
  156. public function compare_to_rule( $value, $rule ) {
  157. $result = ( $value == $rule['value'] );
  158. // Allow "all" to match any value.
  159. if ( $rule['value'] === 'all' ) {
  160. $result = true;
  161. }
  162. // Reverse result for "!=" operator.
  163. if ( $rule['operator'] === '!=' ) {
  164. return ! $result;
  165. }
  166. return $result;
  167. }
  168. }
  169. endif; // class_exists check