|
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
|
|
|
/** |
|
10
|
|
|
* Class Constructor |
|
11
|
|
|
*/function __construct() { |
|
12
|
|
|
add_action( 'init', array( $this, 'add_taxonomies' ) ); |
|
13
|
|
|
|
|
14
|
|
|
if ( is_admin() ) { |
|
15
|
|
|
add_action( 'admin_menu', array( $this, 'add_menu_items' ), 1 ); |
|
16
|
|
|
add_filter( 'parent_file', array( $this, 'set_current_menu' ) ); |
|
17
|
|
|
add_filter( 'manage_media_columns', array( $this, 'change_attachment_column_names' ) ); |
|
18
|
|
|
add_filter( 'manage_edit-foogallery_attachment_tag_columns', array( $this, 'clean_column_names' ), 999 ); |
|
19
|
|
|
add_filter( 'manage_edit-foogallery_attachment_collection_columns', array( $this, 'clean_column_names' ), 999 ); |
|
20
|
|
|
add_filter( 'foogallery_attachment_add_fields', array( $this, 'remove_taxonomy_fields') ); |
|
21
|
|
|
add_action( 'restrict_manage_posts', array( $this, 'add_collection_filter' ) ); |
|
22
|
|
|
|
|
23
|
|
|
add_filter( 'foogallery_attachment_custom_fields', array( $this, 'add_media_tag_field' ) ); |
|
24
|
|
|
add_filter( 'foogallery_attachment_field_taxonomy_tag', array( $this, 'customize_media_tag_field'), 10, 2 ); |
|
25
|
|
|
add_filter( 'foogallery_attachment_save_field_taxonomy_tag', array( $this, 'save_media_tag_field' ), 10, 4 ); |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
function change_attachment_column_names( $columns ) { |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
if ( array_key_exists( 'taxonomy-foogallery_attachment_collection', $columns ) ) { |
|
35
|
|
|
$columns['taxonomy-foogallery_attachment_collection'] = __('Collections', 'foogallery'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return $columns; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Clean up the taxonomy columns |
|
43
|
|
|
* |
|
44
|
|
|
* @param $columns |
|
45
|
|
|
* @return mixed |
|
46
|
|
|
*/ |
|
47
|
|
|
function clean_column_names( $columns ) { |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
//cleanup wpseo columns! |
|
50
|
|
|
if ( array_key_exists( 'wpseo_score', $columns ) ) { |
|
51
|
|
|
unset( $columns['wpseo_score'] ); |
|
52
|
|
|
} |
|
53
|
|
|
if ( array_key_exists( 'wpseo_score_readability', $columns ) ) { |
|
54
|
|
|
unset( $columns['wpseo_score_readability'] ); |
|
55
|
|
|
} |
|
56
|
|
|
return $columns; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Add the menu items under the FooGalleru main menu |
|
61
|
|
|
*/ |
|
62
|
|
|
function add_menu_items() { |
|
|
|
|
|
|
63
|
|
|
foogallery_add_submenu_page( |
|
64
|
|
|
__( 'Media Tags', 'foogallery' ), |
|
65
|
|
|
'manage_options', |
|
66
|
|
|
'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_TAG . '&post_type=' . FOOGALLERY_CPT_GALLERY, |
|
67
|
|
|
null |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
|
|
foogallery_add_submenu_page( |
|
71
|
|
|
__( 'Media Collections', 'foogallery' ), |
|
72
|
|
|
'manage_options', |
|
73
|
|
|
'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION . '&post_type=' . FOOGALLERY_CPT_GALLERY, |
|
74
|
|
|
null |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Make sure the tqaxonomy menu items are highlighted |
|
80
|
|
|
* @param $parent_file |
|
81
|
|
|
* @return mixed |
|
82
|
|
|
*/ |
|
83
|
|
|
function set_current_menu( $parent_file ) { |
|
|
|
|
|
|
84
|
|
|
global $submenu_file, $current_screen, $pagenow; |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
if ( $current_screen->post_type == FOOGALLERY_CPT_GALLERY ) { |
|
87
|
|
|
|
|
88
|
|
|
if ( 'edit-foogallery_attachment_tag' === $current_screen->id ) { |
|
89
|
|
|
$submenu_file = 'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_TAG . '&post_type=' . FOOGALLERY_CPT_GALLERY; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if ( 'edit-foogallery_attachment_collection' === $current_screen->id ) { |
|
93
|
|
|
$submenu_file = 'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION . '&post_type=' . FOOGALLERY_CPT_GALLERY; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
return $parent_file; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Register the taxonomies for attachments |
|
101
|
|
|
*/ |
|
102
|
|
|
function add_taxonomies() { |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
$tag_args = array( |
|
105
|
|
|
'labels' => array( |
|
106
|
|
|
'name' => __( 'Media Tags', 'foogallery' ), |
|
107
|
|
|
'singular_name' => __( 'Tag', 'foogallery' ), |
|
108
|
|
|
'search_items' => __( 'Search Tags', 'foogallery' ), |
|
109
|
|
|
'all_items' => __( 'All Tags', 'foogallery' ), |
|
110
|
|
|
'parent_item' => __( 'Parent Tag', 'foogallery' ), |
|
111
|
|
|
'parent_item_colon' => __( 'Parent Tag:', 'foogallery' ), |
|
112
|
|
|
'edit_item' => __( 'Edit Tag', 'foogallery' ), |
|
113
|
|
|
'update_item' => __( 'Update Tag', 'foogallery' ), |
|
114
|
|
|
'add_new_item' => __( 'Add New Tag', 'foogallery' ), |
|
115
|
|
|
'new_item_name' => __( 'New Tag Name', 'foogallery' ), |
|
116
|
|
|
'menu_name' => __( 'Media Tags', 'foogallery' ) |
|
117
|
|
|
), |
|
118
|
|
|
'hierarchical' => false, |
|
119
|
|
|
'query_var' => true, |
|
120
|
|
|
'rewrite' => false, |
|
121
|
|
|
'show_admin_column' => false, |
|
122
|
|
|
'show_in_menu' => false, |
|
123
|
|
|
'update_count_callback' => '_update_post_term_count' //array( $this, 'update_taxonomy_tag_count' ) |
|
|
|
|
|
|
124
|
|
|
); |
|
125
|
|
|
|
|
126
|
|
|
register_taxonomy( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, 'attachment', $tag_args ); |
|
127
|
|
|
|
|
128
|
|
|
$collection_args = array( |
|
129
|
|
|
'labels' => array( |
|
130
|
|
|
'name' => __( 'Media Collections', 'foogallery' ), |
|
131
|
|
|
'singular_name' => __( 'Collection', 'foogallery' ), |
|
132
|
|
|
'search_items' => __( 'Search Collections', 'foogallery' ), |
|
133
|
|
|
'all_items' => __( 'All Collections', 'foogallery' ), |
|
134
|
|
|
'parent_item' => __( 'Parent Collection', 'foogallery' ), |
|
135
|
|
|
'parent_item_colon' => __( 'Parent Collection:', 'foogallery' ), |
|
136
|
|
|
'edit_item' => __( 'Edit Collection', 'foogallery' ), |
|
137
|
|
|
'update_item' => __( 'Update Collection', 'foogallery' ), |
|
138
|
|
|
'add_new_item' => __( 'Add New Collection', 'foogallery' ), |
|
139
|
|
|
'new_item_name' => __( 'New Collection Name', 'foogallery' ), |
|
140
|
|
|
'menu_name' => __( 'Media Collections', 'foogallery' ) |
|
141
|
|
|
), |
|
142
|
|
|
'hierarchical' => true, |
|
143
|
|
|
'query_var' => true, |
|
144
|
|
|
'rewrite' => false, |
|
145
|
|
|
'show_admin_column' => true, |
|
146
|
|
|
'show_in_menu' => false, |
|
147
|
|
|
'update_count_callback' => '_update_post_term_count' |
|
148
|
|
|
); |
|
149
|
|
|
|
|
150
|
|
|
register_taxonomy( FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION, 'attachment', $collection_args ); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Function for updating the 'tag' taxonomy count. What this does is update the count of a specific term |
|
155
|
|
|
* by the number of attachments that have been given the term. |
|
156
|
|
|
* We're just updating the count with no specifics for simplicity. |
|
157
|
|
|
* |
|
158
|
|
|
* See the _update_post_term_count() function in WordPress for more info. |
|
159
|
|
|
* |
|
160
|
|
|
* @param array $terms List of Term taxonomy IDs |
|
161
|
|
|
* @param object $taxonomy Current taxonomy object of terms |
|
162
|
|
|
*/ |
|
163
|
|
|
function update_taxonomy_tag_count( $terms, $taxonomy ) { |
|
|
|
|
|
|
164
|
|
|
global $wpdb; |
|
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
foreach ( (array) $terms as $term ) { |
|
167
|
|
|
|
|
168
|
|
|
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) ); |
|
169
|
|
|
|
|
170
|
|
|
do_action( 'edit_term_taxonomy', $term, $taxonomy ); |
|
171
|
|
|
$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); |
|
172
|
|
|
do_action( 'edited_term_taxonomy', $term, $taxonomy ); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Add a new tag field to the attachments |
|
178
|
|
|
* |
|
179
|
|
|
* @param $fields array All fields that will be added to the media modal |
|
180
|
|
|
* |
|
181
|
|
|
* @return mixed |
|
182
|
|
|
*/ |
|
183
|
|
|
function add_media_tag_field( $fields ) { |
|
|
|
|
|
|
184
|
|
|
$args = array( |
|
185
|
|
|
'orderby' => 'name', |
|
186
|
|
|
'order' => 'ASC', |
|
187
|
|
|
'hide_empty' => false |
|
188
|
|
|
); |
|
189
|
|
|
|
|
190
|
|
|
//pull all terms |
|
191
|
|
|
$terms = get_terms( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, $args ); |
|
192
|
|
|
|
|
193
|
|
|
$media_tags = array(); |
|
194
|
|
|
|
|
195
|
|
|
foreach ( $terms as $term ) { |
|
196
|
|
|
$media_tags[ $term->term_id ] = $term->name; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
$fields[FOOGALLERY_ATTACHMENT_TAXONOMY_TAG] = array( |
|
200
|
|
|
'label' => __( 'Tags', 'foogallery' ), |
|
201
|
|
|
'input' => FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, |
|
202
|
|
|
'helps' => __( 'Tag your attachments', 'foogallery' ), |
|
203
|
|
|
'options' => $media_tags, |
|
204
|
|
|
'exclusions' => array() |
|
205
|
|
|
); |
|
206
|
|
|
|
|
207
|
|
|
return $fields; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Remove the automatically added attachments fields |
|
212
|
|
|
* @param $fields |
|
213
|
|
|
* |
|
214
|
|
|
* @return mixed |
|
215
|
|
|
*/ |
|
216
|
|
|
function remove_taxonomy_fields( $fields ) { |
|
|
|
|
|
|
217
|
|
|
if ( array_key_exists( FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, $fields ) ) { |
|
218
|
|
|
unset( $fields[FOOGALLERY_ATTACHMENT_TAXONOMY_TAG] ); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
if ( array_key_exists( FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION, $fields ) ) { |
|
222
|
|
|
unset( $fields[FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION] ); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
return $fields; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Customize the media tag field to make sure we output a checkboxlist |
|
230
|
|
|
* @param $field_values |
|
231
|
|
|
* |
|
232
|
|
|
* @return mixed |
|
233
|
|
|
*/ |
|
234
|
|
|
function customize_media_tag_field( $field_values, $post_id ) { |
|
235
|
|
|
|
|
236
|
|
|
$media_tags = array(); |
|
237
|
|
|
|
|
238
|
|
|
//get the terms linked to the attachment |
|
239
|
|
|
$terms = get_the_terms( $post_id, FOOGALLERY_ATTACHMENT_TAXONOMY_TAG ); |
|
240
|
|
|
if ( $terms && ! is_wp_error( $terms ) ) { |
|
241
|
|
|
foreach ( $terms as $term ) { |
|
242
|
|
|
$media_tags[ $term->term_id ] = $term->name; |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
//set to html |
|
247
|
|
|
$field_values['input'] = 'html'; |
|
248
|
|
|
|
|
249
|
|
|
$html = ''; |
|
250
|
|
|
$i = 0; |
|
251
|
|
|
|
|
252
|
|
|
if ( ! empty( $field_values['options'] ) ) { |
|
253
|
|
|
|
|
254
|
|
|
foreach ( $field_values['options'] as $k => $v ) { |
|
255
|
|
|
if ( array_key_exists( $k, $media_tags ) ) { |
|
256
|
|
|
$checked = ' checked="checked"'; |
|
257
|
|
|
} else { |
|
258
|
|
|
$checked = ''; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
$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> '; |
|
262
|
|
|
$i++; |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
if ( 0 === $i ) { |
|
267
|
|
|
$html .= __( 'No Tags Available!', 'foogallery' ); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
$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>'; |
|
271
|
|
|
|
|
272
|
|
|
$html .= '<br /><a target="_blank" href="' . admin_url( 'edit-tags.php?taxonomy=' . FOOGALLERY_ATTACHMENT_TAXONOMY_TAG . '&post_type=attachment' ) . '">' . __( 'Manage Tags', 'foogallery' ) . '</a>'; |
|
273
|
|
|
|
|
274
|
|
|
$field_values['value'] = ''; |
|
275
|
|
|
$field_values['html'] = $html; |
|
276
|
|
|
|
|
277
|
|
|
return $field_values; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Save the tags for the attachment |
|
282
|
|
|
* |
|
283
|
|
|
* @param $field |
|
284
|
|
|
* @param $values |
|
285
|
|
|
* @param $post |
|
286
|
|
|
* @param $attachment |
|
287
|
|
|
*/ |
|
288
|
|
|
function save_media_tag_field($field, $values, $post, $attachment) { |
|
289
|
|
|
$post_id = $post['ID']; |
|
290
|
|
|
|
|
291
|
|
|
//first clear any tags for the post |
|
292
|
|
|
wp_delete_object_term_relationships( $post_id, FOOGALLERY_ATTACHMENT_TAXONOMY_TAG ); |
|
293
|
|
|
|
|
294
|
|
|
$tag_ids = $attachment[ $field ]; |
|
295
|
|
|
|
|
296
|
|
|
if ( !empty( $tag_ids ) ) { |
|
297
|
|
|
//clean tag ids |
|
298
|
|
|
$tag_ids = array_keys( $tag_ids ); |
|
299
|
|
|
$tag_ids = array_map( 'intval', $tag_ids ); |
|
300
|
|
|
$tag_ids = array_unique( $tag_ids ); |
|
301
|
|
|
|
|
302
|
|
|
$term_taxonomy_ids = wp_set_object_terms( $post_id, $tag_ids, FOOGALLERY_ATTACHMENT_TAXONOMY_TAG ); |
|
303
|
|
|
|
|
304
|
|
|
if ( is_wp_error( $term_taxonomy_ids ) ) { |
|
305
|
|
|
// There was an error somewhere and the terms couldn't be set. |
|
306
|
|
|
$post['errors'][ $field ]['errors'][] = __( 'Error saving the tags for the attachment!', 'foogallery' ); |
|
307
|
|
|
} |
|
308
|
|
|
} |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
|
|
312
|
|
|
/*** |
|
313
|
|
|
* |
|
314
|
|
|
* Add a tag filter to the attachments listing page |
|
315
|
|
|
*/ |
|
316
|
|
|
function add_collection_filter() { |
|
|
|
|
|
|
317
|
|
|
global $pagenow; |
|
318
|
|
|
if ( 'upload.php' == $pagenow ) { |
|
319
|
|
|
|
|
320
|
|
|
$dropdown_options = array( |
|
321
|
|
|
'taxonomy' => FOOGALLERY_ATTACHMENT_TAXONOMY_COLLECTION, |
|
322
|
|
|
'show_option_all' => __( 'All Collections' ), |
|
323
|
|
|
'hide_empty' => false, |
|
324
|
|
|
'hierarchical' => true, |
|
325
|
|
|
'orderby' => 'name', |
|
326
|
|
|
'show_count' => true, |
|
327
|
|
|
'walker' => new foogallery_walker_category_dropdown(), |
|
328
|
|
|
'value' => 'slug' |
|
329
|
|
|
); |
|
330
|
|
|
|
|
331
|
|
|
wp_dropdown_categories( $dropdown_options ); |
|
332
|
|
|
} |
|
333
|
|
|
} |
|
334
|
|
|
} |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** Custom walker for wp_dropdown_categories, based on https://gist.github.com/stephenh1988/2902509 */ |
|
338
|
|
|
class foogallery_walker_category_dropdown extends Walker_CategoryDropdown{ |
|
339
|
|
|
|
|
340
|
|
|
function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) { |
|
341
|
|
|
$pad = str_repeat( ' ', $depth * 3 ); |
|
342
|
|
|
$cat_name = apply_filters( 'list_cats', $category->name, $category ); |
|
343
|
|
|
|
|
344
|
|
|
if( ! isset( $args['value'] ) ) { |
|
345
|
|
|
$args['value'] = ( $category->taxonomy != 'category' ? 'slug' : 'id' ); |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
$value = ( $args['value']=='slug' ? $category->slug : $category->term_id ); |
|
349
|
|
|
if ( 0 == $args['selected'] && isset( $_GET['category_media'] ) && '' != $_GET['category_media'] ) { |
|
350
|
|
|
$args['selected'] = $_GET['category_media']; |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
$output .= '<option class="level-' . $depth . '" value="' . $value . '"'; |
|
354
|
|
|
if ( $value === (string) $args['selected'] ) { |
|
355
|
|
|
$output .= ' selected="selected"'; |
|
356
|
|
|
} |
|
357
|
|
|
$output .= '>'; |
|
358
|
|
|
$output .= $pad . $cat_name; |
|
359
|
|
|
if ( $args['show_count'] ) |
|
360
|
|
|
$output .= ' (' . $category->count . ')'; |
|
361
|
|
|
|
|
362
|
|
|
$output .= "</option>\n"; |
|
363
|
|
|
} |
|
364
|
|
|
} |
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.