Add sorting and limiting to the links widget. Props yoavf. Fixes #12785.

git-svn-id: http://svn.automattic.com/wordpress/trunk@20071 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
duck_ 2012-03-02 13:58:58 +00:00
parent 86577f34ea
commit aea371da0d
1 changed files with 31 additions and 6 deletions

View File

@ -102,6 +102,9 @@ class WP_Widget_Links extends WP_Widget {
$show_rating = isset($instance['rating']) ? $instance['rating'] : false; $show_rating = isset($instance['rating']) ? $instance['rating'] : false;
$show_images = isset($instance['images']) ? $instance['images'] : true; $show_images = isset($instance['images']) ? $instance['images'] : true;
$category = isset($instance['category']) ? $instance['category'] : false; $category = isset($instance['category']) ? $instance['category'] : false;
$orderby = isset( $instance['orderby'] ) ? $instance['orderby'] : 'name';
$order = $orderby == 'rating' ? 'DESC' : 'ASC';
$limit = isset( $instance['limit'] ) ? $instance['limit'] : -1;
if ( is_admin() && !$category ) { if ( is_admin() && !$category ) {
// Display All Links widget as such in the widgets screen // Display All Links widget as such in the widgets screen
@ -115,18 +118,26 @@ class WP_Widget_Links extends WP_Widget {
'category_before' => $before_widget, 'category_after' => $after_widget, 'category_before' => $before_widget, 'category_after' => $after_widget,
'show_images' => $show_images, 'show_description' => $show_description, 'show_images' => $show_images, 'show_description' => $show_description,
'show_name' => $show_name, 'show_rating' => $show_rating, 'show_name' => $show_name, 'show_rating' => $show_rating,
'category' => $category, 'class' => 'linkcat widget' 'category' => $category, 'class' => 'linkcat widget',
'orderby' => $orderby, 'order' => $order,
'limit' => $limit,
))); )));
} }
function update( $new_instance, $old_instance ) { function update( $new_instance, $old_instance ) {
$new_instance = (array) $new_instance; $new_instance = (array) $new_instance;
$instance = array( 'images' => 0, 'name' => 0, 'description' => 0, 'rating' => 0); $instance = array( 'images' => 0, 'name' => 0, 'description' => 0, 'rating' => 0 );
foreach ( $instance as $field => $val ) { foreach ( $instance as $field => $val ) {
if ( isset($new_instance[$field]) ) if ( isset($new_instance[$field]) )
$instance[$field] = 1; $instance[$field] = 1;
} }
$instance['category'] = intval($new_instance['category']);
$instance['orderby'] = 'name';
if ( in_array( $new_instance['orderby'], array( 'name', 'rating', 'id', 'rand' ) ) )
$instance['orderby'] = $new_instance['orderby'];
$instance['category'] = intval( $new_instance['category'] );
$instance['limit'] = ! empty( $new_instance['limit'] ) ? intval( $new_instance['limit'] ) : -1;
return $instance; return $instance;
} }
@ -134,8 +145,10 @@ class WP_Widget_Links extends WP_Widget {
function form( $instance ) { function form( $instance ) {
//Defaults //Defaults
$instance = wp_parse_args( (array) $instance, array( 'images' => true, 'name' => true, 'description' => false, 'rating' => false, 'category' => false ) ); $instance = wp_parse_args( (array) $instance, array( 'images' => true, 'name' => true, 'description' => false, 'rating' => false, 'category' => false, 'orderby' => 'name', 'limit' => -1 ) );
$link_cats = get_terms( 'link_category'); $link_cats = get_terms( 'link_category' );
if ( ! $limit = intval( $instance['limit'] ) )
$limit = -1;
?> ?>
<p> <p>
<label for="<?php echo $this->get_field_id('category'); ?>" class="screen-reader-text"><?php _e('Select Link Category'); ?></label> <label for="<?php echo $this->get_field_id('category'); ?>" class="screen-reader-text"><?php _e('Select Link Category'); ?></label>
@ -148,7 +161,15 @@ class WP_Widget_Links extends WP_Widget {
. '>' . $link_cat->name . "</option>\n"; . '>' . $link_cat->name . "</option>\n";
} }
?> ?>
</select></p> </select>
<label for="<?php echo $this->get_field_id('orderby'); ?>"><?php _e( 'Sort by:' ); ?></label>
<select name="<?php echo $this->get_field_name('orderby'); ?>" id="<?php echo $this->get_field_id('orderby'); ?>" class="widefat">
<option value="name"<?php selected( $instance['orderby'], 'name' ); ?>><?php _e( 'Link title' ); ?></option>
<option value="rating"<?php selected( $instance['orderby'], 'rating' ); ?>><?php _e( 'Link rating' ); ?></option>
<option value="id"<?php selected( $instance['orderby'], 'id' ); ?>><?php _e( 'Link ID' ); ?></option>
<option value="rand"<?php selected( $instance['orderby'], 'rand' ); ?>><?php _e( 'Random' ); ?></option>
</select>
</p>
<p> <p>
<input class="checkbox" type="checkbox" <?php checked($instance['images'], true) ?> id="<?php echo $this->get_field_id('images'); ?>" name="<?php echo $this->get_field_name('images'); ?>" /> <input class="checkbox" type="checkbox" <?php checked($instance['images'], true) ?> id="<?php echo $this->get_field_id('images'); ?>" name="<?php echo $this->get_field_name('images'); ?>" />
<label for="<?php echo $this->get_field_id('images'); ?>"><?php _e('Show Link Image'); ?></label><br /> <label for="<?php echo $this->get_field_id('images'); ?>"><?php _e('Show Link Image'); ?></label><br />
@ -159,6 +180,10 @@ class WP_Widget_Links extends WP_Widget {
<input class="checkbox" type="checkbox" <?php checked($instance['rating'], true) ?> id="<?php echo $this->get_field_id('rating'); ?>" name="<?php echo $this->get_field_name('rating'); ?>" /> <input class="checkbox" type="checkbox" <?php checked($instance['rating'], true) ?> id="<?php echo $this->get_field_id('rating'); ?>" name="<?php echo $this->get_field_name('rating'); ?>" />
<label for="<?php echo $this->get_field_id('rating'); ?>"><?php _e('Show Link Rating'); ?></label> <label for="<?php echo $this->get_field_id('rating'); ?>"><?php _e('Show Link Rating'); ?></label>
</p> </p>
<p>
<label for="<?php echo $this->get_field_id('limit'); ?>"><?php _e( 'Number of links to show:' ); ?></label>
<input id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" type="text" value="<?php echo $limit == -1 ? '' : intval( $limit ); ?>" size="3" />
</p>
<?php <?php
} }
} }