| 1 |
<?php |
| 2 |
/** |
| 3 |
* Creates a widget that allows users to list users of their site. |
| 4 |
* |
| 5 |
* @package Members |
| 6 |
* @subpackage Includes |
| 7 |
* @author The MemberPress Team |
| 8 |
* @copyright Copyright (c) 2009 - 2018, The MemberPress Team |
| 9 |
* @link https://members-plugin.com/ |
| 10 |
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
| 11 |
*/ |
| 12 |
namespace Members; |
| 13 |
|
| 14 |
defined('ABSPATH') || exit; |
| 15 |
/** |
| 16 |
* Users widget archive class. |
| 17 |
* |
| 18 |
* @since 2.0.0 |
| 19 |
* @access public |
| 20 |
*/ |
| 21 |
class Widget_Users extends \WP_Widget { |
| 22 |
|
| 23 |
/** |
| 24 |
* Default arguments for the widget settings. |
| 25 |
* |
| 26 |
* @since 2.0.0 |
| 27 |
* @access public |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
public $defaults = array(); |
| 31 |
|
| 32 |
/** |
| 33 |
* Set up the widget's unique name, ID, class, description, and other options. |
| 34 |
* |
| 35 |
* @since 2.0.0 |
| 36 |
* @access public |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public function __construct() { |
| 40 |
|
| 41 |
// Set up the widget options. |
| 42 |
$widget_options = array( |
| 43 |
'classname' => 'users', |
| 44 |
'description' => esc_html__( 'Provides the ability to list the users of the site.', 'members' ) |
| 45 |
); |
| 46 |
|
| 47 |
// Set up the widget control options. |
| 48 |
$control_options = array( |
| 49 |
'width' => 525, |
| 50 |
'height' => 350, |
| 51 |
'id_base' => 'members-widget-users' |
| 52 |
); |
| 53 |
|
| 54 |
// Create the widget. |
| 55 |
parent::__construct( 'members-widget-users', esc_html__( 'Members: Users', 'members' ), $widget_options, $control_options ); |
| 56 |
|
| 57 |
// Set up the defaults. |
| 58 |
$this->defaults = array( |
| 59 |
'title' => esc_attr__( 'Users', 'members' ), |
| 60 |
'order' => 'ASC', |
| 61 |
'orderby' => 'login', |
| 62 |
'role' => '', |
| 63 |
'meta_key' => '', |
| 64 |
'meta_value' => '', |
| 65 |
'include' => '', |
| 66 |
'exclude' => '', |
| 67 |
'search' => '', |
| 68 |
'offset' => '', |
| 69 |
'number' => '' |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Outputs the widget based on the arguments input through the widget controls. |
| 75 |
* |
| 76 |
* @since 2.0.0 |
| 77 |
* @access public |
| 78 |
* @param array $sidebar |
| 79 |
* @param array $instance |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
function widget( $sidebar, $instance ) { |
| 83 |
|
| 84 |
$instance = wp_parse_args( $instance, $this->defaults ); |
| 85 |
|
| 86 |
// Set up the arguments for get_users(). |
| 87 |
$args = array( |
| 88 |
'role' => $instance['role'], |
| 89 |
'meta_key' => $instance['meta_key'], |
| 90 |
'meta_value' => $instance['meta_value'], |
| 91 |
'include' => ! empty( $instance['include'] ) ? explode( ',', $instance['include'] ) : '', |
| 92 |
'exclude' => ! empty( $instance['exclude'] ) ? explode( ',', $instance['exclude'] ) : '', |
| 93 |
'search' => $instance['search'], |
| 94 |
'orderby' => $instance['orderby'], |
| 95 |
'order' => $instance['order'], |
| 96 |
'offset' => ! empty( $instance['offset'] ) ? intval( $instance['offset'] ) : '', |
| 97 |
'number' => ! empty( $instance['number'] ) ? intval( $instance['number'] ) : '', |
| 98 |
); |
| 99 |
|
| 100 |
// Output the theme's $before_widget wrapper. |
| 101 |
echo $sidebar['before_widget']; |
| 102 |
|
| 103 |
// If a title was input by the user, display it. |
| 104 |
if ( $instance['title'] ) |
| 105 |
echo $sidebar['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $sidebar['after_title']; |
| 106 |
|
| 107 |
// Get users. |
| 108 |
$users = get_users( $args ); |
| 109 |
|
| 110 |
// If users were found. |
| 111 |
if ( ! empty( $users ) ) { |
| 112 |
|
| 113 |
echo '<ul class="xoxo users">'; |
| 114 |
|
| 115 |
// Loop through each available user, creating a list item with a link to the user's archive. |
| 116 |
foreach ( $users as $user ) { |
| 117 |
|
| 118 |
$class = sanitize_html_class( "user-{$user->ID}" ); |
| 119 |
|
| 120 |
if ( is_author( $user->ID ) ) |
| 121 |
$class .= ' current-user'; |
| 122 |
|
| 123 |
printf( |
| 124 |
'<li class="%s"><a href="%s">%s</a>', |
| 125 |
esc_attr( $class ), |
| 126 |
esc_url( get_author_posts_url( $user->ID, $user->user_nicename ) ), |
| 127 |
esc_html( $user->display_name ) |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
echo '</ul>'; |
| 132 |
} |
| 133 |
|
| 134 |
// Close the theme's widget wrapper. |
| 135 |
echo $sidebar['after_widget']; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Sanitizes/Validates widget options before being saved. |
| 140 |
* |
| 141 |
* @since 2.0.0 |
| 142 |
* @access public |
| 143 |
* @param array $new_instance |
| 144 |
* @param array $old_instance |
| 145 |
* @return array |
| 146 |
*/ |
| 147 |
function update( $new_instance, $old_instance ) { |
| 148 |
|
| 149 |
// Text fields. |
| 150 |
$instance['title'] = sanitize_text_field( $new_instance['title'] ); |
| 151 |
$instance['order'] = sanitize_text_field( $new_instance['order'] ); |
| 152 |
$instance['orderby'] = sanitize_text_field( $new_instance['orderby'] ); |
| 153 |
$instance['meta_key'] = sanitize_text_field( $new_instance['meta_key'] ); |
| 154 |
$instance['meta_value'] = sanitize_text_field( $new_instance['meta_value'] ); |
| 155 |
$instance['search'] = sanitize_text_field( $new_instance['search'] ); |
| 156 |
|
| 157 |
// Roles. |
| 158 |
$instance['role'] = members_role_exists( $new_instance['role'] ) ? $new_instance['role'] : ''; |
| 159 |
|
| 160 |
// ID lists. |
| 161 |
$instance['include'] = $new_instance['include'] ? join( ',', wp_parse_id_list( $new_instance['include'] ) ) : ''; |
| 162 |
$instance['exclude'] = $new_instance['exclude'] ? join( ',', wp_parse_id_list( $new_instance['exclude'] ) ) : ''; |
| 163 |
|
| 164 |
// Integers. |
| 165 |
$instance['offset'] = absint( $new_instance['offset'] ); |
| 166 |
$instance['number'] = absint( $new_instance['number'] ) > 0 ? absint( $new_instance['number'] ) : ''; |
| 167 |
|
| 168 |
return $instance; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Displays the widget control options in the Widgets admin screen. |
| 173 |
* |
| 174 |
* @since 2.0.0 |
| 175 |
* @access public |
| 176 |
* @param array $instance |
| 177 |
* @return void |
| 178 |
*/ |
| 179 |
function form( $instance ) { |
| 180 |
|
| 181 |
// We need to load our admin functions file to support the new block-based Widgets editor. |
| 182 |
// This is because is_admin() does not work in REST requests, so it won't work in this new editor. |
| 183 |
// Since we load these functions globally using is_admin(), they won't load here, so we have to require them. |
| 184 |
// @see https://github.com/WordPress/gutenberg/issues/33443 |
| 185 |
// @see https://make.wordpress.org/core/2021/06/29/block-based-widgets-editor-in-wordpress-5-8/ |
| 186 |
require_once( members_plugin()->dir . 'admin/functions-admin.php' ); |
| 187 |
|
| 188 |
// Merge the user-selected arguments with the defaults. |
| 189 |
$instance = wp_parse_args( (array) $instance, $this->defaults ); |
| 190 |
|
| 191 |
$order = array( |
| 192 |
'ASC' => esc_attr__( 'Ascending', 'members' ), |
| 193 |
'DESC' => esc_attr__( 'Descending', 'members' ) |
| 194 |
); |
| 195 |
|
| 196 |
$orderby = array( |
| 197 |
'display_name' => esc_attr__( 'Display Name', 'members' ), |
| 198 |
'email' => esc_attr__( 'Email', 'members' ), |
| 199 |
'ID' => esc_attr__( 'ID', 'members' ), |
| 200 |
'nicename' => esc_attr__( 'Nice Name', 'members' ), |
| 201 |
'post_count' => esc_attr__( 'Post Count', 'members' ), |
| 202 |
'registered' => esc_attr__( 'Registered', 'members' ), |
| 203 |
'url' => esc_attr__( 'URL', 'members' ), |
| 204 |
'user_login' => esc_attr__( 'Login', 'members' ) |
| 205 |
); |
| 206 |
|
| 207 |
$meta_key = array_merge( array( '' ), (array) members_get_user_meta_keys() ); |
| 208 |
|
| 209 |
$roles = members_get_roles(); |
| 210 |
asort( $roles ); ?> |
| 211 |
|
| 212 |
<div style="float: left;width: 48%;"> |
| 213 |
|
| 214 |
<p> |
| 215 |
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'members' ); ?></label> |
| 216 |
<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" /> |
| 217 |
</p> |
| 218 |
<p> |
| 219 |
<label for="<?php echo $this->get_field_id( 'orderby' ); ?>"><?php esc_html_e( 'Order By:', 'members' ); ?></label> |
| 220 |
<select class="widefat" id="<?php echo $this->get_field_id( 'orderby' ); ?>" name="<?php echo $this->get_field_name( 'orderby' ); ?>"> |
| 221 |
<?php foreach ( $orderby as $option_value => $option_label ) : ?> |
| 222 |
<option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['orderby'], $option_value ); ?>><?php echo esc_html( $option_label ); ?></option> |
| 223 |
<?php endforeach; ?> |
| 224 |
</select> |
| 225 |
</p> |
| 226 |
<p> |
| 227 |
<label for="<?php echo $this->get_field_id( 'order' ); ?>"><?php esc_html_e( 'Order:', 'members' ); ?></label> |
| 228 |
<select class="widefat" id="<?php echo $this->get_field_id( 'order' ); ?>" name="<?php echo $this->get_field_name( 'order' ); ?>"> |
| 229 |
<?php foreach ( $order as $option_value => $option_label ) : ?> |
| 230 |
<option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['order'], $option_value ); ?>><?php echo esc_html( $option_label ); ?></option> |
| 231 |
<?php endforeach; ?> |
| 232 |
</select> |
| 233 |
</p> |
| 234 |
<p> |
| 235 |
<label for="<?php echo $this->get_field_id( 'role' ); ?>"><?php esc_html_e( 'Role:', 'members' ); ?></label> |
| 236 |
<select class="widefat" id="<?php echo $this->get_field_id( 'role' ); ?>" name="<?php echo $this->get_field_name( 'role' ); ?>"> |
| 237 |
<option value="" <?php selected( $instance['role'], '' ); ?>></option> |
| 238 |
<?php foreach ( $roles as $name => $role ) : ?> |
| 239 |
<option value="<?php echo esc_attr( $name ); ?>" <?php selected( $instance['role'], $name ); ?>><?php echo esc_html( $role->get( 'label' ) ); ?></option> |
| 240 |
<?php endforeach; ?> |
| 241 |
</select> |
| 242 |
</p> |
| 243 |
<p> |
| 244 |
<label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php esc_html_e( 'Limit:', 'members' ); ?></label> |
| 245 |
<input type="number" min="0" class="widefat code" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo esc_attr( $instance['number'] ); ?>" /> |
| 246 |
</p> |
| 247 |
<p> |
| 248 |
<label for="<?php echo $this->get_field_id( 'offset' ); ?>"><?php esc_html_e( 'Offset:', 'members' ); ?></label> |
| 249 |
<input type="number" min="1" class="widefat code" id="<?php echo $this->get_field_id( 'offset' ); ?>" name="<?php echo $this->get_field_name( 'offset' ); ?>" value="<?php echo esc_attr( $instance['offset'] ); ?>" /> |
| 250 |
</p> |
| 251 |
|
| 252 |
</div> |
| 253 |
<div style="float: right; width: 48%;"> |
| 254 |
|
| 255 |
<p> |
| 256 |
<label for="<?php echo $this->get_field_id( 'include' ); ?>"><?php esc_html_e( 'Include:', 'members' ); ?></label> |
| 257 |
<input type="text" class="widefat code" id="<?php echo $this->get_field_id( 'include' ); ?>" name="<?php echo $this->get_field_name( 'include' ); ?>" value="<?php echo esc_attr( $instance['include'] ); ?>" /> |
| 258 |
</p> |
| 259 |
<p> |
| 260 |
<label for="<?php echo $this->get_field_id( 'exclude' ); ?>"><?php esc_html_e( 'Exclude:', 'members' ); ?></label> |
| 261 |
<input type="text" class="widefat code" id="<?php echo $this->get_field_id( 'exclude' ); ?>" name="<?php echo $this->get_field_name( 'exclude' ); ?>" value="<?php echo esc_attr( $instance['exclude'] ); ?>" /> |
| 262 |
</p> |
| 263 |
<p> |
| 264 |
<label for="<?php echo $this->get_field_id( 'search' ); ?>"><?php esc_html_e( 'Search:', 'members' ); ?></label> |
| 265 |
<input type="text" class="widefat code" id="<?php echo $this->get_field_id( 'search' ); ?>" name="<?php echo $this->get_field_name( 'search' ); ?>" value="<?php echo esc_attr( $instance['search'] ); ?>" /> |
| 266 |
</p> |
| 267 |
<p> |
| 268 |
<label for="<?php echo $this->get_field_id( 'meta_key' ); ?>"><?php esc_html_e( 'Meta Key:', 'members' ); ?></label> |
| 269 |
<select class="widefat" id="<?php echo $this->get_field_id( 'meta_key' ); ?>" name="<?php echo $this->get_field_name( 'meta_key' ); ?>"> |
| 270 |
<?php foreach ( $meta_key as $meta ) { ?> |
| 271 |
<option value="<?php echo esc_attr( $meta ); ?>" <?php selected( $instance['meta_key'], $meta ); ?>><?php echo esc_html( $meta ); ?></option> |
| 272 |
<?php } ?> |
| 273 |
</select> |
| 274 |
</p> |
| 275 |
<p> |
| 276 |
<label for="<?php echo $this->get_field_id( 'meta_value' ); ?>"><?php esc_html_e( 'Meta Value:', 'members' ); ?></label> |
| 277 |
<input type="text" class="widefat code" id="<?php echo $this->get_field_id( 'meta_value' ); ?>" name="<?php echo $this->get_field_name( 'meta_value' ); ?>" value="<?php echo esc_attr( $instance['meta_value'] ); ?>" /> |
| 278 |
</p> |
| 279 |
|
| 280 |
</div> |
| 281 |
|
| 282 |
<div style="clear:both;"> </div> |
| 283 |
<?php |
| 284 |
} |
| 285 |
} |
| 286 |
|