Convert pages to WP_Widget. see #8441

git-svn-id: http://svn.automattic.com/wordpress/trunk@10785 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2009-03-14 17:24:11 +00:00
parent 06f219dc4f
commit 77518dd6ca
1 changed files with 86 additions and 3 deletions

View File

@ -209,6 +209,7 @@ class WP_Widget {
}
foreach( (array) $_POST['widget-' . $this->id_base] as $number => $new_instance ) {
$new_instance = stripslashes_deep($new_instance);
$this->_set($number);
if ( isset($all_instances[$number]) )
$instance = $this->update($new_instance, $all_instances[$number]);
@ -1004,6 +1005,87 @@ function wp_widget_pages_control() {
<?php
}
/**
* Pages widget class
*
* @since 2.8.0
*/
class WP_Widget_Pages extends WP_Widget {
function WP_Widget_Pages() {
$widget_ops = array('classname' => 'widget_pages', 'description' => __( "Your blog's WordPress Pages") );
$this->WP_Widget('pages', __('Pages'), $widget_ops);
}
function widget( $args, $instance ) {
extract( $args );
$title = empty( $instance['title'] ) ? __( 'Pages' ) : apply_filters('widget_title', $instance['title']);
$sortby = empty( $instance['sortby'] ) ? 'menu_order' : $instance['sortby'];
$exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude'];
if ( $sortby == 'menu_order' )
$sortby = 'menu_order, post_title';
$out = wp_list_pages( array('title_li' => '', 'echo' => 0, 'sort_column' => $sortby, 'exclude' => $exclude) );
if ( !empty( $out ) ) {
echo $before_widget;
echo $before_title . $title . $after_title;
?>
<ul>
<?php echo $out; ?>
</ul>
<?php
echo $after_widget;
}
}
function update( $new_instance, $old_instance ) {
if ( !isset($new_instance['submit']) ) // user clicked cancel?
return false;
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
if ( in_array( $new_instance['sortby'], array( 'post_title', 'menu_order', 'ID' ) ) ) {
$instance['sortby'] = $new_instance['sortby'];
} else {
$instance['sortby'] = 'menu_order';
}
$instance['exclude'] = strip_tags( $new_instance['exclude'] );
return $instance;
}
function form( $instance ) {
//Defaults
$instance = wp_parse_args( (array) $instance, array( 'sortby' => 'post_title', 'title' => '', 'exclude' => '') );
$title = attribute_escape( $instance['title'] );
$exclude = attribute_escape( $instance['exclude'] );
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?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('sortby'); ?>"><?php _e( 'Sort by:' ); ?>
<select name="<?php echo $this->get_field_name('sortby'); ?>" id="<?php echo $this->get_field_id('sortby'); ?>" class="widefat">
<option value="post_title"<?php selected( $instance['sortby'], 'post_title' ); ?>><?php _e('Page title'); ?></option>
<option value="menu_order"<?php selected( $instance['sortby'], 'menu_order' ); ?>><?php _e('Page order'); ?></option>
<option value="ID"<?php selected( $instance['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option>
</select>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('exclude'); ?>"><?php _e( 'Exclude:' ); ?> <input type="text" value="<?php echo $exclude; ?>" name="<?php echo $this->get_field_name('exclude'); ?>" id="<?php echo $this->get_field_id('exclude'); ?>" class="widefat" /></label>
<br />
<small><?php _e( 'Page IDs, separated by commas.' ); ?></small>
</p>
<input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" />
<?php
}
}
/**
* Links widget class
*
@ -2231,9 +2313,10 @@ function wp_widgets_init() {
if ( !is_blog_installed() )
return;
$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' );
//$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' );
new WP_Widget_Pages();
$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);