This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
class-osadi-acf-field-term.php
219 lines (191 loc) · 5.78 KB
/
class-osadi-acf-field-term.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
* Osadi Term Field
*
* Registers a new field type with ACF. This field makes it possible to set up a relation
* to one or more taxonomy terms on the edit screen.
*
*/
class Osadi_ACF_Field_Term extends acf_field
{
// Visible when selecting a field type.
public $label = 'Term';
// Internal name.
public $name = 'osadi_acf_term';
// List this field under category 'relational' in the select for field type.
public $category = 'relational';
/*
* Defaults for all registered fields.
* The key is the fields 'name'
*/
public $defaults = array (
'allow_null' => 1,
'multiple' => 0,
'hide_empty' => 1,
'return_format' => 'taxonomy_id',
);
public function __construct()
{
parent::__construct();
}
/**
* Render the fields on the Edit Field Group screen.
*
* @param array $field
*/
public function render_field_settings( $field )
{
// Allow null.
acf_render_field_setting( $field, array(
'label' => __('Allow Null?','osadi-acf-term'),
'instructions' => '',
'name' => 'allow_null',
'type' => 'true_false',
'ui' => 1,
));
// Allow multiple values.
acf_render_field_setting( $field, array(
'label' => __('Select multiple values?','osadi-acf-term'),
'instructions' => '',
'name' => 'multiple',
'type' => 'true_false',
'ui' => 1,
));
// Hide empty.
acf_render_field_setting( $field, array(
'label' => __( 'Hide empty?', 'osadi-acf-term' ),
'instructions' => '',
'type' => 'radio',
'name' => 'hide_empty',
'choices' => array(
1 => __( 'Yes', 'osadi-acf-term' ),
0 => __( 'No', 'osadi-acf-term' ),
),
'layout' => 'horizontal',
));
// Setup choices for our return formats
$return_format_choices = array(
'id' => __( 'Term ID', 'osadi-acf-term' ),
'taxonomy_id' => __( 'Taxonomy name & Term ID', 'osadi-acf-term' ),
'object' => __( 'Term Object', 'osadi-acf-term' ),
);
if ( class_exists( 'Timber' ) ) {
$return_format_choices['timber_object'] = __( 'Timber Term', 'osadi-acf-term' );
}
// Return format of term.
acf_render_field_setting( $field, array(
'label' => __( 'Return format', 'osadi-acf-term' ),
'instructions' => '',
'type' => 'radio',
'name' => 'return_format',
'choices' => $return_format_choices,
'layout' => 'horizontal',
));
}
/**
* Render the field on the edit page/post/tax etc. screen.
*
* @param array $field Field data loaded from db.
*/
public function render_field( $field )
{
// Change field type from 'osadi_acf_term' to 'select'.
$field['type'] = 'select';
$field['ui'] = 1;
$field['ajax'] = 0;
$field['choices'] = $this->get_taxonomies_terms_assoc( $field['hide_empty'] );
acf_render_field( $field );
}
/**
* Get all available taxonomies and associate them with all available terms.
*
* Borrowed heavily from the ACF api-helpers. @see acf_get_taxonomy_terms().
*
* @return array $taxonomies_terms
* An array with the translated taxonomy name as key and the value as an array of terms.
* Key for term is 'term_id:taxonomy' and value is term name.
*/
private function get_taxonomies_terms_assoc( $hide_empty )
{
$taxonomies = acf_get_pretty_taxonomies();
$taxonomies_terms = array();
foreach ( array_keys( $taxonomies ) as $taxonomy ) {
$label = $taxonomies[ $taxonomy ];
$terms = get_terms( $taxonomy, array( 'hide_empty' => $hide_empty ) );
if ( ! empty( $terms ) ) {
$taxonomies_terms[ $label ] = array();
foreach ( $terms as $term ) {
$key = "{$term->term_id}:{$taxonomy}";
$taxonomies_terms[ $label ][ $key ] = $term->name;
}
}
}
return $taxonomies_terms;
}
/**
* Enqueue our javascript.
*
* This action seems to be called in:
* @see acf_input_listener::__construct()
* @see acf_admin_field_group::admin_enqueue_scripts()
*
*/
public function input_admin_enqueue_scripts()
{
$dir = plugin_dir_url( __FILE__ );
$handle = str_replace( '_', '-', $this->name );
$dependencies = array( 'acf-input' );
wp_register_script( $handle, "{$dir}js/{$handle}.js", $dependencies, $version = false, $in_footer = true );
wp_localize_script( $handle, $this->name, array( 'field_name' => $this->name ) );
wp_enqueue_script( $handle );
}
/**
* This filter is applied to the $value after it is loaded from the db and before it is returned to the template.
*
* If 'return_format' is 'object' we change the value to contain the term objects instead of IDs.
*
* @type filter
*
* @param mixed $value The value which was loaded from the database.
* @param mixed $post_id The $post_id from which the value was loaded.
* @param array $field All the field options loaded from db.
* @return array $terms The modified value.
*/
public function format_value( $value, $post_id, $field )
{
if ( empty( $value ) ) {
return $value;
}
$terms = array();
// Wrap our string in an array.
if ( is_string( $value ) ) {
$value = array( $value );
}
foreach ( $value as $id_taxonomy ) {
list( $term_id, $taxonomy ) = explode( ':', $id_taxonomy );
switch ( $field['return_format'] ) {
case 'object':
$term = get_term( intval( $term_id ), $taxonomy );
if ( ! is_wp_error( $term ) && is_object( $term ) ) {
$terms[] = $term;
}
break;
case 'timber_object':
$term = Timber::get_terms( $taxonomy, array(
'include' => array( intval( $term_id ) )
));
if ( ! is_wp_error( $term ) && is_array( $term ) ) {
$terms[] = $term[0];
}
break;
case 'taxonomy_id':
$terms[$taxonomy][] = intval( $term_id );
break;
default:
$terms[] = intval( $term_id );
}
}
return $terms;
}
}
new Osadi_ACF_Field_Term();