]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/widgets/class-wp-widget-links.php
WordPress 4.7
[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(
27                         'description' => __( 'Your blogroll' ),
28                         'customize_selective_refresh' => true,
29                 );
30                 parent::__construct( 'links', __( 'Links' ), $widget_ops );
31         }
32
33         /**
34          * Outputs the content for the current Links widget instance.
35          *
36          * @since 2.8.0
37          * @access public
38          *
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.
42          */
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;
52
53                 $before_widget = preg_replace( '/id="[^"]*"/', 'id="%id"', $args['before_widget'] );
54
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,
67                         'order'            => $order,
68                         'limit'            => $limit,
69                 );
70
71                 /**
72                  * Filters the arguments for the Links widget.
73                  *
74                  * @since 2.6.0
75                  * @since 4.4.0 The `$instance` parameter was added.
76                  *
77                  * @see wp_list_bookmarks()
78                  *
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.
81                  */
82                 wp_list_bookmarks( apply_filters( 'widget_links_args', $widget_links_args, $instance ) );
83         }
84
85         /**
86          * Handles updating settings for the current Links widget instance.
87          *
88          * @since 2.8.0
89          * @access public
90          *
91          * @param array $new_instance New settings for this instance as input by the user via
92          *                            WP_Widget::form().
93          * @param array $old_instance Old settings for this instance.
94          * @return array Updated settings to save.
95          */
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;
102                 }
103
104                 $instance['orderby'] = 'name';
105                 if ( in_array( $new_instance['orderby'], array( 'name', 'rating', 'id', 'rand' ) ) )
106                         $instance['orderby'] = $new_instance['orderby'];
107
108                 $instance['category'] = intval( $new_instance['category'] );
109                 $instance['limit'] = ! empty( $new_instance['limit'] ) ? intval( $new_instance['limit'] ) : -1;
110
111                 return $instance;
112         }
113
114         /**
115          * Outputs the settings form for the Links widget.
116          *
117          * @since 2.8.0
118          * @access public
119          *
120          * @param array $instance Current settings.
121          */
122         public function form( $instance ) {
123
124                 //Defaults
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'] ) )
128                         $limit = -1;
129                         ?>
130                 <p>
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>
134                 <?php
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";
139                 }
140                 ?>
141                 </select>
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>
148                 </select>
149                 </p>
150                 <p>
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>
159                 </p>
160                 <p>
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" />
163                 </p>
164                 <?php
165         }
166 }