1
|
|
|
<?php |
2
|
|
|
if ( ! class_exists( 'FooGallery_Attachment_Taxonomies' ) ) { |
3
|
|
|
|
4
|
|
|
define( 'FOOGALLERY_ATTACHMENT_TAXONOMY_TAG', 'foogallery_attachment_tag' ); |
5
|
|
|
define( 'FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION', 'foogallery_attachment_collection' ); |
6
|
|
|
|
7
|
|
|
class FooGallery_Attachment_Taxonomies { |
|
|
|
|
8
|
|
|
|
9
|
|
|
private $cached_terms = array(); |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Constructor |
13
|
|
|
*/ |
14
|
|
|
function __construct() { |
|
|
|
|
15
|
|
|
add_action( 'init', array( $this, 'add_taxonomies' ) ); |
16
|
|
|
|
17
|
|
|
if ( is_admin() ) { |
18
|
|
|
add_action( 'admin_menu', array( $this, 'add_menu_items' ), 1 ); |
19
|
|
|
add_filter( 'parent_file', array( $this, 'set_current_menu' ) ); |
20
|
|
|
add_filter( 'manage_media_columns', array( $this, 'change_attachment_column_names' ) ); |
21
|
|
|
add_filter( 'manage_edit-foogallery_attachment_tag_columns', array( $this, 'clean_column_names' ), 999 ); |
22
|
|
|
add_filter( 'manage_edit-foogallery_attachment_collection_columns', array( $this, 'clean_column_names' ), 999 ); |
23
|
|
|
|
24
|
|
|
//make the attachment taxonomies awesome |
25
|
|
|
add_action( 'admin_head', array( $this, 'add_custom_js' ) ); |
26
|
|
|
add_filter( 'attachment_fields_to_edit', array( $this, 'inject_code_into_field' ), 10, 2 ); |
27
|
|
|
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_js' ) ); |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
//add_filter( 'foogallery_attachment_add_fields', array( $this, 'remove_taxonomy_fields') ); |
|
|
|
|
31
|
|
|
//add_action( 'restrict_manage_posts', array( $this, 'add_collection_filter' ) ); |
|
|
|
|
32
|
|
|
|
33
|
|
|
//add_filter( 'foogallery_attachment_custom_fields_with_post', array( $this, 'add_taxonomy_fields' ), 10, 2 ); |
|
|
|
|
34
|
|
|
//add_filter( 'foogallery_attachment_field_taxonomy_tag', array( $this, 'customize_media_tag_field'), 10, 2 ); |
|
|
|
|
35
|
|
|
//add_filter( 'foogallery_attachment_save_field_taxonomy_tag', array( $this, 'save_media_tag_field' ), 10, 4 ); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Enqueue admin script and styles |
41
|
|
|
* |
42
|
|
|
* @since 1.0.0 |
43
|
|
|
* @access public |
44
|
|
|
* @static |
45
|
|
|
*/ |
46
|
|
|
public function enqueue_js() { |
47
|
|
|
global $pagenow, $mode; |
|
|
|
|
48
|
|
|
|
49
|
|
|
$should_add = wp_script_is('media-views') || ($pagenow === 'upload.php' && $mode === 'grid'); |
50
|
|
|
|
51
|
|
|
if( !$should_add ) { |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// // Enqueue filters script |
|
|
|
|
56
|
|
|
// if(Property::$has_filter) { |
57
|
|
|
// wp_enqueue_script( |
58
|
|
|
// 'f4-media-taxonomies-filter', |
59
|
|
|
// F4_MT_URL . 'Core/Assets/js/filter.js', |
60
|
|
|
// array(), |
61
|
|
|
// false, |
62
|
|
|
// true |
63
|
|
|
// ); |
64
|
|
|
// } |
65
|
|
|
|
66
|
|
|
//enqueue selectize assets |
67
|
|
|
wp_enqueue_script( 'foogallery-selectize-core', FOOGALLERY_URL . 'lib/selectize/selectize.min.js', array('jquery'), FOOGALLERY_VERSION ); |
68
|
|
|
wp_enqueue_script( 'foogallery-selectize', FOOGALLERY_URL . 'lib/selectize/foogallery.selectize.js', array('foogallery-selectize-core'), FOOGALLERY_VERSION ); |
69
|
|
|
wp_enqueue_style( 'foogallery-selectize', FOOGALLERY_URL . 'lib/selectize/selectize.css', array(), FOOGALLERY_VERSION ); |
70
|
|
|
|
71
|
|
|
// wp_enqueue_script( |
|
|
|
|
72
|
|
|
// 'f4-media-taxonomies-assignment', |
73
|
|
|
// F4_MT_URL . 'Core/Assets/js/assignment.js', |
74
|
|
|
// array(), |
75
|
|
|
// false, |
76
|
|
|
// true |
77
|
|
|
// ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Add fields to attachment |
82
|
|
|
* |
83
|
|
|
* @since 1.0.0 |
84
|
|
|
* @access public |
85
|
|
|
* @static |
86
|
|
|
* @param array $fields An array with all fields to edit |
87
|
|
|
* @param object $post An object for the current post |
88
|
|
|
* @return array $fields An array with all fields to edit |
89
|
|
|
*/ |
90
|
|
|
public function inject_code_into_field($fields, $post) { |
91
|
|
|
if ( array_key_exists( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, $fields ) ) { |
92
|
|
|
|
93
|
|
|
$value = trim( $fields[FOOGALLERY_ATTACHMENT_TAXONOMY_TAG]['value'] ); |
94
|
|
|
|
95
|
|
|
$fields[FOOGALLERY_ATTACHMENT_TAXONOMY_TAG] = array( |
96
|
|
|
'show_in_edit' => false, |
97
|
|
|
'input' => 'html', |
98
|
|
|
'html' => $this->build_taxonomy_html( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, $post, $value ), |
99
|
|
|
'label' => __( 'Media Tags', 'foogallery' ) |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ( array_key_exists( FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION, $fields ) ) { |
104
|
|
|
|
105
|
|
|
$value = $fields[FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION]['value']; |
|
|
|
|
106
|
|
|
|
107
|
|
|
// $fields[FOOGALLERY_ATTACHMENT_TAXONOMY_TAG] = array( |
|
|
|
|
108
|
|
|
// 'show_in_edit' => false, |
109
|
|
|
// 'input' => 'html', |
110
|
|
|
// 'html' => $this->build_taxonomy_html( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, $post, $value ), |
111
|
|
|
// 'label' => __( 'Media Tags', 'foogallery' ) |
112
|
|
|
// ); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $fields; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Add custom js into admin head so that we can build up decent taxonomy selectize controls |
120
|
|
|
* |
121
|
|
|
* @since 1.0.0 |
122
|
|
|
* @access public |
123
|
|
|
* @static |
124
|
|
|
*/ |
125
|
|
|
public function add_custom_js() { |
126
|
|
|
global $pagenow, $mode; |
|
|
|
|
127
|
|
|
|
128
|
|
|
$should_add = wp_script_is('media-views') || ($pagenow === 'upload.php' && $mode === 'grid'); |
129
|
|
|
|
130
|
|
|
if( !$should_add ) { |
131
|
|
|
return; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$taxonomy_data[FOOGALLERY_ATTACHMENT_TAXONOMY_TAG] = array( |
|
|
|
|
135
|
|
|
'slug' => FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, |
136
|
|
|
'terms' => $this->build_terms_recursive(FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, array('hide_empty' => false)), |
137
|
|
|
'query_var' => true, |
138
|
|
|
'labels' => array( |
139
|
|
|
'placeholder' => __( 'Select tags, or add a new tag...', 'foogallery' ), |
140
|
|
|
'add' => __( 'Add new tag', 'foogallery' ) |
141
|
|
|
), |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
$taxonomy_data[FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION] = array( |
145
|
|
|
'slug' => FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION, |
146
|
|
|
'terms' => $this->build_terms_recursive(FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION, array('hide_empty' => false)), |
147
|
|
|
'query_var' => true, |
148
|
|
|
'labels' => array( |
149
|
|
|
'placeholder' => __( 'Select collections, or add a new collection...', 'foogallery' ), |
150
|
|
|
'add' => __( 'Add new collection', 'foogallery' ) |
151
|
|
|
), |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
echo '<script type="text/javascript"> |
155
|
|
|
window.FOOGALLERY_TAXONOMY_DATA = ' . json_encode($taxonomy_data) . '; |
156
|
|
|
</script>'; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
function change_attachment_column_names( $columns ) { |
|
|
|
|
160
|
|
|
|
161
|
|
|
if ( array_key_exists( 'taxonomy-foogallery_attachment_collection', $columns ) ) { |
162
|
|
|
$columns['taxonomy-foogallery_attachment_collection'] = __('Collections', 'foogallery'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $columns; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Clean up the taxonomy columns for WP Seo plugin |
170
|
|
|
* |
171
|
|
|
* @param $columns |
172
|
|
|
* @return mixed |
173
|
|
|
*/ |
174
|
|
|
function clean_column_names( $columns ) { |
|
|
|
|
175
|
|
|
|
176
|
|
|
//cleanup wpseo columns! |
177
|
|
|
if ( array_key_exists( 'wpseo_score', $columns ) ) { |
178
|
|
|
unset( $columns['wpseo_score'] ); |
179
|
|
|
} |
180
|
|
|
if ( array_key_exists( 'wpseo_score_readability', $columns ) ) { |
181
|
|
|
unset( $columns['wpseo_score_readability'] ); |
182
|
|
|
} |
183
|
|
|
return $columns; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Add the menu items under the FooGallery main menu |
188
|
|
|
*/ |
189
|
|
|
function add_menu_items() { |
|
|
|
|
190
|
|
|
foogallery_add_submenu_page( |
191
|
|
|
__( 'Media Tags', 'foogallery' ), |
192
|
|
|
'manage_options', |
193
|
|
|
'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_TAG . '&post_type=' . FOOGALLERY_CPT_GALLERY, |
194
|
|
|
null |
195
|
|
|
); |
196
|
|
|
|
197
|
|
|
foogallery_add_submenu_page( |
198
|
|
|
__( 'Media Collections', 'foogallery' ), |
199
|
|
|
'manage_options', |
200
|
|
|
'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION . '&post_type=' . FOOGALLERY_CPT_GALLERY, |
201
|
|
|
null |
202
|
|
|
); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Make sure the taxonomy menu items are highlighted |
207
|
|
|
* @param $parent_file |
208
|
|
|
* @return mixed |
209
|
|
|
*/ |
210
|
|
|
function set_current_menu( $parent_file ) { |
|
|
|
|
211
|
|
|
global $submenu_file, $current_screen; |
|
|
|
|
212
|
|
|
|
213
|
|
|
if ( $current_screen->post_type == FOOGALLERY_CPT_GALLERY ) { |
214
|
|
|
|
215
|
|
|
if ( 'edit-foogallery_attachment_tag' === $current_screen->id ) { |
216
|
|
|
$submenu_file = 'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_TAG . '&post_type=' . FOOGALLERY_CPT_GALLERY; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
if ( 'edit-foogallery_attachment_collection' === $current_screen->id ) { |
220
|
|
|
$submenu_file = 'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION . '&post_type=' . FOOGALLERY_CPT_GALLERY; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return $parent_file; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Register the taxonomies for attachments |
229
|
|
|
*/ |
230
|
|
|
function add_taxonomies() { |
|
|
|
|
231
|
|
|
|
232
|
|
|
$tag_args = array( |
233
|
|
|
'labels' => array( |
234
|
|
|
'name' => __( 'Media Tags', 'foogallery' ), |
235
|
|
|
'singular_name' => __( 'Tag', 'foogallery' ), |
236
|
|
|
'search_items' => __( 'Search Tags', 'foogallery' ), |
237
|
|
|
'all_items' => __( 'All Tags', 'foogallery' ), |
238
|
|
|
'parent_item' => __( 'Parent Tag', 'foogallery' ), |
239
|
|
|
'parent_item_colon' => __( 'Parent Tag:', 'foogallery' ), |
240
|
|
|
'edit_item' => __( 'Edit Tag', 'foogallery' ), |
241
|
|
|
'update_item' => __( 'Update Tag', 'foogallery' ), |
242
|
|
|
'add_new_item' => __( 'Add New Tag', 'foogallery' ), |
243
|
|
|
'new_item_name' => __( 'New Tag Name', 'foogallery' ), |
244
|
|
|
'menu_name' => __( 'Media Tags', 'foogallery' ) |
245
|
|
|
), |
246
|
|
|
'hierarchical' => false, |
247
|
|
|
'query_var' => true, |
248
|
|
|
'rewrite' => false, |
249
|
|
|
'show_admin_column' => false, |
250
|
|
|
'show_in_menu' => false, |
251
|
|
|
'update_count_callback' => '_update_generic_term_count' |
252
|
|
|
); |
253
|
|
|
|
254
|
|
|
register_taxonomy( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, 'attachment', $tag_args ); |
255
|
|
|
|
256
|
|
|
$collection_args = array( |
257
|
|
|
'labels' => array( |
258
|
|
|
'name' => __( 'Media Collections', 'foogallery' ), |
259
|
|
|
'singular_name' => __( 'Collection', 'foogallery' ), |
260
|
|
|
'search_items' => __( 'Search Collections', 'foogallery' ), |
261
|
|
|
'all_items' => __( 'All Collections', 'foogallery' ), |
262
|
|
|
'parent_item' => __( 'Parent Collection', 'foogallery' ), |
263
|
|
|
'parent_item_colon' => __( 'Parent Collection:', 'foogallery' ), |
264
|
|
|
'edit_item' => __( 'Edit Collection', 'foogallery' ), |
265
|
|
|
'update_item' => __( 'Update Collection', 'foogallery' ), |
266
|
|
|
'add_new_item' => __( 'Add New Collection', 'foogallery' ), |
267
|
|
|
'new_item_name' => __( 'New Collection Name', 'foogallery' ), |
268
|
|
|
'menu_name' => __( 'Media Collections', 'foogallery' ) |
269
|
|
|
), |
270
|
|
|
'hierarchical' => true, |
271
|
|
|
'query_var' => true, |
272
|
|
|
'rewrite' => false, |
273
|
|
|
'show_admin_column' => true, |
274
|
|
|
'show_in_menu' => false, |
275
|
|
|
'update_count_callback' => '_update_generic_term_count' |
276
|
|
|
); |
277
|
|
|
|
278
|
|
|
register_taxonomy( FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION, 'attachment', $collection_args ); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Add the taxonomy fields to the attachment |
283
|
|
|
* |
284
|
|
|
* @param $fields array All fields that will be added to the media modal |
285
|
|
|
* @param $post |
286
|
|
|
* |
287
|
|
|
* @return mixed |
288
|
|
|
*/ |
289
|
|
|
function add_taxonomy_fields( $fields, $post ) { |
|
|
|
|
290
|
|
|
|
291
|
|
|
$fields[FOOGALLERY_ATTACHMENT_TAXONOMY_TAG] = array( |
292
|
|
|
'show_in_edit' => false, |
293
|
|
|
'input' => 'html', |
294
|
|
|
'html' => $this->build_taxonomy_html( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, $post ), |
|
|
|
|
295
|
|
|
'label' => __( 'Tags', 'foogallery' ) |
296
|
|
|
); |
297
|
|
|
|
298
|
|
|
$fields[FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION] = array( |
299
|
|
|
'show_in_edit' => false, |
300
|
|
|
'input' => 'html', |
301
|
|
|
'html' => $this->build_taxonomy_html( FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION, $post ), |
|
|
|
|
302
|
|
|
'label' => __( 'Collections', 'foogallery' ) |
303
|
|
|
); |
304
|
|
|
|
305
|
|
|
return $fields; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Build up a taxonomy field HTML |
310
|
|
|
* |
311
|
|
|
* @param $taxonomy |
312
|
|
|
* @param $post |
313
|
|
|
* |
314
|
|
|
* @return array |
315
|
|
|
*/ |
316
|
|
|
function build_taxonomy_html( $taxonomy, $post, $value ) { |
|
|
|
|
317
|
|
|
|
318
|
|
|
$html = '<input type="text" id="attachments-' . $post->ID .'-' . $taxonomy . '" name="attachments[' . $post->ID .'][' . $taxonomy . ']" value="' . $value . '" />'; |
319
|
|
|
$html .= '<script type="script/javascript"> |
320
|
|
|
FOOGALLERY_SELECTIZE(\'#attachments-' . $post->ID .'-' . $taxonomy . '\', \'' . $taxonomy .'\'); |
321
|
|
|
</script>'; |
322
|
|
|
return $html; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Get terms sorted by hierarchy in a recursive way |
327
|
|
|
* |
328
|
|
|
* @param string $taxonomy The taxonomy name |
329
|
|
|
* @param array $args The arguments which should be passed to the get_terms function |
330
|
|
|
* @param int $parent The terms parent id (for recursive usage) |
331
|
|
|
* @param int $level The current level (for recursive usage) |
332
|
|
|
* @param array $parents An array with all the parent terms (for recursive usage) |
333
|
|
|
* |
334
|
|
|
* @return array $terms_all An array with all the terms for this taxonomy |
335
|
|
|
*/ |
336
|
|
|
function build_terms_recursive($taxonomy, $args = array(), $parent = 0, $level = 1, $parents = array()) { |
|
|
|
|
337
|
|
|
//check if the taxonomy terms have already been built up |
338
|
|
|
if ( 0 === $parent && array_key_exists( $taxonomy, $this->cached_terms ) ) { |
339
|
|
|
return $this->cached_terms[$taxonomy]; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
$terms_all = array(); |
343
|
|
|
|
344
|
|
|
$args['parent'] = $args['child_of'] = $parent; |
345
|
|
|
|
346
|
|
|
$terms = get_terms($taxonomy, $args); |
347
|
|
|
|
348
|
|
|
foreach($terms as $term) { |
349
|
|
|
$term->level = $level; |
350
|
|
|
$term->parents = $parents; |
351
|
|
|
$term_parents = $parents; |
352
|
|
|
$term_parents[] = $term->name; |
353
|
|
|
$terms_all[] = $term; |
354
|
|
|
$terms_sub = $this->build_terms_recursive($taxonomy, $args, $term->term_id, $level + 1, $term_parents); |
355
|
|
|
|
356
|
|
|
if(!empty($terms_sub)) { |
357
|
|
|
$terms_all = array_merge($terms_all, $terms_sub); |
358
|
|
|
} |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
//cache what we have built up |
362
|
|
|
if ( 0 === $parent && !array_key_exists( $taxonomy, $this->cached_terms ) ) { |
363
|
|
|
$this->cached_terms[$taxonomy] = $terms_all; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
return $terms_all; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Remove the automatically added attachments fields |
371
|
|
|
* @param $fields |
372
|
|
|
* |
373
|
|
|
* @return mixed |
374
|
|
|
*/ |
375
|
|
|
function remove_taxonomy_fields( $fields ) { |
|
|
|
|
376
|
|
|
if ( array_key_exists( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, $fields ) ) { |
377
|
|
|
unset( $fields[FOOGALLERY_ATTACHMENT_TAXONOMY_TAG] ); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
if ( array_key_exists( FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION, $fields ) ) { |
381
|
|
|
unset( $fields[FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION] ); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
return $fields; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Customize the media tag field to make sure we output a checkboxlist |
389
|
|
|
* @param $field_values |
390
|
|
|
* |
391
|
|
|
* @return mixed |
392
|
|
|
*/ |
393
|
|
|
function customize_media_tag_field( $field_values, $post_id ) { |
394
|
|
|
|
395
|
|
|
$media_tags = array(); |
396
|
|
|
|
397
|
|
|
//get the terms linked to the attachment |
398
|
|
|
$terms = get_the_terms( $post_id, FOOGALLERY_ATTACHMENT_TAXONOMY_TAG ); |
399
|
|
|
if ( $terms && ! is_wp_error( $terms ) ) { |
400
|
|
|
foreach ( $terms as $term ) { |
401
|
|
|
$media_tags[ $term->term_id ] = $term->name; |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
//set to html |
406
|
|
|
$field_values['input'] = 'html'; |
407
|
|
|
|
408
|
|
|
$html = ''; |
409
|
|
|
$i = 0; |
410
|
|
|
|
411
|
|
|
if ( ! empty( $field_values['options'] ) ) { |
412
|
|
|
|
413
|
|
|
foreach ( $field_values['options'] as $k => $v ) { |
414
|
|
|
if ( array_key_exists( $k, $media_tags ) ) { |
415
|
|
|
$checked = ' checked="checked"'; |
416
|
|
|
} else { |
417
|
|
|
$checked = ''; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
$html .= '<input' . $checked . ' value="' . $k . '" type="checkbox" name="attachments[' . $post_id . '][' . FOOGALLERY_ATTACHMENT_TAXONOMY_TAG . '][' . $k . ']" id="' . sanitize_key( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG . '_' . $post_id . '_' . $i ) . '" /> <label for="' . sanitize_key( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG . '_' . $post_id . '_' . $i ) . '">' . $v . '</label> '; |
421
|
|
|
$i++; |
422
|
|
|
} |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
if ( 0 === $i ) { |
426
|
|
|
$html .= __( 'No Tags Available!', 'foogallery' ); |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
$html .= '<style>.compat-field-foogallery_media_tags .field input {margin-right: 0px;} .compat-field-foogallery_media_tags .field label {vertical-align: bottom; margin-right: 10px;}</style>'; |
430
|
|
|
|
431
|
|
|
$html .= '<br /><a target="_blank" href="' . admin_url( 'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_TAG . '&post_type=attachment' ) . '">' . __( 'Manage Tags', 'foogallery' ) . '</a>'; |
432
|
|
|
|
433
|
|
|
$field_values['value'] = ''; |
434
|
|
|
$field_values['html'] = $html; |
435
|
|
|
|
436
|
|
|
return $field_values; |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* Save the tags for the attachment |
441
|
|
|
* |
442
|
|
|
* @param $field |
443
|
|
|
* @param $values |
444
|
|
|
* @param $post |
445
|
|
|
* @param $attachment |
446
|
|
|
*/ |
447
|
|
|
function save_media_tag_field($field, $values, $post, $attachment) { |
|
|
|
|
448
|
|
|
$post_id = $post['ID']; |
449
|
|
|
|
450
|
|
|
//first clear any tags for the post |
451
|
|
|
wp_delete_object_term_relationships( $post_id, FOOGALLERY_ATTACHMENT_TAXONOMY_TAG ); |
452
|
|
|
|
453
|
|
|
$tag_ids = $attachment[ $field ]; |
454
|
|
|
|
455
|
|
|
if ( !empty( $tag_ids ) ) { |
456
|
|
|
//clean tag ids |
457
|
|
|
$tag_ids = array_keys( $tag_ids ); |
458
|
|
|
$tag_ids = array_map( 'intval', $tag_ids ); |
459
|
|
|
$tag_ids = array_unique( $tag_ids ); |
460
|
|
|
|
461
|
|
|
$term_taxonomy_ids = wp_set_object_terms( $post_id, $tag_ids, FOOGALLERY_ATTACHMENT_TAXONOMY_TAG ); |
462
|
|
|
|
463
|
|
|
if ( is_wp_error( $term_taxonomy_ids ) ) { |
464
|
|
|
// There was an error somewhere and the terms couldn't be set. |
465
|
|
|
$post['errors'][ $field ]['errors'][] = __( 'Error saving the tags for the attachment!', 'foogallery' ); |
466
|
|
|
} |
467
|
|
|
} |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
|
471
|
|
|
/*** |
472
|
|
|
* |
473
|
|
|
* Add a tag filter to the attachments listing page |
474
|
|
|
*/ |
475
|
|
|
function add_collection_filter() { |
|
|
|
|
476
|
|
|
global $pagenow; |
477
|
|
|
if ( 'upload.php' == $pagenow ) { |
478
|
|
|
|
479
|
|
|
$dropdown_options = array( |
480
|
|
|
'taxonomy' => FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION, |
481
|
|
|
'show_option_all' => __( 'All Collections' ), |
482
|
|
|
'hide_empty' => false, |
483
|
|
|
'hierarchical' => true, |
484
|
|
|
'orderby' => 'name', |
485
|
|
|
'show_count' => true, |
486
|
|
|
'walker' => new foogallery_walker_category_dropdown(), |
487
|
|
|
'value' => 'slug' |
488
|
|
|
); |
489
|
|
|
|
490
|
|
|
wp_dropdown_categories( $dropdown_options ); |
491
|
|
|
} |
492
|
|
|
} |
493
|
|
|
} |
494
|
|
|
} |
495
|
|
|
|
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.