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