diff --git a/wp-includes/default-widgets.php b/wp-includes/default-widgets.php index b8b3e9790a..407af8f586 100644 --- a/wp-includes/default-widgets.php +++ b/wp-includes/default-widgets.php @@ -487,91 +487,88 @@ class WP_Widget_Categories extends WP_Widget { } /** - * Display recent entries widget. + * Recent_Posts widget class * - * @since 2.2.0 - * - * @param array $args Widget arguments. - * @return int Displayed cache. + * @since 2.8.0 */ -function wp_widget_recent_entries($args) { - if ( '%BEG_OF_TITLE%' != $args['before_title'] ) { - if ( $output = wp_cache_get('widget_recent_entries', 'widget') ) - return print($output); - ob_start(); +class WP_Widget_Recent_Posts extends WP_Widget { + + function WP_Widget_Recent_Posts() { + $widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your blog") ); + $this->WP_Widget('recent_posts', __('Recent Posts'), $widget_ops); + + add_action( 'save_post', array(&$this, 'flush_widget_cache') ); + add_action( 'deleted_post', array(&$this, 'flush_widget_cache') ); + add_action( 'switch_theme', array(&$this, 'flush_widget_cache') ); } - extract($args); - $options = get_option('widget_recent_entries'); - $title = empty($options['title']) ? __('Recent Posts') : apply_filters('widget_title', $options['title']); - if ( !$number = (int) $options['number'] ) - $number = 10; - else if ( $number < 1 ) - $number = 1; - else if ( $number > 15 ) - $number = 15; + function widget($args, $instance) { + if ( $output = wp_cache_get('widget_recent_entries', 'widget') ) + return print($output); - $r = new WP_Query(array('showposts' => $number, 'what_to_show' => 'posts', 'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1)); - if ($r->have_posts()) : + ob_start(); + + extract($args); + + $title = empty($instance['title']) ? __('Recent Posts') : apply_filters('widget_title', $instance['title']); + if ( !$number = (int) $instance['number'] ) + $number = 10; + else if ( $number < 1 ) + $number = 1; + else if ( $number > 15 ) + $number = 15; + + $r = new WP_Query(array('showposts' => $number, 'what_to_show' => 'posts', 'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1)); + if ($r->have_posts()) : ?> - + - + flush_widget_cache(); + + return $instance; } - $title = attribute_escape($options['title']); - if ( !$number = (int) $options['number'] ) - $number = 5; + + function flush_widget_cache() { + wp_cache_delete('widget_recent_entries', 'widget'); + } + + function form( $instance ) { + $title = attribute_escape($instance['title']); + if ( !$number = (int) $instance['number'] ) + $number = 5; ?> - -

-

- -
- -

- +

+ +

+

+
+ +

'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' ); + register_widget('WP_Widget_Recent_Posts'); $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); diff --git a/wp-includes/widgets.php b/wp-includes/widgets.php index 62fc5c8991..c728fdb060 100644 --- a/wp-includes/widgets.php +++ b/wp-includes/widgets.php @@ -87,9 +87,9 @@ class WP_Widget { * - height */ function __construct( $id_base = false, $name, $widget_options = array(), $control_options = array() ) { - $this->id_base = $id_base === false ? str_replace( 'wp_widget_', '', strtolower(get_class($this)) ) : $id_base; + $this->id_base = empty($id_base) ? preg_replace( '/(wp_)?widget_/', '', strtolower(get_class($this)) ) : $id_base; $this->name = $name; - $this->option_name = 'widget_' . $id_base; + $this->option_name = 'widget_' . $this->id_base; $this->widget_options = wp_parse_args( $widget_options, array('classname' => $this->option_name) ); $this->control_options = wp_parse_args( $control_options, array('id_base' => $this->id_base) ); }