-
Notifications
You must be signed in to change notification settings - Fork 97
/
term.php
221 lines (186 loc) · 8.02 KB
/
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
220
221
<?php
class cfs_term extends cfs_field
{
function __construct() {
$this->name = 'term';
$this->label = __( 'Term', 'cfs' );
}
function html( $field ) {
global $wpdb;
$selected_posts = [];
$available_posts = [];
$taxonomies = [];
if ( ! empty( $field->options['taxonomies'] ) ) {
foreach ( $field->options['taxonomies'] as $taxonomy ) {
$taxonomies[] = $taxonomy;
}
}
else {
$taxonomies = get_taxonomies( [ 'public' => true ] );
}
$args = [
'taxonomy' => $taxonomies,
'hide_empty' => false,
'fields' => 'ids',
'orderby' => 'name',
'order' => 'ASC'
];
$args = apply_filters( 'cfs_field_term_query_args', $args, [ 'field' => $field ] );
// Use older `get_terms` function signature for older versions of WP
if ( version_compare( get_bloginfo('version'), '4.5', '<' ) ) {
$taxonomy = $args['taxonomy'];
unset( $args['taxonomy'] );
$query = get_terms( $taxonomy, $args );
} else {
$query = get_terms( $args );
}
foreach ( $query as $term_id ) {
$term = get_term( $term_id );
$available_posts[] = (object) [
'term_id' => $term->term_id,
'taxonomy' => $term->taxonomy,
'name' => $term->name,
];
}
if ( ! empty( $field->value ) ) {
$results = $wpdb->get_results( "SELECT term_id, name FROM $wpdb->terms WHERE term_id IN ($field->value) ORDER BY FIELD(term_id,$field->value)" );
foreach ( $results as $result ) {
$selected_posts[ $result->term_id ] = $result;
}
}
?>
<div class="filter_posts">
<input type="text" class="cfs_filter_input" autocomplete="off" placeholder="<?php _e( 'Search terms', 'cfs' ); ?>" />
</div>
<div class="available_posts post_list">
<?php foreach ( $available_posts as $term ) : ?>
<?php $class = ( isset( $selected_posts[ $term->term_id ] ) ) ? ' class="used"' : ''; ?>
<div rel="<?php echo $term->term_id; ?>"<?php echo $class; ?> title="<?php echo $term->name; ?>"><?php echo apply_filters( 'cfs_term_display', $term->name, $term->term_id, $field ); ?></div>
<?php endforeach; ?>
</div>
<div class="selected_posts post_list">
<?php foreach ( $selected_posts as $term ) : ?>
<div rel="<?php echo $term->term_id; ?>"><span class="remove"></span><?php echo apply_filters( 'cfs_term_display', $term->name, $term->term_id, $field ); ?></div>
<?php endforeach; ?>
</div>
<div class="clear"></div>
<input type="hidden" name="<?php echo $field->input_name; ?>" class="<?php echo $field->input_class; ?>" value="<?php echo $field->value; ?>" />
<?php
}
function options_html( $key, $field ) {
$args = [ 'public' => true ];
$choices = apply_filters( 'cfs_field_term_taxonomies', get_taxonomies( $args ) );
?>
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php _e('Taxonomies', 'cfs'); ?></label>
<p class="description"><?php _e('Limit terms to the following taxonomies', 'cfs'); ?></p>
</td>
<td>
<?php
CFS()->create_field( [
'type' => 'select',
'input_name' => "cfs[fields][$key][options][taxonomies]",
'options' => [ 'multiple' => '1', 'choices' => $choices ],
'value' => $this->get_option( $field, 'taxonomies' ),
] );
?>
</td>
</tr>
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php _e( 'Limits', 'cfs' ); ?></label>
</td>
<td>
<input type="text" name="cfs[fields][<?php echo $key; ?>][options][limit_min]" value="<?php echo $this->get_option( $field, 'limit_min' ); ?>" placeholder="min" style="width:60px" />
<input type="text" name="cfs[fields][<?php echo $key; ?>][options][limit_max]" value="<?php echo $this->get_option( $field, 'limit_max' ); ?>" placeholder="max" style="width:60px" />
</td>
</tr>
<?php
}
function input_head( $field = null ) {
?>
<script>
(function($) {
update_term_values = function(field) {
var term_ids = [];
field.find('.selected_posts div').each(function(idx) {
term_ids[idx] = $(this).attr('rel');
});
field.find('input.term').val(term_ids.join(','));
}
$(function() {
$(document).on('cfs/ready', '.cfs_add_field', function() {
$('.cfs_term:not(.ready)').init_term();
});
$('.cfs_term').init_term();
// add selected post
$(document).on('click', '.cfs_term .available_posts div', function() {
var parent = $(this).closest('.field');
var term_id = $(this).attr('rel');
var html = $(this).html();
$(this).addClass('used');
parent.find('.selected_posts').append('<div rel="'+term_id+'"><span class="remove"></span>'+html+'</div>');
update_term_values(parent);
});
// remove selected post
$(document).on('click', '.cfs_term .selected_posts .remove', function() {
var div = $(this).parent();
var parent = div.closest('.field');
var term_id = div.attr('rel');
parent.find('.available_posts div[rel='+term_id+']').removeClass('used');
div.remove();
update_term_values(parent);
});
// filter posts
$(document).on('keyup', '.cfs_term .cfs_filter_input', function() {
var input = $(this).val();
var parent = $(this).closest('.field');
var regex = new RegExp(input, 'i');
parent.find('.available_posts div:not(.used)').each(function() {
if (-1 < $(this).html().search(regex)) {
$(this).removeClass('hidden');
}
else {
$(this).addClass('hidden');
}
});
});
});
$.fn.init_term = function() {
this.each(function() {
var $this = $(this);
$this.addClass('ready');
// sortable
$this.find('.selected_posts').sortable({
axis: 'y',
update: function(event, ui) {
var parent = $(this).closest('.field');
update_term_values(parent);
}
});
});
}
})(jQuery);
</script>
<?php
}
function prepare_value( $value, $field = null ) {
return $value;
}
function format_value_for_input( $value, $field = null ) {
return empty( $value ) ? '' : implode( ',', $value );
}
function pre_save( $value, $field = null ) {
if ( ! empty( $value ) ) {
// Inside a loop, the value is $value[0]
$value = (array) $value;
// The raw input saves a comma-separated string
if ( false !== strpos( $value[0], ',' ) ) {
return explode( ',', $value[0] );
}
return $value;
}
return [];
}
}