class Carousel_Widget extends WP_Widget { function Carousel_Widget() { if ( !session_id() ) { session_start(); } $widget_ops = array('classname' => 'widget_carousel', 'description' => 'A widget that contains rotating widgets' ); $this->WP_Widget('widget_carousel', 'Carousel of Widgets', $widget_ops); } function widget($args, $instance) { global $wp_registered_widgets, $wp_registered_sidebars; $options = get_option("widget_Carousel_widget"); $sidebars_widgets = wp_get_sidebars_widgets(); $curSidebar = ''; if ( !isset($_SESSION['default']['curCarouselWidgetName']) ) { $_SESSION['default']['curCarouselWidgetName'] = ''; $curWidgetName = ''; } else { $curWidgetName = $_SESSION['default']['curCarouselWidgetName']; } $activeWidgets = array(); $c = 0; $n = 0; foreach ( $options as $key => $value ) { if ( ($key == 'widget_Carousel_display') || ($value == 'none') ) { continue; } if ( ($options['widget_Carousel_display']=='sequence') || ( ($options['widget_Carousel_display']=='random') && ($value != $_SESSION['default']['curCarouselWidgetName']) ) ) { $activeWidgets[] = $value; if ( $value == $_SESSION['default']['curCarouselWidgetName'] ) $n = $c; } $c++; } if ( $options['widget_Carousel_display']=='sequence' ) { $n++; if ( $n > count($activeWidgets)-1 ) $n = 0; $curWidgetNum = $n; } else { srand((double) microtime()*1000000); $curWidgetNum = rand(0, count($activeWidgets)-1); } $curWidgetName = $activeWidgets[$curWidgetNum]; $_SESSION['default']['curCarouselWidgetName'] = $curWidgetName; // Find the sidebar foreach ( $sidebars_widgets as $sidebar => $widgets ) { if ( $sidebar=='wp_inactive_widgets' ) { continue; } foreach ( $widgets as $widget ) { if ( strpos($widget, 'widget_carousel')!==FALSE ) { $curSidebar = $sidebar; break; } } if ( $curSidebar != '' ) { break; } } $params = array_merge( array( array_merge($wp_registered_sidebars[$curSidebar], array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$curWidgetName]['name']) ) ), (array) $wp_registered_widgets[$curWidgetName]['params'] ); // Substitute HTML id and class attributes into before_widget $classname_ = ''; foreach ( (array) $wp_registered_widgets[$curWidgetName]['classname'] as $cn ) { if ( is_string($cn) ) $classname_ .= '_' . $cn; elseif ( is_object($cn) ) $classname_ .= '_' . get_class($cn); } $classname_ = ltrim($classname_, '_'); $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_); $params = apply_filters( 'dynamic_sidebar_params', $params ); $callback = $wp_registered_widgets[$curWidgetName]['callback']; if ( is_callable($callback) ) call_user_func_array($callback, $params); } function update($new_instance, $old_instance) { for ( $i=1; $i < 11; $i++ ) { $options['widget_Carousel_display'] = $_POST['display_sequence']; if ( $_POST["widget_Carousel_widget$i"] ) { $options["widget_Carousel_widget$i"] = $_POST["widget_Carousel_widget$i"]; update_option("widget_Carousel_widget", $options); } } } function form($instance) { global $wp_registered_widgets, $wp_registered_widget_controls; $options = get_option("widget_Carousel_widget"); $availWidgets = array(); echo 'Display: '; if ( $options['widget_Carousel_display'] == 'random' ) { echo ' Randomly'; echo ' Sequential'; } else { echo ' Randomly'; echo ' Sequential'; } echo '
| $i. |