/
media-categories.php
403 lines (326 loc) · 15.1 KB
/
media-categories.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<?php /*
Plugin Name: Media Categories
Plugin URI: http://wordpress.org/extend/plugins/media-categories-2
Description: Allows users to assign categories to media with a clean and simplified, filterable category meta box and use shortcodes to display category galleries
Version: 1.5
Author: Eddie Moya
Author URL: http://eddiemoya.com
*/
require_once(plugin_dir_path(__FILE__) . 'metaboxes/filterable-taxonomy-faux-metabox.php');
require_once(plugin_dir_path(__FILE__) . 'metaboxes/filterable-taxonomy-metabox.php');
class Media_Categories {
public static $version = 1.5;
public static $instances;
public $override_default_gallery;
public $taxonomy;
/**
* While normally run statically, this allows
* @param type $taxonomy
*/
public function __construct($taxonomy, $args = array()) {
// Store each instance of this class (for use when localizing scripts)
$this->taxonomy = $taxonomy;
self::$instances[] = $this;
//Todo: $args should have a default and then be array merged.
if(isset($args['override_default_gallery'])){
$this->override_default_gallery = $args['override_default_gallery'];
}
$this->setup();
}
public function setup(){
global $wp_version;
add_action('init', array(&$this, 'register_media_categories'));
add_action('init', array(&$this, 'taxonomy_gallery_shortcode'));
add_action('init', array(&$this, 'default_gallery_shortcode')); // For backward compatibility only!
// In < 3.5 this is used for the main metabox on media admin pages - because normal metaboxes were not available
// In 3.5 > This is used soley for the Media Modal right rail. Where there is also no normal metabox availability
add_filter('attachment_fields_to_edit', array(new Filterable_Taxonomy_Faux_Metabox($this->taxonomy), 'add_taxonomy_meta_box'), null, 2);
/* Only before WordPress 3.5 */
if( $wp_version < 3.5 ){
// Patch to solve this in 3.5 was accepted @see http://core.trac.wordpress.org/ticket/20765
add_filter('attachment_fields_to_edit', array(__CLASS__, 'get_attachment_fields_to_edit'), 11, 2);
// Loading these in this fashion no longer applies in 3.5 because of new built-in support for taxonomy metaboxes on the editor page.
add_action('admin_enqueue_scripts', array(__CLASS__, 'enqueue_media_categories_scripts'));
add_action('admin_enqueue_scripts', array(__CLASS__, 'enqueue_media_categories_styles') );
} else {
add_filter('admin_menu', array(new Filterable_Taxonomy_Metabox($this->taxonomy), 'add_taxonomy_meta_box'));
add_action('restrict_manage_posts',array($this, 'restrict_manage_attachments'));
add_action('admin_enqueue_scripts', array(__CLASS__, 'enqueue_media_categories_scripts'));
add_action('admin_enqueue_scripts', array(__CLASS__, 'enqueue_media_categories_styles') );
add_action('wp_enqueue_media', array(__CLASS__, 'enqueue_media_categories_scripts'));
add_action('wp_enqueue_media', array(__CLASS__, 'enqueue_media_categories_styles') );
}
}
/**
* Enqueue javascript
*/
function enqueue_media_categories_scripts() {
global $wp_version;
if (is_admin()) {
// Get each instance of this class, and pass each taxonomy in to javascript
foreach (self::$instances as $instance){
$tax[] = apply_filters('mc_taxonomy', $instance->taxonomy);
}
$filename = ($wp_version < 3.5) ? 'media-categories-script-3.4.js' : 'media-categories-script.js';
wp_enqueue_media();
wp_register_script('media_categories_metabox_script', plugins_url($filename, __FILE__));
wp_enqueue_script('media_categories_metabox_script');
wp_localize_script('media_categories_metabox_script', 'taxonomy', $tax);
}
}
/**
*
*/
function enqueue_media_categories_styles() {
global $wp_version;
if (is_admin()) {
$filename = ($wp_version < 3.5) ? 'media-categories-style-3.4.css' : 'media-categories-style.css';
wp_register_style('media_categories_metabox_style', plugins_url($filename , __FILE__));
wp_enqueue_style( 'media_categories_metabox_style');
}
}
/**
* This adds native support for categories to the attachment editor, however
* instead of the standard metabox wordpress only provides a text area wich
* the user would have to type slugs.
*/
function register_media_categories() {
$tax_name = apply_filters('mc_taxonomy', $this->taxonomy);
register_taxonomy_for_object_type($tax_name, 'attachment');
}
function default_gallery_shortcode(){
if($this->override_default_gallery === true){
remove_shortcode('gallery');
add_shortcode('gallery', array(&$this,'gallery_shortcode'));
}
}
function taxonomy_gallery_shortcode(){
add_shortcode('media_gallery', array(&$this,'gallery_shortcode'));
}
/**
* The Gallery shortcode with category parameter.
*
* This implements the functionality of the Gallery Shortcode for displaying
* WordPress images on a post.
*
* Almost Identical to the gallery_shortcode() function in /wp-includes/media.php
* but adds a category parameter to the shortcode.
*
* @since 1.2
* @since WordPress 2.6.0
*
* @param array $attr Attributes of the shortcode.
* @return string HTML content to display gallery.
*/
function gallery_shortcode($attr) {
global $wp_version;
// Could probably just leave it as get_post(), but i'm being lazy and don't feel like testing to be sure - so im putting in this logic to avoid any possible problem.
if($wp_version < 3.5){
global $post;
} else {
$post = get_post();
}
static $instance = 0;
$instance++;
if ( ! empty( $attr['ids'] ) ) {
// 'ids' is explicitly ordered, unless you specify otherwise.
if ( empty( $attr['orderby'] ) )
$attr['orderby'] = 'post__in';
$attr['include'] = $attr['ids'];
}
// Allow plugins/themes to override the default gallery template.
$output = apply_filters('post_gallery', '', $attr);
if ( $output != '' )
return $output;
// We're trusting author input, so let's at least make sure it looks like a valid orderby statement
if ( isset( $attr['orderby'] ) ) {
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
if ( !$attr['orderby'] )
unset( $attr['orderby'] );
}
$mc_tax = apply_filters('mc_taxonomy', $this->taxonomy);
extract(shortcode_atts(array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post->ID,
'itemtag' => 'dl',
'icontag' => 'dt',
'captiontag' => 'dd',
'columns' => 3,
'size' => 'thumbnail',
'include' => '',
'exclude' => '',
$mc_tax => ''
), $attr));
$id = intval($id);
if ( 'RAND' == $order )
$orderby = 'none';
$tax_query = array();
if( !empty($$mc_tax) ){
//Split the categories on commas into an array of categories
$term = explode(',',${$mc_tax});
$term_field = (is_numeric($term)) ? 'id' : 'slug';
$tax_query = array(
'tax_query' => array(
array(
'taxonomy' => $mc_tax,
'field' => $term_field,
'terms' => $term
)
)
);
if(!isset($attr['id']))
$id = '';
}
if ( !empty($include) ) {
//$include = preg_replace( '/[^0-9,]+/', '', $include ); see: http://core.trac.wordpress.org/ticket/21827
$_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) + $tax_query );
$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
} elseif ( !empty($exclude) ) {
//$exclude = preg_replace( '/[^0-9,]+/', '', $exclude ); see: http://core.trac.wordpress.org/ticket/21827
$attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) + $tax_query );
} else {
$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) + $tax_query );
}
if ( empty($attachments) )
return '';
if ( is_feed() ) {
$output = "\n";
foreach ( $attachments as $att_id => $attachment )
$output .= wp_get_attachment_link($att_id, $size, true) . "\n";
return $output;
}
$itemtag = tag_escape($itemtag);
$captiontag = tag_escape($captiontag);
$columns = intval($columns);
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
$float = is_rtl() ? 'right' : 'left';
$selector = "gallery-{$instance}";
$gallery_style = $gallery_div = '';
if ( apply_filters( 'use_default_gallery_style', true ) )
$gallery_style = "
<style type='text/css'>
#{$selector} {
margin: auto;
}
#{$selector} .gallery-item {
float: {$float};
margin-top: 10px;
text-align: center;
width: {$itemwidth}%;
}
#{$selector} img {
border: 2px solid #cfcfcf;
}
#{$selector} .gallery-caption {
margin-left: 0;
}
</style>
<!-- see gallery_shortcode() in wp-includes/media.php -->";
$size_class = sanitize_html_class( $size );
$gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
$output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );
$i = 0;
foreach ( $attachments as $id => $attachment ) {
$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
$output .= "<{$itemtag} class='gallery-item'>";
$output .= "
<{$icontag} class='gallery-icon'>
$link
</{$icontag}>";
if ( $captiontag && trim($attachment->post_excerpt) ) {
$output .= "
<{$captiontag} class='wp-caption-text gallery-caption'>
" . wptexturize($attachment->post_excerpt) . "
</{$captiontag}>";
}
$output .= "</{$itemtag}>";
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= '<br style="clear: both" />';
}
$output .= "
<br style='clear: both;' />
</div>\n";
return $output;
}
/**
* This function serves to work around the problem explained in trac ticket 20765 and reported
* to me in the plugin directory support forum on WordPress.org.
*
* It is an exact duplication of code the `get_attachment_fields_to_edit()` function,
* and its only purpose is to change the output of terms in attachments so that they used term slugs
* rather than names.
*
* NOTE: This is no longer necessary in WordPress 3.5 - the patch fixing this problem was committed.
*
* @link http://core.trac.wordpress.org/ticket/20765
* @link http://wordpress.org/support/topic/media-categories-2-not-saving-correctly-when-two-categories-with-same-name
* @see /wp-admin/includes/media.php:get_attachemt_fields_to_edit()
*
* @param type $form_fields
* @param type $post
* @return type
*/
function get_attachment_fields_to_edit($form_fields, $post) {
foreach (get_attachment_taxonomies($post) as $taxonomy) {
$t = (array) get_taxonomy($taxonomy);
if (!$t['public'])
continue;
if (empty($t['label']))
$t['label'] = $taxonomy;
if (empty($t['args']))
$t['args'] = array();
$terms = get_object_term_cache($post->ID, $taxonomy);
if (empty($terms))
$terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
$values = array();
foreach ($terms as $term)
$values[] = $term->slug;
$t['value'] = join(', ', $values);
$form_fields[$taxonomy] = $t;
}
return $form_fields;
}
/**
* This method generates a dropdown to filter items on the Media Library page.
**/
public function restrict_manage_attachments() {
global $pagenow;
global $wp_query;
require_once(plugin_dir_path(__FILE__) . 'walkers/mc-walker-taxonomy-dropdown.php');
$terms = get_terms($this->taxonomy);
$walker = new SH_Walker_TaxonomyDropdown();
if ('category' == $this->taxonomy) {
$name = 'cat';
$value = 'id';
} else {
$name = $this->taxonomy;
$value = 'slug';
}
if ($pagenow=='upload.php' && !empty($terms)) {
$taxonomy = get_taxonomy($this->taxonomy);
$dropdown_args = array(
'show_option_all' => __("Show All {$taxonomy->label}"),
'taxonomy' => $this->taxonomy,
'name' => $name,
'orderby' => 'name',
'hierarchical' => true,
'depth' => 3,
'show_count' => true, // Show # listings in parens
'hide_empty' => true, // Don't show businesses w/o listings
'hide_if_empty' => true,
'walker' => $walker,
'value' => $value
);
if (isset($wp_query->query[$this->taxonomy])){
$dropdown_args['selected'] = $wp_query->query[$this->taxonomy];
}
return wp_dropdown_categories($dropdown_args);
}
}
}
global $mc_media_categories;
$mc_media_categories = new Media_Categories('category');