PluginProbe
Essential Widgets / 2.0
Essential Widgets v2.0
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 2.0, at includes/widgets/class-ew-authors.php

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