ig_caticons = $table_prefix.'ig_caticons'; if (basename($_SERVER['SCRIPT_NAME']) == 'plugins.php' && isset($_GET['activate']) && $_GET['activate'] == 'true') add_action('init', 'ig_caticons_install'); add_action('admin_menu', 'ig_caticons_adminmenu'); /*------------------ Initialization and Hooks END ----------------------------*/ /** * WordPress Template Tag to Insert Category Icon * @param $fit_width int [-1] Maximum width (or desired width if $expanded=true) of the image. * @param $fit_height int[-1] Macimum height (or desired height if $expanded=true) of the image. * @param $expand boolean [false] Whether the image should be expanded to fit the rectangle specified by fit_xxx. * @param $cat int Category ID. If not specified, the current category is used or the current post's category. * @param $small boolean [false] Use the small icon. * @param $prefix string String to echo before the image tag. If no image, no otuput. * @param $suffix string String to echo after the image tag. Ignored if no image found. * @param $class string [] Class attribute for the image tag. * @param $link boolean [1] If true the image is made a hyperlink (wrapped by anchor tag). * * @return boolean True if image found. */ function get_cat_icon($params="") { parse_str($params, $p); if (!isset($p['fit_width'])) $p['fit_width']=-1; if (!isset($p['fit_height'])) $p['fit_height']=-1; if (!isset($p['expand'])) $p['expand']=false; if (!isset($p['cat'])) $p['cat']=$GLOBALS['cat']; if (!isset($p['small'])) $p['small']=false; if (!isset($p['prefix'])) $p['prefix'] = ''; if (!isset($p['suffix'])) $p['suffix'] = ''; if (!isset($p['class'])) $p['class'] = ''; if (!isset($p['link'])) $p['link'] = 1; stripslaghes_gpc_arr($p); if (empty($p['cat']) && isset($GLOBALS['post'])) { $catlist = get_the_category($GLOBALS['post']->ID); if (is_array($catlist)) { $p['cat'] = $catlist[0]->cat_ID; } } if (!isset($p['cat'])) return; list($icon, $small_icon) = ig_caticons_get_icons($p['cat']); if ($p['small']) { $file = ig_caticons_path()."/$small_icon"; $url = ig_caticons_url()."/$small_icon"; if (!is_file($file)) { $file = ig_caticons_path()."/$icon"; $url = ig_caticons_url()."/$icon"; } } else { $file = ig_caticons_path()."/$icon"; $url = ig_caticons_url()."/$icon"; if (!is_file($file)) { $file = ig_caticons_path()."/$small_icon"; $url = ig_caticons_url()."/$small_icon"; } } if ($p['link']) { $p['prefix'] .= ''; $p['suffix'] = ''.$p['suffix']; } if (is_file($file)) { list($width, $height, $type, $attr) = getimagesize($file); list($w, $h) = ig_fit_rect($width, $height, $p['fit_width'], $p['fit_height'], $p['expand']); echo("$p[prefix]$p[suffix]"); return true; } return false; } /** * Install the plugin * @access private */ function ig_caticons_install() { global $wpdb, $table_prefix; $wpdb->query("CREATE TABLE IF NOT EXISTS `$wpdb->ig_caticons` ( `cat_id` INT NOT NULL , `icon` TEXT NOT NULL , `small_icon` TEXT NOT NULL , PRIMARY KEY ( `cat_id` ))"); add_option('igcaticons_path', ''); add_option('igcaticons_url', ''); add_option('igcaticons_filetypes', 'jpg gif jpeg png'); } function ig_caticons_defupload() { $def_path = str_replace(ABSPATH, '', get_settings('upload_path')); $def_url = trailingslashit(get_settings('siteurl')) . $def_path; return array($def_path, $def_url); } /** * Integrate into the admin menu. * @access private */ function ig_caticons_adminmenu() { define('IG_ADMIN_SELF', $_SERVER['PHP_SELF'].'?page='.$_REQUEST['page']); if (function_exists('add_options_page')) add_options_page( 'Cat Icons', 'Category Icons', 5, basename(__FILE__), 'ig_caticons_adminpanel'); } /** * Handle admin panel * @access private */ function ig_caticons_adminpanel() { ?>
Select: Manage | Options
escape($_REQUEST['cat_ID']); $wpdb->query("DELETE FROM $wpdb->ig_caticons WHERE cat_id='$cat'"); break; case 'do_select': $cat = $wpdb->escape($_REQUEST['cat_ID']); $icon = $wpdb->escape($_REQUEST['ig_icon']); $small_icon = $wpdb->escape($_REQUEST['ig_small_icon']); if ($wpdb->get_var("SELECT cat_id FROM $wpdb->ig_caticons WHERE cat_id='$cat'")) { $wpdb->query("UPDATE $wpdb->ig_caticons SET icon='$icon', small_icon='$small_icon' WHERE cat_id='$cat'"); } else { $wpdb->query("INSERT INTO $wpdb->ig_caticons (cat_id, icon, small_icon) VALUES ('$cat', '$icon', '$small_icon')"); } break; } switch ($action) : // List categories case 'select': $cat = $wpdb->escape($_REQUEST['cat_ID']); $c = $wpdb->get_row("SELECT * FROM $wpdb->categories WHERE cat_ID=$cat"); list($icon, $small_icon) = ig_caticons_get_icons($c->cat_ID); ?>

Category ID: cat_ID;?>
Name: cat_name;?>
Icon: " />
Small Icon: " />

Settings updated.

Category Icons Settings

General Settings
Local Path to Icons:
Leave blank to use default upload path ().
URL to Icons:
Leave blank to use default upload url ().
Image File Types:
Separate by space or comma. E.g. "jpg gif jpeg png"
read())) { if (is_dir($path.DIRECTORY_SEPARATOR.$entry)) continue; $p = strrpos($entry,'.'); if (false===$p || !in_array(strtolower(substr($entry, $p+1)), $ext)) continue; $sel = ($selected==$entry) ? ' selected="selected"' : ''; $entry_escaped = htmlspecialchars($entry); echo(""); } $d->close(); } /** * Get category icons. * @param int $cat Category ID * @return array (icon, small_icon) */ function ig_caticons_get_icons($cat) { global $wpdb; $cat = $wpdb->escape($cat); if ($row = $wpdb->get_row("SELECT icon, small_icon FROM $wpdb->ig_caticons WHERE cat_id='$cat'")) { return array($row->icon, $row->small_icon); } else return false; } /** * Utility function to compute a rectangle to fit a given rectangle by maintaining the aspect ratio. */ function ig_fit_rect($width, $height, $max_width=-1, $max_height=-1, $expand=false) { $h = $height; $w = $width; if ($max_width>0 && ($w > $max_width || $expand)) { $w = $max_width; $h = floor(($w*$height)/$width); } if ($max_height>0 && $h >$max_height) { $h = $max_height; $w = floor(($h*$width)/$height); } return array($w,$h); } /** * Print category rows for admin panel */ function ig_caticons_rows($parent = 0, $level = 0, $categories = 0) { global $wpdb, $class, $user_level; if (!$categories) $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories ORDER BY cat_name"); if ($categories) { foreach ($categories as $category) { if ($category->category_parent == $parent) { list($icon, $small_icon) = ig_caticons_get_icons($category->cat_ID); $icon_cell = ''; if (''!=$icon) { list($width, $height, $type, $attr) = getimagesize(ig_caticons_path()."/$icon"); list($w, $h) = ig_fit_rect($width, $height, 100, 100); $icon_cell = '\"$small_icon\"
$icon ($width x $height)"; } $small_icon_cell = ''; if (''!=$small_icon) { list($width, $height, $type, $attr) = getimagesize(ig_caticons_path()."/$small_icon"); list($w, $h) = ig_fit_rect($width, $height, 100, 100); $small_icon_cell = '\"$small_icon\"
$small_icon ($width x $height)"; } $category->cat_name = wp_specialchars($category->cat_name); $count = $wpdb->get_var("SELECT COUNT(post_id) FROM $wpdb->post2cat WHERE category_id = $category->cat_ID"); $pad = str_repeat('— ', $level); if ( $user_level > 3 ) $edit = "" . __('Select') . "cat_name)) . "')\" class='delete'>" . __('Remove') . ""; else $edit = ''; $class = ('alternate' == $class) ? '' : 'alternate'; echo "$category->cat_ID$pad $category->cat_name $icon_cell $small_icon_cell $edit "; ig_caticons_rows($category->cat_ID, $level + 1, $categories); } } } else { return false; } } if (!function_exists('stripslaghes_gpc_arr')) { function stripslaghes_gpc_arr(&$arr) { if (get_magic_quotes_gpc()) { foreach(array_keys($arr) as $k) $arr[$k] = stripslashes($arr[$k]); } } } ?>