acf-fieldoptions.php
3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
class CACIE_ACF_FieldOptions {
/**
* Field options caught from ACF ([option] => [label])
*
* @var array
* @since 1.0
*/
protected $field_options = NULL;
/**
* Get field options for an ACF field
*
* @since 1.0
*
* @param array $field ACF field array
* @return array {
* List of field options. [option] => [label] or,
* in case of options group:
*
* @type array {
* @type string $label Option label.
* @type array $options Options array ([option] => [label]).
* }
* }
*/
public function get_field_options( $field ) {
if ( ! in_array( $field['type'], array( 'checkbox', 'page_link', 'post_object', 'radio', 'relationship', 'select', 'taxonomy' ) ) ) {
return false;
}
// Prefill field value as it's required for some types
if ( ! isset( $field['value'] ) ) {
$field['value'] = '';
}
// Finalized array of options
$options = array();
if ( $field['type'] == 'taxonomy' ) {
// Options for taxonomy
$args = array(
'taxonomy' => $field['taxonomy'],
'hide_empty' => false
);
$args = apply_filters( 'acf/fields/taxonomy/wp_list_categories', $args, $field );
$terms = get_categories( $args );
foreach ( $terms as $term ) {
$options[ $term->term_id ] = $term->name;
}
}
else {
// Fetch field options from ACF
$options_raw = $this->get_field_options_raw( $field );
if ( $options_raw === false ) {
// Possibly not a dropdown field, try to fetch choices directly
// Select and checkbox fields can have predefined choices
if ( ! empty( $field['choices'] ) ) {
$options = $field['choices'];
}
}
else {
foreach ( $options_raw as $index => $option ) {
if ( is_array( $option ) ) {
// Option group
$options[] = array(
'label' => $index,
'options' => $option
);
}
else {
// Single option
$options[ $index ] = $option;
}
}
}
}
return $options;
}
/**
* Get raw field options from ACF
*
* @since 1.0
*
* @param array $field ACF field array
* @return array List of raw field options
*/
public function get_field_options_raw( $field ) {
if ( empty( $field['value'] ) ) {
$field['value'] = '';
}
$this->field_options = NULL;
$acf_mv = function_exists( 'acf_get_setting' ) ? '5' : '4';
if ( $acf_mv == '5' ) {
add_action( 'acf/render_field', array( $this, 'catch_field_options' ) );
ob_start();
acf_render_field( $field );
ob_end_clean();
remove_action( 'acf/render_field', array( $this, 'catch_field_options' ) );
}
else {
add_action( 'acf/create_field', array( $this, 'catch_field_options' ) );
ob_start();
do_action( 'acf/create_field', $field );
ob_end_clean();
remove_action( 'acf/create_field', array( $this, 'catch_field_options' ) );
}
if ( is_array( $this->field_options ) ) {
return $this->field_options;
}
return false;
}
/**
* Catch field options from an ACF field.
* Should be called as part of the field choices retrieval process on the acf/render_field
* or acf/create_field filters.
*
* @since 1.0
*
* @param array $field ACF field array
*/
public function catch_field_options( $field ) {
if ( isset( $field['choices'] ) ) {
$this->field_options = $field['choices'];
}
}
}