|
1
|
|
|
<?php |
|
2
|
|
|
if ( ! class_exists( 'FooGallery_Pro_Attachment_Taxonomies' ) ) { |
|
3
|
|
|
|
|
4
|
|
|
define( 'FOOGALLERY_ATTACHMENT_TAXONOMY_TAG', 'foogallery_attachment_tag' ); |
|
5
|
|
|
define( 'FOOGALLERY_ATTACHMENT_TAXONOMY_CATEGORY', 'foogallery_attachment_category' ); |
|
6
|
|
|
|
|
7
|
|
|
class FooGallery_Pro_Attachment_Taxonomies { |
|
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
private $cached_terms = array(); |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class Constructor |
|
13
|
|
|
*/ |
|
14
|
|
|
function __construct() { |
|
|
|
|
|
|
15
|
|
|
add_action( 'init', array( $this, 'init_all' ), 11 ); |
|
16
|
|
|
add_action( 'foogallery_admin_settings_override', array( $this, 'add_admin_setting' ) ); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Adds a setting to disable all FooGallery taxonomies |
|
21
|
|
|
* |
|
22
|
|
|
* @param $settings |
|
23
|
|
|
* |
|
24
|
|
|
* @return mixed |
|
25
|
|
|
*/ |
|
26
|
|
|
function add_admin_setting($settings) { |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
$settings['settings'][] = array( |
|
29
|
|
|
'id' => 'disable_attachment_taxonomies', |
|
30
|
|
|
'title' => __( 'Disable Attachment Taxonomies', 'foogallery' ), |
|
31
|
|
|
'desc' => sprintf( __( 'Disables the %s attachment taxonomies (Media Tags and Media Categories).', 'foogallery' ), foogallery_plugin_name() ), |
|
32
|
|
|
'type' => 'checkbox', |
|
33
|
|
|
'tab' => 'advanced' |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
return $settings; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Initialize all the hooks if the taxonomies are not disabled |
|
41
|
|
|
*/ |
|
42
|
|
|
function init_all() { |
|
|
|
|
|
|
43
|
|
|
if ( foogallery_get_setting( 'disable_attachment_taxonomies' ) === 'on' ) { |
|
44
|
|
|
return; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$this->add_taxonomies(); |
|
48
|
|
|
|
|
49
|
|
|
if ( is_admin() ) { |
|
50
|
|
|
add_action( 'admin_menu', array( $this, 'add_menu_items' ), 1 ); |
|
51
|
|
|
add_filter( 'parent_file', array( $this, 'set_current_menu' ) ); |
|
52
|
|
|
add_filter( 'manage_media_columns', array( $this, 'change_attachment_column_names' ) ); |
|
53
|
|
|
add_filter( 'manage_edit-foogallery_attachment_tag_columns', array( $this, 'clean_column_names' ), 999 ); |
|
54
|
|
|
add_filter( 'manage_edit-foogallery_attachment_category_columns', array( $this, 'clean_column_names' ), 999 ); |
|
55
|
|
|
|
|
56
|
|
|
//make the attachment taxonomies awesome |
|
57
|
|
|
add_action( 'admin_head', array( $this, 'include_inline_taxonomy_data_script' ) ); |
|
58
|
|
|
add_filter( 'attachment_fields_to_edit', array( $this, 'inject_code_into_field' ), 10, 2 ); |
|
59
|
|
|
//add_filter( 'attachment_fields_to_save', array( $this, 'save_fields' ), 10, 2 ); |
|
|
|
|
|
|
60
|
|
|
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_js' ), 99 ); |
|
61
|
|
|
|
|
62
|
|
|
//ajax actions from the media modal |
|
63
|
|
|
add_action( 'wp_ajax_foogallery-taxonomies-add-term', array( $this, 'ajax_add_term' ) ); |
|
64
|
|
|
add_action( 'wp_ajax_foogallery-taxonomies-save-terms', array( $this, 'ajax_save_terms' ) ); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
/** |
|
68
|
|
|
* Save terms for an attachment |
|
69
|
|
|
* |
|
70
|
|
|
* @since 1.4.19 |
|
71
|
|
|
*/ |
|
72
|
|
|
public function ajax_save_terms() |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
$nonce = $_POST['nonce']; |
|
75
|
|
|
if (wp_verify_nonce($nonce, 'foogallery-attachment-taxonomy')) { |
|
76
|
|
|
|
|
77
|
|
|
$attachment_id = $_POST['attachment_id']; |
|
78
|
|
|
$terms = $_POST['terms']; |
|
79
|
|
|
$taxonomy = $_POST['taxonomy']; |
|
80
|
|
|
|
|
81
|
|
|
$result = wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $terms)), $taxonomy, false); |
|
82
|
|
|
|
|
83
|
|
|
clean_post_cache($attachment_id); |
|
84
|
|
|
|
|
85
|
|
|
if ( !is_wp_error( $result ) ) { |
|
86
|
|
|
wp_send_json( $terms ); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
die(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Add new term via an ajax call from admin |
|
94
|
|
|
* |
|
95
|
|
|
* @since 1.4.19 |
|
96
|
|
|
* @access public |
|
97
|
|
|
*/ |
|
98
|
|
|
public function ajax_add_term() { |
|
|
|
|
|
|
99
|
|
|
$nonce = $_POST['nonce']; |
|
100
|
|
|
if (wp_verify_nonce($nonce, 'foogallery-attachment-taxonomy')) { |
|
101
|
|
|
|
|
102
|
|
|
$new_term = wp_insert_term($_POST['term_label'], $_POST['taxonomy']); |
|
103
|
|
|
|
|
104
|
|
|
if (is_wp_error($new_term)) { |
|
105
|
|
|
die(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$new_term_obj = null; |
|
109
|
|
|
|
|
110
|
|
|
if (isset($new_term['term_id'])) { |
|
111
|
|
|
$new_term_obj = get_term($new_term['term_id']); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if (!is_wp_error($new_term_obj)) { |
|
115
|
|
|
wp_send_json(array( |
|
116
|
|
|
'new_term' => $new_term_obj, |
|
117
|
|
|
'all_terms' => $this->build_terms_recursive($_POST['taxonomy'], array('hide_empty' => false)) |
|
118
|
|
|
)); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
die(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function save_fields( $post, $attachment ) { |
|
|
|
|
|
|
126
|
|
|
$something = $_POST; |
|
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
return $post; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Enqueue admin script and styles |
|
133
|
|
|
* |
|
134
|
|
|
* @since 1.0.0 |
|
135
|
|
|
* @access public |
|
136
|
|
|
* @static |
|
137
|
|
|
*/ |
|
138
|
|
|
public function enqueue_js() { |
|
139
|
|
|
global $pagenow, $mode; |
|
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
$should_add = wp_script_is('media-views') || ($pagenow === 'upload.php' && $mode === 'grid'); |
|
142
|
|
|
|
|
143
|
|
|
if( !$should_add ) { |
|
144
|
|
|
return; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
//enqueue selectize assets |
|
148
|
|
|
wp_enqueue_script( 'foogallery-selectize-core', FOOGALLERY_URL . 'lib/selectize/selectize.min.js', array('jquery'), FOOGALLERY_VERSION ); |
|
149
|
|
|
wp_enqueue_script( 'foogallery-selectize', FOOGALLERY_URL . 'lib/selectize/foogallery.selectize.js', array('foogallery-selectize-core'), FOOGALLERY_VERSION ); |
|
150
|
|
|
wp_enqueue_style( 'foogallery-selectize', FOOGALLERY_URL . 'lib/selectize/selectize.css', array(), FOOGALLERY_VERSION ); |
|
151
|
|
|
|
|
152
|
|
|
//enqueue media attachment autosave script |
|
153
|
|
|
wp_enqueue_script( 'foogallery-attachment-autosave', FOOGALLERY_URL . 'js/admin-foogallery-attachment-autosave.js', ['media-views']); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Add fields to attachment |
|
158
|
|
|
* |
|
159
|
|
|
* @since 1.0.0 |
|
160
|
|
|
* @access public |
|
161
|
|
|
* @static |
|
162
|
|
|
* @param array $fields An array with all fields to edit |
|
163
|
|
|
* @param object $post An object for the current post |
|
164
|
|
|
* @return array $fields An array with all fields to edit |
|
165
|
|
|
*/ |
|
166
|
|
|
public function inject_code_into_field($fields, $post) { |
|
167
|
|
|
if ( array_key_exists( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, $fields ) ) { |
|
168
|
|
|
|
|
169
|
|
|
$value = trim( $fields[FOOGALLERY_ATTACHMENT_TAXONOMY_TAG]['value'] ); |
|
170
|
|
|
|
|
171
|
|
|
$fields[FOOGALLERY_ATTACHMENT_TAXONOMY_TAG] = array( |
|
172
|
|
|
'show_in_edit' => false, |
|
173
|
|
|
'input' => 'html', |
|
174
|
|
|
'html' => $this->build_taxonomy_html( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, $post, $value ), |
|
175
|
|
|
'label' => __( 'Media Tags', 'foogallery' ) |
|
176
|
|
|
); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
if ( array_key_exists( FOOGALLERY_ATTACHMENT_TAXONOMY_CATEGORY, $fields ) ) { |
|
180
|
|
|
|
|
181
|
|
|
$value = trim( $fields[FOOGALLERY_ATTACHMENT_TAXONOMY_CATEGORY]['value'] ); |
|
182
|
|
|
|
|
183
|
|
|
$fields[FOOGALLERY_ATTACHMENT_TAXONOMY_CATEGORY] = array( |
|
184
|
|
|
'show_in_edit' => false, |
|
185
|
|
|
'input' => 'html', |
|
186
|
|
|
'html' => $this->build_taxonomy_html( FOOGALLERY_ATTACHMENT_TAXONOMY_CATEGORY, $post, $value ), |
|
187
|
|
|
'label' => __( 'Media Categories', 'foogallery' ) |
|
188
|
|
|
); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
return $fields; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Add custom js into admin head so that we can build up decent taxonomy selectize controls |
|
196
|
|
|
* |
|
197
|
|
|
* @since 1.0.0 |
|
198
|
|
|
* @access public |
|
199
|
|
|
* @static |
|
200
|
|
|
*/ |
|
201
|
|
|
public function include_inline_taxonomy_data_script() { |
|
202
|
|
|
global $pagenow, $mode; |
|
|
|
|
|
|
203
|
|
|
|
|
204
|
|
|
$should_add = wp_script_is('media-views') || ($pagenow === 'upload.php' && $mode === 'grid'); |
|
205
|
|
|
|
|
206
|
|
|
if( !$should_add ) { |
|
207
|
|
|
return; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
$taxonomy_data[FOOGALLERY_ATTACHMENT_TAXONOMY_TAG] = array( |
|
|
|
|
|
|
211
|
|
|
'slug' => FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, |
|
212
|
|
|
'terms' => $this->build_terms_recursive(FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, array('hide_empty' => false)), |
|
213
|
|
|
'query_var' => true, |
|
214
|
|
|
'labels' => array( |
|
215
|
|
|
'placeholder' => __( 'Select tags, or add a new tag...', 'foogallery' ), |
|
216
|
|
|
'add' => __( 'Add new tag', 'foogallery' ) |
|
217
|
|
|
), |
|
218
|
|
|
); |
|
219
|
|
|
|
|
220
|
|
|
$taxonomy_data[FOOGALLERY_ATTACHMENT_TAXONOMY_CATEGORY] = array( |
|
221
|
|
|
'slug' => FOOGALLERY_ATTACHMENT_TAXONOMY_CATEGORY, |
|
222
|
|
|
'terms' => $this->build_terms_recursive(FOOGALLERY_ATTACHMENT_TAXONOMY_CATEGORY, array('hide_empty' => false)), |
|
223
|
|
|
'query_var' => true, |
|
224
|
|
|
'labels' => array( |
|
225
|
|
|
'placeholder' => __( 'Select categories, or add a new category...', 'foogallery' ), |
|
226
|
|
|
'add' => __( 'Add new category', 'foogallery' ) |
|
227
|
|
|
), |
|
228
|
|
|
); |
|
229
|
|
|
|
|
230
|
|
|
$taxonomy_data['nonce'] = wp_create_nonce( 'foogallery-attachment-taxonomy' ); |
|
231
|
|
|
|
|
232
|
|
|
echo '<script type="text/javascript"> |
|
233
|
|
|
window.FOOGALLERY_TAXONOMY_DATA = ' . json_encode($taxonomy_data) . '; |
|
234
|
|
|
</script>'; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
function change_attachment_column_names( $columns ) { |
|
|
|
|
|
|
238
|
|
|
|
|
239
|
|
|
if ( array_key_exists( 'taxonomy-foogallery_attachment_category', $columns ) ) { |
|
240
|
|
|
$columns['taxonomy-foogallery_attachment_category'] = __('Categories', 'foogallery'); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
return $columns; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Clean up the taxonomy columns for WP Seo plugin |
|
248
|
|
|
* |
|
249
|
|
|
* @param $columns |
|
250
|
|
|
* @return mixed |
|
251
|
|
|
*/ |
|
252
|
|
|
function clean_column_names( $columns ) { |
|
|
|
|
|
|
253
|
|
|
|
|
254
|
|
|
//cleanup wpseo columns! |
|
255
|
|
|
if ( array_key_exists( 'wpseo_score', $columns ) ) { |
|
256
|
|
|
unset( $columns['wpseo_score'] ); |
|
257
|
|
|
} |
|
258
|
|
|
if ( array_key_exists( 'wpseo_score_readability', $columns ) ) { |
|
259
|
|
|
unset( $columns['wpseo_score_readability'] ); |
|
260
|
|
|
} |
|
261
|
|
|
return $columns; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Add the menu items under the FooGallery main menu |
|
266
|
|
|
*/ |
|
267
|
|
|
function add_menu_items() { |
|
|
|
|
|
|
268
|
|
|
foogallery_add_submenu_page( |
|
269
|
|
|
__( 'Media Tags', 'foogallery' ), |
|
270
|
|
|
'manage_options', |
|
271
|
|
|
'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_TAG . '&post_type=' . FOOGALLERY_CPT_GALLERY, |
|
272
|
|
|
null |
|
273
|
|
|
); |
|
274
|
|
|
|
|
275
|
|
|
foogallery_add_submenu_page( |
|
276
|
|
|
__( 'Media Categories', 'foogallery' ), |
|
277
|
|
|
'manage_options', |
|
278
|
|
|
'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_CATEGORY . '&post_type=' . FOOGALLERY_CPT_GALLERY, |
|
279
|
|
|
null |
|
280
|
|
|
); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Make sure the taxonomy menu items are highlighted |
|
285
|
|
|
* @param $parent_file |
|
286
|
|
|
* @return mixed |
|
287
|
|
|
*/ |
|
288
|
|
|
function set_current_menu( $parent_file ) { |
|
|
|
|
|
|
289
|
|
|
global $submenu_file, $current_screen; |
|
|
|
|
|
|
290
|
|
|
|
|
291
|
|
|
if ( $current_screen->post_type == FOOGALLERY_CPT_GALLERY ) { |
|
292
|
|
|
|
|
293
|
|
|
if ( 'edit-foogallery_attachment_tag' === $current_screen->id ) { |
|
294
|
|
|
$submenu_file = 'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_TAG . '&post_type=' . FOOGALLERY_CPT_GALLERY; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
if ( 'edit-foogallery_attachment_category' === $current_screen->id ) { |
|
298
|
|
|
$submenu_file = 'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_CATEGORY . '&post_type=' . FOOGALLERY_CPT_GALLERY; |
|
299
|
|
|
} |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
return $parent_file; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* Register the taxonomies for attachments |
|
307
|
|
|
*/ |
|
308
|
|
|
function add_taxonomies() { |
|
|
|
|
|
|
309
|
|
|
|
|
310
|
|
|
// if ( foogallery_get_setting( 'disable_attachment_taxonomies') === 'on' ) { |
|
|
|
|
|
|
311
|
|
|
// return; |
|
312
|
|
|
// } |
|
313
|
|
|
|
|
314
|
|
|
$tag_args = array( |
|
315
|
|
|
'labels' => array( |
|
316
|
|
|
'name' => __( 'Media Tags', 'foogallery' ), |
|
317
|
|
|
'singular_name' => __( 'Tag', 'foogallery' ), |
|
318
|
|
|
'search_items' => __( 'Search Tags', 'foogallery' ), |
|
319
|
|
|
'all_items' => __( 'All Tags', 'foogallery' ), |
|
320
|
|
|
'parent_item' => __( 'Parent Tag', 'foogallery' ), |
|
321
|
|
|
'parent_item_colon' => __( 'Parent Tag:', 'foogallery' ), |
|
322
|
|
|
'edit_item' => __( 'Edit Tag', 'foogallery' ), |
|
323
|
|
|
'update_item' => __( 'Update Tag', 'foogallery' ), |
|
324
|
|
|
'add_new_item' => __( 'Add New Tag', 'foogallery' ), |
|
325
|
|
|
'new_item_name' => __( 'New Tag Name', 'foogallery' ), |
|
326
|
|
|
'menu_name' => __( 'Media Tags', 'foogallery' ) |
|
327
|
|
|
), |
|
328
|
|
|
'hierarchical' => false, |
|
329
|
|
|
'query_var' => true, |
|
330
|
|
|
'rewrite' => false, |
|
331
|
|
|
'show_admin_column' => false, |
|
332
|
|
|
'show_in_menu' => false, |
|
333
|
|
|
'update_count_callback' => '_update_generic_term_count' |
|
334
|
|
|
); |
|
335
|
|
|
|
|
336
|
|
|
register_taxonomy( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, 'attachment', $tag_args ); |
|
337
|
|
|
|
|
338
|
|
|
$category_args = array( |
|
339
|
|
|
'labels' => array( |
|
340
|
|
|
'name' => __( 'Media Categories', 'foogallery' ), |
|
341
|
|
|
'singular_name' => __( 'Category', 'foogallery' ), |
|
342
|
|
|
'search_items' => __( 'Search Categories', 'foogallery' ), |
|
343
|
|
|
'all_items' => __( 'All Categories', 'foogallery' ), |
|
344
|
|
|
'parent_item' => __( 'Parent Category', 'foogallery' ), |
|
345
|
|
|
'parent_item_colon' => __( 'Parent Category:', 'foogallery' ), |
|
346
|
|
|
'edit_item' => __( 'Edit Category', 'foogallery' ), |
|
347
|
|
|
'update_item' => __( 'Update Category', 'foogallery' ), |
|
348
|
|
|
'add_new_item' => __( 'Add New Category', 'foogallery' ), |
|
349
|
|
|
'new_item_name' => __( 'New Category Name', 'foogallery' ), |
|
350
|
|
|
'menu_name' => __( 'Media Categories', 'foogallery' ) |
|
351
|
|
|
), |
|
352
|
|
|
'hierarchical' => true, |
|
353
|
|
|
'query_var' => true, |
|
354
|
|
|
'rewrite' => false, |
|
355
|
|
|
'show_admin_column' => true, |
|
356
|
|
|
'show_in_menu' => false, |
|
357
|
|
|
'update_count_callback' => '_update_generic_term_count' |
|
358
|
|
|
); |
|
359
|
|
|
|
|
360
|
|
|
register_taxonomy( FOOGALLERY_ATTACHMENT_TAXONOMY_CATEGORY, 'attachment', $category_args ); |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
/** |
|
364
|
|
|
* Build up a taxonomy field HTML |
|
365
|
|
|
* |
|
366
|
|
|
* @param $taxonomy |
|
367
|
|
|
* @param $post |
|
368
|
|
|
* |
|
369
|
|
|
* @return array |
|
370
|
|
|
*/ |
|
371
|
|
|
function build_taxonomy_html( $taxonomy, $post, $value ) { |
|
|
|
|
|
|
372
|
|
|
$html = '<input type="text" data-attachment_id="' . $post->ID . '" class="foogallery-attachment-ignore-change" id="attachments-' . $post->ID .'-' . $taxonomy . '" name="attachments-' . $post->ID .'-' . $taxonomy . '" value="' . $value . '" data-original-value="' . $value . '" />'; |
|
373
|
|
|
$html .= '<script type="script/javascript"> |
|
374
|
|
|
FOOGALLERY_SELECTIZE(\'#attachments-' . $post->ID .'-' . $taxonomy . '\', \'' . $taxonomy .'\'); |
|
375
|
|
|
</script>'; |
|
376
|
|
|
return $html; |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
/** |
|
380
|
|
|
* Get terms sorted by hierarchy in a recursive way |
|
381
|
|
|
* |
|
382
|
|
|
* @param string $taxonomy The taxonomy name |
|
383
|
|
|
* @param array $args The arguments which should be passed to the get_terms function |
|
384
|
|
|
* @param int $parent The terms parent id (for recursive usage) |
|
385
|
|
|
* @param int $level The current level (for recursive usage) |
|
386
|
|
|
* @param array $parents An array with all the parent terms (for recursive usage) |
|
387
|
|
|
* |
|
388
|
|
|
* @return array $terms_all An array with all the terms for this taxonomy |
|
389
|
|
|
*/ |
|
390
|
|
|
function build_terms_recursive($taxonomy, $args = array(), $parent = 0, $level = 1, $parents = array()) { |
|
|
|
|
|
|
391
|
|
|
//check if the taxonomy terms have already been built up |
|
392
|
|
|
if ( 0 === $parent && array_key_exists( $taxonomy, $this->cached_terms ) ) { |
|
393
|
|
|
return $this->cached_terms[$taxonomy]; |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
$terms_all = array(); |
|
397
|
|
|
|
|
398
|
|
|
$args['parent'] = $args['child_of'] = $parent; |
|
399
|
|
|
|
|
400
|
|
|
$terms = get_terms($taxonomy, $args); |
|
401
|
|
|
|
|
402
|
|
|
foreach($terms as $term) { |
|
403
|
|
|
$term->level = $level; |
|
404
|
|
|
$term->parents = $parents; |
|
405
|
|
|
$term_parents = $parents; |
|
406
|
|
|
$term_parents[] = $term->name; |
|
407
|
|
|
$terms_all[] = $term; |
|
408
|
|
|
$terms_sub = $this->build_terms_recursive($taxonomy, $args, $term->term_id, $level + 1, $term_parents); |
|
409
|
|
|
|
|
410
|
|
|
if(!empty($terms_sub)) { |
|
411
|
|
|
$terms_all = array_merge($terms_all, $terms_sub); |
|
412
|
|
|
} |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
//cache what we have built up |
|
416
|
|
|
if ( 0 === $parent && !array_key_exists( $taxonomy, $this->cached_terms ) ) { |
|
417
|
|
|
$this->cached_terms[$taxonomy] = $terms_all; |
|
418
|
|
|
} |
|
419
|
|
|
|
|
420
|
|
|
return $terms_all; |
|
421
|
|
|
} |
|
422
|
|
|
} |
|
423
|
|
|
} |
|
424
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.