1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* acf_get_setting |
5
|
|
|
* |
6
|
|
|
* This function will return a value from the settings array found in the acf object |
7
|
|
|
* |
8
|
|
|
* @type function |
9
|
|
|
* @date 28/09/13 |
10
|
|
|
* @since 5.0.0 |
11
|
|
|
* |
12
|
|
|
* @param $name (string) the setting name to return |
13
|
|
|
* @return (mixed) |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
function acf_get_setting( $name, $default = null ) { |
17
|
|
|
|
18
|
|
|
// vars |
19
|
|
|
$settings = acf()->settings; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
// find setting |
23
|
|
|
$setting = acf_maybe_get( $settings, $name, $default ); |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
// filter for 3rd party customization |
27
|
|
|
$setting = apply_filters( "acf/settings/{$name}", $setting ); |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
// return |
31
|
|
|
return $setting; |
32
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/* |
37
|
|
|
* acf_get_compatibility |
38
|
|
|
* |
39
|
|
|
* This function will return true or false for a given compatibility setting |
40
|
|
|
* |
41
|
|
|
* @type function |
42
|
|
|
* @date 20/01/2015 |
43
|
|
|
* @since 5.1.5 |
44
|
|
|
* |
45
|
|
|
* @param $name (string) |
46
|
|
|
* @return (boolean) |
47
|
|
|
*/ |
48
|
|
|
|
49
|
|
|
function acf_get_compatibility( $name ) { |
50
|
|
|
|
51
|
|
|
return apply_filters( "acf/compatibility/{$name}", false ); |
52
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/* |
57
|
|
|
* acf_update_setting |
58
|
|
|
* |
59
|
|
|
* This function will update a value into the settings array found in the acf object |
60
|
|
|
* |
61
|
|
|
* @type function |
62
|
|
|
* @date 28/09/13 |
63
|
|
|
* @since 5.0.0 |
64
|
|
|
* |
65
|
|
|
* @param $name (string) |
66
|
|
|
* @param $value (mixed) |
67
|
|
|
* @return n/a |
68
|
|
|
*/ |
|
|
|
|
69
|
|
|
|
70
|
|
|
function acf_update_setting( $name, $value ) { |
71
|
|
|
|
72
|
|
|
acf()->settings[ $name ] = $value; |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/* |
78
|
|
|
* acf_append_setting |
79
|
|
|
* |
80
|
|
|
* This function will add a value into the settings array found in the acf object |
81
|
|
|
* |
82
|
|
|
* @type function |
83
|
|
|
* @date 28/09/13 |
84
|
|
|
* @since 5.0.0 |
85
|
|
|
* |
86
|
|
|
* @param $name (string) |
87
|
|
|
* @param $value (mixed) |
88
|
|
|
* @return n/a |
89
|
|
|
*/ |
|
|
|
|
90
|
|
|
|
91
|
|
|
function acf_append_setting( $name, $value ) { |
92
|
|
|
|
93
|
|
|
// createa array if needed |
94
|
|
|
if( !isset(acf()->settings[ $name ]) ) { |
95
|
|
|
|
96
|
|
|
acf()->settings[ $name ] = array(); |
97
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
// append to array |
102
|
|
|
acf()->settings[ $name ][] = $value; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/* |
107
|
|
|
* acf_has_done |
108
|
|
|
* |
109
|
|
|
* This function will return true if this action has already been done |
110
|
|
|
* |
111
|
|
|
* @type function |
112
|
|
|
* @date 16/12/2015 |
113
|
|
|
* @since 5.3.2 |
114
|
|
|
* |
115
|
|
|
* @param $name (string) |
116
|
|
|
* @return (boolean) |
117
|
|
|
*/ |
118
|
|
|
|
119
|
|
|
function acf_has_done( $name ) { |
120
|
|
|
|
121
|
|
|
// vars |
122
|
|
|
$setting = 'has_done_' . $name; |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
// return true if already done |
126
|
|
|
if( acf_get_setting($setting) ) return true; |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
// update setting |
130
|
|
|
acf_update_setting($setting, true); |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
// return |
134
|
|
|
return false; |
135
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
/* |
140
|
|
|
* acf_get_path |
141
|
|
|
* |
142
|
|
|
* This function will return the path to a file within the ACF plugin folder |
143
|
|
|
* |
144
|
|
|
* @type function |
145
|
|
|
* @date 28/09/13 |
146
|
|
|
* @since 5.0.0 |
147
|
|
|
* |
148
|
|
|
* @param $path (string) the relative path from the root of the ACF plugin folder |
149
|
|
|
* @return (string) |
150
|
|
|
*/ |
151
|
|
|
|
152
|
|
|
function acf_get_path( $path ) { |
153
|
|
|
|
154
|
|
|
return acf_get_setting('path') . $path; |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
/* |
160
|
|
|
* acf_get_dir |
161
|
|
|
* |
162
|
|
|
* This function will return the url to a file within the ACF plugin folder |
163
|
|
|
* |
164
|
|
|
* @type function |
165
|
|
|
* @date 28/09/13 |
166
|
|
|
* @since 5.0.0 |
167
|
|
|
* |
168
|
|
|
* @param $path (string) the relative path from the root of the ACF plugin folder |
169
|
|
|
* @return (string) |
170
|
|
|
*/ |
171
|
|
|
|
172
|
|
|
function acf_get_dir( $path ) { |
173
|
|
|
|
174
|
|
|
return acf_get_setting('dir') . $path; |
175
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
/* |
180
|
|
|
* acf_include |
181
|
|
|
* |
182
|
|
|
* This function will include a file |
183
|
|
|
* |
184
|
|
|
* @type function |
185
|
|
|
* @date 10/03/2014 |
186
|
|
|
* @since 5.0.0 |
187
|
|
|
* |
188
|
|
|
* @param $post_id (int) |
189
|
|
|
* @return $post_id (int) |
190
|
|
|
*/ |
|
|
|
|
191
|
|
|
|
192
|
|
|
function acf_include( $file ) { |
193
|
|
|
|
194
|
|
|
$path = acf_get_path( $file ); |
195
|
|
|
|
196
|
|
|
if( file_exists($path) ) { |
197
|
|
|
|
198
|
|
|
include_once( $path ); |
199
|
|
|
|
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
/* |
206
|
|
|
* acf_parse_args |
207
|
|
|
* |
208
|
|
|
* This function will merge together 2 arrays and also convert any numeric values to ints |
209
|
|
|
* |
210
|
|
|
* @type function |
211
|
|
|
* @date 18/10/13 |
212
|
|
|
* @since 5.0.0 |
213
|
|
|
* |
214
|
|
|
* @param $args (array) |
215
|
|
|
* @param $defaults (array) |
216
|
|
|
* @return $args (array) |
217
|
|
|
*/ |
|
|
|
|
218
|
|
|
|
219
|
|
|
function acf_parse_args( $args, $defaults = array() ) { |
220
|
|
|
|
221
|
|
|
// $args may not be na array! |
222
|
|
|
if( !is_array($args) ) { |
223
|
|
|
|
224
|
|
|
$args = array(); |
225
|
|
|
|
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
|
229
|
|
|
// parse args |
230
|
|
|
$args = wp_parse_args( $args, $defaults ); |
231
|
|
|
|
232
|
|
|
|
233
|
|
|
// parse types |
234
|
|
|
$args = acf_parse_types( $args ); |
235
|
|
|
|
236
|
|
|
|
237
|
|
|
// return |
238
|
|
|
return $args; |
239
|
|
|
|
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
|
243
|
|
|
/* |
244
|
|
|
* acf_parse_types |
245
|
|
|
* |
246
|
|
|
* This function will convert any numeric values to int and trim strings |
247
|
|
|
* |
248
|
|
|
* @type function |
249
|
|
|
* @date 18/10/13 |
250
|
|
|
* @since 5.0.0 |
251
|
|
|
* |
252
|
|
|
* @param $var (mixed) |
253
|
|
|
* @return $var (mixed) |
254
|
|
|
*/ |
|
|
|
|
255
|
|
|
|
256
|
|
|
function acf_parse_types( $array ) { |
257
|
|
|
|
258
|
|
|
// some keys are restricted |
259
|
|
|
$restricted = array( |
260
|
|
|
'label', |
261
|
|
|
'name', |
262
|
|
|
'value', |
263
|
|
|
'instructions', |
264
|
|
|
'nonce' |
265
|
|
|
); |
266
|
|
|
|
267
|
|
|
|
268
|
|
|
// loop |
269
|
|
|
foreach( array_keys($array) as $k ) { |
270
|
|
|
|
271
|
|
|
// parse type if not restricted |
272
|
|
|
if( !in_array($k, $restricted, true) ) { |
273
|
|
|
|
274
|
|
|
$array[ $k ] = acf_parse_type( $array[ $k ] ); |
275
|
|
|
|
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
// return |
281
|
|
|
return $array; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
|
285
|
|
|
/* |
286
|
|
|
* acf_parse_type |
287
|
|
|
* |
288
|
|
|
* description |
289
|
|
|
* |
290
|
|
|
* @type function |
291
|
|
|
* @date 11/11/2014 |
292
|
|
|
* @since 5.0.9 |
293
|
|
|
* |
294
|
|
|
* @param $post_id (int) |
295
|
|
|
* @return $post_id (int) |
296
|
|
|
*/ |
|
|
|
|
297
|
|
|
|
298
|
|
|
function acf_parse_type( $v ) { |
299
|
|
|
|
300
|
|
|
// test for array |
301
|
|
|
if( is_array($v) ) { |
302
|
|
|
|
303
|
|
|
return acf_parse_types($v); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
|
307
|
|
|
// bail early if not string |
308
|
|
|
if( !is_string($v) ) { |
309
|
|
|
|
310
|
|
|
return $v; |
311
|
|
|
|
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
|
315
|
|
|
// trim |
316
|
|
|
$v = trim($v); |
317
|
|
|
|
318
|
|
|
|
319
|
|
|
// numbers |
320
|
|
|
if( is_numeric($v) && strval((int)$v) === $v ) { |
321
|
|
|
|
322
|
|
|
$v = intval( $v ); |
323
|
|
|
|
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
|
327
|
|
|
// return |
328
|
|
|
return $v; |
329
|
|
|
|
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
|
333
|
|
|
/* |
334
|
|
|
* acf_get_view |
335
|
|
|
* |
336
|
|
|
* This function will load in a file from the 'admin/views' folder and allow variables to be passed through |
337
|
|
|
* |
338
|
|
|
* @type function |
339
|
|
|
* @date 28/09/13 |
340
|
|
|
* @since 5.0.0 |
341
|
|
|
* |
342
|
|
|
* @param $view_name (string) |
343
|
|
|
* @param $args (array) |
344
|
|
|
* @return n/a |
345
|
|
|
*/ |
|
|
|
|
346
|
|
|
|
347
|
|
|
function acf_get_view( $view_name = '', $args = array() ) { |
348
|
|
|
|
349
|
|
|
// vars |
350
|
|
|
$path = acf_get_path("admin/views/{$view_name}.php"); |
351
|
|
|
|
352
|
|
|
if( file_exists($path) ) { |
353
|
|
|
|
354
|
|
|
include( $path ); |
355
|
|
|
|
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
|
361
|
|
|
/* |
362
|
|
|
* acf_merge_atts |
363
|
|
|
* |
364
|
|
|
* description |
365
|
|
|
* |
366
|
|
|
* @type function |
367
|
|
|
* @date 2/11/2014 |
368
|
|
|
* @since 5.0.9 |
369
|
|
|
* |
370
|
|
|
* @param $post_id (int) |
371
|
|
|
* @return $post_id (int) |
372
|
|
|
*/ |
|
|
|
|
373
|
|
|
|
374
|
|
|
function acf_merge_atts( $atts, $extra = array() ) { |
375
|
|
|
|
376
|
|
|
// bail ealry if no $extra |
377
|
|
|
if( empty($extra) ) { |
378
|
|
|
|
379
|
|
|
return $atts; |
380
|
|
|
|
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
|
384
|
|
|
// merge in new atts |
385
|
|
|
foreach( $extra as $k => $v ) { |
386
|
|
|
|
387
|
|
|
if( $k == 'class' || $k == 'style' ) { |
388
|
|
|
|
389
|
|
|
if( $v === '' ) { |
390
|
|
|
|
391
|
|
|
continue; |
392
|
|
|
|
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
$v = $atts[ $k ] . ' ' . $v; |
396
|
|
|
|
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
$atts[ $k ] = $v; |
400
|
|
|
|
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
|
404
|
|
|
// return |
405
|
|
|
return $atts; |
406
|
|
|
|
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
|
410
|
|
|
/* |
411
|
|
|
* acf_esc_attr |
412
|
|
|
* |
413
|
|
|
* This function will return a render of an array of attributes to be used in markup |
414
|
|
|
* |
415
|
|
|
* @type function |
416
|
|
|
* @date 1/10/13 |
417
|
|
|
* @since 5.0.0 |
418
|
|
|
* |
419
|
|
|
* @param $atts (array) |
420
|
|
|
* @return n/a |
421
|
|
|
*/ |
|
|
|
|
422
|
|
|
|
423
|
|
|
function acf_esc_attr( $atts ) { |
424
|
|
|
|
425
|
|
|
// is string? |
426
|
|
|
if( is_string($atts) ) { |
427
|
|
|
|
428
|
|
|
$atts = trim( $atts ); |
429
|
|
|
return esc_attr( $atts ); |
430
|
|
|
|
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
|
434
|
|
|
// validate |
435
|
|
|
if( empty($atts) ) { |
436
|
|
|
|
437
|
|
|
return ''; |
438
|
|
|
|
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
|
442
|
|
|
// vars |
443
|
|
|
$e = array(); |
444
|
|
|
|
445
|
|
|
|
446
|
|
|
// loop through and render |
447
|
|
|
foreach( $atts as $k => $v ) { |
448
|
|
|
|
449
|
|
|
// object |
450
|
|
|
if( is_array($v) || is_object($v) ) { |
451
|
|
|
|
452
|
|
|
$v = json_encode($v); |
453
|
|
|
|
454
|
|
|
// boolean |
455
|
|
|
} elseif( is_bool($v) ) { |
456
|
|
|
|
457
|
|
|
$v = $v ? 1 : 0; |
458
|
|
|
|
459
|
|
|
// string |
460
|
|
|
} elseif( is_string($v) ) { |
461
|
|
|
|
462
|
|
|
$v = trim($v); |
463
|
|
|
|
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
|
467
|
|
|
// append |
468
|
|
|
$e[] = $k . '="' . esc_attr( $v ) . '"'; |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
|
472
|
|
|
// echo |
473
|
|
|
return implode(' ', $e); |
474
|
|
|
|
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
function acf_esc_attr_e( $atts ) { |
478
|
|
|
|
479
|
|
|
echo acf_esc_attr( $atts ); |
480
|
|
|
|
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
|
484
|
|
|
/* |
485
|
|
|
* acf_hidden_input |
486
|
|
|
* |
487
|
|
|
* description |
488
|
|
|
* |
489
|
|
|
* @type function |
490
|
|
|
* @date 3/02/2014 |
491
|
|
|
* @since 5.0.0 |
492
|
|
|
* |
493
|
|
|
* @param $post_id (int) |
494
|
|
|
* @return $post_id (int) |
495
|
|
|
*/ |
|
|
|
|
496
|
|
|
|
497
|
|
|
function acf_get_hidden_input( $atts ) { |
498
|
|
|
|
499
|
|
|
$atts['type'] = 'hidden'; |
500
|
|
|
|
501
|
|
|
return '<input ' . acf_esc_attr( $atts ) . ' />'; |
502
|
|
|
|
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
function acf_hidden_input( $atts ) { |
506
|
|
|
|
507
|
|
|
echo acf_get_hidden_input( $atts ); |
508
|
|
|
|
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
|
512
|
|
|
/* |
513
|
|
|
* acf_extract_var |
514
|
|
|
* |
515
|
|
|
* This function will remove the var from the array, and return the var |
516
|
|
|
* |
517
|
|
|
* @type function |
518
|
|
|
* @date 2/10/13 |
519
|
|
|
* @since 5.0.0 |
520
|
|
|
* |
521
|
|
|
* @param $array (array) |
522
|
|
|
* @param $key (string) |
523
|
|
|
* @return (mixed) |
524
|
|
|
*/ |
525
|
|
|
|
526
|
|
|
function acf_extract_var( &$array, $key, $default = null ) { |
527
|
|
|
|
528
|
|
|
// check if exists |
529
|
|
|
if( is_array($array) && array_key_exists($key, $array) ) { |
530
|
|
|
|
531
|
|
|
// store value |
532
|
|
|
$v = $array[ $key ]; |
533
|
|
|
|
534
|
|
|
|
535
|
|
|
// unset |
536
|
|
|
unset( $array[ $key ] ); |
537
|
|
|
|
538
|
|
|
|
539
|
|
|
// return |
540
|
|
|
return $v; |
541
|
|
|
|
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
|
545
|
|
|
// return |
546
|
|
|
return $default; |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
|
550
|
|
|
/* |
551
|
|
|
* acf_extract_vars |
552
|
|
|
* |
553
|
|
|
* This function will remove the vars from the array, and return the vars |
554
|
|
|
* |
555
|
|
|
* @type function |
556
|
|
|
* @date 8/10/13 |
557
|
|
|
* @since 5.0.0 |
558
|
|
|
* |
559
|
|
|
* @param $post_id (int) |
560
|
|
|
* @return $post_id (int) |
561
|
|
|
*/ |
|
|
|
|
562
|
|
|
|
563
|
|
|
function acf_extract_vars( &$array, $keys ) { |
564
|
|
|
|
565
|
|
|
$r = array(); |
566
|
|
|
|
567
|
|
|
foreach( $keys as $key ) { |
568
|
|
|
|
569
|
|
|
$r[ $key ] = acf_extract_var( $array, $key ); |
570
|
|
|
|
571
|
|
|
} |
572
|
|
|
|
573
|
|
|
return $r; |
574
|
|
|
} |
575
|
|
|
|
576
|
|
|
|
577
|
|
|
/* |
578
|
|
|
* acf_get_post_types |
579
|
|
|
* |
580
|
|
|
* This function will return an array of available post types |
581
|
|
|
* |
582
|
|
|
* @type function |
583
|
|
|
* @date 7/10/13 |
584
|
|
|
* @since 5.0.0 |
585
|
|
|
* |
586
|
|
|
* @param $exclude (array) |
587
|
|
|
* @param $include (array) |
588
|
|
|
* @return (array) |
589
|
|
|
*/ |
590
|
|
|
|
591
|
|
|
function acf_get_post_types( $exclude = array(), $include = array() ) { |
592
|
|
|
|
593
|
|
|
// get all custom post types |
594
|
|
|
$post_types = get_post_types(); |
595
|
|
|
|
596
|
|
|
|
597
|
|
|
// core exclude |
598
|
|
|
$exclude = wp_parse_args( $exclude, array('acf-field', 'acf-field-group', 'revision', 'nav_menu_item') ); |
599
|
|
|
|
600
|
|
|
|
601
|
|
|
// include |
602
|
|
|
if( !empty($include) ) { |
603
|
|
|
|
604
|
|
|
foreach( array_keys($include) as $i ) { |
605
|
|
|
|
606
|
|
|
$post_type = $include[ $i ]; |
607
|
|
|
|
608
|
|
|
if( post_type_exists($post_type) ) { |
609
|
|
|
|
610
|
|
|
$post_types[ $post_type ] = $post_type; |
611
|
|
|
|
612
|
|
|
} |
613
|
|
|
|
614
|
|
|
} |
615
|
|
|
|
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
|
619
|
|
|
// exclude |
620
|
|
|
foreach( array_values($exclude) as $i ) { |
621
|
|
|
|
622
|
|
|
unset( $post_types[ $i ] ); |
623
|
|
|
|
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
|
627
|
|
|
// simplify keys |
628
|
|
|
$post_types = array_values($post_types); |
629
|
|
|
|
630
|
|
|
|
631
|
|
|
// return |
632
|
|
|
return $post_types; |
633
|
|
|
|
634
|
|
|
} |
635
|
|
|
|
636
|
|
|
|
637
|
|
View Code Duplication |
function acf_get_pretty_post_types( $post_types = array() ) { |
|
|
|
|
638
|
|
|
|
639
|
|
|
// get post types |
640
|
|
|
if( empty($post_types) ) { |
641
|
|
|
|
642
|
|
|
// get all custom post types |
643
|
|
|
$post_types = acf_get_post_types(); |
644
|
|
|
|
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
|
648
|
|
|
// get labels |
649
|
|
|
$ref = array(); |
650
|
|
|
$r = array(); |
651
|
|
|
|
652
|
|
|
foreach( $post_types as $post_type ) { |
653
|
|
|
|
654
|
|
|
// vars |
655
|
|
|
$label = $post_type; |
656
|
|
|
|
657
|
|
|
|
658
|
|
|
// check that object exists (case exists when importing field group from another install and post type does not exist) |
659
|
|
|
if( post_type_exists($post_type) ) { |
660
|
|
|
|
661
|
|
|
$obj = get_post_type_object($post_type); |
662
|
|
|
$label = $obj->labels->singular_name; |
663
|
|
|
|
664
|
|
|
} |
665
|
|
|
|
666
|
|
|
|
667
|
|
|
// append to r |
668
|
|
|
$r[ $post_type ] = $label; |
669
|
|
|
|
670
|
|
|
|
671
|
|
|
// increase counter |
672
|
|
|
if( !isset($ref[ $label ]) ) { |
673
|
|
|
|
674
|
|
|
$ref[ $label ] = 0; |
675
|
|
|
|
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
$ref[ $label ]++; |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
|
682
|
|
|
// get slugs |
683
|
|
|
foreach( array_keys($r) as $i ) { |
684
|
|
|
|
685
|
|
|
// vars |
686
|
|
|
$post_type = $r[ $i ]; |
687
|
|
|
|
688
|
|
|
if( $ref[ $post_type ] > 1 ) { |
689
|
|
|
|
690
|
|
|
$r[ $i ] .= ' (' . $i . ')'; |
691
|
|
|
|
692
|
|
|
} |
693
|
|
|
|
694
|
|
|
} |
695
|
|
|
|
696
|
|
|
|
697
|
|
|
// return |
698
|
|
|
return $r; |
699
|
|
|
|
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
|
703
|
|
|
/* |
704
|
|
|
* acf_verify_nonce |
705
|
|
|
* |
706
|
|
|
* This function will look at the $_POST['_acfnonce'] value and return true or false |
707
|
|
|
* |
708
|
|
|
* @type function |
709
|
|
|
* @date 15/10/13 |
710
|
|
|
* @since 5.0.0 |
711
|
|
|
* |
712
|
|
|
* @param $nonce (string) |
713
|
|
|
* @return (boolean) |
714
|
|
|
*/ |
715
|
|
|
|
716
|
|
|
function acf_verify_nonce( $value, $post_id = 0 ) { |
717
|
|
|
|
718
|
|
|
// vars |
719
|
|
|
$nonce = acf_maybe_get( $_POST, '_acfnonce' ); |
720
|
|
|
|
721
|
|
|
|
722
|
|
|
// bail early if no nonce or if nonce does not match (post|user|comment|term) |
723
|
|
|
if( !$nonce || !wp_verify_nonce($nonce, $value) ) { |
724
|
|
|
|
725
|
|
|
return false; |
726
|
|
|
|
727
|
|
|
} |
728
|
|
|
|
729
|
|
|
|
730
|
|
|
// if saving specific post |
731
|
|
|
if( $post_id ) { |
732
|
|
|
|
733
|
|
|
// vars |
734
|
|
|
$form_post_id = (int) acf_maybe_get( $_POST, 'post_ID' ); |
735
|
|
|
$post_parent = wp_is_post_revision( $post_id ); |
736
|
|
|
|
737
|
|
|
|
738
|
|
|
// 1. no $_POST['post_id'] (shopp plugin) |
739
|
|
|
if( !$form_post_id ) { |
740
|
|
|
|
741
|
|
|
// do nothing (don't remove this if statement!) |
742
|
|
|
|
743
|
|
|
// 2. direct match (this is the post we were editing) |
744
|
|
|
} elseif( $post_id === $form_post_id ) { |
745
|
|
|
|
746
|
|
|
// do nothing (don't remove this if statement!) |
747
|
|
|
|
748
|
|
|
// 3. revision (this post is a revision of the post we were editing) |
749
|
|
|
} elseif( $post_parent === $form_post_id ) { |
750
|
|
|
|
751
|
|
|
// return true early and prevent $_POST['_acfnonce'] from being reset |
752
|
|
|
// this will allow another save_post to save the real post |
753
|
|
|
return true; |
754
|
|
|
|
755
|
|
|
// 4. no match (this post is a custom created one during the save proccess via either WP or 3rd party) |
756
|
|
|
} else { |
757
|
|
|
|
758
|
|
|
// return false early and prevent $_POST['_acfnonce'] from being reset |
759
|
|
|
// this will allow another save_post to save the real post |
760
|
|
|
return false; |
761
|
|
|
|
762
|
|
|
} |
763
|
|
|
|
764
|
|
|
} |
765
|
|
|
|
766
|
|
|
|
767
|
|
|
// reset nonce (only allow 1 save) |
768
|
|
|
$_POST['_acfnonce'] = false; |
769
|
|
|
|
770
|
|
|
|
771
|
|
|
// return |
772
|
|
|
return true; |
773
|
|
|
|
774
|
|
|
} |
775
|
|
|
|
776
|
|
|
|
777
|
|
|
/* |
778
|
|
|
* acf_verify_ajax |
779
|
|
|
* |
780
|
|
|
* This function will return true if the current AJAX request is valid |
781
|
|
|
* It's action will also allow WPML to set the lang and avoid AJAX get_posts issues |
782
|
|
|
* |
783
|
|
|
* @type function |
784
|
|
|
* @date 7/08/2015 |
785
|
|
|
* @since 5.2.3 |
786
|
|
|
* |
787
|
|
|
* @param n/a |
788
|
|
|
* @return (boolean) |
789
|
|
|
*/ |
790
|
|
|
|
791
|
|
|
function acf_verify_ajax() { |
792
|
|
|
|
793
|
|
|
// bail early if not acf action |
794
|
|
|
if( empty($_POST['action']) || substr($_POST['action'], 0, 3) !== 'acf' ) { |
795
|
|
|
|
796
|
|
|
return false; |
797
|
|
|
|
798
|
|
|
} |
799
|
|
|
|
800
|
|
|
|
801
|
|
|
// bail early if not acf nonce |
802
|
|
|
if( empty($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'acf_nonce') ) { |
803
|
|
|
|
804
|
|
|
return false; |
805
|
|
|
|
806
|
|
|
} |
807
|
|
|
|
808
|
|
|
|
809
|
|
|
// action for 3rd party customization |
810
|
|
|
do_action('acf/verify_ajax'); |
811
|
|
|
|
812
|
|
|
|
813
|
|
|
// return |
814
|
|
|
return true; |
815
|
|
|
|
816
|
|
|
} |
817
|
|
|
|
818
|
|
|
|
819
|
|
|
/* |
820
|
|
|
* acf_add_admin_notice |
821
|
|
|
* |
822
|
|
|
* This function will add the notice data to a setting in the acf object for the admin_notices action to use |
823
|
|
|
* |
824
|
|
|
* @type function |
825
|
|
|
* @date 17/10/13 |
826
|
|
|
* @since 5.0.0 |
827
|
|
|
* |
828
|
|
|
* @param $text (string) |
829
|
|
|
* @param $class (string) |
830
|
|
|
* @return (int) message ID (array position) |
831
|
|
|
*/ |
832
|
|
|
|
833
|
|
|
function acf_add_admin_notice( $text, $class = '', $wrap = 'p' ) |
834
|
|
|
{ |
835
|
|
|
// vars |
836
|
|
|
$admin_notices = acf_get_admin_notices(); |
837
|
|
|
|
838
|
|
|
|
839
|
|
|
// add to array |
840
|
|
|
$admin_notices[] = array( |
841
|
|
|
'text' => $text, |
842
|
|
|
'class' => "updated {$class}", |
843
|
|
|
'wrap' => $wrap |
844
|
|
|
); |
845
|
|
|
|
846
|
|
|
|
847
|
|
|
// update |
848
|
|
|
acf_update_setting( 'admin_notices', $admin_notices ); |
849
|
|
|
|
850
|
|
|
|
851
|
|
|
// return |
852
|
|
|
return ( count( $admin_notices ) - 1 ); |
853
|
|
|
|
854
|
|
|
} |
855
|
|
|
|
856
|
|
|
|
857
|
|
|
/* |
858
|
|
|
* acf_get_admin_notices |
859
|
|
|
* |
860
|
|
|
* This function will return an array containing any admin notices |
861
|
|
|
* |
862
|
|
|
* @type function |
863
|
|
|
* @date 17/10/13 |
864
|
|
|
* @since 5.0.0 |
865
|
|
|
* |
866
|
|
|
* @param n/a |
867
|
|
|
* @return (array) |
868
|
|
|
*/ |
869
|
|
|
|
870
|
|
|
function acf_get_admin_notices() |
871
|
|
|
{ |
872
|
|
|
// vars |
873
|
|
|
$admin_notices = acf_get_setting( 'admin_notices' ); |
874
|
|
|
|
875
|
|
|
|
876
|
|
|
// validate |
877
|
|
|
if( !$admin_notices ) |
878
|
|
|
{ |
879
|
|
|
$admin_notices = array(); |
880
|
|
|
} |
881
|
|
|
|
882
|
|
|
|
883
|
|
|
// return |
884
|
|
|
return $admin_notices; |
885
|
|
|
} |
886
|
|
|
|
887
|
|
|
|
888
|
|
|
/* |
889
|
|
|
* acf_get_image_sizes |
890
|
|
|
* |
891
|
|
|
* This function will return an array of available image sizes |
892
|
|
|
* |
893
|
|
|
* @type function |
894
|
|
|
* @date 23/10/13 |
895
|
|
|
* @since 5.0.0 |
896
|
|
|
* |
897
|
|
|
* @param n/a |
898
|
|
|
* @return (array) |
899
|
|
|
*/ |
900
|
|
|
|
901
|
|
|
function acf_get_image_sizes() { |
902
|
|
|
|
903
|
|
|
// global |
904
|
|
|
global $_wp_additional_image_sizes; |
905
|
|
|
|
906
|
|
|
|
907
|
|
|
// vars |
908
|
|
|
$sizes = array( |
909
|
|
|
'thumbnail' => __("Thumbnail",'acf'), |
910
|
|
|
'medium' => __("Medium",'acf'), |
911
|
|
|
'large' => __("Large",'acf') |
912
|
|
|
); |
913
|
|
|
|
914
|
|
|
|
915
|
|
|
// find all sizes |
916
|
|
|
$all_sizes = get_intermediate_image_sizes(); |
917
|
|
|
|
918
|
|
|
|
919
|
|
|
// add extra registered sizes |
920
|
|
View Code Duplication |
if( !empty($all_sizes) ) { |
|
|
|
|
921
|
|
|
|
922
|
|
|
foreach( $all_sizes as $size ) { |
923
|
|
|
|
924
|
|
|
// bail early if already in array |
925
|
|
|
if( isset($sizes[ $size ]) ) { |
926
|
|
|
|
927
|
|
|
continue; |
928
|
|
|
|
929
|
|
|
} |
930
|
|
|
|
931
|
|
|
|
932
|
|
|
// append to array |
933
|
|
|
$label = str_replace('-', ' ', $size); |
934
|
|
|
$label = ucwords( $label ); |
935
|
|
|
$sizes[ $size ] = $label; |
936
|
|
|
|
937
|
|
|
} |
938
|
|
|
|
939
|
|
|
} |
940
|
|
|
|
941
|
|
|
|
942
|
|
|
// add sizes |
943
|
|
|
foreach( array_keys($sizes) as $s ) { |
944
|
|
|
|
945
|
|
|
// vars |
946
|
|
|
$w = isset($_wp_additional_image_sizes[$s]['width']) ? $_wp_additional_image_sizes[$s]['width'] : get_option( "{$s}_size_w" ); |
947
|
|
|
$h = isset($_wp_additional_image_sizes[$s]['height']) ? $_wp_additional_image_sizes[$s]['height'] : get_option( "{$s}_size_h" ); |
948
|
|
|
|
949
|
|
|
if( $w && $h ) { |
950
|
|
|
|
951
|
|
|
$sizes[ $s ] .= " ({$w} x {$h})"; |
952
|
|
|
|
953
|
|
|
} |
954
|
|
|
|
955
|
|
|
} |
956
|
|
|
|
957
|
|
|
|
958
|
|
|
// add full end |
959
|
|
|
$sizes['full'] = __("Full Size",'acf'); |
960
|
|
|
|
961
|
|
|
|
962
|
|
|
// filter for 3rd party customization |
963
|
|
|
$sizes = apply_filters( 'acf/get_image_sizes', $sizes ); |
964
|
|
|
|
965
|
|
|
|
966
|
|
|
// return |
967
|
|
|
return $sizes; |
968
|
|
|
|
969
|
|
|
} |
970
|
|
|
|
971
|
|
|
|
972
|
|
|
/* |
973
|
|
|
* acf_get_taxonomies |
974
|
|
|
* |
975
|
|
|
* This function will return an array of available taxonomies |
976
|
|
|
* |
977
|
|
|
* @type function |
978
|
|
|
* @date 7/10/13 |
979
|
|
|
* @since 5.0.0 |
980
|
|
|
* |
981
|
|
|
* @param n/a |
982
|
|
|
* @return (array) |
983
|
|
|
*/ |
984
|
|
|
|
985
|
|
|
function acf_get_taxonomies() { |
986
|
|
|
|
987
|
|
|
// get all taxonomies |
988
|
|
|
$taxonomies = get_taxonomies( false, 'objects' ); |
989
|
|
|
$ignore = array( 'nav_menu', 'link_category' ); |
990
|
|
|
$r = array(); |
991
|
|
|
|
992
|
|
|
|
993
|
|
|
// populate $r |
994
|
|
|
foreach( $taxonomies as $taxonomy ) |
995
|
|
|
{ |
996
|
|
|
if( in_array($taxonomy->name, $ignore) ) |
997
|
|
|
{ |
998
|
|
|
continue; |
999
|
|
|
|
1000
|
|
|
} |
1001
|
|
|
|
1002
|
|
|
$r[ $taxonomy->name ] = $taxonomy->name; //"{$taxonomy->labels->singular_name}"; // ({$taxonomy->name}) |
1003
|
|
|
} |
1004
|
|
|
|
1005
|
|
|
|
1006
|
|
|
// return |
1007
|
|
|
return $r; |
1008
|
|
|
|
1009
|
|
|
} |
1010
|
|
|
|
1011
|
|
|
|
1012
|
|
View Code Duplication |
function acf_get_pretty_taxonomies( $taxonomies = array() ) { |
|
|
|
|
1013
|
|
|
|
1014
|
|
|
// get post types |
1015
|
|
|
if( empty($taxonomies) ) { |
1016
|
|
|
|
1017
|
|
|
// get all custom post types |
1018
|
|
|
$taxonomies = acf_get_taxonomies(); |
1019
|
|
|
|
1020
|
|
|
} |
1021
|
|
|
|
1022
|
|
|
|
1023
|
|
|
// get labels |
1024
|
|
|
$ref = array(); |
1025
|
|
|
$r = array(); |
1026
|
|
|
|
1027
|
|
|
foreach( array_keys($taxonomies) as $i ) { |
1028
|
|
|
|
1029
|
|
|
// vars |
1030
|
|
|
$taxonomy = acf_extract_var( $taxonomies, $i); |
1031
|
|
|
$obj = get_taxonomy( $taxonomy ); |
1032
|
|
|
$name = $obj->labels->singular_name; |
1033
|
|
|
|
1034
|
|
|
|
1035
|
|
|
// append to r |
1036
|
|
|
$r[ $taxonomy ] = $name; |
1037
|
|
|
|
1038
|
|
|
|
1039
|
|
|
// increase counter |
1040
|
|
|
if( !isset($ref[ $name ]) ) { |
1041
|
|
|
|
1042
|
|
|
$ref[ $name ] = 0; |
1043
|
|
|
|
1044
|
|
|
} |
1045
|
|
|
|
1046
|
|
|
$ref[ $name ]++; |
1047
|
|
|
} |
1048
|
|
|
|
1049
|
|
|
|
1050
|
|
|
// get slugs |
1051
|
|
|
foreach( array_keys($r) as $i ) { |
1052
|
|
|
|
1053
|
|
|
// vars |
1054
|
|
|
$taxonomy = $r[ $i ]; |
1055
|
|
|
|
1056
|
|
|
if( $ref[ $taxonomy ] > 1 ) { |
1057
|
|
|
|
1058
|
|
|
$r[ $i ] .= ' (' . $i . ')'; |
1059
|
|
|
|
1060
|
|
|
} |
1061
|
|
|
|
1062
|
|
|
} |
1063
|
|
|
|
1064
|
|
|
|
1065
|
|
|
// return |
1066
|
|
|
return $r; |
1067
|
|
|
|
1068
|
|
|
} |
1069
|
|
|
|
1070
|
|
|
|
1071
|
|
|
/* |
1072
|
|
|
* acf_get_taxonomy_terms |
1073
|
|
|
* |
1074
|
|
|
* This function will return an array of available taxonomy terms |
1075
|
|
|
* |
1076
|
|
|
* @type function |
1077
|
|
|
* @date 7/10/13 |
1078
|
|
|
* @since 5.0.0 |
1079
|
|
|
* |
1080
|
|
|
* @param $taxonomies (array) |
1081
|
|
|
* @return (array) |
1082
|
|
|
*/ |
1083
|
|
|
|
1084
|
|
|
function acf_get_taxonomy_terms( $taxonomies = array() ) { |
1085
|
|
|
|
1086
|
|
|
// force array |
1087
|
|
|
$taxonomies = acf_get_array( $taxonomies ); |
|
|
|
|
1088
|
|
|
|
1089
|
|
|
|
1090
|
|
|
// get pretty taxonomy names |
1091
|
|
|
$taxonomies = acf_get_pretty_taxonomies( $taxonomies ); |
1092
|
|
|
|
1093
|
|
|
|
1094
|
|
|
// vars |
1095
|
|
|
$r = array(); |
1096
|
|
|
|
1097
|
|
|
|
1098
|
|
|
// populate $r |
1099
|
|
|
foreach( array_keys($taxonomies) as $taxonomy ) { |
1100
|
|
|
|
1101
|
|
|
// vars |
1102
|
|
|
$label = $taxonomies[ $taxonomy ]; |
1103
|
|
|
$terms = get_terms( $taxonomy, array( 'hide_empty' => false ) ); |
1104
|
|
|
$is_hierarchical = is_taxonomy_hierarchical( $taxonomy ); |
1105
|
|
|
|
1106
|
|
|
|
1107
|
|
|
// bail early i no terms |
1108
|
|
|
if( empty($terms) ) continue; |
1109
|
|
|
|
1110
|
|
|
|
1111
|
|
|
// sort into hierachial order! |
1112
|
|
|
if( $is_hierarchical ) { |
1113
|
|
|
|
1114
|
|
|
$terms = _get_term_children( 0, $terms, $taxonomy ); |
1115
|
|
|
|
1116
|
|
|
} |
1117
|
|
|
|
1118
|
|
|
|
1119
|
|
|
// add placeholder |
1120
|
|
|
$r[ $label ] = array(); |
1121
|
|
|
|
1122
|
|
|
|
1123
|
|
|
// add choices |
1124
|
|
|
foreach( $terms as $term ) { |
1125
|
|
|
|
1126
|
|
|
$k = "{$taxonomy}:{$term->slug}"; |
1127
|
|
|
$r[ $label ][ $k ] = acf_get_term_title( $term ); |
1128
|
|
|
|
1129
|
|
|
} |
1130
|
|
|
|
1131
|
|
|
} |
1132
|
|
|
|
1133
|
|
|
|
1134
|
|
|
// return |
1135
|
|
|
return $r; |
1136
|
|
|
|
1137
|
|
|
} |
1138
|
|
|
|
1139
|
|
|
|
1140
|
|
|
function acf_get_term_title( $term ) { |
1141
|
|
|
|
1142
|
|
|
// title |
1143
|
|
|
$title = $term->name; |
1144
|
|
|
|
1145
|
|
|
|
1146
|
|
|
// empty |
1147
|
|
|
if( $title === '' ) { |
1148
|
|
|
|
1149
|
|
|
$title = __('(no title)', 'acf'); |
1150
|
|
|
|
1151
|
|
|
} |
1152
|
|
|
|
1153
|
|
|
|
1154
|
|
|
// ancestors |
1155
|
|
|
if( is_taxonomy_hierarchical($term->taxonomy) ) { |
1156
|
|
|
|
1157
|
|
|
$ancestors = get_ancestors( $term->term_id, $term->taxonomy ); |
1158
|
|
|
|
1159
|
|
|
$title = str_repeat('- ', count($ancestors)) . $title; |
1160
|
|
|
|
1161
|
|
|
} |
1162
|
|
|
|
1163
|
|
|
|
1164
|
|
|
// return |
1165
|
|
|
return $title; |
1166
|
|
|
|
1167
|
|
|
} |
1168
|
|
|
|
1169
|
|
|
|
1170
|
|
|
/* |
1171
|
|
|
* acf_decode_taxonomy_terms |
1172
|
|
|
* |
1173
|
|
|
* This function decodes the $taxonomy:$term strings into a nested array |
1174
|
|
|
* |
1175
|
|
|
* @type function |
1176
|
|
|
* @date 27/02/2014 |
1177
|
|
|
* @since 5.0.0 |
1178
|
|
|
* |
1179
|
|
|
* @param $terms (array) |
1180
|
|
|
* @return (array) |
1181
|
|
|
*/ |
1182
|
|
|
|
1183
|
|
|
function acf_decode_taxonomy_terms( $terms = false ) { |
1184
|
|
|
|
1185
|
|
|
// load all taxonomies if not specified in args |
1186
|
|
|
if( !$terms ) { |
1187
|
|
|
|
1188
|
|
|
$terms = acf_get_taxonomy_terms(); |
1189
|
|
|
|
1190
|
|
|
} |
1191
|
|
|
|
1192
|
|
|
|
1193
|
|
|
// vars |
1194
|
|
|
$r = array(); |
1195
|
|
|
|
1196
|
|
|
|
1197
|
|
|
foreach( $terms as $term ) { |
|
|
|
|
1198
|
|
|
|
1199
|
|
|
// vars |
1200
|
|
|
$data = acf_decode_taxonomy_term( $term ); |
1201
|
|
|
|
1202
|
|
|
|
1203
|
|
|
// create empty array |
1204
|
|
|
if( !array_key_exists($data['taxonomy'], $r) ) |
1205
|
|
|
{ |
1206
|
|
|
$r[ $data['taxonomy'] ] = array(); |
1207
|
|
|
} |
1208
|
|
|
|
1209
|
|
|
|
1210
|
|
|
// append to taxonomy |
1211
|
|
|
$r[ $data['taxonomy'] ][] = $data['term']; |
1212
|
|
|
|
1213
|
|
|
} |
1214
|
|
|
|
1215
|
|
|
|
1216
|
|
|
// return |
1217
|
|
|
return $r; |
1218
|
|
|
|
1219
|
|
|
} |
1220
|
|
|
|
1221
|
|
|
|
1222
|
|
|
/* |
1223
|
|
|
* acf_decode_taxonomy_term |
1224
|
|
|
* |
1225
|
|
|
* This function will convert a term string into an array of term data |
1226
|
|
|
* |
1227
|
|
|
* @type function |
1228
|
|
|
* @date 31/03/2014 |
1229
|
|
|
* @since 5.0.0 |
1230
|
|
|
* |
1231
|
|
|
* @param $string (string) |
1232
|
|
|
* @return (array) |
1233
|
|
|
*/ |
1234
|
|
|
|
1235
|
|
|
function acf_decode_taxonomy_term( $string ) { |
1236
|
|
|
|
1237
|
|
|
// vars |
1238
|
|
|
$r = array(); |
1239
|
|
|
|
1240
|
|
|
|
1241
|
|
|
// vars |
1242
|
|
|
$data = explode(':', $string); |
1243
|
|
|
$taxonomy = 'category'; |
1244
|
|
|
$term = ''; |
1245
|
|
|
|
1246
|
|
|
|
1247
|
|
|
// check data |
1248
|
|
|
if( isset($data[1]) ) { |
1249
|
|
|
|
1250
|
|
|
$taxonomy = $data[0]; |
1251
|
|
|
$term = $data[1]; |
1252
|
|
|
|
1253
|
|
|
} |
1254
|
|
|
|
1255
|
|
|
|
1256
|
|
|
// add data to $r |
1257
|
|
|
$r['taxonomy'] = $taxonomy; |
1258
|
|
|
$r['term'] = $term; |
1259
|
|
|
|
1260
|
|
|
|
1261
|
|
|
// return |
1262
|
|
|
return $r; |
1263
|
|
|
|
1264
|
|
|
} |
1265
|
|
|
|
1266
|
|
|
|
1267
|
|
|
/* |
1268
|
|
|
* acf_cache_get |
1269
|
|
|
* |
1270
|
|
|
* This function is a wrapper for the wp_cache_get to allow for 3rd party customization |
1271
|
|
|
* |
1272
|
|
|
* @type function |
1273
|
|
|
* @date 4/12/2013 |
1274
|
|
|
* @since 5.0.0 |
1275
|
|
|
* |
1276
|
|
|
* @param $post_id (int) |
1277
|
|
|
* @return $post_id (int) |
1278
|
|
|
*/ |
1279
|
|
|
|
1280
|
|
|
/* |
1281
|
|
|
function acf_cache_get( $key, &$found ) { |
1282
|
|
|
|
1283
|
|
|
// vars |
1284
|
|
|
$group = 'acf'; |
1285
|
|
|
$force = false; |
1286
|
|
|
|
1287
|
|
|
|
1288
|
|
|
// load from cache |
1289
|
|
|
$cache = wp_cache_get( $key, $group, $force, $found ); |
1290
|
|
|
|
1291
|
|
|
|
1292
|
|
|
// allow 3rd party customization if cache was not found |
1293
|
|
|
if( !$found ) |
1294
|
|
|
{ |
1295
|
|
|
$custom = apply_filters("acf/get_cache/{$key}", $cache); |
1296
|
|
|
|
1297
|
|
|
if( $custom !== $cache ) |
1298
|
|
|
{ |
1299
|
|
|
$cache = $custom; |
1300
|
|
|
$found = true; |
1301
|
|
|
} |
1302
|
|
|
} |
1303
|
|
|
|
1304
|
|
|
|
1305
|
|
|
// return |
1306
|
|
|
return $cache; |
1307
|
|
|
|
1308
|
|
|
} |
1309
|
|
|
*/ |
1310
|
|
|
|
1311
|
|
|
|
1312
|
|
|
/* |
1313
|
|
|
* acf_get_array |
1314
|
|
|
* |
1315
|
|
|
* This function will force a variable to become an array |
1316
|
|
|
* |
1317
|
|
|
* @type function |
1318
|
|
|
* @date 4/02/2014 |
1319
|
|
|
* @since 5.0.0 |
1320
|
|
|
* |
1321
|
|
|
* @param $var (mixed) |
1322
|
|
|
* @return (array) |
1323
|
|
|
*/ |
1324
|
|
|
|
1325
|
|
|
function acf_get_array( $var = false, $delimiter = ',' ) { |
1326
|
|
|
|
1327
|
|
|
// is array? |
1328
|
|
|
if( is_array($var) ) { |
1329
|
|
|
|
1330
|
|
|
return $var; |
1331
|
|
|
|
1332
|
|
|
} |
1333
|
|
|
|
1334
|
|
|
|
1335
|
|
|
// bail early if empty |
1336
|
|
|
if( empty($var) && !is_numeric($var) ) { |
1337
|
|
|
|
1338
|
|
|
return array(); |
1339
|
|
|
|
1340
|
|
|
} |
1341
|
|
|
|
1342
|
|
|
|
1343
|
|
|
// string |
1344
|
|
|
if( is_string($var) && $delimiter ) { |
1345
|
|
|
|
1346
|
|
|
return explode($delimiter, $var); |
1347
|
|
|
|
1348
|
|
|
} |
1349
|
|
|
|
1350
|
|
|
|
1351
|
|
|
// place in array |
1352
|
|
|
return array( $var ); |
1353
|
|
|
|
1354
|
|
|
} |
1355
|
|
|
|
1356
|
|
|
|
1357
|
|
|
/* |
1358
|
|
|
* acf_get_posts |
1359
|
|
|
* |
1360
|
|
|
* This function will return an array of posts making sure the order is correct |
1361
|
|
|
* |
1362
|
|
|
* @type function |
1363
|
|
|
* @date 3/03/2015 |
1364
|
|
|
* @since 5.1.5 |
1365
|
|
|
* |
1366
|
|
|
* @param $args (array) |
1367
|
|
|
* @return (array) |
1368
|
|
|
*/ |
1369
|
|
|
|
1370
|
|
|
function acf_get_posts( $args = array() ) { |
1371
|
|
|
|
1372
|
|
|
// vars |
1373
|
|
|
$posts = array(); |
|
|
|
|
1374
|
|
|
|
1375
|
|
|
|
1376
|
|
|
// defaults |
1377
|
|
|
// leave suppress_filters as true becuase we don't want any plugins to modify the query as we know exactly what |
1378
|
|
|
$args = acf_parse_args( $args, array( |
1379
|
|
|
'posts_per_page' => -1, |
1380
|
|
|
'post_type' => '', |
1381
|
|
|
'post_status' => 'any' |
1382
|
|
|
)); |
1383
|
|
|
|
1384
|
|
|
|
1385
|
|
|
// post type |
1386
|
|
|
if( empty($args['post_type']) ) { |
1387
|
|
|
|
1388
|
|
|
$args['post_type'] = acf_get_post_types(); |
1389
|
|
|
|
1390
|
|
|
} |
1391
|
|
|
|
1392
|
|
|
|
1393
|
|
|
// validate post__in |
1394
|
|
|
if( $args['post__in'] ) { |
1395
|
|
|
|
1396
|
|
|
// force value to array |
1397
|
|
|
$args['post__in'] = acf_get_array( $args['post__in'] ); |
1398
|
|
|
|
1399
|
|
|
|
1400
|
|
|
// convert to int |
1401
|
|
|
$args['post__in'] = array_map('intval', $args['post__in']); |
1402
|
|
|
|
1403
|
|
|
|
1404
|
|
|
// add filter to remove post_type |
1405
|
|
|
// use 'query' filter so that 'suppress_filters' can remain true |
1406
|
|
|
//add_filter('query', '_acf_query_remove_post_type'); |
1407
|
|
|
|
1408
|
|
|
|
1409
|
|
|
// order by post__in |
1410
|
|
|
$args['orderby'] = 'post__in'; |
1411
|
|
|
|
1412
|
|
|
} |
1413
|
|
|
|
1414
|
|
|
|
1415
|
|
|
// load posts in 1 query to save multiple DB calls from following code |
1416
|
|
|
$posts = get_posts($args); |
1417
|
|
|
|
1418
|
|
|
|
1419
|
|
|
// remove this filter (only once) |
1420
|
|
|
//remove_filter('query', '_acf_query_remove_post_type'); |
1421
|
|
|
|
1422
|
|
|
|
1423
|
|
|
// validate order |
1424
|
|
|
if( $posts && $args['post__in'] ) { |
1425
|
|
|
|
1426
|
|
|
// vars |
1427
|
|
|
$order = array(); |
1428
|
|
|
|
1429
|
|
|
|
1430
|
|
|
// generate sort order |
1431
|
|
|
foreach( $posts as $i => $post ) { |
1432
|
|
|
|
1433
|
|
|
$order[ $i ] = array_search($post->ID, $args['post__in']); |
1434
|
|
|
|
1435
|
|
|
} |
1436
|
|
|
|
1437
|
|
|
|
1438
|
|
|
// sort |
1439
|
|
|
array_multisort($order, $posts); |
1440
|
|
|
|
1441
|
|
|
} |
1442
|
|
|
|
1443
|
|
|
|
1444
|
|
|
// return |
1445
|
|
|
return $posts; |
1446
|
|
|
|
1447
|
|
|
} |
1448
|
|
|
|
1449
|
|
|
|
1450
|
|
|
/* |
1451
|
|
|
* _acf_query_remove_post_type |
1452
|
|
|
* |
1453
|
|
|
* This function will remove the 'wp_posts.post_type' WHERE clause completely |
1454
|
|
|
* When using 'post__in', this clause is unneccessary and slow. |
1455
|
|
|
* |
1456
|
|
|
* @type function |
1457
|
|
|
* @date 4/03/2015 |
1458
|
|
|
* @since 5.1.5 |
1459
|
|
|
* |
1460
|
|
|
* @param $sql (string) |
1461
|
|
|
* @return $sql |
1462
|
|
|
*/ |
|
|
|
|
1463
|
|
|
|
1464
|
|
|
function _acf_query_remove_post_type( $sql ) { |
1465
|
|
|
|
1466
|
|
|
// global |
1467
|
|
|
global $wpdb; |
1468
|
|
|
|
1469
|
|
|
|
1470
|
|
|
// bail ealry if no 'wp_posts.ID IN' |
1471
|
|
|
if( strpos($sql, "$wpdb->posts.ID IN") === false ) { |
1472
|
|
|
|
1473
|
|
|
return $sql; |
1474
|
|
|
|
1475
|
|
|
} |
1476
|
|
|
|
1477
|
|
|
|
1478
|
|
|
// get bits |
1479
|
|
|
$glue = 'AND'; |
1480
|
|
|
$bits = explode($glue, $sql); |
1481
|
|
|
|
1482
|
|
|
|
1483
|
|
|
// loop through $where and remove any post_type queries |
1484
|
|
|
foreach( $bits as $i => $bit ) { |
1485
|
|
|
|
1486
|
|
|
if( strpos($bit, "$wpdb->posts.post_type") !== false ) { |
1487
|
|
|
|
1488
|
|
|
unset( $bits[ $i ] ); |
1489
|
|
|
|
1490
|
|
|
} |
1491
|
|
|
|
1492
|
|
|
} |
1493
|
|
|
|
1494
|
|
|
|
1495
|
|
|
// join $where back together |
1496
|
|
|
$sql = implode($glue, $bits); |
1497
|
|
|
|
1498
|
|
|
|
1499
|
|
|
// return |
1500
|
|
|
return $sql; |
1501
|
|
|
|
1502
|
|
|
} |
1503
|
|
|
|
1504
|
|
|
|
1505
|
|
|
/* |
1506
|
|
|
* acf_get_grouped_posts |
1507
|
|
|
* |
1508
|
|
|
* This function will return all posts grouped by post_type |
1509
|
|
|
* This is handy for select settings |
1510
|
|
|
* |
1511
|
|
|
* @type function |
1512
|
|
|
* @date 27/02/2014 |
1513
|
|
|
* @since 5.0.0 |
1514
|
|
|
* |
1515
|
|
|
* @param $args (array) |
1516
|
|
|
* @return (array) |
1517
|
|
|
*/ |
1518
|
|
|
|
1519
|
|
|
function acf_get_grouped_posts( $args ) { |
1520
|
|
|
|
1521
|
|
|
// vars |
1522
|
|
|
$r = array(); |
1523
|
|
|
|
1524
|
|
|
|
1525
|
|
|
// defaults |
1526
|
|
|
$args = acf_parse_args( $args, array( |
1527
|
|
|
'posts_per_page' => -1, |
1528
|
|
|
'paged' => 0, |
1529
|
|
|
'post_type' => 'post', |
1530
|
|
|
'orderby' => 'menu_order title', |
1531
|
|
|
'order' => 'ASC', |
1532
|
|
|
'post_status' => 'any', |
1533
|
|
|
'suppress_filters' => false, |
1534
|
|
|
'update_post_meta_cache' => false, |
1535
|
|
|
)); |
1536
|
|
|
|
1537
|
|
|
|
1538
|
|
|
// find array of post_type |
1539
|
|
|
$post_types = acf_get_array( $args['post_type'] ); |
1540
|
|
|
$post_types_labels = acf_get_pretty_post_types($post_types); |
1541
|
|
|
|
1542
|
|
|
|
1543
|
|
|
// attachment doesn't work if it is the only item in an array |
1544
|
|
|
if( count($post_types) == 1 ) { |
1545
|
|
|
|
1546
|
|
|
$args['post_type'] = current($post_types); |
1547
|
|
|
|
1548
|
|
|
} |
1549
|
|
|
|
1550
|
|
|
|
1551
|
|
|
// add filter to orderby post type |
1552
|
|
|
add_filter('posts_orderby', '_acf_orderby_post_type', 10, 2); |
1553
|
|
|
|
1554
|
|
|
|
1555
|
|
|
// get posts |
1556
|
|
|
$posts = get_posts( $args ); |
1557
|
|
|
|
1558
|
|
|
|
1559
|
|
|
// remove this filter (only once) |
1560
|
|
|
remove_filter('posts_orderby', '_acf_orderby_post_type'); |
1561
|
|
|
|
1562
|
|
|
|
1563
|
|
|
// loop |
1564
|
|
|
foreach( $post_types as $post_type ) { |
1565
|
|
|
|
1566
|
|
|
// vars |
1567
|
|
|
$this_posts = array(); |
1568
|
|
|
$this_group = array(); |
1569
|
|
|
|
1570
|
|
|
|
1571
|
|
|
// populate $this_posts |
1572
|
|
|
foreach( array_keys($posts) as $key ) { |
1573
|
|
|
|
1574
|
|
|
if( $posts[ $key ]->post_type == $post_type ) { |
1575
|
|
|
|
1576
|
|
|
$this_posts[] = acf_extract_var( $posts, $key ); |
1577
|
|
|
|
1578
|
|
|
} |
1579
|
|
|
|
1580
|
|
|
} |
1581
|
|
|
|
1582
|
|
|
|
1583
|
|
|
// bail early if no posts for this post type |
1584
|
|
|
if( empty($this_posts) ) { |
1585
|
|
|
|
1586
|
|
|
continue; |
1587
|
|
|
|
1588
|
|
|
} |
1589
|
|
|
|
1590
|
|
|
|
1591
|
|
|
// sort into hierachial order! |
1592
|
|
|
// this will fail if a search has taken place because parents wont exist |
1593
|
|
|
if( is_post_type_hierarchical($post_type) && empty($args['s'])) { |
1594
|
|
|
|
1595
|
|
|
// vars |
1596
|
|
|
$match_id = $this_posts[ 0 ]->ID; |
1597
|
|
|
$offset = 0; |
1598
|
|
|
$length = count($this_posts); |
1599
|
|
|
$parent = acf_maybe_get( $args, 'post_parent', 0 ); |
1600
|
|
|
|
1601
|
|
|
|
1602
|
|
|
// reset $this_posts |
1603
|
|
|
$this_posts = array(); |
1604
|
|
|
|
1605
|
|
|
|
1606
|
|
|
// get all posts |
1607
|
|
|
$all_args = array_merge($args, array( |
1608
|
|
|
'posts_per_page' => -1, |
1609
|
|
|
'paged' => 0, |
1610
|
|
|
'post_type' => $post_type |
1611
|
|
|
)); |
1612
|
|
|
|
1613
|
|
|
$all_posts = get_posts( $all_args ); |
1614
|
|
|
|
1615
|
|
|
|
1616
|
|
|
// loop over posts and update $offset |
1617
|
|
|
foreach( $all_posts as $offset => $p ) { |
1618
|
|
|
|
1619
|
|
|
if( $p->ID == $match_id ) { |
1620
|
|
|
|
1621
|
|
|
break; |
1622
|
|
|
|
1623
|
|
|
} |
1624
|
|
|
|
1625
|
|
|
} |
1626
|
|
|
|
1627
|
|
|
|
1628
|
|
|
// order posts |
1629
|
|
|
$all_posts = get_page_children( $parent, $all_posts ); |
1630
|
|
|
|
1631
|
|
|
|
1632
|
|
|
// append |
1633
|
|
|
for( $i = $offset; $i < ($offset + $length); $i++ ) { |
1634
|
|
|
|
1635
|
|
|
$this_posts[] = acf_extract_var( $all_posts, $i); |
1636
|
|
|
|
1637
|
|
|
} |
1638
|
|
|
|
1639
|
|
|
} |
1640
|
|
|
|
1641
|
|
|
|
1642
|
|
|
// populate $this_posts |
1643
|
|
|
foreach( array_keys($this_posts) as $key ) { |
1644
|
|
|
|
1645
|
|
|
// extract post |
1646
|
|
|
$post = acf_extract_var( $this_posts, $key ); |
1647
|
|
|
|
1648
|
|
|
|
1649
|
|
|
|
1650
|
|
|
// add to group |
1651
|
|
|
$this_group[ $post->ID ] = $post; |
1652
|
|
|
|
1653
|
|
|
} |
1654
|
|
|
|
1655
|
|
|
|
1656
|
|
|
// group by post type |
1657
|
|
|
$post_type_name = $post_types_labels[ $post_type ]; |
1658
|
|
|
|
1659
|
|
|
$r[ $post_type_name ] = $this_group; |
1660
|
|
|
|
1661
|
|
|
} |
1662
|
|
|
|
1663
|
|
|
|
1664
|
|
|
// return |
1665
|
|
|
return $r; |
1666
|
|
|
|
1667
|
|
|
} |
1668
|
|
|
|
1669
|
|
|
function _acf_orderby_post_type( $ordeby, $wp_query ) { |
1670
|
|
|
|
1671
|
|
|
// global |
1672
|
|
|
global $wpdb; |
1673
|
|
|
|
1674
|
|
|
|
1675
|
|
|
// get post types |
1676
|
|
|
$post_types = $wp_query->get('post_type'); |
1677
|
|
|
|
1678
|
|
|
|
1679
|
|
|
// prepend SQL |
1680
|
|
|
if( is_array($post_types) ) { |
1681
|
|
|
|
1682
|
|
|
$post_types = implode("','", $post_types); |
1683
|
|
|
$ordeby = "FIELD({$wpdb->posts}.post_type,'$post_types')," . $ordeby; |
1684
|
|
|
|
1685
|
|
|
} |
1686
|
|
|
|
1687
|
|
|
|
1688
|
|
|
// return |
1689
|
|
|
return $ordeby; |
1690
|
|
|
|
1691
|
|
|
} |
1692
|
|
|
|
1693
|
|
|
|
1694
|
|
|
function acf_get_post_title( $post = 0 ) { |
1695
|
|
|
|
1696
|
|
|
// load post if given an ID |
1697
|
|
|
if( is_numeric($post) ) { |
1698
|
|
|
|
1699
|
|
|
$post = get_post($post); |
1700
|
|
|
|
1701
|
|
|
} |
1702
|
|
|
|
1703
|
|
|
|
1704
|
|
|
// title |
1705
|
|
|
$title = get_the_title( $post->ID ); |
1706
|
|
|
|
1707
|
|
|
|
1708
|
|
|
// empty |
1709
|
|
|
if( $title === '' ) { |
1710
|
|
|
|
1711
|
|
|
$title = __('(no title)', 'acf'); |
1712
|
|
|
|
1713
|
|
|
} |
1714
|
|
|
|
1715
|
|
|
|
1716
|
|
|
// ancestors |
1717
|
|
|
if( $post->post_type != 'attachment' ) { |
1718
|
|
|
|
1719
|
|
|
$ancestors = get_ancestors( $post->ID, $post->post_type ); |
1720
|
|
|
|
1721
|
|
|
$title = str_repeat('- ', count($ancestors)) . $title; |
1722
|
|
|
|
1723
|
|
|
} |
1724
|
|
|
|
1725
|
|
|
|
1726
|
|
|
// status |
1727
|
|
|
if( get_post_status( $post->ID ) != "publish" ) { |
1728
|
|
|
|
1729
|
|
|
$title .= ' (' . get_post_status( $post->ID ) . ')'; |
1730
|
|
|
|
1731
|
|
|
} |
1732
|
|
|
|
1733
|
|
|
|
1734
|
|
|
// return |
1735
|
|
|
return $title; |
1736
|
|
|
|
1737
|
|
|
} |
1738
|
|
|
|
1739
|
|
|
|
1740
|
|
|
function acf_order_by_search( $array, $search ) { |
1741
|
|
|
|
1742
|
|
|
// vars |
1743
|
|
|
$weights = array(); |
1744
|
|
|
$needle = strtolower( $search ); |
1745
|
|
|
|
1746
|
|
|
|
1747
|
|
|
// add key prefix |
1748
|
|
|
foreach( array_keys($array) as $k ) { |
1749
|
|
|
|
1750
|
|
|
$array[ '_' . $k ] = acf_extract_var( $array, $k ); |
1751
|
|
|
|
1752
|
|
|
} |
1753
|
|
|
|
1754
|
|
|
|
1755
|
|
|
// add search weight |
1756
|
|
|
foreach( $array as $k => $v ) { |
1757
|
|
|
|
1758
|
|
|
// vars |
1759
|
|
|
$weight = 0; |
1760
|
|
|
$haystack = strtolower( $v ); |
1761
|
|
|
$strpos = strpos( $haystack, $needle ); |
1762
|
|
|
|
1763
|
|
|
|
1764
|
|
|
// detect search match |
1765
|
|
|
if( $strpos !== false ) { |
1766
|
|
|
|
1767
|
|
|
// set eright to length of match |
1768
|
|
|
$weight = strlen( $search ); |
1769
|
|
|
|
1770
|
|
|
|
1771
|
|
|
// increase weight if match starts at begining of string |
1772
|
|
|
if( $strpos == 0 ) { |
1773
|
|
|
|
1774
|
|
|
$weight++; |
1775
|
|
|
|
1776
|
|
|
} |
1777
|
|
|
|
1778
|
|
|
} |
1779
|
|
|
|
1780
|
|
|
|
1781
|
|
|
// append to wights |
1782
|
|
|
$weights[ $k ] = $weight; |
1783
|
|
|
|
1784
|
|
|
} |
1785
|
|
|
|
1786
|
|
|
|
1787
|
|
|
// sort the array with menu_order ascending |
1788
|
|
|
array_multisort( $weights, SORT_DESC, $array ); |
1789
|
|
|
|
1790
|
|
|
|
1791
|
|
|
// remove key prefix |
1792
|
|
|
foreach( array_keys($array) as $k ) { |
1793
|
|
|
|
1794
|
|
|
$array[ substr($k,1) ] = acf_extract_var( $array, $k ); |
1795
|
|
|
|
1796
|
|
|
} |
1797
|
|
|
|
1798
|
|
|
|
1799
|
|
|
// return |
1800
|
|
|
return $array; |
1801
|
|
|
} |
1802
|
|
|
|
1803
|
|
|
|
1804
|
|
|
|
1805
|
|
|
/* |
1806
|
|
|
* acf_json_encode |
1807
|
|
|
* |
1808
|
|
|
* This function will return pretty JSON for all PHP versions |
1809
|
|
|
* |
1810
|
|
|
* @type function |
1811
|
|
|
* @date 6/03/2014 |
1812
|
|
|
* @since 5.0.0 |
1813
|
|
|
* |
1814
|
|
|
* @param $json (array) |
1815
|
|
|
* @return (string) |
1816
|
|
|
*/ |
1817
|
|
|
|
1818
|
|
|
function acf_json_encode( $json ) { |
1819
|
|
|
|
1820
|
|
|
// PHP at least 5.4 |
1821
|
|
|
if( version_compare(PHP_VERSION, '5.4.0', '>=') ) { |
1822
|
|
|
|
1823
|
|
|
return json_encode($json, JSON_PRETTY_PRINT); |
1824
|
|
|
|
1825
|
|
|
} |
1826
|
|
|
|
1827
|
|
|
|
1828
|
|
|
|
1829
|
|
|
// PHP less than 5.4 |
1830
|
|
|
$json = json_encode($json); |
1831
|
|
|
|
1832
|
|
|
|
1833
|
|
|
// http://snipplr.com/view.php?codeview&id=60559 |
1834
|
|
|
$result = ''; |
1835
|
|
|
$pos = 0; |
1836
|
|
|
$strLen = strlen($json); |
1837
|
|
|
$indentStr = " "; |
1838
|
|
|
$newLine = "\n"; |
1839
|
|
|
$prevChar = ''; |
1840
|
|
|
$outOfQuotes = true; |
1841
|
|
|
|
1842
|
|
|
for ($i=0; $i<=$strLen; $i++) { |
1843
|
|
|
|
1844
|
|
|
// Grab the next character in the string. |
1845
|
|
|
$char = substr($json, $i, 1); |
1846
|
|
|
|
1847
|
|
|
// Are we inside a quoted string? |
1848
|
|
|
if ($char == '"' && $prevChar != '\\') { |
1849
|
|
|
$outOfQuotes = !$outOfQuotes; |
1850
|
|
|
|
1851
|
|
|
// If this character is the end of an element, |
1852
|
|
|
// output a new line and indent the next line. |
1853
|
|
|
} else if(($char == '}' || $char == ']') && $outOfQuotes) { |
1854
|
|
|
$result .= $newLine; |
1855
|
|
|
$pos --; |
1856
|
|
|
for ($j=0; $j<$pos; $j++) { |
1857
|
|
|
$result .= $indentStr; |
1858
|
|
|
} |
1859
|
|
|
} |
1860
|
|
|
|
1861
|
|
|
// Add the character to the result string. |
1862
|
|
|
$result .= $char; |
1863
|
|
|
|
1864
|
|
|
// If this character is ':' adda space after it |
1865
|
|
|
if($char == ':' && $outOfQuotes) { |
1866
|
|
|
$result .= ' '; |
1867
|
|
|
} |
1868
|
|
|
|
1869
|
|
|
// If the last character was the beginning of an element, |
1870
|
|
|
// output a new line and indent the next line. |
1871
|
|
|
if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) { |
1872
|
|
|
$result .= $newLine; |
1873
|
|
|
if ($char == '{' || $char == '[') { |
1874
|
|
|
$pos ++; |
1875
|
|
|
} |
1876
|
|
|
|
1877
|
|
|
for ($j = 0; $j < $pos; $j++) { |
1878
|
|
|
$result .= $indentStr; |
1879
|
|
|
} |
1880
|
|
|
} |
1881
|
|
|
|
1882
|
|
|
$prevChar = $char; |
1883
|
|
|
} |
1884
|
|
|
|
1885
|
|
|
|
1886
|
|
|
// return |
1887
|
|
|
return $result; |
1888
|
|
|
|
1889
|
|
|
} |
1890
|
|
|
|
1891
|
|
|
|
1892
|
|
|
/* |
1893
|
|
|
* acf_str_exists |
1894
|
|
|
* |
1895
|
|
|
* This function will return true if a sub string is found |
1896
|
|
|
* |
1897
|
|
|
* @type function |
1898
|
|
|
* @date 1/05/2014 |
1899
|
|
|
* @since 5.0.0 |
1900
|
|
|
* |
1901
|
|
|
* @param $needle (string) |
1902
|
|
|
* @param $haystack (string) |
1903
|
|
|
* @return (boolean) |
1904
|
|
|
*/ |
1905
|
|
|
|
1906
|
|
|
function acf_str_exists( $needle, $haystack ) { |
1907
|
|
|
|
1908
|
|
|
// return true if $haystack contains the $needle |
1909
|
|
|
if( is_string($haystack) && strpos($haystack, $needle) !== false ) { |
1910
|
|
|
|
1911
|
|
|
return true; |
1912
|
|
|
|
1913
|
|
|
} |
1914
|
|
|
|
1915
|
|
|
|
1916
|
|
|
// return |
1917
|
|
|
return false; |
1918
|
|
|
} |
1919
|
|
|
|
1920
|
|
|
|
1921
|
|
|
/* |
1922
|
|
|
* acf_debug |
1923
|
|
|
* |
1924
|
|
|
* description |
1925
|
|
|
* |
1926
|
|
|
* @type function |
1927
|
|
|
* @date 2/05/2014 |
1928
|
|
|
* @since 5.0.0 |
1929
|
|
|
* |
1930
|
|
|
* @param $post_id (int) |
1931
|
|
|
* @return $post_id (int) |
1932
|
|
|
*/ |
|
|
|
|
1933
|
|
|
|
1934
|
|
|
function acf_debug() { |
1935
|
|
|
|
1936
|
|
|
// vars |
1937
|
|
|
$args = func_get_args(); |
1938
|
|
|
$s = array_shift($args); |
1939
|
|
|
$o = ''; |
1940
|
|
|
$nl = "\r\n"; |
1941
|
|
|
|
1942
|
|
|
|
1943
|
|
|
// start script |
1944
|
|
|
$o .= '<script type="text/javascript">' . $nl; |
1945
|
|
|
|
1946
|
|
|
$o .= 'console.log("' . $s . '"'; |
1947
|
|
|
|
1948
|
|
|
if( !empty($args) ) { |
1949
|
|
|
|
1950
|
|
|
foreach( $args as $arg ) { |
1951
|
|
|
|
1952
|
|
|
if( is_object($arg) || is_array($arg) ) { |
1953
|
|
|
|
1954
|
|
|
$arg = json_encode($arg); |
1955
|
|
|
|
1956
|
|
|
} elseif( is_bool($arg) ) { |
1957
|
|
|
|
1958
|
|
|
$arg = $arg ? 'true' : 'false'; |
1959
|
|
|
|
1960
|
|
|
}elseif( is_string($arg) ) { |
1961
|
|
|
|
1962
|
|
|
$arg = '"' . $arg . '"'; |
1963
|
|
|
|
1964
|
|
|
} |
1965
|
|
|
|
1966
|
|
|
$o .= ', ' . $arg; |
1967
|
|
|
|
1968
|
|
|
} |
1969
|
|
|
} |
1970
|
|
|
|
1971
|
|
|
$o .= ');' . $nl; |
1972
|
|
|
|
1973
|
|
|
|
1974
|
|
|
// end script |
1975
|
|
|
$o .= '</script>' . $nl; |
1976
|
|
|
|
1977
|
|
|
|
1978
|
|
|
// echo |
1979
|
|
|
echo $o; |
1980
|
|
|
} |
1981
|
|
|
|
1982
|
|
|
function acf_debug_start() { |
1983
|
|
|
|
1984
|
|
|
acf_update_setting( 'debug_start', memory_get_usage()); |
1985
|
|
|
|
1986
|
|
|
} |
1987
|
|
|
|
1988
|
|
|
function acf_debug_end() { |
1989
|
|
|
|
1990
|
|
|
$start = acf_get_setting( 'debug_start' ); |
1991
|
|
|
$end = memory_get_usage(); |
1992
|
|
|
|
1993
|
|
|
return $end - $start; |
1994
|
|
|
|
1995
|
|
|
} |
1996
|
|
|
|
1997
|
|
|
|
1998
|
|
|
/* |
1999
|
|
|
* acf_get_updates |
2000
|
|
|
* |
2001
|
|
|
* This function will reutrn all or relevant updates for ACF |
2002
|
|
|
* |
2003
|
|
|
* @type function |
2004
|
|
|
* @date 12/05/2014 |
2005
|
|
|
* @since 5.0.0 |
2006
|
|
|
* |
2007
|
|
|
* @param $post_id (int) |
2008
|
|
|
* @return $post_id (int) |
2009
|
|
|
*/ |
|
|
|
|
2010
|
|
|
|
2011
|
|
|
function acf_get_updates() { |
2012
|
|
|
|
2013
|
|
|
// vars |
2014
|
|
|
$updates = array(); |
2015
|
|
|
$plugin_version = acf_get_setting('version'); |
2016
|
|
|
$acf_version = get_option('acf_version'); |
2017
|
|
|
$path = acf_get_path('admin/updates'); |
2018
|
|
|
|
2019
|
|
|
|
2020
|
|
|
// bail early if no version (not activated) |
2021
|
|
|
if( !$acf_version ) { |
2022
|
|
|
|
2023
|
|
|
return false; |
2024
|
|
|
|
2025
|
|
|
} |
2026
|
|
|
|
2027
|
|
|
|
2028
|
|
|
// check that path exists |
2029
|
|
|
if( !file_exists( $path ) ) { |
2030
|
|
|
|
2031
|
|
|
return false; |
2032
|
|
|
|
2033
|
|
|
} |
2034
|
|
|
|
2035
|
|
|
|
2036
|
|
|
$dir = opendir( $path ); |
2037
|
|
|
|
2038
|
|
|
while(false !== ( $file = readdir($dir)) ) { |
2039
|
|
|
|
2040
|
|
|
// only php files |
2041
|
|
|
if( substr($file, -4) !== '.php' ) { |
2042
|
|
|
|
2043
|
|
|
continue; |
2044
|
|
|
|
2045
|
|
|
} |
2046
|
|
|
|
2047
|
|
|
|
2048
|
|
|
// get version number |
2049
|
|
|
$update_version = substr($file, 0, -4); |
2050
|
|
|
|
2051
|
|
|
|
2052
|
|
|
// ignore if update is for a future version. May exist for testing |
2053
|
|
|
if( version_compare( $update_version, $plugin_version, '>') ) { |
2054
|
|
|
|
2055
|
|
|
continue; |
2056
|
|
|
|
2057
|
|
|
} |
2058
|
|
|
|
2059
|
|
|
// ignore if update has already been run |
2060
|
|
|
if( version_compare( $update_version, $acf_version, '<=') ) { |
2061
|
|
|
|
2062
|
|
|
continue; |
2063
|
|
|
|
2064
|
|
|
} |
2065
|
|
|
|
2066
|
|
|
|
2067
|
|
|
// append |
2068
|
|
|
$updates[] = $update_version; |
2069
|
|
|
|
2070
|
|
|
} |
2071
|
|
|
|
2072
|
|
|
|
2073
|
|
|
// return |
2074
|
|
|
return $updates; |
2075
|
|
|
|
2076
|
|
|
} |
2077
|
|
|
|
2078
|
|
|
|
2079
|
|
|
/* |
2080
|
|
|
* acf_encode_choices |
2081
|
|
|
* |
2082
|
|
|
* description |
2083
|
|
|
* |
2084
|
|
|
* @type function |
2085
|
|
|
* @date 4/06/2014 |
2086
|
|
|
* @since 5.0.0 |
2087
|
|
|
* |
2088
|
|
|
* @param $post_id (int) |
2089
|
|
|
* @return $post_id (int) |
2090
|
|
|
*/ |
|
|
|
|
2091
|
|
|
|
2092
|
|
|
function acf_encode_choices( $array = array() ) { |
2093
|
|
|
|
2094
|
|
|
// bail early if not array |
2095
|
|
|
if( !is_array($array) ) { |
2096
|
|
|
|
2097
|
|
|
return $array; |
2098
|
|
|
|
2099
|
|
|
} |
2100
|
|
|
|
2101
|
|
|
|
2102
|
|
|
// vars |
2103
|
|
|
$string = ''; |
2104
|
|
|
|
2105
|
|
|
|
2106
|
|
|
if( !empty($array) ) { |
2107
|
|
|
|
2108
|
|
|
foreach( $array as $k => $v ) { |
2109
|
|
|
|
2110
|
|
|
if( $k !== $v ) { |
2111
|
|
|
|
2112
|
|
|
$array[ $k ] = $k . ' : ' . $v; |
2113
|
|
|
|
2114
|
|
|
} |
2115
|
|
|
|
2116
|
|
|
} |
2117
|
|
|
|
2118
|
|
|
$string = implode("\n", $array); |
2119
|
|
|
|
2120
|
|
|
} |
2121
|
|
|
|
2122
|
|
|
|
2123
|
|
|
// return |
2124
|
|
|
return $string; |
2125
|
|
|
|
2126
|
|
|
} |
2127
|
|
|
|
2128
|
|
|
function acf_decode_choices( $string = '' ) { |
2129
|
|
|
|
2130
|
|
|
// validate |
2131
|
|
|
if( $string === '') { |
2132
|
|
|
|
2133
|
|
|
return array(); |
2134
|
|
|
|
2135
|
|
|
// force array on single numeric values |
2136
|
|
|
} elseif( is_numeric($string) ) { |
2137
|
|
|
|
2138
|
|
|
// allow |
2139
|
|
|
|
2140
|
|
|
// bail early if not a a string |
2141
|
|
|
} elseif( !is_string($string) ) { |
2142
|
|
|
|
2143
|
|
|
return $string; |
2144
|
|
|
|
2145
|
|
|
} |
2146
|
|
|
|
2147
|
|
|
|
2148
|
|
|
// vars |
2149
|
|
|
$array = array(); |
2150
|
|
|
|
2151
|
|
|
|
2152
|
|
|
// explode |
2153
|
|
|
$lines = explode("\n", $string); |
2154
|
|
|
|
2155
|
|
|
|
2156
|
|
|
// key => value |
2157
|
|
|
foreach( $lines as $line ) { |
2158
|
|
|
|
2159
|
|
|
// vars |
2160
|
|
|
$k = trim($line); |
2161
|
|
|
$v = trim($line); |
2162
|
|
|
|
2163
|
|
|
|
2164
|
|
|
// look for ' : ' |
2165
|
|
|
if( acf_str_exists(' : ', $line) ) { |
2166
|
|
|
|
2167
|
|
|
$line = explode(' : ', $line); |
2168
|
|
|
|
2169
|
|
|
$k = trim($line[0]); |
2170
|
|
|
$v = trim($line[1]); |
2171
|
|
|
|
2172
|
|
|
} |
2173
|
|
|
|
2174
|
|
|
|
2175
|
|
|
// append |
2176
|
|
|
$array[ $k ] = $v; |
2177
|
|
|
|
2178
|
|
|
} |
2179
|
|
|
|
2180
|
|
|
|
2181
|
|
|
// return |
2182
|
|
|
return $array; |
2183
|
|
|
|
2184
|
|
|
} |
2185
|
|
|
|
2186
|
|
|
|
2187
|
|
|
|
2188
|
|
|
/* |
2189
|
|
|
* acf_convert_date_to_php |
2190
|
|
|
* |
2191
|
|
|
* This fucntion converts a date format string from JS to PHP |
2192
|
|
|
* |
2193
|
|
|
* @type function |
2194
|
|
|
* @date 20/06/2014 |
2195
|
|
|
* @since 5.0.0 |
2196
|
|
|
* |
2197
|
|
|
* @param $date (string) |
2198
|
|
|
* @return $date (string) |
2199
|
|
|
*/ |
|
|
|
|
2200
|
|
|
|
2201
|
|
|
acf_update_setting('php_to_js_date_formats', array( |
2202
|
|
|
|
2203
|
|
|
// Year |
2204
|
|
|
'Y' => 'yy', // Numeric, 4 digits 1999, 2003 |
2205
|
|
|
'y' => 'y', // Numeric, 2 digits 99, 03 |
2206
|
|
|
|
2207
|
|
|
|
2208
|
|
|
// Month |
2209
|
|
|
'm' => 'mm', // Numeric, with leading zeros 01–12 |
2210
|
|
|
'n' => 'm', // Numeric, without leading zeros 1–12 |
2211
|
|
|
'F' => 'MM', // Textual full January – December |
2212
|
|
|
'M' => 'M', // Textual three letters Jan - Dec |
2213
|
|
|
|
2214
|
|
|
|
2215
|
|
|
// Weekday |
2216
|
|
|
'l' => 'DD', // Full name (lowercase 'L') Sunday – Saturday |
2217
|
|
|
'D' => 'D', // Three letter name Mon – Sun |
2218
|
|
|
|
2219
|
|
|
|
2220
|
|
|
// Day of Month |
2221
|
|
|
'd' => 'dd', // Numeric, with leading zeros 01–31 |
2222
|
|
|
'j' => 'd', // Numeric, without leading zeros 1–31 |
2223
|
|
|
'S' => '', // The English suffix for the day of the month st, nd or th in the 1st, 2nd or 15th. |
2224
|
|
|
|
2225
|
|
|
)); |
2226
|
|
|
|
2227
|
|
View Code Duplication |
function acf_convert_date_to_php( $date ) { |
|
|
|
|
2228
|
|
|
|
2229
|
|
|
// vars |
2230
|
|
|
$ignore = array(); |
2231
|
|
|
|
2232
|
|
|
|
2233
|
|
|
// conversion |
2234
|
|
|
$php_to_js = acf_get_setting('php_to_js_date_formats'); |
2235
|
|
|
|
2236
|
|
|
|
2237
|
|
|
// loop over conversions |
2238
|
|
|
foreach( $php_to_js as $replace => $search ) { |
2239
|
|
|
|
2240
|
|
|
// ignore this replace? |
2241
|
|
|
if( in_array($search, $ignore) ) { |
2242
|
|
|
|
2243
|
|
|
continue; |
2244
|
|
|
|
2245
|
|
|
} |
2246
|
|
|
|
2247
|
|
|
|
2248
|
|
|
// replace |
2249
|
|
|
$date = str_replace($search, $replace, $date); |
2250
|
|
|
|
2251
|
|
|
|
2252
|
|
|
// append to ignore |
2253
|
|
|
$ignore[] = $replace; |
2254
|
|
|
} |
2255
|
|
|
|
2256
|
|
|
|
2257
|
|
|
// return |
2258
|
|
|
return $date; |
2259
|
|
|
|
2260
|
|
|
} |
2261
|
|
|
|
2262
|
|
|
/* |
2263
|
|
|
* acf_convert_date_to_js |
2264
|
|
|
* |
2265
|
|
|
* This fucntion converts a date format string from PHP to JS |
2266
|
|
|
* |
2267
|
|
|
* @type function |
2268
|
|
|
* @date 20/06/2014 |
2269
|
|
|
* @since 5.0.0 |
2270
|
|
|
* |
2271
|
|
|
* @param $post_id (int) |
2272
|
|
|
* @return $post_id (int) |
2273
|
|
|
*/ |
|
|
|
|
2274
|
|
|
|
2275
|
|
View Code Duplication |
function acf_convert_date_to_js( $date ) { |
|
|
|
|
2276
|
|
|
|
2277
|
|
|
// vars |
2278
|
|
|
$ignore = array(); |
2279
|
|
|
|
2280
|
|
|
|
2281
|
|
|
// conversion |
2282
|
|
|
$php_to_js = acf_get_setting('php_to_js_date_formats'); |
2283
|
|
|
|
2284
|
|
|
|
2285
|
|
|
// loop over conversions |
2286
|
|
|
foreach( $php_to_js as $search => $replace ) { |
2287
|
|
|
|
2288
|
|
|
// ignore this replace? |
2289
|
|
|
if( in_array($search, $ignore) ) { |
2290
|
|
|
|
2291
|
|
|
continue; |
2292
|
|
|
|
2293
|
|
|
} |
2294
|
|
|
|
2295
|
|
|
|
2296
|
|
|
// replace |
2297
|
|
|
$date = str_replace($search, $replace, $date); |
2298
|
|
|
|
2299
|
|
|
|
2300
|
|
|
// append to ignore |
2301
|
|
|
$ignore[] = $replace; |
2302
|
|
|
} |
2303
|
|
|
|
2304
|
|
|
|
2305
|
|
|
// return |
2306
|
|
|
return $date; |
2307
|
|
|
|
2308
|
|
|
} |
2309
|
|
|
|
2310
|
|
|
|
2311
|
|
|
/* |
2312
|
|
|
* acf_update_user_setting |
2313
|
|
|
* |
2314
|
|
|
* description |
2315
|
|
|
* |
2316
|
|
|
* @type function |
2317
|
|
|
* @date 15/07/2014 |
2318
|
|
|
* @since 5.0.0 |
2319
|
|
|
* |
2320
|
|
|
* @param $post_id (int) |
2321
|
|
|
* @return $post_id (int) |
2322
|
|
|
*/ |
|
|
|
|
2323
|
|
|
|
2324
|
|
|
function acf_update_user_setting( $name, $value ) { |
2325
|
|
|
|
2326
|
|
|
// get current user id |
2327
|
|
|
$user_id = get_current_user_id(); |
2328
|
|
|
|
2329
|
|
|
|
2330
|
|
|
// get user settings |
2331
|
|
|
$settings = get_user_meta( $user_id, 'acf_user_settings', false ); |
2332
|
|
|
|
2333
|
|
|
|
2334
|
|
|
// find settings |
2335
|
|
|
if( isset($settings[0]) ) { |
2336
|
|
|
|
2337
|
|
|
$settings = $settings[0]; |
2338
|
|
|
|
2339
|
|
|
} else { |
2340
|
|
|
|
2341
|
|
|
$settings = array(); |
2342
|
|
|
|
2343
|
|
|
} |
2344
|
|
|
|
2345
|
|
|
|
2346
|
|
|
// delete setting (allow 0 to save) |
2347
|
|
|
if( !$value && !is_numeric($value) ) { |
2348
|
|
|
|
2349
|
|
|
unset($settings[ $name ]); |
2350
|
|
|
|
2351
|
|
|
// append setting |
2352
|
|
|
} else { |
2353
|
|
|
|
2354
|
|
|
$settings[ $name ] = $value; |
2355
|
|
|
|
2356
|
|
|
} |
2357
|
|
|
|
2358
|
|
|
|
2359
|
|
|
// update user data |
2360
|
|
|
return update_metadata('user', $user_id, 'acf_user_settings', $settings); |
2361
|
|
|
|
2362
|
|
|
|
2363
|
|
|
} |
2364
|
|
|
|
2365
|
|
|
|
2366
|
|
|
/* |
2367
|
|
|
* acf_get_user_setting |
2368
|
|
|
* |
2369
|
|
|
* description |
2370
|
|
|
* |
2371
|
|
|
* @type function |
2372
|
|
|
* @date 15/07/2014 |
2373
|
|
|
* @since 5.0.0 |
2374
|
|
|
* |
2375
|
|
|
* @param $post_id (int) |
2376
|
|
|
* @return $post_id (int) |
2377
|
|
|
*/ |
|
|
|
|
2378
|
|
|
|
2379
|
|
|
function acf_get_user_setting( $name = '', $default = false ) { |
2380
|
|
|
|
2381
|
|
|
// get current user id |
2382
|
|
|
$user_id = get_current_user_id(); |
2383
|
|
|
|
2384
|
|
|
|
2385
|
|
|
// get user settings |
2386
|
|
|
$settings = get_user_meta( $user_id, 'acf_user_settings', false ); |
2387
|
|
|
|
2388
|
|
|
|
2389
|
|
|
// bail arly if no settings |
2390
|
|
|
if( !isset($settings[0][$name]) ) { |
2391
|
|
|
|
2392
|
|
|
return $default; |
2393
|
|
|
|
2394
|
|
|
} |
2395
|
|
|
|
2396
|
|
|
|
2397
|
|
|
// return |
2398
|
|
|
return $settings[0][$name]; |
2399
|
|
|
|
2400
|
|
|
} |
2401
|
|
|
|
2402
|
|
|
|
2403
|
|
|
/* |
2404
|
|
|
* acf_in_array |
2405
|
|
|
* |
2406
|
|
|
* description |
2407
|
|
|
* |
2408
|
|
|
* @type function |
2409
|
|
|
* @date 22/07/2014 |
2410
|
|
|
* @since 5.0.0 |
2411
|
|
|
* |
2412
|
|
|
* @param $post_id (int) |
2413
|
|
|
* @return $post_id (int) |
2414
|
|
|
*/ |
|
|
|
|
2415
|
|
|
|
2416
|
|
|
function acf_in_array( $value, $array ) { |
2417
|
|
|
|
2418
|
|
|
// bail early if not array |
2419
|
|
|
if( !is_array($array) ) { |
2420
|
|
|
|
2421
|
|
|
return false; |
2422
|
|
|
|
2423
|
|
|
} |
2424
|
|
|
|
2425
|
|
|
|
2426
|
|
|
// find value in array |
2427
|
|
|
return in_array($value, $array); |
2428
|
|
|
|
2429
|
|
|
} |
2430
|
|
|
|
2431
|
|
|
|
2432
|
|
|
/* |
2433
|
|
|
* acf_get_valid_post_id |
2434
|
|
|
* |
2435
|
|
|
* This function will return a valid post_id based on the current screen / parameter |
2436
|
|
|
* |
2437
|
|
|
* @type function |
2438
|
|
|
* @date 8/12/2013 |
2439
|
|
|
* @since 5.0.0 |
2440
|
|
|
* |
2441
|
|
|
* @param $post_id (mixed) |
2442
|
|
|
* @return $post_id (mixed) |
2443
|
|
|
*/ |
|
|
|
|
2444
|
|
|
|
2445
|
|
|
function acf_get_valid_post_id( $post_id = 0 ) { |
2446
|
|
|
|
2447
|
|
|
// set post_id to global |
2448
|
|
|
if( !$post_id ) { |
2449
|
|
|
|
2450
|
|
|
$post_id = (int) get_the_ID(); |
2451
|
|
|
|
2452
|
|
|
} |
2453
|
|
|
|
2454
|
|
|
|
2455
|
|
|
// allow for option == options |
2456
|
|
|
if( $post_id == 'option' ) { |
2457
|
|
|
|
2458
|
|
|
$post_id = 'options'; |
2459
|
|
|
|
2460
|
|
|
} |
2461
|
|
|
|
2462
|
|
|
|
2463
|
|
|
// $post_id may be an object |
2464
|
|
|
if( is_object($post_id) ) { |
2465
|
|
|
|
2466
|
|
|
if( isset($post_id->roles, $post_id->ID) ) { |
2467
|
|
|
|
2468
|
|
|
$post_id = 'user_' . $post_id->ID; |
2469
|
|
|
|
2470
|
|
|
} elseif( isset($post_id->taxonomy, $post_id->term_id) ) { |
2471
|
|
|
|
2472
|
|
|
$post_id = $post_id->taxonomy . '_' . $post_id->term_id; |
2473
|
|
|
|
2474
|
|
|
} elseif( isset($post_id->comment_ID) ) { |
2475
|
|
|
|
2476
|
|
|
$post_id = 'comment_' . $post_id->comment_ID; |
2477
|
|
|
|
2478
|
|
|
} elseif( isset($post_id->ID) ) { |
2479
|
|
|
|
2480
|
|
|
$post_id = $post_id->ID; |
2481
|
|
|
|
2482
|
|
|
} |
2483
|
|
|
|
2484
|
|
|
} |
2485
|
|
|
|
2486
|
|
|
|
2487
|
|
|
// append language code |
2488
|
|
|
if( $post_id == 'options' ) { |
2489
|
|
|
|
2490
|
|
|
$dl = acf_get_setting('default_language'); |
2491
|
|
|
$cl = acf_get_setting('current_language'); |
2492
|
|
|
|
2493
|
|
|
if( $cl && $cl !== $dl ) { |
2494
|
|
|
|
2495
|
|
|
$post_id .= '_' . $cl; |
2496
|
|
|
|
2497
|
|
|
} |
2498
|
|
|
|
2499
|
|
|
} |
2500
|
|
|
|
2501
|
|
|
|
2502
|
|
|
/* |
2503
|
|
|
* Override for preview |
2504
|
|
|
* |
2505
|
|
|
* If the $_GET['preview_id'] is set, then the user wants to see the preview data. |
2506
|
|
|
* There is also the case of previewing a page with post_id = 1, but using get_field |
2507
|
|
|
* to load data from another post_id. |
2508
|
|
|
* In this case, we need to make sure that the autosave revision is actually related |
2509
|
|
|
* to the $post_id variable. If they match, then the autosave data will be used, otherwise, |
2510
|
|
|
* the user wants to load data from a completely different post_id |
2511
|
|
|
*/ |
2512
|
|
|
|
2513
|
|
|
if( isset($_GET['preview_id']) ) { |
2514
|
|
|
|
2515
|
|
|
$autosave = wp_get_post_autosave( $_GET['preview_id'] ); |
2516
|
|
|
|
2517
|
|
|
if( $autosave && $autosave->post_parent == $post_id ) { |
2518
|
|
|
|
2519
|
|
|
$post_id = (int) $autosave->ID; |
2520
|
|
|
|
2521
|
|
|
} |
2522
|
|
|
|
2523
|
|
|
} |
2524
|
|
|
|
2525
|
|
|
|
2526
|
|
|
// return |
2527
|
|
|
return $post_id; |
2528
|
|
|
|
2529
|
|
|
} |
2530
|
|
|
|
2531
|
|
|
|
2532
|
|
|
/* |
2533
|
|
|
* acf_upload_files |
2534
|
|
|
* |
2535
|
|
|
* This function will walk througfh the $_FILES data and upload each found |
2536
|
|
|
* |
2537
|
|
|
* @type function |
2538
|
|
|
* @date 25/10/2014 |
2539
|
|
|
* @since 5.0.9 |
2540
|
|
|
* |
2541
|
|
|
* @param $ancestors (array) an internal parameter, not required |
2542
|
|
|
* @return n/a |
2543
|
|
|
*/ |
|
|
|
|
2544
|
|
|
|
2545
|
|
|
function acf_upload_files( $ancestors = array() ) { |
2546
|
|
|
|
2547
|
|
|
// vars |
2548
|
|
|
$file = array( |
2549
|
|
|
'name' => '', |
2550
|
|
|
'type' => '', |
2551
|
|
|
'tmp_name' => '', |
2552
|
|
|
'error' => '', |
2553
|
|
|
'size' => '' |
2554
|
|
|
); |
2555
|
|
|
|
2556
|
|
|
|
2557
|
|
|
// populate with $_FILES data |
2558
|
|
|
foreach( array_keys($file) as $k ) { |
2559
|
|
|
|
2560
|
|
|
$file[ $k ] = $_FILES['acf'][ $k ]; |
2561
|
|
|
|
2562
|
|
|
} |
2563
|
|
|
|
2564
|
|
|
|
2565
|
|
|
// walk through ancestors |
2566
|
|
|
if( !empty($ancestors) ) { |
2567
|
|
|
|
2568
|
|
|
foreach( $ancestors as $a ) { |
2569
|
|
|
|
2570
|
|
|
foreach( array_keys($file) as $k ) { |
2571
|
|
|
|
2572
|
|
|
$file[ $k ] = $file[ $k ][ $a ]; |
2573
|
|
|
|
2574
|
|
|
} |
2575
|
|
|
|
2576
|
|
|
} |
2577
|
|
|
|
2578
|
|
|
} |
2579
|
|
|
|
2580
|
|
|
|
2581
|
|
|
// is array? |
2582
|
|
|
if( is_array($file['name']) ) { |
2583
|
|
|
|
2584
|
|
|
foreach( array_keys($file['name']) as $k ) { |
2585
|
|
|
|
2586
|
|
|
$_ancestors = array_merge($ancestors, array($k)); |
2587
|
|
|
|
2588
|
|
|
acf_upload_files( $_ancestors ); |
2589
|
|
|
|
2590
|
|
|
} |
2591
|
|
|
|
2592
|
|
|
return; |
2593
|
|
|
|
2594
|
|
|
} |
2595
|
|
|
|
2596
|
|
|
|
2597
|
|
|
// bail ealry if file has error (no file uploaded) |
2598
|
|
|
if( $file['error'] ) { |
2599
|
|
|
|
2600
|
|
|
return; |
2601
|
|
|
|
2602
|
|
|
} |
2603
|
|
|
|
2604
|
|
|
|
2605
|
|
|
// assign global _acfuploader for media validation |
2606
|
|
|
$_POST['_acfuploader'] = end($ancestors); |
2607
|
|
|
|
2608
|
|
|
|
2609
|
|
|
// file found! |
2610
|
|
|
$attachment_id = acf_upload_file( $file ); |
2611
|
|
|
|
2612
|
|
|
|
2613
|
|
|
// update $_POST |
2614
|
|
|
array_unshift($ancestors, 'acf'); |
2615
|
|
|
acf_update_nested_array( $_POST, $ancestors, $attachment_id ); |
2616
|
|
|
|
2617
|
|
|
} |
2618
|
|
|
|
2619
|
|
|
|
2620
|
|
|
/* |
2621
|
|
|
* acf_upload_file |
2622
|
|
|
* |
2623
|
|
|
* This function will uploade a $_FILE |
2624
|
|
|
* |
2625
|
|
|
* @type function |
2626
|
|
|
* @date 27/10/2014 |
2627
|
|
|
* @since 5.0.9 |
2628
|
|
|
* |
2629
|
|
|
* @param $uploaded_file (array) array found from $_FILE data |
2630
|
|
|
* @return $id (int) new attachment ID |
2631
|
|
|
*/ |
|
|
|
|
2632
|
|
|
|
2633
|
|
|
function acf_upload_file( $uploaded_file ) { |
2634
|
|
|
|
2635
|
|
|
// required |
2636
|
|
|
require_once( ABSPATH . "/wp-load.php" ); |
2637
|
|
|
require_once( ABSPATH . "/wp-admin/includes/file.php" ); |
2638
|
|
|
require_once( ABSPATH . "/wp-admin/includes/image.php" ); |
2639
|
|
|
|
2640
|
|
|
|
2641
|
|
|
// required for wp_handle_upload() to upload the file |
2642
|
|
|
$upload_overrides = array( 'test_form' => false ); |
2643
|
|
|
|
2644
|
|
|
|
2645
|
|
|
// upload |
2646
|
|
|
$file = wp_handle_upload( $uploaded_file, $upload_overrides ); |
2647
|
|
|
|
2648
|
|
|
|
2649
|
|
|
// bail ealry if upload failed |
2650
|
|
|
if( isset($file['error']) ) { |
2651
|
|
|
|
2652
|
|
|
return $file['error']; |
2653
|
|
|
|
2654
|
|
|
} |
2655
|
|
|
|
2656
|
|
|
|
2657
|
|
|
// vars |
2658
|
|
|
$url = $file['url']; |
2659
|
|
|
$type = $file['type']; |
2660
|
|
|
$file = $file['file']; |
2661
|
|
|
$filename = basename($file); |
2662
|
|
|
|
2663
|
|
|
|
2664
|
|
|
// Construct the object array |
2665
|
|
|
$object = array( |
2666
|
|
|
'post_title' => $filename, |
2667
|
|
|
'post_mime_type' => $type, |
2668
|
|
|
'guid' => $url, |
2669
|
|
|
'context' => 'acf-upload' |
2670
|
|
|
); |
2671
|
|
|
|
2672
|
|
|
// Save the data |
2673
|
|
|
$id = wp_insert_attachment($object, $file); |
2674
|
|
|
|
2675
|
|
|
// Add the meta-data |
2676
|
|
|
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) ); |
2677
|
|
|
|
2678
|
|
|
/** This action is documented in wp-admin/custom-header.php */ |
2679
|
|
|
do_action( 'wp_create_file_in_uploads', $file, $id ); // For replication |
2680
|
|
|
|
2681
|
|
|
// return new ID |
2682
|
|
|
return $id; |
2683
|
|
|
|
2684
|
|
|
} |
2685
|
|
|
|
2686
|
|
|
|
2687
|
|
|
/* |
2688
|
|
|
* acf_update_nested_array |
2689
|
|
|
* |
2690
|
|
|
* This function will update a nested array value. Useful for modifying the $_POST array |
2691
|
|
|
* |
2692
|
|
|
* @type function |
2693
|
|
|
* @date 27/10/2014 |
2694
|
|
|
* @since 5.0.9 |
2695
|
|
|
* |
2696
|
|
|
* @param $array (array) target array to be updated |
2697
|
|
|
* @param $ancestors (array) array of keys to navigate through to find the child |
2698
|
|
|
* @param $value (mixed) The new value |
2699
|
|
|
* @return (boolean) |
2700
|
|
|
*/ |
2701
|
|
|
|
2702
|
|
|
function acf_update_nested_array( &$array, $ancestors, $value ) { |
2703
|
|
|
|
2704
|
|
|
// if no more ancestors, update the current var |
2705
|
|
|
if( empty($ancestors) ) { |
2706
|
|
|
|
2707
|
|
|
$array = $value; |
2708
|
|
|
|
2709
|
|
|
// return |
2710
|
|
|
return true; |
2711
|
|
|
|
2712
|
|
|
} |
2713
|
|
|
|
2714
|
|
|
|
2715
|
|
|
// shift the next ancestor from the array |
2716
|
|
|
$k = array_shift( $ancestors ); |
2717
|
|
|
|
2718
|
|
|
|
2719
|
|
|
// if exists |
2720
|
|
|
if( isset($array[ $k ]) ) { |
2721
|
|
|
|
2722
|
|
|
return acf_update_nested_array( $array[ $k ], $ancestors, $value ); |
2723
|
|
|
|
2724
|
|
|
} |
2725
|
|
|
|
2726
|
|
|
|
2727
|
|
|
// return |
2728
|
|
|
return false; |
2729
|
|
|
} |
2730
|
|
|
|
2731
|
|
|
|
2732
|
|
|
/* |
2733
|
|
|
* acf_is_screen |
2734
|
|
|
* |
2735
|
|
|
* This function will return true if all args are matched for the current screen |
2736
|
|
|
* |
2737
|
|
|
* @type function |
2738
|
|
|
* @date 9/12/2014 |
2739
|
|
|
* @since 5.1.5 |
2740
|
|
|
* |
2741
|
|
|
* @param $post_id (int) |
2742
|
|
|
* @return $post_id (int) |
2743
|
|
|
*/ |
|
|
|
|
2744
|
|
|
|
2745
|
|
|
function acf_is_screen( $id = '' ) { |
2746
|
|
|
|
2747
|
|
|
// vars |
2748
|
|
|
$current_screen = get_current_screen(); |
2749
|
|
|
|
2750
|
|
|
|
2751
|
|
|
// return |
2752
|
|
|
return ($id === $current_screen->id); |
2753
|
|
|
|
2754
|
|
|
} |
2755
|
|
|
|
2756
|
|
|
|
2757
|
|
|
/* |
2758
|
|
|
* acf_maybe_get |
2759
|
|
|
* |
2760
|
|
|
* This function will return a var if it exists in an array |
2761
|
|
|
* |
2762
|
|
|
* @type function |
2763
|
|
|
* @date 9/12/2014 |
2764
|
|
|
* @since 5.1.5 |
2765
|
|
|
* |
2766
|
|
|
* @param $array (array) the array to look within |
2767
|
|
|
* @param $key (key) the array key to look for. Nested values may be found using '/' |
2768
|
|
|
* @param $default (mixed) the value returned if not found |
2769
|
|
|
* @return $post_id (int) |
2770
|
|
|
*/ |
|
|
|
|
2771
|
|
|
|
2772
|
|
|
function acf_maybe_get( $array, $key, $default = null ) { |
2773
|
|
|
|
2774
|
|
|
// vars |
2775
|
|
|
$keys = explode('/', $key); |
2776
|
|
|
|
2777
|
|
|
|
2778
|
|
|
// loop through keys |
2779
|
|
|
foreach( $keys as $k ) { |
2780
|
|
|
|
2781
|
|
|
// return default if does not exist |
2782
|
|
|
if( !isset($array[ $k ]) ) { |
2783
|
|
|
|
2784
|
|
|
return $default; |
2785
|
|
|
|
2786
|
|
|
} |
2787
|
|
|
|
2788
|
|
|
|
2789
|
|
|
// update $array |
2790
|
|
|
$array = $array[ $k ]; |
2791
|
|
|
|
2792
|
|
|
} |
2793
|
|
|
|
2794
|
|
|
|
2795
|
|
|
// return |
2796
|
|
|
return $array; |
2797
|
|
|
|
2798
|
|
|
} |
2799
|
|
|
|
2800
|
|
|
|
2801
|
|
|
/* |
2802
|
|
|
* acf_get_attachment |
2803
|
|
|
* |
2804
|
|
|
* This function will return an array of attachment data |
2805
|
|
|
* |
2806
|
|
|
* @type function |
2807
|
|
|
* @date 5/01/2015 |
2808
|
|
|
* @since 5.1.5 |
2809
|
|
|
* |
2810
|
|
|
* @param $post (mixed) either post ID or post object |
2811
|
|
|
* @return (array) |
2812
|
|
|
*/ |
2813
|
|
|
|
2814
|
|
|
function acf_get_attachment( $post ) { |
2815
|
|
|
|
2816
|
|
|
// get post |
2817
|
|
|
if ( !$post = get_post( $post ) ) { |
2818
|
|
|
|
2819
|
|
|
return false; |
2820
|
|
|
|
2821
|
|
|
} |
2822
|
|
|
|
2823
|
|
|
|
2824
|
|
|
// vars |
2825
|
|
|
$thumb_id = 0; |
2826
|
|
|
$id = $post->ID; |
2827
|
|
|
$a = array( |
2828
|
|
|
'ID' => $id, |
2829
|
|
|
'id' => $id, |
2830
|
|
|
'title' => $post->post_title, |
2831
|
|
|
'filename' => wp_basename( $post->guid ), |
2832
|
|
|
'url' => wp_get_attachment_url( $id ), |
2833
|
|
|
'alt' => get_post_meta($id, '_wp_attachment_image_alt', true), |
2834
|
|
|
'author' => $post->post_author, |
2835
|
|
|
'description' => $post->post_content, |
2836
|
|
|
'caption' => $post->post_excerpt, |
2837
|
|
|
'name' => $post->post_name, |
2838
|
|
|
'date' => $post->post_date_gmt, |
2839
|
|
|
'modified' => $post->post_modified_gmt, |
2840
|
|
|
'mime_type' => $post->post_mime_type, |
2841
|
|
|
'type' => acf_maybe_get( explode('/', $post->post_mime_type), 0, '' ), |
2842
|
|
|
'icon' => wp_mime_type_icon( $id ) |
2843
|
|
|
); |
2844
|
|
|
|
2845
|
|
|
|
2846
|
|
|
// video may use featured image |
2847
|
|
|
if( $a['type'] === 'image' ) { |
2848
|
|
|
|
2849
|
|
|
$thumb_id = $id; |
2850
|
|
|
$src = wp_get_attachment_image_src( $id, 'full' ); |
2851
|
|
|
|
2852
|
|
|
$a['url'] = $src[0]; |
2853
|
|
|
$a['width'] = $src[1]; |
2854
|
|
|
$a['height'] = $src[2]; |
2855
|
|
|
|
2856
|
|
|
|
2857
|
|
|
} elseif( $a['type'] === 'audio' || $a['type'] === 'video' ) { |
2858
|
|
|
|
2859
|
|
|
// video dimentions |
2860
|
|
|
if( $a['type'] == 'video' ) { |
2861
|
|
|
|
2862
|
|
|
$meta = wp_get_attachment_metadata( $id ); |
2863
|
|
|
$a['width'] = acf_maybe_get($meta, 'width', 0); |
2864
|
|
|
$a['height'] = acf_maybe_get($meta, 'height', 0); |
2865
|
|
|
|
2866
|
|
|
} |
2867
|
|
|
|
2868
|
|
|
|
2869
|
|
|
// feature image |
2870
|
|
|
if( $featured_id = get_post_thumbnail_id($id) ) { |
2871
|
|
|
|
2872
|
|
|
$thumb_id = $featured_id; |
2873
|
|
|
|
2874
|
|
|
} |
2875
|
|
|
|
2876
|
|
|
} |
2877
|
|
|
|
2878
|
|
|
|
2879
|
|
|
// sizes |
2880
|
|
|
if( $thumb_id ) { |
2881
|
|
|
|
2882
|
|
|
// find all image sizes |
2883
|
|
|
if( $sizes = get_intermediate_image_sizes() ) { |
2884
|
|
|
|
2885
|
|
|
$a['sizes'] = array(); |
2886
|
|
|
|
2887
|
|
|
foreach( $sizes as $size ) { |
2888
|
|
|
|
2889
|
|
|
// url |
2890
|
|
|
$src = wp_get_attachment_image_src( $thumb_id, $size ); |
2891
|
|
|
|
2892
|
|
|
// add src |
2893
|
|
|
$a['sizes'][ $size ] = $src[0]; |
2894
|
|
|
$a['sizes'][ $size . '-width' ] = $src[1]; |
2895
|
|
|
$a['sizes'][ $size . '-height' ] = $src[2]; |
2896
|
|
|
|
2897
|
|
|
} |
2898
|
|
|
|
2899
|
|
|
} |
2900
|
|
|
|
2901
|
|
|
} |
2902
|
|
|
|
2903
|
|
|
|
2904
|
|
|
// return |
2905
|
|
|
return $a; |
2906
|
|
|
|
2907
|
|
|
} |
2908
|
|
|
|
2909
|
|
|
|
2910
|
|
|
/* |
2911
|
|
|
* acf_get_truncated |
2912
|
|
|
* |
2913
|
|
|
* This function will truncate and return a string |
2914
|
|
|
* |
2915
|
|
|
* @type function |
2916
|
|
|
* @date 8/08/2014 |
2917
|
|
|
* @since 5.0.0 |
2918
|
|
|
* |
2919
|
|
|
* @param $text (string) |
2920
|
|
|
* @param $length (int) |
2921
|
|
|
* @return (string) |
2922
|
|
|
*/ |
2923
|
|
|
|
2924
|
|
|
function acf_get_truncated( $text, $length = 64 ) { |
2925
|
|
|
|
2926
|
|
|
// vars |
2927
|
|
|
$text = trim($text); |
2928
|
|
|
$the_length = strlen( $text ); |
2929
|
|
|
|
2930
|
|
|
|
2931
|
|
|
// cut |
2932
|
|
|
$return = substr( $text, 0, ($length - 3) ); |
2933
|
|
|
|
2934
|
|
|
|
2935
|
|
|
// ... |
2936
|
|
|
if( $the_length > ($length - 3) ) { |
2937
|
|
|
|
2938
|
|
|
$return .= '...'; |
2939
|
|
|
|
2940
|
|
|
} |
2941
|
|
|
|
2942
|
|
|
|
2943
|
|
|
// return |
2944
|
|
|
return $return; |
2945
|
|
|
|
2946
|
|
|
} |
2947
|
|
|
|
2948
|
|
|
|
2949
|
|
|
/* |
2950
|
|
|
* acf_get_current_url |
2951
|
|
|
* |
2952
|
|
|
* This function will return the current URL |
2953
|
|
|
* |
2954
|
|
|
* @type function |
2955
|
|
|
* @date 23/01/2015 |
2956
|
|
|
* @since 5.1.5 |
2957
|
|
|
* |
2958
|
|
|
* @param n/a |
2959
|
|
|
* @return (string) |
2960
|
|
|
*/ |
2961
|
|
|
|
2962
|
|
|
function acf_get_current_url() { |
2963
|
|
|
|
2964
|
|
|
// vars |
2965
|
|
|
$home = home_url(); |
2966
|
|
|
$url = home_url($_SERVER['REQUEST_URI']); |
2967
|
|
|
|
2968
|
|
|
|
2969
|
|
|
// test |
2970
|
|
|
//$home = 'http://acf5/dev/wp-admin'; |
2971
|
|
|
//$url = $home . '/dev/wp-admin/api-template/acf_form'; |
2972
|
|
|
|
2973
|
|
|
|
2974
|
|
|
// explode url (4th bit is the sub folder) |
2975
|
|
|
$bits = explode('/', $home, 4); |
2976
|
|
|
|
2977
|
|
|
|
2978
|
|
|
/* |
2979
|
|
|
Array ( |
2980
|
|
|
[0] => http: |
2981
|
|
|
[1] => |
2982
|
|
|
[2] => acf5 |
2983
|
|
|
[3] => dev |
2984
|
|
|
) |
2985
|
|
|
*/ |
2986
|
|
|
|
2987
|
|
|
|
2988
|
|
|
// handle sub folder |
2989
|
|
|
if( !empty($bits[3]) ) { |
2990
|
|
|
|
2991
|
|
|
$find = '/' . $bits[3]; |
2992
|
|
|
$pos = strpos($url, $find); |
2993
|
|
|
$length = strlen($find); |
2994
|
|
|
|
2995
|
|
|
if( $pos !== false ) { |
2996
|
|
|
|
2997
|
|
|
$url = substr_replace($url, '', $pos, $length); |
2998
|
|
|
|
2999
|
|
|
} |
3000
|
|
|
|
3001
|
|
|
} |
3002
|
|
|
|
3003
|
|
|
|
3004
|
|
|
// return |
3005
|
|
|
return $url; |
3006
|
|
|
|
3007
|
|
|
} |
3008
|
|
|
|
3009
|
|
|
|
3010
|
|
|
/* |
3011
|
|
|
* acf_current_user_can_admin |
3012
|
|
|
* |
3013
|
|
|
* This function will return true if the current user can administrate the ACF field groups |
3014
|
|
|
* |
3015
|
|
|
* @type function |
3016
|
|
|
* @date 9/02/2015 |
3017
|
|
|
* @since 5.1.5 |
3018
|
|
|
* |
3019
|
|
|
* @param $post_id (int) |
3020
|
|
|
* @return $post_id (int) |
3021
|
|
|
*/ |
|
|
|
|
3022
|
|
|
|
3023
|
|
|
function acf_current_user_can_admin() { |
3024
|
|
|
|
3025
|
|
|
if( acf_get_setting('show_admin') && current_user_can(acf_get_setting('capability')) ) { |
3026
|
|
|
|
3027
|
|
|
return true; |
3028
|
|
|
|
3029
|
|
|
} |
3030
|
|
|
|
3031
|
|
|
|
3032
|
|
|
// return |
3033
|
|
|
return false; |
3034
|
|
|
|
3035
|
|
|
} |
3036
|
|
|
|
3037
|
|
|
|
3038
|
|
|
/* |
3039
|
|
|
* acf_get_filesize |
3040
|
|
|
* |
3041
|
|
|
* This function will return a numeric value of bytes for a given filesize string |
3042
|
|
|
* |
3043
|
|
|
* @type function |
3044
|
|
|
* @date 18/02/2015 |
3045
|
|
|
* @since 5.1.5 |
3046
|
|
|
* |
3047
|
|
|
* @param $size (mixed) |
3048
|
|
|
* @return (int) |
3049
|
|
|
*/ |
3050
|
|
|
|
3051
|
|
|
function acf_get_filesize( $size = 1 ) { |
3052
|
|
|
|
3053
|
|
|
// vars |
3054
|
|
|
$unit = 'MB'; |
3055
|
|
|
$units = array( |
3056
|
|
|
'TB' => 4, |
3057
|
|
|
'GB' => 3, |
3058
|
|
|
'MB' => 2, |
3059
|
|
|
'KB' => 1, |
3060
|
|
|
); |
3061
|
|
|
|
3062
|
|
|
|
3063
|
|
|
// look for $unit within the $size parameter (123 KB) |
3064
|
|
|
if( is_string($size) ) { |
3065
|
|
|
|
3066
|
|
|
// vars |
3067
|
|
|
$custom = strtoupper( substr($size, -2) ); |
3068
|
|
|
|
3069
|
|
|
foreach( $units as $k => $v ) { |
3070
|
|
|
|
3071
|
|
|
if( $custom === $k ) { |
3072
|
|
|
|
3073
|
|
|
$unit = $k; |
3074
|
|
|
$size = substr($size, 0, -2); |
3075
|
|
|
|
3076
|
|
|
} |
3077
|
|
|
|
3078
|
|
|
} |
3079
|
|
|
|
3080
|
|
|
} |
3081
|
|
|
|
3082
|
|
|
|
3083
|
|
|
// calc bytes |
3084
|
|
|
$bytes = floatval($size) * pow(1024, $units[$unit]); |
3085
|
|
|
|
3086
|
|
|
|
3087
|
|
|
// return |
3088
|
|
|
return $bytes; |
3089
|
|
|
|
3090
|
|
|
} |
3091
|
|
|
|
3092
|
|
|
|
3093
|
|
|
/* |
3094
|
|
|
* acf_format_filesize |
3095
|
|
|
* |
3096
|
|
|
* This function will return a formatted string containing the filesize and unit |
3097
|
|
|
* |
3098
|
|
|
* @type function |
3099
|
|
|
* @date 18/02/2015 |
3100
|
|
|
* @since 5.1.5 |
3101
|
|
|
* |
3102
|
|
|
* @param $size (mixed) |
3103
|
|
|
* @return (int) |
3104
|
|
|
*/ |
3105
|
|
|
|
3106
|
|
|
function acf_format_filesize( $size = 1 ) { |
3107
|
|
|
|
3108
|
|
|
// convert |
3109
|
|
|
$bytes = acf_get_filesize( $size ); |
3110
|
|
|
|
3111
|
|
|
|
3112
|
|
|
// vars |
3113
|
|
|
$units = array( |
3114
|
|
|
'TB' => 4, |
3115
|
|
|
'GB' => 3, |
3116
|
|
|
'MB' => 2, |
3117
|
|
|
'KB' => 1, |
3118
|
|
|
); |
3119
|
|
|
|
3120
|
|
|
|
3121
|
|
|
// loop through units |
3122
|
|
|
foreach( $units as $k => $v ) { |
3123
|
|
|
|
3124
|
|
|
$result = $bytes / pow(1024, $v); |
3125
|
|
|
|
3126
|
|
|
if( $result >= 1 ) { |
3127
|
|
|
|
3128
|
|
|
return $result . ' ' . $k; |
3129
|
|
|
|
3130
|
|
|
} |
3131
|
|
|
|
3132
|
|
|
} |
3133
|
|
|
|
3134
|
|
|
|
3135
|
|
|
// return |
3136
|
|
|
return $bytes . ' B'; |
3137
|
|
|
|
3138
|
|
|
} |
3139
|
|
|
|
3140
|
|
|
|
3141
|
|
|
/* |
3142
|
|
|
* acf_get_valid_terms |
3143
|
|
|
* |
3144
|
|
|
* This function will replace old terms with new split term ids |
3145
|
|
|
* |
3146
|
|
|
* @type function |
3147
|
|
|
* @date 27/02/2015 |
3148
|
|
|
* @since 5.1.5 |
3149
|
|
|
* |
3150
|
|
|
* @param $terms (int|array) |
3151
|
|
|
* @param $taxonomy (string) |
3152
|
|
|
* @return $terms |
3153
|
|
|
*/ |
|
|
|
|
3154
|
|
|
|
3155
|
|
|
function acf_get_valid_terms( $terms = false, $taxonomy = 'category' ) { |
3156
|
|
|
|
3157
|
|
|
// bail early if function does not yet exist or |
3158
|
|
|
if( !function_exists('wp_get_split_term') || empty($terms) ) { |
3159
|
|
|
|
3160
|
|
|
return $terms; |
3161
|
|
|
|
3162
|
|
|
} |
3163
|
|
|
|
3164
|
|
|
|
3165
|
|
|
// vars |
3166
|
|
|
$is_array = is_array($terms); |
3167
|
|
|
|
3168
|
|
|
|
3169
|
|
|
// force into array |
3170
|
|
|
$terms = acf_get_array( $terms ); |
3171
|
|
|
|
3172
|
|
|
|
3173
|
|
|
// force ints |
3174
|
|
|
$terms = array_map('intval', $terms); |
3175
|
|
|
|
3176
|
|
|
|
3177
|
|
|
// attempt to find new terms |
3178
|
|
|
foreach( $terms as $i => $term_id ) { |
3179
|
|
|
|
3180
|
|
|
$new_term_id = wp_get_split_term($term_id, $taxonomy); |
3181
|
|
|
|
3182
|
|
|
if( $new_term_id ) { |
3183
|
|
|
|
3184
|
|
|
$terms[ $i ] = $new_term_id; |
3185
|
|
|
|
3186
|
|
|
} |
3187
|
|
|
|
3188
|
|
|
} |
3189
|
|
|
|
3190
|
|
|
|
3191
|
|
|
// revert array if needed |
3192
|
|
|
if( !$is_array ) { |
3193
|
|
|
|
3194
|
|
|
$terms = $terms[0]; |
3195
|
|
|
|
3196
|
|
|
} |
3197
|
|
|
|
3198
|
|
|
|
3199
|
|
|
// return |
3200
|
|
|
return $terms; |
3201
|
|
|
|
3202
|
|
|
} |
3203
|
|
|
|
3204
|
|
|
|
3205
|
|
|
/* |
3206
|
|
|
* acf_esc_html_deep |
3207
|
|
|
* |
3208
|
|
|
* Navigates through an array and escapes html from the values. |
3209
|
|
|
* |
3210
|
|
|
* @type function |
3211
|
|
|
* @date 10/06/2015 |
3212
|
|
|
* @since 5.2.7 |
3213
|
|
|
* |
3214
|
|
|
* @param $value (mixed) |
3215
|
|
|
* @return $value |
3216
|
|
|
*/ |
3217
|
|
|
|
3218
|
|
|
/* |
3219
|
|
|
function acf_esc_html_deep( $value ) { |
3220
|
|
|
|
3221
|
|
|
// array |
3222
|
|
|
if( is_array($value) ) { |
3223
|
|
|
|
3224
|
|
|
$value = array_map('acf_esc_html_deep', $value); |
3225
|
|
|
|
3226
|
|
|
// object |
3227
|
|
|
} elseif( is_object($value) ) { |
3228
|
|
|
|
3229
|
|
|
$vars = get_object_vars( $value ); |
3230
|
|
|
|
3231
|
|
|
foreach( $vars as $k => $v ) { |
3232
|
|
|
|
3233
|
|
|
$value->{$k} = acf_esc_html_deep( $v ); |
3234
|
|
|
|
3235
|
|
|
} |
3236
|
|
|
|
3237
|
|
|
// string |
3238
|
|
|
} elseif( is_string($value) ) { |
3239
|
|
|
|
3240
|
|
|
$value = esc_html($value); |
3241
|
|
|
|
3242
|
|
|
} |
3243
|
|
|
|
3244
|
|
|
|
3245
|
|
|
// return |
3246
|
|
|
return $value; |
3247
|
|
|
|
3248
|
|
|
} |
3249
|
|
|
*/ |
3250
|
|
|
|
3251
|
|
|
|
3252
|
|
|
/* |
3253
|
|
|
* acf_validate_attachment |
3254
|
|
|
* |
3255
|
|
|
* This function will validate an attachment based on a field's resrictions and return an array of errors |
3256
|
|
|
* |
3257
|
|
|
* @type function |
3258
|
|
|
* @date 3/07/2015 |
3259
|
|
|
* @since 5.2.3 |
3260
|
|
|
* |
3261
|
|
|
* @param $attachment (array) attachment data. Cahnges based on context |
3262
|
|
|
* @param $field (array) field settings containing restrictions |
3263
|
|
|
* @param $context (string) $file is different when uploading / preparing |
3264
|
|
|
* @return $errors (array) |
3265
|
|
|
*/ |
|
|
|
|
3266
|
|
|
|
3267
|
|
|
function acf_validate_attachment( $attachment, $field, $context = 'prepare' ) { |
3268
|
|
|
|
3269
|
|
|
// vars |
3270
|
|
|
$errors = array(); |
3271
|
|
|
$file = array( |
3272
|
|
|
'type' => '', |
3273
|
|
|
'width' => 0, |
3274
|
|
|
'height' => 0, |
3275
|
|
|
'size' => 0 |
3276
|
|
|
); |
3277
|
|
|
|
3278
|
|
|
|
3279
|
|
|
// upload |
3280
|
|
|
if( $context == 'upload' ) { |
3281
|
|
|
|
3282
|
|
|
// vars |
3283
|
|
|
$file['type'] = pathinfo($attachment['name'], PATHINFO_EXTENSION); |
3284
|
|
|
$file['size'] = filesize($attachment['tmp_name']); |
3285
|
|
|
|
3286
|
|
|
if( strpos($attachment['type'], 'image') !== false ) { |
3287
|
|
|
|
3288
|
|
|
$size = getimagesize($attachment['tmp_name']); |
3289
|
|
|
$file['width'] = acf_maybe_get($size, 0); |
3290
|
|
|
$file['height'] = acf_maybe_get($size, 1); |
3291
|
|
|
|
3292
|
|
|
} |
3293
|
|
|
|
3294
|
|
|
// prepare |
3295
|
|
|
} elseif( $context == 'prepare' ) { |
3296
|
|
|
|
3297
|
|
|
$file['type'] = pathinfo($attachment['url'], PATHINFO_EXTENSION); |
3298
|
|
|
$file['size'] = acf_maybe_get($attachment, 'filesizeInBytes', 0); |
3299
|
|
|
$file['width'] = acf_maybe_get($attachment, 'width', 0); |
3300
|
|
|
$file['height'] = acf_maybe_get($attachment, 'height', 0); |
3301
|
|
|
|
3302
|
|
|
// custom |
3303
|
|
|
} else { |
3304
|
|
|
|
3305
|
|
|
$file = wp_parse_args($file, $attachment); |
3306
|
|
|
|
3307
|
|
|
} |
3308
|
|
|
|
3309
|
|
|
|
3310
|
|
|
// image |
3311
|
|
|
if( $file['width'] || $file['height'] ) { |
3312
|
|
|
|
3313
|
|
|
// width |
3314
|
|
|
$min_width = (int) acf_maybe_get($field, 'min_width', 0); |
3315
|
|
|
$max_width = (int) acf_maybe_get($field, 'max_width', 0); |
3316
|
|
|
|
3317
|
|
View Code Duplication |
if( $file['width'] ) { |
|
|
|
|
3318
|
|
|
|
3319
|
|
|
if( $min_width && $file['width'] < $min_width ) { |
3320
|
|
|
|
3321
|
|
|
// min width |
3322
|
|
|
$errors['min_width'] = sprintf(__('Image width must be at least %dpx.', 'acf'), $min_width ); |
3323
|
|
|
|
3324
|
|
|
} elseif( $max_width && $file['width'] > $max_width ) { |
3325
|
|
|
|
3326
|
|
|
// min width |
3327
|
|
|
$errors['max_width'] = sprintf(__('Image width must not exceed %dpx.', 'acf'), $max_width ); |
3328
|
|
|
|
3329
|
|
|
} |
3330
|
|
|
|
3331
|
|
|
} |
3332
|
|
|
|
3333
|
|
|
|
3334
|
|
|
// height |
3335
|
|
|
$min_height = (int) acf_maybe_get($field, 'min_height', 0); |
3336
|
|
|
$max_height = (int) acf_maybe_get($field, 'max_height', 0); |
3337
|
|
|
|
3338
|
|
View Code Duplication |
if( $file['height'] ) { |
|
|
|
|
3339
|
|
|
|
3340
|
|
|
if( $min_height && $file['height'] < $min_height ) { |
3341
|
|
|
|
3342
|
|
|
// min height |
3343
|
|
|
$errors['min_height'] = sprintf(__('Image height must be at least %dpx.', 'acf'), $min_height ); |
3344
|
|
|
|
3345
|
|
|
} elseif( $max_height && $file['height'] > $max_height ) { |
3346
|
|
|
|
3347
|
|
|
// min height |
3348
|
|
|
$errors['max_height'] = sprintf(__('Image height must not exceed %dpx.', 'acf'), $max_height ); |
3349
|
|
|
|
3350
|
|
|
} |
3351
|
|
|
|
3352
|
|
|
} |
3353
|
|
|
|
3354
|
|
|
} |
3355
|
|
|
|
3356
|
|
|
|
3357
|
|
|
// file size |
3358
|
|
|
if( $file['size'] ) { |
3359
|
|
|
|
3360
|
|
|
$min_size = acf_maybe_get($field, 'min_size', 0); |
3361
|
|
|
$max_size = acf_maybe_get($field, 'max_size', 0); |
3362
|
|
|
|
3363
|
|
|
if( $min_size && $file['size'] < acf_get_filesize($min_size) ) { |
3364
|
|
|
|
3365
|
|
|
// min width |
3366
|
|
|
$errors['min_size'] = sprintf(__('File size must be at least %s.', 'acf'), acf_format_filesize($min_size) ); |
3367
|
|
|
|
3368
|
|
|
} elseif( $max_size && $file['size'] > acf_get_filesize($max_size) ) { |
3369
|
|
|
|
3370
|
|
|
// min width |
3371
|
|
|
$errors['max_size'] = sprintf(__('File size must must not exceed %s.', 'acf'), acf_format_filesize($max_size) ); |
3372
|
|
|
|
3373
|
|
|
} |
3374
|
|
|
|
3375
|
|
|
} |
3376
|
|
|
|
3377
|
|
|
|
3378
|
|
|
// file type |
3379
|
|
|
if( $file['type'] ) { |
3380
|
|
|
|
3381
|
|
|
$mime_types = acf_maybe_get($field, 'mime_types', ''); |
3382
|
|
|
|
3383
|
|
|
// lower case |
3384
|
|
|
$file['type'] = strtolower($file['type']); |
3385
|
|
|
$mime_types = strtolower($mime_types); |
3386
|
|
|
|
3387
|
|
|
|
3388
|
|
|
// explode |
3389
|
|
|
$mime_types = str_replace(array(' ', '.'), '', $mime_types); |
3390
|
|
|
$mime_types = explode(',', $mime_types); // split pieces |
3391
|
|
|
$mime_types = array_filter($mime_types); // remove empty pieces |
3392
|
|
|
|
3393
|
|
|
if( !empty($mime_types) && !in_array($file['type'], $mime_types) ) { |
3394
|
|
|
|
3395
|
|
|
// glue together last 2 types |
3396
|
|
|
if( count($mime_types) > 1 ) { |
3397
|
|
|
|
3398
|
|
|
$last1 = array_pop($mime_types); |
3399
|
|
|
$last2 = array_pop($mime_types); |
3400
|
|
|
|
3401
|
|
|
$mime_types[] = $last2 . ' ' . __('or', 'acf') . ' ' . $last1; |
3402
|
|
|
|
3403
|
|
|
} |
3404
|
|
|
|
3405
|
|
|
$errors['mime_types'] = sprintf(__('File type must be %s.', 'acf'), implode(', ', $mime_types) ); |
3406
|
|
|
|
3407
|
|
|
} |
3408
|
|
|
|
3409
|
|
|
} |
3410
|
|
|
|
3411
|
|
|
|
3412
|
|
|
// filter for 3rd party customization |
3413
|
|
|
$errors = apply_filters("acf/validate_attachment", $errors, $file, $attachment, $field); |
3414
|
|
|
$errors = apply_filters("acf/validate_attachment/type={$field['type']}", $errors, $file, $attachment, $field ); |
3415
|
|
|
$errors = apply_filters("acf/validate_attachment/name={$field['name']}", $errors, $file, $attachment, $field ); |
3416
|
|
|
$errors = apply_filters("acf/validate_attachment/key={$field['key']}", $errors, $file, $attachment, $field ); |
3417
|
|
|
|
3418
|
|
|
|
3419
|
|
|
// return |
3420
|
|
|
return $errors; |
3421
|
|
|
|
3422
|
|
|
} |
3423
|
|
|
|
3424
|
|
|
|
3425
|
|
|
/* |
3426
|
|
|
* _acf_settings_uploader |
3427
|
|
|
* |
3428
|
|
|
* Dynamic logic for uploader setting |
3429
|
|
|
* |
3430
|
|
|
* @type function |
3431
|
|
|
* @date 7/05/2015 |
3432
|
|
|
* @since 5.2.3 |
3433
|
|
|
* |
3434
|
|
|
* @param $uploader (string) |
3435
|
|
|
* @return $uploader |
3436
|
|
|
*/ |
|
|
|
|
3437
|
|
|
|
3438
|
|
|
add_filter('acf/settings/uploader', '_acf_settings_uploader'); |
3439
|
|
|
|
3440
|
|
|
function _acf_settings_uploader( $uploader ) { |
3441
|
|
|
|
3442
|
|
|
// if can't upload files |
3443
|
|
|
if( !current_user_can('upload_files') ) { |
3444
|
|
|
|
3445
|
|
|
$uploader = 'basic'; |
3446
|
|
|
|
3447
|
|
|
} |
3448
|
|
|
|
3449
|
|
|
|
3450
|
|
|
// return |
3451
|
|
|
return $uploader; |
3452
|
|
|
} |
3453
|
|
|
|
3454
|
|
|
|
3455
|
|
|
/* |
3456
|
|
|
* acf_translate_keys |
3457
|
|
|
* |
3458
|
|
|
* description |
3459
|
|
|
* |
3460
|
|
|
* @type function |
3461
|
|
|
* @date 7/12/2015 |
3462
|
|
|
* @since 5.3.2 |
3463
|
|
|
* |
3464
|
|
|
* @param $post_id (int) |
3465
|
|
|
* @return $post_id (int) |
3466
|
|
|
*/ |
|
|
|
|
3467
|
|
|
|
3468
|
|
|
function acf_translate_keys( $array, $keys ) { |
3469
|
|
|
|
3470
|
|
|
// bail early if no keys |
3471
|
|
|
if( empty($keys) ) return $array; |
3472
|
|
|
|
3473
|
|
|
|
3474
|
|
|
// translate |
3475
|
|
|
foreach( $keys as $k ) { |
3476
|
|
|
|
3477
|
|
|
// bail ealry if not exists |
3478
|
|
|
if( !isset($array[ $k ]) ) continue; |
3479
|
|
|
|
3480
|
|
|
|
3481
|
|
|
// translate |
3482
|
|
|
$array[ $k ] = acf_translate( $array[ $k ] ); |
3483
|
|
|
|
3484
|
|
|
} |
3485
|
|
|
|
3486
|
|
|
|
3487
|
|
|
// return |
3488
|
|
|
return $array; |
3489
|
|
|
|
3490
|
|
|
} |
3491
|
|
|
|
3492
|
|
|
|
3493
|
|
|
/* |
3494
|
|
|
* acf_translate |
3495
|
|
|
* |
3496
|
|
|
* This function will translate a string using the new 'l10n_textdomain' setting |
3497
|
|
|
* Also works for arrays which is great for fields - select -> choices |
3498
|
|
|
* |
3499
|
|
|
* @type function |
3500
|
|
|
* @date 4/12/2015 |
3501
|
|
|
* @since 5.3.2 |
3502
|
|
|
* |
3503
|
|
|
* @param $string (mixed) string or array containins strings to be translated |
3504
|
|
|
* @return $string |
3505
|
|
|
*/ |
|
|
|
|
3506
|
|
|
|
3507
|
|
|
function acf_translate( $string ) { |
3508
|
|
|
|
3509
|
|
|
// bail early if not enabled |
3510
|
|
|
if( !acf_get_setting('l10n') ) return $string; |
3511
|
|
|
|
3512
|
|
|
|
3513
|
|
|
// bail early if no textdomain |
3514
|
|
|
if( !acf_get_setting('l10n_textdomain') ) return $string; |
3515
|
|
|
|
3516
|
|
|
|
3517
|
|
|
// is array |
3518
|
|
|
if( is_array($string) ) { |
3519
|
|
|
|
3520
|
|
|
return array_map('acf_translate', $string); |
3521
|
|
|
|
3522
|
|
|
} |
3523
|
|
|
|
3524
|
|
|
|
3525
|
|
|
// bail early if not string |
3526
|
|
|
if( !is_string($string) ) return $string; |
3527
|
|
|
|
3528
|
|
|
|
3529
|
|
|
// bail early if empty |
3530
|
|
|
if( $string === '' ) return $string; |
3531
|
|
|
|
3532
|
|
|
|
3533
|
|
|
// allow for var_export export |
3534
|
|
|
if( acf_get_setting('l10n_var_export') ){ |
3535
|
|
|
|
3536
|
|
|
return "!!__(!!'" . $string . "!!', !!'" . acf_get_setting('l10n_textdomain') . "!!')!!"; |
3537
|
|
|
|
3538
|
|
|
} |
3539
|
|
|
|
3540
|
|
|
|
3541
|
|
|
// vars |
3542
|
|
|
return __( $string, acf_get_setting('l10n_textdomain') ); |
3543
|
|
|
|
3544
|
|
|
} |
3545
|
|
|
|
3546
|
|
|
|
3547
|
|
|
/* |
3548
|
|
|
* acf_maybe_add_action |
3549
|
|
|
* |
3550
|
|
|
* This function will determine if the action has already run before adding / calling the function |
3551
|
|
|
* |
3552
|
|
|
* @type function |
3553
|
|
|
* @date 13/01/2016 |
3554
|
|
|
* @since 5.3.2 |
3555
|
|
|
* |
3556
|
|
|
* @param $post_id (int) |
3557
|
|
|
* @return $post_id (int) |
3558
|
|
|
*/ |
|
|
|
|
3559
|
|
|
|
3560
|
|
|
function acf_maybe_add_action( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) { |
3561
|
|
|
|
3562
|
|
|
// if action has already run, execute it |
3563
|
|
|
if( did_action($tag) ) { |
3564
|
|
|
|
3565
|
|
|
call_user_func( $function_to_add ); |
3566
|
|
|
|
3567
|
|
|
// if action has not yet run, add it |
3568
|
|
|
} else { |
3569
|
|
|
|
3570
|
|
|
add_action( $tag, $function_to_add, $priority, $accepted_args ); |
3571
|
|
|
|
3572
|
|
|
} |
3573
|
|
|
|
3574
|
|
|
} |
3575
|
|
|
|
3576
|
|
|
|
3577
|
|
|
/* |
3578
|
|
|
* Hacks |
3579
|
|
|
* |
3580
|
|
|
* description |
3581
|
|
|
* |
3582
|
|
|
* @type function |
3583
|
|
|
* @date 17/01/2014 |
3584
|
|
|
* @since 5.0.0 |
3585
|
|
|
* |
3586
|
|
|
* @param $post_id (int) |
3587
|
|
|
* @return $post_id (int) |
3588
|
|
|
*/ |
|
|
|
|
3589
|
|
|
|
3590
|
|
|
add_filter("acf/settings/slug", '_acf_settings_slug'); |
3591
|
|
|
|
3592
|
|
|
function _acf_settings_slug( $v ) { |
|
|
|
|
3593
|
|
|
|
3594
|
|
|
$basename = acf_get_setting('basename'); |
3595
|
|
|
$slug = explode('/', $basename); |
3596
|
|
|
$slug = current($slug); |
3597
|
|
|
|
3598
|
|
|
return $slug; |
3599
|
|
|
} |
3600
|
|
|
|
3601
|
|
|
?> |
|
|
|
|
3602
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.