Plugin Directory

Changeset 2256871


Ignore:
Timestamp:
03/08/2020 07:10:54 PM (6 years ago)
Author:
elzahlan
Message:
  • Fix settings page issues
  • Fix compatibility with the latest Wordpress version
  • Rewrote the whole plugin from scratch, now the code is much efficient, readable and cleaner
Location:
categories-images
Files:
55 added
2 edited

Legend:

Unmodified
Added
Removed
  • categories-images/trunk/categories-images.php

    r1803373 r2256871  
    11<?php
    22/**
     3 * @package CategoriesImages
     4 */
     5
     6/**
    37 * Plugin Name: Categories Images
    4  * Plugin URI: http://zahlan.net/blog/2012/06/categories-images/
     8 * Plugin URI: http://zahlan.net/blog/categories-images/
    59 * Description: Categories Images Plugin allow you to add an image to category or any custom term.
    6  * Author: Muhammad Said El Zahlan
    7  * Version: 2.5.4
     10 * Author: Muhammad El Zahlan
     11 * Version: 3.0.0
    812 * Author URI: http://zahlan.net/
    913 * Domain Path: /languages
    1014 * Text Domain: categories-images
     15 * License: GPLv2 or later
    1116 */
     17
     18if (!defined('ABSPATH'))
     19    die;
    1220
    1321if (!defined('Z_PLUGIN_URL'))
    1422    define('Z_PLUGIN_URL', untrailingslashit(plugins_url('', __FILE__)));
    1523
    16 define('Z_IMAGE_PLACEHOLDER', Z_PLUGIN_URL."/images/placeholder.png");
    17 
    18 // l10n
    19 load_plugin_textdomain('categories-images', FALSE, 'categories-images/languages');
    20 
    21 add_action('admin_init', 'z_init');
    22 function z_init() {
    23     $z_taxonomies = get_taxonomies();
    24     if (is_array($z_taxonomies)) {
    25         $zci_options = get_option('zci_options');
    26        
    27         if (!is_array($zci_options))
    28             $zci_options = array();
    29        
    30         if (empty($zci_options['excluded_taxonomies']))
    31             $zci_options['excluded_taxonomies'] = array();
    32        
    33         foreach ($z_taxonomies as $z_taxonomy) {
    34             if (in_array($z_taxonomy, $zci_options['excluded_taxonomies']))
    35                 continue;
    36             add_action($z_taxonomy.'_add_form_fields', 'z_add_texonomy_field');
    37             add_action($z_taxonomy.'_edit_form_fields', 'z_edit_texonomy_field');
    38             add_filter( 'manage_edit-' . $z_taxonomy . '_columns', 'z_taxonomy_columns' );
    39             add_filter( 'manage_' . $z_taxonomy . '_custom_column', 'z_taxonomy_column', 10, 3 );
    40         }
     24class ZCategoriesImages
     25{
     26    public $plugin_name;
     27    private $zci_placeholder;
     28
     29    function __construct() {
     30        $this->plugin_name = plugin_basename(__FILE__);
     31
     32        // The placeholder image url
     33        $this->zci_placeholder = plugins_url('/assets/images/placeholder.png', __FILE__);
     34        add_action('admin_init', [$this, 'zAdminInit']);
     35
     36        // save our taxonomy image while edit or create term
     37        add_action('edit_term', [$this, 'zSaveTaxonomyImage']);
     38        add_action('create_term', [$this, 'zSaveTaxonomyImage']);
     39
     40        // Plugin menu in admin panel
     41        add_action('admin_menu', [$this, 'zSettingsMenu']);
     42
     43        // Settings page link in plugins list
     44        add_filter("plugin_action_links_{$this->plugin_name}", [$this, 'zSettingsLink']);
     45    }
     46
     47    function zAdminInit() {
     48        $z_taxonomies = get_taxonomies();
     49        if (is_array($z_taxonomies)) {
     50            $zci_options = get_option('zci_options');
     51           
     52            if (!is_array($zci_options))
     53                $zci_options = array();
     54           
     55            if (empty($zci_options['excluded_taxonomies']))
     56                $zci_options['excluded_taxonomies'] = array();
     57           
     58            foreach ($z_taxonomies as $z_taxonomy) {
     59                if (in_array($z_taxonomy, $zci_options['excluded_taxonomies']))
     60                    continue;
     61                add_action($z_taxonomy.'_add_form_fields', [$this, 'zAddTexonomyField']);
     62                add_action($z_taxonomy.'_edit_form_fields', [$this, 'zEditTexonomyField']);
     63                add_filter('manage_edit-'.$z_taxonomy.'_columns', [$this, 'zTaxonomyColumns']);
     64                add_filter('manage_'.$z_taxonomy.'_custom_column', [$this, 'zTaxonomyColumn'], 10, 3 );
     65
     66                // If tax is deleted
     67                add_action("delete_{$z_taxonomy}", function($tt_id) {
     68                    delete_option('z_taxonomy_image'.$tt_id);
     69                });
     70            }
     71        }
     72
     73        // Register styles and scripts
     74        if ( strpos( $_SERVER['SCRIPT_NAME'], 'edit-tags.php' ) > 0 || strpos( $_SERVER['SCRIPT_NAME'], 'term.php' ) > 0 ) {
     75            add_action('admin_enqueue_scripts', [$this, 'zAdminEnqueue']);
     76            add_action('quick_edit_custom_box', [$this, 'zQuickEditCustomBox'], 10, 3);
     77        }
     78
     79        // Register settings
     80        register_setting('zci_options', 'zci_options');
     81        add_settings_section('zci_settings', __('Categories Images settings', 'categories-images'), [$this, 'zSectionText'], 'zci-options');
     82        add_settings_field('z_excluded_taxonomies', __('Excluded Taxonomies', 'categories-images'), [$this, 'zExcludedTaxonomies'], 'zci-options', 'zci_settings');
     83    }
     84
     85    function zAdminEnqueue() {
     86        wp_enqueue_style('categories-images-styles', plugins_url('/assets/css/zci-styles.css', __FILE__));
     87        wp_enqueue_script('categories-images-scripts', plugins_url('/assets/js/zci-scripts.js', __FILE__));
     88
     89        $zci_js_config = [
     90            'wordpress_ver' => get_bloginfo("version"),
     91            'placeholder' => $this->zci_placeholder
     92        ];
     93        wp_localize_script('categories-images-scripts', 'zci_config', $zci_js_config);
     94    }
     95
     96    // add image field in add form
     97    function zAddTexonomyField() {
     98        if (get_bloginfo('version') >= 3.5)
     99            wp_enqueue_media();
     100        else {
     101            wp_enqueue_style('thickbox');
     102            wp_enqueue_script('thickbox');
     103        }
     104       
     105        echo '<div class="form-field">
     106            <label for="zci_taxonomy_image">' . __('Image', 'categories-images') . '</label>
     107            <input type="text" name="zci_taxonomy_image" id="zci_taxonomy_image" value="" />
     108            <br/>
     109            <button class="z_upload_image_button button">' . __('Upload/Add image', 'categories-images') . '</button>
     110        </div>';
     111    }
     112
     113    // add image field in edit form
     114    function zEditTexonomyField($taxonomy) {
     115        if (get_bloginfo('version') >= 3.5)
     116            wp_enqueue_media();
     117        else {
     118            wp_enqueue_style('thickbox');
     119            wp_enqueue_script('thickbox');
     120        }
     121       
     122        if ($this->zTaxonomyImageUrl( $taxonomy->term_id, NULL, TRUE ) == $this->zci_placeholder)
     123            $image_url = "";
     124        else
     125            $image_url = $this->zTaxonomyImageUrl( $taxonomy->term_id, NULL, TRUE );
     126        echo '<tr class="form-field">
     127            <th scope="row" valign="top"><label for="zci_taxonomy_image">' . __('Image', 'categories-images') . '</label></th>
     128            <td><img class="zci-taxonomy-image" src="' . $this->zTaxonomyImageUrl( $taxonomy->term_id, 'medium', TRUE ) . '"/><br/><input type="text" name="zci_taxonomy_image" id="zci_taxonomy_image" value="'.$image_url.'" /><br />
     129            <button class="z_upload_image_button button">' . __('Upload/Add image', 'categories-images') . '</button>
     130            <button class="z_remove_image_button button">' . __('Remove image', 'categories-images') . '</button>
     131            </td>
     132        </tr>';
     133    }
     134
     135    /**
     136     * Thumbnail column added to category admin.
     137     *
     138     * @access public
     139     * @param mixed $columns
     140     * @return void
     141     */
     142    function zTaxonomyColumns( $columns ) {
     143        $new_columns = array();
     144        $new_columns['cb'] = $columns['cb'];
     145        $new_columns['thumb'] = __('Image', 'categories-images');
     146
     147        unset( $columns['cb'] );
     148
     149        return array_merge( $new_columns, $columns );
     150    }
     151
     152    /**
     153     * Thumbnail column value added to category admin.
     154     *
     155     * @access public
     156     * @param mixed $columns
     157     * @param mixed $column
     158     * @param mixed $id
     159     * @return void
     160     */
     161    function zTaxonomyColumn( $columns, $column, $id ) {
     162        if ( $column == 'thumb' )
     163            $columns = '<span><img src="' . $this->zTaxonomyImageUrl($id, 'thumbnail', TRUE) . '" alt="' . __('Thumbnail', 'categories-images') . '" class="wp-post-image" /></span>';
     164       
     165        return $columns;
     166    }
     167
     168    function zQuickEditCustomBox($column_name, $screen, $name) {
     169        if ($column_name == 'thumb')
     170            echo '<fieldset>
     171            <div class="thumb inline-edit-col">
     172                <label>
     173                    <span class="title"><img src="" alt="Thumbnail"/></span>
     174                    <span class="input-text-wrap"><input type="text" name="zci_taxonomy_image" value="" class="tax_list" /></span>
     175                    <span class="input-text-wrap">
     176                        <button class="z_upload_image_button button">' . __('Upload/Add image', 'categories-images') . '</button>
     177                        <button class="z_remove_image_button button">' . __('Remove image', 'categories-images') . '</button>
     178                    </span>
     179                </label>
     180            </div>
     181        </fieldset>';
     182    }
     183
     184    function zSaveTaxonomyImage($term_id) {
     185        if(isset($_POST['zci_taxonomy_image'])) {
     186            update_option('z_taxonomy_image'.$term_id, $_POST['zci_taxonomy_image'], NULL);
     187        }
     188    }
     189
     190    // get attachment ID by image url
     191    function zGetAttachmentIdByUrl($image_src) {
     192        global $wpdb;
     193        $query = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid = %s", $image_src);
     194        $id = $wpdb->get_var($query);
     195        return (!empty($id)) ? $id : NULL;
     196    }
     197
     198    // get taxonomy image url for the given term_id (Place holder image by default)
     199    function zTaxonomyImageUrl($term_id = NULL, $size = 'full', $return_placeholder = FALSE) {
     200        if (!$term_id) {
     201            if (is_category())
     202                $term_id = get_query_var('cat');
     203            elseif (is_tag())
     204                $term_id = get_query_var('tag_id');
     205            elseif (is_tax()) {
     206                $current_term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
     207                $term_id = $current_term->term_id;
     208            }
     209        }
     210       
     211        $taxonomy_image_url = get_option('z_taxonomy_image'.$term_id);
     212        if(!empty($taxonomy_image_url)) {
     213            $attachment_id = $this->zGetAttachmentIdByUrl($taxonomy_image_url);
     214            if(!empty($attachment_id)) {
     215                $taxonomy_image_url = wp_get_attachment_image_src($attachment_id, $size);
     216                $taxonomy_image_url = $taxonomy_image_url[0];
     217            }
     218        }
     219
     220        if ($return_placeholder)
     221            return ($taxonomy_image_url != '') ? $taxonomy_image_url : $this->zci_placeholder;
     222        else
     223            return $taxonomy_image_url;
     224    }
     225
     226    // display taxonomy image for the given term_id
     227    function zTaxonomyImage($term_id = NULL, $size = 'full', $attr = NULL, $echo = TRUE) {
     228        if (!$term_id) {
     229            if (is_category())
     230                $term_id = get_query_var('cat');
     231            elseif (is_tag())
     232                $term_id = get_query_var('tag_id');
     233            elseif (is_tax()) {
     234                $current_term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
     235                $term_id = $current_term->term_id;
     236            }
     237        }
     238       
     239        $taxonomy_image_url = get_option('z_taxonomy_image'.$term_id);
     240        if(!empty($taxonomy_image_url)) {
     241            $attachment_id = $this->zGetAttachmentIdByUrl($taxonomy_image_url);
     242            if(!empty($attachment_id))
     243                $taxonomy_image = wp_get_attachment_image($attachment_id, $size, FALSE, $attr);
     244            else {
     245                $image_attr = '';
     246                if(is_array($attr)) {
     247                    if(!empty($attr['class']))
     248                        $image_attr .= ' class="'.$attr['class'].'" ';
     249                    if(!empty($attr['alt']))
     250                        $image_attr .= ' alt="'.$attr['alt'].'" ';
     251                    if(!empty($attr['width']))
     252                        $image_attr .= ' width="'.$attr['width'].'" ';
     253                    if(!empty($attr['height']))
     254                        $image_attr .= ' height="'.$attr['height'].'" ';
     255                    if(!empty($attr['title']))
     256                        $image_attr .= ' title="'.$attr['title'].'" ';
     257                }
     258                $taxonomy_image = '<img src="'.$taxonomy_image_url.'" '.$image_attr.'/>';
     259            }
     260        }
     261        else{
     262            $taxonomy_image = '';
     263        }
     264
     265        if ($echo)
     266            echo $taxonomy_image;
     267        else
     268            return $taxonomy_image;
     269    }
     270
     271    function zSettingsMenu() {
     272        add_menu_page(__('Categories Images settings', 'categories-images'), __('Categories Images', 'categories-images'), 'manage_options', 'zci_settings', [$this, 'zSettingsPage'], 'dashicons-format-image', 80);
     273    }
     274
     275    // Plugin option page
     276    function zSettingsPage() {
     277        if (!current_user_can('manage_options'))
     278            wp_die(__( 'You do not have sufficient permissions to access this page.', 'categories-images'));
     279        require_once plugin_dir_path(__FILE__).'templates/admin.php';
     280    }
     281
     282    function zSettingsLink($links) {
     283        $settings_link = '<a href="admin.php?page=zci_settings">Settings</a>';
     284        array_push($links, $settings_link);
     285        return $links;
     286    }
     287
     288    // Settings section description
     289    function zSectionText() {
     290        echo '<p>'.__('Please select the taxonomies you want to exclude it from Categories Images plugin', 'categories-images').'</p>';
     291    }
     292
     293    // Excluded taxonomies checkboxs
     294    function zExcludedTaxonomies() {
     295        $options = get_option('zci_options');
     296        $disabled_taxonomies = ['nav_menu', 'link_category', 'post_format'];
     297        foreach (get_taxonomies() as $tax) : if (in_array($tax, $disabled_taxonomies)) continue; ?>
     298            <input type="checkbox" name="zci_options[excluded_taxonomies][<?php echo $tax ?>]" value="<?php echo $tax ?>" <?php checked(isset($options['excluded_taxonomies'][$tax])); ?> /> <?php echo $tax ;?><br />
     299        <?php endforeach;
     300    }
     301   
     302    function activate() {
     303        // Things will happen if the plugin activated.
     304        flush_rewrite_rules();
     305    }
     306
     307    function deactivate() {
     308        // Things will happen if the plugin deactivated.
     309        flush_rewrite_rules();
    41310    }
    42311}
    43312
    44 function z_add_style() {
    45     echo '<style type="text/css" media="screen">
    46         th.column-thumb {width:60px;}
    47         .form-field img.taxonomy-image {border:1px solid #eee;max-width:300px;max-height:300px;}
    48         .inline-edit-row fieldset .thumb label span.title {width:48px;height:48px;border:1px solid #eee;display:inline-block;}
    49         .column-thumb span {width:48px;height:48px;border:1px solid #eee;display:inline-block;}
    50         .inline-edit-row fieldset .thumb img,.column-thumb img {width:48px;height:48px;}
    51     </style>';
     313if (class_exists('ZCategoriesImages')) {
     314    $z_categories_images = new ZCategoriesImages();
     315
     316    // After activating the plugin
     317    register_activation_hook(__FILE__, [$z_categories_images, 'activate']);
     318
     319    // After deactivating the plugin
     320    register_deactivation_hook(__FILE__, [$z_categories_images, 'deactivate']);
     321   
     322    function z_taxonomy_image_url($term_id = NULL, $size = 'full', $return_placeholder = FALSE) {
     323        $zci = new ZCategoriesImages();
     324        return $zci->zTaxonomyImageUrl($term_id, $size, $return_placeholder);
     325    }
     326
     327    function z_taxonomy_image($term_id = NULL, $size = 'full', $attr = NULL, $echo = TRUE) {
     328        $zci = new ZCategoriesImages();
     329        return $zci->zTaxonomyImage($term_id, $size, $attr, $echo);
     330    }
    52331}
    53 
    54 // add image field in add form
    55 function z_add_texonomy_field() {
    56     if (get_bloginfo('version') >= 3.5)
    57         wp_enqueue_media();
    58     else {
    59         wp_enqueue_style('thickbox');
    60         wp_enqueue_script('thickbox');
    61     }
    62    
    63     echo '<div class="form-field">
    64         <label for="taxonomy_image">' . __('Image', 'categories-images') . '</label>
    65         <input type="text" name="taxonomy_image" id="taxonomy_image" value="" />
    66         <br/>
    67         <button class="z_upload_image_button button">' . __('Upload/Add image', 'categories-images') . '</button>
    68     </div>'.z_script();
    69 }
    70 
    71 // add image field in edit form
    72 function z_edit_texonomy_field($taxonomy) {
    73     if (get_bloginfo('version') >= 3.5)
    74         wp_enqueue_media();
    75     else {
    76         wp_enqueue_style('thickbox');
    77         wp_enqueue_script('thickbox');
    78     }
    79    
    80     if (z_taxonomy_image_url( $taxonomy->term_id, NULL, TRUE ) == Z_IMAGE_PLACEHOLDER)
    81         $image_url = "";
    82     else
    83         $image_url = z_taxonomy_image_url( $taxonomy->term_id, NULL, TRUE );
    84     echo '<tr class="form-field">
    85         <th scope="row" valign="top"><label for="taxonomy_image">' . __('Image', 'categories-images') . '</label></th>
    86         <td><img class="taxonomy-image" src="' . z_taxonomy_image_url( $taxonomy->term_id, 'medium', TRUE ) . '"/><br/><input type="text" name="taxonomy_image" id="taxonomy_image" value="'.$image_url.'" /><br />
    87         <button class="z_upload_image_button button">' . __('Upload/Add image', 'categories-images') . '</button>
    88         <button class="z_remove_image_button button">' . __('Remove image', 'categories-images') . '</button>
    89         </td>
    90     </tr>'.z_script();
    91 }
    92 
    93 // upload using wordpress upload
    94 function z_script() {
    95     return '<script type="text/javascript">
    96         jQuery(document).ready(function($) {
    97             var wordpress_ver = "'.get_bloginfo("version").'", upload_button;
    98             $(".z_upload_image_button").click(function(event) {
    99                 upload_button = $(this);
    100                 var frame;
    101                 if (wordpress_ver >= "3.5") {
    102                     event.preventDefault();
    103                     if (frame) {
    104                         frame.open();
    105                         return;
    106                     }
    107                     frame = wp.media();
    108                     frame.on( "select", function() {
    109                         // Grab the selected attachment.
    110                         var attachment = frame.state().get("selection").first();
    111                         frame.close();
    112                         if (upload_button.parent().prev().children().hasClass("tax_list")) {
    113                             upload_button.parent().prev().children().val(attachment.attributes.url);
    114                             upload_button.parent().prev().prev().children().attr("src", attachment.attributes.url);
    115                         }
    116                         else
    117                             $("#taxonomy_image").val(attachment.attributes.url);
    118                     });
    119                     frame.open();
    120                 }
    121                 else {
    122                     tb_show("", "media-upload.php?type=image&amp;TB_iframe=true");
    123                     return false;
    124                 }
    125             });
    126            
    127             $(".z_remove_image_button").click(function() {
    128                 $(".taxonomy-image").attr("src", "'.Z_IMAGE_PLACEHOLDER.'");
    129                 $("#taxonomy_image").val("");
    130                 $(this).parent().siblings(".title").children("img").attr("src","' . Z_IMAGE_PLACEHOLDER . '");
    131                 $(".inline-edit-col :input[name=\'taxonomy_image\']").val("");
    132                 return false;
    133             });
    134            
    135             if (wordpress_ver < "3.5") {
    136                 window.send_to_editor = function(html) {
    137                     imgurl = $("img",html).attr("src");
    138                     if (upload_button.parent().prev().children().hasClass("tax_list")) {
    139                         upload_button.parent().prev().children().val(imgurl);
    140                         upload_button.parent().prev().prev().children().attr("src", imgurl);
    141                     }
    142                     else
    143                         $("#taxonomy_image").val(imgurl);
    144                     tb_remove();
    145                 }
    146             }
    147            
    148             $(".editinline").click(function() {
    149                 var tax_id = $(this).parents("tr").attr("id").substr(4);
    150                 var thumb = $("#tag-"+tax_id+" .thumb img").attr("src");
    151 
    152                 if (thumb != "' . Z_IMAGE_PLACEHOLDER . '") {
    153                     $(".inline-edit-col :input[name=\'taxonomy_image\']").val(thumb);
    154                 } else {
    155                     $(".inline-edit-col :input[name=\'taxonomy_image\']").val("");
    156                 }
    157                
    158                 $(".inline-edit-col .title img").attr("src",thumb);
    159             });
    160         });
    161     </script>';
    162 }
    163 
    164 // save our taxonomy image while edit or save term
    165 add_action('edit_term','z_save_taxonomy_image');
    166 add_action('create_term','z_save_taxonomy_image');
    167 function z_save_taxonomy_image($term_id) {
    168     if(isset($_POST['taxonomy_image']))
    169         update_option('z_taxonomy_image'.$term_id, $_POST['taxonomy_image'], NULL);
    170 }
    171 
    172 // get attachment ID by image url
    173 function z_get_attachment_id_by_url($image_src) {
    174     global $wpdb;
    175     $query = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid = %s", $image_src);
    176     $id = $wpdb->get_var($query);
    177     return (!empty($id)) ? $id : NULL;
    178 }
    179 
    180 // get taxonomy image url for the given term_id (Place holder image by default)
    181 function z_taxonomy_image_url($term_id = NULL, $size = 'full', $return_placeholder = FALSE) {
    182     if (!$term_id) {
    183         if (is_category())
    184             $term_id = get_query_var('cat');
    185         elseif (is_tag())
    186             $term_id = get_query_var('tag_id');
    187         elseif (is_tax()) {
    188             $current_term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
    189             $term_id = $current_term->term_id;
    190         }
    191     }
    192    
    193     $taxonomy_image_url = get_option('z_taxonomy_image'.$term_id);
    194     if(!empty($taxonomy_image_url)) {
    195         $attachment_id = z_get_attachment_id_by_url($taxonomy_image_url);
    196         if(!empty($attachment_id)) {
    197             $taxonomy_image_url = wp_get_attachment_image_src($attachment_id, $size);
    198             $taxonomy_image_url = $taxonomy_image_url[0];
    199         }
    200     }
    201 
    202     if ($return_placeholder)
    203         return ($taxonomy_image_url != '') ? $taxonomy_image_url : Z_IMAGE_PLACEHOLDER;
    204     else
    205         return $taxonomy_image_url;
    206 }
    207 
    208 function z_quick_edit_custom_box($column_name, $screen, $name) {
    209     if ($column_name == 'thumb')
    210         echo '<fieldset>
    211         <div class="thumb inline-edit-col">
    212             <label>
    213                 <span class="title"><img src="" alt="Thumbnail"/></span>
    214                 <span class="input-text-wrap"><input type="text" name="taxonomy_image" value="" class="tax_list" /></span>
    215                 <span class="input-text-wrap">
    216                     <button class="z_upload_image_button button">' . __('Upload/Add image', 'categories-images') . '</button>
    217                     <button class="z_remove_image_button button">' . __('Remove image', 'categories-images') . '</button>
    218                 </span>
    219             </label>
    220         </div>
    221     </fieldset>';
    222 }
    223 
    224 /**
    225  * Thumbnail column added to category admin.
    226  *
    227  * @access public
    228  * @param mixed $columns
    229  * @return void
    230  */
    231 function z_taxonomy_columns( $columns ) {
    232     $new_columns = array();
    233     $new_columns['cb'] = $columns['cb'];
    234     $new_columns['thumb'] = __('Image', 'categories-images');
    235 
    236     unset( $columns['cb'] );
    237 
    238     return array_merge( $new_columns, $columns );
    239 }
    240 
    241 /**
    242  * Thumbnail column value added to category admin.
    243  *
    244  * @access public
    245  * @param mixed $columns
    246  * @param mixed $column
    247  * @param mixed $id
    248  * @return void
    249  */
    250 function z_taxonomy_column( $columns, $column, $id ) {
    251     if ( $column == 'thumb' )
    252         $columns = '<span><img src="' . z_taxonomy_image_url($id, 'thumbnail', TRUE) . '" alt="' . __('Thumbnail', 'categories-images') . '" class="wp-post-image" /></span>';
    253    
    254     return $columns;
    255 }
    256 
    257 // Change 'insert into post' to 'use this image'
    258 function z_change_insert_button_text($safe_text, $text) {
    259     return str_replace("Insert into Post", "Use this image", $text);
    260 }
    261 
    262 // Style the image in category list
    263 if ( strpos( $_SERVER['SCRIPT_NAME'], 'edit-tags.php' ) > 0 ) {
    264     add_action( 'admin_head', 'z_add_style' );
    265     add_action('quick_edit_custom_box', 'z_quick_edit_custom_box', 10, 3);
    266     add_filter("attribute_escape", "z_change_insert_button_text", 10, 2);
    267 }
    268 
    269 // New menu submenu for plugin options in Settings menu
    270 add_action('admin_menu', 'z_options_menu');
    271 function z_options_menu() {
    272     add_options_page(__('Categories Images settings', 'categories-images'), __('Categories Images', 'categories-images'), 'manage_options', 'zci-options', 'zci_options');
    273     add_action('admin_init', 'z_register_settings');
    274 }
    275 
    276 // Register plugin settings
    277 function z_register_settings() {
    278     register_setting('zci_options', 'zci_options', 'z_options_validate');
    279     add_settings_section('zci_settings', __('Categories Images settings', 'categories-images'), 'z_section_text', 'zci-options');
    280     add_settings_field('z_excluded_taxonomies', __('Excluded Taxonomies', 'categories-images'), 'z_excluded_taxonomies', 'zci-options', 'zci_settings');
    281 }
    282 
    283 // Settings section description
    284 function z_section_text() {
    285     echo '<p>'.__('Please select the taxonomies you want to exclude it from Categories Images plugin', 'categories-images').'</p>';
    286 }
    287 
    288 // Excluded taxonomies checkboxs
    289 function z_excluded_taxonomies() {
    290     $options = get_option('zci_options');
    291     $disabled_taxonomies = array('nav_menu', 'link_category', 'post_format');
    292     foreach (get_taxonomies() as $tax) : if (in_array($tax, $disabled_taxonomies)) continue; ?>
    293         <input type="checkbox" name="zci_options[excluded_taxonomies][<?php echo $tax ?>]" value="<?php echo $tax ?>" <?php checked(isset($options['excluded_taxonomies'][$tax])); ?> /> <?php echo $tax ;?><br />
    294     <?php endforeach;
    295 }
    296 
    297 // Validating options
    298 function z_options_validate($input) {
    299     return $input;
    300 }
    301 
    302 // Plugin option page
    303 function zci_options() {
    304     if (!current_user_can('manage_options'))
    305         wp_die(__( 'You do not have sufficient permissions to access this page.', 'categories-images'));
    306         $options = get_option('zci_options');
    307     ?>
    308     <div class="wrap">
    309         <?php screen_icon(); ?>
    310         <h2><?php _e('Categories Images', 'categories-images'); ?></h2>
    311         <form method="post" action="options.php">
    312             <?php settings_fields('zci_options'); ?>
    313             <?php do_settings_sections('zci-options'); ?>
    314             <?php submit_button(); ?>
    315         </form>
    316     </div>
    317 <?php
    318 }
    319 
    320 // display taxonomy image for the given term_id
    321 function z_taxonomy_image($term_id = NULL, $size = 'full', $attr = NULL, $echo = TRUE) {
    322     if (!$term_id) {
    323         if (is_category())
    324             $term_id = get_query_var('cat');
    325         elseif (is_tag())
    326             $term_id = get_query_var('tag_id');
    327         elseif (is_tax()) {
    328             $current_term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
    329             $term_id = $current_term->term_id;
    330         }
    331     }
    332    
    333     $taxonomy_image_url = get_option('z_taxonomy_image'.$term_id);
    334     if(!empty($taxonomy_image_url)) {
    335         $attachment_id = z_get_attachment_id_by_url($taxonomy_image_url);
    336         if(!empty($attachment_id))
    337             $taxonomy_image = wp_get_attachment_image($attachment_id, $size, FALSE, $attr);
    338         else {
    339             $image_attr = '';
    340             if(is_array($attr)) {
    341                 if(!empty($attr['class']))
    342                     $image_attr .= ' class="'.$attr['class'].'" ';
    343                 if(!empty($attr['alt']))
    344                     $image_attr .= ' alt="'.$attr['alt'].'" ';
    345                 if(!empty($attr['width']))
    346                     $image_attr .= ' width="'.$attr['width'].'" ';
    347                 if(!empty($attr['height']))
    348                     $image_attr .= ' height="'.$attr['height'].'" ';
    349                 if(!empty($attr['title']))
    350                     $image_attr .= ' title="'.$attr['title'].'" ';
    351             }
    352             $taxonomy_image = '<img src="'.$taxonomy_image_url.'" '.$image_attr.'/>';
    353         }
    354     }
    355     else{
    356         $taxonomy_image = '';
    357     }
    358 
    359     if ($echo)
    360         echo $taxonomy_image;
    361     else
    362         return $taxonomy_image;
    363 }
  • categories-images/trunk/readme.txt

    r1803373 r2256871  
    22Contributors: elzahlan
    33Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=G8LC4VSYKYSGA
    4 Tags: Category Image, Category Images, Categories Images, taxonomy image, taxonomy images, taxonomies images, category icon, categories icons, category logo, categories logos, admin, wp-admin, category image plugin, categories images plugin, category featured image, categories featured images, feature image for category
     4Tags: Category Image, Category Images, Categories Images, taxonomy image, taxonomy images, taxonomies images, category icon, categories icons, category logo, categories logos, admin, wp-admin, category image plugin, categories images plugin, category featured image, categories featured images, feature image for category,term image, tag image, term images, tag images, media category
    55Requires at least: 2.8
    6 Tested up to: 4.9.1
    7 Stable tag: 2.5.4
     6Tested up to: 5.3.2
     7Stable tag: 3.0.0
     8License: GPLv2 or later
    89
    910The Categories Images Plugin allow you to add image with category or taxonomy.
     
    53542. New image field with (upload/remove) buttons to allow you to edit category or taxonomy image in quick edit.
    54553. When you click the upload button the wordpress upload box will popup, upload or select image then press use this image.
    55 4. New submenu (Categories Images) in Settings menu.
     564. New menu (Categories Images) for the plugin settings.
    56575. Now you can exclude any taxonomy from the plugin and save changes.
    5758
    5859== Changelog ==
     60
     61= 3.0.0 =
     62* Fix settings page issues
     63* Fix compatibility with the latest Wordpress version
     64* Rewrote the whole plugin from scratch, now the code is much efficient, readable and cleaner
     65
    5966= 2.5.4 =
    6067* Fix compatibility with the latest Wordpress version
     
    134141* Added thumbnail in categories or taxonomies list.
    135142* Added image thumbnail, image text box, upload button and remove button in quick edit.
    136 
    137143Thank so much to Joe Tse http://tkjune.com :)
    138144
Note: See TracChangeset for help on using the changeset viewer.