Make WordPress Core


Ignore:
Timestamp:
04/24/2007 06:41:42 PM (20 years ago)
Author:
ryan
Message:

widgets.php be doubling down. Once is enough.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/widgets.php

    r5299 r5300  
    918918
    919919?>
    920 <?php
    921 
    922 /* Global Variables */
    923 
    924 $wp_registered_sidebars = array();
    925 $wp_registered_widgets = array();
    926 $wp_registered_widget_controls = array();
    927 $wp_registered_widget_styles = array();
    928 $wp_register_widget_defaults = false;
    929 
    930 /* Template tags & API functions */
    931 
    932 if ( !function_exists( 'register_sidebars' ) ) {
    933         function register_sidebars( $number = 1, $args = array() ) {
    934                 $number = (int) $number;
    935        
    936                 if ( is_string( $args ) ) {
    937                         parse_str( $args, $args );
    938                 }
    939        
    940                 $name = ( !empty( $args['name'] ) ) ? $args['name'] : __( 'Sidebar' );
    941        
    942                 for ( $i = 1; $i <= $number; $i++ ) {
    943                         if ( isset( $args['name'] ) && $number > 1 ) {
    944                                 if ( strpos( $name, '%d' ) === false ) {
    945                                         $name = $name . ' %d';
    946                                 }
    947                        
    948                                 $args['name'] = sprintf( $name, $i );
    949                         }
    950                
    951                         register_sidebar( $args );
    952                 }
    953         }
    954 }
    955 
    956 if ( !function_exists( 'register_sidebar' ) ) {
    957         function register_sidebar( $args = array() ) {
    958                 global $wp_registered_sidebars;
    959                
    960                 if ( is_string( $args ) ) {
    961                         parse_str( $args, $args );
    962                 }
    963                
    964                 $defaults = array(
    965                         'name' => sprintf( __( 'Sidebar %d' ), count( $wp_registered_sidebars + 1 ) ),
    966                         'before_widget' => '<li id="%1$s" class="widget %2$s">',
    967                         'after_widget' => "</li>\n",
    968                         'before_title' => '<h2 class="widgettitle">',
    969                         'after_title' => "</h2>\n"
    970                 );
    971                
    972                 $defaults = apply_filters( 'register_sidebar_defaults', $defaults, $args );
    973                
    974                 $sidebar = array_merge( $defaults, $args );
    975                
    976                 $sidebar['id'] = sanitize_title( $sidebar['name'] );
    977                
    978                 $wp_registered_sidebars[$sidebar['id']] = $sidbar;
    979                
    980                 return $sidebar['id'];
    981         }
    982 }
    983 
    984 if ( !function_exists( 'unregister_sidebar' ) ) {
    985         function unregister_sidebar( $name ) {
    986                 global $wp_registered_sidebars;
    987                
    988                 if ( isset( $wp_registered_sidebars[$name] ) ) {
    989                         unset( $wp_registered_sidebars[$name] );
    990                 }
    991         }
    992 }
    993 
    994 if ( !function_exists( 'register_sidebar_widget' ) ) {
    995         function register_sidebar_widget( $name, $output_callback, $classname = '' ) {
    996                 global $wp_registered_widgets, $wp_register_widget_defaults;
    997                
    998                 if ( is_array( $name ) ) {
    999                         $id = sanitize_title( sprintf( $name[0], $name[2] ) );
    1000                         $name = sprintf( __( $name[0], $name[1] ), $name[2] );
    1001                 } else {
    1002                         $id = sanitize_title( $name );
    1003                         $name = __( $name );
    1004                 }
    1005                
    1006                 if ( ( empty( $classname ) || !is_string( $classname ) ) && is_string( $output_callback ) ) {
    1007                         $classname = $output_callback;
    1008                 }
    1009                
    1010                 $widget = array(
    1011                         'id' => $id,
    1012                         'callback' => $output_callback,
    1013                         'classname' => $classname,
    1014                         'params' => array_slice( func_get_args(), 2 )
    1015                 );
    1016                
    1017                 if ( empty( $output_callback ) ) {
    1018                         unset( $wp_registered_widgets[$name] );
    1019                 } elseif ( is_callable( $output_callback ) && ( !isset( $wp_registered_widgets[$name] ) || !$wp_register_widget_defaults ) ) {
    1020                         $wp_registered_widgets[$name] = $widget;
    1021                 }
    1022         }
    1023 }
    1024 
    1025 if ( !function_exists( 'unregister_sidebar_widget' ) ) {
    1026         function unregister_sidebar_widget( $name ) {
    1027                 register_sidebar_widget( $name, '' );
    1028                 unregister_widget_control( $name );
    1029         }
    1030 }
    1031 
    1032 if ( !function_exists( 'register_widget_control' ) ) {
    1033         function register_widget_control( $name, $control_callback, $width = 300, $height = 200 ) {
    1034                 global $wp_registered_widget_controls, $wp_register_sidebar_defaults;
    1035                
    1036                 $width = (int) $width;
    1037                 $height = (int) $height;
    1038                
    1039                 if ( is_array( $name ) ) {
    1040                         $id = sanitize_title( sprintf( $name[0], $name[2] ) );
    1041                         $name = sprintf( __( $name[0], $name[1] ), $name[2] );
    1042                 } else {
    1043                         $id = sanitize_title( $name );
    1044                         $name = __( $name );
    1045                 }
    1046                
    1047                 $width = ( $width > 90 ) ? $width + 60 : 360;
    1048                 $height = ( $height > 60 ) ? $height + 40 : 240;
    1049                
    1050                 if ( empty( $control_callback ) ) {
    1051                         unset( $wp_registered_widget_controls[$name] );
    1052                 } elseif ( !isset( $wp_registered_widget_controls[$name] ) || !$wp_registered_sidebar_defaults ) {
    1053                         $wp_registered_widget_controls[$name] = array(
    1054                                 'id' => $id,
    1055                                 'callback' => $control_callback,
    1056                                 'width' => $width,
    1057                                 'height' => $height,
    1058                                 'params' => array_slice( func_get_args(), 4 )
    1059                         );
    1060                 }
    1061         }
    1062 }
    1063 
    1064 if ( !function_exists( 'unregister_widget_control' ) ) {
    1065         function unregister_widget_control( $name ) {
    1066                 register_sidebar_control( $name, '' );
    1067         }
    1068 }
    1069 
    1070 if ( !function_exists( 'dynamic_sidebar' ) ) {
    1071         function dynamic_sidebar( $name = 1 ) {
    1072                 global $wp_registered_sidebars, $wp_registered_widgets;
    1073                
    1074                 if ( is_int( $name ) ) {
    1075                         $index = sanitize_title( __( 'Sidebar' ) . ' ' . $name );
    1076                         $name = sprintf( __( 'Sidebar %d' ), $name );
    1077                 } else {
    1078                         $index = sanitize_title( $name );
    1079                 }
    1080                
    1081                 $sidebars_widgets = wp_get_sidebars_widgets();
    1082                
    1083                 $sidebar = $wp_registered_sidebars[$index];
    1084                
    1085                 if ( empty( $sidebar ) || !is_array( $sidebars_widgets[$index] ) || empty( $sidebars_widgets[$index] ) ) {
    1086                         return false;
    1087                 }
    1088                
    1089                 $did_one = false;
    1090                
    1091                 foreach ( $sidebars_widgets[$index] as $name ) {
    1092                         $callback = $wp_registered_widgets[$name]['callback'];
    1093                        
    1094                         $params = array_merge( array( $sidebar ), (array) $wp_registered_widgets[$name]['params'] );
    1095                         $params[0]['before_widget'] = sprintf( $params[0]['before_widget'], $wp_registered_widgets[$name]['id'], $wp_registered_widgets[$name]['classname'] );
    1096                        
    1097                         if ( is_callable( $callback ) ) {
    1098                                 call_user_func_array( $callback, $params );
    1099                                 $did_one = true;
    1100                         }
    1101                 }
    1102                
    1103                 return $did_one;
    1104         }
    1105 }
    1106 
    1107 if ( !function_exists( 'is_active_widget' ) ) {
    1108         function is_active_widget( $callback ) {
    1109                 global $wp_registered_widgets;
    1110                
    1111                 $sidebars_widgets = wp_get_sidebars_widgets();
    1112                
    1113                 if ( is_array( $sidebars_widgets ) ) {
    1114                         foreach ( $sidebars_widgets as $sidebar => $widgets ) {
    1115                                 if ( is_array( $widgets) ) {
    1116                                         foreach ( $widgets as $widget ) {
    1117                                                 if ( $wp_registered_widgets[$widget]['callback'] == $callback ) {
    1118                                                         return true;
    1119                                                 }
    1120                                         }
    1121                                 }
    1122                         }
    1123                 }
    1124                
    1125                 return false;
    1126         }
    1127 }
    1128 
    1129 if ( !function_exists( 'is_dynamic_sidebar' ) ) {
    1130         function is_dynamic_sidebar() {
    1131                 global $wp_registered_sidebars, $wp_registered_widgets;
    1132                
    1133                 $sidebars_widgets = wp_get_sidebars_widgets();
    1134                
    1135                 foreach ( $wp_registered_sidebars as $index => $sidebar ) {
    1136                         if ( count( $sidebars_widgets[$index] ) > 0 ) {
    1137                                 foreach ( $sidebars_widgets[$index] as $widget ) {
    1138                                         if ( array_key_exists( $widget, $wp_registered_sidebars ) ) {
    1139                                                 return true;
    1140                                         }
    1141                                 }
    1142                         }
    1143                 }
    1144                
    1145                 return false;
    1146         }
    1147 }
    1148 
    1149 /* Internal Functions (not admin-related) */
    1150 
    1151 function wp_get_sidebars_widgets() {
    1152         return get_option( 'wp_sidebars_widgets' );
    1153 }
    1154 
    1155 /* Default Widgets */
    1156 
    1157 function wp_widget_pages( $args ) {
    1158         extract( $args );
    1159        
    1160         $options = get_option( 'wp_widget_pages' );
    1161        
    1162         $title = ( empty( $options['title'] ) ) ? __( 'Pages' ) : $options['title'];
    1163        
    1164         echo $before_widget . $before_title . $title . $after_title . "<ul>\n";
    1165         wp_list_pages( 'title_li=' );
    1166         echo "</ul>\n" . $after_widget;
    1167 }
    1168 
    1169 function wp_widget_pages_control() {
    1170         $options = $newoptions = get_option( 'wp_widget_pages' );
    1171        
    1172         if ( isset( $_POST['pages-submit'] ) ) {
    1173                 $newoptions['title'] = strip_tags( stripslashes( $_POST['pages-title'] ) );
    1174                
    1175                 if ( $newoptions != $options ) {
    1176                         $options = $newoptions;
    1177                         update_option( 'wp_widget_pages', $options );
    1178                 }
    1179         }
    1180        
    1181         $title = htmlspecialchars( $options['title'], ENT_QUOTES );
    1182 ?>
    1183                 <p><label for="pages-title"><?php _e( 'Title:' ); ?> <input style="width:250px" id="pages-title" name="pages-title" type="text" value="<?php echo $title; ?>" /></label></p>
    1184                 <input type="hidden" id="pages-submit" name="pages-submit" value="1" />
    1185 <?php
    1186 }
    1187 
    1188 function wp_widget_links( $args ) {
    1189         global $wp_db_version;
    1190         extract( $args );
    1191        
    1192         if ( $wp_db_version < 3582 ) {
    1193                 get_links_list();
    1194         } else {
    1195                 wp_list_bookmarks( array( 'title_before' => $before_title, 'title_after' => $after_title ) );
    1196         }
    1197 }
    1198 
    1199 function wp_widget_search( $args ) {
    1200         extract( $args );
    1201 ?>
    1202                 <?php echo $before_widget; ?>
    1203                         <form id="searchform" action="<?php bloginfo( 'url' ); ?>" method="get">
    1204                                 <div><input type="text" name="s" id="s" size="15" /><br />
    1205                                         <input type="submit" value="<?php _e( 'Search' ); ?>" /></div>
    1206                         </form>
    1207                 <?php echo $after_widget; ?>
    1208 <?php
    1209 }
    1210 
    1211 function wp_widget_archives( $args ) {
    1212         extract( $args );
    1213        
    1214         $options = get_option( 'wp_widget_archives' );
    1215         $c = ( $options['count'] ) ? '1' : '0';
    1216         $title = ( empty( $options['title'] ) ) ? __( 'Archives' ) : $options['title'];
    1217 ?>
    1218                 <?php echo $before_widget; ?>
    1219                         <?php echo $before_title . $title . $after_title; ?>
    1220                         <ul>
    1221                         <?php wp_get_archives( 'type=monthly&show_post_count=' . $c ); ?>
    1222                         </ul>
    1223                 <?php echo $after_widget; ?>
    1224 <?php
    1225 }
    1226 
    1227 function wp_widget_archives_control() {
    1228         $options = $newoptions = get_option( 'wp_widget_archives' );
    1229        
    1230         if ( isset( $_POST['archives-submit'] ) ) {
    1231                 $newoptions['count'] = isset( $_POST['archives-count'] );
    1232                 $newoptions['title'] = strip_tags( stripslashes( $_POST['archives-title'] ) );
    1233                
    1234                 if ( $newoptions != $options ) {
    1235                         $options = $newoptions;
    1236                         update_option( 'wp_widget_archives', $options );
    1237                 }
    1238         }
    1239        
    1240         $count = ( isset( $options['count'] ) ) ? ' checked="checked"' : '';
    1241         $title = htmlspecialchars( $options['title'], ENT_QUOTES );
    1242 ?>
    1243                 <p><label for="archives-title"><?php _e( 'Title:' ); ?> <input style="width:250px" id="archives-title" name="archives-title" type="text" value="<?php echo $title; ?>" /></label></p>
    1244                 <p style="text-align:right;margin-right:40px"><label for="archives-count"><?php _e( 'Show post counts?' ); ?> <input type="checkbox" class="checkbox" name="archives-count" id="archives-count" <?php echo $count; ?>/></p>
    1245                 <input type="hidden" name="archives-submit" id="archives-submit" value="1" />
    1246 <?php
    1247 }
    1248 
    1249 function wp_widget_meta( $args ) {
    1250         extract( $args );
    1251        
    1252         $options = get_option( 'wp_widget_meta' );
    1253         $title = ( empty( $options['title'] ) ) ? __( 'Meta' ) : $options['title'];
    1254 ?>
    1255                 <?php echo $before_widget; ?>
    1256                         <?php echo $before_title . $title . $after_title; ?>
    1257                         <ul>
    1258                                 <?php wp_register(); ?>
    1259                                 <li><?php wp_loginout(); ?></li>
    1260                                 <li><a href="<?php bloginfo( 'rss2_url' ); ?>" title="<?php _e( 'Syndicate this site using RSS 2.0' ); ?>"><?php _e( 'Entries <abbr title="Really Simple Syndication">RSS</abbr>' ); ?></a></li>
    1261                                 <li><a href="<?php bloginfo( 'comments_rss2_url' ); ?>" title="<?php _e( 'The latest comments to all posts in RSS' ); ?>"><?php _e( 'Comments <abbr title="Really Simple Syndication">RSS</abbr>' ); ?></a></li>
    1262                                 <li><a href="http://wordpress.com/" title="<?php _e( 'Powered by Wordpress, state-of-the-art semantic personal publishing platform.' ); ?>">WordPress.com</a></li>
    1263                                 <?php wp_meta(); ?>
    1264                         </ul>
    1265                 <?php echo $after_widget; ?>
    1266 <?php
    1267 }
    1268 
    1269 function wp_widget_meta_control() {
    1270         $options = $newoptions = get_option( 'wp_widget_meta' );
    1271        
    1272         if ( isset( $_POST['meta-submit'] ) ) {
    1273                 $newoptions['title'] = strip_tags( stripslashes( $_POST['meta-title'] ) );
    1274                
    1275                 if ( $newoptions != $options ) {
    1276                         $options = $newoptions;
    1277                         update_option( 'wp_widget_meta', $options );
    1278                 }
    1279         }
    1280        
    1281         $title = htmlspecialchars( $options['title'], ENT_QUOTES );
    1282 ?>
    1283                 <p><label for="meta-title"><?php _e( 'Title:' ); ?> <input type="text" name="meta-title" id="meta-title" style="width:250px" value="<?php echo $title; ?>" /></label></p>
    1284                 <input type="hidden" name="meta-submit" id="meta-submit" value="1" />
    1285 <?php
    1286 }
    1287 
    1288 function wp_widget_calendar( $args ) {
    1289         extract( $args );
    1290        
    1291         $title = ( empty( $options['title'] ) ) ? '&nbsp;' : $options['title'];
    1292 ?>
    1293                 <?php echo $before_widget; ?>
    1294                         <?php echo $before_title . $title . $after_title; ?>
    1295                         <div id="calendar_wrap">
    1296                                 <?php get_calendar(); ?>
    1297                         </div>
    1298                 <?php echo $after_widget; ?>
    1299 <?php
    1300 }
    1301 
    1302 function wp_widget_calendar_control() {
    1303         $options = $newoptions = get_option( 'wp_widget_calendar' );
    1304        
    1305         if ( isset( $_POST['calendar-submit'] ) ) {
    1306                 $newoptions['title'] = strip_tags( stripslashes( $_POST['calendar-title'] ) );
    1307                
    1308                 if ( $newoptions != $options ) {
    1309                         $options = $newoptions;
    1310                         update_option( 'wp_widget_calendar', $options );
    1311                 }
    1312         }
    1313        
    1314         $title = htmlspecialchars( $options['title'], ENT_QUOTES );
    1315 ?>
    1316                 <p><label for="calendar-title"><?php _e( 'Title:' ); ?> <input type="text" style="width:250px" id="calendar-title" name="calendar-title" value="<?php echo $title; ?>" /></label></p>
    1317                 <input type="hidden" id="calendar-title" name="calendar-title" value="1" />
    1318 <?php
    1319 }
    1320 
    1321 function wp_widget_text( $args, $i = 1 ) {
    1322         extract( $args );
    1323        
    1324         $options = get_option( 'wp_widget_text' );
    1325        
    1326         $title = ( empty( $options[$i]['title'] ) ) ? '' : $options[$i]['title'];
    1327 ?>
    1328                 <?php echo $before_widget; ?>
    1329                         <?php print ( empty( $title ) ) ? '' : $before_title . $title . $after_title; ?>
    1330                         <div class="textwidget"><?php echo $text; ?></div>
    1331                 <?php echo $after_widget; ?>
    1332 <?php
    1333 }
    1334 
    1335 function wp_widget_text_control( $i ) {
    1336         $options = $newoptions = get_option( 'wp_widget_text' );
    1337        
    1338         if ( isset( $_POST['text-submit-' . $i] ) ) {
    1339                 $newoptions[$i]['title'] = strip_tags( stripslashes( $_POST['text-title-' . $i] ) );
    1340                 $newoptions[$i]['text'] = stripslashes( $_POST['text-text-' . $i] );
    1341                
    1342                 if ( !current_user_can( 'unfiltered_html' ) ) {
    1343                         $newoptions[$i]['text'] = stripslashes( wp_filter_post_kses( $newoptions[$i]['text'] ) );
    1344                 }
    1345                
    1346                 if ( $newoptions != $options ) {
    1347                         $options = $newoptions;
    1348                         update_options( 'wp_widget_text', $options );
    1349                 }
    1350         }
    1351        
    1352         $title = htmlspecialchars( $options[$i]['title'], ENT_QUOTES );
    1353         $text = htmlspecialchars( $options[$i]['text'], ENT_QUOTES );
    1354 ?>
    1355                 <input style="width:450px" id="text-title-<?php echo $i; ?>" name="text-title-<?php echo $i; ?>" type="text" value="<?php echo $title; ?>" />
    1356                 <textarea style="width:450px;height:280px" id="text-text-<?php echo $i; ?>" name="text-text-<?php echo $i; ?>"><?php echo $text; ?></textarea>
    1357                 <input type="hidden" id="text-submit-<?php echo $i; ?>" name="text-submit-<?php echo $i; ?>" value="1" />
    1358 <?php
    1359 }
    1360 
    1361 function wp_widget_text_setup() {
    1362         $options = $newoptions = get_option( 'wp_widget_text' );
    1363        
    1364         if ( isset( $_POST['text-number-submit'] ) ) {
    1365                 $i = (int) $_POST['text-number'];
    1366                
    1367                 if ( $i > 9 ) {
    1368                         $i = 9;
    1369                 } elseif ( $i < 1 ) {
    1370                         $i = 1;
    1371                 }
    1372                
    1373                 $newoptions['number'] = $i;
    1374                
    1375                 if ( $newoptions != $options ) {
    1376                         $options = $newoptions;
    1377                         update_option( 'wp_widget_text', $options );
    1378                 }
    1379         }
    1380 }
    1381 
    1382 function wp_widget_text_page() {
    1383         $options = get_option( 'widget_text' );
    1384        
    1385         $i = $option['number'];
    1386 ?>
    1387         <div class="wrap">
    1388                 <form method="post">
    1389                         <h2><?php _e( 'Text Widgets' ); ?></h2>
    1390                        
    1391                         <p style="line-height:30px"><?php _e( 'How many widgets would you like?' ); ?>
    1392                                 <select id="text-number" name="text-number" value="<?php echo $i; ?>">
    1393                                 <?php for ( $j = 1; $j < 10; $j++ ) { ?>
    1394                                         <option value="<?php echo $j; ?>"<?php if ( $i == $j ) { ?> selected="selected"<?php } ?>><?php echo $j; ?></option>
    1395                                 <?php } ?>
    1396                                 </select>
    1397                                 <span class="submit"><input type="submit" name="text-number-submit" id="text-number-submit" value="<?php _e( 'Save' ); ?>" /></span>
    1398                         </p>
    1399                 </form>
    1400         </div>
    1401 <?php
    1402 }
    1403 
    1404 function wp_widget_text_register() {
    1405         $options = get_option( 'wp_widget_text' );
    1406        
    1407         $i = $options['number'];
    1408        
    1409         if ( $i < 1 ) {
    1410                 $i = 1;
    1411         } elseif ( $i > 9 ) {
    1412                 $i = 9;
    1413         }
    1414        
    1415         for ( $j = 1; $j <= 9; $j++ ) {
    1416                 $name = array( 'Text %s', '', $i );
    1417                 register_sidebar_widget( $name, ( $j <= $i ) ? 'wp_widget_text' : '', $j );
    1418                 register_widget_control( $name, ( $j <= $i ) ? 'wp_widget_text_control' : '', 460, 350, $j );
    1419         }
    1420        
    1421         add_action( 'sidebar_admin_setup', 'wp_widget_text_setup' );
    1422         add_action( 'sidebar_admin_page', 'wp_widget_text_page' );
    1423 }
    1424 
    1425 function wp_widget_categories( $args ) {
    1426         extract( $args );
    1427        
    1428         $options = get_option( 'wp_widget_categories' );
    1429        
    1430         $title = ( empty( $options['title'] ) ) ? __( 'Categories' ) : $options['title'];
    1431         $c = ( $options['count'] ) ? '1' : '0';
    1432         $h = ( $options['hierarchical'] ) ? '1' : '0';
    1433 ?>
    1434                 <?php echo $before_widget; ?>
    1435                         <?php echo $before_title . $title . $after_title; ?>
    1436                         <ul>
    1437                                 <?php wp_list_cats( 'sort_column=name&optioncount=' . $c . '&hierarchical=' . $h ); ?>
    1438                         </ul>
    1439                 <?php echo $after_widget; ?>
    1440 <?php
    1441 }
    1442 
    1443 function wp_widget_categories_control() {
    1444         $options = $newoptions = get_option( 'wp_widget_categories' );
    1445        
    1446         if ( isset( $_POST['categories-submit'] ) ) {
    1447                 $newoptions['count'] = isset( $_POST['categories-count'] );
    1448                 $newoptions['hierarchical'] = isset( $_POST['categories-hierarchical'] );
    1449                 $newoptions['title'] = strip_tags( stripslashes( $_POST['categories-title'] ) );
    1450                
    1451                 if ( $newoptions != $options ) {
    1452                         $options = $newoptions;
    1453                         update_option( 'wp_widget_categories', $options );
    1454                 }
    1455         }
    1456        
    1457         $count = ( $options['count'] ) ? ' checked="checked"' : '';
    1458         $hierarchical = ( $options['hierarchical'] ) ? ' checked="checked"' : '';
    1459         $title = wp_specialchars( $options['title'] );
    1460 ?>
    1461                 <p><label for="categories-title"><?php _e( 'Title:' ); ?> <input type="text" value="<?php echo $title; ?>" id="categories-title" name="categories-title" /></label></p>
    1462                 <p style="text-align:right;margin-right:40px"><label for="categories-count"><?php _e( 'Show post counts? '); ?> <input type="checkbox" class="checkbox"<?php echo $count; ?> id="categories-count" name="categories-count" /></label></p>
    1463                 <p style="text-align:right;margin-right:40px"><label for="categories-hierarchical"><?php _e( 'Show hierarchy?' ); ?> <input type="checkbox" class="checkbox"<?php echo $hierarchical; ?> id="categories-hierarchical" name="categories-hierarchical" /></label></p>
    1464                 <input type="hidden" name="categories-submit" id="categories-submit" value="1" />
    1465 <?php
    1466 }
    1467 
    1468 function wp_widget_recent_entries( $args ) {
    1469         extract( $args );
    1470        
    1471         $title = __( 'Recent Posts' );
    1472        
    1473         $query = new WP_Query( 'showposts=10' );
    1474        
    1475         if ( $query->have_posts() ) {
    1476 ?>
    1477                 <?php echo $before_widget; ?>
    1478                         <?php echo $before_title . $title . $after_title; ?>
    1479                         <ul>
    1480                         <?php while ( $query->have_posts() ) {
    1481                                 $query->the_post();
    1482                         ?>
    1483                                 <li><a href="<?php the_permalink(); ?>"><?php
    1484                                         if ( !empty( get_the_title() ) ) {
    1485                                                 the_title();
    1486                                         } else {
    1487                                                 the_ID();
    1488                                         }
    1489                                 ?></a></li>
    1490                         <?php } ?>
    1491                         </ul>
    1492                 <?php echo $after_widget; ?>
    1493 <?php
    1494 }
    1495 
    1496 function wp_widget_recent_comments( $args ) {
    1497         global $wpdb;
    1498        
    1499         extract( $args );
    1500        
    1501         $options = get_option( 'wp_widget_recent_comments' );
    1502        
    1503         $title = ( empty( $options['title'] ) ) ? __( 'Recent Comments' ) : $options['title'];
    1504        
    1505         $comments = $wpdb->get_results( "SELECT * FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 5" );
    1506        
    1507         if ( is_array( $comments ) && count( $comments ) > 0 ) {
    1508 ?>
    1509                 <?php echo $before_widget; ?>
    1510                         <?php echo $before_title . $title . $after_title; ?>
    1511                         <ul id="recentcomments">
    1512                         <?php foreach ( $comments as $comment ) { ?>
    1513                                 <li class="recentcomments"><?php echo sprintf( __( '%1$s on %2$s' ), get_comment_author_link(), '<a href="' . get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID . '">' . get_the_title( $comment->comment_post_ID ) . '</a>' ); ?></li>
    1514                         <?php } ?>
    1515                         </ul>
    1516                 <?php echo $after_widget; ?>
    1517 <?php
    1518         }
    1519 }
    1520 
    1521 function wp_widget_recent_comments_control() {
    1522         $options = $newoptions = get_option( 'wp_widget_recent_comments' );
    1523        
    1524         if ( isset( $_POST['recent-comments-submit'] ) ) {
    1525                 $newoptions['title'] = strip_tags( stripslashes( $_POST['recent-comments-title'] ) );
    1526                
    1527                 if ( $newoptions != $options ) {
    1528                         $options = $newoptions;
    1529                         update_option( 'wp_widget_recent_comments', $options );
    1530                 }
    1531         }
    1532        
    1533         $title = htmlspecialchars( $options['title'], ENT_QUOTES );
    1534 ?>
    1535                 <p><label for="recent-comments-title"><?php _e( 'Title:' ); ?> <input type="text" style="width:250px" id="recent-comments-title" name="recent-comments-title" value="<?php echo $title; ?>" /></label></p>
    1536                 <input type="hidden" name="recent-comments-submit" id="recent-comments-submit" value="1" />
    1537 <?php
    1538 }
    1539 
    1540 function wp_widget_recent_comments_wphead() {
    1541 ?>
    1542                 <style type="text/css">
    1543                         .recentcomments a {
    1544                                 display: inline !important;
    1545                                 padding: 0 !important;
    1546                                 margin: 0 !important;
    1547                         }
    1548                 </style>
    1549 <?php
    1550 }
    1551 
    1552 function wp_widget_recent_comments_register() {
    1553         register_sidebar_widget( array( 'Recent Comments', '' ), 'wp_widget_recent_comments' );
    1554         register_widget_control( array( 'Recent Comments', '' ), 'wp_widget_recent_comments_control' );
    1555        
    1556         if ( is_active_widget( 'wp_widget_recent_comments' ) ) {
    1557                 add_action( 'wp_head', 'wp_widget_recent_comments_wphead' );
    1558         }
    1559 }
    1560 
    1561 function wp_widget_rss( $args, $i = 1 ) {
    1562         extract( $args );
    1563        
    1564         if ( file_exists( ABSPATH . WPINC . '/rss.php' ) ) {
    1565                 require_once ABSPATH . WPINC . '/rss.php';
    1566         } else {
    1567                 require_once ABSPATH . WPINC . '/rss-functions.php';
    1568         }
    1569        
    1570         $options = get_option( 'wp_widget_rss' );
    1571        
    1572         $number_items = (int) $options[$i]['number_items'];
    1573         $show_summary = $options[$i]['show_summary'];
    1574        
    1575         if ( empty( $number_items ) || $number_items < 1 || $number_items > 10 ) {
    1576                 $number_items = 10;
    1577         }
    1578        
    1579         $url = $options[$i]['url'];
    1580        
    1581         if ( empty( $url ) ) {
    1582                 return;
    1583         }
    1584        
    1585         while ( strstr( $url, 'http' ) != $url ) {
    1586                 $url = substr( $url, 1 );
    1587         }
    1588        
    1589         $rss = fetch_rss( $url );
    1590        
    1591         $link = wp_specialchars( strip_tags( $rss->channel['link'] ), 1 );
    1592        
    1593         while ( strstr( $link, 'http' ) != $link ) {
    1594                 $link = substr( $link, 1 );
    1595         }
    1596        
    1597         $desc = wp_specialchars( strip_tags( html_entity_decode( $rss->channel['description'], ENT_QUOTES ) ), 1 );
    1598        
    1599         $title = $options[$i]['title'];
    1600        
    1601         if ( empty( $title ) ) {
    1602                 $title = htmlentities( strip_tags( $rss->channel['title'] ) );
    1603         }
    1604        
    1605         if ( empty( $title ) ) {
    1606                 $title = $desc;
    1607         }
    1608        
    1609         if ( empty( $title ) ) {
    1610                 $title = __( 'Unknown Feed' );
    1611         }
    1612        
    1613         $url = wp_specialchars( strip_tags( $url ), 1 );
    1614        
    1615         $icon = get_option( 'url' ) . '/wp-includes/images/rss.png';
    1616        
    1617         $title = '<a href="%1$s" class="rsswidget" title="%2$s"><img src="%3$s" style="width:14px;height:14px" alt="%4$s" /></a> <a href="%5$s" class="rsswidget" title="%6$s">%7$s</a>';
    1618         $title = sprintf( $title, $url, __( 'Syndicate this content' ), $icon, __( 'RSS' ), $link, $desc, $title );
    1619 ?>
    1620                 <?php echo $before_widget; ?>
    1621                         <?php echo $before_title . $title . $after_title; ?>
    1622                         <ul>
    1623                         <?php
    1624                                 if ( is_array( $rss->items ) ) {
    1625                                         $rss->items = array_slice( $rss->items, 0, $number_items );
    1626                                        
    1627                                         foreach ( $rss->items as $item ) {
    1628                                                 while ( strstr( $item['link'], 'http' ) != $item['link'] ) {
    1629                                                         $item['link'] = substr( $item['link'], 1 );
    1630                                                 }
    1631                                                
    1632                                                 $link = wp_specialchars( strip_tags( $item['link'] ), 1 );
    1633                                                 $title = wp_specialchars( strip_tags( $item['title'] ), 1 );
    1634                                                
    1635                                                 if ( empty( $title ) ) {
    1636                                                         $title = __( 'Untitled' );
    1637                                                 }
    1638                                                
    1639                                                 $desc = '';
    1640                                                
    1641                                                 if ( $show_summary ) {
    1642                                                         $summary = '<div class="rssSummary">' . $item['description'] . '</div>';
    1643                                                 } else {
    1644                                                         $desc = str_replace( array( "\r", "\n" ), ' ', wp_specialchars( strip_tags( html_entity_decode( $item['description'], ENT_QUOTES ) ), 1 ) );
    1645                                                         $summary = '';
    1646                                                 }
    1647                         ?>
    1648                                 <li><a class="rsswidget" href="<?php echo $link; ?>" title="<?php echo $desc; ?>"><?php echo $title; ?></a><?php echo $summary; ?></li>
    1649                         <?php
    1650                                         }
    1651                                 } else {
    1652                         ?>
    1653                                 <li><?php _e( 'An error has occurred; the feed is probably down. Try again later.' ); ?></li>
    1654                         <?php
    1655                                 }
    1656                         ?>
    1657                 <?php echo $after_widget; ?>
    1658 <?php
    1659 }
    1660 
    1661 function wp_widget_rss_control( $i ) {
    1662         $options = $newoptions = get_option( 'wp_widget_rss' );
    1663        
    1664         if ( isset( $_POST['rss-submit-' . $i] ) ) {
    1665                 $newoptions[$i]['number_items'] = (int) $_POST['rss-items-' . $i];
    1666                 $newoptions[$i]['url'] = strip_tags( stripslashes( $_POST['rss-url-' . $i] ) );
    1667                 $newoptions[$i]['title'] = trim( strip_tags( stripslashes( $_POST['rss-title-' . $i] ) ) );
    1668                
    1669                 if ( $newoptions != $options ) {
    1670                         $options = $newoptions;
    1671                         update_option( 'wp_widget_rss', $options );
    1672                 }
    1673         }
    1674        
    1675         $url = htmlspecialchars( $options[$i]['url'], ENT_QUOTES );
    1676         $number_items = (int) $options[$i]['number_items'];
    1677         $title = htmlspecialchars( $options[$i]['title'], ENT_QUOTES );
    1678        
    1679         if ( empty( $number_items ) || $number_items < 1 ) {
    1680                 $number_items = 10;
    1681         }
    1682 ?>
    1683                 <p style="text-align:center;"><?php _e( 'Enter the RSS feed URL here:' ); ?></p>
    1684                 <input style="width: 400px;" id="rss-url-<?php echo $i; ?>" name="rss-url-<?php echo $i; ?>" type="text" value="<?php echo $url; ?>" />
    1685                 <p style="text-align:center;"><?php _e( 'Give the feed a title (optional):' ); ?></p>
    1686                 <input style="width: 400px;" id="rss-title-<?php echo $i; ?>" name="rss-title-<?php echo $i; ?>" type="text" value="<?php echo $title; ?>" />
    1687                 <p style="text-align:center; line-height: 30px;"><?php _e('How many items would you like to display?', 'widgets'); ?> <select id="rss-items-<?php echo $i; ?>" name="rss-items-<?php echo $i; ?>"><?php for ( $j = 1; $j <= 10; $j++ ) echo "<option value='$j' ".($items==$j ? "selected='selected'" : '').">$j</option>"; ?></select></p>
    1688                 <input type="hidden" id="rss-submit-<?php echo $i; ?>" name="rss-submit-<?php echo $i; ?>" value="1" />
    1689 <?php
    1690 }
    1691 
    1692 function wp_widget_rss_setup() {
    1693         $options = $newoptions = get_option( 'wp_widget_rss' );
    1694        
    1695         if ( isset( $_POST['rss-number-submit'] ) ) {
    1696                 $i = (int) $_POST['rss-number'];
    1697                
    1698                 if ( $i > 9 ) {
    1699                         $number = 9;
    1700                 } elseif ( $i < 1 ) {
    1701                         $number = 1;
    1702                 }
    1703                
    1704                 $newoptions['number'] = $i;
    1705                
    1706                 if ( $newoptions != $options ) {
    1707                         $options = $newoptions;
    1708                         update_option( 'wp_widget_rss', $options );
    1709                         widget_rss_register( $options['number'] );
    1710                 }
    1711         }
    1712 }
    1713 
    1714 function wp_widget_rss_page() {
    1715         $options = get_option( 'wp_widget_rss' );
    1716        
    1717         $i = $options['number'];
    1718 ?>
    1719         <div class="wrap">
    1720                 <form method="post">
    1721                         <h2><?php _e( 'RSS Feed Widgets' ); ?></h2>
    1722                        
    1723                         <p style="line-height:30px"><?php _e( 'How many RSS widgets would you like?' ); ?>
    1724                                 <select id="rss-number" name="rss-number" value="<?php echo $i; ?>">
    1725                                 <?php for ( $j = 1; $j < 10; $j++ ) { ?>
    1726                                         <option value="<?php echo $j; ?>"<?php
    1727                                                 if ( $i == $j ) {
    1728                                                         echo ' selected="selected"';
    1729                                                 }
    1730                                         ?>><?php echo $j; ?></option>
    1731                                 <?php } ?>
    1732                                 </select>
    1733                         </p>
    1734                 </form>
    1735         </div>
    1736 <?php
    1737 }
    1738 
    1739 function wp_widget_rss_register() {
    1740         $options = get_option( 'wp_widget_rss' );
    1741        
    1742         $i = $options['number'];
    1743        
    1744         if ( $i < 1 ) {
    1745                 $i = 1;
    1746         } elseif ( $i > 9 ) {
    1747                 $i = 9;
    1748         }
    1749        
    1750         for ( $j = 1; $j <= 9; $j++ ) {
    1751                 $name = array( 'RSS %s', '', $j );
    1752                 register_sidebar_widget( $name, ( $j <= $i ) ? 'wp_widget_rss' : '', $j );
    1753                 register_widget_control( $name, ( $j <= $i ) ? 'wp_widget_rss_control' : '', 410, 200, $j );
    1754         }
    1755        
    1756         add_action( 'sidebar_admin_setup', 'wp_widget_rss_setup' );
    1757         add_action( 'sidebar_admin_page', 'wp_widget_rss_page' );
    1758        
    1759         if ( is_active_widget( 'wp_widget_rss' ) ) {
    1760                 add_action( 'wp_head', 'wp_widget_rss_wphead' );
    1761         }
    1762 }
    1763 
    1764 function wp_widget_rss_wphead() {
    1765 ?>
    1766                 <style type="text/css">
    1767                         a.rsswidget {
    1768                                 display: inline !important;
    1769                         }
    1770                        
    1771                         a.rsswidget img {
    1772                                 background: orange;
    1773                                 color: white;
    1774                         }
    1775                 </style>
    1776 <?php
    1777 }
    1778 
    1779 function wp_widgets_init() {
    1780         global $wp_register_widget_defaults;
    1781        
    1782         $wp_register_widget_defaults = true;
    1783        
    1784         wp_widget_text_register();
    1785         wp_widget_rss_register();
    1786         wp_widget_recent_comments_register();
    1787        
    1788         register_sidebar_widget( 'Pages', 'wp_widget_pages' );
    1789         register_widget_control( 'Pages', 'wp_widget_pages_control', 300, 90 );
    1790        
    1791         register_sidebar_widget( 'Calendar', 'wp_widget_calendar' );
    1792         register_widget_control( 'Calendar', 'wp_widget_calendar_control', 300, 90 );
    1793        
    1794         register_sidebar_widget( 'Archives', 'wp_widget_archives' );
    1795         register_widget_control( 'Archives', 'wp_widget_archives_control', 300, 90 );
    1796        
    1797         register_sidebar_widget( 'Links', 'wp_widget_links' );
    1798        
    1799         register_sidebar_widget( 'Meta', 'wp_widget_meta' );
    1800         register_widget_control( 'Meta', 'wp_widget_meta_control', 300, 90 );
    1801        
    1802         register_sidebar_widget( 'Search', 'wp_widget_search' );
    1803        
    1804         register_sidebar_widget( 'Categories', 'wp_widget_categories' );
    1805         register_widget_control( 'Categories', 'wp_widget_categories_control', 300, 150 );
    1806        
    1807         register_sidebar_widget( 'Recent Posts', 'wp_widget_recent_entries' );
    1808        
    1809         $wp_register_widget_defaults = false;
    1810        
    1811         do_action( 'widgets_init' );
    1812 }
    1813 
    1814 ?>
Note: See TracChangeset for help on using the changeset viewer.