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