PluginProbe
Essential Widgets / 1.1
Essential Widgets v1.1
3.2.1 3.3 3.2 trunk 1.0.0 1.0.1 1.1 1.2 1.2.1 1.2.2 1.3 1.4 1.4.1 1.5 1.6 1.7 1.7.1 1.7.2 1.7.3 1.7.4 1.8 1.9 2.0 2.1 2.2 All 31 releases
essential-widgets / includes / widgets / class-ew-authors.php

class-ew-authors.php in Essential Widgets 1.1, at includes/widgets/class-ew-authors.php

331 lines 11.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Custom Author Widget
4 *
5 * @package Essential_Widgets
6 */
7
8
9 /**
10 * Custom Author Widget
11 */
12 class EW_Authors extends WP_Widget {
13
14 /**
15 * Holds widget settings defaults, populated in constructor.
16 *
17 * @var array
18 */
19 protected $defaults;
20
21 function __construct() {
22
23 // Set up the defaults.
24 $topic_count_text = _n_noop( '%s topic', '%s topics', 'essential-widgets' );
25
26 // Set up defaults.
27 $this->defaults = array(
28 'title' => esc_attr__( 'Authors', 'essential-widgets' ),
29 'order' => 'ASC',
30 'orderby' => 'display_name',
31 'number' => '',
32 'include' => '',
33 'exclude' => '',
34 'optioncount' => false,
35 'exclude_admin' => false,
36 'show_fullname' => true,
37 'hide_empty' => true,
38 'style' => 'list',
39 'html' => true,
40 'feed' => '',
41 'feed_type' => '',
42 'feed_image' => ''
43 );
44
45 $widget_ops = array(
46 'classname' => 'essential-widgets ew-author ewauthor',
47 'description' => esc_html__( 'Displays a list of author', 'essential-widgets' ),
48 );
49
50 $control_ops = array(
51 'id_base' => 'ew-author'
52 );
53
54 parent::__construct(
55 'ew-author', // Base ID
56 __( 'EW: Authors', 'essential-widgets' ), // Name
57 $widget_ops,
58 $control_ops
59 );
60 }
61
62 function form($instance) {
63 // Merge the user-selected arguments with the defaults.
64 $instance = wp_parse_args( (array) $instance, $this->defaults );
65
66 $order = array(
67 'ASC' => esc_attr__( 'Ascending', 'essential-widgets' ),
68 'DESC' => esc_attr__( 'Descending', 'essential-widgets' )
69 );
70
71 $orderby = array(
72 'display_name' => esc_attr__( 'Display Name', 'essential-widgets' ),
73 'email' => esc_attr__( 'Email', 'essential-widgets' ),
74 'ID' => esc_attr__( 'ID', 'essential-widgets' ),
75 'nicename' => esc_attr__( 'Nice Name', 'essential-widgets' ),
76 'post_count' => esc_attr__( 'Post Count', 'essential-widgets' ),
77 'registered' => esc_attr__( 'Registered', 'essential-widgets' ),
78 'url' => esc_attr__( 'URL', 'essential-widgets' ),
79 'user_login' => esc_attr__( 'Login', 'essential-widgets' )
80 );
81
82 $style = array(
83 'list' => esc_attr__( 'List', 'essential-widgets'),
84 'none' => esc_attr__( 'Plain', 'essential-widgets' )
85 );
86
87 $feed_type = array(
88 '' => '',
89 'atom' => esc_attr__( 'Atom', 'essential-widgets' ),
90 'rdf' => esc_attr__( 'RDF', 'essential-widgets' ),
91 'rss' => esc_attr__( 'RSS', 'essential-widgets' ),
92 'rss2' => esc_attr__( 'RSS 2.0', 'essential-widgets' )
93 ); ?>
94
95 <p>
96 <label>
97 <?php esc_html_e( 'Title:', 'essential-widgets' ); ?>
98 <input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" placeholder="<?php echo esc_attr( $this->defaults['title'] ); ?>" />
99 </label>
100 </p>
101
102 <p>
103 <label>
104 <?php esc_html_e( 'Order:', 'essential-widgets' ); ?>
105
106 <select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'order' ) ); ?>" name="<?php esc_attr( $this->get_field_name( 'order' ) ); ?>">
107
108 <?php foreach ( $order as $option_value => $option_label ) : ?>
109
110 <option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['order'], $option_value ); ?>><?php echo esc_html($option_label ); ?></option>
111
112 <?php endforeach; ?>
113
114 </select>
115 </label>
116 </p>
117
118 <p>
119 <label>
120 <?php esc_html_e( 'Order By:', 'essential-widgets' ); ?>
121
122 <select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'orderby' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'orderby' ) ); ?>">
123
124 <?php foreach ( $orderby as $option_value => $option_label ) : ?>
125
126 <option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['orderby'], $option_value ); ?>><?php echo esc_html($option_label ); ?></option>
127
128 <?php endforeach; ?>
129
130 </select>
131 </label>
132 </p>
133
134 <p>
135 <label>
136 <?php esc_html_e( 'Number:', 'essential-widgets' ); ?>
137 <input type="number" class="widefat code" size="5" min="0" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" value="<?php echo esc_attr( $instance['number'] ); ?>" placeholder="0" />
138 </label>
139 </p>
140
141 <p>
142 <label>
143 <?php esc_html_e( 'Display as:', 'essential-widgets' ); ?>
144
145 <select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'style' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'style' ) ); ?>">
146
147 <?php foreach ( $style as $option_value => $option_label ) : ?>
148
149 <option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['style'], $option_value ); ?>><?php echo esc_html($option_label ); ?></option>
150
151 <?php endforeach; ?>
152
153 </select>
154 </label>
155 </p>
156
157 <p>
158 <label>
159 <?php esc_html_e( 'Include:', 'essential-widgets' ); ?>
160 <input type="text" class="widefat code" name="<?php echo esc_attr( $this->get_field_name( 'include' ) ); ?>" value="<?php echo esc_attr( $instance['include'] ); ?>" placeholder="1,2,3&hellip;" />
161 </label>
162 </p>
163
164 <p>
165 <label>
166 <?php esc_html_e( 'Exclude:', 'essential-widgets' ); ?>
167 <input type="text" class="widefat code" name="<?php echo esc_attr( $this->get_field_name( 'exclude' ) ); ?>" value="<?php echo esc_attr( $instance['exclude'] ); ?>" placeholder="1,2,3&hellip;" />
168 </label>
169 </p>
170
171 <p class="button-primary more"><?php esc_html_e( 'More Options', 'essential-widgets' ); ?><i class="dashicons dashicons-arrow-down"></i></p>
172
173 <div class="advanced-section">
174
175 <p>
176 <label>
177 <?php esc_html_e( 'Feed Type:', 'essential-widgets' ); ?>
178
179 <select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'feed_type' ) ); ?>">
180
181 <?php foreach ( $feed_type as $option_value => $option_label ) : ?>
182
183 <option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['feed_type'], $option_value ); ?>><?php echo esc_html($option_label ); ?></option>
184
185 <?php endforeach; ?>
186
187 </select>
188 </label>
189 </p>
190
191 <p>
192 <label>
193 <?php esc_html_e( 'Feed Text:', 'essential-widgets' ); ?>
194 <input type="text" class="widefat code" name="<?php echo esc_attr( $this->get_field_name( 'feed' ) ); ?>" value="<?php echo esc_attr( $instance['feed'] ); ?>" />
195 </label>
196 </p>
197
198 <p>
199 <label>
200 <?php esc_html_e( 'Feed Image:', 'essential-widgets' ); ?>
201 <input type="url" class="widefat code" name="<?php echo esc_attr( $this->get_field_name( 'feed_image' ) ); ?>" value="<?php echo esc_attr( $instance['feed_image'] ); ?>" placeholder="<?php echo esc_attr( home_url( 'images/example.png' ) ); ?>" />
202 </label>
203 </p>
204
205 <p>
206 <label>
207 <input type="checkbox" <?php checked( $instance['html'], true ); ?> name="<?php echo esc_attr( $this->get_field_name( 'html' ) ); ?>" />
208 <?php esc_html_e( 'HTML?', 'essential-widgets' ); ?>
209 </label>
210 </p>
211
212 <p>
213 <label>
214 <input type="checkbox" <?php checked( $instance['optioncount'], true ); ?> name="<?php echo esc_attr( $this->get_field_name( 'optioncount' ) ); ?>" />
215 <?php esc_html_e( 'Show post count?', 'essential-widgets' ); ?>
216 </label>
217 </p>
218
219 <p>
220 <label>
221 <input type="checkbox" <?php checked( $instance['exclude_admin'], true ); ?> name="<?php echo esc_attr( $this->get_field_name( 'exclude_admin' ) ); ?>" />
222 <?php esc_html_e( 'Exclude admin?', 'essential-widgets' ); ?>
223 </label>
224 </p>
225
226 <p>
227 <label>
228 <input type="checkbox" <?php checked( $instance['show_fullname'], true ); ?> name="<?php echo esc_attr( $this->get_field_name( 'show_fullname' ) ); ?>" />
229 <?php esc_html_e( 'Show full name?', 'essential-widgets' ); ?>
230 </label>
231 </p>
232
233 <p>
234 <label>
235 <input type="checkbox" <?php checked( $instance['hide_empty'], true ); ?> name="<?php echo esc_attr( $this->get_field_name( 'hide_empty' ) ); ?>" />
236 <?php esc_html_e( 'Hide empty?', 'essential-widgets' ); ?>
237 </label>
238 </p>
239
240 </div><!-- .advanced-section -->
241
242 <div style="clear:both;">&nbsp;</div>
243 <?php
244 }
245
246 function update( $new_instance, $old_instance ) {
247
248 // Sanitize title.
249 $instance['title'] = sanitize_text_field( $new_instance['title'] );
250
251 // Strip tags.
252 $instance['feed'] = strip_tags( $new_instance['feed'] );
253
254 // Whitelist options.
255 $order = array( 'ASC', 'DESC' );
256 $orderby = array( 'display_name', 'email', 'ID', 'nicename', 'post_count', 'registered', 'url', 'user_login' );
257 $style = array( 'list', 'none' );
258 $feed_type = array( '', 'atom', 'rdf', 'rss', 'rss2' );
259
260 $instance['order'] = in_array( $new_instance['order'], $order ) ? $new_instance['order'] : 'ASC';
261 $instance['orderby'] = in_array( $new_instance['orderby'], $orderby ) ? $new_instance['orderby'] : 'display_name';
262 $instance['style'] = in_array( $new_instance['style'], $style ) ? $new_instance['style'] : 'list';
263 $instance['feed_type'] = in_array( $new_instance['feed_type'], $feed_type ) ? $new_instance['feed_type'] : '';
264
265 // Integers.
266 $instance['number'] = intval( $new_instance['number'] );
267
268 // Only allow integers and commas.
269 $instance['include'] = preg_replace( '/[^0-9,]/', '', $new_instance['include'] );
270 $instance['exclude'] = preg_replace( '/[^0-9,]/', '', $new_instance['exclude'] );
271
272 // URLs.
273 $instance['feed_image'] = esc_url_raw( $new_instance['feed_image'] );
274
275 // Checkboxes.
276 $instance['html'] = isset( $new_instance['html'] ) ? 1 : 0;
277 $instance['optioncount'] = isset( $new_instance['optioncount'] ) ? 1 : 0;
278 $instance['exclude_admin'] = isset( $new_instance['exclude_admin'] ) ? 1 : 0;
279 $instance['show_fullname'] = isset( $new_instance['show_fullname'] ) ? 1 : 0;
280 $instance['hide_empty'] = isset( $new_instance['hide_empty'] ) ? 1 : 0;
281
282 // Return sanitized options.
283 return $instance;
284 }
285
286 function widget( $args, $instance ) {
287 // Set the $args for wp_list_authors() to the $instance array.
288 $instance = wp_parse_args( $instance, $this->defaults );
289
290 // instance the $echo argument and set it to false.
291 $instance['echo'] = false;
292
293 // Output the args's $before_widget wrapper.
294 echo $args['before_widget'];
295
296 // If a title was input by the user, display it.
297 if ( ! empty( $instance['title'] ) )
298 echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $args['after_title'];
299
300 // Get the authors list.
301 $authors = str_replace( array( "\r", "\n", "\t" ), '', wp_list_authors( $instance ) );
302
303 $authors = str_replace('</a> (', '</a> <span>(', $authors);
304 $authors = str_replace(')', ')</span>', $authors);
305
306 // If 'list' is the style and the output should be HTML, wrap the authors in a <ul>.
307 if ( 'list' == $instance['style'] && $instance['html'] )
308 $authors = '<ul class="authors">' . $authors . '</ul><!-- .xoxo .authors -->';
309
310 // If 'none' is the style and the output should be HTML, wrap the authors in a <p>.
311 elseif ( 'none' == $instance['style'] && $instance['html'] )
312 $authors = '<p class="authors">' . $authors . '</p><!-- .authors -->';
313
314 // Display the authors list.
315 echo $authors;
316
317 // Close the args's widget wrapper.
318 echo $args['after_widget'];
319 }
320 }// end Author_Widget class
321
322 /**
323 * Intiate Author_Widget Class.
324 *
325 * @since 1.0.0
326 */
327 function ew_authors_register() {
328 register_widget( 'EW_Authors' );
329 }
330 add_action( 'widgets_init', 'ew_authors_register' );
331