forked from ControlledChaos/acf-term-and-taxonomy-chooser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-taxonomy-chooser.php
407 lines (322 loc) · 10.6 KB
/
class-taxonomy-chooser.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<?php
/**
* Core plugin class
*
* @package ACF_Taxonomy_Chooser
*
* @version 1.0.0
* @author Marktime Media, modified by Controlled Chaos Design
*/
namespace ACF_Tax_Chooser;
class Taxonomy_Chooser extends \acf_field {
/**
* Constructor method
*
* @since 1.0.0
* @access public
* @return self
*/
public function __construct() {
/**
* Field name
*
* Single word, no spaces. Underscores allowed
*
* @return string
*/
$this->name = 'taxonomy-chooser';
/**
* Field label
*
* Multiple words, can include spaces, visible when selecting a field type
*
* @return string
*/
$this->label = __( 'Term and Taxonomy Chooser', 'acf-taxonomy-chooser' );
/**
* Field category
*
* basic|content|choice|relational|jquery|layout|CUSTOM GROUP NAME
*
* @return string
*/
$this->category = 'choice';
/**
* Field defaults
*
* Array of default settings which are merged into the field object.
* These are used later in settings.
*
* @return array
*
* Notes: 'multiple' used to be associated with a 'select multiple values field' also
*/
$this->defaults = [
'choices' => [],
'tax_type' => 0,
'allow_null' => 0,
'null_text' => '',
'ui' => 0,
'ajax' => 0,
'type_value' => 1,
'multiple' => 0,
];
/**
* Field l10n
*
* Array of strings that are used in JavaScript.
*
* This allows JS strings to be translated in PHP and loaded via:
* `var message = acf._e( 'taxonomy-chooser', 'error' );`
*
* @return array
*/
$this->l10n = array(
'error' => __( 'Error! Please enter a higher value', 'acf-taxonomy-chooser' ),
);
/**
* Extend constructor
*
* This extends the constructor method of the `acf_field` class.
*/
parent::__construct();
}
/**
* Render Field Settings
*
* @param $field arra) The field being edited.
* @return n/a
*/
function render_field_settings( $field ) {
// Term or taxonomy.
acf_render_field_setting( $field, [
'label' => __( 'Select Type', 'acf-taxonomy-chooser' ),
'instructions' => '',
'type' => 'select',
'name' => 'tax_type',
'choices' => [
1 => __( 'Taxonomy', 'acf-taxonomy-chooser' ),
0 => __( 'Term', 'acf-taxonomy-chooser' )
],
'layout' => 'horizontal',
] );
// Allowed taxonomies.
acf_render_field_setting( $field, [
'label' => __( 'Choose Allowed Taxonomies', 'acf-taxonomy-chooser' ),
'instructions' => '',
'type' => 'select',
'name' => 'choices',
'choices' => acf_get_pretty_taxonomies(),
'multiple' => 1,
'ui' => 1,
'allow_null' => 1,
'placeholder' => __( 'All Taxonomies', 'acf-taxonomy-chooser' )
] );
// Allow null.
acf_render_field_setting( $field, [
'label' => __( 'Allow Null', 'acf-taxonomy-chooser' ),
'instructions' => '',
'name' => 'allow_null',
'type' => 'true_false',
'ui' => 1
] );
// Null text.
acf_render_field_setting( $field, [
'label' => __( 'Null Text', 'acf-taxonomy-chooser' ),
'instructions' => 'Text if no selection is made. Defaults to "Select".',
'name' => 'null_text',
'type' => 'text',
'placeholder' => __( 'Select', 'acf-taxonomy-chooser' )
] );
// Term ID or slug.
acf_render_field_setting( $field, [
'label' => __( 'Return Term Value', 'acf-taxonomy-chooser' ),
'instructions' => __( 'Specify the returned value on front end (taxonomies always return as slug)', 'acf-taxonomy-chooser' ),
'type' => 'radio',
'name' => 'type_value',
'choices' => [
1 => __( 'ID', 'acf-taxonomy-chooser' ),
0 => __( 'Slug', 'acf-taxonomy-chooser' )
],
'layout' => 'horizontal'
] );
}
/**
* Render Field
*
* @param $field array The $field being edited.
* @return n/a
*/
function render_field( $field ) {
$taxonomies = [];
$taxonomies = acf_get_array( $taxonomies );
$taxonomies = acf_get_pretty_taxonomies( $taxonomies );
$taxonomy_terms = acf_get_taxonomy_terms();
$selected_taxonomies = [];
$terms = [];
if ( ! empty( $field['choices'] ) ) {
$slug_name = $field['choices'];
} else {
$slug_name = array_keys( acf_get_pretty_taxonomies() );
}
// Select terms.
if ( 'Term' == $field['tax_type'] ) {
foreach ( $slug_name as $k1 => $v1 ) {
$found_terms = get_terms( $v1, [ 'hide_empty' => false ] );
if ( ! empty( $found_terms ) && is_array( $found_terms ) ) {
$terms = array_merge( $terms, $found_terms );
}
foreach( $taxonomies as $k2 => $v2 ) {
if ( $v1 == $k2 ) {
$slug_name[$k1] = $v2;
}
}
}
foreach ( $slug_name as $k1 => $v1 ) {
foreach ( $taxonomy_terms as $k2 => $v2 ) {
if ( $v1 == $k2 ) {
$selected_taxonomies[$v1] = $taxonomy_terms[$k2];
}
}
}
// Select taxonomies.
} else {
$taxonomies = [];
// Only use allowed taxonomies.
foreach ( $slug_name as $tax_name ) {
$taxonomies[ $tax_name ] = get_taxonomy( $tax_name );
}
foreach( $taxonomies as $taxonomy ) {
$selected_taxonomies[ $taxonomy->name ] = $taxonomy->label;
}
}
$slug_name = $selected_taxonomies;
// Add empty value (allows '' to be selected).
if ( empty( $field['value'] ) ) {
$field['value'] = '';
}
// Select field placeholder.
if ( empty( $field['placeholder'] ) ) {
// Use the null text if set.
if ( $field['null_text'] ) {
$field['placeholder'] = $field['null_text'];
// Default placeholder if no null text is set.
} else {
$field['placeholder'] = __( 'Select', 'acf-taxonomy-chooser' );
}
}
// Select field attributes.
$atts = [
'id' => $field['id'],
'class' => $field['class'] . ' js-multi-taxonomy-select2',
'name' => $field['name'],
'data-ui' => $field['ui'],
'data-ajax' => $field['ajax'],
'data-placeholder' => $field['placeholder'],
'data-allow_null' => $field['allow_null']
];
// Hidden input.
if ( $field['ui'] ) {
acf_hidden_input( [
'type' => 'hidden',
'id' => $field['id'],
'name' => $field['name'],
'value' => implode( ',', $field['value'])
] );
} elseif ( $field['multiple'] ) {
acf_hidden_input( [
'type' => 'hidden',
'name' => $field['name'],
] );
}
// User interface;
if ( $field['ui'] ) {
$atts['disabled'] = 'disabled';
$atts['class'] .= ' acf-hidden';
}
// Special attributes.
foreach ( [ 'readonly', 'disabled' ] as $k ) {
if ( ! empty( $field[ $k ] ) ) {
$atts[ $k ] = $k;
}
}
// Variables.
$els = [];
$choices = [];
// Loop through values and add them as options.
if ( ! empty( $slug_name ) ) {
// Allowed taxonomies.
foreach ( $slug_name as $k => $v ) {
if ( is_array( $v ) ) {
// The optgroup elements.
$els[] = [ 'type' => 'optgroup', 'label' => $k ];
if ( ! empty( $v ) ) {
foreach ( $v as $k2 => $v2 ) {
// Child categories have hyphens before the name, we need to remove them in order to match them.
$strip_v2_hyphen = preg_replace( '#-\s?#', '', $v2 );
// Value = term ID.
if ( $field['type_value'] ) {
foreach ( $terms as $key => $val ) {
if ( $val->name == $strip_v2_hyphen ) {
$els[] = [ 'type' => 'option', 'value' => $val->term_id, 'label' => $v2 , 'selected' => $slct = ( $val->term_id == $field['value'] ? "selected": "" ) ];
}
}
// Value = term slug.
} else {
// Originally returns 'taxonomy:term-slug' this removes 'taxonomy:'.
preg_match( '#(?::)(.*)#', $k2, $value );
$els[] = [ 'type' => 'option', 'value' => $value[1], 'label' => $v2, 'selected' => $slct = ( $value[1] == $field['value'] ? "selected": "" ) ];
}
$choices[] = $k2;
}
}
$els[] = [ 'type' => '/optgroup' ];
// Value = Taxonomy Slug.
} else {
$els[] = [ 'type' => 'option', 'value' => $k, 'label' => $v, 'selected' => $slct = ( $k == $field['value'] ? "selected": "" ) ];
$choices[] = $k;
}
}
}
// Null: the "Select" option.
if ( $field['allow_null'] ) {
array_unshift( $els, [ 'type' => 'option', 'value' => '', 'label' => $field['placeholder'] ] );
}
// Select element markup.
echo '<select ' . acf_esc_attr( $atts ) . '>';
// Construct HTML.
if ( ! empty( $els ) ) {
foreach ( $els as $el ) {
// Extract type.
$type = acf_extract_var( $el, 'type' );
if ( $type == 'option' ) {
// Get label.
$label = acf_extract_var( $el, 'label' );
// Validate selected.
if ( acf_extract_var( $el, 'selected' ) ) {
$el['selected'] = 'selected';
}
echo '<option ' . acf_esc_attr( $el ) . '>' . $label . '</option>';
} else {
echo '<' . $type . ' ' . acf_esc_attr( $el ) . '>';
}
}
}
echo '</select>';
}
/**
* Enqueue scripts
*
* This action is called in the admin_enqueue_scripts action on
* the edit screen where your field is created.
*
* Use this action to add CSS + JavaScript to assist your render_field() action.
*/
function input_admin_enqueue_scripts() {
$dir = plugin_dir_url( __FILE__ );
wp_register_script( 'acf-input-taxonomy-chooser', "{$dir}js/input.min.js" );
wp_enqueue_script( 'acf-input-taxonomy-chooser' );
}
}
// Run the class.
$acf_tax_chooser = new Taxonomy_Chooser();