-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaxonomy-filter.php
More file actions
294 lines (255 loc) · 10.4 KB
/
Copy pathtaxonomy-filter.php
File metadata and controls
294 lines (255 loc) · 10.4 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
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
<?php
/*
Plugin Name: Taxonomy Filter
Description: Adds an input field to filter taxonomy values on admin post pages and, for some user, hides several taxonomy terms according to admin settings
Author: Andrea Landonio
Author URI: http://www.andrealandonio.it
Text Domain: taxonomy_filter
Domain Path: /languages/
Version: 2.2.10-yoko
License: GPL v3
Taxonomy Filter
Copyright (C) 2013-2020, Andrea Landonio - landonio.andrea@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Security check
if ( ! defined( 'ABSPATH' ) ) die( 'Accesso diretto al file non permesso' );
// Path missing "__DIR__" constant on environment
if ( ! defined( '__DIR__' ) ) {
define( '__DIR__', dirname( __FILE__ ) );
}
/***************************************************
INCLUDES
**/
require_once( 'taxonomy-filter-constants.php' );
require_once( 'taxonomy-filter-settings.php' );
require_once( 'taxonomy-filter-users.php' );
require_once( 'taxonomy-filter-bulks.php' );
require_once( 'taxonomy-filter-terms.php' );
/***************************************************
PLUGIN ACTIVATION
**/
/**
* Register activation hook
*/
function taxonomy_filter_activation() {
// Update default settings
$options = taxonomy_filter_category_default_options();
update_option( TFP_PREFIX, $options );
add_option( TFP_PREFIX, $options );
}
register_activation_hook( __FILE__, 'taxonomy_filter_activation' );
/**
* Retrieve default category options (for standard settings)
*
* @return stdClass
*/
function taxonomy_filter_category_default_options() {
// Retrieve hierarchical taxonomies
$args = array( 'hierarchical' => true );
$taxonomies = get_taxonomies( $args, 'objects' );
$options = new stdClass();
// Loop taxonomies
foreach ( $taxonomies as $taxonomy ) {
// Set data (from current taxonomy object)
$tax = $taxonomy->name;
$replace = TFP_DEFAULT_REPLACE;
$hide_blank = TFP_DEFAULT_HIDE_BLANK;
$option = new stdClass();
// Save taxonomy slug
$option->slug = $tax;
// Save replace value (1 = replace, 0 = WordPress default)
$option->replace = $replace;
// Save hide blank value (1 = hide, 0 = show)
$option->hide_blank = $hide_blank;
// Add current taxonomy to options class
$options->$tax = $option;
}
return $options;
}
/***************************************************
PLUGIN DEACTIVATION
**/
/**
* Register deactivation hook
*/
function taxonomy_filter_deactivation() {
delete_option( TFP_PREFIX );
}
register_deactivation_hook( __FILE__, 'taxonomy_filter_deactivation' );
/***************************************************
PLUGIN INIT
**/
/**
* Init
*/
function taxonomy_filter_init() {
// Read options
$options = get_option( TFP_OPTIONS );
// Loop over taxonomy_filter option items
if ( ! empty( $options ) ) {
foreach ( $options as $taxonomy ) {
// If current taxonomy is enabled add actions
if ( ! empty( $taxonomy ) && ! empty( $taxonomy->replace ) && $taxonomy->replace == 1 ) {
add_action( $taxonomy->slug . '_add_form_fields', 'taxonomy_filter_terms_add_form_fields' );
add_action( $taxonomy->slug . '_edit_form_fields', 'taxonomy_filter_terms_edit_form_fields' );
add_action( 'edit_' . $taxonomy->slug, 'taxonomy_filter_terms_save_form_fields' );
add_action( 'create_' . $taxonomy->slug, 'taxonomy_filter_terms_save_form_fields' );
add_filter( 'manage_edit-' . $taxonomy->slug . '_columns', 'taxonomy_filter_terms_edit_columns' );
add_filter( 'manage_' . $taxonomy->slug . '_custom_column', 'taxonomy_filter_terms_manage_columns', 10, 3 );
}
}
}
}
add_action( 'init', 'taxonomy_filter_init' );
/***************************************************
PLUGIN ACTIONS
**/
/**
* Add menu settings
*/
function taxonomy_filter_setting_menu() {
// Register stylesheet
wp_register_style( 'taxonomy_filter_style', plugins_url( 'taxonomy-filter/css/tfp.css' ) );
wp_enqueue_style( 'taxonomy_filter_style' );
// Add option page
add_options_page( 'Taxonomy Filter', 'Taxonomy Filter', 'manage_options', TFP_PREFIX, 'taxonomy_filter_settings' );
}
add_action( 'admin_menu', 'taxonomy_filter_setting_menu' );
/**
* Add taxonomy filter boxes to postbox
*/
function taxonomy_filter_add_boxes() {
global $pagenow;
if ( 'post.php' == $pagenow || 'post-new.php' == $pagenow ) {
$show_filter = true;
// Get hidden taxonomies
$options = get_option( TFP_OPTIONS );
if ( isset( $options->hidden ) && ! empty( $options->hidden ) ) {
$hidden_taxonomies = $options->hidden;
}
else {
unset( $options->hidden );
}
if ( empty( $hidden_taxonomies ) ) $hidden_taxonomies = array();
// Get current user hidden taxonomies
$user = wp_get_current_user();
$user_hidden_taxonomies = get_user_meta( $user->ID, TFP_META_HIDDEN_TAXONOMIES, true );
if ( empty( $user_hidden_taxonomies ) ) $user_hidden_taxonomies = array();
?>
<script type="text/javascript">
jQuery(document).ready(function() {
<?php
// Loop over taxonomy_filter option items
if ( ! empty( $options ) ) {
foreach ( $options as $taxonomy ) {
// If current taxonomy is enabled for replace add filter box
if ( isset( $taxonomy->replace ) && ! empty( $taxonomy->replace ) && $taxonomy->replace == 1 ) {
if ( isset( $taxonomy->hide_blank ) && ! empty( $taxonomy->hide_blank ) && $taxonomy->hide_blank == 1 ) {
$terms = get_terms( $taxonomy->slug );
$show_filter = !empty( $terms );
}
// Show filter box
if ( $show_filter ) {
?>
// Apply hidden taxonomies filter
jQuery('<?php echo "#taxonomy-" . $taxonomy->slug; ?>').find('<?php echo "#" . $taxonomy->slug . "checklist" ?>').find('li').each(function () {
var hidden_taxonomies_array = [];
var user_hidden_taxonomies_array = [];
var taxonomy_id = jQuery(this).attr('id');
var normalized_taxonomy_id = taxonomy_id.replace('<?php echo $taxonomy->slug . "-" ?>', '');
hidden_taxonomies_array = <?php echo json_encode($hidden_taxonomies) ?>;
user_hidden_taxonomies_array = <?php echo json_encode($user_hidden_taxonomies) ?>;
// Remove taxonomy checking hidden values
if (hidden_taxonomies_array[normalized_taxonomy_id] === 1) {
jQuery(this).remove();
}
// Remove taxonomy checking user hidden values
if (jQuery.inArray(normalized_taxonomy_id, user_hidden_taxonomies_array) !== -1) {
jQuery(this).remove();
}
});
// Recursive function to show or hide items based on search results
function handleNestedItems(item, show) {
if (show) {
item.show();
} else {
item.hide();
}
item.children("ul.children").children("li").each(function () {
handleNestedItems(jQuery(this), show);
});
}
// Append filter input to taxonomy postbox
jQuery('<?php echo ".post-php #taxonomy-".$taxonomy->slug ?>, <?php echo ".post-new-php #taxonomy-".$taxonomy->slug; ?>').prepend('' +
'<label for="<?php echo TFP_PREFIX."_value_".$taxonomy->slug ?>"><?php _e('Filter', TFP_PREFIX);?>:</label> ' +
'<input type="text" id="<?php echo TFP_PREFIX."_value_".$taxonomy->slug ?>" name="<?php echo TFP_PREFIX."_value_".$taxonomy->slug ?>" class="<?php echo TFP_PREFIX."_value" ?>" autocomplete="off"/> ' +
'<input type="button" value="reset" id="<?php echo TFP_PREFIX."_reset_".$taxonomy->slug ?>" name="<?php echo TFP_PREFIX."_reset_".$taxonomy->slug ?>" class="bubble-float-left <?php echo TFP_PREFIX."_reset" ?>"/>' +
'<p class="tips"><i><?php _e('Use the above field to apply filter', TFP_PREFIX);?></i></p>'
);
// Input value KeyUp event management
jQuery('<?php echo '#' . TFP_PREFIX . '_value_' . $taxonomy->slug; ?>').keyup(function () {
// Read current taxonomy filter value
var filter_value = jQuery(this).val();
var filter_ul_id = '<?php echo '#' . $taxonomy->slug . 'checklist'; ?>';
jQuery(filter_ul_id).find("li").each(function () {
// Clean up all classes on KeyUp event
jQuery(this).removeClass("filter-exists");
jQuery(this).parent("ul.children").removeClass("filter-exists");
});
jQuery(filter_ul_id).find("input[type='checkbox']").each(function () {
// Loop over taxonomy checkboxes
var filter_item = jQuery(this).parent(); // checkbox label element
var filter_li = jQuery(this).parent().parent(); // checkbox li element
if (filter_item.text().toLowerCase().indexOf(filter_value.toLowerCase()) > -1) {
// Show checkbox if text matches the filter value
handleNestedItems(filter_li, true);
// Add "filter-exists" class to identify valid filtered items
filter_li.addClass("filter-exists");
// Add class to all parent UL if at least a valid filtered item exists
filter_li.parents("ul.children").addClass("filter-exists");
}
});
jQuery(filter_ul_id).find("li:not(.filter-exists):not(.user-hidden)").each(function () {
// Hide items without children or show previously hidden items (now valid)
if (jQuery(this).children("ul.children.filter-exists").length === 0 && jQuery(this).parent("ul.children").parent("li.filter-exists").length === 0) {
// Hide items (without a child with class "filter-exists" or without a parent with class "filter-exists")
handleNestedItems(jQuery(this), false);
}
else {
// Show items (with at least a child with class "filter-exists")
handleNestedItems(jQuery(this), true);
}
});
});
// Input reset click event management
jQuery('<?php echo '#' . TFP_PREFIX . '_reset_' . $taxonomy->slug; ?>').click(function () {
// Loop over taxonomy checkboxes
jQuery('<?php echo '#' . $taxonomy->slug . 'checklist'; ?>').find("input[type='checkbox']").each(function () {
// Show all checkboxes
jQuery(this).parent().parent().show();
});
// Clean up value field
jQuery('<?php echo '#' . TFP_PREFIX . '_value_' . $taxonomy->slug; ?>').val('');
});
<?php
}
}
}
}
?>
});
</script>
<?php
}
}
add_action( 'admin_head', 'taxonomy_filter_add_boxes' );