Changeset 1269061
- Timestamp:
- 10/20/2015 02:19:29 AM (10 years ago)
- Location:
- gecka-terms-thumbnails/trunk
- Files:
-
- 3 edited
-
gecka-terms-thumbnails.php (modified) (14 diffs)
-
readme.txt (modified) (2 diffs)
-
settings.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
gecka-terms-thumbnails/trunk/gecka-terms-thumbnails.php
r453683 r1269061 1 <?php 1 <?php 2 2 /* 3 Plugin Name: Gecka Terms Thumbnails 4 Plugin URI: http://gecka-apps.com 5 Description: Add thumbnails support to categories and any choosen taxonomies. 6 Version: 1.0-beta4 7 Author: Gecka Apps 8 Author URI: http://gecka.nc 9 Licence: GPL 10 Requires at least: 3.0 11 Tested up to: 3.2 12 */ 13 14 /* Copyright 2011 Gecka (email : [email protected]) 15 16 This program is free software; you can redistribute it and/or modify 17 it under the terms of the GNU General Public License, version 2, as 18 published by the Free Software Foundation. 19 20 This program is distributed in the hope that it will be useful, 21 but WITHOUT ANY WARRANTY; without even the implied warranty of 22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 GNU General Public License for more details. 24 25 You should have received a copy of the GNU General Public License 26 along with this program; if not, write to the Free Software 27 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 28 */ 3 * Plugin Name: Gecka Terms Thumbnails 4 * Plugin URI: http://gecka-apps.com 5 * Description: Add thumbnails support to categories and any chosen taxonomies. 6 * Version: 1.1 7 * Author: Gecka Apps, Gecka 8 * Author URI: http://gecka.nc 9 * Text Domain: gecka-terms-thumbnails 10 * Domain Path: /languages 11 * Licence: GPL 12 * Requires at least: 3.0 13 * Tested up to: 4.4 14 */ 15 16 /* 17 * Copyright 2011 Gecka (email : [email protected]) 18 * 19 * This program is free software; you can redistribute it and/or modify 20 * it under the terms of the GNU General Public License, version 2, as 21 * published by the Free Software Foundation. 22 * 23 * This program is distributed in the hope that it will be useful, 24 * but WITHOUT ANY WARRANTY; without even the implied warranty of 25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 26 * GNU General Public License for more details. 27 * 28 * You should have received a copy of the GNU General Public License 29 * along with this program; if not, write to the Free Software 30 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 31 */ 29 32 30 33 // Load the textdomain 31 load_plugin_textdomain( 'gecka-terms-thumbnails', false, dirname(plugin_basename(__FILE__)) . '/languages');32 33 require_once dirname( __FILE__) . '/settings.php';34 load_plugin_textdomain( 'gecka-terms-thumbnails', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); 35 36 require_once dirname( __FILE__ ) . '/settings.php'; 34 37 35 38 $gecka_terms_thumbnails = Gecka_Terms_Thumbnails::instance(); 36 39 37 40 class Gecka_Terms_Thumbnails { 41 42 /** 43 * Plugin settings 44 * @var Gecka_Terms_Thumbnails_Settings 45 */ 46 public static $settings; 47 48 /** 49 * Allowed mime types 50 * @var array 51 */ 52 public static $mimes = array( 53 'jpg|jpeg|jpe' => 'image/jpeg', 54 'gif' => 'image/gif', 55 'png' => 'image/png', 56 ); 38 57 39 58 /** … … 42 61 */ 43 62 private static $instance; 44 63 45 64 /** 46 65 * Uri to the plugin folder … … 48 67 */ 49 68 private static $plugin_url; 50 69 51 70 /** 52 71 * Absolute path to the plugin folder 53 72 * @var string 54 73 */ 55 private static $plugin_path;56 57 private static $taxonomies = array('category');58 74 private static $plugin_path; 75 76 private static $taxonomies = array( 'category' ); 77 59 78 /** 60 79 * Thumbnails sizes … … 63 82 private static $thumbnails_sizes = array(); 64 83 65 /**66 * Plugin settings67 * @var Gecka_Terms_Thumbnails_Settings68 */69 public static $settings;70 71 84 private $error; 72 73 /** 74 * Allowed mime types 75 * @var array 76 */ 77 public static $mimes = array('jpg|jpeg|jpe' => 'image/jpeg', 78 'gif' => 'image/gif', 79 'png' => 'image/png',); 80 85 81 86 /** 82 87 * Private constructor (singleton) 83 88 */ 84 89 private function __construct() { 85 86 self::$plugin_url = plugins_url('', __FILE__);87 self::$plugin_path = dirname(__FILE__);88 89 // init settings90 self::$settings = Gecka_Terms_Thumbnails_Settings::instance();91 92 // add default thumbnails sizes93 self::add_image_size('admin-thumbnail', 50, 50, true);94 self::add_image_size('thumbnail', self::$settings->term_thumbnail_size_w, self::$settings->term_thumbnail_size_h, self::$settings->term_thumbnail_crop);95 self::add_image_size('medium', self::$settings->term_medium_size_w, self::$settings->term_medium_size_h, self::$settings->term_medium_crop);96 90 91 self::$plugin_url = plugins_url( '', __FILE__ ); 92 self::$plugin_path = dirname( __FILE__ ); 93 94 // init settings 95 self::$settings = Gecka_Terms_Thumbnails_Settings::instance(); 96 97 // add default thumbnails sizes 98 self::add_image_size( 'admin-thumbnail', 50, 50, true ); 99 self::add_image_size( 'thumbnail', self::$settings->term_thumbnail_size_w, self::$settings->term_thumbnail_size_h, self::$settings->term_thumbnail_crop ); 100 self::add_image_size( 'medium', self::$settings->term_medium_size_w, self::$settings->term_medium_size_h, self::$settings->term_medium_crop ); 101 97 102 register_activation_hook( __FILE__, array( $this, 'activation_hook' ) ); 98 99 add_action( 'plugins_loaded', array($this, 'plugins_loaded'), 5 ); 100 add_action( 'after_setup_theme', array($this, 'after_setup_theme'), 5 ); 101 102 add_action( 'init', array($this, 'metadata_wpdbfix') ); 103 add_action( 'switch_blog', array($this, 'metadata_wpdbfix') ); 104 105 add_filter( 'widget_categories_args', array($this, 'widget_categories_args') ); 106 107 add_action( 'admin_init', array($this, 'admin_init') ); 108 109 } 110 111 /** 112 * Returns the singleton instance 113 * @return Gecka_Terms_Thumbnails 114 */ 115 public static function instance () { 116 117 if ( ! isset( self::$instance ) ) { 118 $class_name = __CLASS__; 119 self::$instance = new $class_name; 120 } 121 122 return self::$instance; 123 124 } 125 126 /* =Manages taxonomies terms image support 127 ----------------------------------------------- */ 128 129 /** 130 * Adds thumbnail support for the specified taxonomy 131 * @param string $taxonomy 132 */ 133 public static function add_taxonomy_support( $taxonomy ) { 134 135 $taxonomies = (array)$taxonomy ; 136 self::$taxonomies = array_merge( self::$taxonomies, $taxonomies ); 137 138 } 139 140 /** 141 * Removes thumbnail support for the specified taxonomy 142 * @param string $taxonomy 143 */ 144 public static function remove_taxonomy_support( $taxonomy ) { 145 146 $key = array_search ( $taxonomy, self::$taxonomies ); 147 if( false !== $key ) unset( self::$taxonomies[$key] ); 148 149 } 150 151 /** 152 * Return true if the specified taxonomy has terms thumbnails support 153 * @param string $taxonomy 154 * @return bool 155 */ 156 public static function has_support ( $taxonomy ) { 157 158 if( in_array($taxonomy, self::$taxonomies) ) return true; 159 return false; 160 161 } 162 163 /* =Manages terms thumbnails sizes 164 ----------------------------------------------- */ 165 103 104 add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ), 5 ); 105 add_action( 'after_setup_theme', array( $this, 'after_setup_theme' ), 5 ); 106 107 add_action( 'init', array( $this, 'metadata_wpdbfix' ) ); 108 add_action( 'switch_blog', array( $this, 'metadata_wpdbfix' ) ); 109 110 add_filter( 'widget_categories_args', array( $this, 'widget_categories_args' ) ); 111 112 add_action( 'admin_init', array( $this, 'admin_init' ) ); 113 114 } 115 166 116 /** 167 117 * Registers a new term thumbnail size 168 * 118 * 169 119 * @param string $name 170 120 * @param int $width … … 173 123 */ 174 124 public static function add_image_size( $name, $width = 0, $height = 0, $crop = false ) { 175 self::$thumbnails_sizes[$name] = array( 'width' => absint( $width ), 'height' => absint( $height ), 'crop' => (bool) $crop ); 176 } 177 125 self::$thumbnails_sizes[ $name ] = array( 126 'width' => absint( $width ), 127 'height' => absint( $height ), 128 'crop' => (bool) $crop 129 ); 130 } 131 132 /* =Manages taxonomies terms image support 133 ----------------------------------------------- */ 134 135 /** 136 * Returns the singleton instance 137 * @return Gecka_Terms_Thumbnails 138 */ 139 public static function instance() { 140 141 if ( ! isset( self::$instance ) ) { 142 $class_name = __CLASS__; 143 self::$instance = new $class_name; 144 } 145 146 return self::$instance; 147 148 } 149 150 /** 151 * Adds thumbnail support for the specified taxonomy 152 * 153 * @param string $taxonomy 154 */ 155 public static function add_taxonomy_support( $taxonomy ) { 156 157 $taxonomies = (array) $taxonomy; 158 self::$taxonomies = array_merge( self::$taxonomies, $taxonomies ); 159 160 } 161 162 /** 163 * Removes thumbnail support for the specified taxonomy 164 * 165 * @param string $taxonomy 166 */ 167 public static function remove_taxonomy_support( $taxonomy ) { 168 169 $key = array_search( $taxonomy, self::$taxonomies ); 170 if ( false !== $key ) { 171 unset( self::$taxonomies[ $key ] ); 172 } 173 174 } 175 176 /* =Manages terms thumbnails sizes 177 ----------------------------------------------- */ 178 178 179 /** 179 180 * Sets the default thumbnail size 181 * 180 182 * @param int $width 181 * @param int $height183 * @param int $height 182 184 * @param bool $crop 183 185 */ … … 185 187 self::add_image_size( 'thumbnail', $width, $height, $crop ); 186 188 } 187 188 189 190 /** 191 * Returns true if the specified term has a thumbnail image for the specified category and size 192 * 193 * @param int $term_id 194 * @param string $size 195 */ 196 public static function has_term_thumbnail( $term_id, $taxonomy, $size = null ) { 197 198 $image_infos = self::get_term_image_infos( $term_id, $taxonomy ); 199 200 if ( empty( $image_infos ) ) { 201 return false; 202 } elseif ( ! $size ) { 203 return true; 204 } 205 206 if ( isset ( $image_infos['thumbnails'][ $size ] ) ) { 207 return true; 208 } 209 210 return false; 211 } 212 213 189 214 /* =Static functions to display terms thumbnails 190 215 ----------------------------------------------- */ 191 192 /** 193 * Return s true if the specified term has a thumbnail image for the specified category and size216 217 /** 218 * Return a term's thumbnail meta data for the specified taxonomy 194 219 * 195 220 * @param int $term_id 221 * @param string $taxonomy 222 * 223 * @return array 224 */ 225 public static function get_term_image_infos( $term_id, $taxonomy ) { 226 227 $meta_data = false; 228 229 if ( $taxonomy ) { 230 $meta_data = get_metadata( 'term', $term_id, 'image-' . $taxonomy, true ); 231 } 232 233 // compatibility with beta1 234 if ( ! $meta_data ) { 235 $meta_data = get_metadata( 'term', $term_id, 'image', true ); 236 } 237 238 return $meta_data; 239 } 240 241 /** 242 * Returns the specified term's thumbnail's HTML code for the specified taxonomy and size 243 * 244 * @param int $term_id 245 * @param string $taxonomy 196 246 * @param string $size 197 */198 public static function has_term_thumbnail ( $term_id, $taxonomy, $size=null ) {199 200 $image_infos = self::get_term_image_infos( $term_id, $taxonomy );201 202 if( empty( $image_infos ) ) return false;203 elseif( ! $size ) return true;204 205 if( isset ( $image_infos['thumbnails'][$size] ) ) return true;206 207 return false;208 }209 210 /**211 * Returns the specified term's thumbnail's HTML code for the specified taxonomy and size212 * @param int $term_id213 * @param string $taxonomy214 * @param string $size215 247 * @param array $attr 216 248 */ 217 249 public static function get_the_term_thumbnail( $term_id, $taxonomy, $size = 'thumbnail', $attr = '' ) { 218 250 219 251 $size = apply_filters( 'term_thumbnail_size', $size, $term_id, $taxonomy ); 220 221 $image = self::get_term_thumbnail( $term_id, $taxonomy, $size);222 223 $term = get_term( $term_id, $taxonomy);224 252 253 $image = self::get_term_thumbnail( $term_id, $taxonomy, $size ); 254 255 $term = get_term( $term_id, $taxonomy ); 256 225 257 if ( $image ) { 226 258 do_action( 'begin_fetch_term_thumbnail_html', $term_id, $taxonomy, $image, $size ); 227 228 list( $src, $width, $height) = $image;229 $hwstring = image_hwstring( $width, $height);230 231 if ( is_array( $size) )259 260 list( $src, $width, $height ) = $image; 261 $hwstring = image_hwstring( $width, $height ); 262 263 if ( is_array( $size ) ) { 232 264 $size = $width . 'x' . $height; 233 265 } 266 234 267 $default_attr = array( 235 'src' => $src,236 'class' => "attachment-$size $taxonomy-thumbnail",237 'alt' => trim(strip_tags( $term->name )), // Use Alt field first238 'title' => trim(strip_tags( $term->name )),268 'src' => $src, 269 'class' => "attachment-$size $taxonomy-thumbnail", 270 'alt' => trim( strip_tags( $term->name ) ), // Use Alt field first 271 'title' => trim( strip_tags( $term->name ) ), 239 272 ); 240 241 $attr = wp_parse_args( $attr, $default_attr);242 $attr = apply_filters( 'get_the_term_thumbnail_attributes', $attr, $term_id, $taxonomy, $image, $size );273 274 $attr = wp_parse_args( $attr, $default_attr ); 275 $attr = apply_filters( 'get_the_term_thumbnail_attributes', $attr, $term_id, $taxonomy, $image, $size ); 243 276 $attr = array_map( 'esc_attr', $attr ); 244 $html = rtrim( "<img $hwstring");277 $html = rtrim( "<img $hwstring" ); 245 278 foreach ( $attr as $name => $value ) { 246 279 $html .= " $name=" . '"' . $value . '"'; … … 248 281 $html .= ' />'; 249 282 do_action( 'end_fetch_term_thumbnail_html', $term_id, $taxonomy, $image, $size ); 250 283 251 284 } else { 252 285 $html = ''; 253 286 } 287 254 288 return apply_filters( 'term_thumbnail_html', $html, $term_id, $taxonomy, $image, $size, $attr ); 255 289 } 256 290 257 291 /** 258 292 * Returns the specified term's thumbnail for the specified taxonomy and size 293 * 259 294 * @param int $term_id 260 295 * @param string $taxonomy 261 * @param string $size 262 */ 263 public static function get_term_thumbnail ( $term_id, $taxonomy, $size = null ) { 264 265 $infos = self::get_term_image_infos($term_id, $taxonomy); 266 if(!$infos) return false; 267 268 if( ! $size ) return array($infos['url'], $infos['infos'][0], $infos['infos'][1]); 269 270 if( is_array($size) ) { 296 * @param string $size 297 */ 298 public static function get_term_thumbnail( $term_id, $taxonomy, $size = null ) { 299 300 $infos = self::get_term_image_infos( $term_id, $taxonomy ); 301 if ( ! $infos ) { 302 return false; 303 } 304 305 if ( ! $size ) { 306 return array( $infos['url'], $infos['infos'][0], $infos['infos'][1] ); 307 } 308 309 if ( is_array( $size ) ) { 271 310 /* 272 311 * @TODO here we need to fing the thumbnail nearest to the asked size 273 312 */ 274 313 } 275 276 if( !isset( $infos['thumbnails'][$size] ) ) return false; 277 $infos = $infos['thumbnails'][$size]; 278 279 return array($infos['url'], $infos['infos'][0], $infos['infos'][1]); 280 281 } 282 314 315 if ( ! isset( $infos['thumbnails'][ $size ] ) ) { 316 return false; 317 } 318 $infos = $infos['thumbnails'][ $size ]; 319 320 return array( $infos['url'], $infos['infos'][0], $infos['infos'][1] ); 321 322 } 323 283 324 /** 284 325 * Manage terms image meta 285 326 */ 286 287 /** 288 * Return a term's thumbnail meta data for the specified taxonomy 289 * @param int $term_id 290 * @param string $taxonomy 291 * @return array 292 */ 293 public static function get_term_image_infos ( $term_id, $taxonomy) { 294 295 $meta_data = false; 296 297 if( $taxonomy ) { 298 $meta_data = get_metadata( 'term', $term_id, 'image-' . $taxonomy, true); 299 } 300 301 // compatibility with beta1 302 if( !$meta_data ) { 303 $meta_data = get_metadata( 'term', $term_id, 'image', true); 304 } 305 306 return $meta_data; 307 } 308 327 328 /** 329 * Checks PHP version and create the needed database table on plugin activation 330 */ 331 public function activation_hook() { 332 333 // checks the PHP version 334 if ( version_compare( PHP_VERSION, '5.0.0', '<' ) ) { 335 deactivate_plugins( basename( dirname( __FILE__ ) ) . '/' . basename( __FILE__ ) ); // Deactivate ourself 336 wp_die( "Sorry, the Gecka Terms Ordering plugin requires PHP 5 or higher." ); 337 } 338 339 340 // creates the needed database table 341 global $wpdb; 342 343 /** 344 * Create the termmeta database table, for WordPress < version 4.4. 345 * 346 * The max index length is required since 4.2, because of the move to utf8mb4 collation. 347 * 348 * @see wp_get_db_schema() 349 */ 350 $charset_collate = $wpdb->get_charset_collate(); 351 $table_name = $wpdb->prefix . "termmeta"; 352 $max_index_length = 191; 353 354 $sql = "CREATE TABLE IF NOT EXISTS $table_name ( 355 meta_id bigint(20) unsigned NOT NULL auto_increment, 356 term_id bigint(20) unsigned NOT NULL default '0', 357 meta_key varchar(255) default NULL, 358 meta_value longtext, 359 PRIMARY KEY (meta_id), 360 KEY term_id (term_id), 361 KEY meta_key (meta_key($max_index_length)) 362 ) $charset_collate;"; 363 364 require_once ABSPATH . 'wp-admin/includes/upgrade.php'; 365 dbDelta( $sql ); 366 367 } 368 309 369 /* =Manages the terms images metadata 310 370 ----------------------------------------------- */ 311 312 /** 313 * Updates a term thumbnail metadata 314 * @param int $term_id 315 * @param sttring $taxonomy 316 * @param array $infos 317 * @return boolean 318 */ 319 public static function update_term_image_infos ( $term_id, $taxonomy, $infos ) { 320 321 // compatibility with beta1 322 if( get_metadata( 'term', $term_id, 'image', true) ) { 323 delete_metadata( 'term', $term_id, 'image' ); 324 } 325 326 return update_metadata( 'term', $term_id, 'image-' . $taxonomy, $infos ); 327 328 } 329 330 /** 331 * Deletes a term's thumbnail metadata 332 * @param int $term_id 333 * @param string $taxonomy 334 * @return boolean 335 */ 336 public static function delete_term_image_infos ( $term_id, $taxonomy ) { 337 338 // compatibility with beta1 339 if( get_metadata( 'term', $term_id, 'image-' . $taxonomy, true) ) { 340 return delete_metadata( 'term', $term_id, 'image-' . $taxonomy ); 341 } 342 343 // compatibility with beta1 344 if( get_metadata( 'term', $term_id, 'image', true) ) { 345 return delete_metadata( 'term', $term_id, 'image' ); 346 } 347 348 return delete_metadata( 'term', $term_id, 'image-' . $taxonomy ); 349 350 } 351 371 372 /** 373 * Filters default thumbnails sizes and supported taxonomies 374 * Runs on the plugins_loaded action hook 375 */ 376 public function plugins_loaded() { 377 self::$taxonomies = apply_filters( 'terms-thumbnails-default-sizes', self::$taxonomies ); 378 self::$thumbnails_sizes = apply_filters( 'terms-thumbnails-default-sizes', self::$thumbnails_sizes ); 379 } 380 381 /** 382 * Filters default thumbnails sizes and supported taxonomies 383 * Runs on the after_setup_theme action hook 384 */ 385 public function after_setup_theme() { 386 self::$taxonomies = apply_filters( 'terms-thumbnails-taxonomies', self::$taxonomies ); 387 self::$thumbnails_sizes = apply_filters( 'terms-thumbnails-sizes', self::$thumbnails_sizes ); 388 } 389 352 390 /* =Terms images and thumbnails path and url 353 391 ----------------------------------------------- */ 354 355 /** 356 * Returns the absolute path to the thumbnails folder 357 * @return string 358 */ 359 public static function images_dir () { 360 $upload_dir_infos = wp_upload_dir(); 361 return $upload_dir_infos['basedir'] . '/terms-images'; 362 } 363 364 /** 365 * Returns the URI to the thumbnails folder 366 * @return string 367 */ 368 public static function images_url () { 369 $upload_dir_infos = wp_upload_dir(); 370 return $upload_dir_infos['baseurl'] . '/terms-images'; 371 } 372 392 393 /** 394 * Sets our table name into wpdb 395 * Runs on the init and switch_blog action hooks 396 */ 397 public function metadata_wpdbfix() { 398 global $wpdb; 399 if ( ! isset( $wpdb->termmeta ) ) { 400 $wpdb->termmeta = "{$wpdb->prefix}termmeta"; 401 } 402 } 403 404 /** 405 * Filters the default categories widget args 406 * Run on the widget_categories_args filter hook 407 * 408 * @param array $args 409 * 410 * @return array 411 */ 412 public function widget_categories_args( $args ) { 413 414 // default taxonomy 415 $taxonomy = empty( $args->taxonomy ) ? 'category' : $args->taxonomy; 416 417 // the taxonomy hasn't thumbnail support, so we ignore it 418 if ( ! self::has_support( $taxonomy ) ) { 419 return $args; 420 } 421 422 // default thumbnail size 423 if ( ! isset( $args['show_thumbnail'] ) ) { 424 $args['show_thumbnail'] = 'thumbnail'; 425 } 426 427 // our custom walker to add thumbnails 428 $args['walker'] = new Walker_Term(); 429 430 return $args; 431 } 432 373 433 /* =Static functions to manage terms images 374 434 ----------------------------------------------- */ 375 376 /** 377 * Generate a term's thumbnails 378 * @param int $term_id 379 * @param string $taxonomy 380 */ 381 public static function generate_thumbnails ( $term_id, $taxonomy ) { 382 383 $infos = self::get_term_image_infos( $term_id, $taxonomy ); 384 385 if ( ! $infos ) return; 386 387 $thumbnails = !empty($infos['thumbnails']) ? $infos['thumbnails'] : array(); 388 389 // removes obsolete thumbnails 390 foreach ( $thumbnails as $name => $size ) { 391 if( ! isset( self::$thumbnails_sizes[$name] ) ) self::remove_term_thumbnail( $name, $term_id, $taxonomy ); 392 } 393 394 // creates all thumbnails images 395 foreach ( self::$thumbnails_sizes as $key => $size ) { 396 397 if( ! empty( $thumbnails[$key] ) ) self::remove_term_thumbnail( $key, $term_id, $taxonomy ); 398 399 $img = image_resize( $infos['path'], $size['width'], $size['height'], $size['crop'], $key); 400 401 if( ! $img || is_wp_error($img) ) continue; 402 403 $file_infos = array(); 404 $file_infos ['name'] = basename($img); 405 $file_infos ['path'] = $img; 406 $file_infos ['infos'] = getimagesize($img); 407 $file_infos ['url'] = self::images_url() . '/' . $taxonomy . '/' . basename($img); 408 409 $infos['thumbnails'][$key] = $file_infos; 410 } 411 412 self::update_term_image_infos($term_id, $taxonomy, $infos); 413 414 } 415 416 /** 417 * Remove a term's image (and its thumbnails) 418 * @param int $term_id 419 * @param string $taxonomy 420 */ 421 public static function remove_term_image ( $term_id, $taxonomy ) { 422 423 $infos = self::get_term_image_infos( $term_id, $taxonomy ); 424 425 if ( !$infos ) return; 426 427 if( !empty($infos) && isset( $infos['path'] ) ) { 428 429 if( false === self::remove_term_thumbnails($term_id, $taxonomy) ) return false; 430 431 if( ! @ unlink($infos['path']) && file_exists($infos['path']) ) return false; 432 433 } 434 435 self::delete_term_image_infos($term_id, $taxonomy); 436 437 return true; 438 439 } 440 441 /** 442 * Removes the generated thumbnails of a term 443 * @param int $term_id 444 * @param string $taxonomy 435 436 /** 437 * Return true if the specified taxonomy has terms thumbnails support 438 * 439 * @param string $taxonomy 440 * 445 441 * @return bool 446 442 */ 447 public static function remove_term_thumbnails ( $term_id, $taxonomy ) { 448 449 $infos = self::get_term_image_infos( $term_id, $taxonomy ); 450 451 if ( !$infos ) return; 452 453 if( empty($infos['thumbnails']) ) return true; 454 455 foreach ($infos['thumbnails'] as $name => $thumbnail ) { 456 if( false === self::remove_term_thumbnail($name, $term_id, $taxonomy) ) return false; 457 } 458 459 return true; 460 461 } 462 463 /** 464 * Removes aterm's thumbnail 465 * @pram string $thumbnail_name 466 * @param int $term_id 467 * @param string $taxonomy 468 * @return bool 469 */ 470 public static function remove_term_thumbnail ( $thumbnail_name, $term_id, $taxonomy ) { 471 472 if($thumbnail_name == 'admin_thumbnail') return; 473 474 $infos = self::get_term_image_infos( $term_id, $taxonomy ); 475 if ( !$infos ) return; 476 477 if( empty($infos['thumbnails']) || empty($infos['thumbnails'][$thumbnail_name]) ) return true; 478 479 $thumbnail = $infos['thumbnails'][$thumbnail_name]; 480 481 if ( file_exists($thumbnail['path'] ) && ! @ unlink($thumbnail['path']) ) return false; 482 unset( $infos['thumbnails'][$thumbnail_name] ); 483 484 self::update_term_image_infos($term_id, $taxonomy, $infos); 485 486 return true; 487 488 } 489 443 public static function has_support( $taxonomy ) { 444 445 if ( in_array( $taxonomy, self::$taxonomies ) ) { 446 return true; 447 } 448 449 return false; 450 451 } 452 453 /** 454 * Init the admin 455 * Runs on the admin_init action hook 456 */ 457 public function admin_init() { 458 459 // adds scripts and css 460 add_action( 'admin_head-edit-tags.php', array( $this, 'admin_head' ) ); 461 462 // show our admin notices 463 add_action( 'admin_notices', array( $this, 'admin_notice' ) ); 464 465 // adds/removes our errors var to url on redirect 466 add_filter( 'wp_redirect', array( $this, 'wp_redirect' ) ); 467 468 foreach ( self::$taxonomies as $taxonomy ) { 469 470 // add a file field to terms add and edit form 471 add_action( $taxonomy . '_add_form_fields', array( $this, 'add_field' ), 10, 2 ); 472 add_action( $taxonomy . '_edit_form_fields', array( $this, 'edit_field' ), 10, 2 ); 473 474 // save image on term save 475 add_action( "edited_$taxonomy", array( $this, 'process_upload' ), 10, 2 ); 476 477 // generate thumbnails after a term is saved 478 add_action( "edited_$taxonomy", array( $this, 'generate_thumbnails_action' ), 15, 2 ); 479 480 // delete image and thumbnails of deleted terms 481 add_action( "delete_term", array( $this, 'delete_term' ), 5, 3 ); 482 483 // add images column to terms list-table 484 add_filter( "manage_edit-{$taxonomy}_columns", array( $this, 'edit_columns' ) ); 485 add_filter( "manage_{$taxonomy}_custom_column", array( $this, 'columns' ), 10, 3 ); 486 487 } 488 489 add_action( 'wp_ajax_delete_term_image', array( $this, 'ajax_delete_term_image' ) ); 490 491 } 492 493 /** 494 * Css and script on the admin terms forms 495 * Runs on the admin_head-edit-tags.php action hook 496 */ 497 public function admin_head() { 498 499 if ( empty( $_GET['taxonomy'] ) || ! self::has_support( $_GET['taxonomy'] ) ) { 500 return; 501 } 502 503 504 if ( isset( $_GET['tag_ID'] ) && $term_id = (int) $_GET['tag_ID'] ) : 505 ?> 506 <script type="text/javascript"> 507 <!-- 508 jQuery(document).ready(function ($) { 509 510 var nonce = '<?php echo wp_create_nonce( 'delete_term_image' ) ?>'; 511 512 $('#delete-thumb-button').click( 513 function () { 514 $.post(ajaxurl, { 515 term_id: <?php echo esc_js($term_id) ?>, 516 taxonomy: '<?php echo esc_js($_GET['taxonomy']) ?>', 517 action: 'delete_term_image', 518 _nonce: nonce 519 }, function (data) { 520 if (data == '1') $('#term_thumbnail').hide('slow'); 521 }); 522 } 523 ); 524 525 }); 526 //--> 527 </script><?php 528 endif; 529 ?> 530 <style type="text/css"> 531 <!-- 532 th#image { 533 width: 55px 534 } 535 536 .attachment-admin-thumbnail { 537 border: 1px solid #ccc; 538 padding: 3px; 539 } 540 541 --> 542 </style><?php 543 544 } 545 546 /** 547 * Shows errors in admin 548 * Runs on the admin_notices action hook 549 */ 550 public function admin_notice() { 551 552 if ( empty( $_GET['term_image_error'] ) ) { 553 return; 554 } 555 556 $error = unserialize( base64_decode( $_GET['term_image_error'] ) ); 557 558 if ( ! is_wp_error( $error ) ) { 559 return; 560 } 561 562 echo '<div class="error"> 563 <p><strong>' . __( 'Image upload error: ', 'gecka_terms_ordering' ) . '</strong>' . $error->get_error_message() . '</p> 564 </div>'; 565 } 566 567 /** 568 * On wp_redirect, we add/remove our errors var as needed 569 * 570 * @param string $location 571 * Runs on the wp_redirect filter hook 572 */ 573 public function wp_redirect( $location ) { 574 575 $location = remove_query_arg( 'term_image_error', $location ); 576 577 if ( ! $this->error ) { 578 return $location; 579 } 580 581 $location = add_query_arg( 'term_image_error', urlencode( base64_encode( serialize( $this->error ) ) ), $location ); 582 583 return $location; 584 } 585 490 586 /* =Misc static functions 491 587 ----------------------------------------------- */ 492 493 /** 494 * Make a directory 495 * @param string $taxonomy optional category name to create a taxnonomy directory 496 */ 497 public static function images_mkdir ($taxonomy='') { 498 499 global $wp_filesystem; 500 WP_Filesystem(); 501 502 $dir = self::images_dir() . ($taxonomy ? '/' . $taxonomy : ''); 503 504 if ( ! wp_mkdir_p($dir) && ! is_dir($dir) ) // Only check to see if the Dir exists upon creation failure. Less I/O this way. 505 wp_die(__('Could not create directory.')); 506 507 return $dir; 508 } 509 510 /* =Action and filter hooks 511 ----------------------------------------------- */ 512 513 /** 514 * Checks PHP version and create the needed database table on plugin activation 515 */ 516 public function activation_hook () { 517 518 // checks the PHP version 519 if (version_compare(PHP_VERSION, '5.0.0', '<')) { 520 deactivate_plugins( basename(dirname(__FILE__)) . '/' . basename(__FILE__) ); // Deactivate ourself 521 wp_die("Sorry, the Gecka Terms Ordering plugin requires PHP 5 or higher."); 522 } 523 524 525 // creates the needed database table 526 global $wpdb; 527 528 $collate = ''; 529 if($wpdb->supports_collation()) { 530 if(!empty($wpdb->charset)) $collate = "DEFAULT CHARACTER SET $wpdb->charset"; 531 if(!empty($wpdb->collate)) $collate .= " COLLATE $wpdb->collate"; 532 } 533 534 $sql = "CREATE TABLE IF NOT EXISTS ". $wpdb->prefix . "termmeta" ." ( 535 `meta_id` bigint(20) unsigned NOT NULL auto_increment, 536 `term_id` bigint(20) unsigned NOT NULL default '0', 537 `meta_key` varchar(255) default NULL, 538 `meta_value` longtext, 539 PRIMARY KEY (meta_id), 540 KEY term_id (term_id), 541 KEY meta_key (meta_key) ) $collate;"; 542 543 $wpdb->query($sql); 544 545 } 546 547 /** 548 * Filters default thumbnails sizes and supported taxonomies 549 * Runs on the plugins_loaded action hook 550 */ 551 public function plugins_loaded () { 552 self::$taxonomies = apply_filters( 'terms-thumbnails-default-sizes', self::$taxonomies ); 553 self::$thumbnails_sizes = apply_filters( 'terms-thumbnails-default-sizes', self::$thumbnails_sizes ); 554 } 555 556 /** 557 * Filters default thumbnails sizes and supported taxonomies 558 * Runs on the after_setup_theme action hook 559 */ 560 public function after_setup_theme () { 561 self::$taxonomies = apply_filters( 'terms-thumbnails-taxonomies', self::$taxonomies ); 562 self::$thumbnails_sizes = apply_filters( 'terms-thumbnails-sizes', self::$thumbnails_sizes ); 563 } 564 565 /** 566 * Sets our table name into wpdb 567 * Runs on the init and switch_blog action hooks 568 */ 569 public function metadata_wpdbfix () { 570 global $wpdb; 571 $wpdb->termmeta = "{$wpdb->prefix}termmeta"; 572 } 573 574 /** 575 * Filters the default categories widget args 576 * Run on the widget_categories_args filter hook 577 * @param array $args 578 * @return array 579 */ 580 public function widget_categories_args ($args) { 581 582 // default taxonomy 583 $taxonomy = empty( $args->taxonomy ) ? 'category' : $args->taxonomy; 584 585 // the taxonomy hasn't thumbnail support, so we ignore it 586 if( ! self::has_support( $taxonomy ) ) return $args; 587 588 // default thumbnail size 589 if( !isset($args['show_thumbnail']) ) $args['show_thumbnail'] = 'thumbnail'; 590 591 // our custom walker to add thumbnails 592 $args['walker'] = new Walker_Term(); 593 594 return $args; 595 } 596 597 /** 598 * Init the admin 599 * Runs on the admin_init action hook 600 */ 601 public function admin_init () { 602 603 // adds scripts and css 604 add_action ( 'admin_head-edit-tags.php', array($this, 'admin_head')); 605 606 // show our admin notices 607 add_action ( 'admin_notices', array($this, 'admin_notice') ); 608 609 // adds/removes our errors var to url on redirect 610 add_filter ( 'wp_redirect', array($this, 'wp_redirect') ); 611 612 foreach ( self::$taxonomies as $taxonomy ) { 613 614 // add a file field to terms add and edit form 615 add_action( $taxonomy . '_add_form_fields', array($this, 'add_field'), 10, 2 ); 616 add_action( $taxonomy . '_edit_form_fields', array($this, 'edit_field'), 10, 2 ); 617 618 // save image on term save 619 add_action( "edited_$taxonomy", array($this, 'process_upload'), 10, 2 ); 620 621 // generate thumbnails after a term is saved 622 add_action( "edited_$taxonomy", array($this, 'generate_thumbnails_action'), 15, 2 ); 623 624 // delete image and thumbnails of deleted terms 625 add_action( "delete_term", array($this, 'delete_term'), 5, 3 ); 626 627 // add images column to terms list-table 628 add_filter( "manage_edit-{$taxonomy}_columns", array($this, 'edit_columns') ); 629 add_filter( "manage_{$taxonomy}_custom_column", array($this, 'columns'), 10, 3 ); 630 631 } 632 633 add_action ( 'wp_ajax_delete_term_image', array($this, 'ajax_delete_term_image') ); 634 635 } 636 637 /** 638 * Css and script on the admin terms forms 639 * Runs on the admin_head-edit-tags.php action hook 640 */ 641 public function admin_head () { 642 643 if( empty( $_GET['taxonomy'] ) || ! self::has_support( $_GET['taxonomy'] ) ) return; 644 645 646 if( isset($_GET['tag_ID']) && $term_id = (int) $_GET['tag_ID'] ) : 647 ?> 648 <script type="text/javascript"> 649 <!-- 650 jQuery(document).ready(function($) { 651 652 var nonce = '<?php echo wp_create_nonce( 'delete_term_image' ) ?>'; 653 654 $('#delete-thumb-button').click( 655 function () { 656 $.post( ajaxurl, {term_id: <?php echo esc_js($term_id) ?>, taxonomy: '<?php echo esc_js($_GET['taxonomy']) ?>', action: 'delete_term_image', _nonce: nonce}, function (data) { if(data == '1') $('#term_thumbnail').hide('slow'); } ); 657 } 658 ); 659 660 }); 661 //--> 662 </script><?php 663 endif; 664 ?> 665 <style type="text/css"> 666 <!-- 667 th#image {width: 55px} 668 .attachment-admin-thumbnail { border: 1px solid #ccc; padding: 3px; } 669 --> 670 </style><?php 671 672 } 673 674 /** 675 * Shows errors in admin 676 * Runs on the admin_notices action hook 677 */ 678 public function admin_notice () { 679 680 if( empty($_GET['term_image_error']) ) return; 681 682 $error = unserialize(base64_decode($_GET['term_image_error'])); 683 684 if( ! is_wp_error($error) ) return; 685 686 echo '<div class="error"> 687 <p><strong>' . __('Image upload error: ', 'gecka_terms_ordering') . '</strong>' . $error->get_error_message() . '</p> 688 </div>'; 689 } 690 691 /** 692 * On wp_redirect, we add/remove our errors var as needed 693 * @param string $location 694 * Runs on the wp_redirect filter hook 695 */ 696 public function wp_redirect ($location) { 697 698 $location = remove_query_arg('term_image_error', $location); 699 700 if ( ! $this->error ) return $location; 701 702 $location = add_query_arg( 'term_image_error', urlencode( base64_encode( serialize( $this->error ) ) ) , $location ); 703 704 return $location; 705 } 706 588 707 589 /** 708 590 * Adds a field to the "Add term" form 591 * 709 592 * @param string $taxonomy 710 593 * Runs on the {$taxonomy}_add_form_fields action hook 711 594 */ 712 public function add_field ( $taxonomy ) {713 595 public function add_field( $taxonomy ) { 596 714 597 ?> 715 598 <div class="form-field"> 716 <label for="tag-description"><?php _e( 'Image', 'Gecka_Terms_Thumbnails') ?></label>717 599 <label for="tag-description"><?php _e( 'Image', 'Gecka_Terms_Thumbnails' ) ?></label> 600 718 601 <p><?php _e( 'Once you have added this new term, edit it to set its image.', 'gecka-terms-thumbnails' ); ?></p> 719 602 </div> 720 603 <?php 721 604 } 722 605 606 /* =Action and filter hooks 607 ----------------------------------------------- */ 608 723 609 /** 724 610 * Adds a field to the "Edit term" form 611 * 725 612 * @param string $taxonomy 726 613 * Runs on the {$taxonomy}_edit_form_fields action hook 727 614 */ 728 public function edit_field ( $term, $taxonomy ) {729 615 public function edit_field( $term, $taxonomy ) { 616 730 617 $term_id = $term->term_id; 731 732 $current = self::get_term_image_infos( $term_id, $taxonomy );733 $upload_size_unit = $max_upload_size = wp_max_upload_size();734 $sizes = array( 'KB', 'MB', 'GB' );735 for ( $u = - 1; $upload_size_unit > 1024 && $u < count( $sizes ) - 1; $u++ )618 619 $current = self::get_term_image_infos( $term_id, $taxonomy ); 620 $upload_size_unit = $max_upload_size = wp_max_upload_size(); 621 $sizes = array( 'KB', 'MB', 'GB' ); 622 for ( $u = - 1; $upload_size_unit > 1024 && $u < count( $sizes ) - 1; $u ++ ) { 736 623 $upload_size_unit /= 1024; 624 } 737 625 if ( $u < 0 ) { 738 626 $upload_size_unit = 0; 739 $u = 0;627 $u = 0; 740 628 } else { 741 629 $upload_size_unit = (int) $upload_size_unit; 742 630 } 743 631 744 632 ?> 745 633 <tr class="form-field"> 746 <th scope="row" valign="top"><label for="image"><?php _ex('Image', 'Taxonomy Image', 'gecka-terms-thumbnails'); ?></label></th> 634 <th scope="row" valign="top"><label 635 for="image"><?php _ex( 'Image', 'Taxonomy Image', 'gecka-terms-thumbnails' ); ?></label></th> 747 636 <td> 748 <?php if( has_term_thumbnail($term_id, $taxonomy) ) : ?> 749 750 <div id="term_thumbnail"> 751 <p class="description"><?php _e( 'You already have an image defined. You can delete it or replace. To keep it, ignore the following fields.', 'gecka-terms-thumbnails' ); ?></p> 752 <br> 753 <?php the_term_thumbnail($term_id, $taxonomy, 'admin-thumbnail', array('style'=>'float:left; border: 1px solid #ccc; margin-right: 10px; padding: 3px; ')); ?> 754 755 <input type="button" id="delete-thumb-button" value="<?php _e('Delete the current image', 'gecka-terms-thumbnails') ?>" class="button-secondary action" style="width: auto"> 756 <br><br> 757 </div> 758 759 <?php endif; ?> 760 <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_upload_size ?>" /> 761 <input type="file" id="image" name="image" style="width: 97%;" class="button-secondary action"><br /> 762 763 <span class="description"><?php printf( __( 'Maximum upload file size: %d%s' ), $upload_size_unit, $sizes[$u] ); ?></span> 637 <?php if ( has_term_thumbnail( $term_id, $taxonomy ) ) : ?> 638 639 <div id="term_thumbnail"> 640 <p class="description"><?php _e( 'You already have an image defined. You can delete it or replace. To keep it, ignore the following fields.', 'gecka-terms-thumbnails' ); ?></p> 641 <br> 642 <?php the_term_thumbnail( $term_id, $taxonomy, 'admin-thumbnail', array( 'style' => 'float:left; border: 1px solid #ccc; margin-right: 10px; padding: 3px; ' ) ); ?> 643 644 <input type="button" id="delete-thumb-button" 645 value="<?php _e( 'Delete the current image', 'gecka-terms-thumbnails' ) ?>" 646 class="button-secondary action" style="width: auto"> 647 <br><br> 648 </div> 649 650 <?php endif; ?> 651 <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_upload_size ?>"/> 652 <input type="file" id="image" name="image" style="width: 97%;" class="button-secondary action"><br/> 653 654 <span 655 class="description"><?php printf( __( 'Maximum upload file size: %d%s' ), $upload_size_unit, $sizes[ $u ] ); ?></span> 764 656 765 657 </tr> 766 658 <script type="text/javascript"> 767 <!--768 jQuery('#edittag').attr('enctype', 'multipart/form-data').attr('encoding', 'multipart/form-data');769 //-->659 <!-- 660 jQuery('#edittag').attr('enctype', 'multipart/form-data').attr('encoding', 'multipart/form-data'); 661 //--> 770 662 </script> 771 663 <?php 772 664 } 773 665 774 666 /** 775 667 * Process the thumbnial upload 776 668 * Runs on the edited_$taxonomy action hook 669 * 777 670 * @param int $term_id 778 671 * @param int $tt_id 672 * 779 673 * @return stdClass|boolean: 780 674 */ 781 public function process_upload ( $term_id, $tt_id ) {675 public function process_upload( $term_id, $tt_id ) { 782 676 783 677 // get the taxonomy and check that it supports images 784 if( empty( $_POST['taxonomy'] ) || ! self::has_support( $_POST['taxonomy'] ) ) return; 678 if ( empty( $_POST['taxonomy'] ) || ! self::has_support( $_POST['taxonomy'] ) ) { 679 return; 680 } 785 681 786 682 $taxonomy = $_POST['taxonomy']; 787 if( ! self::has_support( $taxonomy ) ) return $term; 788 789 $file = isset($_FILES['image']) ? $_FILES['image'] : null; 790 if( ! $file ) return $term; 683 if ( ! self::has_support( $taxonomy ) ) { 684 return $term; 685 } 686 687 $file = isset( $_FILES['image'] ) ? $_FILES['image'] : null; 688 if ( ! $file ) { 689 return $term; 690 } 791 691 792 692 /* create the taxonomy directory if needed */ 793 if( ! $dir = self::images_mkdir($taxonomy) ) return $this->upload_error( $file, __("Permission error creating the terms-images/{taxonomy} folder.", 'gecka-terms-thumbnails') ); 693 if ( ! $dir = self::images_mkdir( $taxonomy ) ) { 694 return $this->upload_error( $file, __( "Permission error creating the terms-images/{taxonomy} folder.", 'gecka-terms-thumbnails' ) ); 695 } 794 696 795 697 // Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error']. 796 $upload_error_strings = array( false, 797 __( "The uploaded file exceeds the <code>upload_max_filesize</code> directive in <code>php.ini</code>."), 798 __( "The uploaded file exceeds the <em>MAX_FILE_SIZE</em> directive that was specified in the HTML form."), 698 $upload_error_strings = array( 699 false, 700 __( "The uploaded file exceeds the <code>upload_max_filesize</code> directive in <code>php.ini</code>." ), 701 __( "The uploaded file exceeds the <em>MAX_FILE_SIZE</em> directive that was specified in the HTML form." ), 799 702 __( "The uploaded file was only partially uploaded." ), 800 703 __( "No file was uploaded.", 'gecka-terms-thumbnails' ), … … 802 705 __( "Missing a temporary folder." ), 803 706 __( "Failed to write file to disk." ), 804 __( "File upload stopped by extension." )); 805 806 if ( $file['error'] > 0 && $file['error'] !== 4 ) 807 return $this->upload_error( $file, $upload_error_strings[$file['error']] ); 808 809 if ( isset( $file['error'] ) && ! is_numeric( $file['error'] ) && $file['error'] && $file['error'] !== 4) 707 __( "File upload stopped by extension." ) 708 ); 709 710 if ( $file['error'] > 0 && $file['error'] !== 4 ) { 711 return $this->upload_error( $file, $upload_error_strings[ $file['error'] ] ); 712 } 713 714 if ( isset( $file['error'] ) && ! is_numeric( $file['error'] ) && $file['error'] && $file['error'] !== 4 ) { 810 715 return $this->upload_error( $file, $file['error'] ); 811 716 } 717 812 718 // A non-empty file will pass this test. 813 if ( ! ($file['size'] > 0 ) ) return $term; 814 719 if ( ! ( $file['size'] > 0 ) ) { 720 return $term; 721 } 722 815 723 // A properly uploaded file will pass this test. 816 if ( ! @ is_uploaded_file( $file['tmp_name'] ) ) 817 return $this->upload_error($file, __( 'Specified file failed upload test.' )); 818 724 if ( ! @ is_uploaded_file( $file['tmp_name'] ) ) { 725 return $this->upload_error( $file, __( 'Specified file failed upload test.' ) ); 726 } 727 819 728 // mime check 820 729 $wp_filetype = wp_check_filetype_and_ext( $file['tmp_name'], $file['name'], self::$mimes ); 821 730 extract( $wp_filetype ); 822 731 823 if ( ( !$type || !$ext ) && !current_user_can( 'unfiltered_upload' ) ) 824 return $this->upload_error($file, __( 'Sorry, this file type is not permitted for security reasons.' )); 732 if ( ( ! $type || ! $ext ) && ! current_user_can( 'unfiltered_upload' ) ) { 733 return $this->upload_error( $file, __( 'Sorry, this file type is not permitted for security reasons.' ) ); 734 } 825 735 826 736 /* delete old image if it exists */ 827 if( false === self::remove_term_image($term_id, $taxonomy) ) { 828 @ unlink($new_file); 829 return $this->upload_error($file, __( 'An error occured when trying to remove the old image.', 'gecka-terms-thumbnails' )); 830 } 831 832 if ( $proper_filename ) 737 if ( false === self::remove_term_image( $term_id, $taxonomy ) ) { 738 @ unlink( $new_file ); 739 740 return $this->upload_error( $file, __( 'An error occured when trying to remove the old image.', 'gecka-terms-thumbnails' ) ); 741 } 742 743 if ( $proper_filename ) { 833 744 $file['name'] = $proper_filename; 834 835 if ( !$ext ) 836 $ext = ltrim(strrchr($file['name'], '.'), '.'); 837 838 if ( !$type ) 745 } 746 747 if ( ! $ext ) { 748 $ext = ltrim( strrchr( $file['name'], '.' ), '.' ); 749 } 750 751 if ( ! $type ) { 839 752 $type = $file['type']; 840 753 } 754 841 755 /* get a unique filename */ 842 843 756 757 844 758 $filename = wp_unique_filename( $dir, $file['name'] ); 845 759 846 760 // Move the file to the uploads dir 847 761 $new_file = $dir . "/$filename"; 848 762 849 763 /* moves uploaded file */ 850 if ( false === @ move_uploaded_file( $file['tmp_name'], $new_file ) ) 764 if ( false === @ move_uploaded_file( $file['tmp_name'], $new_file ) ) { 851 765 return 852 853 $file_infos = array(); 854 $file_infos ['name'] = $filename; 855 $file_infos ['size'] = $file['size']; 856 $file_infos ['path'] = $new_file; 857 $file_infos ['url'] = self::images_url() . '/' . $taxonomy . '/' . $filename; 858 $file_infos ['type'] = $type; 859 $file_infos ['ext'] = $ext; 860 $file_infos ['infos'] = getimagesize($new_file); 861 $file_infos ['thumbnails'] = array(); 862 863 self::update_term_image_infos($term_id, $taxonomy, $file_infos); 864 865 } 866 766 767 $file_infos = array(); 768 } 769 $file_infos ['name'] = $filename; 770 $file_infos ['size'] = $file['size']; 771 $file_infos ['path'] = $new_file; 772 $file_infos ['url'] = self::images_url() . '/' . $taxonomy . '/' . $filename; 773 $file_infos ['type'] = $type; 774 $file_infos ['ext'] = $ext; 775 $file_infos ['infos'] = getimagesize( $new_file ); 776 $file_infos ['thumbnails'] = array(); 777 778 self::update_term_image_infos( $term_id, $taxonomy, $file_infos ); 779 780 } 781 782 /** 783 * Make a directory 784 * 785 * @param string $taxonomy optional category name to create a taxnonomy directory 786 */ 787 public static function images_mkdir( $taxonomy = '' ) { 788 789 global $wp_filesystem; 790 WP_Filesystem(); 791 792 $dir = self::images_dir() . ( $taxonomy ? '/' . $taxonomy : '' ); 793 794 if ( ! wp_mkdir_p( $dir ) && ! is_dir( $dir ) ) // Only check to see if the Dir exists upon creation failure. Less I/O this way. 795 { 796 wp_die( __( 'Could not create directory.' ) ); 797 } 798 799 return $dir; 800 } 801 802 /** 803 * Returns the absolute path to the thumbnails folder 804 * @return string 805 */ 806 public static function images_dir() { 807 $upload_dir_infos = wp_upload_dir(); 808 809 return $upload_dir_infos['basedir'] . '/terms-images'; 810 } 811 812 /** 813 * Handles upload errors 814 * 815 * @param array $file 816 * @param $message $message 817 */ 818 private function upload_error( &$file, $message ) { 819 $this->error = new WP_Error( 'invalid_upload', $message, $file ); 820 821 return false; 822 } 823 824 /** 825 * Remove a term's image (and its thumbnails) 826 * 827 * @param int $term_id 828 * @param string $taxonomy 829 */ 830 public static function remove_term_image( $term_id, $taxonomy ) { 831 832 $infos = self::get_term_image_infos( $term_id, $taxonomy ); 833 834 if ( ! $infos ) { 835 return; 836 } 837 838 if ( ! empty( $infos ) && isset( $infos['path'] ) ) { 839 840 if ( false === self::remove_term_thumbnails( $term_id, $taxonomy ) ) { 841 return false; 842 } 843 844 if ( ! @ unlink( $infos['path'] ) && file_exists( $infos['path'] ) ) { 845 return false; 846 } 847 848 } 849 850 self::delete_term_image_infos( $term_id, $taxonomy ); 851 852 return true; 853 854 } 855 856 /** 857 * Removes the generated thumbnails of a term 858 * 859 * @param int $term_id 860 * @param string $taxonomy 861 * 862 * @return bool 863 */ 864 public static function remove_term_thumbnails( $term_id, $taxonomy ) { 865 866 $infos = self::get_term_image_infos( $term_id, $taxonomy ); 867 868 if ( ! $infos ) { 869 return; 870 } 871 872 if ( empty( $infos['thumbnails'] ) ) { 873 return true; 874 } 875 876 foreach ( $infos['thumbnails'] as $name => $thumbnail ) { 877 if ( false === self::remove_term_thumbnail( $name, $term_id, $taxonomy ) ) { 878 return false; 879 } 880 } 881 882 return true; 883 884 } 885 886 /** 887 * Removes aterm's thumbnail 888 * @pram string $thumbnail_name 889 * 890 * @param int $term_id 891 * @param string $taxonomy 892 * 893 * @return bool 894 */ 895 public static function remove_term_thumbnail( $thumbnail_name, $term_id, $taxonomy ) { 896 897 if ( $thumbnail_name == 'admin_thumbnail' ) { 898 return; 899 } 900 901 $infos = self::get_term_image_infos( $term_id, $taxonomy ); 902 if ( ! $infos ) { 903 return; 904 } 905 906 if ( empty( $infos['thumbnails'] ) || empty( $infos['thumbnails'][ $thumbnail_name ] ) ) { 907 return true; 908 } 909 910 $thumbnail = $infos['thumbnails'][ $thumbnail_name ]; 911 912 if ( file_exists( $thumbnail['path'] ) && ! @ unlink( $thumbnail['path'] ) ) { 913 return false; 914 } 915 unset( $infos['thumbnails'][ $thumbnail_name ] ); 916 917 self::update_term_image_infos( $term_id, $taxonomy, $infos ); 918 919 return true; 920 921 } 922 923 /** 924 * Updates a term thumbnail metadata 925 * 926 * @param int $term_id 927 * @param sttring $taxonomy 928 * @param array $infos 929 * 930 * @return boolean 931 */ 932 public static function update_term_image_infos( $term_id, $taxonomy, $infos ) { 933 934 // compatibility with beta1 935 if ( get_metadata( 'term', $term_id, 'image', true ) ) { 936 delete_metadata( 'term', $term_id, 'image' ); 937 } 938 939 return update_metadata( 'term', $term_id, 'image-' . $taxonomy, $infos ); 940 941 } 942 943 /** 944 * Deletes a term's thumbnail metadata 945 * 946 * @param int $term_id 947 * @param string $taxonomy 948 * 949 * @return boolean 950 */ 951 public static function delete_term_image_infos( $term_id, $taxonomy ) { 952 953 // compatibility with beta1 954 if ( get_metadata( 'term', $term_id, 'image-' . $taxonomy, true ) ) { 955 return delete_metadata( 'term', $term_id, 'image-' . $taxonomy ); 956 } 957 958 // compatibility with beta1 959 if ( get_metadata( 'term', $term_id, 'image', true ) ) { 960 return delete_metadata( 'term', $term_id, 'image' ); 961 } 962 963 return delete_metadata( 'term', $term_id, 'image-' . $taxonomy ); 964 965 } 966 967 /** 968 * Returns the URI to the thumbnails folder 969 * @return string 970 */ 971 public static function images_url() { 972 $upload_dir_infos = wp_upload_dir(); 973 974 return $upload_dir_infos['baseurl'] . '/terms-images'; 975 } 976 867 977 /** 868 978 * Denerates the thumbnails of a saved term 869 979 * Runs on the edited_$taxonomy action hook 980 * 870 981 * @param unknown_type $term_id 871 982 * @param unknown_type $tt_id 872 983 */ 873 public function generate_thumbnails_action ($term_id, $tt_id) { 874 875 $taxonomy = !empty($_POST['taxonomy']) ? $_POST['taxonomy'] : 'category'; 876 877 if( ! self::has_support($taxonomy) ) return; 878 879 self::generate_thumbnails($term_id, $taxonomy); 880 } 881 984 public function generate_thumbnails_action( $term_id, $tt_id ) { 985 986 $taxonomy = ! empty( $_POST['taxonomy'] ) ? $_POST['taxonomy'] : 'category'; 987 988 if ( ! self::has_support( $taxonomy ) ) { 989 return; 990 } 991 992 self::generate_thumbnails( $term_id, $taxonomy ); 993 } 994 995 /** 996 * Generate a term's thumbnails 997 * 998 * @param int $term_id 999 * @param string $taxonomy 1000 */ 1001 public static function generate_thumbnails( $term_id, $taxonomy ) { 1002 1003 $infos = self::get_term_image_infos( $term_id, $taxonomy ); 1004 1005 if ( ! $infos ) { 1006 return; 1007 } 1008 1009 $thumbnails = ! empty( $infos['thumbnails'] ) ? $infos['thumbnails'] : array(); 1010 1011 // removes obsolete thumbnails 1012 foreach ( $thumbnails as $name => $size ) { 1013 if ( ! isset( self::$thumbnails_sizes[ $name ] ) ) { 1014 self::remove_term_thumbnail( $name, $term_id, $taxonomy ); 1015 } 1016 } 1017 1018 // creates all thumbnails images 1019 foreach ( self::$thumbnails_sizes as $key => $size ) { 1020 1021 if ( ! empty( $thumbnails[ $key ] ) ) { 1022 self::remove_term_thumbnail( $key, $term_id, $taxonomy ); 1023 } 1024 1025 $img = self::image_resize( $infos['path'], $size['width'], $size['height'], $size['crop'], $key ); 1026 1027 if ( ! $img || is_wp_error( $img ) ) { 1028 continue; 1029 } 1030 1031 $file_infos = array(); 1032 $file_infos ['name'] = basename( $img ); 1033 $file_infos ['path'] = $img; 1034 $file_infos ['infos'] = getimagesize( $img ); 1035 $file_infos ['url'] = self::images_url() . '/' . $taxonomy . '/' . basename( $img ); 1036 1037 $infos['thumbnails'][ $key ] = $file_infos; 1038 } 1039 1040 self::update_term_image_infos( $term_id, $taxonomy, $infos ); 1041 1042 } 1043 1044 public static function image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) { 1045 1046 $editor = wp_get_image_editor( $file ); 1047 if ( is_wp_error( $editor ) ) { 1048 return $editor; 1049 } 1050 $editor->set_quality( $jpeg_quality ); 1051 1052 $resized = $editor->resize( $max_w, $max_h, $crop ); 1053 if ( is_wp_error( $resized ) ) { 1054 return $resized; 1055 } 1056 1057 $dest_file = $editor->generate_filename( $suffix, $dest_path ); 1058 $saved = $editor->save( $dest_file ); 1059 1060 if ( is_wp_error( $saved ) ) { 1061 return $saved; 1062 } 1063 1064 return $dest_file; 1065 } 1066 882 1067 /** 883 1068 * Remove term image on deletetion of that term 884 1069 * Runs on the delete_term action hook 1070 * 885 1071 * @param int $term 886 1072 * @param int $tt_id 887 1073 * @param string $taxonomy 888 1074 */ 889 public function delete_term ( $term, $tt_id, $taxonomy ) {890 self::remove_term_image( $term, $taxonomy);891 } 892 1075 public function delete_term( $term, $tt_id, $taxonomy ) { 1076 self::remove_term_image( $term, $taxonomy ); 1077 } 1078 893 1079 /** 894 1080 * Adds a new column to taxonomies supporting thumbnails 895 1081 * Runs on the manage_edit-{$taxonomy}_columns action hook 896 1082 */ 897 public function edit_columns ($columns) {898 unset( $columns["cb"] );899 900 $custom_array = array(901 'cb'=> '<input type="checkbox" />',902 'image' => __( 'Image' )903 );904 905 $columns = array_merge( $custom_array, $columns );906 907 return $columns;908 } 909 1083 public function edit_columns( $columns ) { 1084 unset( $columns["cb"] ); 1085 1086 $custom_array = array( 1087 'cb' => '<input type="checkbox" />', 1088 'image' => __( 'Image' ) 1089 ); 1090 1091 $columns = array_merge( $custom_array, $columns ); 1092 1093 return $columns; 1094 } 1095 910 1096 /** 911 1097 * Handles the thumbnial column content 912 1098 * Runs on the manage_{$taxonomy}_columns action hook 913 1099 */ 914 public function columns ($null, $column_name, $term_id) {915 1100 public function columns( $null, $column_name, $term_id ) { 1101 916 1102 $taxonomy = isset( $_GET['taxonomy'] ) ? $_GET['taxonomy'] : ''; 917 918 if( !$taxonomy ) return ''; 919 1103 1104 if ( ! $taxonomy ) { 1105 return ''; 1106 } 1107 920 1108 switch ( $column_name ) { 921 1109 case 'image': 922 return get_the_term_thumbnail( $term_id, $taxonomy, 'admin-thumbnail' ); 1110 return get_the_term_thumbnail( $term_id, $taxonomy, 'admin-thumbnail' ); 923 1111 break; 924 1112 } 925 1113 926 1114 return ''; 927 1115 } 928 1116 929 1117 /** 930 1118 * Handles httpr term image deletion 931 1119 * Runs on the wp_ajax_delete_term_image action hook 932 1120 */ 933 public function ajax_delete_term_image () { 934 935 $term_id = isset($_POST['term_id']) && (int) $_POST['term_id'] ? (int) $_POST['term_id'] : ''; 936 $taxonomy = isset($_POST['taxonomy']) && $_POST['taxonomy'] ? $_POST['taxonomy'] : ''; 937 938 if( ! get_taxonomy($taxonomy) ) die(0); 939 940 if( ! $term_id || ! wp_verify_nonce( $_POST['_nonce'], 'delete_term_image') ) die(0); 941 942 self::remove_term_image($term_id, $taxonomy); 943 944 die('1'); 945 } 946 947 /** 948 * Handles upload errors 949 * @param array $file 950 * @param $message $message 951 */ 952 private function upload_error( &$file, $message ) { 953 $this->error = new WP_Error('invalid_upload', $message, $file); 954 955 return false; 956 } 957 1121 public function ajax_delete_term_image() { 1122 1123 $term_id = isset( $_POST['term_id'] ) && (int) $_POST['term_id'] ? (int) $_POST['term_id'] : ''; 1124 $taxonomy = isset( $_POST['taxonomy'] ) && $_POST['taxonomy'] ? $_POST['taxonomy'] : ''; 1125 1126 if ( ! get_taxonomy( $taxonomy ) ) { 1127 die( 0 ); 1128 } 1129 1130 if ( ! $term_id || ! wp_verify_nonce( $_POST['_nonce'], 'delete_term_image' ) ) { 1131 die( 0 ); 1132 } 1133 1134 self::remove_term_image( $term_id, $taxonomy ); 1135 1136 die( '1' ); 1137 } 1138 958 1139 } 959 1140 960 if ( ! function_exists('add_term_thumbnails_support') ) {1141 if ( ! function_exists( 'add_term_thumbnails_support' ) ) { 961 1142 /** 962 1143 * Adds thumbnails support for the provided taxonomy 963 * @param string $taxonomy 964 */ 965 function add_term_thumbnails_support ($taxonomy) { 966 Gecka_Terms_Thumbnails::add_taxonomy_support($taxonomy); 967 } 1144 * 1145 * @param string $taxonomy 1146 */ 1147 function add_term_thumbnails_support( $taxonomy ) { 1148 Gecka_Terms_Thumbnails::add_taxonomy_support( $taxonomy ); 1149 } 968 1150 } 969 1151 970 if ( ! function_exists('remove_term_thumbnails_support') ) {1152 if ( ! function_exists( 'remove_term_thumbnails_support' ) ) { 971 1153 /** 972 1154 * Removes thumbnails support for the provided taxonomy 973 * @param string $taxonomy 974 */ 975 function remove_term_thumbnails_support ($taxonomy) { 976 Gecka_Terms_Thumbnails::remove_taxonomy_support($taxonomy); 977 } 1155 * 1156 * @param string $taxonomy 1157 */ 1158 function remove_term_thumbnails_support( $taxonomy ) { 1159 Gecka_Terms_Thumbnails::remove_taxonomy_support( $taxonomy ); 1160 } 978 1161 } 979 1162 980 if ( ! function_exists('has_term_thumbnails_support') ) {1163 if ( ! function_exists( 'has_term_thumbnails_support' ) ) { 981 1164 /** 982 1165 * Checks if the provided taxonomy has thumbnails support 983 * @param string $taxonomy 1166 * 1167 * @param string $taxonomy 1168 * 984 1169 * @return bool true if the taxonomy has thumbnial support, false otherwise 985 1170 */ 986 function has_term_thumbnails_support ($taxonomy) {987 return Gecka_Terms_Thumbnails::has_support( $taxonomy);988 } 1171 function has_term_thumbnails_support( $taxonomy ) { 1172 return Gecka_Terms_Thumbnails::has_support( $taxonomy ); 1173 } 989 1174 } 990 1175 991 if ( ! function_exists('add_term_image_size') ) {1176 if ( ! function_exists( 'add_term_image_size' ) ) { 992 1177 /** 993 1178 * Adds a term image size 1179 * 994 1180 * @param string $name the thumbnail size name for reference 995 * @param unknown_type $width996 * @param unknown_type $height997 * @param unknown_type $crop998 */999 function add_term_image_size ( $name, $width = 0, $height = 0, $crop = false ) {1000 return Gecka_Terms_Thumbnails::add_image_size ( $name, $width, $height, $crop );1001 }1002 }1003 1004 if( ! function_exists('set_term_thumbnail') ) {1005 /**1006 * Sets the default thumbnail size1007 1181 * @param unknown_type $width 1008 1182 * @param unknown_type $height 1009 1183 * @param unknown_type $crop 1010 1184 */ 1011 function set_term_thumbnail ( $width = 0, $height = 0, $crop = false ) { 1185 function add_term_image_size( $name, $width = 0, $height = 0, $crop = false ) { 1186 return Gecka_Terms_Thumbnails::add_image_size( $name, $width, $height, $crop ); 1187 } 1188 } 1189 1190 if ( ! function_exists( 'set_term_thumbnail' ) ) { 1191 /** 1192 * Sets the default thumbnail size 1193 * 1194 * @param unknown_type $width 1195 * @param unknown_type $height 1196 * @param unknown_type $crop 1197 */ 1198 function set_term_thumbnail( $width = 0, $height = 0, $crop = false ) { 1012 1199 return Gecka_Terms_Thumbnails::set_thumbnail( $width, $height, $crop ); 1013 } 1200 } 1014 1201 } 1015 1202 1016 if ( ! function_exists('has_term_thumbnail') ) {1203 if ( ! function_exists( 'has_term_thumbnail' ) ) { 1017 1204 /** 1018 1205 * Checks if the secified term has a thumbnail image for the specified taxonomy and size 1206 * 1019 1207 * @param int $term_id the term ID 1020 * @param string $taxonomy the taxonomy name 1208 * @param string $taxonomy the taxonomy name 1021 1209 * @param string $size the thumbnail size 1022 1210 */ 1023 function has_term_thumbnail ( $term_id, $taxonomy, $size=null ) {1024 return Gecka_Terms_Thumbnails::has_term_thumbnail( $term_id, $taxonomy, $size ); 1211 function has_term_thumbnail( $term_id, $taxonomy, $size = null ) { 1212 return Gecka_Terms_Thumbnails::has_term_thumbnail( $term_id, $taxonomy, $size ); 1025 1213 } 1026 1214 } 1027 1215 1028 if ( ! function_exists('the_term_thumbnail') ) {1216 if ( ! function_exists( 'the_term_thumbnail' ) ) { 1029 1217 /** 1030 1218 * Prints the specified term's thumbnail HTML code for the specified taxonomy and size 1219 * 1031 1220 * @param int $term_id the term ID 1032 1221 * @param string $taxonomy the taxonomy name … … 1034 1223 * @param array $attr additionnal attributes 1035 1224 */ 1036 function the_term_thumbnail ( $term_id, $taxonomy, $size = 'thumbnail', $attr = '') {1037 echo Gecka_Terms_Thumbnails::get_the_term_thumbnail( $term_id, $taxonomy, $size, $attr ); 1225 function the_term_thumbnail( $term_id, $taxonomy, $size = 'thumbnail', $attr = '' ) { 1226 echo Gecka_Terms_Thumbnails::get_the_term_thumbnail( $term_id, $taxonomy, $size, $attr ); 1038 1227 } 1039 1228 } 1040 1229 1041 if ( ! function_exists('get_the_term_thumbnail') ) {1230 if ( ! function_exists( 'get_the_term_thumbnail' ) ) { 1042 1231 /** 1043 1232 * Returns the specified term's thumbnail HTML code for the specified taxonomy and size 1233 * 1044 1234 * @param int $term_id the term ID 1045 1235 * @param string $taxonomy the taxonomy name … … 1047 1237 * @param array $attr additionnal attributes 1048 1238 */ 1049 function get_the_term_thumbnail ( $term_id, $taxonomy, $size = 'thumbnail', $attr = '' ) {1050 return Gecka_Terms_Thumbnails::get_the_term_thumbnail( $term_id, $taxonomy, $size, $attr ); 1239 function get_the_term_thumbnail( $term_id, $taxonomy, $size = 'thumbnail', $attr = '' ) { 1240 return Gecka_Terms_Thumbnails::get_the_term_thumbnail( $term_id, $taxonomy, $size, $attr ); 1051 1241 } 1052 1242 } 1053 1243 1054 if ( ! function_exists('get_term_thumbnail') ) {1244 if ( ! function_exists( 'get_term_thumbnail' ) ) { 1055 1245 /** 1056 1246 * Returns the specified term's thumbnail for the specified taxonomy and size 1247 * 1057 1248 * @param int $term_id the term ID 1058 1249 * @param string $taxonomy the taxonomy name … … 1060 1251 * @param array $attr additionnal attributes 1061 1252 */ 1062 function get_term_thumbnail ($term_id, $size) {1063 return Gecka_Terms_Thumbnails::get_term_thumbnail( $term_id, $taxonomy, $size);1253 function get_term_thumbnail( $term_id, $size ) { 1254 return Gecka_Terms_Thumbnails::get_term_thumbnail( $term_id, $taxonomy, $size ); 1064 1255 } 1065 1256 } 1066 1257 1067 if ( ! function_exists( 'wp_list_terms') ) {1258 if ( ! function_exists( 'wp_list_terms' ) ) { 1068 1259 /** 1069 1260 * Returns an HTML list of terms 1261 * 1070 1262 * @param array $args 1263 * 1071 1264 * @return Ambigous <string, boolean, mixed> 1072 1265 */ 1073 function wp_list_terms ( $args ) { 1074 1075 wp_parse_args($args); 1076 if( ! isset($args['walker']) || ! is_a( $args['walker'], 'Walker') ) $args['walker'] = new Walker_Term(); 1077 1266 function wp_list_terms( $args ) { 1267 1268 wp_parse_args( $args ); 1269 if ( ! isset( $args['walker'] ) || ! is_a( $args['walker'], 'Walker' ) ) { 1270 $args['walker'] = new Walker_Term(); 1271 } 1272 1078 1273 return wp_list_categories( $args ); 1079 1274 } … … 1098 1293 * @param array $args 1099 1294 */ 1100 function start_el( &$output, $category, $depth, $args) {1101 extract( $args);1295 function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) { 1296 extract( $args ); 1102 1297 1103 1298 $cat_name = esc_attr( $category->name ); 1104 1299 $cat_name = apply_filters( 'list_cats', $cat_name, $category ); 1105 1106 $link = '<a href="' . esc_attr( get_term_link( $category) ) . '" ';1107 if ( $use_desc_for_title == 0 || empty( $category->description) )1108 $link .= 'title="' . esc_attr( sprintf( __( 'View all posts filed under %s' ), $cat_name) ) . '"';1109 else1300 1301 $link = '<a href="' . esc_attr( get_term_link( $category ) ) . '" '; 1302 if ( $use_desc_for_title == 0 || empty( $category->description ) ) { 1303 $link .= 'title="' . esc_attr( sprintf( __( 'View all posts filed under %s' ), $cat_name ) ) . '"'; 1304 } else { 1110 1305 $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"'; 1306 } 1111 1307 $link .= '>'; 1112 1308 1113 if ( !empty($args['show_thumbnail']) && has_term_thumbnail($category->term_id, $category->taxonomy, $args['show_thumbnail']) ) {1114 1115 if ( ! empty($args['thumbnail_position']) && $args['thumbnail_position'] === 'inside' )1116 $link .= get_the_term_thumbnail( $category->term_id, $category->taxonomy, $args['show_thumbnail']);1117 1118 else1119 $link = $link . get_the_term_thumbnail($category->term_id, $category->taxonomy, $args['show_thumbnail']). '</a> ' . $link;1120 } 1121 1122 $link .= $cat_name . '</a>';1123 1124 if ( ! empty($feed_image) || !empty($feed) ) {1309 if ( ! empty( $args['show_thumbnail'] ) && has_term_thumbnail( $category->term_id, $category->taxonomy, $args['show_thumbnail'] ) ) { 1310 1311 if ( ! empty( $args['thumbnail_position'] ) && $args['thumbnail_position'] === 'inside' ) { 1312 $link .= get_the_term_thumbnail( $category->term_id, $category->taxonomy, $args['show_thumbnail'] ); 1313 } else { 1314 $link = $link . get_the_term_thumbnail( $category->term_id, $category->taxonomy, $args['show_thumbnail'] ) . '</a> ' . $link; 1315 } 1316 } 1317 1318 $link .= $cat_name . '</a>'; 1319 1320 if ( ! empty( $feed_image ) || ! empty( $feed ) ) { 1125 1321 $link .= ' '; 1126 1322 1127 if ( empty( $feed_image) )1323 if ( empty( $feed_image ) ) { 1128 1324 $link .= '('; 1325 } 1129 1326 1130 1327 $link .= '<a href="' . get_term_feed_link( $category->term_id, $category->taxonomy, $feed_type ) . '"'; 1131 1328 1132 if ( empty( $feed) ) {1133 $alt = ' alt="' . sprintf( __( 'Feed for all posts filed under %s' ), $cat_name ) . '"';1329 if ( empty( $feed ) ) { 1330 $alt = ' alt="' . sprintf( __( 'Feed for all posts filed under %s' ), $cat_name ) . '"'; 1134 1331 } else { 1135 1332 $title = ' title="' . $feed . '"'; 1136 $alt = ' alt="' . $feed . '"';1137 $name = $feed;1333 $alt = ' alt="' . $feed . '"'; 1334 $name = $feed; 1138 1335 $link .= $title; 1139 1336 } … … 1141 1338 $link .= '>'; 1142 1339 1143 if ( empty( $feed_image) )1340 if ( empty( $feed_image ) ) { 1144 1341 $link .= $name; 1145 else1342 } else { 1146 1343 $link .= "<img src='$feed_image'$alt$title" . ' />'; 1344 } 1147 1345 1148 1346 $link .= '</a>'; 1149 1347 1150 if ( empty( $feed_image) )1348 if ( empty( $feed_image ) ) { 1151 1349 $link .= ')'; 1152 } 1153 1154 if ( !empty($show_count) ) 1155 $link .= ' (' . intval($category->count) . ')'; 1156 1157 if ( !empty($show_date) ) 1158 $link .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp); 1350 } 1351 } 1352 1353 if ( ! empty( $show_count ) ) { 1354 $link .= ' (' . intval( $category->count ) . ')'; 1355 } 1356 1357 if ( ! empty( $show_date ) ) { 1358 $link .= ' ' . gmdate( 'Y-m-d', $category->last_update_timestamp ); 1359 } 1159 1360 1160 1361 if ( 'list' == $args['style'] ) { 1161 1362 $output .= "\t<li"; 1162 1363 $class = 'cat-item cat-item-' . $category->term_id; 1163 if ( ! empty($current_category) ) {1364 if ( ! empty( $current_category ) ) { 1164 1365 $_current_category = get_term( $current_category, $category->taxonomy ); 1165 if ( $category->term_id == $current_category ) 1166 $class .= ' current-cat'; 1167 elseif ( $category->term_id == $_current_category->parent ) 1168 $class .= ' current-cat-parent'; 1169 } 1170 $output .= ' class="' . $class . '"'; 1366 if ( $category->term_id == $current_category ) { 1367 $class .= ' current-cat'; 1368 } elseif ( $category->term_id == $_current_category->parent ) { 1369 $class .= ' current-cat-parent'; 1370 } 1371 } 1372 $output .= ' class="' . $class . '"'; 1171 1373 $output .= ">$link\n"; 1172 1374 } else { … … 1175 1377 } 1176 1378 1177 1178 1379 } 1179 1180 -
gecka-terms-thumbnails/trunk/readme.txt
r453683 r1269061 1 1 === Gecka Terms Thumbnails === 2 Contributors: Gecka Apps 3 Tags: term, terms, category, categories, term image, category image, categories images, taxonomy image, taxo omies images, categories thumbnails, thumbnail2 Contributors: Gecka Apps, Gecka 3 Tags: term, terms, category, categories, term image, category image, categories images, taxonomy image, taxonomies images, categories thumbnails, thumbnail 4 4 Requires at least: 3.0 5 Tested up to: 3.26 Stable tag: 1. 0-beta47 Manage t umbnails for categories, tags or any other taxonomy's terms of your Wordpress website.5 Tested up to: 4.4 6 Stable tag: 1.1 7 Manage thumbnails for categories, tags or any other taxonomy's terms of your Wordpress website. 8 8 9 9 == Description == … … 31 31 == Changelog == 32 32 33 = 1.1 = 34 * Support up to Wordpress 4.4 35 33 36 = 1.0-beta4 = 34 37 * Fixes has_term_thumbnail function -
gecka-terms-thumbnails/trunk/settings.php
r453680 r1269061 5 5 */ 6 6 class Gecka_Terms_Thumbnails_Settings { 7 7 8 8 private static $instance; 9 9 10 10 private static $settings; 11 12 private static $defaults = array( 'term_image_max_w' => 1024, 13 'term_image_max_h' => 1024, 14 'term_thumbnail_size_w' => 50, 15 'term_thumbnail_size_h' => 50, 16 'term_thumbnail_crop' => 1, 17 18 'term_medium_size_w' => 150, 19 'term_medium_size_h' => 150, 20 'term_medium_crop' => 0, 21 22 'use_wp_media' => false 23 ); 24 11 12 private static $defaults = array( 13 'term_image_max_w' => 1024, 14 'term_image_max_h' => 1024, 15 'term_thumbnail_size_w' => 50, 16 'term_thumbnail_size_h' => 50, 17 'term_thumbnail_crop' => 1, 18 'term_medium_size_w' => 150, 19 'term_medium_size_h' => 150, 20 'term_medium_crop' => 0, 21 'use_wp_media' => false 22 ); 23 25 24 private function __construct() { 26 27 foreach ( self::$defaults as $key => $value) {28 self::$settings[ $key] = get_option($key, $value);25 26 foreach ( self::$defaults as $key => $value ) { 27 self::$settings[ $key ] = get_option( $key, $value ); 29 28 } 30 31 add_action( 'admin_init', array( $this, 'admin_init') );32 29 30 add_action( 'admin_init', array( $this, 'admin_init' ) ); 31 33 32 } 34 33 34 /*************************************************************************** 35 * Static functions 36 **************************************************************************/ 37 38 public static function instance() { 39 40 if ( ! isset( self::$instance ) ) { 41 $class_name = __CLASS__; 42 self::$instance = new $class_name; 43 } 44 45 return self::$instance; 46 47 } 48 35 49 public function __get( $key ) { 36 if( isset( self::$settings[$key] ) ) return self::$settings[$key]; 50 if ( isset( self::$settings[ $key ] ) ) { 51 return self::$settings[ $key ]; 52 } 53 37 54 return; 38 55 } 39 56 40 57 public function __set( $key, $value ) { 41 if(isset(self::$defaults[$key])) 42 self::$settings[$key] = $value; 58 if ( isset( self::$defaults[ $key ] ) ) { 59 self::$settings[ $key ] = $value; 60 } 61 43 62 return; 44 63 } 45 46 public function __save( ) { 47 foreach (self::$settings as $key => $value) { 48 if(isset(self::$defaults[$key])) 49 update_option($key, $value); 64 65 public function __save() { 66 foreach ( self::$settings as $key => $value ) { 67 if ( isset( self::$defaults[ $key ] ) ) { 68 update_option( $key, $value ); 69 } 50 70 } 51 71 } 52 72 53 73 /*************************************************************************** 54 * Static functions 55 **************************************************************************/ 56 57 public static function instance () { 74 * Actions and filters hooks 75 **************************************************************************/ 76 public function admin_init() { 58 77 59 if ( ! isset( self::$instance ) ) { 60 $class_name = __CLASS__; 61 self::$instance = new $class_name; 62 } 78 register_setting( 'media', 'term_image_max_w', 'intval' ); 79 register_setting( 'media', 'term_image_max_h', 'intval' ); 80 add_settings_field( 'term_image_max_w', __( 'Term image max size', 'gecka-terms-thumbnails' ), array( 81 $this, 82 'field_image_max_size' 83 ), 'media' ); 63 84 64 return self::$instance; 65 66 } 67 68 /*************************************************************************** 69 * Actions and filters hooks 70 **************************************************************************/ 71 public function admin_init() { 72 73 register_setting('media','term_image_max_w', 'intval'); 74 register_setting('media','term_image_max_h', 'intval'); 75 add_settings_field('term_image_max_w', __('Term image max size', 'gecka-terms-thumbnails'), array($this, 'field_image_max_size'), 'media'); 76 77 register_setting('media','term_thumbnail_size_w', 'intval'); 78 register_setting('media','term_thumbnail_size_h', 'intval'); 79 register_setting('media','term_thumbnail_crop'); 80 add_settings_field('term_thumbnail_size_w', __('Term thumbnail max size', 'gecka-terms-thumbnails'), array($this, 'field_thumbnail_max_size'), 'media'); 81 82 register_setting('media','term_medium_size_w', 'intval'); 83 register_setting('media','term_medium_size_h', 'intval'); 84 register_setting('media','term_medium_crop'); 85 add_settings_field('term_medium_size_w', __('Term medium thumbnail max size', 'gecka-terms-thumbnails'), array($this, 'field_medium_max_size'), 'media'); 86 87 } 88 89 public function field_image_max_size () { 85 register_setting( 'media', 'term_thumbnail_size_w', 'intval' ); 86 register_setting( 'media', 'term_thumbnail_size_h', 'intval' ); 87 register_setting( 'media', 'term_thumbnail_crop' ); 88 add_settings_field( 'term_thumbnail_size_w', __( 'Term thumbnail max size', 'gecka-terms-thumbnails' ), array( 89 $this, 90 'field_thumbnail_max_size' 91 ), 'media' ); 92 93 register_setting( 'media', 'term_medium_size_w', 'intval' ); 94 register_setting( 'media', 'term_medium_size_h', 'intval' ); 95 register_setting( 'media', 'term_medium_crop' ); 96 add_settings_field( 'term_medium_size_w', __( 'Term medium thumbnail max size', 'gecka-terms-thumbnails' ), array( 97 $this, 98 'field_medium_max_size' 99 ), 'media' ); 100 101 } 102 103 public function field_image_max_size() { 90 104 91 105 ?> 92 <label for="term_image_max_w"><?php _e('Max Width'); ?></label> 93 <input name="term_image_max_w" type="text" id="term_image_max_w" value="<?php esc_attr_e( $this->term_image_max_w ); ?>" class="small-text" /> 94 <label for="term_image_max_h"><?php _e('Max Height'); ?></label> 95 <input name="term_image_max_h" type="text" id="term_image_max_h" value="<?php esc_attr_e( $this->term_image_max_h ); ?>" class="small-text" /> 106 <label for="term_image_max_w"><?php _e( 'Max Width' ); ?></label> 107 <input name="term_image_max_w" type="text" id="term_image_max_w" 108 value="<?php esc_attr_e( $this->term_image_max_w ); ?>" class="small-text"/> 109 <label for="term_image_max_h"><?php _e( 'Max Height' ); ?></label> 110 <input name="term_image_max_h" type="text" id="term_image_max_h" 111 value="<?php esc_attr_e( $this->term_image_max_h ); ?>" class="small-text"/> 96 112 <br> 97 <span class="description"><?php _e('If one is set, any uploaded original term image will be proportionally resized to this max width.', 'gecka-terms-thumbnails'); ?></span> 113 <span 114 class="description"><?php _e( 'If one is set, any uploaded original term image will be proportionally resized to this max width.', 'gecka-terms-thumbnails' ); ?></span> 98 115 <?php 99 116 100 117 } 101 102 public function field_thumbnail_max_size () {103 118 119 public function field_thumbnail_max_size() { 120 104 121 ?> 105 <label for="term_thumbnail_size_w"><?php _e('Max Width'); ?></label> 106 <input name="term_thumbnail_size_w" type="text" id="term_thumbnail_size_w" value="<?php esc_attr_e( $this->term_thumbnail_size_w ); ?>" class="small-text" /> 107 <label for="term_thumbnail_size_h"><?php _e('Max Height'); ?></label> 108 <input name="term_thumbnail_size_h" type="text" id="term_thumbnail_size_h" value="<?php esc_attr_e( $this->term_thumbnail_size_h ); ?>" class="small-text" /> 122 <label for="term_thumbnail_size_w"><?php _e( 'Max Width' ); ?></label> 123 <input name="term_thumbnail_size_w" type="text" id="term_thumbnail_size_w" 124 value="<?php esc_attr_e( $this->term_thumbnail_size_w ); ?>" class="small-text"/> 125 <label for="term_thumbnail_size_h"><?php _e( 'Max Height' ); ?></label> 126 <input name="term_thumbnail_size_h" type="text" id="term_thumbnail_size_h" 127 value="<?php esc_attr_e( $this->term_thumbnail_size_h ); ?>" class="small-text"/> 109 128 <br> 110 <input id="term_thumbnail_crop" type="checkbox" <?php checked( $this->term_thumbnail_crop, 1 ); ?> value="1" name="term_thumbnail_crop"> 111 <label for="term_thumbnail_crop"><?php _e('Crop thumbnail to exact dimensions (normally thumbnails are proportional)') ?></label> 129 <input id="term_thumbnail_crop" type="checkbox" <?php checked( $this->term_thumbnail_crop, 1 ); ?> value="1" 130 name="term_thumbnail_crop"> 131 <label 132 for="term_thumbnail_crop"><?php _e( 'Crop thumbnail to exact dimensions (normally thumbnails are proportional)' ) ?></label> 112 133 <?php 113 134 114 135 } 115 116 public function field_medium_max_size () {117 136 137 public function field_medium_max_size() { 138 118 139 ?> 119 <label for="term_medium_size_w"><?php _e('Max Width'); ?></label> 120 <input name="term_medium_size_w" type="text" id="term_medium_size_w" value="<?php esc_attr_e( $this->term_medium_size_w ); ?>" class="small-text" /> 121 <label for="term_medium_size_h"><?php _e('Max Height'); ?></label> 122 <input name="term_medium_size_h" type="text" id="term_medium_size_h" value="<?php esc_attr_e( $this->term_medium_size_h ); ?>" class="small-text" /> 140 <label for="term_medium_size_w"><?php _e( 'Max Width' ); ?></label> 141 <input name="term_medium_size_w" type="text" id="term_medium_size_w" 142 value="<?php esc_attr_e( $this->term_medium_size_w ); ?>" class="small-text"/> 143 <label for="term_medium_size_h"><?php _e( 'Max Height' ); ?></label> 144 <input name="term_medium_size_h" type="text" id="term_medium_size_h" 145 value="<?php esc_attr_e( $this->term_medium_size_h ); ?>" class="small-text"/> 123 146 <br> 124 <input id="term_medium_crop" type="checkbox" <?php checked( $this->term_medium_crop, 1 ); ?> value="1" name="term_medium_crop"> 125 <label for="term_medium_crop"><?php _e('Crop thumbnail to exact dimensions (normally thumbnails are proportional)') ?></label> 147 <input id="term_medium_crop" type="checkbox" <?php checked( $this->term_medium_crop, 1 ); ?> value="1" 148 name="term_medium_crop"> 149 <label 150 for="term_medium_crop"><?php _e( 'Crop thumbnail to exact dimensions (normally thumbnails are proportional)' ) ?></label> 126 151 <?php 127 152 128 153 } 129 154 130 155 }
Note: See TracChangeset
for help on using the changeset viewer.