WordPress.org

Plugin Directory

Changeset 391118


Ignore:
Timestamp:
05/31/11 05:10:03 (3 years ago)
Author:
mfields
Message:

Importing version 0.7

Location:
taxonomy-images/trunk
Files:
7 added
4 deleted
6 edited

Legend:

Unmodified
Added
Removed
  • taxonomy-images/trunk/admin.css

    r287106 r391118  
    1 .taxonomy-image-thumbnail { display:block; height:77px; width:77px; overflow:hidden;text-align:center; margin-bottom:3px; } 
    2 .taxonomy-image-control .control { display:block; float:left; text-indent:-9999em; width:15px; height:15px; background-image: url( controls.png ); background-repeat:no-repeat;} 
     1.taxonomy-images-modal .create-association .term-name { 
     2    font-style:italic; 
     3} 
     4.taxonomy-images-modal .create-association { 
     5    display:inline; 
     6} 
     7.taxonomy-images-modal .remove-association { 
     8    display:none; 
     9    color:#bc0b0b; 
     10    text-decoration:underline; 
     11    cursor:pointer; 
     12} 
     13.taxonomy-images-modal #tab-type_url, 
     14.taxonomy-images-modal .savesend input { 
     15    display:none !important; 
     16} 
     17.taxonomy-image-thumbnail { 
     18    display:block; 
     19    height:77px; 
     20    width:77px; 
     21    overflow:hidden; 
     22    text-align:center; 
     23    margin-bottom:3px; 
     24} 
     25.taxonomy-image-control .control { 
     26    display:block; 
     27    float:left; 
     28    text-indent:-9999em; 
     29    width:15px; 
     30    height:15px; 
     31    background:url( controls.png ); 
     32    background-repeat:no-repeat; 
     33} 
     34.taxonomy-image-control .control:hover { cursor:pointer; } 
    335.taxonomy-image-control .upload { background-position: 0 0; } 
    4 .taxonomy-image-control .delete { background-position: -30px 0; } 
     36.taxonomy-image-control .upload:hover { background-position: -15px 0 } 
     37.taxonomy-image-control .remove { background-position:-30px 0; } 
     38.taxonomy-image-control .remove:hover { background-position:-45px 0; } 
    539.taxonomy-image-control .library { background-position: -60px 0; } 
    640.taxonomy-image-control .hide { visibility:hidden; } 
  • taxonomy-images/trunk/edit-tags.js

    r287106 r391118  
     1jQuery( document ).ready( function( $ ) { 
    12 
    2 jQuery( document ).ready( function( $ ) { 
    3      
    4     /* General UI. */ 
    5     $( '.taxonomy-image-control .control' ).hover(  
    6         function() { $( this ).css( 'cursor', 'pointer' ); }, 
    7         function() { $( this ).css( 'cursor', 'default' ); } 
    8         ); 
    9     $( '.taxonomy-image-control .upload' ).hover(  
    10         function() { $( this ).css( 'background-position', '-15px 0' ); }, 
    11         function() { $( this ).css( 'background-position', '0 0' ); } 
    12         ); 
    13     $( '.taxonomy-image-control .delete' ).hover(  
    14         function() { $( this ).css( 'background-position', '-45px 0' ); }, 
    15         function() { $( this ).css( 'background-position', '-30px 0' ); } 
    16         ); 
    17     $( '.taxonomy-image-control .library' ).hover(  
    18         function() { $( this ).css( 'background-position', '-75px 0' ); }, 
    19         function() { $( this ).css( 'background-position', '-60px 0' ); } 
    20         ); 
    21      
    22     /* Delete association via ajax. */ 
    23     $( '.taxonomy-image-control .delete' ).click( function () { 
    24         var remove = $( this ); 
    25         $.ajax({   
     3    $( '.taxonomy-image-control a' ).live( 'click', function () { 
     4        taxonomyImagesPlugin.tt_id = parseInt( $( this ).parent().find( 'input.tt_id' ).val() ); 
     5        taxonomyImagesPlugin.term_name = $( this ).parent().find( 'input.term_name' ).val(); 
     6        taxonomyImagesPlugin.image_id = parseInt( $( this ).parent().find( 'input.image_id' ).val() ); 
     7    } ); 
     8 
     9    $( '.taxonomy-images-modal .remove' ).live( 'click', function () { 
     10        var tt_id = parseInt( $( this ).attr( 'rel' ) ); 
     11        $.ajax( { 
    2612            url: ajaxurl, 
    2713            type: "POST", 
    28             dataType: 'json',                            
     14            dataType: 'json', 
    2915            data: { 
    30                 'action' : 'taxonomy_images_remove_association', 
    31                 'term_id' : parseInt( $( this ).attr( 'rel' ) ), 
    32                 'wp_nonce' : taxonomyImagesPlugin.nonce_remove 
     16                'action'   : 'taxonomy_image_plugin_remove_association', 
     17                'wp_nonce' : taxonomyImagesPlugin.nonce, 
     18                'tt_id'    : tt_id 
    3319                }, 
    3420            cache: false, 
    35             success: function ( data, textStatus ) { 
    36                 $( remove ).addClass( 'hide' ); 
    37                 $( remove ).parent().find( 'img' ).attr( 'src', taxonomyImagesPlugin.img_src ); 
    38             }          
    39         }); 
     21            success: function ( response ) { 
     22                if ( 'good' === response.status ) { 
     23                    $( '#remove-' + tt_id ).addClass( 'hide' ); 
     24                    $( '#taxonomy_image_plugin_' + tt_id ).attr( 'src', taxonomyImagesPlugin.img_src ); 
     25                } 
     26                else if ( 'bad' === response.status ) { 
     27                    alert( response.why ); 
     28                } 
     29            } 
     30        } ); 
     31        return false; 
    4032    } ); 
    4133} ); 
  • taxonomy-images/trunk/media-upload-popup.js

    r287106 r391118  
     1var TaxonomyImagesCreateAssociation; 
     2 
    13jQuery( document ).ready( function( $ ) { 
    2     /* 
    3     * Loop over all anchors in the media upload iframe and add 
    4     * a query var for those links that do not already possess 
    5     * one. 
    6     */ 
    7     $.each( $( 'a' ), function ( order, img ) { 
    8          
    9         /* Capture href attribute for all links on page.*/ 
    10         var href = $( this ).attr( 'href' ); 
    11          
    12         /* See if custom attribute already exists. */ 
    13         var hasAttr = href.indexOf( taxonomyImagesPlugin.attrSlug ); 
    14          
    15         /* See if there is a question mark in the url. */ 
    16         var hasQueryString = href.indexOf( '?' ); 
    17          
    18         /* Set to true if href contains only the hash character. */ 
    19         var isHash = ( href == '#' ) ? true : false; 
    20          
    21         /* Append attribute to all links that do not already posses it. */ 
    22         if( hasAttr == -1 && !isHash ) { 
    23             if( hasQueryString == -1 ) { 
    24                 href += '?' + taxonomyImagesPlugin.attr; 
    25             } 
    26             else { 
    27                 href += '&' + taxonomyImagesPlugin.attr; 
     4    var ID = 0, below; 
     5 
     6    /* Get window that opened the thickbox. */ 
     7    below = window.dialogArguments || opener || parent || top; 
     8 
     9    if ( null !== below && 'taxonomyImagesPlugin' in below ) { 
     10        /* Set the value of ID. */ 
     11        if ( 'tt_id' in below.taxonomyImagesPlugin ) { 
     12            ID = parseInt( below.taxonomyImagesPlugin.tt_id ); 
     13            if ( isNaN( ID ) ) { 
     14                ID = 0; 
    2815            } 
    2916        } 
    30          
    31         /* Replace the href attribute with new value. */ 
    32         $( this ).attr( 'href', href ); 
    33     }); 
    34     $( '.' + taxonomyImagesPlugin.locale ).live( 'click', function () { 
    35         var data = { 
    36             'action' : 'taxonomy_images_create_association', 
    37             'term_id' : taxonomyImagesPlugin.term_id, 
    38             'wp_nonce' : taxonomyImagesPlugin.nonce_create, 
    39             'attachment_id' : $( this ).attr( 'rel' ) 
    40             }; 
    41          
    42         /* Process $_POST request */ 
    43         $.ajax({ 
     17        /* Replace term name. */ 
     18        if ( 'term_name' in below.taxonomyImagesPlugin ) { 
     19            $( '.create-association .term-name' ).html( TaxonomyImagesModal.termBefore + below.taxonomyImagesPlugin.term_name + TaxonomyImagesModal.termAfter ); 
     20        } 
     21    } 
     22 
     23    if ( 0 < ID ) { 
     24        $( 'body' ).addClass( 'taxonomy-images-modal' ); 
     25 
     26        var buttons = $( '.taxonomy-images-modal .create-association' ); 
     27 
     28        /* Add hidden input to search form. */ 
     29        $( '#filter' ).prepend( '<input type="hidden" name="taxonomy_images_plugin" value="' + ID + '" />' ); 
     30 
     31        if ( 'image_id' in below.taxonomyImagesPlugin ) { 
     32            buttons.each( function( i, e ) { 
     33                var image_id = $( e ).parent().find( '.taxonomy-image-button-image-id' ).val(); 
     34                if ( image_id == below.taxonomyImagesPlugin.image_id ) { 
     35                    $( e ).hide(); 
     36                    $( e ).parent().find( '.remove-association' ).css( 'display', 'inline' ); 
     37                } 
     38            } ); 
     39        } 
     40    } 
     41 
     42    $( '.taxonomy-images-modal .remove-association' ).live( 'click', function () { 
     43        var button = $( this ); 
     44        originalText = button.html(); 
     45        button.html( TaxonomyImagesModal.removing ); 
     46 
     47        $.ajax( { 
    4448            url: ajaxurl, 
    4549            type: "POST", 
    46             dataType: 'json',                            
    47             data: data, 
     50            dataType: 'json', 
     51            data: { 
     52                'action'   : 'taxonomy_image_plugin_remove_association', 
     53                'wp_nonce' : $( this ).parent().find( '.taxonomy-image-button-nonce-remove' ).val(), 
     54                'tt_id'    : ID 
     55                }, 
    4856            cache: false, 
    49             success: function ( data, textStatus ) { 
    50                 /* Vars */ 
    51                 data = eval( data ); 
    52                 var tableRowId = 'cat-' + data.term_id; 
    53                  
    54                 /* Refresh the image on the screen below */ 
    55                 if( data.attachment_thumb_src != 'false' ) { 
    56                     $( parent.document.getElementById( taxonomyImagesPlugin.locale + '_' + taxonomyImagesPlugin.term_id ) ).attr( 'src', data.attachment_thumb_src ); 
    57                     $( parent.document.getElementById( 'remove-' + taxonomyImagesPlugin.term_id ) ).removeClass( 'hide' ); 
     57            success: function ( response ) { 
     58                if ( 'good' === response.status ) { 
     59                    button.html( TaxonomyImagesModal.removed ).fadeOut( 200, function() { 
     60                        $( this ).hide(); 
     61                        var selector = parent.document.getElementById( 'taxonomy_image_plugin_' + ID ); 
     62                        $( selector ).attr( 'src', below.taxonomyImagesPlugin.img_src ); 
     63                        $( this ).parent().find( '.create-association' ).show(); 
     64                        $( this ).html( originalText ); 
     65                    } ); 
    5866                } 
    59                  
    60                 /* Close Thickbox */ 
    61                 self.parent.tb_remove(); 
     67                else if ( 'bad' === response.status ) { 
     68                    alert( response.why ); 
     69                } 
    6270            } 
    63         }); 
     71        } ); 
    6472        return false; 
    65     } );  
     73    } ); 
     74 
     75    $( '.taxonomy-images-modal .create-association' ).live( 'click', function () { 
     76        var button, selector, originalText; 
     77        if ( 0 == ID ) { 
     78            return; 
     79        } 
     80 
     81        button = $( this ); 
     82        originalText = button.html(); 
     83        button.html( TaxonomyImagesModal.associating ); 
     84 
     85        $.ajax( { 
     86            url      : ajaxurl, 
     87            type     : "POST", 
     88            dataType : 'json', 
     89            data: { 
     90                'action'        : 'taxonomy_image_create_association', 
     91                'wp_nonce'      : $( this ).parent().find( '.taxonomy-image-button-nonce-create' ).val(), 
     92                'attachment_id' : $( this ).parent().find( '.taxonomy-image-button-image-id' ).val(), 
     93                'tt_id'         : parseInt( ID ) 
     94                }, 
     95            success: function ( response ) { 
     96                if ( 'good' === response.status ) { 
     97                    var parent_id = button.parent().attr( 'id' ); 
     98 
     99                    /* Set state of all other buttons. */ 
     100                    $( '.taxonomy-image-modal-control' ).each( function( i, e ) { 
     101                        if ( parent_id == $( e ).attr( 'id' ) ) { 
     102                            return true; 
     103                        } 
     104                        $( e ).find( '.create-association' ).show(); 
     105                        $( e ).find( '.remove-association' ).hide(); 
     106                    } ); 
     107 
     108                    selector = parent.document.getElementById( 'taxonomy-image-control-' + ID ); 
     109 
     110                    /* Update the image on the screen below */ 
     111                    $( selector ).find( '.taxonomy-image-thumbnail img' ).each( function ( i, e ) { 
     112                        $( e ).attr( 'src', response.attachment_thumb_src ); 
     113                    } ); 
     114 
     115                    /* Show delete control on the screen below */ 
     116                    $( selector ).find( '.remove' ).each( function ( i, e ) { 
     117                        $( e ).removeClass( 'hide' ); 
     118                    } ); 
     119 
     120                    button.show().html( TaxonomyImagesModal.success ).fadeOut( 200, function() { 
     121                        var remove = button.parent().find( '.remove-association' ); 
     122                        if ( undefined !== remove[0] ) { 
     123                            $( remove ).css( 'display', 'inline' ); 
     124                            button.hide(); 
     125                            button.html( originalText ); 
     126                        } 
     127                    } ); 
     128                } 
     129                else if ( 'bad' === response.status ) { 
     130                    alert( response.why ); 
     131                } 
     132            } 
     133        } ); 
     134        return false; 
     135    } ); 
    66136} ); 
  • taxonomy-images/trunk/readme.txt

    r287106 r391118  
    1 ===Plugin Name=== 
    2 Taxonomy Images 
    3 Contributors: mfields 
    4 Donate link: http://mfields.org/donate/ 
    5 Tags: taxonomy, tag, category, image, upload, media 
    6 Requires at least: 2.9.2 
    7 Tested up to: 3.0.1 
    8 Stable tag: trunk 
    9 Easily associate images from your media library to categories, tags and custom taxonomies. 
     1===Taxonomy Images=== 
     2 
     3Contributors:         mfields 
     4Donate link:          http://wordpress.mfields.org/donate/ 
     5Tags:                 taxonomy, tag, category, image, upload, media 
     6Requires at least:    3.1 
     7Tested up to:         3.2-beta2-18055 
     8Stable tag:           trunk 
     9 
     10Associate images from your media library to categories, tags and custom taxonomies. 
    1011 
    1112==Description== 
    1213 
    13 The Taxonomy Images plugin allows you to associate images from your Media Library to categories, tags and custom taxonomies. For usage instructions please view the [screencast](http://screenr.com/zMx). 
    14  
    15 = Displaying Images in a Page or Post = 
    16  
    17 Version 0.4 introduces a new shortcode which will display a list of all active terms of a given taxonomy. This list features the term in a second level heading `<h2>`, the term description in a paragraph and an intermediate version of the uploaded image floated to the left. Here is the shortcode in it's simplist form: 
    18  
    19 `[taxonomy_image_plugin]` 
    20  
    21 This will display a list of all terms in the built-in "Category" taxonomy with desriptions and images (sized as "thumbnails"). 
    22  
    23 If you would like to change the taxonomy, you can set the "taxonomy" argument: 
    24  
    25 `[taxonomy_image_plugin taxonomy="post_tags"]` or `[taxonomy_image_plugin taxonomy="my_custom_taxonomy_name"]` 
    26  
    27 You can also change the size of the image by setting the size attribute: 
    28  
    29 `[taxonomy_image_plugin size="medium"]` or ``[taxonomy_image_plugin size="detail"]`` 
    30  
    31 = Displaying Images in your Theme #1: Category, Tag + Custom Taxonomy archives = 
    32  
    33 To display the images in your theme, you will want to use the following code in the appropriate theme file. The correct file will vary depending on your theme. category.php, tag.php, archive.php are a few file that this code will work in. Please see [Template Hierarchy](http://codex.wordpress.org/Template_Hierarchy) for more information. 
    34  
    35 `<?php do_action( 'taxonomy_image_plugin_print_image_html', 'detail' ); ?>` 
    36  
    37 Here we have passed to arguments to the WordPress core function do_action(). The first is `taxonomy_image_plugin_print_image_html` and should not be changed. The second represents the size of the image that you would like displayed. Acceptable values are:  
    38  
    39 * detail 
    40 * thumbnail 
    41 * medium 
    42 * large 
    43 * fullsize 
    44  
    45 = Displaying Images in your Theme #2: Category Thumbs = 
    46  
    47 The following example was inspired by [this thread](http://wordpress.org/support/topic/364299) in the WordPress.org Support Fourms. This code can be placed inside any theme template and will produce a string of links for every taxonomy that has an associated image. 
    48  
    49 `$cats = get_categories(); 
    50 foreach ( $cats as $c ) { 
    51     $url = get_category_link( $c->term_id ); 
    52     $img = $taxonomy_images_plugin->get_image_html( 'detail', $c->term_taxonomy_id ); 
    53     if( !empty( $img ) ) 
    54         print '<a href="' . $url . '">' . $img . '</a>'; 
    55 }` 
     14For usage instructions please view the [screencast](http://screenr.com/zMx). 
     15 
     16= Displaying Your Images in Your Theme = 
     17 
     18There are a few filters that you can use in your theme to display the image associations created by this plugin. Please read below for detailed information. 
     19 
     20= Display a single image representing the term archive = 
     21 
     22The following filter will display the image associated with the term asked for in the query string of the url. This filter only works in views that naturally use templates like category.php, tag.php taxonomy.php and all of their derivatives. Please read about [template hierarchy](http://codex.wordpress.org/Template_Hierarchy) for more information about these templates. The simplest use of this filter looks like: 
     23 
     24`print apply_filters( 'taxonomy-images-queried-term-image', '' );` 
     25 
     26This code will generate and print an image tag. It's output can be modifed by passig an optional third parameter to apply filters. This parameter is an array and the following keys may be set: 
     27 
     28* __after__ (string) - Text to append to the image's HTML. 
     29 
     30* __attr__ (array) - Key/value pairs representing the attributes of the img tag. Available options include: alt, class, src and title. This array will be passed as the fourth parameter to WordPress core function wp_get_attachment_image() without modification. 
     31 
     32* __before__ (string) - Text to prepend to the image's HTML. 
     33 
     34* __image_size__ (string) - May be any image size registered with WordPress. If no image size is specified, 'thumbnail' will be used as a default value. In the event that an unregistered size is specified, this filter will return an empty string. 
     35 
     36Here's an example of what a fully customized version of this filter might look like: 
     37 
     38`print apply_filters( 'taxonomy-images-queried-term-image', '', array( 
     39    'after' => '</div>' 
     40    'attr' => array( 
     41        'alt'   => 'Custom alternative text', 
     42        'class' => 'my-class-list bunnies turtles', 
     43        'src'   => 'this-is-where-the-image-lives.png', 
     44        'title' => 'Custom Title', 
     45        ), 
     46    'before' => '<div id="my-custom-div">', 
     47    'image_size' => 'medium', 
     48    ) ); 
     49` 
     50 
     51= Similar functionality = 
     52 
     53If you just need to get the database ID for the image, you may want to use: 
     54 
     55`$image_id = apply_filters( 'taxonomy-images-queried-term-image-id', 0 );` 
     56 
     57If you need to get the full object of the image, you may want to use: 
     58 
     59`$image = apply_filters( 'taxonomy-images-queried-term-image-object', '' );` 
     60 
     61If you need to get the url to the image, you may want to use the following: 
     62 
     63`$image_url = apply_filters( 'taxonomy-images-queried-term-image-url', '' );` 
     64 
     65You can specify the size of the image in an option third parameter: 
     66 
     67` 
     68$image_url = apply_filters( 'taxonomy-images-queried-term-image-url', '', array( 
     69    'image_size' => 'medium' 
     70    ) ); 
     71` 
     72 
     73If you need data about the image, you may want to use: 
     74 
     75`$image_data = apply_filters( 'taxonomy-images-queried-term-image-data', '' );` 
     76 
     77You can specify the size of the image in an option third parameter: 
     78 
     79` 
     80$image_data = apply_filters( 'taxonomy-images-queried-term-image-data', '', array( 
     81    'image_size' => 'medium' 
     82    ) ); 
     83` 
     84 
     85= List term images associated with a post object = 
     86 
     87When a post is being displayed you may want to display all of the images associated with all of the terms that are associated with the post (a mouthful? Yes indeed!). The `taxonomy-images-list-the-terms` filter does this. Here's what it looks like in its simplest form: 
     88 
     89`print apply_filters( 'taxonomy-images-list-the-terms', '' );` 
     90 
     91This filter accepts an optional third parameter that you can use to customize its output. It is an array which recognizes the following keys: 
     92 
     93* __after__ (string) - Text to append to the output. Default value is a closing unordered list element. 
     94 
     95* __after_image__ (string) - Text to append to each image. Default value is a closing list-item element. 
     96 
     97* __before__ (string) - Text to prepend to the output. Default value is an open unordered list element with an class attribute of "taxonomy-images-the-terms". 
     98 
     99* __before_image__ (string) - Text to prepend to each image. Default value is an open list-item element. 
     100 
     101* __image_size__ (string) - Any registered image size. Values will vary from installation to installation. Image sizes defined in core include: "thumbnail", "medium" and "large". "Fullsize" may also be used to get the unmodified image that was uploaded. Defaults to "thumbnail". 
     102  
     103* __post_id__ (int) - The post to retrieve terms from. Defaults to the ID property of the global $post object. 
     104  
     105* __taxonomy__ (string) - Name of a registered taxonomy to return terms from. Defaults to "category". 
     106 
     107Here's an example of what a fully customized version of this filter might look like: 
     108 
     109` 
     110print apply_filters( 'taxonomy-images-list-the-terms', '', array( 
     111    'after'        => '</div>', 
     112    'after_image'  => '</span>', 
     113    'before'       => '<div class="my-custom-class-name">', 
     114    'before_image' => '<span>', 
     115    'image_size'   => 'detail', 
     116    'post_id'      => 1234, 
     117    'taxonomy'     => 'post_tag', 
     118    ) ); 
     119` 
     120 
     121= Working with all terms of a given taxonomy = 
     122 
     123You will want to use the 'taxonomy-images-get-terms' filter. This filter is basically a wrapper for WordPress core function [get_terms()](http://codex.wordpress.org/Function_Reference/get_terms). It will return an array of enhanced term objects: each term object will have a custom property named image_id which is an integer representing the database ID of the image associated with the term. This filter can be used to create custom lists of terms. Here's what it's default useage looks like: 
     124 
     125`$terms = apply_filters( 'taxonomy-images-get-terms', '' );` 
     126 
     127Here is what php's print_r() function may return: 
     128 
     129` 
     130Array 
     131( 
     132    [0] => stdClass Object 
     133        ( 
     134            [term_id] => 8 
     135            [name] => Pirate 
     136            [slug] => pirate 
     137            [term_group] => 0 
     138            [term_taxonomy_id] => 8 
     139            [taxonomy] => category 
     140            [description] => Pirates live in the ocean and ride around on boats. 
     141            [parent] => 0 
     142            [count] => 1 
     143            [image_id] => 44 
     144        ) 
     145) 
     146` 
     147 
     148As you can see, all of the goodness of get_terms() is there with an added bonus: the image_id parameter! 
     149 
     150This filter recognizes an optional third parameter which is an array of arguments that can be used to modify its output: 
     151 
     152* __cache_images__ (bool) If this value is true all assocaite images will be queried for and cached for later use in various template tags. If it is set to false, this query will be suppressed. Do not set this value to false unless you have a really good reason for doing so :) Default value is true. 
     153 
     154* __having_images__ (bool) If this value is true then only terms that have associated images will be returned. Setting it to false will return all terms. Default value is true. 
     155 
     156* __taxonomy__ (string) Name of a registered taxonomy to return terms from. Multiple taxonomies may be specified by separating each name by a comma. Defaults to "category". 
     157 
     158* __term_args__ (array) Arguments to pass to [get_terms()](http://codex.wordpress.org/Function_Reference/get_terms) as the second parameter. Default value is an empty array. 
     159 
     160Here's and example of a simple custom loop that you can make to display all term images: 
     161 
     162` 
     163$terms = apply_filters( 'taxonomy-images-get-terms', '' ); 
     164if ( ! empty( $terms ) ) { 
     165    print '<ul>'; 
     166    foreach( (array) $terms as $term ) { 
     167        print '<li><a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, 'detail' ) . '</li>'; 
     168    } 
     169    print '</ul>'; 
     170} 
     171` 
    56172 
    57173= Support = 
    58174 
    59 If you find that this plugin is has a bug, does not play nicely with other plugins or if you have a suggestion or comment, please <a href="http://wordpress.org/tags/taxonomy-images?forum_id=10#postform">use this link to add a new thread to the WordPress Support Forum</a> 
     175If you have questions about integrating this plugin into your site, please [add a new thread to the WordPress Support Forum](http://wordpress.org/tags/taxonomy-images?forum_id=10#postform). I try to answer these, but I may not always be able to. In the event that I cannot there may be someone else who can help. 
     176 
     177= Bugs, Suggestions = 
     178 
     179Development of this plugin is hosted in a public repository on [Github](https://github.com/mfields/Taxonomy-Images). If you find a bug in this plugin or have a suggestion to make it better, please [create a new issue](https://github.com/mfields/Taxonomy-Images/issues/new) 
    60180 
    61181= Hook it up yo! = 
     
    63183If you have fallen in love with this plugin and would not be able to sleep without helping out in some way, please see the following list of ways that you can _hook it up!_: 
    64184 
    65 * __Rate it!__ - use the star tool on the right-hand sidebar of the [homepage](http://wordpress.org/extend/plugins/taxonomy-images/) to tells me what you think. 
    66 * __Let me know if it works__ - Use the _Compatibility_ widget on the [homepage](http://wordpress.org/extend/plugins/taxonomy-images/) to let me know that the current version works with your version of WordPress. 
     185* __Rate it!__ - Use the star tool on the right-hand sidebar of the [homepage](http://wordpress.org/extend/plugins/taxonomy-images/). 
     186 
     187* __Let me know if it works__ - Use the _Compatibility_ widget on the [homepage](http://wordpress.org/extend/plugins/taxonomy-images/) to let everyone know that the current version works with your version of WordPress. 
     188 
    67189* __Do you Twitter?__ Help promote by using this shortlink: [http://bit.ly/taxonomy-images](http://bit.ly/taxonomy-images) 
     190 
    68191* __Are you a writer?__ Help promote by writing an article on your website about this plugin. 
    69 * __Are you Super-Wicked-Awesome?__ If so, you can always [make a donation](http://mfields.org/donate/). 
     192 
     193* __Are you Super-Wicked-Awesome?__ If so, you can always [make a donation](http://wordpress.mfields.org/donate/). 
    70194 
    71195= Need More Taxonomy Plugins? = 
     
    782021. Log into WordPress and navigate to the "Plugins" panel. 
    792031. Activate the plugin. 
     2041. Click the "Taxonomy Images" link under the Settings section in the admin menu. There you can select the taxonomies that you would like to add image support for. 
     205 
     206== Upgrade Notice == 
     207 
     208= 0.7 = 
     209Complete rewrite. Better everything. Many bug fixes.  
    80210 
    81211==Changelog== 
     212 
     213= 0.7 = 
     214* No longer breaks display of the [Better Plugin Compatibility Control](http://wordpress.org/extend/plugins/better-plugin-compatibility-control/) plugin. 
     215* Created a custom filter interface for plugin and theme integration. 
     216* Lots of inline documentation added. 
     217* Added custom notices if plugin is used in an unsupported way. 
     218* No notices generated by PHP or WordPress. 
     219* Deprecated function calls removed. 
     220* Security updates. 
     221* All strings are now internationalized. 
     222* Add image to term functionality mimics "Add Featured Image". 
     223* Taxonomy modal button now available in search + upload states. 
     224* Image interface has been added to single term edit screen. 
     225* Users can now choose which taxonomys have image support. 
     226* All functions are now private. 
     227* Shortcode deprecated. 
     228* All global variables and constants have been removed or deprecated. 
     229 
     230= 0.6 = 
     231* Never released. 
     232* Completely recoded. 
    82233 
    83234= 0.5 = 
  • taxonomy-images/trunk/taxonomy-images.php

    r287106 r391118  
    11<?php 
    22/* 
    3 Plugin Name: Taxonomy Images BETA 
    4 Plugin URI: http://wordpress.mfields.org/plugins/taxonomy-images/ 
    5 Description: The Taxonomy Images plugin enables you to associate images from your Media Library to categories, tags and taxonomies. 
    6 Version: 0.5 
    7 Author: Michael Fields 
    8 Author URI: http://wordpress.mfields.org/ 
    9 License: GPLv2 
    10  
    11 Copyright 2010  Michael Fields  michael@mfields.org 
     3Plugin Name:          Taxonomy Images 
     4Plugin URI:           http://wordpress.mfields.org/plugins/taxonomy-images/ 
     5Description:          Associate images from your media library to categories, tags and custom taxonomies. 
     6Version:              0.7 
     7Author:               Michael Fields 
     8Author URI:           http://wordpress.mfields.org/ 
     9License:              GPLv2 
     10 
     11Copyright 2010-2011  Michael Fields  michael@mfields.org 
    1212 
    1313This program is free software; you can redistribute it and/or modify 
     
    2323along with this program; if not, write to the Free Software 
    2424Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
    25      
    26 TODO LIST: 
    27     5.  Add support for wp_list_categories() or create new functionality based on wp_list_categories() 
    2825*/ 
    2926 
    30 if( !function_exists( 'pr' ) ) { 
    31     function pr( $var ) { 
    32         print '<pre>' . print_r( $var, true ) . '</pre>'; 
    33     } 
    34 } 
    35  
    36 /* 2.9 Branch support */ 
    37 if( !function_exists( 'taxonomy_exists' ) ) { 
    38     function taxonomy_exists( $taxonomy ) { 
    39         global $wp_taxonomies; 
    40         return isset( $wp_taxonomies[$taxonomy] ); 
    41     } 
    42 } 
    43 if( !class_exists( 'taxonomy_images_plugin' ) ) { 
    44     /** 
    45     * Category Thumbs 
    46     * @author Michael Fields <michael@mfields.org> 
    47     * @copyright Copyright (c) 2009, Michael Fields. 
    48     * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License v2 
    49     * @package Plugins 
    50     * @filesource 
    51     */ 
    52     class taxonomy_images_plugin { 
    53         public $settings = array(); 
    54         public $locale = 'taxonomy_image_plugin'; 
    55         public $version = '0.5'; 
    56         private $permission = 'manage_categories'; 
    57         private $attr_slug = 'mf_term_id'; 
    58         private $detail_size = array( 75, 75, true ); 
    59         private $custom_taxonomies = array(); 
    60         private $current_taxonomy = false; 
    61         private $plugin_basename = ''; 
    62         public function __construct() { 
    63             /* Set Properties */ 
    64             $this->dir = dirname( __FILE__ ); 
    65             $this->uri = plugin_dir_url( __FILE__ ); 
    66             $this->url = $this->uri; 
    67             $this->settings = get_option( $this->locale ); 
    68             $this->plugin_basename = plugin_basename( __FILE__ ); 
    69              
    70             /* Plugin Activation Hooks */ 
    71             register_activation_hook( __FILE__, array( &$this, 'activate' ) ); 
    72              
    73             /* General Hooks. */ 
    74             add_action( 'init', array( &$this, 'add_new_image_size' ) ); 
    75             add_action( 'admin_init', array( &$this, 'register_settings' ) ); 
    76             add_action( 'admin_head', array( &$this, 'set_current_taxonomy' ), 10 ); 
    77             add_action( 'wp_head', array( &$this, 'set_current_taxonomy' ), 10 ); 
    78              
    79             /* Media Upload Thickbox Hooks. */ 
    80             add_filter( 'attachment_fields_to_edit', array( &$this, 'control_add_image_to_taxonomy' ), 20, 2 ); 
    81             add_action( 'admin_print_scripts-media-upload-popup', array( &$this, 'media_upload_popup_js' ), 2000 ); 
    82             add_action( 'wp_ajax_taxonomy_images_create_association', array( &$this, 'ajax_create_association' ), 10 ); 
    83             add_action( 'wp_ajax_taxonomy_images_remove_association', array( &$this, 'ajax_remove_association' ), 10 ); 
    84                          
    85             /* Category Admin Hooks. */ 
    86             add_action( 'admin_print_scripts-categories.php', array( &$this, 'scripts' ) ); 
    87             add_action( 'admin_print_styles-categories.php', array( &$this, 'styles' ) ); 
    88              
    89             /* 3.0 and beyond. Dynamically create hooks. */ 
    90             add_action( 'admin_init', array( &$this, 'admin_init' ) ); 
    91              
    92             /* 2.9 Support - hook into taxonomy terms administration panel. */ 
    93             add_filter( 'manage_categories_custom_column', array( &$this, 'category_rows' ), 15, 3 ); 
    94             add_filter( 'manage_categories_columns', array( &$this, 'category_columns' ) ); 
    95             add_filter( 'manage_edit-tags_columns', array( &$this, 'category_columns' ) ); 
    96              
    97             /* Styles and Scripts */ 
    98             add_action( 'admin_print_scripts-edit-tags.php', array( &$this, 'edit_tags_js' ) ); 
    99             add_action( 'admin_print_styles-edit-tags.php', array( &$this, 'edit_tags_css' ) ); 
    100              
    101             /* Custom Actions for front-end. */ 
    102             add_action( $this->locale . '_print_image_html', array( &$this, 'print_image_html' ), 1, 3 ); 
    103             add_shortcode( $this->locale, array( &$this, 'list_term_images_shortcode' ) ); 
    104         } 
    105         public function activate() { 
    106             add_option( $this->locale, array() ); 
    107         } 
    108         public function media_upload_popup_js() { 
    109             if( isset( $_GET[ $this->attr_slug ] ) ) { 
    110                 wp_enqueue_script( 'taxonomy-images-media-upload-popup', $this->uri . 'media-upload-popup.js', array( 'jquery' ), $this->version ); 
    111                 $term_id = (int) $_GET[ $this->attr_slug ]; 
    112                 wp_localize_script( 'taxonomy-images-media-upload-popup', 'taxonomyImagesPlugin', array ( 
    113                     'attr' => $this->attr( $term_id ), 
    114                     'locale' => $this->locale, 
    115                     'term_id' => $term_id, 
    116                     'attr_slug' => $this->attr_slug, 
    117                     'nonce_create' => wp_create_nonce( 'taxonomy-images-plugin-create-association' ), 
    118                     ) ); 
    119             } 
    120         } 
    121         public function edit_tags_js() { 
    122             wp_enqueue_script( 'thickbox' ); 
    123             wp_enqueue_script( 'taxonomy-images-edit-tags', $this->uri . 'edit-tags.js', array( 'jquery' ), $this->version ); 
    124             wp_localize_script( 'taxonomy-images-edit-tags', 'taxonomyImagesPlugin', array ( 
    125                 'nonce_remove' => wp_create_nonce( 'taxonomy-images-plugin-remove-association' ), 
    126                 'img_src' => $this->url . 'default-image.png', 
    127                 ) ); 
    128         } 
    129         public function edit_tags_css() { 
    130             wp_enqueue_style( 'thickbox' ); 
    131             wp_enqueue_style( 'taxonomy-images-edit-tags', $this->uri . 'admin.css', array(), $this->version, 'screen' ); 
    132         } 
    133         public function register_settings() { 
    134             register_setting( $this-locale, $this-locale, array( $this, 'sanitize_term_image_array' ) ); 
    135         } 
    136         /** 
    137          * Dynamically create hooks for the columns and rows of edit-tags.php 
    138          * @since 0.4.3 
    139          * @uses $wp_taxonomies 
    140          * @return void 
    141          */ 
    142         public function admin_init() { 
    143             global $wp_taxonomies; 
    144             foreach( $wp_taxonomies as $taxonomy => $taxonomies ) { 
    145                 add_filter( 'manage_' . $taxonomy . '_custom_column', array( &$this, 'category_rows' ), 10, 3 ); 
    146                 add_filter( 'manage_edit-' . $taxonomy . '_columns', array( &$this, 'category_columns' ), 10, 3 ); 
    147             } 
    148         } 
    149         public function get_fullsize_image_dimensions( $term_tax_id ) { 
    150             $post_id = false; 
    151             $meta = false; 
    152             if( array_key_exists( $term_tax_id, $this->settings ) ) { 
    153                 $post_id = $this->settings[$term_tax_id]; 
    154             } 
    155             if ( $post_id ) { 
    156                 $meta = get_post_meta( $post_id, '_wp_attachment_metadata', true ); 
    157             } 
    158             return $meta; 
    159         } 
    160         public function list_term_images_shortcode( $atts = array() ) { 
    161             $o = ''; 
    162             $defaults = array( 
    163                 // 'id' => false, 
    164                 'taxonomy' => 'category', 
    165                 'size' => 'detail', 
    166                 'template' => 'list' 
     27 
     28require_once( trailingslashit( dirname( __FILE__ ) ) . 'deprecated.php' ); 
     29require_once( trailingslashit( dirname( __FILE__ ) ) . 'public-filters.php' ); 
     30 
     31 
     32/** 
     33 * Version Number. 
     34 * 
     35 * @return    string    The plugin's version number. 
     36 * @access    private 
     37 * @since     0.7 
     38 */ 
     39function taxonomy_image_plugin_version() { 
     40    return '0.7'; 
     41} 
     42 
     43 
     44/** 
     45 * Get a url to a file in this plugin. 
     46 * 
     47 * @return    string 
     48 * @access    private 
     49 * @since     0.7 
     50 */ 
     51function taxonomy_image_plugin_url( $file = '' ) { 
     52    static $path = ''; 
     53    if ( empty( $path ) ) { 
     54        $path = plugin_dir_url( __FILE__ ); 
     55    } 
     56    return $path . $file; 
     57} 
     58 
     59 
     60/** 
     61 * Detail Image Size. 
     62 * 
     63 * @return    array     Configuration for the "detail" image size. 
     64 * @access    private 
     65 * @since     0.7 
     66 */ 
     67function taxonomy_image_plugin_detail_image_size() { 
     68    return array( 
     69        'name' => 'detail', 
     70        'size' => array( 75, 75, true ) 
     71        ); 
     72} 
     73 
     74 
     75/** 
     76 * Register custom image size with WordPress. 
     77 * 
     78 * @access    private 
     79 * @since     2010-10-28 
     80 */ 
     81function taxonomy_image_plugin_add_image_size() { 
     82    $detail = taxonomy_image_plugin_detail_image_size(); 
     83    add_image_size( 
     84        $detail['name'], 
     85        $detail['size'][0], 
     86        $detail['size'][1], 
     87        $detail['size'][2] 
     88        ); 
     89} 
     90add_action( 'init', 'taxonomy_image_plugin_add_image_size' ); 
     91 
     92 
     93/** 
     94 * Modal Button. 
     95 * 
     96 * Create a button in the modal media window to associate the current image to the term. 
     97 * 
     98 * @param     array     Multidimensional array representing the images form. 
     99 * @param     stdClass  WordPress post object. 
     100 * @return    array     The image's form array with added button if modal window was accessed by this script. 
     101 * 
     102 * @access    private 
     103 * @since     2010-10-28 
     104 * @alter     0.7 
     105 */ 
     106function taxonomy_image_plugin_modal_button( $fields, $post ) { 
     107    if ( isset( $fields['image-size'] ) && isset( $post->ID ) ) { 
     108        $image_id = (int) $post->ID; 
     109 
     110        $o = '<div class="taxonomy-image-modal-control" id="taxonomy-image-modal-control-' . $image_id . '">'; 
     111 
     112        $o.= '<span class="button create-association">' . sprintf( __( 'Associate with %1$s', 'taxonomy-images' ), '<span class="term-name">' . esc_html__( 'this term', 'taxonomy-images' ) . '</span>' ) . '</span>'; 
     113 
     114        $o.= '<span class="remove-association">' . sprintf( __( 'Remove association with %1$s', 'taxonomy-images' ), '<span class="term-name">' . esc_html__( 'this term', 'taxonomy-images' ) . '</span>' ) . '</span>'; 
     115 
     116        $o.= '<input class="taxonomy-image-button-image-id" name="' . esc_attr( 'taxonomy-image-button-image-id-' . $image_id ) . '" type="hidden" value="' . esc_attr( $image_id ) . '" />'; 
     117 
     118        $o.= '<input class="taxonomy-image-button-nonce-create" name="' . esc_attr( 'taxonomy-image-button-nonce-create-' . $image_id ) . '" type="hidden" value="' . wp_create_nonce( 'taxonomy-image-plugin-create-association' ) . '" />'; 
     119 
     120        $o.= '<input class="taxonomy-image-button-nonce-remove" name="' . esc_attr( 'taxonomy-image-button-nonce-remove-' . $image_id ) . '" type="hidden" value="' . wp_create_nonce( 'taxonomy-image-plugin-remove-association' ) . '" />'; 
     121 
     122        $o.= '</div>'; 
     123 
     124        $fields['image-size']['extra_rows']['taxonomy-image-plugin-button']['html'] = $o; 
     125    } 
     126    return $fields; 
     127} 
     128add_filter( 'attachment_fields_to_edit', 'taxonomy_image_plugin_modal_button', 20, 2 ); 
     129 
     130 
     131/** 
     132 * Get Image Source. 
     133 * 
     134 * Return a uri to a custom image size. 
     135 * 
     136 * If size doesn't exist, attempt to create a resized version. 
     137 * The output of this function should be escaped before printing to the browser. 
     138 * 
     139 * @param     int       Image ID. 
     140 * @return    string    URI of custom image on success; emtpy string otherwise. 
     141 * 
     142 * @access    private. 
     143 * @since     2010-10-28 
     144 */ 
     145function taxonomy_image_plugin_get_image_src( $id ) { 
     146    $detail = taxonomy_image_plugin_detail_image_size(); 
     147 
     148    /* Return url to custom intermediate size if it exists. */ 
     149    $img = image_get_intermediate_size( $id, $detail['name'] ); 
     150    if ( isset( $img['url'] ) ) { 
     151        return $img['url']; 
     152    } 
     153 
     154    /* Detail image does not exist, attempt to create it. */ 
     155    $wp_upload_dir = wp_upload_dir(); 
     156    if ( isset( $wp_upload_dir['basedir'] ) ) { 
     157 
     158        /* Create path to original uploaded image. */ 
     159        $path = trailingslashit( $wp_upload_dir['basedir'] ) . get_post_meta( $id, '_wp_attached_file', true ); 
     160        if ( is_file( $path ) ) { 
     161 
     162            /* Attempt to create a new downsized version of the original image. */ 
     163            $new = image_resize( $path, 
     164                $detail['size'][0], 
     165                $detail['size'][1], 
     166                $detail['size'][2] 
    167167                ); 
    168                  
    169             extract( shortcode_atts( $defaults, $atts ) ); 
    170              
    171             /* No taxonomy defined return an html comment. */ 
    172             if( !taxonomy_exists( $taxonomy ) ) { 
    173                 $tax = strip_tags( trim( $taxonomy ) ); 
    174                 return '<!--' . $this->locale . ' error: Taxonomy "' . $taxonomy . '" is not defined.-->'; 
    175             } 
    176              
    177             $terms = get_terms( $taxonomy ); 
    178              
    179             if( !is_wp_error( $terms ) ) { 
    180                 foreach ( $terms as $term ) { 
    181                     $open = ''; 
    182                     $close = ''; 
    183                     $img_tag = ''; 
    184                     $url = get_term_link( $term, $term->taxonomy ); 
    185                     $img = $this->get_image_html( $size, $term->term_taxonomy_id, true, 'left' ); 
    186                     $title = apply_filters( 'the_title', $term->name ); 
    187                     $title_attr = esc_attr( $term->name . ' (' . $term->count . ')' ); 
    188                     $description = apply_filters( 'the_content', $term->description ); 
    189                     if( $template === 'grid' ) { 
    190                         $o.= "\n\t" . '<div class="' . $this->locale . '-' . $template . '">'; 
    191                         $o.= "\n\t\t" . '<a style="float:left;" title="' . $title_attr . '" href="' . $url . '">' . $img . '</a>'; 
    192                         $o.= "\n\t" . '</div>'; 
    193                     } 
    194                     else { 
    195                         $o.= "\n\t\t" . '<a title="' . $title_attr . '" href="' . $url . '">' . $img . '</a>';; 
    196                         $o.= "\n\t\t" . '<h2 style="clear:none;margin-top:0;padding-top:0;line-height:1em;"><a href="' . $url . '">' . $title . '</a></h2>'; 
    197                         $o.= $description; 
    198                         $o.= "\n\t" . '<div style="clear:both;height:1.5em"></div>'; 
    199                         $o.= "\n"; 
    200                     } 
     168 
     169            /* Image creation successful. Generate and cache image metadata. Return url. */ 
     170            if ( ! is_wp_error( $new ) ) { 
     171                $meta = wp_generate_attachment_metadata( $id, $path ); 
     172                wp_update_attachment_metadata( $id, $meta ); 
     173                $img = image_get_intermediate_size( $id, $detail['name'] ); 
     174                if ( isset( $img['url'] ) ) { 
     175                    return $img['url']; 
    201176                } 
    202177            } 
    203             return $o; 
    204         } 
    205         public function set_current_taxonomy() { 
    206             if( is_admin() ) { 
    207                 global $hook_suffix; 
    208                 if( $hook_suffix === 'categories.php' ) 
    209                     $this->current_taxonomy = 'category'; 
    210                 if( $hook_suffix === 'edit-tags.php' && isset( $_GET['taxonomy'] ) ) 
    211                     $this->current_taxonomy = ( get_taxonomy( $_GET['taxonomy'] ) ) ? $_GET['taxonomy'] : false; 
     178        } 
     179    } 
     180 
     181    /* Custom intermediate size cannot be created, try for thumbnail. */ 
     182    $img = image_get_intermediate_size( $id, 'thumbnail' ); 
     183    if ( isset( $img['url'] ) ) { 
     184        return $img['url']; 
     185    } 
     186 
     187    /* Thumbnail cannot be found, try fullsize. */ 
     188    $url = wp_get_attachment_url( $id ); 
     189    if ( ! empty( $url ) ) { 
     190        return $url; 
     191    } 
     192 
     193    /* 
     194     * No image can be found. 
     195     * This is most likely caused by a user deleting an attachment before deleting it's association with a taxonomy. 
     196     * If we are in the administration panels: 
     197     * - Delete the association. 
     198     * - Return uri to default.png. 
     199     */ 
     200    if ( is_admin() ) { 
     201        $assoc = taxonomy_image_plugin_get_associations(); 
     202        foreach ( $assoc as $term => $img ) { 
     203            if ( $img === $id ) { 
     204                unset( $assoc[$term] ); 
    212205            } 
    213             else { 
    214                 global $wp_query; 
    215                 $q = $wp_query->get_queried_object(); 
    216                 $this->current_taxonomy = ( get_taxonomy( $q->taxonomy ) ) ? $q->taxonomy : false; 
     206        } 
     207        update_option( 'taxonomy_image_plugin', $assoc ); 
     208        return taxonomy_image_plugin_url( 'default.png' ); 
     209    } 
     210 
     211    /* 
     212     * No image can be found. 
     213     * Return path to blank-image.png. 
     214     */ 
     215    return taxonomy_image_plugin_url( 'blank.png' ); 
     216} 
     217 
     218 
     219/** 
     220 * Sanitize Associations. 
     221 * 
     222 * Ensures that all key/value pairs are positive integers. 
     223 * This filter will discard all zero and negative values. 
     224 * 
     225 * @param     array     An array of term_taxonomy_id/attachment_id pairs. 
     226 * @return    array     Sanitized version of parameter. 
     227 * 
     228 * @access    private 
     229 */ 
     230function taxonomy_image_plugin_sanitize_associations( $associations ) { 
     231    $o = array(); 
     232    foreach ( (array) $associations as $tt_id => $im_id ) { 
     233        $tt_id = absint( $tt_id ); 
     234        $im_id = absint( $im_id ); 
     235        if ( 0 < $tt_id && 0 < $im_id ) { 
     236            $o[$tt_id] = $im_id; 
     237        } 
     238    } 
     239    return $o; 
     240} 
     241 
     242 
     243/** 
     244 * Sanitize Settings. 
     245 * 
     246 * This function is responsible for ensuring that 
     247 * all values within the 'taxonomy_image_plugin_settings' 
     248 * options are of the appropriate type. 
     249 * 
     250 * @param     array     Unknown. 
     251 * @return    array     Multi-dimensional array of sanitized settings. 
     252 * 
     253 * @access    private 
     254 * @since     0.7 
     255 */ 
     256function taxonomy_image_plugin_settings_sanitize( $dirty ) { 
     257    $clean = array(); 
     258    if ( isset( $dirty['taxonomies'] ) ) { 
     259        $taxonomies = get_taxonomies(); 
     260        foreach ( (array) $dirty['taxonomies'] as $taxonomy ) { 
     261            if ( in_array( $taxonomy, $taxonomies ) ) { 
     262                $clean['taxonomies'][] = $taxonomy; 
    217263            } 
    218264        } 
    219         public function add_new_image_size() { 
    220             add_image_size( 'detail', $this->detail_size[0], $this->detail_size[1], $this->detail_size[2] ); 
    221         } 
    222         public function create_nonce_action( $action ) { 
    223             return $this->locale . '-' . $action; 
    224         } 
    225         public function installation_success( $c ) { 
    226             return $c . ' here I am'; 
    227         } 
    228         /* 
    229         * Ensures that all key/value pairs in an array are integers. 
    230         * @param array On dimensional array of term_taxonomy_id/attachment_id pairs. 
    231         * @return array  
    232         */ 
    233         public function sanitize_term_image_array( $settings ) { 
    234             $o = array(); 
    235             if( is_array( $settings ) ) { 
    236                 foreach( $settings as $key => $value ) { 
    237                     $o[ (int) $key ] = (int) $value; 
     265    } 
     266 
     267    $message = __( 'Taxonomies have been updated', 'taxonomy-images' ); 
     268    if ( empty( $clean ) ) { 
     269        $message = __( 'All taxonomies have been removed', 'taxonomy-images' ); 
     270    } 
     271 
     272    add_settings_error( 'taxonomy_image_plugin_settings', 'taxonomies_updated', $message, 'updated' ); 
     273 
     274    return $clean; 
     275} 
     276 
     277 
     278/** 
     279 * Register settings with WordPress. 
     280 * 
     281 * This plugin will store to sets of settings in the 
     282 * options table. The first is named 'taxonomy_image_plugin' 
     283 * and stores the associations between terms and images. The 
     284 * keys in this array represent the term_taxonomy_id of the 
     285 * term while the value represents the ID of the image 
     286 * attachment. 
     287 * 
     288 * The second setting is used to store everything else. As of 
     289 * version 0.7 it has one key named 'taxonomies' whichi is a 
     290 * flat array consisting of taxonomy names representing a 
     291 * black-list of registered taxonomies. These taxonomies will 
     292 * NOT be given an image UI. 
     293 * 
     294 * @access    private 
     295 */ 
     296function taxonomy_image_plugin_register_settings() { 
     297    register_setting( 
     298        'taxonomy_image_plugin', 
     299        'taxonomy_image_plugin', 
     300        'taxonomy_image_plugin_sanitize_associations' 
     301        ); 
     302    register_setting( 
     303        'taxonomy_image_plugin_settings', 
     304        'taxonomy_image_plugin_settings', 
     305        'taxonomy_image_plugin_settings_sanitize' 
     306        ); 
     307    add_settings_section( 
     308        'taxonomy_image_plugin_settings', 
     309        __( 'Settings', 'taxonomy-images' ), 
     310        '__return_false', 
     311        'taxonomy_image_plugin_settings' 
     312        ); 
     313    add_settings_field( 
     314        'taxonomy-images', 
     315        __( 'Taxonomies', 'taxonomy-images' ), 
     316        'taxonomy_image_plugin_control_taxonomies', 
     317        'taxonomy_image_plugin_settings', 
     318        'taxonomy_image_plugin_settings' 
     319        ); 
     320} 
     321add_action( 'admin_init', 'taxonomy_image_plugin_register_settings' ); 
     322 
     323 
     324/** 
     325 * Admin Menu. 
     326 * 
     327 * Create the admin menu link for the settings page. 
     328 * 
     329 * @access    private 
     330 * @since     0.7 
     331 */ 
     332function taxonomy_images_settings_menu() { 
     333    add_options_page( 
     334        __( 'Taxonomy Images', 'taxonomy-images' ), 
     335        __( 'Taxonomy Images', 'taxonomy-images' ), 
     336        'manage_options', 
     337        'taxonomy_image_plugin_settings', 
     338        'taxonomy_image_plugin_settings_page' 
     339        ); 
     340} 
     341add_action( 'admin_menu', 'taxonomy_images_settings_menu' ); 
     342 
     343 
     344/** 
     345 * Settings Page Template. 
     346 * 
     347 * This function in conjunction with others usei the WordPress 
     348 * Settings API to create a settings page where users can adjust 
     349 * the behaviour of this plugin. Please see the following functions 
     350 * for more insight on the output generated by this function: 
     351 * 
     352 * taxonomy_image_plugin_control_taxonomies() 
     353 * 
     354 * @access    private 
     355 * @since     0.7 
     356 */ 
     357function taxonomy_image_plugin_settings_page() { 
     358    print "\n" . '<div class="wrap">'; 
     359    screen_icon(); 
     360    print "\n" . '<h2>' . __( 'Taxonomy Images Plugin Settings', 'taxonomy-images' ) . '</h2>'; 
     361    print "\n" . '<div id="taxonomy-images">'; 
     362    print "\n" . '<form action="options.php" method="post">'; 
     363 
     364    settings_fields( 'taxonomy_image_plugin_settings' ); 
     365    do_settings_sections( 'taxonomy_image_plugin_settings' ); 
     366 
     367    print "\n" . '<div class="button-holder"><input name="Submit" type="submit" value="' . esc_attr__( 'Save Changes', 'taxonomy-images' ) . '" /></div>'; 
     368    print "\n" . '</div></form></div>'; 
     369} 
     370 
     371 
     372/** 
     373 * Taxonomy Checklist. 
     374 * 
     375 * @access    private 
     376 */ 
     377function taxonomy_image_plugin_control_taxonomies() { 
     378    $settings = get_option( 'taxonomy_image_plugin_settings' ); 
     379    $taxonomies = get_taxonomies( array(), 'objects' ); 
     380    foreach ( (array) $taxonomies as $taxonomy ) { 
     381        if ( ! isset( $taxonomy->name ) ) { 
     382            continue; 
     383        } 
     384        if ( ! isset( $taxonomy->label ) ) { 
     385            continue; 
     386        } 
     387        if ( ! isset( $taxonomy->show_ui ) || empty( $taxonomy->show_ui ) ) { 
     388            continue; 
     389        } 
     390        $id = 'taxonomy-images-' . $taxonomy->name; 
     391        $checked = ''; 
     392        if ( isset( $settings['taxonomies'] ) && in_array( $taxonomy->name, (array) $settings['taxonomies'] ) ) { 
     393            $checked = ' checked="checked"'; 
     394        } 
     395        print "\n" . '<p><label for="' . esc_attr( $id ) . '">'; 
     396        print '<input' . $checked . ' id="' . esc_attr( $id ) . '" type="checkbox" name="taxonomy_image_plugin_settings[taxonomies][]" value="' . esc_attr( $taxonomy->name ) . '">'; 
     397        print ' ' . esc_html( $taxonomy->label ) . '</label></p>'; 
     398    } 
     399} 
     400 
     401 
     402/** 
     403 * JSON Respose. 
     404 * Terminates script execution. 
     405 * 
     406 * @param     array     Associative array of values to be encoded in JSON. 
     407 * 
     408 * @access    private 
     409 */ 
     410function taxonomy_image_plugin_json_response( $args ) { 
     411    $response = wp_parse_args( $args, array( 
     412        'status' => 'bad', 
     413        'why'    => 'Unknown error encountered' 
     414    ) ); 
     415    header( 'Content-type: application/jsonrequest' ); 
     416    print json_encode( $response ); 
     417    exit; 
     418} 
     419 
     420 
     421/** 
     422 * Get Term Info 
     423 * 
     424 * Returns term info by term_taxonomy_id. 
     425 * 
     426 * @param     int       term_taxonomy_id 
     427 * @return    array     Keys: term_id (int) and taxonomy (string). 
     428 * 
     429 * @access    private 
     430 */ 
     431function taxonomy_image_plugin_get_term_info( $tt_id ) { 
     432    static $cache = array(); 
     433    if ( isset( $cache[$tt_id] ) ) { 
     434        return $cache[$tt_id]; 
     435    } 
     436    global $wpdb; 
     437    $data = $wpdb->get_results( $wpdb->prepare( "SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %s LIMIT 1", $tt_id ) ); 
     438    if ( isset( $data[0]->term_id ) ) { 
     439        $cache[$tt_id]['term_id'] = absint( $data[0]->term_id ); 
     440    } 
     441    if ( isset( $data[0]->taxonomy ) ) { 
     442        $cache[$tt_id]['taxonomy'] = sanitize_title_with_dashes( $data[0]->taxonomy ); 
     443    } 
     444    if ( isset( $cache[$tt_id] ) ) { 
     445        return $cache[$tt_id]; 
     446    } 
     447    return array(); 
     448} 
     449 
     450 
     451/** 
     452 * Check Taxonomy Permissions. 
     453 * 
     454 * Allows a permission check to be performed on a term 
     455 * when all you know is the term_taxonomy_id. 
     456 * 
     457 * @param     int       term_taxonomy_id 
     458 * @return    bool      True if user can edit terms, False if not. 
     459 * 
     460 * @access    private 
     461 */ 
     462function taxonomy_image_plugin_check_permissions( $tt_id ) { 
     463    $data = taxonomy_image_plugin_get_term_info( $tt_id ); 
     464    if ( ! isset( $data['taxonomy'] ) ) { 
     465        return false; 
     466    } 
     467 
     468    $taxonomy = get_taxonomy( $data['taxonomy'] ); 
     469    if ( ! isset( $taxonomy->cap->edit_terms ) ) { 
     470        return false; 
     471    } 
     472 
     473    return current_user_can( $taxonomy->cap->edit_terms ); 
     474} 
     475 
     476 
     477/** 
     478 * Create an association. 
     479 * 
     480 * Callback for the wp_ajax_{$_GET['action']} hook. 
     481 * 
     482 * @access    private 
     483 */ 
     484function taxonomy_image_plugin_create_association() { 
     485    if ( ! isset( $_POST['tt_id'] ) ) { 
     486        taxonomy_image_plugin_json_response( array( 
     487            'status' => 'bad', 
     488            'why'    => __( 'tt_id not sent', 'taxonomy-images' ), 
     489        ) ); 
     490    } 
     491 
     492    $tt_id = absint( $_POST['tt_id'] ); 
     493    if ( empty( $tt_id ) ) { 
     494        taxonomy_image_plugin_json_response( array( 
     495            'status' => 'bad', 
     496            'why'    => __( 'tt_id is empty', 'taxonomy-images' ), 
     497        ) ); 
     498    } 
     499 
     500    if ( ! taxonomy_image_plugin_check_permissions( $tt_id ) ) { 
     501        taxonomy_image_plugin_json_response( array( 
     502            'status' => 'bad', 
     503            'why'    => __( 'You do not have the correct capability to manage this term', 'taxonomy-images' ), 
     504        ) ); 
     505    } 
     506 
     507    if ( ! isset( $_POST['wp_nonce'] ) ) { 
     508        taxonomy_image_plugin_json_response( array( 
     509            'status' => 'bad', 
     510            'why'    => __( 'No nonce included.', 'taxonomy-images' ), 
     511        ) ); 
     512    } 
     513 
     514    if ( ! wp_verify_nonce( $_POST['wp_nonce'], 'taxonomy-image-plugin-create-association' ) ) { 
     515        taxonomy_image_plugin_json_response( array( 
     516            'status' => 'bad', 
     517            'why'    => __( 'Nonce did not match', 'taxonomy-images' ), 
     518        ) ); 
     519    } 
     520 
     521    if ( ! isset( $_POST['attachment_id'] ) ) { 
     522        taxonomy_image_plugin_json_response( array( 
     523            'status' => 'bad', 
     524            'why'    => __( 'Image id not sent', 'taxonomy-images' ) 
     525        ) ); 
     526    } 
     527 
     528    $image_id = absint( $_POST['attachment_id'] ); 
     529    if ( empty( $image_id ) ) { 
     530        taxonomy_image_plugin_json_response( array( 
     531            'status' => 'bad', 
     532            'why'    => __( 'Image id is not a positive integer', 'taxonomy-images' ) 
     533        ) ); 
     534    } 
     535 
     536    $assoc = taxonomy_image_plugin_get_associations(); 
     537    $assoc[$tt_id] = $image_id; 
     538    if ( update_option( 'taxonomy_image_plugin', taxonomy_image_plugin_sanitize_associations( $assoc ) ) ) { 
     539        taxonomy_image_plugin_json_response( array( 
     540            'status' => 'good', 
     541            'why'    => 'Image successfully associated', 
     542            'attachment_thumb_src' => taxonomy_image_plugin_get_image_src( $image_id ) 
     543        ) ); 
     544    } 
     545    else { 
     546        taxonomy_image_plugin_json_response( array( 
     547            'status' => 'bad', 
     548            'why'    => __( 'Association could not be created', 'taxonomy-images' ) 
     549        ) ); 
     550    } 
     551 
     552    /* Don't know why, but something didn't work. */ 
     553    taxonomy_image_plugin_json_response(); 
     554} 
     555add_action( 'wp_ajax_taxonomy_image_create_association', 'taxonomy_image_plugin_create_association' ); 
     556 
     557 
     558/** 
     559 * Remove an association. 
     560 * 
     561 * Removes an association from the setting stored in the database. 
     562 * Print json encoded message and terminates script execution. 
     563 * 
     564 * @access    private 
     565 */ 
     566function taxonomy_image_plugin_remove_association() { 
     567    if ( ! isset( $_POST['tt_id'] ) ) { 
     568        taxonomy_image_plugin_json_response( array( 
     569            'status' => 'bad', 
     570            'why'    => __( 'tt_id not sent', 'taxonomy-images' ), 
     571        ) ); 
     572    } 
     573 
     574    $tt_id = absint( $_POST['tt_id'] ); 
     575    if ( empty( $tt_id ) ) { 
     576        taxonomy_image_plugin_json_response( array( 
     577            'status' => 'bad', 
     578            'why'    => __( 'tt_id is empty', 'taxonomy-images' ), 
     579        ) ); 
     580    } 
     581 
     582    if ( ! taxonomy_image_plugin_check_permissions( $tt_id ) ) { 
     583        taxonomy_image_plugin_json_response( array( 
     584            'status' => 'bad', 
     585            'why'    => __( 'You do not have the correct capability to manage this term', 'taxonomy-images' ), 
     586        ) ); 
     587    } 
     588 
     589    if ( ! isset( $_POST['wp_nonce'] ) ) { 
     590        taxonomy_image_plugin_json_response( array( 
     591            'status' => 'bad', 
     592            'why'    => __( 'No nonce included', 'taxonomy-images' ), 
     593        ) ); 
     594    } 
     595 
     596    if ( ! wp_verify_nonce( $_POST['wp_nonce'], 'taxonomy-image-plugin-remove-association') ) { 
     597        taxonomy_image_plugin_json_response( array( 
     598            'status' => 'bad', 
     599            'why'    => __( 'Nonce did not match', 'taxonomy-images' ), 
     600        ) ); 
     601    } 
     602 
     603    $assoc = taxonomy_image_plugin_get_associations(); 
     604    if ( ! isset( $assoc[$tt_id] ) ) { 
     605        taxonomy_image_plugin_json_response( array( 
     606            'status' => 'good', 
     607            'why'    => __( 'Nothing to remove', 'taxonomy-images' ) 
     608        ) ); 
     609    } 
     610 
     611    unset( $assoc[$tt_id] ); 
     612 
     613    if ( update_option( 'taxonomy_image_plugin', $assoc ) ) { 
     614        taxonomy_image_plugin_json_response( array( 
     615            'status' => 'good', 
     616            'why'    => __( 'Association successfully removed', 'taxonomy-images' ) 
     617        ) ); 
     618    } 
     619    else { 
     620        taxonomy_image_plugin_json_response( array( 
     621            'status' => 'bad', 
     622            'why'    => __( 'Association could not be removed', 'taxonomy-images' ) 
     623        ) ); 
     624    } 
     625 
     626    /* Don't know why, but something didn't work. */ 
     627    taxonomy_image_plugin_json_response(); 
     628} 
     629add_action( 'wp_ajax_taxonomy_image_plugin_remove_association', 'taxonomy_image_plugin_remove_association' ); 
     630 
     631 
     632/** 
     633 * Get a list of user-defined associations. 
     634 * Associations are stored in the WordPress options table. 
     635 * 
     636 * @param     bool      Should WordPress query the database for the results 
     637 * @return    array     List of associations. Key => taxonomy_term_id; Value => image_id 
     638 * 
     639 * @access    private 
     640 */ 
     641function taxonomy_image_plugin_get_associations( $refresh = false ) { 
     642    static $associations = array(); 
     643    if ( empty( $associations ) || $refresh ) { 
     644        $associations = taxonomy_image_plugin_sanitize_associations( get_option( 'taxonomy_image_plugin' ) ); 
     645    } 
     646    return $associations; 
     647} 
     648add_action( 'init', 'taxonomy_image_plugin_get_associations' ); 
     649 
     650 
     651/** 
     652 * Dynamically create hooks for each taxonomy. 
     653 * 
     654 * Adds hooks for each taxonomy that the user has given 
     655 * an image interface to via settings page. These hooks 
     656 * enable the image interface on wp-admin/edit-tags.php. 
     657 * 
     658 * @access    private 
     659 * @since     0.4.3 
     660 * @alter     0.7 
     661 */ 
     662function taxonomy_image_plugin_add_dynamic_hooks() { 
     663    $settings = get_option( 'taxonomy_image_plugin_settings' ); 
     664    if ( ! isset( $settings['taxonomies'] ) ) { 
     665        return; 
     666    } 
     667    foreach ( $settings['taxonomies'] as $taxonomy ) { 
     668        add_filter( 'manage_' . $taxonomy . '_custom_column', 'taxonomy_image_plugin_taxonomy_rows', 15, 3 ); 
     669        add_filter( 'manage_edit-' . $taxonomy . '_columns',  'taxonomy_image_plugin_taxonomy_columns' ); 
     670        add_action( $taxonomy . '_edit_form_fields',          'taxonomy_image_plugin_edit_tag_form', 10, 2 ); 
     671    } 
     672} 
     673add_action( 'admin_init', 'taxonomy_image_plugin_add_dynamic_hooks' ); 
     674 
     675 
     676/** 
     677 * Edit Term Columns. 
     678 * 
     679 * Insert a new column on wp-admin/edit-tags.php. 
     680 * 
     681 * @see taxonomy_image_plugin_add_dynamic_hooks() 
     682 * 
     683 * @param     array     A list of columns. 
     684 * @return    array     List of columns with "Images" inserted after the checkbox. 
     685 * 
     686 * @access    private 
     687 * @since     0.4.3 
     688 */ 
     689function taxonomy_image_plugin_taxonomy_columns( $original_columns ) { 
     690    $new_columns = $original_columns; 
     691    array_splice( $new_columns, 1 ); 
     692    $new_columns['taxonomy_image_plugin'] = __( 'Image', 'taxonomy-images' ); /* Add custom column */ 
     693    return array_merge( $new_columns, $original_columns ); 
     694} 
     695 
     696 
     697/** 
     698 * Edit Term Rows. 
     699 * 
     700 * Create image control for each term row of wp-admin/edit-tags.php. 
     701 * 
     702 * @see taxonomy_image_plugin_add_dynamic_hooks() 
     703 * 
     704 * @param     string    Row. 
     705 * @param     string    Name of the current column. 
     706 * @param     int       Term ID. 
     707 * @return    string    @see taxonomy_image_plugin_control_image() 
     708 * 
     709 * @access    private 
     710 * @since     2010-11-08 
     711 */ 
     712function taxonomy_image_plugin_taxonomy_rows( $row, $column_name, $term_id ) { 
     713    if ( 'taxonomy_image_plugin' === $column_name ) { 
     714        global $taxonomy; 
     715        return $row . taxonomy_image_plugin_control_image( $term_id, $taxonomy ); 
     716    } 
     717    return $row; 
     718} 
     719 
     720 
     721/** 
     722 * Edit Term Control. 
     723 * 
     724 * Create image control for wp-admin/edit-tag-form.php. 
     725 * Hooked into the '{$taxonomy}_edit_form_fields' action. 
     726 * 
     727 * @param     stdClass  Term object. 
     728 * @param     string    Taxonomy slug. 
     729 * 
     730 * @access    private 
     731 * @since     2010-11-08 
     732 */ 
     733function taxonomy_image_plugin_edit_tag_form( $term, $taxonomy ) { 
     734    $taxonomy = get_taxonomy( $taxonomy ); 
     735    $name = __( 'term', 'taxonomy-images' ); 
     736    if ( isset( $taxonomy->labels->singular_name ) ) { 
     737        $name = strtolower( $taxonomy->labels->singular_name ); 
     738    } 
     739    ?> 
     740    <tr class="form-field hide-if-no-js"> 
     741        <th scope="row" valign="top"><label for="description"><?php _e( 'Image', 'taxonomy-images' ) ?></label></th> 
     742        <td> 
     743            <?php print taxonomy_image_plugin_control_image( $term->term_id, $taxonomy->name ); ?> 
     744            <div class="clear"></div> 
     745            <span class="description"><?php printf( __( 'Associate an image from your media library to this %1$s.', 'taxonomy-images' ), $name ); ?></span> 
     746        </td> 
     747    </tr> 
     748    <?php 
     749} 
     750 
     751/** 
     752 * Image Control. 
     753 * 
     754 * Creates all image controls on edit-tags.php. 
     755 * 
     756 * @todo      Remove rel tag from link... will need to adjust js to accomodate. 
     757 * @since     0.7 
     758 * @access    private 
     759 */ 
     760function taxonomy_image_plugin_control_image( $term_id, $taxonomy ) { 
     761 
     762    $term = get_term( $term_id, $taxonomy ); 
     763 
     764    $tt_id = 0; 
     765    if ( isset( $term->term_taxonomy_id ) ) { 
     766        $tt_id = (int) $term->term_taxonomy_id; 
     767    } 
     768 
     769    $taxonomy = get_taxonomy( $taxonomy ); 
     770 
     771    $name = __( 'term', 'taxonomy-images' ); 
     772    if ( isset( $taxonomy->labels->singular_name ) ) { 
     773        $name = strtolower( $taxonomy->labels->singular_name ); 
     774    } 
     775 
     776    $hide = ' hide'; 
     777    $attachment_id = 0; 
     778    $associations = taxonomy_image_plugin_get_associations(); 
     779    if ( isset( $associations[ $tt_id ] ) ) { 
     780        $attachment_id = (int) $associations[ $tt_id ]; 
     781        $hide = ''; 
     782    } 
     783 
     784    $img = taxonomy_image_plugin_get_image_src( $attachment_id ); 
     785 
     786    $term = get_term( $term_id, $taxonomy->name ); 
     787 
     788    $o = "\n" . '<div id="' . esc_attr( 'taxonomy-image-control-' . $tt_id ) . '" class="taxonomy-image-control hide-if-no-js">'; 
     789    $o.= "\n" . '<a class="thickbox taxonomy-image-thumbnail" href="' . esc_url( admin_url( 'media-upload.php' ) . '?type=image&tab=library&post_id=0&TB_iframe=true' ) . '" title="' . sprintf( esc_attr__( 'Associate an image with the %1$s named &#8220;%2$s&#8221;.', 'taxonomy-images' ), $name, $term->name ) . '"><img id="' . esc_attr( 'taxonomy_image_plugin_' . $tt_id ) . '" src="' . esc_url( $img ) . '" alt="" /></a>'; 
     790    $o.= "\n" . '<a class="control upload thickbox" href="' . esc_url( admin_url( 'media-upload.php' ) . '?type=image&tab=type&post_id=0&TB_iframe=true' ) . '" title="' . sprintf( esc_attr__( 'Upload a new image for this %s.', 'taxonomy-images' ), $name ) . '">' . esc_html__( 'Upload.', 'taxonomy-images' ) . '</a>'; 
     791    $o.= "\n" . '<a class="control remove' . $hide . '" href="#" id="' . esc_attr( 'remove-' . $tt_id ) . '" rel="' . esc_attr( $tt_id ) . '" title="' . sprintf( esc_attr__( 'Remove image from this %s.', 'taxonomy-images' ), $name ) . '">' . esc_html__( 'Delete', 'taxonomy-images' ) . '</a>'; 
     792    $o.= "\n" . '<input type="hidden" class="tt_id" name="' . esc_attr( 'tt_id-' . $tt_id ) . '" value="' . esc_attr( $tt_id ) . '" />'; 
     793 
     794    $o.= "\n" . '<input type="hidden" class="image_id" name="' . esc_attr( 'image_id-' . $tt_id ) . '" value="' . esc_attr( $attachment_id ) . '" />'; 
     795 
     796    if ( isset( $term->name ) && isset( $term->slug ) ) { 
     797        $o.= "\n" . '<input type="hidden" class="term_name" name="' . esc_attr( 'term_name-' . $term->slug ) . '" value="' . esc_attr( $term->name ) . '" />'; 
     798    } 
     799 
     800    $o.= "\n" . '</div>'; 
     801    return $o; 
     802} 
     803 
     804 
     805/** 
     806 * Custom javascript for modal media box. 
     807 * 
     808 * This script need to be added to all instance of the media upload box. 
     809 * 
     810 * @access    private 
     811 */ 
     812function taxonomy_image_plugin_media_upload_popup_js() { 
     813    wp_enqueue_script( 
     814        'taxonomy-images-media-upload-popup', 
     815        taxonomy_image_plugin_url( 'media-upload-popup.js' ), 
     816        array( 'jquery' ), 
     817        taxonomy_image_plugin_version() 
     818        ); 
     819    wp_localize_script( 'taxonomy-images-media-upload-popup', 'TaxonomyImagesModal', array ( 
     820        'termBefore'  => __( '&#8220;', 'taxonomy-images' ), 
     821        'termAfter'   => __( '&#8221;', 'taxonomy-images' ), 
     822        'associating' => __( 'Associating &#8230;', 'taxonomy-images' ), 
     823        'success'     => __( 'Successfully Associated', 'taxonomy-images' ), 
     824        'removing'    => __( 'Removing &#8230;', 'taxonomy-images' ), 
     825        'removed'     => __( 'Successfully Removed', 'taxonomy-images' ) 
     826        ) ); 
     827} 
     828add_action( 'admin_print_scripts-media-upload-popup', 'taxonomy_image_plugin_media_upload_popup_js' ); 
     829 
     830 
     831/** 
     832 * Custom javascript for wp-admin/edit-tags.php. 
     833 * 
     834 * @access    private 
     835 */ 
     836function taxonomy_image_plugin_edit_tags_js() { 
     837    if ( false == taxonomy_image_plugin_is_screen_active() ) { 
     838        return; 
     839    } 
     840    wp_enqueue_script( 
     841        'taxonomy-image-plugin-edit-tags', 
     842        taxonomy_image_plugin_url( 'edit-tags.js' ), 
     843        array( 'jquery', 'thickbox' ), 
     844        taxonomy_image_plugin_version() 
     845        ); 
     846    wp_localize_script( 'taxonomy-image-plugin-edit-tags', 'taxonomyImagesPlugin', array ( 
     847        'nonce'    => wp_create_nonce( 'taxonomy-image-plugin-remove-association' ), 
     848        'img_src'  => taxonomy_image_plugin_url( 'default.png' ), 
     849        'tt_id'    => 0, 
     850        'image_id' => 0, 
     851        ) ); 
     852} 
     853add_action( 'admin_print_scripts-edit-tags.php', 'taxonomy_image_plugin_edit_tags_js' ); 
     854 
     855 
     856/** 
     857 * Custom styles. 
     858 * 
     859 * @since     0.7 
     860 * @access    private 
     861 */ 
     862function taxonomy_image_plugin_css_admin() { 
     863    if ( false == taxonomy_image_plugin_is_screen_active() && 'admin_print_styles-media-upload-popup' != current_filter() ) { 
     864        return; 
     865    } 
     866    wp_enqueue_style( 
     867        'taxonomy-image-plugin-edit-tags', 
     868        taxonomy_image_plugin_url( 'admin.css' ), 
     869        array(), 
     870        taxonomy_image_plugin_version(), 
     871        'screen' 
     872        ); 
     873} 
     874add_action( 'admin_print_styles-edit-tags.php', 'taxonomy_image_plugin_css_admin' ); 
     875add_action( 'admin_print_styles-media-upload-popup', 'taxonomy_image_plugin_css_admin' ); 
     876 
     877 
     878/** 
     879 * Thickbox styles. 
     880 * 
     881 * @since     0.7 
     882 * @access    private 
     883 */ 
     884function taxonomy_image_plugin_css_thickbox() { 
     885    if ( false == taxonomy_image_plugin_is_screen_active() ) { 
     886        return; 
     887    } 
     888    wp_enqueue_style( 'thickbox' ); 
     889} 
     890add_action( 'admin_print_styles-edit-tags.php', 'taxonomy_image_plugin_css_thickbox' ); 
     891 
     892 
     893/** 
     894 * Public Styles. 
     895 * 
     896 * Prints custom css to all public pages. If you do not 
     897 * wish to have these styles included for you, please 
     898 * insert the following code into your theme's functions.php 
     899 * file: 
     900 * 
     901 * add_filter( 'taxonomy-images-disable-public-css', '__return_true' ); 
     902 * 
     903 * @since     0.7 
     904 * @access    private 
     905 */ 
     906function taxonomy_image_plugin_css_public() { 
     907    if ( apply_filters( 'taxonomy-images-disable-public-css', false ) ) { 
     908        return; 
     909    } 
     910    wp_enqueue_style( 
     911        'taxonomy-image-plugin-public', 
     912        taxonomy_image_plugin_url( 'style.css' ), 
     913        array(), 
     914        taxonomy_image_plugin_version(), 
     915        'screen' 
     916        ); 
     917} 
     918add_action( 'wp_print_styles', 'taxonomy_image_plugin_css_public' ); 
     919 
     920 
     921/** 
     922 * Activation. 
     923 * 
     924 * Two entries in the options table will created when this 
     925 * plugin is activated in the event that they do not exist. 
     926 * 
     927 * 'taxonomy_image_plugin' (array) A flat list of all assocaitions 
     928 * made by this plugin. Keys are integers representing the 
     929 * term_taxonomy_id of terms. Values are integers representing the 
     930 * ID property of an image attachment. 
     931 * 
     932 * 'taxonomy_image_plugin_settings' (array) A multi-dimensional array 
     933 * of user-defined settings. As of version 0.7, only one key is used: 
     934 * 'taxonomies' which is a whitelist of registered taxonomies having ui 
     935 * that support the custom image ui provided by this plugin. 
     936 * 
     937 * @access    private 
     938 * @alter     0.7 
     939 */ 
     940function taxonomy_image_plugin_activate() { 
     941    $associations = get_option( 'taxonomy_image_plugin' ); 
     942    if ( false === $associations ) { 
     943        add_option( 'taxonomy_image_plugin', array() ); 
     944    } 
     945    $settings = get_option( 'taxonomy_image_plugin_settings' ); 
     946    if ( false === $settings ) { 
     947        add_option( 'taxonomy_image_plugin_settings', array( 
     948            'taxonomies' => array() 
     949            ) ); 
     950    } 
     951} 
     952register_activation_hook( __FILE__, 'taxonomy_image_plugin_activate' ); 
     953 
     954 
     955/** 
     956 * Is Screen Active? 
     957 * 
     958 * @return    bool 
     959 * 
     960 * @access    private 
     961 * @since     0.7 
     962 */ 
     963function taxonomy_image_plugin_is_screen_active() { 
     964    $screen = get_current_screen(); 
     965    if ( ! isset( $screen->taxonomy ) ) { 
     966        return false; 
     967    } 
     968 
     969    $settings = get_option( 'taxonomy_image_plugin_settings' ); 
     970    if ( ! isset( $settings['taxonomies'] ) ) { 
     971        return false; 
     972    } 
     973 
     974    if ( in_array( $screen->taxonomy, $settings['taxonomies'] ) ) { 
     975        return true; 
     976    } 
     977    return false; 
     978} 
     979 
     980 
     981/** 
     982 * Cache Images 
     983 * 
     984 * Sets the WordPress object cache for all term images 
     985 * associated to the posts in the provided array. This 
     986 * function has been created to minimize queries when 
     987 * using this plugins get_the_terms() style function. 
     988 * 
     989 * @param     array          Post objects. 
     990 * 
     991 * @access    private 
     992 * @since     1.1 
     993 */ 
     994function taxonomy_image_plugin_cache_images( $posts ) { 
     995    $assoc = taxonomy_image_plugin_get_associations(); 
     996    if ( empty( $assoc ) ) { 
     997        return; 
     998    } 
     999 
     1000    $tt_ids = array(); 
     1001    foreach ( (array) $posts as $post ) { 
     1002        if ( ! isset( $post->ID ) || ! isset( $post->post_type ) ) { 
     1003            continue; 
     1004        } 
     1005 
     1006        $taxonomies = get_object_taxonomies( $post->post_type ); 
     1007        if ( empty( $taxonomies ) ) { 
     1008            continue; 
     1009        } 
     1010 
     1011        foreach ( $taxonomies as $taxonomy ) { 
     1012            $the_terms = get_the_terms( $post->ID, $taxonomy ); 
     1013            foreach ( (array) $the_terms as $term ) { 
     1014                if ( ! isset( $term->term_taxonomy_id ) ) { 
     1015                    continue; 
    2381016                } 
     1017                $tt_ids[] = $term->term_taxonomy_id; 
    2391018            } 
    240             return $o; 
    241         } 
    242         public function control_add_image_to_taxonomy( $fields, $post ) { 
    243             if( 
    244                 /* Newly uploaded image in media popup. */ 
    245                 ( isset( $_POST['fetch'] ) && 1 == $_POST['fetch'] ) || 
    246                  
    247                 /* Media Library tab of media popup.  */ 
    248                 ( isset( $_GET[$this->attr_slug] ) )  
    249             ) { 
    250                 $id = (int) $post->ID; 
    251                 $text = __( 'Add Thumbnail to Taxonomy', $this->locale ); 
    252                 $button = '<a rel="' . $id . '" class="button-primary ' . $this->locale . '" href="#" onclick="return false;">' . $text . '</a>'; 
    253                 $fields['image-size']['extra_rows']['taxonomy-image-plugin-button']['html'] = $button; 
    254             } 
    255             return $fields; 
    256         } 
    257         public function category_rows( $c, $column_name, $term_id ) { 
    258             if( $column_name === 'custom' ) { 
    259                 $term_id = $this->term_tax_id( (int) $term_id ); 
    260                 $href_library = $this->uri_library( $term_id ); 
    261                 $href_upload = $this->uri_upload( $term_id ); 
    262                 $id = $this->locale . '_' . $term_id; 
    263                 $attachment_id = ( isset( $this->settings[ $term_id ] ) ) ? (int) $this->settings[ $term_id ] : false; 
    264                 $img = ( $attachment_id ) ? $this->get_thumb( $attachment_id ) : $this->url . 'default-image.png'; 
    265                 $text = array( 
    266                     esc_attr__( 'Please enable javascript to activate the taxonomy images plugin.', $this->locale ), 
    267                     esc_attr__( 'Upload.', $this->locale ), 
    268                     esc_attr__( 'Upload a new image for this taxonomy.', $this->locale ), 
    269                     esc_attr__( 'Media Library.', $this->locale ), 
    270                     esc_attr__( 'Choose an image from you Media Library.', $this->locale ), 
    271                     esc_attr__( 'Delete', $this->locale ), 
    272                     esc_attr__( 'Remove this association.', $this->locale ), 
    273                     ); 
    274                 $class = array( 
    275                     'remove' => '', 
    276                     ); 
    277                 if( !$attachment_id ) { 
    278                     $class['remove'] = ' hide'; 
    279                 } 
    280                 return <<<EOF 
    281 {$c} 
    282 <img class="hide-if-js" src="{$this->url}no-javascript.png" alt="{$text[0]}" /> 
    283 <div id="taxonomy-image-control-{$term_id}" class="taxonomy-image-control hide-if-no-js"> 
    284     <a class="thickbox taxonomy-image-thumbnail" onclick="return false;" href="{$href_library}"><img id="{$id}" src="{$img}" alt="" /></a> 
    285     <a class="upload control thickbox" onclick="return false;" href="{$href_upload}" title="{$text[2]}">{$text[1]}</a> 
    286     <span id="remove-{$term_id}" rel="{$term_id}" class="delete control{$class['remove']}" title="{$text[1]}">{$text[0]}</span> 
    287 </div> 
    288 EOF; 
    289             } 
    290         } 
    291         public function category_columns( $original_columns ) { 
    292             $new_columns = $original_columns; 
    293             array_splice( $new_columns, 1 ); /* isolate the checkbox column */ 
    294             $new_columns['custom'] = __( 'Image', $this->locale ); /* Add custom column */ 
    295             return array_merge( $new_columns, $original_columns );  
    296         } 
    297         public function term_tax_id( $term ) { 
    298             if( empty( $this->current_taxonomy ) ) { 
    299                 return false; 
    300             } 
    301             $data = get_term( $term, $this->current_taxonomy ); 
    302             if( isset( $data->term_taxonomy_id ) && !empty( $data->term_taxonomy_id ) ) { 
    303                 return $data->term_taxonomy_id; 
    304             } 
    305             else { 
    306                 return false; 
    307             } 
    308         } 
    309         public function print_image_html( $size = 'medium', $term_tax_id = false, $title = true, $align = 'none' ) { 
    310             print $this->get_image_html( $size, $term_tax_id, $title, $align ); 
    311         } 
    312         /* 
    313         * @uses $wp_query 
    314         */ 
    315         public function get_image_html( $size = 'medium', $term_tax_id = false, $title = true, $align = 'none' ) { 
    316             $o = ''; 
    317             if( !$term_tax_id ) { 
    318                 global $wp_query; 
    319                 $mfields_queried_object = $wp_query->get_queried_object(); 
    320                 $term_tax_id = $mfields_queried_object->term_taxonomy_id; 
    321             } 
    322              
    323             $term_tax_id = (int) $term_tax_id; 
    324              
    325             if( isset( $this->settings[ $term_tax_id ] ) ) { 
    326                 $attachment_id = (int) $this->settings[ $term_tax_id ]; 
    327                 $alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ); 
    328                 $attachment = get_post( $attachment_id ); /* Just in case an attachment was deleted, but there is still a record for it in this plugins settings. */ 
    329                 if( $attachment !== NULL ) { 
    330                     $o = get_image_tag( $attachment_id, $alt, '', $align, $size ); 
    331                      
    332                 } 
    333             } 
    334             return $o; 
    335         } 
    336         /* 
    337         * @param $id (int) Attachment ID 
    338         */ 
    339         public function get_thumb( $id ) { 
    340             global $wp_version; 
    341              
    342             /* Get the originally uploaded size path. */ 
    343             list( $img_url, $img_path ) = get_attachment_icon_src( $id, true ); 
    344              
    345             /* Attepmt to get custom intermediate size. */ 
    346             $img = image_get_intermediate_size( $id, 'detail' ); 
    347              
    348             /* If custom intermediate size cannot be found, attempt to create it. */ 
    349             if( !$img ) { 
    350                  
    351                 /* Need to check to see if fullsize path can be found - sometimes this disappears during import/export. */ 
    352                 if( !is_file( $img_path ) ) { 
    353                     $wp_upload_dir = wp_upload_dir(); 
    354                     $img_path = $wp_upload_dir['path'] . get_post_meta( $id, '_wp_attached_file', true ); 
    355                 } 
    356                  
    357                 if( is_file( $img_path ) ) { 
    358                     $new = image_resize( $img_path, $this->detail_size[0], $this->detail_size[1], $this->detail_size[2] ); 
    359                      
    360                     if( !is_wp_error( $new ) ) { 
    361                         $meta = wp_generate_attachment_metadata( $id, $img_path ); 
    362                         wp_update_attachment_metadata( $id, $meta ); 
    363                         $img = image_get_intermediate_size( $id, 'detail' ); 
    364                     } 
    365                 } 
    366             } 
    367              
    368             /* Custom intermediate size cannot be created, try for thumbnail. */ 
    369             if( !$img ) { 
    370                 $img = image_get_intermediate_size( $id, 'thumbnail' ); 
    371             } 
    372              
    373             /* Thumbnail cannot be found, try fullsize. */ 
    374             if( !$img ) { 
    375                 $img['url'] = wp_get_attachment_url( $id ); 
    376             } 
    377              
    378             /* Administration */ 
    379             if( isset( $img['url'] ) && !empty( $img['url'] ) ) { 
    380                 return $img['url']; 
    381             } 
    382             else if( is_admin() ) { 
    383                 return $this->url . 'deleted-image.png'; 
    384             } 
    385             return false; 
    386         } 
    387         public function uri_library( $term_tax_id = 0 ) { 
    388             return admin_url( 'media-upload.php' ) . '?type=image&amp;tab=library&amp;' . $this->attr( $term_tax_id ). '&amp;TB_iframe=true'; 
    389         } 
    390         public function uri_upload( $term_tax_id = 0 ) { 
    391             return admin_url( 'media-upload.php' ) . '?type=image&amp;tab=type&amp;' . $this->attr( $term_tax_id ). '&amp;TB_iframe=true'; 
    392         } 
    393         public function attr( $id = 0 ) { /* $id = term_id */ 
    394             return $this->attr_slug . '=' . (int) $id; 
    395         } 
    396         public function ajax_create_association() { 
    397             /* Vars */ 
    398             global $wpdb; 
    399             $nonce = false; 
    400             $message = 'bad'; /* No need to localize. */ 
    401             $term_id = false; 
    402             $term_exists = false; 
    403             $attachment_id = false; 
    404             $attachement_exists = false; 
    405             $attachment_thumb_src = false; 
    406              
    407             /* Check permissions */ 
    408             if( !current_user_can( $this->permission ) ) { 
    409                 wp_die( __( 'Sorry, you do not have the propper permissions to access this resource.', $this->locale ) ); 
    410             } 
    411  
    412             /* Nonce does not match */ 
    413             if( !isset( $_POST['wp_nonce'] ) ) { 
    414                 wp_die( __( 'Access Denied to this resource 1.', $this->locale ) ); 
    415             } 
    416  
    417             if( !wp_verify_nonce( $_POST['wp_nonce'], 'taxonomy-images-plugin-create-association' ) ) { 
    418                 wp_die( __( 'Access Denied to this resource 2.', $this->locale ) ); 
    419             } 
    420  
    421             /* Check value of $_POST['term_id'] */ 
    422             if( isset( $_POST['term_id'] ) && !empty( $_POST['term_id'] ) && ( $_POST['term_id'] !== 'undefined' ) ) { 
    423                 $term_id = (int) $_POST['term_id']; 
    424             } 
    425  
    426             /* Query for $term_id */ 
    427             if( $term_id ) { 
    428                 $term_exists = $wpdb->get_var( "SELECT term_id FROM {$wpdb->term_taxonomy} WHERE `term_taxonomy_id` = {$term_id}" ); 
    429             } 
    430  
    431             /* Term does not exist - do not proceed. */  
    432             if( !$term_exists ) { 
    433                 wp_die( __( 'The term does not exist.', $this->locale ) ); 
    434             } 
    435                  
    436             /* Query for $attachment_id */ 
    437             if( isset( $_POST['attachment_id'] ) ) { 
    438                 $attachment_id = (int) $_POST['attachment_id']; 
    439                 if( is_object( get_post( $attachment_id ) ) ) { 
    440                     $attachement_exists = true; 
    441                 } 
    442             } 
    443  
    444             /* Attachment does not exist - do not proceed */ 
    445             if( !$attachement_exists ) { 
    446                 wp_die( __( 'The attachment does not exist.', $this->locale ) ); 
    447             } 
    448  
    449             $setting = get_option( $this->locale ); 
    450             $setting[$term_id] = $attachment_id; 
    451  
    452             if( update_option( $this->locale, $setting ) ) { 
    453                 $message = 'good'; /* No need to localize. */ 
    454                 $attachment_thumb_src = $this->get_thumb( $attachment_id ); 
    455             } 
    456  
    457             /* Output */ 
    458             mfields_json_response( array( 
    459                 'message' => $message, 
    460                 'term_id' => $term_id, 
    461                 'attachment_id' => $attachment_id, 
    462                 'attachment_thumb_src' => $attachment_thumb_src, 
    463                 ) ); 
    464  
    465         } 
    466         public function ajax_remove_association() { 
    467             /* Vars */ 
    468             global $wpdb; 
    469              
    470             /* Check permissions */ 
    471             if( !current_user_can( $this->permission ) ) { 
    472                 mfields_json_response( array( 
    473                     'status' => 'bad', 
    474                     'why' => __( 'Sorry, you do not have the propper permissions to access this resource.', $this->locale ), 
    475                     ) ); 
    476             } 
    477  
    478             /* Nonce does not match */ 
    479             if( !isset( $_POST['wp_nonce'] ) ) { 
    480                 mfields_json_response( array( 
    481                 'status' => 'bad', 
    482                 'why' => __( 'Access Denied to this resource.1', $this->locale ), 
    483                 ) ); 
    484             } 
    485  
    486             if( !wp_verify_nonce( $_POST['wp_nonce'], 'taxonomy-images-plugin-remove-association' ) ) { 
    487                 mfields_json_response( array( 
    488                 'status' => 'bad', 
    489                 'why' => __( 'Access Denied to this resource.2', $this->locale ), 
    490                 ) ); 
    491             } 
    492  
    493             /* Check value of $_POST['term_id'] */ 
    494             if( isset( $_POST['term_id'] ) && !empty( $_POST['term_id'] ) && ( $_POST['term_id'] !== 'undefined' ) ) { 
    495                 $term_id = (int) $_POST['term_id']; 
    496             } 
    497              
    498             $associations = get_option( $this->locale ); 
    499              
    500             if( array_key_exists( $term_id, $associations ) ) { 
    501                 unset( $associations[$term_id] ); 
    502             } 
    503              
    504             update_option( $this->locale, $associations ); 
    505              
    506             /* Output */ 
    507             mfields_json_response( array( 
    508                 'message' => 'good', 
    509                 ) ); 
    510  
    511         } 
    512     } 
    513     $taxonomy_images_plugin = new taxonomy_images_plugin(); 
    514 } 
    515  
    516 if( !function_exists( 'mfields_json_response' ) ) { 
    517     /** 
    518      * JSON Respose. 
    519      * Terminate script execution. 
    520      * @param array Values to be encoded in JSON. 
    521      * @return void 
    522      */ 
    523     function mfields_json_response( $response ) { 
    524         header( 'Content-type: application/jsonrequest' ); 
    525         print json_encode( $response ); 
    526         exit; 
    527     } 
    528 } 
    529  
    530  
    531  
    532  
    533  
    534  
    535  
    536  
    537  
    538  
    539  
    540  
    541  
    542 ?> 
     1019        } 
     1020    } 
     1021    $tt_ids = array_filter( array_unique( $tt_ids ) ); 
     1022 
     1023    $image_ids = array(); 
     1024    foreach ( $tt_ids as $tt_id ) { 
     1025        if ( ! isset( $assoc[$tt_id] ) ) { 
     1026            continue; 
     1027        } 
     1028        if ( in_array( $assoc[$tt_id], $image_ids ) ) { 
     1029            continue; 
     1030        } 
     1031        $image_ids[] = $assoc[$tt_id]; 
     1032    } 
     1033 
     1034    if ( empty( $image_ids ) ) { 
     1035        return; 
     1036    } 
     1037 
     1038    get_posts( array( 
     1039        'include'   => $image_ids, 
     1040        'post_type' => 'attachment' 
     1041        ) ); 
     1042} 
     1043 
     1044 
     1045/** 
     1046 * Cache Images 
     1047 * 
     1048 * Cache all term images associated with posts in 
     1049 * the main WordPress query. 
     1050 * 
     1051 * @param     array          Post objects. 
     1052 * 
     1053 * @access    private 
     1054 * @since     0.7 
     1055 */ 
     1056function taxonomy_image_plugin_cache_queried_images() { 
     1057    global $posts; 
     1058    taxonomy_image_plugin_cache_images( $posts ); 
     1059} 
     1060add_action( 'template_redirect', 'taxonomy_image_plugin_cache_queried_images' ); 
     1061 
     1062 
     1063/** 
     1064 * Check Taxonomy 
     1065 * 
     1066 * Wrapper for WordPress core functions taxonomy_exists(). 
     1067 * In the event that an unregistered taxonomy is passed a 
     1068 * E_USER_NOTICE will be generated. 
     1069 * 
     1070 * @param     string         Taxonomy name as registered with WordPress. 
     1071 * @param     string         Name of the current function or filter. 
     1072 * @return    bool           True if taxonomy exists, False if not. 
     1073 * 
     1074 * @access    private 
     1075 * @since     0.7 
     1076 */ 
     1077function taxonomy_image_plugin_check_taxonomy( $taxonomy, $filter ) { 
     1078    if ( ! taxonomy_exists( $taxonomy ) ) { 
     1079        trigger_error( sprintf( esc_html__( 'The %1$s argument for %2$s is set to %3$s which is not a registered taxonomy. Please check the spelling and update the argument.', 'taxonomy-images' ), 
     1080        '<var>' . esc_html__( 'taxonomy', 'taxonomy-images' ) . '</var>', 
     1081        '<code>' . esc_html( $filter ) . '</code>', 
     1082        '<strong>' . esc_html( $taxonomy ) . '</strong>' 
     1083        ) ); 
     1084        return false; 
     1085    } 
     1086 
     1087    $settings = get_option( 'taxonomy_image_plugin_settings' ); 
     1088 
     1089    if ( ! isset( $settings['taxonomies'] ) ) { 
     1090        $link = ''; 
     1091        if ( current_user_can( 'manage_options' ) ) { 
     1092            $link = '<a href="' . esc_url(  ) . '">' . esc_html__( 'Manage settings.', 'taxonomy-images' ) . '</a>'; 
     1093        } 
     1094        trigger_error( sprintf( esc_html__( 'No taxonomies have image support. %1$s', 'taxonomy-images' ), taxonomy_images_plugin_settings_page_link() ) ); 
     1095        return false; 
     1096    } 
     1097 
     1098    if ( ! in_array( $taxonomy, (array) $settings['taxonomies'] ) ) { 
     1099        trigger_error( sprintf( esc_html__( 'The %1$s taxonomy does not have image support. %2$s', 'taxonomy-images' ), 
     1100        '<strong>' . esc_html( $taxonomy ) . '</strong>', 
     1101        taxonomy_images_plugin_settings_page_link() 
     1102        ) ); 
     1103        return false; 
     1104    } 
     1105 
     1106    return true; 
     1107} 
     1108 
     1109 
     1110/** 
     1111 * Please Use Filter. 
     1112 * 
     1113 * Report to user that they are directly calling a function 
     1114 * instead of using supported filters. A E_USER_NOTICE will 
     1115 * be generated. 
     1116 * 
     1117 * @param     string         Name of function called. 
     1118 * @param     string         Name of filter to use instead. 
     1119 * 
     1120 * @access    private 
     1121 * @since     0.7 
     1122 */ 
     1123function taxonomy_image_plugin_please_use_filter( $function, $filter ) { 
     1124    trigger_error( sprintf( esc_html( 'The %1$s has been called directly. Please use the %2$s filter instead.' ), 
     1125    '<code>' . esc_html( $function . '()' ) . '</code>', 
     1126    '<code>' . esc_html( $filter ) . '</code>' 
     1127    ) ); 
     1128} 
     1129 
     1130 
     1131/** 
     1132 * Plugin Meta Links. 
     1133 * 
     1134 * Add a link to this plugin's setting page when it 
     1135 * displays in the table on wp-admin/plugins.php. 
     1136 * 
     1137 * @param     array          List of links. 
     1138 * @param     string         Current plugin being displayed in plugins.php. 
     1139 * @return    array          Potentially modified list of links. 
     1140 * 
     1141 * @access    private 
     1142 * @since     0.7 
     1143 */ 
     1144function taxonomy_images_plugin_row_meta( $links, $file ) { 
     1145    static $plugin_name = ''; 
     1146 
     1147    if ( empty( $plugin_name ) ) { 
     1148        $plugin_name = plugin_basename( __FILE__ ); 
     1149    } 
     1150 
     1151    if ( $plugin_name != $file ) { 
     1152        return $links; 
     1153    } 
     1154 
     1155    $link = taxonomy_images_plugin_settings_page_link( __( 'Settings', 'taxonomy-images' ) ); 
     1156    if ( ! empty( $link ) ) { 
     1157        $links[] = $link; 
     1158    } 
     1159 
     1160    $links[] = '<a href="http://wordpress.mfields.org/donate/">' . __( 'Donate', 'taxonomy-images' ) . '</a>'; 
     1161 
     1162    return $links; 
     1163} 
     1164add_filter( 'plugin_row_meta', 'taxonomy_images_plugin_row_meta', 10, 2 ); 
     1165 
     1166 
     1167/** 
     1168 * Settings Page Link. 
     1169 * 
     1170 * @param     array     Localized link text. 
     1171 * @return    string    HTML link to settings page. 
     1172 * 
     1173 * @access    private 
     1174 * @since     0.7 
     1175 */ 
     1176function taxonomy_images_plugin_settings_page_link( $link_text = '' ) { 
     1177    if ( empty( $link_text ) ) { 
     1178        $link_text = esc_html__( 'Manage Settings', 'taxonomy-images' ); 
     1179    } 
     1180 
     1181    $link = ''; 
     1182    if ( current_user_can( 'manage_options' ) ) { 
     1183        $link = '<a href="' . esc_url( add_query_arg( array( 'page' => 'taxonomy_image_plugin_settings' ), admin_url( 'options-general.php' ) ) ) . '">' . $link_text . '</a>'; 
     1184    } 
     1185 
     1186    return $link; 
     1187} 
Note: See TracChangeset for help on using the changeset viewer.