PHP Cross Reference of WordPress Subversion HEAD |
| [ Index ] [ Classes ] [ Functions ] [ Variables ] [ Constants ] |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* Global Variables */ 4 5 global $wp_registered_sidebars, $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_styles, $wp_registered_widget_defaults; 6 7 $wp_registered_sidebars = array(); 8 $wp_registered_widgets = array(); 9 $wp_registered_widget_controls = array(); 10 $wp_registered_widget_styles = array(); 11 $wp_register_widget_defaults = false; 12 13 /* Template tags & API functions */ 14 15 function register_sidebars($number = 1, $args = array()) { 16 $number = (int) $number; 17 18 if ( is_string($args) ) 19 parse_str($args, $args); 20 21 for ( $i=1; $i <= $number; $i++ ) { 22 $_args = $args; 23 if ( $number > 1 ) { 24 $_args['name'] = isset($args['name']) ? $args['name'] : sprintf(__('Sidebar %d'), $i); 25 } else { 26 $_args['name'] = isset($args['name']) ? $args['name'] : __('Sidebar'); 27 } 28 $_args['id'] = isset($args['id']) ? $args['id'] : "sidebar-$i"; 29 register_sidebar($_args); 30 } 31 } 32 33 function register_sidebar($args = array()) { 34 global $wp_registered_sidebars; 35 36 if ( is_string($args) ) 37 parse_str($args, $args); 38 39 $i = count($wp_registered_sidebars) + 1; 40 41 $defaults = array( 42 'name' => sprintf(__('Sidebar %d'), $i ), 43 'id' => "sidebar-$i", 44 'before_widget' => '<li id="%1$s" class="widget %2$s">', 45 'after_widget' => "</li>\n", 46 'before_title' => '<h2 class="widgettitle">', 47 'after_title' => "</h2>\n", 48 ); 49 50 $sidebar = array_merge($defaults, $args); 51 52 $wp_registered_sidebars[$sidebar['id']] = $sidebar; 53 54 return $sidebar['id']; 55 } 56 57 function unregister_sidebar( $name ) { 58 global $wp_registered_sidebars; 59 60 if ( isset( $wp_registered_sidebars[$name] ) ) 61 unset( $wp_registered_sidebars[$name] ); 62 } 63 64 function register_sidebar_widget($name, $output_callback, $classname = '') { 65 // Compat 66 if ( is_array($name) ) { 67 if ( count($name) == 3 ) 68 $name = sprintf($name[0], $name[2]); 69 else 70 $name = $name[0]; 71 } 72 73 $id = sanitize_title($name); 74 $options = array(); 75 if ( !empty($classname) && is_string($classname) ) 76 $options['classname'] = $classname; 77 $params = array_slice(func_get_args(), 2); 78 $args = array($id, $name, $output_callback, $options); 79 if ( !empty($params) ) 80 $args = array_merge($args, $params); 81 82 call_user_func_array('wp_register_sidebar_widget', $args); 83 } 84 85 function wp_register_sidebar_widget($id, $name, $output_callback, $options = array()) { 86 87 global $wp_registered_widgets, $wp_register_widget_defaults; 88 89 if ( empty($output_callback) ) { 90 unset($wp_registered_widgets[$id]); 91 return; 92 } 93 94 $defaults = array('classname' => $output_callback); 95 $options = wp_parse_args($options, $defaults); 96 $widget = array( 97 'name' => $name, 98 'id' => $id, 99 'callback' => $output_callback, 100 'params' => array_slice(func_get_args(), 4) 101 ); 102 $widget = array_merge($widget, $options); 103 104 if ( is_callable($output_callback) && ( !isset($wp_registered_widgets[$id]) || !$wp_register_widget_defaults) ) 105 $wp_registered_widgets[$id] = $widget; 106 } 107 108 function unregister_sidebar_widget($id) { 109 return wp_unregister_sidebar_widget($id); 110 } 111 112 function wp_unregister_sidebar_widget($id) { 113 wp_register_sidebar_widget($id, '', ''); 114 wp_unregister_widget_control($id); 115 } 116 117 function register_widget_control($name, $control_callback, $width = '', $height = '') { 118 // Compat 119 if ( is_array($name) ) { 120 if ( count($name) == 3 ) 121 $name = sprintf($name[0], $name[2]); 122 else 123 $name = $name[0]; 124 } 125 126 $id = sanitize_title($name); 127 $options = array(); 128 if ( !empty($width) ) 129 $options['width'] = $width; 130 if ( !empty($height) ) 131 $options['height'] = $height; 132 $params = array_slice(func_get_args(), 4); 133 $args = array($id, $name, $control_callback, $options); 134 if ( !empty($params) ) 135 $args = array_merge($args, $params); 136 137 call_user_func_array('wp_register_widget_control', $args); 138 } 139 140 function wp_register_widget_control($id, $name, $control_callback, $options = array()) { 141 global $wp_registered_widget_controls, $wp_register_widget_defaults; 142 143 if ( empty($control_callback) ) { 144 unset($wp_registered_widget_controls[$id]); 145 return; 146 } 147 148 if ( isset($wp_registered_widget_controls[$id]) && $wp_register_widget_defaults ) 149 return; 150 151 $defaults = array('width' => 300, 'height' => 200); 152 $options = wp_parse_args($options, $defaults); 153 $options['width'] = (int) $options['width']; 154 $options['height'] = (int) $options['height']; 155 $options['width'] = $options['width'] > 90 ? $options['width'] + 60 : 360; 156 $options['height'] = $options['height'] > 60 ? $options['height'] + 40 : 240; 157 158 $widget = array( 159 'name' => $name, 160 'id' => $id, 161 'callback' => $control_callback, 162 'params' => array_slice(func_get_args(), 4) 163 ); 164 $widget = array_merge($widget, $options); 165 166 $wp_registered_widget_controls[$id] = $widget; 167 } 168 169 function unregister_widget_control($id) { 170 return wp_unregister_widget_control($id); 171 } 172 173 function wp_unregister_widget_control($id) { 174 return wp_register_widget_control($id, '', ''); 175 } 176 177 function dynamic_sidebar($index = 1) { 178 global $wp_registered_sidebars, $wp_registered_widgets; 179 180 if ( is_int($index) ) { 181 $index = "sidebar-$index"; 182 } else { 183 $index = sanitize_title($index); 184 foreach ( $wp_registered_sidebars as $key => $value ) { 185 if ( sanitize_title($value['name']) == $index ) { 186 $index = $key; 187 break; 188 } 189 } 190 } 191 192 $sidebars_widgets = wp_get_sidebars_widgets(); 193 194 if ( empty($wp_registered_sidebars[$index]) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) ) 195 return false; 196 197 $sidebar = $wp_registered_sidebars[$index]; 198 199 $did_one = false; 200 foreach ( $sidebars_widgets[$index] as $id ) { 201 $callback = $wp_registered_widgets[$id]['callback']; 202 203 $params = array_merge(array($sidebar), (array) $wp_registered_widgets[$id]['params']); 204 205 // Substitute HTML id and class attributes into before_widget 206 $classname_ = ''; 207 foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) { 208 if ( is_string($cn) ) 209 $classname_ .= '_' . $cn; 210 elseif ( is_object($cn) ) 211 $classname_ .= '_' . get_class($cn); 212 } 213 $classname_ = ltrim($classname_, '_'); 214 $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_); 215 216 if ( is_callable($callback) ) { 217 call_user_func_array($callback, $params); 218 $did_one = true; 219 } 220 } 221 222 return $did_one; 223 } 224 225 function is_active_widget($callback) { 226 global $wp_registered_widgets; 227 228 $sidebars_widgets = wp_get_sidebars_widgets(false); 229 230 if ( is_array($sidebars_widgets) ) foreach ( $sidebars_widgets as $sidebar => $widgets ) 231 if ( is_array($widgets) ) foreach ( $widgets as $widget ) 232 if ( $wp_registered_widgets[$widget]['callback'] == $callback ) 233 return true; 234 235 return false; 236 } 237 238 function is_dynamic_sidebar() { 239 global $wp_registered_widgets, $wp_registered_sidebars; 240 $sidebars_widgets = get_option('sidebars_widgets'); 241 foreach ( $wp_registered_sidebars as $index => $sidebar ) { 242 if ( count($sidebars_widgets[$index]) ) { 243 foreach ( $sidebars_widgets[$index] as $widget ) 244 if ( array_key_exists($widget, $wp_registered_widgets) ) 245 return true; 246 } 247 } 248 return false; 249 } 250 251 /* Internal Functions */ 252 253 function wp_get_sidebars_widgets($update = true) { 254 global $wp_registered_widgets, $wp_registered_sidebars; 255 256 $sidebars_widgets = get_option('sidebars_widgets'); 257 $_sidebars_widgets = array(); 258 259 if ( !isset($sidebars_widgets['array_version']) ) 260 $sidebars_widgets['array_version'] = 1; 261 262 switch ( $sidebars_widgets['array_version'] ) { 263 case 1 : 264 foreach ( $sidebars_widgets as $index => $sidebar ) 265 if ( is_array($sidebar) ) 266 foreach ( $sidebar as $i => $name ) { 267 $id = strtolower($name); 268 if ( isset($wp_registered_widgets[$id]) ) { 269 $_sidebars_widgets[$index][$i] = $id; 270 continue; 271 } 272 $id = sanitize_title($name); 273 if ( isset($wp_registered_widgets[$id]) ) { 274 $_sidebars_widgets[$index][$i] = $id; 275 continue; 276 } 277 unset($_sidebars_widgets[$index][$i]); 278 } 279 $_sidebars_widgets['array_version'] = 2; 280 $sidebars_widgets = $_sidebars_widgets; 281 unset($_sidebars_widgets); 282 283 case 2 : 284 $sidebars = array_keys( $wp_registered_sidebars ); 285 if ( !empty( $sidebars ) ) { 286 // Move the known-good ones first 287 foreach ( $sidebars as $id ) { 288 if ( array_key_exists( $id, $sidebars_widgets ) ) { 289 $_sidebars_widgets[$id] = $sidebars_widgets[$id]; 290 unset($sidebars_widgets[$id], $sidebars[$id]); 291 } 292 } 293 294 // Assign to each unmatched registered sidebar the first available orphan 295 unset( $sidebars_widgets[ 'array_version' ] ); 296 while ( ( $sidebar = array_shift( $sidebars ) ) && $widgets = array_shift( $sidebars_widgets ) ) 297 $_sidebars_widgets[ $sidebar ] = $widgets; 298 299 $_sidebars_widgets['array_version'] = 3; 300 $sidebars_widgets = $_sidebars_widgets; 301 unset($_sidebars_widgets); 302 } 303 304 if ( $update ) 305 update_option('sidebars_widgets', $sidebars_widgets); 306 } 307 308 unset($sidebars_widgets['array_version']); 309 310 return $sidebars_widgets; 311 } 312 313 function wp_set_sidebars_widgets( $sidebars_widgets ) { 314 update_option( 'sidebars_widgets', $sidebars_widgets ); 315 } 316 317 function wp_get_widget_defaults() { 318 global $wp_registered_sidebars; 319 320 $defaults = array(); 321 322 foreach ( $wp_registered_sidebars as $index => $sidebar ) 323 $defaults[$index] = array(); 324 325 return $defaults; 326 } 327 328 /* Default Widgets */ 329 330 function wp_widget_pages( $args ) { 331 extract( $args ); 332 $options = get_option( 'widget_pages' ); 333 334 $title = empty( $options['title'] ) ? __( 'Pages' ) : $options['title']; 335 $sortby = empty( $options['sortby'] ) ? 'menu_order' : $options['sortby']; 336 $exclude = empty( $options['exclude'] ) ? '' : $options['exclude']; 337 338 if ( $sortby == 'menu_order' ) { 339 $sortby = 'menu_order, post_title'; 340 } 341 342 $out = wp_list_pages( array('title_li' => '', 'echo' => 0, 'sort_column' => $sortby, 'exclude' => $exclude) ); 343 344 if ( !empty( $out ) ) { 345 ?> 346 <?php echo $before_widget; ?> 347 <?php echo $before_title . $title . $after_title; ?> 348 <ul> 349 <?php echo $out; ?> 350 </ul> 351 <?php echo $after_widget; ?> 352 <?php 353 } 354 } 355 356 function wp_widget_pages_control() { 357 $options = $newoptions = get_option('widget_pages'); 358 if ( $_POST['pages-submit'] ) { 359 $newoptions['title'] = strip_tags(stripslashes($_POST['pages-title'])); 360 361 $sortby = stripslashes( $_POST['pages-sortby'] ); 362 363 if ( in_array( $sortby, array( 'post_title', 'menu_order', 'ID' ) ) ) { 364 $newoptions['sortby'] = $sortby; 365 } else { 366 $newoptions['sortby'] = 'menu_order'; 367 } 368 369 $newoptions['exclude'] = strip_tags( stripslashes( $_POST['pages-exclude'] ) ); 370 } 371 if ( $options != $newoptions ) { 372 $options = $newoptions; 373 update_option('widget_pages', $options); 374 } 375 $title = attribute_escape($options['title']); 376 $exclude = attribute_escape( $options['exclude'] ); 377 ?> 378 <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> 379 <p><label for="pages-sortby"><?php _e( 'Sort by:' ); ?> 380 <select name="pages-sortby" id="pages-sortby"> 381 <option value="post_title"<?php selected( $options['sortby'], 'post_title' ); ?>><?php _e('Page title'); ?></option> 382 <option value="menu_order"<?php selected( $options['sortby'], 'menu_order' ); ?>><?php _e('Page order'); ?></option> 383 <option value="ID"<?php selected( $options['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option> 384 </select></label></p> 385 <p><label for="pages-exclude"><?php _e( 'Exclude:' ); ?> <input type="text" value="<?php echo $exclude; ?>" name="pages-exclude" id="pages-exclude" style="width: 180px;" /></label><br /> 386 <small><?php _e( 'Page IDs, separated by commas.' ); ?></small></p> 387 <input type="hidden" id="pages-submit" name="pages-submit" value="1" /> 388 <?php 389 } 390 391 function wp_widget_links($args) { 392 extract($args, EXTR_SKIP); 393 wp_list_bookmarks(array( 394 'title_before' => $before_title, 'title_after' => $after_title, 395 'category_before' => $before_widget, 'category_after' => $after_widget, 396 'show_images' => true, 'class' => 'linkcat widget' 397 )); 398 } 399 400 function wp_widget_search($args) { 401 extract($args); 402 ?> 403 <?php echo $before_widget; ?> 404 <form id="searchform" method="get" action="<?php bloginfo('home'); ?>"> 405 <div> 406 <input type="text" name="s" id="s" size="15" /><br /> 407 <input type="submit" value="<?php echo attribute_escape(__('Search')); ?>" /> 408 </div> 409 </form> 410 <?php echo $after_widget; ?> 411 <?php 412 } 413 414 function wp_widget_archives($args) { 415 extract($args); 416 $options = get_option('widget_archives'); 417 $c = $options['count'] ? '1' : '0'; 418 $d = $options['dropdown'] ? '1' : '0'; 419 $title = empty($options['title']) ? __('Archives') : $options['title']; 420 421 echo $before_widget; 422 echo $before_title . $title . $after_title; 423 424 if($d) { 425 ?> 426 <select name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> <option value=""><?php echo attribute_escape(__('Select Month')); ?></option> <?php wp_get_archives("type=monthly&format=option&show_post_count=$c"); ?> </select> 427 <?php 428 } else { 429 ?> 430 <ul> 431 <?php wp_get_archives("type=monthly&show_post_count=$c"); ?> 432 </ul> 433 <?php 434 } 435 436 echo $after_widget; 437 } 438 439 function wp_widget_archives_control() { 440 $options = $newoptions = get_option('widget_archives'); 441 if ( $_POST["archives-submit"] ) { 442 $newoptions['count'] = isset($_POST['archives-count']); 443 $newoptions['dropdown'] = isset($_POST['archives-dropdown']); 444 $newoptions['title'] = strip_tags(stripslashes($_POST["archives-title"])); 445 } 446 if ( $options != $newoptions ) { 447 $options = $newoptions; 448 update_option('widget_archives', $options); 449 } 450 $count = $options['count'] ? 'checked="checked"' : ''; 451 $dropdown = $options['dropdown'] ? 'checked="checked"' : ''; 452 $title = attribute_escape($options['title']); 453 ?> 454 <p><label for="archives-title"><?php _e('Title:'); ?> <input style="width: 250px;" id="archives-title" name="archives-title" type="text" value="<?php echo