Media Library Categories - Version 1.4.12

Version Description

  • Add category option to the default WordPress shortcode gallery Read the FAQ for howto
  • Improve code styling to match WordPress code standard even more strictly
Download this release

Release Info

Developer jeffrey-wp
Plugin Icon 128x128 Media Library Categories
Version 1.4.12
Comparing to
See all releases

Code changes from version 1.4.11 to 1.4.12

Files changed (2) hide show
  1. index.php +137 -31
  2. readme.txt +17 -2
index.php CHANGED
@@ -3,7 +3,7 @@
3
  * Plugin Name: Media Library Categories
4
  * Plugin URI: http://wordpress.org/plugins/wp-media-library-categories/
5
  * Description: Adds the ability to use categories in the media library.
6
- * Version: 1.4.11
7
  * Author: Jeffrey-WP
8
  * Author URI: http://codecanyon.net/user/jeffrey-wp/?ref=jeffrey-wp
9
  */
@@ -18,11 +18,11 @@ function wpmediacategory_update_count_callback( $terms = array(), $taxonomy = 'c
18
  $taxonomy = apply_filters( 'wpmediacategory_taxonomy', $taxonomy );
19
 
20
  // select id & count from taxonomy
21
- $rsCount = $wpdb->get_results("SELECT term_taxonomy_id, MAX(total) AS total FROM ((
22
  SELECT tt.term_taxonomy_id, COUNT(*) AS total FROM $wpdb->term_relationships tr, $wpdb->term_taxonomy tt WHERE tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = '" . $taxonomy . "' GROUP BY tt.term_taxonomy_id
23
  ) UNION ALL (
24
  SELECT term_taxonomy_id, 0 AS total FROM $wpdb->term_taxonomy WHERE taxonomy = '" . $taxonomy . "'
25
- )) AS unioncount GROUP BY term_taxonomy_id");
26
  // update all count values from taxonomy
27
  foreach ( $rsCount as $rowCount ) {
28
  $wpdb->update( $wpdb->term_taxonomy, array( 'count' => $rowCount->total ), array( 'term_taxonomy_id' => $rowCount->term_taxonomy_id ) );
@@ -40,8 +40,8 @@ function wpmediacategory_init() {
40
 
41
  if ( $taxonomy != 'category' ) {
42
  $args = array(
43
- 'hierarchical' => true, // hierarchical: true = display as categories, false = display as tags
44
- 'show_admin_column' => true,
45
  'update_count_callback' => 'wpmediacategory_update_count_callback'
46
  );
47
  register_taxonomy( $taxonomy, array( 'attachment' ), $args );
@@ -71,6 +71,108 @@ function wpmediacategory_change_category_update_count_callback() {
71
  }
72
  add_action( 'init', 'wpmediacategory_change_category_update_count_callback', 100 );
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  // load code that is only needed in the admin section
75
  if ( is_admin() ) {
76
 
@@ -104,20 +206,20 @@ if ( is_admin() ) {
104
  $pad = str_repeat( ' ', $depth * 3 );
105
  $cat_name = apply_filters( 'list_cats', $category->name, $category );
106
 
107
- if( !isset($args['value']) ) {
108
  $args['value'] = ( $category->taxonomy != 'category' ? 'slug' : 'id' );
109
  }
110
 
111
  $value = ( $args['value']=='slug' ? $category->slug : $category->term_id );
112
 
113
- $output .= "\t<option class=\"level-$depth\" value=\"".$value."\"";
114
  if ( $value === (string) $args['selected'] ) {
115
  $output .= ' selected="selected"';
116
  }
117
  $output .= '>';
118
- $output .= $pad.$cat_name;
119
  if ( $args['show_count'] )
120
- $output .= '&nbsp;&nbsp;('. $category->count .')';
121
 
122
  $output .= "</option>\n";
123
  }
@@ -180,14 +282,14 @@ if ( is_admin() ) {
180
  // add categories
181
  foreach ( $terms as $term ) {
182
  $sTxtAdd = esc_js( __( 'Add' ) . ': ' . $term->name );
183
- echo "jQuery('<option>').val('wpmediacategory_add_" . $term->term_taxonomy_id . "').text('". $sTxtAdd . "').appendTo('#wpmediacategory_optgroup1');";
184
- echo "jQuery('<option>').val('wpmediacategory_add_" . $term->term_taxonomy_id . "').text('". $sTxtAdd . "').appendTo('#wpmediacategory_optgroup2');";
185
  }
186
  // remove categories
187
  foreach ( $terms as $term ) {
188
  $sTxtRemove = esc_js( __( 'Remove' ) . ': ' . $term->name );
189
- echo "jQuery('<option>').val('wpmediacategory_remove_" . $term->term_taxonomy_id . "').text('". $sTxtRemove . "').appendTo('#wpmediacategory_optgroup1');";
190
- echo "jQuery('<option>').val('wpmediacategory_remove_" . $term->term_taxonomy_id . "').text('". $sTxtRemove . "').appendTo('#wpmediacategory_optgroup2');";
191
  }
192
  // remove all categories
193
  echo "jQuery('<option>').val('wpmediacategory_remove_0').text('" . esc_js( __( 'Delete all' ) ) . "').appendTo('#wpmediacategory_optgroup1');";
@@ -204,23 +306,27 @@ if ( is_admin() ) {
204
  function wpmediacategory_custom_bulk_action() {
205
  global $wpdb;
206
 
207
- if ( ! isset( $_REQUEST['action'] ) )
208
  return;
 
209
 
210
  // is it a category?
211
- $sAction = ($_REQUEST['action'] != -1) ? $_REQUEST['action'] : $_REQUEST['action2'];
212
- if ( substr( $sAction, 0, 16 ) != 'wpmediacategory_' )
213
  return;
 
214
 
215
  // security check
216
- check_admin_referer('bulk-media');
217
 
218
  // make sure ids are submitted. depending on the resource type, this may be 'media' or 'post'
219
- if(isset($_REQUEST['media'])) {
220
- $post_ids = array_map('intval', $_REQUEST['media']);
221
  }
222
 
223
- if(empty($post_ids)) return;
 
 
224
 
225
  $sendback = admin_url( "upload.php?editCategory=1" );
226
 
@@ -241,13 +347,13 @@ if ( is_admin() ) {
241
 
242
  foreach( $post_ids as $post_id ) {
243
 
244
- if ( is_numeric( str_replace('wpmediacategory_add_', '', $sAction) ) ) {
245
- $nCategory = str_replace('wpmediacategory_add_', '', $sAction);
246
 
247
  // update or insert category
248
  $wpdb->replace( $wpdb->term_relationships,
249
  array(
250
- 'object_id' => $post_id,
251
  'term_taxonomy_id' => $nCategory
252
  ),
253
  array(
@@ -256,11 +362,11 @@ if ( is_admin() ) {
256
  )
257
  );
258
 
259
- } else if ( is_numeric( str_replace('wpmediacategory_remove_', '', $sAction) ) ) {
260
- $nCategory = str_replace('wpmediacategory_remove_', '', $sAction);
261
 
262
  // remove all categories
263
- if ($nCategory == 0) {
264
  $wpdb->delete( $wpdb->term_relationships,
265
  array(
266
  'object_id' => $post_id
@@ -273,7 +379,7 @@ if ( is_admin() ) {
273
  } else {
274
  $wpdb->delete( $wpdb->term_relationships,
275
  array(
276
- 'object_id' => $post_id,
277
  'term_taxonomy_id' => $nCategory
278
  ),
279
  array(
@@ -298,8 +404,8 @@ if ( is_admin() ) {
298
  function wpmediacategory_custom_bulk_admin_notices() {
299
  global $post_type, $pagenow;
300
 
301
- if($pagenow == 'upload.php' && $post_type == 'attachment' && isset($_GET['editCategory'])) {
302
- echo '<div class="updated"><p>' . __('Settings saved.') . '</p></div>';
303
  }
304
  }
305
  add_action( 'admin_notices', 'wpmediacategory_custom_bulk_admin_notices' );
@@ -313,8 +419,8 @@ if ( is_admin() ) {
313
  $taxonomy = apply_filters( 'wpmediacategory_taxonomy', $taxonomy );
314
  return array_merge(
315
  array(
316
- 'settings' => '<a href="' . get_bloginfo( 'wpurl' ) . '/wp-admin/edit-tags.php?taxonomy=' . $taxonomy . '&amp;post_type=attachment">' . __('Categories') . '</a>',
317
- 'premium' => '<a href="http://codecanyon.net/item/media-library-categories-premium/6691290?ref=jeffrey-wp">' . __('Get Premium Version') . '</a>'
318
  ),
319
  $links
320
  );
3
  * Plugin Name: Media Library Categories
4
  * Plugin URI: http://wordpress.org/plugins/wp-media-library-categories/
5
  * Description: Adds the ability to use categories in the media library.
6
+ * Version: 1.4.12
7
  * Author: Jeffrey-WP
8
  * Author URI: http://codecanyon.net/user/jeffrey-wp/?ref=jeffrey-wp
9
  */
18
  $taxonomy = apply_filters( 'wpmediacategory_taxonomy', $taxonomy );
19
 
20
  // select id & count from taxonomy
21
+ $rsCount = $wpdb->get_results( "SELECT term_taxonomy_id, MAX(total) AS total FROM ((
22
  SELECT tt.term_taxonomy_id, COUNT(*) AS total FROM $wpdb->term_relationships tr, $wpdb->term_taxonomy tt WHERE tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = '" . $taxonomy . "' GROUP BY tt.term_taxonomy_id
23
  ) UNION ALL (
24
  SELECT term_taxonomy_id, 0 AS total FROM $wpdb->term_taxonomy WHERE taxonomy = '" . $taxonomy . "'
25
+ )) AS unioncount GROUP BY term_taxonomy_id" );
26
  // update all count values from taxonomy
27
  foreach ( $rsCount as $rowCount ) {
28
  $wpdb->update( $wpdb->term_taxonomy, array( 'count' => $rowCount->total ), array( 'term_taxonomy_id' => $rowCount->term_taxonomy_id ) );
40
 
41
  if ( $taxonomy != 'category' ) {
42
  $args = array(
43
+ 'hierarchical' => true, // hierarchical: true = display as categories, false = display as tags
44
+ 'show_admin_column' => true,
45
  'update_count_callback' => 'wpmediacategory_update_count_callback'
46
  );
47
  register_taxonomy( $taxonomy, array( 'attachment' ), $args );
71
  }
72
  add_action( 'init', 'wpmediacategory_change_category_update_count_callback', 100 );
73
 
74
+
75
+ /** custom gallery shortcode */
76
+ add_shortcode( 'gallery', 'wpmediacategory_custom_gallery_shortcode' );
77
+ function wpmediacategory_custom_gallery_shortcode( $atts ) {
78
+
79
+ if ( isset( $atts['category'] ) ) {
80
+
81
+ // Default taxonomy
82
+ $taxonomy = 'category';
83
+ // Add filter to change the default taxonomy
84
+ $taxonomy = apply_filters( 'wpmediacategory_taxonomy', $taxonomy );
85
+
86
+ $category = $atts['category'];
87
+
88
+ // category slug?
89
+ if ( ! is_numeric( $category ) ) {
90
+
91
+ if ( $taxonomy != 'category' ) {
92
+
93
+ $term = get_term_by( 'slug', $category, $taxonomy );
94
+ if ( false !== $term ) {
95
+ $category = $term->term_id;
96
+ } else {
97
+ // not existing category slug
98
+ $category = '';
99
+ }
100
+
101
+ } else {
102
+
103
+ $categoryObject = get_category_by_slug( $category );
104
+ if ( false !== $categoryObject ) {
105
+ $category = $categoryObject->term_id;
106
+ } else {
107
+ // not existing category slug
108
+ $category = '';
109
+ }
110
+ }
111
+
112
+ }
113
+
114
+ if ( $category != '' ) {
115
+
116
+ $ids_new = array();
117
+
118
+ if ( $taxonomy != 'category' ) {
119
+
120
+ $args = array(
121
+ 'post_type' => 'attachment',
122
+ 'numberposts' => -1,
123
+ 'post_status' => null,
124
+ 'tax_query' => array(
125
+ array(
126
+ 'taxonomy' => $taxonomy,
127
+ 'field' => 'id',
128
+ 'terms' => $category
129
+ )
130
+ )
131
+ );
132
+
133
+ } else {
134
+
135
+ $args = array(
136
+ 'post_type' => 'attachment',
137
+ 'numberposts' => -1,
138
+ 'post_status' => null,
139
+ 'category' => $category
140
+ );
141
+
142
+ }
143
+ $attachments = get_posts( $args );
144
+
145
+ if ( ! empty( $attachments ) ) {
146
+
147
+ // ids attribute already present?
148
+ if ( isset( $atts['ids'] ) ) {
149
+ $ids_old = explode( ',', $atts['ids'] );
150
+ foreach ( $attachments as $attachment ) {
151
+ // preserve id if in the selected category
152
+ if ( in_array( $attachment->ID, $ids_old ) ) {
153
+ $ids_new[] = $attachment->ID;
154
+ }
155
+ }
156
+ } else {
157
+ foreach ( $attachments as $attachment ) {
158
+ $ids_new[] = $attachment->ID;
159
+ }
160
+ }
161
+
162
+ $atts['ids'] = $ids_new;
163
+ } else {
164
+ $atts['ids'] = -1; // don't display images if category is empty
165
+ }
166
+
167
+ }
168
+
169
+ }
170
+
171
+ // call the wordpress shortcode function
172
+ return gallery_shortcode( $atts );
173
+ }
174
+
175
+
176
  // load code that is only needed in the admin section
177
  if ( is_admin() ) {
178
 
206
  $pad = str_repeat( '&nbsp;', $depth * 3 );
207
  $cat_name = apply_filters( 'list_cats', $category->name, $category );
208
 
209
+ if( ! isset( $args['value'] ) ) {
210
  $args['value'] = ( $category->taxonomy != 'category' ? 'slug' : 'id' );
211
  }
212
 
213
  $value = ( $args['value']=='slug' ? $category->slug : $category->term_id );
214
 
215
+ $output .= '<option class="level-' . $depth . '" value="' . $value . '"';
216
  if ( $value === (string) $args['selected'] ) {
217
  $output .= ' selected="selected"';
218
  }
219
  $output .= '>';
220
+ $output .= $pad . $cat_name;
221
  if ( $args['show_count'] )
222
+ $output .= '&nbsp;&nbsp;(' . $category->count . ')';
223
 
224
  $output .= "</option>\n";
225
  }
282
  // add categories
283
  foreach ( $terms as $term ) {
284
  $sTxtAdd = esc_js( __( 'Add' ) . ': ' . $term->name );
285
+ echo "jQuery('<option>').val('wpmediacategory_add_" . $term->term_taxonomy_id . "').text('" . $sTxtAdd . "').appendTo('#wpmediacategory_optgroup1');";
286
+ echo "jQuery('<option>').val('wpmediacategory_add_" . $term->term_taxonomy_id . "').text('" . $sTxtAdd . "').appendTo('#wpmediacategory_optgroup2');";
287
  }
288
  // remove categories
289
  foreach ( $terms as $term ) {
290
  $sTxtRemove = esc_js( __( 'Remove' ) . ': ' . $term->name );
291
+ echo "jQuery('<option>').val('wpmediacategory_remove_" . $term->term_taxonomy_id . "').text('" . $sTxtRemove . "').appendTo('#wpmediacategory_optgroup1');";
292
+ echo "jQuery('<option>').val('wpmediacategory_remove_" . $term->term_taxonomy_id . "').text('" . $sTxtRemove . "').appendTo('#wpmediacategory_optgroup2');";
293
  }
294
  // remove all categories
295
  echo "jQuery('<option>').val('wpmediacategory_remove_0').text('" . esc_js( __( 'Delete all' ) ) . "').appendTo('#wpmediacategory_optgroup1');";
306
  function wpmediacategory_custom_bulk_action() {
307
  global $wpdb;
308
 
309
+ if ( ! isset( $_REQUEST['action'] ) ) {
310
  return;
311
+ }
312
 
313
  // is it a category?
314
+ $sAction = ( $_REQUEST['action'] != -1 ) ? $_REQUEST['action'] : $_REQUEST['action2'];
315
+ if ( substr( $sAction, 0, 16 ) != 'wpmediacategory_' ) {
316
  return;
317
+ }
318
 
319
  // security check
320
+ check_admin_referer( 'bulk-media' );
321
 
322
  // make sure ids are submitted. depending on the resource type, this may be 'media' or 'post'
323
+ if( isset( $_REQUEST['media'] ) ) {
324
+ $post_ids = array_map( 'intval', $_REQUEST['media'] );
325
  }
326
 
327
+ if( empty( $post_ids ) ) {
328
+ return;
329
+ }
330
 
331
  $sendback = admin_url( "upload.php?editCategory=1" );
332
 
347
 
348
  foreach( $post_ids as $post_id ) {
349
 
350
+ if ( is_numeric( str_replace( 'wpmediacategory_add_', '', $sAction ) ) ) {
351
+ $nCategory = str_replace( 'wpmediacategory_add_', '', $sAction );
352
 
353
  // update or insert category
354
  $wpdb->replace( $wpdb->term_relationships,
355
  array(
356
+ 'object_id' => $post_id,
357
  'term_taxonomy_id' => $nCategory
358
  ),
359
  array(
362
  )
363
  );
364
 
365
+ } else if ( is_numeric( str_replace( 'wpmediacategory_remove_', '', $sAction ) ) ) {
366
+ $nCategory = str_replace( 'wpmediacategory_remove_', '', $sAction );
367
 
368
  // remove all categories
369
+ if ( $nCategory == 0 ) {
370
  $wpdb->delete( $wpdb->term_relationships,
371
  array(
372
  'object_id' => $post_id
379
  } else {
380
  $wpdb->delete( $wpdb->term_relationships,
381
  array(
382
+ 'object_id' => $post_id,
383
  'term_taxonomy_id' => $nCategory
384
  ),
385
  array(
404
  function wpmediacategory_custom_bulk_admin_notices() {
405
  global $post_type, $pagenow;
406
 
407
+ if( $pagenow == 'upload.php' && $post_type == 'attachment' && isset( $_GET['editCategory'] ) ) {
408
+ echo '<div class="updated"><p>' . __( 'Settings saved.' ) . '</p></div>';
409
  }
410
  }
411
  add_action( 'admin_notices', 'wpmediacategory_custom_bulk_admin_notices' );
419
  $taxonomy = apply_filters( 'wpmediacategory_taxonomy', $taxonomy );
420
  return array_merge(
421
  array(
422
+ 'settings' => '<a href="' . get_bloginfo( 'wpurl' ) . '/wp-admin/edit-tags.php?taxonomy=' . $taxonomy . '&amp;post_type=attachment">' . __( 'Categories' ) . '</a>',
423
+ 'premium' => '<a href="http://codecanyon.net/item/media-library-categories-premium/6691290?ref=jeffrey-wp">' . __( 'Get Premium Version' ) . '</a>'
424
  ),
425
  $links
426
  );
readme.txt CHANGED
@@ -3,8 +3,8 @@ Contributors: jeffrey-wp
3
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=SSNQMST6R28Q2
4
  Tags: category, categories, media, library, medialibrary, image, images, media category, media categories
5
  Requires at least: 3.1
6
- Tested up to: 3.9.1
7
- Stable tag: 1.4.11
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -50,6 +50,17 @@ add_filter( 'wpmediacategory_taxonomy', function(){ return 'category_media'; },
50
  `
51
 
52
 
 
 
 
 
 
 
 
 
 
 
 
53
  = How can I filter on categories when inserting media into a post or page? =
54
  This feature is only available in the [premium version](http://codecanyon.net/item/media-library-categories-premium/6691290?ref=jeffrey-wp)
55
 
@@ -66,6 +77,10 @@ Maintaining a plugin and keeping it up to date is hard work. Please support me b
66
 
67
  == Changelog ==
68
 
 
 
 
 
69
  = 1.4.11 =
70
  * Remember ordering when changing categories.
71
 
3
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=SSNQMST6R28Q2
4
  Tags: category, categories, media, library, medialibrary, image, images, media category, media categories
5
  Requires at least: 3.1
6
+ Tested up to: 3.9.2
7
+ Stable tag: 1.4.12
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
50
  `
51
 
52
 
53
+ = Can i use category in the [gallery] shortcode, like [gallery category="my-category-slug"]? =
54
+ Yes, it is possible to extend the existing WordPress default `[gallery]` to only show images from one category by adding the `'category'` parameter.
55
+ The value passed to the `'category'` parameter can be either the `category slug`, or the `term_id`.
56
+ `[gallery category="my-category-slug"]
57
+ OR
58
+ [gallery category="14"]`
59
+ Aside from this behavior, the `[gallery]` shortcode works as it does by default with the built-in shortcode.
60
+ If you use an incorrect slug by default WordPress shows the images that are attached to the page / post that is displayed. If you use an incorrect term_id no images are shown.
61
+ For more information on using the built-in [gallery shortcode checkout the codex page](http://codex.wordpress.org/Gallery_Shortcode).
62
+
63
+
64
  = How can I filter on categories when inserting media into a post or page? =
65
  This feature is only available in the [premium version](http://codecanyon.net/item/media-library-categories-premium/6691290?ref=jeffrey-wp)
66
 
77
 
78
  == Changelog ==
79
 
80
+ = 1.4.12 =
81
+ * Add category option to the default WordPress shortcode gallery [Read the FAQ for howto](http://wordpress.org/plugins/wp-media-library-categories/faq/)
82
+ * Improve code styling to match WordPress code standard even more strictly
83
+
84
  = 1.4.11 =
85
  * Remember ordering when changing categories.
86