]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/widgets/class-wp-widget-links.php
WordPress 4.4
[autoinstalls/wordpress.git] / wp-includes / widgets / class-wp-widget-links.php
1 <?php
2 /**
3  * Widget API: WP_Widget_Links class
4  *
5  * @package WordPress
6  * @subpackage Widgets
7  * @since 4.4.0
8  */
9
10 /**
11  * Core class used to implement a Links widget.
12  *
13  * @since 2.8.0
14  *
15  * @see WP_Widget
16  */
17 class WP_Widget_Links extends WP_Widget {
18
19         /**
20          * Sets up a new Links widget instance.
21          *
22          * @since 2.8.0
23          * @access public
24          */
25         public function __construct() {
26                 $widget_ops = array('description' => __( "Your blogroll" ) );
27                 parent::__construct('links', __('Links'), $widget_ops);
28         }
29
30         /**
31          * Outputs the content for the current Links widget instance.
32          *
33          * @since 2.8.0
34          * @access public
35          *
36          * @param array $args     Display arguments including 'before_title', 'after_title',
37          *                        'before_widget', and 'after_widget'.
38          * @param array $instance Settings for the current Links widget instance.
39          */
40         public function widget( $args, $instance ) {
41                 $show_description = isset($instance['description']) ? $instance['description'] : false;
42                 $show_name = isset($instance['name']) ? $instance['name'] : false;
43                 $show_rating = isset($instance['rating']) ? $instance['rating'] : false;
44                 $show_images = isset($instance['images']) ? $instance['images'] : true;
45                 $category = isset($instance['category']) ? $instance['category'] : false;
46                 $orderby = isset( $instance['orderby'] ) ? $instance['orderby'] : 'name';
47                 $order = $orderby == 'rating' ? 'DESC' : 'ASC';
48                 $limit = isset( $instance['limit'] ) ? $instance['limit'] : -1;
49
50                 $before_widget = preg_replace( '/id="[^"]*"/', 'id="%id"', $args['before_widget'] );
51
52                 $widget_links_args = array(
53                         'title_before'     => $args['before_title'],
54                         'title_after'      => $args['after_title'],
55                         'category_before'  => $before_widget,
56                         'category_after'   => $args['after_widget'],
57                         'show_images'      => $show_images,
58                         'show_description' => $show_description,
59                         'show_name'        => $show_name,
60                         'show_rating'      => $show_rating,
61                         'category'         => $category,
62                         'class'            => 'linkcat widget',
63                         'orderby'          => $orderby,
64                         'order'            => $order,
65                         'limit'            => $limit,
66                 );
67
68                 /**
69                  * Filter the arguments for the Links widget.
70                  *
71                  * @since 2.6.0
72                  * @since 4.4.0 The `$instance` parameter was added.
73                  *
74                  * @see wp_list_bookmarks()
75                  *
76                  * @param array $widget_links_args An array of arguments to retrieve the links list.
77                  * @param array $instance          The settings for the particular instance of the widget.
78                  */
79                 wp_list_bookmarks( apply_filters( 'widget_links_args', $widget_links_args, $instance ) );
80         }
81
82         /**
83          * Handles updating settings for the current Links widget instance.
84          *
85          * @since 2.8.0
86          * @access public
87          *
88          * @param array $new_instance New settings for this instance as input by the user via
89          *                            WP_Widget::form().
90          * @param array $old_instance Old settings for this instance.
91          * @return array Updated settings to save.
92          */
93         public function update( $new_instance, $old_instance ) {
94                 $new_instance = (array) $new_instance;
95                 $instance = array( 'images' => 0, 'name' => 0, 'description' => 0, 'rating' => 0 );
96                 foreach ( $instance as $field => $val ) {
97                         if ( isset($new_instance[$field]) )
98                                 $instance[$field] = 1;
99                 }
100
101                 $instance['orderby'] = 'name';
102                 if ( in_array( $new_instance['orderby'], array( 'name', 'rating', 'id', 'rand' ) ) )
103                         $instance['orderby'] = $new_instance['orderby'];
104
105                 $instance['category'] = intval( $new_instance['category'] );
106                 $instance['limit'] = ! empty( $new_instance['limit'] ) ? intval( $new_instance['limit'] ) : -1;
107
108                 return $instance;
109         }
110
111         /**
112          * Outputs the settings form for the Links widget.
113          *
114          * @since 2.8.0
115          * @access public
116          *
117          * @param array $instance Current settings.
118          */
119         public function form( $instance ) {
120
121                 //Defaults
122                 $instance = wp_parse_args( (array) $instance, array( 'images' => true, 'name' => true, 'description' => false, 'rating' => false, 'category' => false, 'orderby' => 'name', 'limit' => -1 ) );
123                 $link_cats = get_terms( 'link_category' );
124                 if ( ! $limit = intval( $instance['limit'] ) )
125                         $limit = -1;
126                         ?>
127                 <p>
128                 <label for="<?php echo $this->get_field_id('category'); ?>"><?php _e( 'Select Link Category:' ); ?></label>
129                 <select class="widefat" id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>">
130                 <option value=""><?php _ex('All Links', 'links widget'); ?></option>
131                 <?php
132                 foreach ( $link_cats as $link_cat ) {
133                         echo '<option value="' . intval( $link_cat->term_id ) . '"'
134                                 . selected( $instance['category'], $link_cat->term_id, false )
135                                 . '>' . $link_cat->name . "</option>\n";
136                 }
137                 ?>
138                 </select>
139                 <label for="<?php echo $this->get_field_id('orderby'); ?>"><?php _e( 'Sort by:' ); ?></label>
140                 <select name="<?php echo $this->get_field_name('orderby'); ?>" id="<?php echo $this->get_field_id('orderby'); ?>" class="widefat">
141                         <option value="name"<?php selected( $instance['orderby'], 'name' ); ?>><?php _e( 'Link title' ); ?></option>
142                         <option value="rating"<?php selected( $instance['orderby'], 'rating' ); ?>><?php _e( 'Link rating' ); ?></option>
143                         <option value="id"<?php selected( $instance['orderby'], 'id' ); ?>><?php _e( 'Link ID' ); ?></option>
144                         <option value="rand"<?php selected( $instance['orderby'], 'rand' ); ?>><?php _ex( 'Random', 'Links widget' ); ?></option>
145                 </select>
146                 </p>
147                 <p>
148                 <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'); ?>" />
149                 <label for="<?php echo $this->get_field_id('images'); ?>"><?php _e('Show Link Image'); ?></label><br />
150                 <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'); ?>" />
151                 <label for="<?php echo $this->get_field_id('name'); ?>"><?php _e('Show Link Name'); ?></label><br />
152                 <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'); ?>" />
153                 <label for="<?php echo $this->get_field_id('description'); ?>"><?php _e('Show Link Description'); ?></label><br />
154                 <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'); ?>" />
155                 <label for="<?php echo $this->get_field_id('rating'); ?>"><?php _e('Show Link Rating'); ?></label>
156                 </p>
157                 <p>
158                 <label for="<?php echo $this->get_field_id('limit'); ?>"><?php _e( 'Number of links to show:' ); ?></label>
159                 <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" />
160                 </p>
161                 <?php
162         }
163 }