public
Created

The WordPress Category Icons plugin had problems with incorrect paths when used on a non standard WordPress setup, which I've fixed in this code http://wordpress.org/support/plugin/category-icons

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
<?php
/*
Plugin Name: Category Icons
Plugin URI: http://www.category-icons.com/
Description: Easily assign icons to your categories. (Minimum WP version : 2.8)
Version: 2.2.3
Author: Brahim Machkouri
Author URI: http://www.category-icons.com/
Text Domain: category_icons
Domain Path: /languages/
Notes: Edited by simon @ WP Sites to use correct methods for building file paths
*/
 
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
 
// Initialization and Hooks
global $table_prefix,$recent_posts_version;
$wpdb->ig_caticons = $table_prefix.'ig_caticons';
$caticons_datas = array();
 
add_action('init', 'ig_caticons_install');
add_action('admin_menu', 'ig_caticons_adminmenu');
add_action('admin_menu', 'bm_caticons_add_js_libs');
add_filter('pre_kses', 'bm_caticons_plugin_description');
add_action('rss2_head','bm_caticons_rss_flag');
add_action('rdf_header','bm_caticons_rss_flag'); // only for Safari
add_action('atom_ns','bm_caticons_rss_flag');
add_filter('the_excerpt_rss','bm_caticons_rss');
add_filter('the_content','bm_caticons_rss');
add_filter('the_content_rss','bm_caticons_rss');
add_filter('plugin_action_links', 'bm_caticons_plugin_action_links', 10, 2);
 
include_once('category_icons_widget.php');
 
/**
* Display "Settings" once the plugin is activated
* @author Brahim Machkouri
*/
function bm_caticons_plugin_action_links($links, $file) {
if ($file == plugin_dir_path(__FILE__).'/category_icons.php'){
$links[] = "<a href='edit.php?page=category_icons.php'>" . __('Settings') . "</a>";
}
return $links;
}
 
/**
* WordPress Template Tag to Insert Category Icon
* @author Brahim Machkouri
* @param boolean $align align attribute for the image tag
* @param int $fit_width Maximum width of the image, or desired width if expand is true. Default : -1
* @param int $fit_height Maximum height (or desired height if $expanded=true) of the image. Default : -1
* @param boolean $expand Whether the image should be expanded to fit the rectangle specified by fit_xxx. Default : false
* @param int $cat Category ID. If not specified, the current category is used or the current post's category. Default :
* @param boolean $small Use the small icon. Default : false
* @param string $prefix String to echo before the image tag. If no image, no output. Default :
* @param string $suffix String to echo after the image tag. Ignored if no image found. Default :
* @param string $class Class attribute for the image tag. Default :
* @param boolean $link If true, an anchor tag wraps the image (a hyperlink to the category is made). Default : true
* @param boolean $echo If true the html code will be 'echoed'. If no, it'll be returned. Default : true
* @param boolean $use_priority If true, only the most prioritized icon will be displayed. Default : false
* @param int $max_icons Maximum number of icons to display. Default : 3
* @param boolean $border If true, displays the icon with a border. If false, no border is diplayed. (Don't use this if you want valid Strict XHTML)
* @param boolean $hierarchical If true, displays the icon in hierarchical order.(horizontally)
* @return boolean True if image found.
*/
function get_cat_icon($params='') {
$one_category_only = false;
if (0 < (int) get_option('igcaticons_iconcatpage',0) ) {
$one_category_only = true;
}
// Compatibility with qTranslate
if (function_exists('qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage')) {
remove_filter('wp_list_categories','qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
remove_filter('list_cats','qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
}
// Compatibility with SEO Friendly Images
if (function_exists('seo_friendly_images') && 1 == get_option('igcaticons_useseo_plugin'))
add_filter('category_icons', 'seo_friendly_images', 50);
parse_str($params, $p);
if (!isset($p['fit_width'])) $p['fit_width'] = (int) get_option('igcaticons_max_width');
if (!isset($p['fit_height'])) $p['fit_height'] = (int) get_option('igcaticons_max_height');
if (!isset($p['expand'])) $p['expand'] = false;
if (!isset($p['small'])) $p['small'] = (int) get_option('igcaticons_use_small');
if (!isset($p['prefix'])) $p['prefix'] = '';
if (!isset($p['suffix'])) $p['suffix'] = '';
if (!isset($p['class'])) $p['class'] = '';
if (!isset($p['link'])) $p['link'] = true;
if (!isset($p['echo'])) $p['echo'] = true;
if (!isset($p['use_priority'])) $p['use_priority'] = false;
if (!isset($p['max_icons'])) $p['max_icons'] = (int) get_option('igcaticons_max_icons');
if (!isset($p['align'])) $p['align'] = false;
if (!isset($p['hierarchical'])) $p['hierarchical'] = false;
if (!$p['hierarchical'] === false) $p['hierarchical'] = bm_caticons_getbool($p['hierarchical']);
if (!isset($p['orderby'])) $p['orderby'] = 'name';
$p['expand'] = bm_caticons_getbool($p['expand']);
$p['small'] = bm_caticons_getbool($p['small']);
$p['echo'] = bm_caticons_getbool($p['echo']);
if (isset($p['border'])) $p['border'] = bm_caticons_getbool($p['border']);
$p['use_priority'] = bm_caticons_getbool($p['use_priority']);
if ($p['max_icons']==1) $p['use_priority'] = true;
$p['link'] = bm_caticons_getbool($p['link']);
stripslaghes_gpc_arr($p);
if (is_category() && in_the_loop() && $one_category_only == true ) {
$cat = (int) get_query_var('cat');
$category=get_category($cat);
if (!isset($p['cat']) || !is_array($p['cat'])) {
$p['cat']=array();
}
$p['cat'][] = $category->cat_ID;
}
else {
if (!isset($p['cat']) && isset($GLOBALS['post'])) {
$catlist = get_the_category($GLOBALS['post']->ID);
if (is_array($catlist)) {
$p['cat'] = array();
if ($p['orderby']!='name' || $p['hierarchical']==true) {
$p['cat'] = bm_caticons_process_categories('orderby='.$p['orderby'].'&hierarchical='.$p['hierarchical']);
} else {
foreach ($catlist as $categorie) {
$p['cat'][] = $categorie->cat_ID; // Adds all the categories in the array
$cat[$categorie->cat_ID] = $categorie->name;
}
}
}
}
if (!isset($p['cat'][0])) return;
if (!is_array($p['cat'])) {
$categorie= (int) $p['cat'];
$p['cat'] = array();
$p['cat'][] = $categorie;
}
}
$nb_icons = 0;
$urlbegin = '';
$urlend = '';
$cat_icons = '';
for ($i=0; $i<count($p['cat']); $i++) {
if($p['use_priority']) // if you decide to use the priority feature
list( $p['cat'][$i] , $priority , $icon , $small_icon ) = ow_caticons_get_priority_icon( $p['cat'] );
else
list( $priority , $icon , $small_icon ) = bm_caticons_get_icons( $p['cat'][$i]);
if ($p['small']) {// If we choose to display the small icon
$file = ig_caticons_path().'/'.$small_icon;
$url = ig_caticons_url().'/'.$small_icon;
if (!is_file($file)) {// if the small icon can't be found, the normal one will be loaded
$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) ) {// If the normal icon can't be found, the small one will be loaded
$file = ig_caticons_path().'/'.$small_icon;
$url = ig_caticons_url().'/'.$small_icon;
}
}
if ( is_file($file) ) {
if ( $p['link'] ) {
$urlbegin = '<a href="'.get_category_link($p['cat'][$i]).'" title="';
if (function_exists('qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage')) // Compatibility with qTranslate
$urlbegin .= qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage(get_cat_name($p['cat'][$i]));
else
$urlbegin .= get_cat_name($p['cat'][$i]);
$urlbegin .= '">';
$urlend = '</a>';
}
list( $width , $height , $type , $attr) = getimagesize($file);
$w = (int) $width;
$h = (int) $height;
if (!empty($p['fit_width']) || !empty($p['fit_height']))
list($w, $h) = ig_caticons_fit_rect($width, $height, $p['fit_width'], $p['fit_height'], $p['expand']);
$cat_icons .= $p['prefix'].$urlbegin.'<img ';
if (!empty($p['class'])) $cat_icons .= 'class="'.$p['class'].'" ';
if (isset($p['border'])) {
if ($p['border'])
$cat_icons .= 'border="1" ';
else
$cat_icons .= 'border="0" ';
}
if (!$p['align']===false) $cat_icons .= 'align="'.$p['align'].'" ';
$fill_with_catname = get_cat_name($p['cat'][$i]);
$title = 'title="'.$fill_with_catname.'"';
$alt = 'alt="'.$fill_with_catname.'"';;//$title;
if (function_exists('seo_friendly_images') && 1 == get_option('igcaticons_useseo_plugin')) $title = '' ; // Compatibility with qTranslate
$cat_icons .= 'src="'.$url.'" width="'.$w.'" height="'.$h.'" '.$alt.' '.$title.' />'.$urlend.$p['suffix'];
$nb_icons++;
}
if ( $p['use_priority'] ) break;
if ( $nb_icons == $p['max_icons']) break;
}
if (function_exists('qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage')) {// Add the filter again, as we finished to process the icon(s)
add_filter('wp_list_categories','qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
add_filter('list_cats','qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
}
$cat_icons = apply_filters('category_icons', $cat_icons);
if (function_exists('qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage') ) // Update the compatibility with qTranslate
$cat_icons = qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($cat_icons);
if ($p['echo']) {
if (empty($cat_icons))
return false;
else {
echo $cat_icons;
return true; // if echo get_cat_icon() is used, true will be displayed as "1". So the parameter echo=false must be used in this case.
}
}
else
return $cat_icons;
}
 
/**
* Order the categories
* @author Brahim Machkouri
* @param string $string parameters
*/
function bm_caticons_process_categories($string='') {
$caticons_processed = array();
$catlist = get_the_category($GLOBALS['post']->ID);
if (count($catlist)>1) {
parse_str($string);
$depth = -1; // Flat
$r = array('orderby' => 'name', 'order' => 'ASC', 'hierarchical' => false, 'echo' => 0, 'hide_empty' => 0);
if ($hierarchical==1) {
$r['hierarchical'] = true;
$depth = 0 ;
}
if (in_array(strtolower($orderby), array('id','slug','count','order','term_order')))
$r['orderby'] = $orderby;
$a = 'array('.bm_caticons_walk_tree( get_categories( $r ), $depth, $r ).');';
eval("\$caticons_processed = $a;");
foreach ($catlist as $object)
$categories[] = $object->cat_ID;
$caticons_processed = array_intersect($caticons_processed,$categories);
} else {
$caticons_processed = array($catlist[0]->cat_ID);
}
return array_values($caticons_processed);
}
 
/**
* Get the categories ordered the way we want
* @author Brahim Machkouri
*/
function bm_caticons_walk_tree() { // Greatly inspired from WordPress Source Code
include_once('category_icons_walker.php');
$args = func_get_args();
// the user's options are the third parameter
if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
$walker = new Walker_Caticons;
else
$walker = $args[2]['walker'];
$string = call_user_func_array(array( &$walker, 'walk' ), $args );
return substr($string, 0, strlen($string)-1);
}
 
/**
* WordPress Template Tag to Inject the html image source code of each category contained in the list given as parameter
* @author Brahim Machkouri
* @param string $list HTML code of the category list
* @param string $parameters The parameters of get_cat_icon() function
*/
function put_cat_icons($list,$parameters='',$type='category') {
global $wpdb,$post;
$echo_output = true;
$original_title = $post->post_title;
$request = "SELECT term_id
FROM $wpdb->term_relationships
right join $wpdb->term_taxonomy on $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id
left join $wpdb->posts on $wpdb->term_relationships.object_id = $wpdb->posts.ID
where post_type = 'page' and $wpdb->term_taxonomy.taxonomy = 'category' and ID=";
$nb_max_icons_page = (int) get_option('igcaticons_max_icons');
$cats = array();
if ($type != 'cat' && $type != 'page') {
$type = 'cat';
if (version_compare($GLOBALS['wp_version'], '3.1', '>=') ) {
$type = 'category_name';
}
}
if (empty($parameters)) // If no parameter is given, the ones from the options will be taken
$parameters = 'fit_width='.get_option('igcaticons_max_width').'&fit_height='.get_option('igcaticons_max_height').'&small='.(bm_caticons_getbool(get_option('igcaticons_use_small')) ?'true':'false');
else {
parse_str($parameters, $p);
if (isset($p['echo']) && bm_caticons_getbool($p['echo'])===false) {
$echo_output = false;
}
if ( !isset($p['icons_only']) ) {
$p['icons_only']=0;
}
if ( isset($p['icons_only']) && 'true' == strtolower( $p['icons_only'] ) ) {
$p['icons_only']=1;
}
}
if (!empty($list)) {
$myarray = bm_caticons_url_extractor($list);
foreach ($myarray as $child) {
$cats = array();
$pos = strpos($child[0],$type.'=');
if ($pos>0) {
parse_str(substr($child[0],$pos, strlen($child[0])-$pos)); // $cat or $category_name is created from this function
if (isset($page) && intval($page) >0) { // get the category from page id
$cats = array();
$cats = $wpdb->get_results($wpdb->prepare($request.intval($page)),ARRAY_A);
}
else {
if (version_compare($GLOBALS['wp_version'], '3.1', '>=')) {
// standard permalinks in wp 3.1+: http://www.sublab.dev/wpdev/?category_name=apple
$cats[] = bm_caticons_get_cat_ID_by_slug($category_name);
}
else { // standard permalinks in wp up to 3.0: http://www.sublab.dev/wpdev/?cat=3
$cats[] = (int) $cat;
}
}
}
else {// not standard permalinks
$cats = array();
$temp = explode('/',($child[0]));
$name = '';
do
$name = array_pop($temp); // get the last non empty/null string from the array
while (is_null($name) || empty($name));
if ($type == 'cat' OR $type == 'category_name') {
$cat = bm_caticons_get_cat_ID_by_slug(trim($name));
$cats[] = $cat;
}
else {
$cats = array();
$page = $wpdb->get_var($wpdb->prepare("SELECT ID FROM `$wpdb->posts` WHERE post_name = '$name'"),0,0);
$cats = $wpdb->get_results($wpdb->prepare($request.intval($page)),ARRAY_A);
}
}
if (is_array($cats[0])) { // Pages Icons
$temp = array();
for ($i=0; $i<count($cats);$i++) {
$temp[$i]= $cats[$i]['term_id'];
}
$cats = $temp;
unset($temp);
}
$img = '';
$i=1;
if (count($cats)>0) {
foreach ($cats as $categ) {
if ($categ == 1 && $type == 'page') continue;// Do not display uncategorized icon for a page
if ($i > $nb_max_icons_page) break;
$post->post_title = get_cat_name($categ);
$img .= get_cat_icon($parameters.'&cat='.$categ.'&echo=false&link=false');
$i++;
}
if ( isset($p) && 1 == $p['icons_only'] && !empty($img) ) {
$list = str_replace($child[1],'>'.$img.'<', $list);
}
else {// Inject icon into the list
$before_name = bm_caticons_getbool(get_option('igcaticons_before_name'));
if ($before_name) { // put the html code of the icons before the category name
$cat_name = substr($child[1],1,strlen($child[1]));
$list = str_replace('>'.$cat_name, '>'.$img.$cat_name,$list);
} else {// else after the category name
$new = substr($child[1],0,strlen($child[1])-1);
$list = str_replace($child[1], $new.$img.'<', $list);
}
}
}
}
$list = apply_filters('category_icons_widget', $list);
if ($echo_output) echo $list;
}
else {
_e("put_cat_icons : your list is empty ! Don't forget to add <strong>echo=0</strong> in the parameters of wp_list_categories().",'category_icons');
}
$post->post_title = $original_title;
if (!$echo_output && !empty($list))
return $list;
}
 
/**
* Get the slug category
* @author Brahim Machkouri
* @param string $slug slug from which you want to get the category
* @return int
*/
function bm_caticons_get_cat_ID_by_slug($slug) {
$idObj = get_category_by_slug($slug);
$id = false;
if (is_object($idObj))
$id = $idObj->term_id;
else { // if no category id is found, this is ?cat=X given
list($dummy,$id) = explode('?cat=',$slug);
$id = (int) $id;
}
return $id;
}
 
/**
* If a RSS feed is created, 'raise the flag'
* @author Brahim Machkouri
*/
function bm_caticons_rss_flag() {
global $bm_caticons_rss;
$bm_caticons_rss = true;
}
 
/**
* Inject the icons into the feeds
* Working only with RSS2 and Atom. (And RDF, but only in Safari)
* @author Brahim Machkouri
* @param string The content to process
* @return string The feed content
*/
function bm_caticons_rss($content) {
global $bm_caticons_rss;
if ( $bm_caticons_rss && 1 == get_option('igcaticons_rssfeeds') ) // If the rss flag is raised, inject icons
$content = get_cat_icon('echo=false&link=false').'<br/>'.$content;
return $content;
}
 
/**
* Localization of the plugin description
* @author Brahim Machkouri
* @param string The string to process
* @return string The string to display
*/
function bm_caticons_plugin_description( $string) {
if (trim($string) == 'Easily assign icons to your categories.') {
$string = __('Easily assign icons to your categories.','category_icons');
}
return $string;
}
 
/**
* Displays informations in the footer of WordPress
* @author Brahim Machkouri
*/
function bm_caticons_credits() {
$translator_name = (__('translator_name','category_icons')) == 'translator_name' ? '' : __('translator_name','category_icons');
$translator_url = (__('translator_url','category_icons')) == 'translator_url' ? '' : __('translator_url','category_icons') ;
$lang = get_locale();
$plugins = get_plugins();
$current_version = trim($plugins['category-icons/category_icons.php']['Version']);
$msg = 'Category Icons '.$current_version.'&nbsp;&nbsp;<img style="border-style:none;vertical-align:middle;padding:0px;padding-bottom:1px;margin:0px;" src="'. plugins_url( "images/w3c-xhtml1.0.png" , __FILE__ ) .'" alt="w3c-xhtml" /> | ';
$msg .= '<a target="_blank" href="http://www.category-icons.com/about/">'.__('About','category_icons').'</a>';
$msg .= ' | <a target="_blank" href="http://www.category-icons.com/category/howto/">'.__('How To','category_icons').'</a>';
$msg .= ' | <a target="_blank" href="http://www.category-icons.com/category/documentation/">'.__('Documentation','category_icons').'</a>';
$msg .= ' | <a target="_blank" href="http://www.category-icons.com/category/faq/">'.__('FAQ','category_icons').'</a>';
$msg .= ' | <a target="_blank" href="http://www.category-icons.com/translate-the-plugin/">'.__('Translate the plugin','category_icons').'</a>';
$msg .= ' | <a target="_blank" href="http://www.category-icons.com/troubleshooting/">'.__('Troubleshooting','category_icons').'</a>';
$msg .= ' | <a target="_blank" href="http://www.category-icons.com/contact/">'.__('Contact the author','category_icons').'</a>' ;
if (!empty($lang) && !empty($translator_name)) {
$msg .= (' | ').__('Translated by','category_icons');
if (!empty($translator_url))
$msg .= ' <a target="_blank" href="'.$translator_url.'">'.$translator_name.'</a>';
else
$msg .= ' '.$translator_name;
}
echo $msg.' <br/>';
}
 
/**
* Loads the scripts needed by the plugin
* @author Brahim Machkouri
*/
function bm_caticons_add_js_libs() {
wp_enqueue_script('jquery');
wp_enqueue_style ( 'caticons-css', WP_PLUGIN_URL.'/category-icons/category_icons.css', array(), '1.0', 'all');
wp_enqueue_script('admin-forms');//WP 2.5+ only
wp_enqueue_script('caticons-tablesorter', WP_PLUGIN_URL.'/category-icons/js/jquery.tablesorter.min.js');
wp_enqueue_script('caticons-metadatas', WP_PLUGIN_URL.'/category-icons/js/jquery.metadata.min.js');
wp_enqueue_script('caticons-js', WP_PLUGIN_URL.'/category-icons/js/category_icons.js');
wp_localize_script( 'caticons-js', 'CatIconsSettings',
array('plugin_url' => get_option('siteurl').WP_PLUGIN_URL.'/category-icons',
'error' => __('Can\'t contact www.category-icons.com. Retry later, please.','category_icons'))
);
}
 
/**
* Return the boolean corresponding to $var
* @author Brahim Machkouri
* @param boolean|int|string $var boolean or string or int to eval
* @return boolean
*/
function bm_caticons_getbool($var) {// i made this function because casting get_option() didn't react as I wanted
if (is_bool($var)) return $var;
switch (strtolower($var)) {
case ('true'):
return true;
case ('false'):
default:
return false;
}
}
 
/**
* Extract the urls & labels
* @author Brahim Machkouri
* @param string $string String containing the urls to extract
* @return array
*/
function bm_caticons_url_extractor($string) {
$myarray = array();
if (preg_match_all('/<a\s+.*?href=[\"\']?([^\"\' >]*)[\"\']?[^>]*(>.*?<)\/a>/i', $string, $correspondances, PREG_SET_ORDER))
foreach ($correspondances as $correspondance)
array_push($myarray, array($correspondance[1], $correspondance[2]));
return $myarray;
}
 
/**
* Find where to place the code (get_cat_icons()) in the template files. This function'll work for numerous templates.
* For the majority of the templates I've seen, the post title is after an anchor tag or after a h2 tag.
* So the function search for : <a href...>the_title()</a> and <h2...>the_title()</h2>.
* @author Brahim Machkouri
* @param string $filename Name of the template folder
* @return array
*/
function bm_caticons_template_code($filename='') { //needs to be optimized or rewritten
$files = array();
$results = array();
$position = 0;
$correspondances = array();
$filename='';
$notfound = 0;
//$path = ABSPATH.WP_CONTENT_DIR.'/themes/'.get_option('template');
$path = WP_CONTENT_DIR.'/themes/'.get_option('template');
$getcat_code = "\n<?php if (function_exists('get_cat_icon')) get_cat_icon(); ?>";
if ($handle = opendir($path)) {
while (false !== ($filename = readdir($handle)))
if ( '.php' == trim(substr($filename, strrpos($filename, '.'), strlen($filename)-strrpos($filename, '.'))) )
$files[] = trim($filename);
closedir($handle);
}
 
foreach ($files as $filename) {// Scan all the php files found for the linked (a href) the_title() function
$fullfilename = $path.'/'.$filename;
$string = file_get_contents($fullfilename);
$found = 0;
if ( (false === strpos(strtolower($string),'put_cat_icons(')) ) {
if (preg_match_all('/(wp_list_cat?[^\)].*\)[;\s])/i', $string, $correspondances, PREG_SET_ORDER)) {
$result = array();
foreach ($correspondances as $correspondance) {
if ( isset($correspondance[1]) )
$result[] = $correspondance[1];
}
$correspondances = array_unique($result);
foreach ($correspondances as $correspondance) {
if (strpos($correspondance,'(')>0) {// Extract the wp_list_categories() parameters
$parameters = '';
if (preg_match_all('/[\(](.*[^\)]\))/i', $correspondance, $trouves, PREG_SET_ORDER)) {
$parameters = substr($trouves[0][1],0,strlen($trouves[0][0])-2);
if ( false === !strpos($parameters,'(') )
$parameters = $trouves[0][1];
}
// Put the code
$putcat_code = "\nif (function_exists('put_cat_icons')) \n\tput_cat_icons( wp_list_categories(".$parameters;
if (!empty($parameters))
$putcat_code .=".'&";
else
$putcat_code .= '\'';
$putcat_code .= "echo=0'));\nelse\n\twp_list_categories(".$parameters.')';
if (strrpos($correspondance,';')>0)
$putcat_code .= ';';
if ( 1 == get_option('igcaticons_templatecode_patch') && is_writable($path.'/'.$filename)) {// patch the files
$handle = fopen($path.'/'.$filename, "wb");
$string = str_replace($correspondance,$putcat_code,$string);
if (fwrite($handle, $string)) $results = array($filename,'-','<i>Ok</i>');
fclose($handle);
}
else {// display the informations about the line and column
$position = 0;
do {
$position = strpos($string,$correspondance,$position);
if ( !( false === $position ) ) {
$line_number = substr_count( substr($string, 0,$position) ,"\n")+1;
$column = $position - strrpos (substr($string, 0,$position),"\n") - 1;
$results[] = array($putcat_code,$filename,$line_number.':'.$column,htmlentities($correspondance));
$position += strlen($correspondance);
}
}
while ( !( false === $position ));
}
}
}
}
}
if ( false === strpos(strtolower($string),'get_cat_icon(') ) {
if (preg_match_all('/(<a\s+.*?href=[\"\']?[^\"\' >]*[\"\']?[^>].*>+[\s\t\r\n]*<\?php +[\/\s\t\r\n$]*?[^>]+[\s\t\r\n]*.*\?>+[\s\t\r\n]*<\/a>)/i', $string, $correspondances, PREG_SET_ORDER)) {
$correspondances = array_unique($correspondances);
foreach ($correspondances as $correspondance) {
if (strpos(strtolower($correspondance[1]),'the_title()')>0) {
$found = 1;
if (get_option('igcaticons_templatecode_patch')==1 && is_writable($path.'/'.$filename)) {// patch the files
$handle = fopen($path.'/'.$filename, "wb");
$string = str_replace($correspondance[1],$getcat_code.$correspondance[1],$string);
if (fwrite($handle, $string))
$results = array($getcat_code,$filename,'-','<i>Ok</i>');
fclose($handle);
}
else {// display the informations about the line and column
do {
$position = strpos($string,$correspondance[1],$position);
if ( !( false === $position ) ) {
$line_number = substr_count( substr($string, 0,$position) ,"\n")+1;
$column = $position - strrpos (substr($string, 0,$position),"\n") - 1;
$results[] = array($getcat_code,$filename,$line_number.':'.$column,htmlentities($correspondance[1]));
$position += strlen($correspondance[1]);
}
}
while (!( false === $position ));
}
}
}
}
if ( 0 == $found && preg_match_all('#<h2[^>]*>+[\s\t\r\n]*(<\?php +[\/\s\t\r\n$]*?[^>]+[\s\t\r\n]*.*\?>+[\s\t\r\n]*</h2>)#Ui', $string, $correspondances, PREG_SET_ORDER)) {
$correspondances = array_unique($correspondances);
foreach ($correspondances as $correspondance) {
if (strpos(strtolower($correspondance[1]),'the_title()')>0) {
if ( 1 == get_option('igcaticons_templatecode_patch') && is_writable($path.'/'.$filename)) {// patch the files
$handle = fopen($path.'/'.$filename, "wb");
$string = str_replace($correspondance[1],$getcat_code.$correspondance[1],$string);
if (fwrite($handle, $string)) $results = array($getcat_code,$filename,'-','<i>Ok</i>');
fclose($handle);
}
else {// display the informations about the line and column
do {
$position = strpos($string,$correspondance[1],$position);
if ( !( false === $position ) ) {
$line_number = substr_count( substr($string, 0,$position) ,"\n")+1;
$column = $position - strrpos (substr($string, 0,$position),"\n") - 1;
$results[] = array($getcat_code,$filename,$line_number.':'.$column,htmlentities($correspondance[1]));
$position += strlen($correspondance[1]);
}
}
while (!( false === $position ));
}
}
}
}
}
}
return $results;
}
 
/**
* Display code template table panel.
* @author Brahim Machkouri
* @param string $message String to display before the table
* @param string $code String representing the code to paste
* @param array $list Every row to display
* @param string $filter Which type of table to display
*/
function bm_caticons_codetemplate_display($message,$list,$filter) {
$table = array();
foreach ($list as $element) {
if (strpos($element[0],$filter)) $table[] = $element;
}
$list = $table;
if ( 0 < count($list) ) {
?>
<?php echo '<p>'.$message.'</p>'; ?>
<?php if ( 'get_cat_icon' == $filter ) echo '<pre>'.htmlentities($list[0][0]).'</pre>'; ?>
<table class="widefat">
<thead>
<tr>
<th ><?php _e('Filename','category_icons'); ?></th>
<th style="text-align:center"><?php _e('Line Number : Column','category_icons'); ?></th>
<th style="text-align:center"><?php _e('Code','category_icons'); ?></th>
</tr>
</thead>
<tbody>
<?php
$class = '';
if ( 0 == count($list) )
$list = array(array('-','-','-'));
foreach ($list as $element) {
$class = ('alternate' == $class) ? '' : 'alternate';
?>
<tr class="<?php echo $class; ?>">
<th><?php echo $element[1]; ?></th>
<td align="center"><?php echo $element[2]; ?></td>
<td align="center"><?php
if ( 'get_cat_icon' == $filter )
echo '<img src="'.plugins_url( "images/arrowdown.png" , __FILE__ ).' alt="arrow" />';
echo $element[3];
if ( 'put_cat_icon' == $filter )
echo '<br/><b>'.__('by','category_icons').'</b><br/>'.htmlentities($element[0]).'<br/>';
?></td>
</tr>
<?php
}
?></tbody>
</table>
<?php
}
}
 
/**
* Check if the default upload path is readable.
* @author Brahim Machkouri
*/
function bm_caticons_check_uploads_path() {
if (!wp_mkdir_p(ig_caticons_path())) {
$options_file = "options-misc.php";
if (version_compare($GLOBALS['wp_version'], '3.0','>=')) {
$options_file ="options-media.php";
}
$message = __('the default upload path is not accessible.','category_icons');
if (!is_multisite()) {
$message = "<a href='$options_file'>".$message.'</a>';
}
else {
$message .= ' ( '.ig_caticons_path().' ) <br/>';
}
if (!is_readable(ig_caticons_path())) {
?>
<div id="message" class="updated fade">
<p><strong>
<?php
echo __('Error in Category Icons','category_icons').' : '.$message.__('Please create one or change the permissions on the directory.','category_icons');
?>
</strong></p>
</div>
<?php
}
}
}
 
/**
* Echoes the option tag filled with icons paths
* @author Brahim Machkouri
* @param string $selected the icon to select in the dropdown menu
*/
function bm_caticons_get_icons_paths($selected='') {
$files = array();
$basepath = ig_caticons_path();
$files = bm_caticons_recursive_readdir($basepath);
natcasesort($files);
foreach ($files as $fullpath => $filename) {
$sel = ($selected==$fullpath) ? ' selected="selected"' : '';
echo('<option value="'.htmlspecialchars($fullpath).'"'.$sel.'>'. htmlspecialchars($filename).' ('.str_replace($filename,'',$fullpath).')</option>');
}
}
 
/**
* Gets the icons paths
* @author Brahim Machkouri
* @param string $folder Name of the folder to browse
* @param array $files Filenames already found
* @return array
*/
function bm_caticons_recursive_readdir($folder,$files=array()) {
$separator = '/';
$types = explode(' ',get_option('igcaticons_filetypes'));
if (is_readable($folder) && $handle = opendir($folder) ) {
while (false !== ($filename = readdir($handle))) {
if ($filename !== '.' && $filename !== '..') {
$path = $folder.$separator.$filename;
if (is_dir ($path))
$files = array_unique(array_merge($files, bm_caticons_recursive_readdir($path,$files))); // recursive call
else {// files
$p = strrpos($filename,'.');
if (false===$p || !in_array(strtolower(substr($filename, $p+1)), $types)) continue;
else
$files[str_replace(ig_caticons_path().$separator,'', trim($path))] = $filename;
}
}
}
closedir($handle);
}
return $files;
}
 
/**
* Print category rows for admin panel
* @author Brahim Machkouri
* @param int $parent Category ID of the parent
* @param int $level Level of the category (parent or child)
* @param array $category Array of categories (Objects)
* @return false False if no category is found
*/
function bm_caticons_rows( $parent = 0, $level = 0, $categories = 0 ) { // I took the code from template.php of WordPress 2.5 and modified it a little
if ( !$categories ) {
$args = array('hide_empty' => 0);
if ( !empty($_GET['s']) )
$args['search'] = $_GET['s'];
$categories = get_categories( $args );
}
$children = _get_term_hierarchy('category');
if ( $categories ) {
ob_start();
foreach ( $categories as $category ) {
if ( $category->parent == $parent) {
echo "\t" . _bm_caticons_row( $category, $level );
if ( isset($children[$category->term_id]) )
bm_caticons_rows( $category->term_id, $level +1, $categories );
}
}
$output = ob_get_contents();
ob_end_clean();
$output = apply_filters('bm_caticons_rows', $output);
echo $output;
}
else
return false;
}
 
/**
* Display the rows of the icons panel in Icons tab
* @author Brahim Machkouri
* @return string
*/
function _bm_caticons_row( $category, $level) { // I took the code from template.php of WordPress 2.5 and modified it a little
global $class;
$icons_path = ig_caticons_path();
$category = get_category( $category );
$warning_flag = 0;
$pad = str_repeat( '&#8212; ', $level );
$name = $pad . ' ' . $category->name ;
list($priority, $icon, $small_icon) = bm_caticons_get_icons($category->term_id);
$message_icon = esc_attr(sprintf(__('If you can\'t see the icons, the URL to icons in the Options panel should be set to %s','category_icons'), ig_caticons_url().get_option('igcaticons_path')));
// Normal icon
$icon_cell = '';
if ( !empty($icon) && is_readable( trailingslashit($icons_path).$icon ) ) {
list($width, $height, $type, $attr) = getimagesize(trailingslashit($icons_path).$icon);
list($w, $h) = ig_caticons_fit_rect($width, $height, 100, 100);
$icon_cell = '<img src="'.trailingslashit(ig_caticons_url())."$icon\" width=\"$w\" height=\"$h\" alt=\"icon\" onerror='alert(\"$message_icon\");' /> <br />$icon ($width x $height)";
$icon_cell = '<a href="'.trailingslashit(ig_caticons_url()).$icon.'">'.$icon_cell.'</a>';
}
if ( !empty($icon) && !is_readable( trailingslashit($icons_path).$icon ) ) {
$icon_cell = '<img src="'.$warning.'">'.trailingslashit($icons_path).$icon._e('is not accessible','category_icons');
$warning_flag = 1;
}
// Small icon
$small_icon_cell = '';
if ( !empty($small_icon) && is_readable( trailingslashit($icons_path).$small_icon ) ) {
list($width, $height, $type, $attr) = getimagesize(trailingslashit($icons_path).$small_icon);
list($w, $h) = ig_caticons_fit_rect($width, $height, 100, 100);
$small_icon_cell = '<img src="'.trailingslashit(ig_caticons_url())."$small_icon\" width=\"$w\" height=\"$h\" alt=\"small icon\" onerror='alert(\"$message_icon\");'/> <br />$small_icon ($width x $height)";
$small_icon_cell = '<a href="'.trailingslashit(ig_caticons_url()).$small_icon.'">'.$small_icon_cell.'</a>';
}
if ( !empty($small_icon) && !is_readable( trailingslashit($icons_path).$small_icon ) ) {
$small_icon_cell = '<img src="'.$warning.'">'.trailingslashit($icons_path).$small_icon._e('is not accessible','category_icons');
$warning_flag = 1;
}
$edit = $name;
if (is_admin() || current_user_can( 'manage_categories' ))
$edit = "<a class='row-title' href='".CI_ADMIN_SELF."&amp;ig_module=caticons&amp;ig_tab=icons&amp;action=edit&amp;cat_ID=$category->term_id' title='"
. esc_attr(sprintf(__('Edit %s','category_icons'), $category->name)) . "'>" .$name . "</a>";
$class = " class='alternate'" == $class ? '' : " class='alternate'";
if ($warning_flag > 0) $class = 'style = "background-color:#FFB06C"';
$category->count = number_format_i18n( $category->count );
$posts_count = $category->count;
// Prepare the output string
$output = '<tr id="cat-'.$category->term_id.'" '.$class.' >'.
'<th scope="row" class="check-column" style="text-align:center;vertical-align:middle;padding:7px 0 8px">';
if (is_admin() || current_user_can( 'manage_categories' ))
$output .= '<input type="checkbox" name="delete[]" value="'.$category->term_id.'" /></th>';
else
$output .= "&nbsp;";
$output .= '<td style="vertical-align:middle;">'.$category->term_id.'</td>'.
'<td style="vertical-align:middle;">'.$edit.'</td>'.
'<td style="vertical-align:middle;">'.$category->description.'</td>'.
'<td class="num" style="vertical-align:middle;text-align:center" align="center" >'.$posts_count.'</td>'.
'<td class="num" style="vertical-align:middle;text-align:center" align="center">'.$priority.'</td>'.
'<td style="vertical-align:middle;text-align:center" align="center">'.$icon_cell.'</td>'.
'<td style="vertical-align:middle;text-align:center" align="center">'.$small_icon_cell.'</td>'.
"\n\t</tr>\n";
return apply_filters('bm_caticons_row', $output);
}
 
/**
* Display the icons panel in Icons tab
* @author Brahim MACHKOURI
*/
function bm_caticons_adminicons() { // I took some of the code from categories.php of WordPress 2.5 and modified it a little
global $wpdb;
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
if ( isset($_GET['deleteit']) && isset($_GET['delete']) )
$action = 'bulk-delete';
switch($action) {
case 'update-category-icon':
$cat_ID = (int) $_GET['cat_ID'];
$priority = $_REQUEST['ig_priority'];
$icon = $_REQUEST['ig_icon'];
$small_icon = $_REQUEST['ig_small_icon'];
if ($wpdb->get_var($wpdb->prepare("SELECT cat_id FROM $wpdb->ig_caticons WHERE cat_id='$cat_ID'"))) {
$wpdb->query($wpdb->prepare("UPDATE $wpdb->ig_caticons SET priority='$priority', icon='$icon', small_icon='$small_icon' WHERE cat_id='$cat_ID'"));
} else {
$wpdb->query($wpdb->prepare("INSERT INTO $wpdb->ig_caticons (cat_id, priority, icon, small_icon) VALUES ('$cat_ID', '$priority', '$icon', '$small_icon')"));
}
break;
case 'delete':
$cat_ID = (int) $_GET['cat_ID'];
if (!is_admin() || !current_user_can( 'manage_categories' ))
wp_die(__('Are you trying to cheat ?','category_icons'));
$cat_name = get_catname($cat_ID);
$request = "DELETE FROM $wpdb->ig_caticons WHERE cat_id='$cat_ID'";
if (false === $wpdb->query($wpdb->prepare($request) ))
wp_die(__('Error in Category Icons','category_icons').' : '.$request);
break;
case 'bulk-delete':
if (!is_admin() || !current_user_can( 'manage_categories' ))
wp_die( __('You are not allowed to delete category icons.','category_icons') );
foreach ( (array) $_GET['delete'] as $cat_ID ) {
$cat_name = get_catname($cat_ID);
$wpdb->query($wpdb->prepare("DELETE FROM $wpdb->ig_caticons WHERE cat_id='$cat_ID'"));
}
break;
}
switch ($action) {
case 'edit':
$cat_ID = (int) $_GET['cat_ID'];
$category = get_category_to_edit($cat_ID);
list($priority, $icon, $small_icon) = bm_caticons_get_icons($cat_ID);
?>
<div class="wrap">
<h2><?php _e('Select Category Icons','category_icons') ?></h2>
<form method="post" name="caticons-form1" action="">
<?php wp_nonce_field('caticons-nonce'); ?>
<input type="hidden" name="ig_module" value="caticons" />
<input type="hidden" name="ig_tab" value="icons" />
<input type="hidden" name="action" value="update-category-icon" />
<table border="0" class="form-table">
<tr>
<th scope="row" style="vertical-align:text-top;"><?php _e('Category ID','category_icons'); ?></th>
<td colspan="2" ><?php echo $cat_ID;?></td>
</tr>
<tr>
<th scope="row" style="vertical-align:text-top;"><?php _e('Name','category_icons'); ?></th>
<td colspan="2"><?php echo $category->name;?></td>
</tr>
<tr>
<th scope="row" class="num" style="vertical-align:text-top;"><?php _e('Priority','category_icons'); ?></th>
<td colspan="2">
<input type="text" name="ig_priority" size="5" value="<?php echo $priority; ?>" />
</td>
</tr>
<tr>
<th scope="row" style="vertical-align:text-top;"><?php _e('Icon','category_icons'); ?></th>
<td valign="top"><label><?php _e('Select a file : ','category_icons');?></label>
<select name="ig_icon" onchange="icon_preview.src=('<?php echo ig_caticons_url();?>/'+this.options[this.selectedIndex].value);">
<option value="">--- <?php _e('No Icon','category_icons'); ?> ---</option>
<?php bm_caticons_get_icons_paths($icon); ?>
</select><br/>
<label><?php _e('Or enter an URL : ','category_icons');?><input type="text" name="bm_icon_url"/></label>
</td>
<td valign="top"><img id="icon_preview" src="<?php echo ig_caticons_url()."/$icon";?>" alt="icon" /></td>
</tr>
<tr>
<th scope="row" style="vertical-align:text-top;"><?php _e('Small Icon','category_icons'); ?></th>
<td valign="top"><label><?php _e('Select a file : ','category_icons');?></label>
<select name="ig_small_icon" onchange="small_icon_preview.src=('<?php echo ig_caticons_url();?>/'+this.options[this.selectedIndex].value);">
<option value="">--- <?php _e('No Icon','category_icons'); ?> ---</option>
<?php bm_caticons_get_icons_paths($small_icon); ?>
</select><br/>
<label><?php _e('Or enter an URL : ','category_icons');?><input type="text" name="bm_smallicon_url"/></label>
</td>
<td valign="top"><img id="small_icon_preview" src="<?php echo ig_caticons_url()."/$small_icon";?>" alt="small icon" /></td>
</tr>
</table>
<div class="submit"><input type="submit" name="info_update" value="<?php _e('Select Icons','category_icons');?> &raquo;" /></div>
</form>
</div>
<?php
break;
default:
?>
<div class="wrap">
<form id="posts-filter" action="" method="get" name="caticons-form2">
<?php wp_nonce_field('caticons-nonce'); ?>
<input type="hidden" name="ig_module" value="caticons" />
<input type="hidden" name="page" value="category_icons.php" />
<input type="hidden" name="action" value="delete" />
<input type="hidden" name="ig_tab" value="icons" />
<div class="tablenav">
<div class="alignleft">
<input type="submit" value="<?php _e('Delete icons and priority','category_icons'); ?>" name="deleteit" class="button-secondary delete" />
</div>
<br class="clear" />
</div>
<br class="clear" />
<table class="widefat" id="caticons_table">
<thead>
<tr>
<th scope="col" id="cb" class="check-column"><input type="checkbox" /></th>
<th scope="col"><?php _e('ID','category_icons') ?></th>
<th scope="col" ><?php _e('Name','category_icons') ?></th>
<th scope="col" style="text-align:center"><?php _e('Description','category_icons') ?></th>
<th scope="col" class="num" style="text-align:center"><?php _e('Posts','category_icons') ?></th>
<th scope="col" style="text-align:center"><?php _e('Priority','category_icons') ?></th>
<th scope="col" style="text-align:center"><?php _e('Icon','category_icons') ?></th>
<th scope="col" style="text-align:center"><?php _e('Small Icon','category_icons') ?></th>
</tr>
</thead>
<tbody id="the-list" class="list:cat">
<?php
bm_caticons_rows();
?>
</tbody>
</table>
</form>
<div class="tablenav">
<br class="clear" />
</div>
<br class="clear" />
</div>
<?php
}// end switch
}
 
/**
* Install the plugin
* @author Ivan Georgiev
*/
function ig_caticons_install() {
global $wpdb, $table_prefix;
$wpdb->query($wpdb->prepare("CREATE TABLE IF NOT EXISTS `$wpdb->ig_caticons` (`cat_id` INT NOT NULL ,`priority` 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');
add_option('igcaticons_max_icons','3');
add_option('igcaticons_before_name','true');
add_option('igcaticons_fit_width','-1');
add_option('igcaticons_fit_height','-1');
add_option('igcaticons_use_small','true');
add_option('igcaticons_templatecode_patch','0');
add_option('igcaticons_templatecode_sidebar','1');
add_option('igcaticons_rssfeeds','1');
add_option('igcaticons_useseo_plugin', '0');
add_option('igcaticons_max_width','-1');
add_option('igcaticons_max_height','-1');
add_option('igcaticons_iconcatpage','0');
}
 
/**
* Get the upload & siteurl paths
* @author Ivan Georgiev
* @return array (path, url)
*/
function ig_caticons_defupload() {
$upload_dir = wp_upload_dir();
$def_path = $upload_dir['basedir']; // wordpress's option
$def_url = $upload_dir['baseurl']; // idem
return array($def_path, $def_url);
}
 
 
/**
* Integrate into the admin menu.
* @author Ivan Georgiev, Brahim Machkouri
*/
function ig_caticons_adminmenu() {
load_plugin_textdomain('category_icons', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
$page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 'category_icons.php';
define('CI_ADMIN_SELF', '?page='.$page);
if (function_exists('add_submenu_page'))
add_submenu_page('edit.php', __('Category Icons','category_icons') , __('Category Icons','category_icons') , 'manage_categories' , basename(__FILE__) , 'ig_caticons_adminpanel' );
}
 
/**
* Handle admin panel
* @authors Ivan Georgiev, Brahim Machkouri
*/
function ig_caticons_adminpanel() {
add_action( 'in_admin_footer', 'bm_caticons_credits' );
if (!(file_exists( plugin_dir_path(__FILE__) . 'category_icons.php'))) {
?>
<div id="message" class="updated fade">
<p><strong>
<?php
echo __('Error in Category Icons','category_icons').' : '.__('you must put the category_icons.php in /wp-content/plugins/category-icons/ directory.<br/> So deactivate the plugin, and move the file into category-icons directory.<br/>Click on Plugins 2 times, to restart the plugin detection.<br/>And then, reactivate it.','category_icons');
?>
</strong></p>
</div>
<?php
}
bm_caticons_check_uploads_path();
$tab = isset($_REQUEST['ig_tab']) ? $_REQUEST['ig_tab'] : '';
if (isset($_POST['info_update']) && 'options' == $tab && check_admin_referer('caticons-nonce')) {
?><div class="updated"><p><strong><?php _e('Settings updated.','category_icons');?></strong></p></div><?php
update_option('igcaticons_path', $_POST['igcaticons_path']);
update_option('igcaticons_url', $_POST['igcaticons_url']);
update_option('igcaticons_filetypes', $_POST['igcaticons_filetypes']);
update_option('igcaticons_max_icons', $_POST['igcaticons_max_icons']);
update_option('igcaticons_before_name', $_POST['igcaticons_before_name']);
update_option('igcaticons_max_width', $_POST['igcaticons_max_width']);
update_option('igcaticons_max_height', $_POST['igcaticons_max_height']);
bm_caticons_updatemyoption('igcaticons_templatecode_patch');
bm_caticons_updatemyoption('igcaticons_templatecode_sidebar');
bm_caticons_updatemyoption('igcaticons_rssfeeds');
bm_caticons_updatemyoption('igcaticons_useseo_plugin');
bm_caticons_updatemyoption('igcaticons_iconcatpage');
}
bm_caticons_menu($tab);
switch ($tab) {
case ('icons'):
bm_caticons_adminicons();
break;
case ('template'):
bm_caticons_admintemplate();
break;
case ('options'):
default:
bm_caticons_adminoptions();
}
}
 
// for the checkboxes options
function bm_caticons_updatemyoption($option) {
if (isset($_POST[$option])) {
update_option($option, $_POST[$option]);
}
else {
update_option($option, 0);
}
}
/**
* Display the menu (Icons, Options, Template Tags)
* @author Brahim Machkouri
* @param string $tab The tab that was seleted
*/
function bm_caticons_menu($tab) {
?> <div class="wrap"><?php screen_icon(); ?>
<h2><a target="_blank" href="http://www.category-icons.com/"><?php _e('Category Icons','category_icons'); ?></a></h2>
<ul style="display:inline" id="submenu" class="subsubsub">
<li id="caticons_icons" style="font-size:12px"> <a style="display:inline" href="<?php echo CI_ADMIN_SELF;?>&amp;ig_module=caticons&amp;ig_tab=icons" <?php if ('icons' == $tab ) echo 'class="current"'; ?>><?php _e('Icons','category_icons'); ?></a>&nbsp;</li>
<li id="caticons_options" style="font-size:12px"> <a style="display:inline" href="<?php echo CI_ADMIN_SELF;?>&amp;ig_module=caticons&amp;ig_tab=options" <?php if ('options' == $tab || empty($tab)) echo 'class="current"'; ?>><?php _e('Options','category_icons'); ?></a>&nbsp;</li>
<li id="caticons_templatecode" style="font-size:12px"> <a style="display:inline" href="<?php echo CI_ADMIN_SELF;?>&amp;ig_module=caticons&amp;ig_tab=template" <?php if ('template' == $tab ) echo 'class="current"'; ?>><?php _e('Template Tags','category_icons'); ?></a>&nbsp;</li>
</ul>
</div><div class="clear"></div>
<?php
}
 
/**
* Display the Options tab
* @author Brahim Machkouri
*/
function bm_caticons_adminoptions() {
list($def_path, $def_url) = ig_caticons_defupload();
?><div class="form-wrap">
<form method="post" action="" name="caticons-form3">
<?php wp_nonce_field('caticons-nonce'); ?>
<input type="hidden" name="ig_module" value="caticons"/>
<input type="hidden" name="ig_tab" value="options"/>
<div class="col-container">
<div id="col-right">
<div class="metabox-holder">
<div class="postbox">
<h3 class="hndle"><span><?php _e('Template Tags','category_icons'); ?></span></h3>
<div class="inside">
<label class="selectit"><input type="checkbox" name="igcaticons_templatecode_patch" id="templatecode_patch" value="1" <?php checked('1', get_option('igcaticons_templatecode_patch')); ?> />
<?php _e('Patch the files if possible (check for true)','category_icons'); ?></label>
<label class="selectit"><input type="checkbox" name="igcaticons_templatecode_sidebar" id="templatecode_sidebar" value="1" <?php checked('1', get_option('igcaticons_templatecode_sidebar')); ?> />
<?php _e('Process the list of categories (usually used in sidebar) ?','category_icons');?></label>
</div>
</div>
</div>
<div class="metabox-holder">
<div class="postbox">
<h3 class="hndle"><span><?php _e('Position of the category icon in the sidebar','category_icons'); ?></span></h3>
<div class="inside">
<label><input type="radio" name="igcaticons_before_name" id="before_name_left" value="true" <?php checked('true',get_option('igcaticons_before_name')); ?>/>&nbsp;<?php _e('Left','category_icons');?></label>
<label><input type="radio" name="igcaticons_before_name" id="before_name_right" value="false" <?php checked('false',get_option('igcaticons_before_name')); ?>/>&nbsp;<?php _e('Right','category_icons');?></label>
<p><?php _e("Select 'Left' if you want to put the category icon in front of the category name in the sidebar, or 'Right' if you want to put it after. <b>Remember that you must use the put_cat_icons() function to display icons in the sidebar, unless you use the <a href='widgets.php'>widget</a></b>.",'category_icons');?></p>
</div>
</div>
</div>
<label><input type="checkbox" name="igcaticons_rssfeeds" id="rssfeeds" value="1" <?php checked('1', get_option('igcaticons_rssfeeds')); ?> />&nbsp;<?php _e('Display category icons in RSS feeds','category_icons'); ?></label>
<label><input type="checkbox" name="igcaticons_iconcatpage" id="iconcatpage" value="1" <?php checked('1', get_option('igcaticons_iconcatpage')); ?> />&nbsp;<?php _e('Display only the icon of the selected category in the category page','category_icons'); ?></label>
<?php if (function_exists('seo_friendly_images')) : ?>
<label><input type="checkbox" name="igcaticons_useseo_plugin" id="useseofriendlyimages" value="1" <?php checked('1', get_option('igcaticons_useseo_plugin')); ?> />
<?php _e('Use SEO Friendly Images plugin','category_icons'); ?></label>
<?php endif; ?>
</div>
<div id="col-left">
<div class="form-field">
<label><?php _e('Local Path to Icons','category_icons');?></label>
<input type="text" name="igcaticons_path" size="50" value="<?php echo htmlspecialchars(get_option('igcaticons_path')); ?>" />
<p><?php _e('Leave blank to use default upload path','category_icons');?>.</p>
<p><?php echo __('Your local path to icons is set to ','category_icons').trailingslashit(ig_caticons_path()); ?></p>
</div>
<div class="form-field">
<label><?php _e('URL to Icons','category_icons');?></label>
<input type="text" name="igcaticons_url" size="50" value="<?php echo htmlspecialchars(get_option('igcaticons_url')); ?>" />
<p><?php _e('Leave blank to use default upload url','category_icons');?>.</p>
<p><?php echo __('Your URL to icons is set to ','category_icons').trailingslashit(ig_caticons_url()); ?></p>
</div>
<div class="form-field">
<label><?php _e('Image File Types','category_icons');?></label>
<input type="text" name="igcaticons_filetypes" size="50" value="<?php echo htmlspecialchars(get_option('igcaticons_filetypes')); ?>" />
<p><?php _e("Separate by space or comma. E.g. 'jpg gif jpeg png'",'category_icons');?></p>
</div>
<div class="form-field">
<label><?php _e('Maximum icon width','category_icons');?></label>
<input type="text" name="igcaticons_max_width" size="3" value="<?php echo htmlspecialchars(get_option('igcaticons_max_width')); ?>" />
<p><?php _e('Enter the maximum width of an icon.','category_icons');?></p>
</div>
<div class="form-field">
<label><?php _e('Maximum icon height','category_icons');?></label>
<input type="text" name="igcaticons_max_height" size="3" value="<?php echo htmlspecialchars(get_option('igcaticons_max_height')); ?>" />
<p><?php _e('Enter the maximum height of an icon.','category_icons');?></p>
</div>
<div class="form-field">
<label><?php _e('Maximum number of icons','category_icons');?></label>
<input type="text" name="igcaticons_max_icons" size="3" value="<?php echo htmlspecialchars(get_option('igcaticons_max_icons')); ?>" />
<p><?php _e('Enter the maximum number of icons to display in front of the posts titles.','category_icons');?></p>
</div>
<div class="submit">
<input type="submit" name="info_update" value="<?php _e('Update Options','category_icons');?> &raquo;" />
</div>
</div>
</div>
</form>
</div>
<?php
}
 
/**
* Display the 'Template Tags' tab
* @author Brahim Machkouri
*/
function bm_caticons_admintemplate() {
$list = bm_caticons_template_code();
$message = __('Remember that these locations are just where you COULD paste the tag, not where you MUST paste it. It\'s up to you to find the appropriate location within your template files. The tag is usually within the Loop.','category_icons');
$message .=' '.__('Paste the following code in the listed files, at the line and column numbers displayed below.','category_icons');
if ( 0 == count($list) )
$message = __('There\'s nothing to do.','category_icons');
?>
<div class="wrap"><h2><?php
$ct = wp_get_theme();//current_theme_info();
_e('Template Tags for','category_icons');
echo ' '.$ct->name;
?></h2>
<h3><a target="_blank" href="http://www.category-icons.com/2008/03/12/function-get_cat_icon/">get_cat_icon()</a></h3>
<?php
if ( 0 < count($list) ) {
bm_caticons_codetemplate_display($message,$list,'get_cat_icon');
if (get_option('igcaticons_templatecode_sidebar')==1) {
echo '<h3><a target="_blank" href="http://www.category-icons.com/2008/03/12/function-put_cat_icon/">put_cat_icons()</a></h3>';
$message = __('Replace the code','category_icons');
bm_caticons_codetemplate_display($message,$list,'put_cat_icon');
}
}
?>
</div>
<?php
}
 
/**
* Get the icons base path.
* @author Ivan Georgiev
* @return array
*/
function ig_caticons_path() {
$path = get_option('igcaticons_path');
$def = ig_caticons_defupload();
if (empty($path))
return $def[0];
else
return $path;
}
 
/**
* Get the icons base url.
* @author Ivan Georgiev
* @return array
*/
function ig_caticons_url() {
$url = get_option('igcaticons_url');
$def = ig_caticons_defupload();
if (empty($url))
return $def[1];
else
return $url;
}
 
/**
* Get file types to show when selecting icons.
* @author Ivan Georgiev
* @return array
*/
function ig_caticons_filetypes() {
$types = get_option('igcaticons_filetypes');
if (empty($types))
return get_option('fileupload_allowedtypes');
else
return $types;
}
 
/**
* Get category icons
* @author Ivan Georgiev
* @param int $cat Category ID
* @return array (priority, icon, small_icon)
*/
function bm_caticons_get_icons($cat=0) {
global $wpdb, $caticons_datas;
$cat = (int) $cat;//$wpdb->escape($cat);
$result = false;
if (empty($caticons_datas)) {
$datas = $wpdb->get_results($wpdb->prepare("SELECT cat_id, priority, icon, small_icon FROM $wpdb->ig_caticons"));
foreach ($datas as $row) {
$caticons_datas[$row->cat_id] = array ($row->priority, $row->icon, $row->small_icon);
}
}
if (isset($caticons_datas[$cat])) $result = $caticons_datas[$cat];
return $result;
}
 
/**
* Get category icon with the hightest priority.
* @param array $cats Array of Category IDs
* @return array (cat_id, priority, icon, small_icon)
* @author Oliver Weichhold
*/
function ow_caticons_get_priority_icon($cats) {
global $wpdb;
$instr = '';
foreach($cats as $cat)
$instr .= $wpdb->escape($cat).',';
$instr = preg_replace('/,$/','', $instr); // Remove trailing comma
if ($row = $wpdb->get_row($wpdb->prepare("SELECT cat_id, priority, icon, small_icon FROM $wpdb->ig_caticons WHERE cat_id IN($instr) ORDER BY priority DESC LIMIT 1")))
return array($row->cat_id, $row->priority, $row->icon, $row->small_icon);
else
return false;
}
 
/**
* Utility function to compute a rectangle to fit a given rectangle by maintaining the aspect ratio.
* @author Ivan Georgiev
* @param int $width Width of the rectangle
* @param int $height Height of the rectangle
* @param int $max_width Maximum Width of the new rectangle
* @param int $max_height Maximum Height of the new rectangle
* @param boolean $expand Expand the rectangle ? Default : false
* @return array (width, height)
*/
function ig_caticons_fit_rect($width, $height, $max_width=-1, $max_height=-1, $expand=false) {
$h = (int) $height;
$w = (int) $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);
}
 
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]);
}
}
}
 
/**
* Adds compatibility with Recent posts and recent comments, similar posts from Rob Marsh (http://rmarsh.com/)
* Just use the tag {caticons} to display the category icon
* @author Brahim Machkouri
*/
function otf_caticons($option_key, $result, $ext) {
$categories = get_the_category($result->ID);
return get_cat_icon('cat='.$categories[0]->term_id.'&echo=0');
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.