Make WordPress Core

Changeset 20071


Ignore:
Timestamp:
03/02/2012 01:58:58 PM (14 years ago)
Author:
duck_
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/default-widgets.php

    r20047 r20071  
    103103                $show_images = isset($instance['images']) ? $instance['images'] : true;
    104104                $category = isset($instance['category']) ? $instance['category'] : false;
     105                $orderby = isset( $instance['orderby'] ) ? $instance['orderby'] : 'name';
     106                $order = $orderby == 'rating' ? 'DESC' : 'ASC';
     107                $limit = isset( $instance['limit'] ) ? $instance['limit'] : -1;
    105108
    106109                if ( is_admin() && !$category ) {
     
    116119                        'show_images' => $show_images, 'show_description' => $show_description,
    117120                        'show_name' => $show_name, 'show_rating' => $show_rating,
    118                         'category' => $category, 'class' => 'linkcat widget'
     121                        'category' => $category, 'class' => 'linkcat widget',
     122                        'orderby' => $orderby, 'order' => $order,
     123                        'limit' => $limit,
    119124                )));
    120125        }
     
    122127        function update( $new_instance, $old_instance ) {
    123128                $new_instance = (array) $new_instance;
    124                 $instance = array( 'images' => 0, 'name' => 0, 'description' => 0, 'rating' => 0);
     129                $instance = array( 'images' => 0, 'name' => 0, 'description' => 0, 'rating' => 0 );
    125130                foreach ( $instance as $field => $val ) {
    126131                        if ( isset($new_instance[$field]) )
    127132                                $instance[$field] = 1;
    128133                }
    129                 $instance['category'] = intval($new_instance['category']);
     134
     135                $instance['orderby'] = 'name';
     136                if ( in_array( $new_instance['orderby'], array( 'name', 'rating', 'id', 'rand' ) ) )
     137                        $instance['orderby'] = $new_instance['orderby'];
     138
     139                $instance['category'] = intval( $new_instance['category'] );
     140                $instance['limit'] = ! empty( $new_instance['limit'] ) ? intval( $new_instance['limit'] ) : -1;
    130141
    131142                return $instance;
     
    135146
    136147                //Defaults
    137                 $instance = wp_parse_args( (array) $instance, array( 'images' => true, 'name' => true, 'description' => false, 'rating' => false, 'category' => false ) );
    138                 $link_cats = get_terms( 'link_category');
     148                $instance = wp_parse_args( (array) $instance, array( 'images' => true, 'name' => true, 'description' => false, 'rating' => false, 'category' => false, 'orderby' => 'name', 'limit' => -1 ) );
     149                $link_cats = get_terms( 'link_category' );
     150                if ( ! $limit = intval( $instance['limit'] ) )
     151                        $limit = -1;
    139152?>
    140153                <p>
     
    149162                }
    150163                ?>
    151                 </select></p>
     164                </select>
     165                <label for="<?php echo $this->get_field_id('orderby'); ?>"><?php _e( 'Sort by:' ); ?></label>
     166                <select name="<?php echo $this->get_field_name('orderby'); ?>" id="<?php echo $this->get_field_id('orderby'); ?>" class="widefat">
     167                        <option value="name"<?php selected( $instance['orderby'], 'name' ); ?>><?php _e( 'Link title' ); ?></option>
     168                        <option value="rating"<?php selected( $instance['orderby'], 'rating' ); ?>><?php _e( 'Link rating' ); ?></option>
     169                        <option value="id"<?php selected( $instance['orderby'], 'id' ); ?>><?php _e( 'Link ID' ); ?></option>
     170                        <option value="rand"<?php selected( $instance['orderby'], 'rand' ); ?>><?php _e( 'Random' ); ?></option>
     171                </select>
     172                </p>
    152173                <p>
    153174                <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'); ?>" />
     
    159180                <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'); ?>" />
    160181                <label for="<?php echo $this->get_field_id('rating'); ?>"><?php _e('Show Link Rating'); ?></label>
     182                </p>
     183                <p>
     184                <label for="<?php echo $this->get_field_id('limit'); ?>"><?php _e( 'Number of links to show:' ); ?></label>
     185                <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" />
    161186                </p>
    162187<?php
Note: See TracChangeset for help on using the changeset viewer.