Move recent posts widget to WP_Widget. see #8441

git-svn-id: http://svn.automattic.com/wordpress/trunk@11087 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
azaozz 2009-04-25 12:43:51 +00:00
parent a4207b955e
commit 143b92ded9
2 changed files with 67 additions and 72 deletions

View File

@ -487,24 +487,31 @@ class WP_Widget_Categories extends WP_Widget {
} }
/** /**
* Display recent entries widget. * Recent_Posts widget class
* *
* @since 2.2.0 * @since 2.8.0
*
* @param array $args Widget arguments.
* @return int Displayed cache.
*/ */
function wp_widget_recent_entries($args) { class WP_Widget_Recent_Posts extends WP_Widget {
if ( '%BEG_OF_TITLE%' != $args['before_title'] ) {
if ( $output = wp_cache_get('widget_recent_entries', 'widget') ) function WP_Widget_Recent_Posts() {
return print($output); $widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your blog") );
ob_start(); $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') );
} }
function widget($args, $instance) {
if ( $output = wp_cache_get('widget_recent_entries', 'widget') )
return print($output);
ob_start();
extract($args); extract($args);
$options = get_option('widget_recent_entries');
$title = empty($options['title']) ? __('Recent Posts') : apply_filters('widget_title', $options['title']); $title = empty($instance['title']) ? __('Recent Posts') : apply_filters('widget_title', $instance['title']);
if ( !$number = (int) $options['number'] ) if ( !$number = (int) $instance['number'] )
$number = 10; $number = 10;
else if ( $number < 1 ) else if ( $number < 1 )
$number = 1; $number = 1;
@ -526,52 +533,42 @@ function wp_widget_recent_entries($args) {
wp_reset_query(); // Restore global post data stomped by the_post(). wp_reset_query(); // Restore global post data stomped by the_post().
endif; endif;
if ( '%BEG_OF_TITLE%' != $args['before_title'] )
wp_cache_add('widget_recent_entries', ob_get_flush(), 'widget'); wp_cache_add('widget_recent_entries', ob_get_flush(), 'widget');
} }
/** function update( $new_instance, $old_instance ) {
* Remove recent entries widget items cache. $instance = $old_instance;
* $instance['title'] = strip_tags($new_instance['title']);
* @since 2.2.0 $instance['number'] = (int) $new_instance['number'];
*/ $this->flush_widget_cache();
function wp_flush_widget_recent_entries() {
return $instance;
}
function flush_widget_cache() {
wp_cache_delete('widget_recent_entries', 'widget'); wp_cache_delete('widget_recent_entries', 'widget');
}
add_action('save_post', 'wp_flush_widget_recent_entries');
add_action('deleted_post', 'wp_flush_widget_recent_entries');
add_action('switch_theme', 'wp_flush_widget_recent_entries');
/**
* Display and process recent entries widget options form.
*
* @since 2.2.0
*/
function wp_widget_recent_entries_control() {
$options = $newoptions = get_option('widget_recent_entries');
if ( isset($_POST["recent-entries-submit"]) ) {
$newoptions['title'] = strip_tags(stripslashes($_POST["recent-entries-title"]));
$newoptions['number'] = (int) $_POST["recent-entries-number"];
} }
if ( $options != $newoptions ) {
$options = $newoptions; function form( $instance ) {
update_option('widget_recent_entries', $options); $title = attribute_escape($instance['title']);
wp_flush_widget_recent_entries(); if ( !$number = (int) $instance['number'] )
}
$title = attribute_escape($options['title']);
if ( !$number = (int) $options['number'] )
$number = 5; $number = 5;
?> ?>
<p><label for="recent-entries-title"><?php _e('Title:'); ?> <input class="widefat" id="recent-entries-title" name="recent-entries-title" type="text" value="<?php echo $title; ?>" /></label></p>
<p> <p>
<label for="recent-entries-number"><?php _e('Number of posts to show:'); ?> <input style="width: 25px; text-align: center;" id="recent-entries-number" name="recent-entries-number" type="text" value="<?php echo $number; ?>" /></label> <label for="<?php echo $this->get_field_id('title'); ?>">
<br /> <?php _e('Title:'); ?>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('number'); ?>">
<?php _e('Number of posts to show:'); ?>
<input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" />
</label><br />
<small><?php _e('(at most 15)'); ?></small> <small><?php _e('(at most 15)'); ?></small>
</p> </p>
<input type="hidden" id="recent-entries-submit" name="recent-entries-submit" value="1" />
<?php <?php
}
} }
/** /**
@ -1136,9 +1133,7 @@ function wp_widgets_init() {
register_widget('WP_Widget_Categories'); register_widget('WP_Widget_Categories');
$widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your blog") ); register_widget('WP_Widget_Recent_Posts');
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' );
$widget_ops = array('classname' => 'widget_tag_cloud', 'description' => __( "Your most used tags in cloud format") ); $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_sidebar_widget('tag_cloud', __('Tag Cloud'), 'wp_widget_tag_cloud', $widget_ops);

View File

@ -87,9 +87,9 @@ class WP_Widget {
* - height * - height
*/ */
function __construct( $id_base = false, $name, $widget_options = array(), $control_options = array() ) { 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->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->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) ); $this->control_options = wp_parse_args( $control_options, array('id_base' => $this->id_base) );
} }