locations.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. // Exit if accessed directly.
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit;
  5. }
  6. // Register store.
  7. acf_register_store( 'location-types' );
  8. /**
  9. * Registers a location type.
  10. *
  11. * @date 8/4/20
  12. * @since 5.9.0
  13. *
  14. * @param string $class_name The location class name.
  15. * @return (ACF_Location|false)
  16. */
  17. function acf_register_location_type( $class_name ) {
  18. $store = acf_get_store( 'location-types' );
  19. // Check class exists.
  20. if ( ! class_exists( $class_name ) ) {
  21. $message = sprintf( __( 'Class "%s" does not exist.', 'acf' ), $class_name );
  22. _doing_it_wrong( __FUNCTION__, $message, '5.9.0' );
  23. return false;
  24. }
  25. // Create instance.
  26. $location_type = new $class_name();
  27. $name = $location_type->name;
  28. // Check location type is unique.
  29. if ( $store->has( $name ) ) {
  30. $message = sprintf( __( 'Location type "%s" is already registered.', 'acf' ), $name );
  31. _doing_it_wrong( __FUNCTION__, $message, '5.9.0' );
  32. return false;
  33. }
  34. // Add to store.
  35. $store->set( $name, $location_type );
  36. /**
  37. * Fires after a location type is registered.
  38. *
  39. * @date 8/4/20
  40. * @since 5.9.0
  41. *
  42. * @param string $name The location type name.
  43. * @param ACF_Location $location_type The location type instance.
  44. */
  45. do_action( 'acf/registered_location_type', $name, $location_type );
  46. // Return location type instance.
  47. return $location_type;
  48. }
  49. /**
  50. * Returns an array of all registered location types.
  51. *
  52. * @date 8/4/20
  53. * @since 5.9.0
  54. *
  55. * @param void
  56. * @return array
  57. */
  58. function acf_get_location_types() {
  59. return acf_get_store( 'location-types' )->get();
  60. }
  61. /**
  62. * Returns a location type for the given name.
  63. *
  64. * @date 18/2/19
  65. * @since 5.7.12
  66. *
  67. * @param string $name The location type name.
  68. * @return (ACF_Location|null)
  69. */
  70. function acf_get_location_type( $name ) {
  71. return acf_get_store( 'location-types' )->get( $name );
  72. }
  73. /**
  74. * Returns a grouped array of all location rule types.
  75. *
  76. * @date 8/4/20
  77. * @since 5.9.0
  78. *
  79. * @param void
  80. * @return array
  81. */
  82. function acf_get_location_rule_types() {
  83. $types = array();
  84. // Default categories.
  85. $categories = array(
  86. 'post' => __( 'Post', 'acf' ),
  87. 'page' => __( 'Page', 'acf' ),
  88. 'user' => __( 'User', 'acf' ),
  89. 'forms' => __( 'Forms', 'acf' ),
  90. );
  91. // Loop over all location types and append to $type.
  92. $location_types = acf_get_location_types();
  93. foreach ( $location_types as $location_type ) {
  94. // Ignore if not public.
  95. if ( ! $location_type->public ) {
  96. continue;
  97. }
  98. // Find category label from category name.
  99. $category = $location_type->category;
  100. if ( isset( $categories[ $category ] ) ) {
  101. $category = $categories[ $category ];
  102. }
  103. // Append
  104. $types[ $category ][ $location_type->name ] = esc_html( $location_type->label );
  105. }
  106. /**
  107. * Filters the location rule types.
  108. *
  109. * @date 8/4/20
  110. * @since 5.9.0
  111. *
  112. * @param array $types The location rule types.
  113. */
  114. return apply_filters( 'acf/location/rule_types', $types );
  115. }
  116. /**
  117. * Returns a validated location rule with all props.
  118. *
  119. * @date 8/4/20
  120. * @since 5.9.0
  121. *
  122. * @param array $rule The location rule.
  123. * @return array
  124. */
  125. function acf_validate_location_rule( $rule = array() ) {
  126. // Apply defaults.
  127. $rule = wp_parse_args(
  128. $rule,
  129. array(
  130. 'id' => '',
  131. 'group' => '',
  132. 'param' => '',
  133. 'operator' => '==',
  134. 'value' => '',
  135. )
  136. );
  137. /**
  138. * Filters the location rule to ensure is valid.
  139. *
  140. * @date 8/4/20
  141. * @since 5.9.0
  142. *
  143. * @param array $rule The location rule.
  144. */
  145. $rule = apply_filters( "acf/location/validate_rule/type={$rule['param']}", $rule );
  146. $rule = apply_filters( 'acf/location/validate_rule', $rule );
  147. return $rule;
  148. }
  149. /**
  150. * Returns an array of operators for a given rule.
  151. *
  152. * @date 30/5/17
  153. * @since 5.6.0
  154. *
  155. * @param array $rule The location rule.
  156. * @return array
  157. */
  158. function acf_get_location_rule_operators( $rule ) {
  159. $operators = ACF_Location::get_operators( $rule );
  160. // Get operators from location type since 5.9.
  161. $location_type = acf_get_location_type( $rule['param'] );
  162. if ( $location_type ) {
  163. $operators = $location_type->get_operators( $rule );
  164. }
  165. /**
  166. * Filters the location rule operators.
  167. *
  168. * @date 30/5/17
  169. * @since 5.6.0
  170. *
  171. * @param array $types The location rule operators.
  172. */
  173. $operators = apply_filters( "acf/location/rule_operators/type={$rule['param']}", $operators, $rule );
  174. $operators = apply_filters( "acf/location/rule_operators/{$rule['param']}", $operators, $rule );
  175. $operators = apply_filters( 'acf/location/rule_operators', $operators, $rule );
  176. return $operators;
  177. }
  178. /**
  179. * Returns an array of values for a given rule.
  180. *
  181. * @date 30/5/17
  182. * @since 5.6.0
  183. *
  184. * @param array $rule The location rule.
  185. * @return array
  186. */
  187. function acf_get_location_rule_values( $rule ) {
  188. $values = array();
  189. // Get values from location type since 5.9.
  190. $location_type = acf_get_location_type( $rule['param'] );
  191. if ( $location_type ) {
  192. $values = $location_type->get_values( $rule );
  193. }
  194. /**
  195. * Filters the location rule values.
  196. *
  197. * @date 30/5/17
  198. * @since 5.6.0
  199. *
  200. * @param array $types The location rule values.
  201. */
  202. $values = apply_filters( "acf/location/rule_values/type={$rule['param']}", $values, $rule );
  203. $values = apply_filters( "acf/location/rule_values/{$rule['param']}", $values, $rule );
  204. $values = apply_filters( 'acf/location/rule_values', $values, $rule );
  205. return $values;
  206. }
  207. /**
  208. * Returns true if the provided rule matches the screen args.
  209. *
  210. * @date 30/5/17
  211. * @since 5.6.0
  212. *
  213. * @param array $rule The location rule.
  214. * @param array $screen The screen args.
  215. * @param array $field The field group array.
  216. * @return bool
  217. */
  218. function acf_match_location_rule( $rule, $screen, $field_group ) {
  219. $result = false;
  220. // Get result from location type since 5.9.
  221. $location_type = acf_get_location_type( $rule['param'] );
  222. if ( $location_type ) {
  223. $result = $location_type->match( $rule, $screen, $field_group );
  224. }
  225. /**
  226. * Filters the result.
  227. *
  228. * @date 30/5/17
  229. * @since 5.6.0
  230. *
  231. * @param bool $result The match result.
  232. * @param array $rule The location rule.
  233. * @param array $screen The screen args.
  234. * @param array $field_group The field group array.
  235. */
  236. $result = apply_filters( "acf/location/match_rule/type={$rule['param']}", $result, $rule, $screen, $field_group );
  237. $result = apply_filters( 'acf/location/match_rule', $result, $rule, $screen, $field_group );
  238. $result = apply_filters( "acf/location/rule_match/{$rule['param']}", $result, $rule, $screen, $field_group );
  239. $result = apply_filters( 'acf/location/rule_match', $result, $rule, $screen, $field_group );
  240. return $result;
  241. }
  242. /**
  243. * Returns ann array of screen args to be used against matching rules.
  244. *
  245. * @date 8/4/20
  246. * @since 5.9.0
  247. *
  248. * @param array $screen The screen args.
  249. * @param array $deprecated The field group array.
  250. * @return array
  251. */
  252. function acf_get_location_screen( $screen = array(), $deprecated = false ) {
  253. // Apply defaults.
  254. $screen = wp_parse_args(
  255. $screen,
  256. array(
  257. 'lang' => acf_get_setting( 'current_language' ),
  258. 'ajax' => false,
  259. )
  260. );
  261. /**
  262. * Filters the result.
  263. *
  264. * @date 30/5/17
  265. * @since 5.6.0
  266. *
  267. * @param array $screen The screen args.
  268. * @param array $deprecated The field group array.
  269. */
  270. return apply_filters( 'acf/location/screen', $screen, $deprecated );
  271. }
  272. /**
  273. * Alias of acf_register_location_type().
  274. *
  275. * @date 31/5/17
  276. * @since 5.6.0
  277. *
  278. * @param string $class_name The location class name.
  279. * @return (ACF_Location|false)
  280. */
  281. function acf_register_location_rule( $class_name ) {
  282. return acf_register_location_type( $class_name );
  283. }
  284. /**
  285. * Alias of acf_get_location_type().
  286. *
  287. * @date 31/5/17
  288. * @since 5.6.0
  289. *
  290. * @param string $class_name The location class name.
  291. * @return (ACF_Location|false)
  292. */
  293. function acf_get_location_rule( $name ) {
  294. return acf_get_location_type( $name );
  295. }
  296. /**
  297. * Alias of acf_validate_location_rule().
  298. *
  299. * @date 30/5/17
  300. * @since 5.6.0
  301. *
  302. * @param array $rule The location rule.
  303. * @return array
  304. */
  305. function acf_get_valid_location_rule( $rule ) {
  306. return acf_validate_location_rule( $rule );
  307. }