+
+
+
channel['link']));
+ while ( strstr($link, 'http') != $link )
+ $link = substr($link, 1);
+ }
+ }
+
+ return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' );
}
function wp_widget_rss_register() {
- $options = get_option('widget_rss');
- $number = $options['number'];
- if ( $number < 1 ) $number = 1;
- if ( $number > 9 ) $number = 9;
- $dims = array('width' => 410, 'height' => 200);
- $class = array('classname' => 'widget_rss');
- for ($i = 1; $i <= 9; $i++) {
- $name = sprintf(__('RSS %d'), $i);
- $id = "rss-$i"; // Never never never translate an id
- wp_register_sidebar_widget($id, $name, $i <= $number ? 'wp_widget_rss' : /* unregister */ '', $class, $i);
- wp_register_widget_control($id, $name, $i <= $number ? 'wp_widget_rss_control' : /* unregister */ '', $dims, $i);
+ if ( !$options = get_option('widget_rss') )
+ $options = array();
+ $widget_ops = array('classname' => 'widget_rss', 'description' => __( 'Entries from any RSS or Atom feed' ));
+ $control_ops = array('width' => 400, 'height' => 200, 'id_base' => 'rss');
+ $name = __('RSS');
+
+ $id = false;
+ foreach ( array_keys($options) as $o ) {
+ // Old widgets can have null values for some reason
+ if ( !isset($options[$o]['url']) || !isset($options[$o]['title']) || !isset($options[$o]['items']) )
+ continue;
+ $id = "rss-$o"; // Never never never translate an id
+ wp_register_sidebar_widget($id, $name, 'wp_widget_rss', $widget_ops, array( 'number' => $o ));
+ wp_register_widget_control($id, $name, 'wp_widget_rss_control', $control_ops, array( 'number' => $o ));
+ }
+
+ // If there are none, we register the widget's existance with a generic template
+ if ( !$id ) {
+ wp_register_sidebar_widget( 'rss-1', $name, 'wp_widget_rss', $widget_ops, array( 'number' => -1 ) );
+ wp_register_widget_control( 'rss-1', $name, 'wp_widget_rss_control', $control_ops, array( 'number' => -1 ) );
}
- add_action('sidebar_admin_setup', 'wp_widget_rss_setup');
- add_action('sidebar_admin_page', 'wp_widget_rss_page');
}
function wp_widget_tag_cloud($args) {
@@ -1108,7 +1346,7 @@ function wp_widget_tag_cloud_control() {
$title = attribute_escape( $options['title'] );
?>
-
+
90, 'width' => 300 );
- $dims100 = array( 'height' => 100, 'width' => 300 );
- $dims150 = array( 'height' => 150, 'width' => 300 );
+ $widget_ops = array('classname' => 'widget_pages', 'description' => __( "Your blog's WordPress Pages") );
+ wp_register_sidebar_widget('pages', __('Pages'), 'wp_widget_pages', $widget_ops);
+ wp_register_widget_control('pages', __('Pages'), 'wp_widget_pages_control' );
- $class = array('classname' => 'widget_pages');
- wp_register_sidebar_widget('pages', __('Pages'), 'wp_widget_pages', $class);
- wp_register_widget_control('pages', __('Pages'), 'wp_widget_pages_control', $dims150);
+ $widget_ops = array('classname' => 'widget_calendar', 'description' => __( "A calendar of your blog's posts") );
+ wp_register_sidebar_widget('calendar', __('Calendar'), 'wp_widget_calendar', $widget_ops);
+ wp_register_widget_control('calendar', __('Calendar'), 'wp_widget_calendar_control' );
- $class['classname'] = 'widget_calendar';
- wp_register_sidebar_widget('calendar', __('Calendar'), 'wp_widget_calendar', $class);
- wp_register_widget_control('calendar', __('Calendar'), 'wp_widget_calendar_control', $dims90);
+ $widget_ops = array('classname' => 'widget_archive', 'description' => __( "A monthly archive of your blog's posts") );
+ wp_register_sidebar_widget('archives', __('Archives'), 'wp_widget_archives', $widget_ops);
+ wp_register_widget_control('archives', __('Archives'), 'wp_widget_archives_control' );
- $class['classname'] = 'widget_archives';
- wp_register_sidebar_widget('archives', __('Archives'), 'wp_widget_archives', $class);
- wp_register_widget_control('archives', __('Archives'), 'wp_widget_archives_control', $dims100);
+ $widget_ops = array('classname' => 'widget_links', 'description' => __( "Your blogroll") );
+ wp_register_sidebar_widget('links', __('Links'), 'wp_widget_links', $widget_ops);
- $class['classname'] = 'widget_links';
- wp_register_sidebar_widget('links', __('Links'), 'wp_widget_links', $class);
+ $widget_ops = array('classname' => 'widget_meta', 'description' => __( "Log in/out, admin, feed and WordPress links") );
+ wp_register_sidebar_widget('meta', __('Meta'), 'wp_widget_meta', $widget_ops);
+ wp_register_widget_control('meta', __('Meta'), 'wp_widget_meta_control' );
- $class['classname'] = 'widget_meta';
- wp_register_sidebar_widget('meta', __('Meta'), 'wp_widget_meta', $class);
- wp_register_widget_control('meta', __('Meta'), 'wp_widget_meta_control', $dims90);
+ $widget_ops = array('classname' => 'widget_search', 'description' => __( "A search form for your blog") );
+ wp_register_sidebar_widget('search', __('Search'), 'wp_widget_search', $widget_ops);
- $class['classname'] = 'widget_search';
- wp_register_sidebar_widget('search', __('Search'), 'wp_widget_search', $class);
+ $widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your blog") );
+ wp_register_sidebar_widget('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries', $widget_ops);
+ wp_register_widget_control('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries_control' );
- $class['classname'] = 'widget_recent_entries';
- wp_register_sidebar_widget('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries', $class);
- wp_register_widget_control('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries_control', $dims90);
-
- $class['classname'] = 'widget_tag_cloud';
- wp_register_sidebar_widget('tag_cloud', __('Tag Cloud'), 'wp_widget_tag_cloud', $class);
- wp_register_widget_control('tag_cloud', __('Tag Cloud'), 'wp_widget_tag_cloud_control', 'width=300&height=160');
+ $widget_ops = array('classname' => 'widget_tag_cloud', 'description' => __( "Your most used tags in cloud format") );
+ wp_register_sidebar_widget('tag_cloud', __('Tag Cloud'), 'wp_widget_tag_cloud', $widget_ops);
+ wp_register_widget_control('tag_cloud', __('Tag Cloud'), 'wp_widget_tag_cloud_control' );
wp_widget_categories_register();
wp_widget_text_register();
wp_widget_rss_register();
wp_widget_recent_comments_register();
- $GLOBALS['wp_register_widget_defaults'] = false;
-
do_action('widgets_init');
}
add_action('init', 'wp_widgets_init', 1);
+/* Pattern for multi-widget (allows multiple instances such as the text widget).
+
+// Displays widget on blag
+// $widget_args: number
+// number: which of the several widgets of this type do we mean
+function widget_many( $args, $widget_args = 1 ) {
+ extract( $args, EXTR_SKIP );
+ if ( is_numeric($widget_args) )
+ $widget_args = array( 'number' => $widget_args );
+ $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
+ extract( $widget_args, EXTR_SKIP );
+
+ // Data should be stored as array: array( number => data for that instance of the widget, ... )
+ $options = get_option('widget_many');
+ if ( !isset($options[$number]) )
+ return;
+
+ echo $before_widget;
+
+ // Do stuff for this widget, drawing data from $options[$number]
+
+ echo $after_widget;
+}
+
+// Displays form for a particular instance of the widget. Also updates the data after a POST submit
+// $widget_args: number
+// number: which of the several widgets of this type do we mean
+function widget_many_control( $widget_args = 1 ) {
+ global $wp_registered_widgets;
+ static $updated = false; // Whether or not we have already updated the data after a POST submit
+
+ if ( is_numeric($widget_args) )
+ $widget_args = array( 'number' => $widget_args );
+ $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
+ extract( $widget_args, EXTR_SKIP );
+
+ // Data should be stored as array: array( number => data for that instance of the widget, ... )
+ $options = get_option('widget_many');
+ if ( !is_array($options) )
+ $options = array();
+
+ // We need to update the data
+ if ( !$updated && !empty($_POST['sidebar']) ) {
+ // Tells us what sidebar to put the data in
+ $sidebar = (string) $_POST['sidebar'];
+
+ $sidebars_widgets = wp_get_sidebars_widgets();
+ if ( isset($sidebars_widgets[$sidebar]) )
+ $this_sidebar =& $sidebars_widgets[$sidebar];
+ else
+ $this_sidebar = array();
+
+ foreach ( $this_sidebar as $_widget_id ) {
+ // Remove all widgets of this type from the sidebar. We'll add the new data in a second. This makes sure we don't get any duplicate data
+ // since widget ids aren't necessarily persistent across multiple updates
+ if ( 'widget_many' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
+ $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
+ if ( !in_array( "many-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed. "many-$widget_number" is "{id_base}-{widget_number}
+ unset($options[$widget_number]);
+ }
+ }
+
+ foreach ( (array) $_POST['widget-many'] as $widget_number => $widget_many_instance ) {
+ // compile data from $widget_many_instance
+ $something = wp_specialchars( $widget_many_instance['something'] );
+ $options[$widget_number] = array( 'something' => $something ); // Even simple widgets should store stuff in array, rather than in scalar
+ }
+
+ update_option('widget_text', $options);
+
+ $updated = true; // So that we don't go through this more than once
+ }
+
+
+ // Here we echo out the form
+ if ( -1 == $number ) { // We echo out a template for a form which can be converted to a specific form later via JS
+ $something = '';
+ $number = '%i%';
+ } else {
+ $something = attribute_escape($options[$number]['something']);
+ }
+
+ // The form has inputs with names like widget-many[$number][something] so that all data for that instance of
+ // the widget are stored in one $_POST variable: $_POST['widget-many'][$number]
+?>
+
+
+
+
+ 'widget_many', 'description' => __('Widget which allows multiple instances'));
+ $control_ops = array('width' => 400, 'height' => 350, 'id_base' => 'many');
+ $name = __('Many');
+
+ $registered = false;
+ foreach ( array_keys($options) as $o ) {
+ // Old widgets can have null values for some reason
+ if ( !isset($options[$o]['something']) ) // we used 'something' above in our exampple. Replace with with whatever your real data are.
+ continue;
+
+ // $id should look like {$id_base}-{$o}
+ $id = "many-$o"; // Never never never translate an id
+ $registered = true;
+ wp_register_sidebar_widget( $id, $name, 'wp_widget_text', $widget_ops, array( 'number' => $o ) );
+ wp_register_widget_control( $id, $name, 'wp_widget_text_control', $control_ops, array( 'number' => $o ) );
+ }
+
+ // If there are none, we register the widget's existance with a generic template
+ if ( !$registered ) {
+ wp_register_sidebar_widget( 'many-1', $name, 'widget_many', $widget_ops, array( 'number' => -1 ) );
+ wp_register_widget_control( 'many-1', $name, 'widget_many_control', $control_ops, array( 'number' => -1 ) );
+ }
+}
+
+// This is important
+add_action( 'widgets_init', 'widget_many_register' )
+
+*/
+
?>
--
cgit