3 * Widget API: WP_Widget_Links class
11 * Core class used to implement a Links widget.
17 class WP_Widget_Links extends WP_Widget {
20 * Sets up a new Links widget instance.
25 public function __construct() {
27 'description' => __( 'Your blogroll' ),
28 'customize_selective_refresh' => true,
30 parent::__construct( 'links', __( 'Links' ), $widget_ops );
34 * Outputs the content for the current Links widget instance.
39 * @param array $args Display arguments including 'before_title', 'after_title',
40 * 'before_widget', and 'after_widget'.
41 * @param array $instance Settings for the current Links widget instance.
43 public function widget( $args, $instance ) {
44 $show_description = isset($instance['description']) ? $instance['description'] : false;
45 $show_name = isset($instance['name']) ? $instance['name'] : false;
46 $show_rating = isset($instance['rating']) ? $instance['rating'] : false;
47 $show_images = isset($instance['images']) ? $instance['images'] : true;
48 $category = isset($instance['category']) ? $instance['category'] : false;
49 $orderby = isset( $instance['orderby'] ) ? $instance['orderby'] : 'name';
50 $order = $orderby == 'rating' ? 'DESC' : 'ASC';
51 $limit = isset( $instance['limit'] ) ? $instance['limit'] : -1;
53 $before_widget = preg_replace( '/id="[^"]*"/', 'id="%id"', $args['before_widget'] );
55 $widget_links_args = array(
56 'title_before' => $args['before_title'],
57 'title_after' => $args['after_title'],
58 'category_before' => $before_widget,
59 'category_after' => $args['after_widget'],
60 'show_images' => $show_images,
61 'show_description' => $show_description,
62 'show_name' => $show_name,
63 'show_rating' => $show_rating,
64 'category' => $category,
65 'class' => 'linkcat widget',
66 'orderby' => $orderby,
72 * Filters the arguments for the Links widget.
75 * @since 4.4.0 The `$instance` parameter was added.
77 * @see wp_list_bookmarks()
79 * @param array $widget_links_args An array of arguments to retrieve the links list.
80 * @param array $instance The settings for the particular instance of the widget.
82 wp_list_bookmarks( apply_filters( 'widget_links_args', $widget_links_args, $instance ) );
86 * Handles updating settings for the current Links widget instance.
91 * @param array $new_instance New settings for this instance as input by the user via
93 * @param array $old_instance Old settings for this instance.
94 * @return array Updated settings to save.
96 public function update( $new_instance, $old_instance ) {
97 $new_instance = (array) $new_instance;
98 $instance = array( 'images' => 0, 'name' => 0, 'description' => 0, 'rating' => 0 );
99 foreach ( $instance as $field => $val ) {
100 if ( isset($new_instance[$field]) )
101 $instance[$field] = 1;
104 $instance['orderby'] = 'name';
105 if ( in_array( $new_instance['orderby'], array( 'name', 'rating', 'id', 'rand' ) ) )
106 $instance['orderby'] = $new_instance['orderby'];
108 $instance['category'] = intval( $new_instance['category'] );
109 $instance['limit'] = ! empty( $new_instance['limit'] ) ? intval( $new_instance['limit'] ) : -1;
115 * Outputs the settings form for the Links widget.
120 * @param array $instance Current settings.
122 public function form( $instance ) {
125 $instance = wp_parse_args( (array) $instance, array( 'images' => true, 'name' => true, 'description' => false, 'rating' => false, 'category' => false, 'orderby' => 'name', 'limit' => -1 ) );
126 $link_cats = get_terms( 'link_category' );
127 if ( ! $limit = intval( $instance['limit'] ) )
131 <label for="<?php echo $this->get_field_id('category'); ?>"><?php _e( 'Select Link Category:' ); ?></label>
132 <select class="widefat" id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>">
133 <option value=""><?php _ex('All Links', 'links widget'); ?></option>
135 foreach ( $link_cats as $link_cat ) {
136 echo '<option value="' . intval( $link_cat->term_id ) . '"'
137 . selected( $instance['category'], $link_cat->term_id, false )
138 . '>' . $link_cat->name . "</option>\n";
142 <label for="<?php echo $this->get_field_id('orderby'); ?>"><?php _e( 'Sort by:' ); ?></label>
143 <select name="<?php echo $this->get_field_name('orderby'); ?>" id="<?php echo $this->get_field_id('orderby'); ?>" class="widefat">
144 <option value="name"<?php selected( $instance['orderby'], 'name' ); ?>><?php _e( 'Link title' ); ?></option>
145 <option value="rating"<?php selected( $instance['orderby'], 'rating' ); ?>><?php _e( 'Link rating' ); ?></option>
146 <option value="id"<?php selected( $instance['orderby'], 'id' ); ?>><?php _e( 'Link ID' ); ?></option>
147 <option value="rand"<?php selected( $instance['orderby'], 'rand' ); ?>><?php _ex( 'Random', 'Links widget' ); ?></option>
151 <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'); ?>" />
152 <label for="<?php echo $this->get_field_id('images'); ?>"><?php _e('Show Link Image'); ?></label><br />
153 <input class="checkbox" type="checkbox"<?php checked($instance['name'], true) ?> id="<?php echo $this->get_field_id('name'); ?>" name="<?php echo $this->get_field_name('name'); ?>" />
154 <label for="<?php echo $this->get_field_id('name'); ?>"><?php _e('Show Link Name'); ?></label><br />
155 <input class="checkbox" type="checkbox"<?php checked($instance['description'], true) ?> id="<?php echo $this->get_field_id('description'); ?>" name="<?php echo $this->get_field_name('description'); ?>" />
156 <label for="<?php echo $this->get_field_id('description'); ?>"><?php _e('Show Link Description'); ?></label><br />
157 <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'); ?>" />
158 <label for="<?php echo $this->get_field_id('rating'); ?>"><?php _e('Show Link Rating'); ?></label>
161 <label for="<?php echo $this->get_field_id('limit'); ?>"><?php _e( 'Number of links to show:' ); ?></label>
162 <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" />