1 ) {
$_args['name'] = isset($args['name']) ? $args['name'] : sprintf(__('Sidebar %d'), $i);
} else {
$_args['name'] = isset($args['name']) ? $args['name'] : __('Sidebar');
}
$_args['id'] = isset($args['id']) ? $args['id'] : "sidebar-$i";
register_sidebar($_args);
++$i;
}
}
function register_sidebar($args = array()) {
global $wp_registered_sidebars;
if ( is_string($args) )
parse_str($args, $args);
$i = count($wp_registered_sidebars) + 1;
$defaults = array(
'name' => sprintf(__('Sidebar %d'), $i ),
'id' => "sidebar-$i",
'before_widget' => '
',
'after_widget' => "\n",
'before_title' => '\n",
);
$sidebar = array_merge($defaults, $args);
$wp_registered_sidebars[$sidebar['id']] = $sidebar;
return $sidebar['id'];
}
function unregister_sidebar( $name ) {
global $wp_registered_sidebars;
if ( isset( $wp_registered_sidebars[$name] ) )
unset( $wp_registered_sidebars[$name] );
}
function register_sidebar_widget($name, $output_callback, $classname = '', $id = '') {
global $wp_registered_widgets, $wp_register_widget_defaults;
// Compat
if ( is_array($name) ) {
if ( count($name) == 3 )
$name = sprintf($name[0], $name[2]);
else
$name = $name[0];
}
// Last resort -- this can be broken when names get translated so please provide a unique id.
if ( empty($id) )
$id = sanitize_title($name);
if ( (!isset($classname) || empty($classname) || !is_string($classname)) && is_string($output_callback) )
$classname = $output_callback;
$widget = array(
'name' => $name,
'id' => $id,
'callback' => $output_callback,
'classname' => $classname,
'params' => array_slice(func_get_args(), 4)
);
if ( empty($output_callback) )
unset($wp_registered_widgets[$id]);
elseif ( is_callable($output_callback) && ( !isset($wp_registered_widgets[$id]) || !$wp_register_widget_defaults) )
$wp_registered_widgets[$id] = $widget;
}
function unregister_sidebar_widget($id) {
$id = sanitize_title($id);
register_sidebar_widget('', '', '', $id);
unregister_widget_control($id);
}
function register_widget_control($name, $control_callback, $width = 300, $height = 200, $id = '') {
global $wp_registered_widget_controls, $wp_register_widget_defaults;
// Compat
if ( is_array($name) ) {
if ( count($name) == 3 )
$name = sprintf($name[0], $name[2]);
else
$name = $name[0];
}
if ( empty($id) )
$id = $name;
$id = sanitize_title($id);
$width = (int) $width > 90 ? (int) $width + 60 : 360;
$height = (int) $height > 60 ? (int) $height + 40 : 240;
if ( empty($control_callback) )
unset($wp_registered_widget_controls[$id]);
elseif ( !isset($wp_registered_widget_controls[$id]) || !$wp_register_widget_defaults )
$wp_registered_widget_controls[$id] = array(
'name' => $name,
'id' => $id,
'callback' => $control_callback,
'width' => $width,
'height' => $height,
'params' => array_slice(func_get_args(), 5)
);
}
function unregister_widget_control($id) {
$id = sanitize_title($id);
return register_widget_control($id, '');
}
function dynamic_sidebar($index = 1) {
global $wp_registered_sidebars, $wp_registered_widgets;
if ( is_int($index) ) {
$index = "sidebar-$index";
} else {
$index = sanitize_title($index);
}
$sidebars_widgets = wp_get_sidebars_widgets();
if ( empty($wp_registered_sidebars[$index]) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) )
return false;
$sidebar = $wp_registered_sidebars[$index];
$did_one = false;
foreach ( $sidebars_widgets[$index] as $id ) {
$callback = $wp_registered_widgets[$id]['callback'];
$params = array_merge(array($sidebar), (array) $wp_registered_widgets[$id]['params']);
// Substitute HTML id and class attributes into before_widget
$params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $wp_registered_widgets[$id]['classname']);
if ( is_callable($callback) ) {
call_user_func_array($callback, $params);
$did_one = true;
}
}
return $did_one;
}
function is_active_widget($callback) {
global $wp_registered_widgets;
$sidebars_widgets = wp_get_sidebars_widgets(false);
if ( is_array($sidebars_widgets) ) foreach ( $sidebars_widgets as $sidebar => $widgets )
if ( is_array($widgets) ) foreach ( $widgets as $widget )
if ( $wp_registered_widgets[$widget]['callback'] == $callback )
return true;
return false;
}
function is_dynamic_sidebar() {
global $wp_registered_widgets, $wp_registered_sidebars;
$sidebars_widgets = get_option('sidebars_widgets');
foreach ( $wp_registered_sidebars as $index => $sidebar ) {
if ( count($sidebars_widgets[$index]) ) {
foreach ( $sidebars_widgets[$index] as $widget )
if ( array_key_exists($widget, $wp_registered_widgets) )
return true;
}
}
return false;
}
/* Internal Functions */
function wp_get_sidebars_widgets($update = true) {
global $wp_registered_widgets, $wp_registered_sidebars;
$sidebars_widgets = get_option('sidebars_widgets');
$_sidebars_widgets = array();
if ( !isset($sidebars_widgets['array_version']) )
$sidebars_widgets['array_version'] = 1;
switch ( $sidebars_widgets['array_version'] ) {
case 1 :
foreach ( $sidebars_widgets as $index => $sidebar )
if ( is_array($sidebar) )
foreach ( $sidebar as $i => $name ) {
$id = strtolower($name);
if ( isset($wp_registered_widgets[$id]) ) {
$_sidebars_widgets[$index][$i] = $id;
continue;
}
$id = sanitize_title($name);
if ( isset($wp_registered_widgets[$id]) ) {
$_sidebars_widgets[$index][$i] = $id;
continue;
}
unset($_sidebars_widgets[$index][$i]);
}
$_sidebars_widgets['array_version'] = 2;
$sidebars_widgets = $_sidebars_widgets;
unset($_sidebars_widgets);
case 2 :
$sidebars = array_keys( $wp_registered_sidebars );
if ( !empty( $sidebars ) ) {
// Move the known-good ones first
foreach ( $sidebars as $id ) {
if ( array_key_exists( $id, $sidebars_widgets ) ) {
$_sidebars_widgets[$id] = $sidebars_widgets[$id];
unset($sidebars_widgets[$id], $sidebars[$id]);
}
}
// Assign to each unmatched registered sidebar the first available orphan
unset( $sidebars_widgets[ 'array_version' ] );
while ( ( $sidebar = array_shift( $sidebars ) ) && $widgets = array_shift( $sidebars_widgets ) )
$_sidebars_widgets[ $sidebar ] = $widgets;
$_sidebars_widgets['array_version'] = 3;
$sidebars_widgets = $_sidebars_widgets;
unset($_sidebars_widgets);
}
if ( $update )
update_option('sidebars_widgets', $sidebars_widgets);
}
unset($sidebars_widgets['array_version']);
return $sidebars_widgets;
}
function wp_set_sidebars_widgets( $sidebars_widgets ) {
update_option( 'sidebars_widgets', $sidebars_widgets );
}
function wp_get_widget_defaults() {
global $wp_registered_sidebars;
$defaults = array();
foreach ( $wp_registered_sidebars as $index => $sidebar )
$defaults[$index] = array();
return $defaults;
}
/* Default Widgets */
function wp_widget_pages($args) {
extract($args);
$options = get_option('widget_pages');
$title = empty($options['title']) ? __('Pages') : $options['title'];
echo $before_widget . $before_title . $title . $after_title . "\n";
wp_list_pages("title_li=");
echo "
\n" . $after_widget;
}
function wp_widget_pages_control() {
$options = $newoptions = get_option('widget_pages');
if ( $_POST["pages-submit"] ) {
$newoptions['title'] = strip_tags(stripslashes($_POST["pages-title"]));
}
if ( $options != $newoptions ) {
$options = $newoptions;
update_option('widget_pages', $options);
}
$title = htmlspecialchars($options['title'], ENT_QUOTES);
?>
$before_title, 'title_after' => $after_title,
'category_before' => $before_widget, 'category_after' => $after_widget,
'show_images' => true, 'class' => 'linkcat widget'
));
}
}
function wp_widget_search($args) {
extract($args);
?>
';
get_calendar();
echo '';
echo $after_widget;
}
function wp_widget_calendar_control() {
$options = $newoptions = get_option('widget_calendar');
if ( $_POST["calendar-submit"] ) {
$newoptions['title'] = strip_tags(stripslashes($_POST["calendar-title"]));
}
if ( $options != $newoptions ) {
$options = $newoptions;
update_option('widget_calendar', $options);
}
$title = htmlspecialchars($options['title'], ENT_QUOTES);
?>
" name="text-title-" type="text" value="" />
" name="text-submit-" value="1" />
9 ) $number = 9;
if ( $number < 1 ) $number = 1;
$newoptions['number'] = $number;
}
if ( $options != $newoptions ) {
$options = $newoptions;
update_option('widget_text', $options);
wp_widget_text_register($options['number']);
}
}
function wp_widget_text_page() {
$options = $newoptions = get_option('widget_text');
?>
9 ) $number = 9;
for ($i = 1; $i <= 9; $i++) {
$name = sprintf(__('Text %d'), $i);
$id = "text-$i"; // Never never never translate an id
register_sidebar_widget($name, $i <= $number ? 'wp_widget_text' : /* unregister */ '', 'widget_text', $id, $i);
register_widget_control($name, $i <= $number ? 'wp_widget_text_control' : /* unregister */ '', 460, 350, $id, $i);
}
add_action('sidebar_admin_setup', 'wp_widget_text_setup');
add_action('sidebar_admin_page', 'wp_widget_text_page');
}
function wp_widget_categories($args) {
extract($args);
$options = get_option('widget_categories');
$c = $options['count'] ? '1' : '0';
$h = $options['hierarchical'] ? '1' : '0';
$d = $options['dropdown'] ? '1' : '0';
$title = empty($options['title']) ? __('Categories') : $options['title'];
echo $before_widget;
echo $before_title . $title . $after_title;
$cat_args = "orderby=name&show_count={$c}&hierarchical={$h}";
if($d) {
wp_dropdown_categories($cat_args);
?>
15 )
$number = 15;
$r = new WP_Query("showposts=$number&what_to_show=posts&nopaging=0");
if ($r->have_posts()) :
?>
have_posts()) : $r->the_post(); ?>
-
15 )
$number = 15;
if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) {
$comments = $wpdb->get_results("SELECT comment_author, comment_author_url, comment_ID, comment_post_ID FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number");
wp_cache_add( 'recent_comments', $comments, 'widget' );
}
?>
10 ) $num_items = 10;
$url = $options[$number]['url'];
while ( strstr($url, 'http') != $url )
$url = substr($url, 1);
if ( empty($url) )
return;
$rss = fetch_rss_summary($url, array( 'link', 'title', 'description' ) );
$link = wp_specialchars(strip_tags($rss->channel['link']), 1);
while ( strstr($link, 'http') != $link )
$link = substr($link, 1);
$desc = wp_specialchars(strip_tags(html_entity_decode($rss->channel['description'], ENT_QUOTES)), 1);
$title = $options[$number]['title'];
if ( empty($title) )
$title = htmlentities(strip_tags($rss->channel['title']));
if ( empty($title) )
$title = $desc;
if ( empty($title) )
$title = __('Unknown Feed');
$url = wp_specialchars(strip_tags($url), 1);
if ( file_exists(dirname(__FILE__) . '/rss.png') )
$icon = str_replace(ABSPATH, get_option('siteurl').'/', dirname(__FILE__)) . '/rss.png';
else
$icon = get_option('siteurl').'/wp-includes/images/rss.png';
$title = " ";
?>
items ) ) {
$rss->items = array_slice($rss->items, 0, $num_items);
foreach ($rss->items as $item ) {
while ( strstr($item['link'], 'http') != $item['link'] )
$item['link'] = substr($item['link'], 1);
$link = wp_specialchars(strip_tags($item['link']), 1);
$title = wp_specialchars(strip_tags($item['title']), 1);
if ( empty($title) )
$title = __('Untitled');
$desc = '';
if ( $show_summary ) {
$summary = '';
} else {
if ( isset( $item['description'] ) && is_string( $item['description'] ) )
$desc = str_replace(array("\n", "\r"), ' ', wp_specialchars(strip_tags(html_entity_decode($item['description'], ENT_QUOTES)), 1));
$summary = '';
}
echo "- $summary
";
}
} else {
echo __('- An error has occured; the feed is probably down. Try again later.
');
}
?>
status == 200 ) {
$newoptions[$number]['url'] = $url;
$newoptions[$number]['error'] = false;
} else {
$newoptions[$number]['error'] = true;
$newoptions[$number]['url'] = wp_specialchars(__('Error: could not find an RSS or ATOM feed at that URL.'), 1);
$error = sprintf(__('Error in RSS %1$d: %2$s', 'sandbox'), $number, $newoptions[$number]['error']);
}
}
}
if ( $options != $newoptions ) {
$options = $newoptions;
update_option('widget_rss', $options);
}
$url = htmlspecialchars($options[$number]['url'], ENT_QUOTES);
$items = (int) $options[$number]['items'];
$title = htmlspecialchars($options[$number]['title'], ENT_QUOTES);
if ( empty($items) || $items < 1 ) $items = 10;
?>
" name="rss-url-" type="text" value="" />
" name="rss-title-" type="text" value="" />
" name="rss-submit-" value="1" />
9 ) $number = 9;
if ( $number < 1 ) $number = 1;
$newoptions['number'] = $number;
}
if ( $options != $newoptions ) {
$options = $newoptions;
update_option('widget_rss', $options);
wp_widget_rss_register($options['number']);
}
}
function wp_widget_rss_page() {
$options = $newoptions = get_option('widget_rss');
?>
9 ) $number = 9;
for ($i = 1; $i <= 9; $i++) {
$name = sprintf(__('RSS %d'), $i);
$id = "rss-$i"; // Never never never translate an id
register_sidebar_widget($name, $i <= $number ? 'wp_widget_rss' : /* unregister */ '', 'widget_rss', $id, $i);
register_widget_control($name, $i <= $number ? 'wp_widget_rss_control' : /* unregister */ '', 410, 200, $id, $i);
}
add_action('sidebar_admin_setup', 'wp_widget_rss_setup');
add_action('sidebar_admin_page', 'wp_widget_rss_page');
}
function wp_widgets_init() {
global $wp_register_widget_defaults;
$wp_register_widget_defaults = true;
register_sidebar_widget(__('Pages'), 'wp_widget_pages', 'widget_pages', 'pages');
register_widget_control(__('Pages'), 'wp_widget_pages_control', 300, 90, 'pages');
register_sidebar_widget(__('Calendar'), 'wp_widget_calendar', 'widget_calendar', 'calendar');
register_widget_control(__('Calendar'), 'wp_widget_calendar_control', 300, 90, 'calendar');
register_sidebar_widget(__('Archives'), 'wp_widget_archives', 'widget_archives', 'archives');
register_widget_control(__('Archives'), 'wp_widget_archives_control', 300, 100, 'archives');
register_sidebar_widget(__('Links'), 'wp_widget_links', 'widget_links', 'links');
register_sidebar_widget(__('Meta'), 'wp_widget_meta', 'widget_meta', 'meta');
register_widget_control(__('Meta'), 'wp_widget_meta_control', 300, 90, 'meta');
register_sidebar_widget(__('Search'), 'wp_widget_search', 'widget_search', 'search');
register_sidebar_widget(__('Categories'), 'wp_widget_categories', 'widget_categories', 'categories');
register_widget_control(__('Categories'), 'wp_widget_categories_control', 300, 150, 'categories');
register_sidebar_widget(__('Recent Posts'), 'wp_widget_recent_entries', 'widget_recent_entries', 'recent-posts');
register_widget_control(__('Recent Posts'), 'wp_widget_recent_entries_control', 300, 90, 'recent-posts');
wp_widget_text_register();
wp_widget_rss_register();
wp_widget_recent_comments_register();
$wp_register_widget_defaults = false;
do_action('widgets_init');
}
add_action('init', 'wp_widgets_init', 1);
?>