Media Library Categories - Version 1.4.14

Version Description

  • Support for WordPress 4.0
  • Added categories filter to media grid view.
  • Security enhancement.
  • Resolved conflict with UberMenu plugin.
Download this release

Release Info

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

Code changes from version 1.4.13 to 1.4.14

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.13
7
  * Author: Jeffrey-WP
8
  * Author URI: http://codecanyon.net/user/jeffrey-wp/?ref=jeffrey-wp
9
  */
@@ -18,16 +18,16 @@ 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 ) );
29
  }
30
-
31
  }
32
 
33
 
@@ -232,8 +232,8 @@ if ( is_admin() ) {
232
 
233
  /** Add a category filter */
234
  function wpmediacategory_add_category_filter() {
235
- $screen = get_current_screen();
236
- if ( 'upload' == $screen->id ) {
237
  // Default taxonomy
238
  $taxonomy = 'category';
239
  // Add filter to change the default taxonomy
@@ -278,7 +278,7 @@ if ( is_admin() ) {
278
  if ( $terms && ! is_wp_error( $terms ) ) :
279
 
280
  echo '<script type="text/javascript">';
281
- echo 'jQuery(document).ready(function() {';
282
  echo 'jQuery(\'<optgroup id="wpmediacategory_optgroup1" label="' . html_entity_decode( __( 'Categories' ), ENT_QUOTES, 'UTF-8' ) . '">\').appendTo("select[name=\'action\']");';
283
  echo 'jQuery(\'<optgroup id="wpmediacategory_optgroup2" label="' . html_entity_decode( __( 'Categories' ), ENT_QUOTES, 'UTF-8' ) . '">\').appendTo("select[name=\'action2\']");';
284
 
@@ -412,7 +412,7 @@ if ( is_admin() ) {
412
  function wpmediacategory_custom_bulk_admin_notices() {
413
  global $post_type, $pagenow;
414
 
415
- if( $pagenow == 'upload.php' && $post_type == 'attachment' && isset( $_GET['editCategory'] ) ) {
416
  echo '<div class="updated"><p>' . __( 'Settings saved.' ) . '</p></div>';
417
  }
418
  }
@@ -434,5 +434,121 @@ if ( is_admin() ) {
434
  );
435
  }
436
  add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'wpmediacategory_add_plugin_action_links' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
437
  }
438
  ?>
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.14
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
+ $query = "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 = %s GROUP BY tt.term_taxonomy_id
23
  ) UNION ALL (
24
+ SELECT term_taxonomy_id, 0 AS total FROM $wpdb->term_taxonomy WHERE taxonomy = %s
25
+ )) AS unioncount GROUP BY term_taxonomy_id";
26
+ $rsCount = $wpdb->get_results( $wpdb->prepare( $query, $taxonomy, $taxonomy ) );
27
  // update all count values from taxonomy
28
  foreach ( $rsCount as $rowCount ) {
29
  $wpdb->update( $wpdb->term_taxonomy, array( 'count' => $rowCount->total ), array( 'term_taxonomy_id' => $rowCount->term_taxonomy_id ) );
30
  }
 
31
  }
32
 
33
 
232
 
233
  /** Add a category filter */
234
  function wpmediacategory_add_category_filter() {
235
+ global $pagenow;
236
+ if ( 'upload.php' == $pagenow ) {
237
  // Default taxonomy
238
  $taxonomy = 'category';
239
  // Add filter to change the default taxonomy
278
  if ( $terms && ! is_wp_error( $terms ) ) :
279
 
280
  echo '<script type="text/javascript">';
281
+ echo 'jQuery(window).load(function() {';
282
  echo 'jQuery(\'<optgroup id="wpmediacategory_optgroup1" label="' . html_entity_decode( __( 'Categories' ), ENT_QUOTES, 'UTF-8' ) . '">\').appendTo("select[name=\'action\']");';
283
  echo 'jQuery(\'<optgroup id="wpmediacategory_optgroup2" label="' . html_entity_decode( __( 'Categories' ), ENT_QUOTES, 'UTF-8' ) . '">\').appendTo("select[name=\'action2\']");';
284
 
412
  function wpmediacategory_custom_bulk_admin_notices() {
413
  global $post_type, $pagenow;
414
 
415
+ if ( $pagenow == 'upload.php' && $post_type == 'attachment' && isset( $_GET['editCategory'] ) ) {
416
  echo '<div class="updated"><p>' . __( 'Settings saved.' ) . '</p></div>';
417
  }
418
  }
434
  );
435
  }
436
  add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'wpmediacategory_add_plugin_action_links' );
437
+
438
+
439
+ /** Changing categories in the 'grid view' */
440
+ function wpmediacategory_ajax_query_attachments() {
441
+
442
+ if ( ! current_user_can( 'upload_files' ) ) {
443
+ wp_send_json_error();
444
+ }
445
+
446
+ $taxonomies = get_object_taxonomies( 'attachment', 'names' );
447
+
448
+ $query = isset( $_REQUEST['query'] ) ? (array) $_REQUEST['query'] : array();
449
+
450
+ $defaults = array(
451
+ 's', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type',
452
+ 'post_parent', 'post__in', 'post__not_in'
453
+ );
454
+ $query = array_intersect_key( $query, array_flip( array_merge( $defaults, $taxonomies ) ) );
455
+
456
+ $query['post_type'] = 'attachment';
457
+ $query['post_status'] = 'inherit';
458
+ if ( current_user_can( get_post_type_object( 'attachment' )->cap->read_private_posts ) )
459
+ $query['post_status'] .= ',private';
460
+
461
+ $query['tax_query'] = array( 'relation' => 'AND' );
462
+
463
+ foreach ( $taxonomies as $taxonomy ) {
464
+ if ( isset( $query[$taxonomy] ) && is_numeric( $query[$taxonomy] ) ) {
465
+ array_push( $query['tax_query'], array(
466
+ 'taxonomy' => $taxonomy,
467
+ 'field' => 'id',
468
+ 'terms' => $query[$taxonomy]
469
+ ));
470
+ }
471
+ unset ( $query[$taxonomy] );
472
+ }
473
+
474
+ $query = apply_filters( 'ajax_query_attachments_args', $query );
475
+ $query = new WP_Query( $query );
476
+
477
+ $posts = array_map( 'wp_prepare_attachment_for_js', $query->posts );
478
+ $posts = array_filter( $posts );
479
+
480
+ wp_send_json_success( $posts );
481
+ }
482
+ add_action( 'wp_ajax_query-attachments', 'wpmediacategory_ajax_query_attachments', 0 );
483
+
484
+
485
+ /** Enqueue admin scripts and styles */
486
+ function wpmediacategory_enqueue_media_action() {
487
+ global $pagenow;
488
+ if ( wp_script_is( 'media-editor' ) && 'upload.php' == $pagenow ) {
489
+
490
+ // Default taxonomy
491
+ $taxonomy = 'category';
492
+ // Add filter to change the default taxonomy
493
+ $taxonomy = apply_filters( 'wpmediacategory_taxonomy', $taxonomy );
494
+
495
+ if ( $taxonomy != 'category' ) {
496
+ $dropdown_options = array(
497
+ 'taxonomy' => $taxonomy,
498
+ 'hide_empty' => false,
499
+ 'hierarchical' => true,
500
+ 'orderby' => 'name',
501
+ 'show_count' => true,
502
+ 'walker' => new wpmediacategory_walker_category_mediagridfilter(),
503
+ 'value' => 'id',
504
+ 'echo' => false
505
+ );
506
+ } else {
507
+ $dropdown_options = array(
508
+ 'taxonomy' => $taxonomy,
509
+ 'hide_empty' => false,
510
+ 'hierarchical' => true,
511
+ 'orderby' => 'name',
512
+ 'show_count' => false,
513
+ 'walker' => new wpmediacategory_walker_category_mediagridfilter(),
514
+ 'value' => 'id',
515
+ 'echo' => false
516
+ );
517
+ }
518
+ $attachment_terms = wp_dropdown_categories( $dropdown_options );
519
+ $attachment_terms = preg_replace( array( "/<select([^>]*)>/", "/<\/select>/" ), "", $attachment_terms );
520
+
521
+ echo '<script type="text/javascript">';
522
+ echo '/* <![CDATA[ */';
523
+ echo 'var wpmediacategory_taxonomies = {"' . $taxonomy . '":{"list_title":"' . html_entity_decode( __( 'View all categories' ), ENT_QUOTES, 'UTF-8' ) . '","term_list":[' . substr( $attachment_terms, 2 ) . ']}};';
524
+ echo '/* ]]> */';
525
+ echo '</script>';
526
+
527
+ wp_enqueue_script( 'wpmediacategory-media-views', plugins_url( 'js/wpmediacategory-media-views.min.js', __FILE__ ), array( 'media-views' ), '1.4.14', true );
528
+ }
529
+ }
530
+ add_action( 'admin_enqueue_scripts', 'wpmediacategory_enqueue_media_action' );
531
+
532
+
533
+ /** Custom walker for wp_dropdown_categories for media grid view filter */
534
+ class wpmediacategory_walker_category_mediagridfilter extends Walker_CategoryDropdown {
535
+
536
+ function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
537
+ $pad = str_repeat( '&nbsp;', $depth * 3 );
538
+
539
+ $cat_name = apply_filters( 'list_cats', $category->name, $category );
540
+
541
+ // {"term_id":"1","term_name":"no category"}
542
+ $output .= ',{"term_id":"' . $category->term_id . '",';
543
+
544
+ $output .= '"term_name":"' . $pad . esc_attr( $cat_name );
545
+ if ( $args['show_count'] ) {
546
+ $output .= '&nbsp;&nbsp;('. $category->count .')';
547
+ }
548
+ $output .= '"}';
549
+ }
550
+
551
+ }
552
+
553
  }
554
  ?>
js/wpmediacategory-media-views.js ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ window.wp = window.wp || {};
2
+
3
+ (function($){
4
+
5
+ var media = wp.media;
6
+
7
+ media.view.AttachmentFilters.Taxonomy = media.view.AttachmentFilters.extend({
8
+
9
+ tagName: 'select',
10
+
11
+ createFilters: function() {
12
+ var filters = {};
13
+ var that = this;
14
+
15
+ _.each( that.options.termList || {}, function( term, key ) {
16
+ var term_id = term['term_id'];
17
+ var term_name = $("<div/>").html(term['term_name']).text();
18
+ filters[ term_id ] = {
19
+ text: term_name,
20
+ priority: key+2
21
+ };
22
+ filters[term_id]['props'] = {};
23
+ filters[term_id]['props'][that.options.taxonomy] = term_id;
24
+ });
25
+
26
+ filters.all = {
27
+ text: that.options.termListTitle,
28
+ priority: 1
29
+ };
30
+ filters['all']['props'] = {};
31
+ filters['all']['props'][that.options.taxonomy] = null;
32
+
33
+ this.filters = filters;
34
+ }
35
+ });
36
+
37
+
38
+
39
+ var curAttachmentsBrowser = media.view.AttachmentsBrowser;
40
+
41
+ media.view.AttachmentsBrowser = media.view.AttachmentsBrowser.extend({
42
+
43
+ createToolbar: function() {
44
+
45
+ var filters = this.options.filters;
46
+
47
+ curAttachmentsBrowser.prototype.createToolbar.apply(this,arguments);
48
+
49
+ var that = this,
50
+ i = 1;
51
+
52
+ $.each(wpmediacategory_taxonomies, function(taxonomy, values)
53
+ {
54
+ if ( values.term_list && filters )
55
+ {
56
+ that.toolbar.set( taxonomy+'-filter', new media.view.AttachmentFilters.Taxonomy({
57
+ controller: that.controller,
58
+ model: that.collection.props,
59
+ priority: -80 + 10*i++,
60
+ taxonomy: taxonomy,
61
+ termList: values.term_list,
62
+ termListTitle: values.list_title,
63
+ className: 'attachment-'+taxonomy+'-filter'
64
+ }).render() );
65
+ }
66
+ });
67
+ }
68
+ });
69
+
70
+ })( jQuery );
js/wpmediacategory-media-views.min.js ADDED
@@ -0,0 +1 @@
 
1
+ window.wp=window.wp||{};(function(a){var b=wp.media;b.view.AttachmentFilters.Taxonomy=b.view.AttachmentFilters.extend({tagName:"select",createFilters:function(){var e={};var d=this;_.each(d.options.termList||{},function(g,f){var h=g.term_id;var i=a("<div/>").html(g.term_name).text();e[h]={text:i,priority:f+2};e[h]["props"]={};e[h]["props"][d.options.taxonomy]=h});e.all={text:d.options.termListTitle,priority:1};e.all["props"]={};e.all["props"][d.options.taxonomy]=null;this.filters=e}});var c=b.view.AttachmentsBrowser;b.view.AttachmentsBrowser=b.view.AttachmentsBrowser.extend({createToolbar:function(){var f=this.options.filters;c.prototype.createToolbar.apply(this,arguments);var e=this,d=1;a.each(wpmediacategory_taxonomies,function(g,h){if(h.term_list&&f){e.toolbar.set(g+"-filter",new b.view.AttachmentFilters.Taxonomy({controller:e.controller,model:e.collection.props,priority:-80+10*d++,taxonomy:g,termList:h.term_list,termListTitle:h.list_title,className:"attachment-"+g+"-filter"}).render())}})}})})(jQuery);
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.2
7
- Stable tag: 1.4.13
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -78,6 +78,12 @@ Maintaining a plugin and keeping it up to date is hard work. Please support me b
78
 
79
  == Changelog ==
80
 
 
 
 
 
 
 
81
  = 1.4.13 =
82
  * Improved compatibility with other plugins that use the [gallery] shortcode.
83
  * Remember author when changing categories.
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: 4.0
7
+ Stable tag: 1.4.14
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
78
 
79
  == Changelog ==
80
 
81
+ = 1.4.14 =
82
+ * Support for WordPress 4.0
83
+ * Added categories filter to media grid view.
84
+ * Security enhancement.
85
+ * Resolved conflict with UberMenu plugin.
86
+
87
  = 1.4.13 =
88
  * Improved compatibility with other plugins that use the [gallery] shortcode.
89
  * Remember author when changing categories.