-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadmin.php
More file actions
189 lines (181 loc) · 6.12 KB
/
admin.php
File metadata and controls
189 lines (181 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* code based on wp-hasty.com
* https://www.wp-hasty.com/tools/wordpress-settings-options-page-generator/
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class User_Listing_Settings_Page {
public function __construct() {
add_action( 'admin_menu', array( $this, 'create_settings' ) );
add_action( 'admin_init', array( $this, 'setup_sections' ) );
add_action( 'admin_init', array( $this, 'setup_fields' ) );
}
public function create_settings() {
$page_title = 'WP User Listing';
$menu_title = 'User Listing';
$capability = 'manage_options';
$slug = 'userlisting';
$callback = array( $this, 'settings_content' );
$icon = 'dashicons-admin-settings';
$position = 80;
add_menu_page( $page_title, $menu_title, $capability, $slug, $callback, $icon, $position );
}
public function settings_content() { ?>
<div class="wrap user-listing">
<h1>WP User Listing</h1>
<?php settings_errors(); ?>
<div class="setting-controls">
<?php
settings_fields( 'userlisting' );
do_settings_sections( 'userlisting' );
//print_r(get_role_names());
//submit_button();
$args = [
'number' => 10 //max number of users 10
];
$users = new WP_User_Query( $args );
$all_users = $users->get_results();
$total_users = $users->get_total();//Get result users count For Pagination
?>
<button class="sort-apply blue-button button button-primary" data-offset="0" type="button">Apply</button>
</div>
<br>
<br>
<div class="table-template">
<?php if ( $total_users ) {
$total_pages = ceil( $total_users / 10 ); //count pages based on 10 users of the given result per page
?>
<div class="tablenav users-pagination">
<?php if ( $total_pages > 1 ) { ?>
<div class="tablenav-pages">
<div class="pagination-links">
<?php
$o = 0;
for ( $p = 1; $p < $total_pages + 1; $p ++ ) {
?>
<a class="next-page sort-apply<?php if ( ! $o ) {
echo ' active-button';
} ?>" data-page-number="<?php echo esc_attr( $p ); ?>"
data-offset="<?php echo esc_attr( $o ); ?>" href="#">
<span aria-hidden="true"><?php echo esc_html( $p ); ?></span>
</a>
<?php
$o += 10;
}
?>
</div>
</div>
<?php } ?>
</div>
<?php } ?>
<table class="widefat" cellspacing="0">
<thead>
<tr>
<th id="hash-column" class="manage-column column-columnname num" scope="col">#</th>
<th id="id-column" class="manage-column column-columnname num" scope="col">ID</th>
<th id="name-columnname" class="manage-column column-columnname" scope="col">Name</th>
<th id="user-name-columnname" class="manage-column column-columnname" scope="col">User Name</th>
<th id="user-name-columnname" class="manage-column column-columnname" scope="col">Role
</th>
</tr>
</thead>
<tbody>
<?php bb_users_list_html( $all_users ); ?>
</tbody>
</table>
</div>
</div>
<?php
}
public function setup_sections() {
add_settings_section( 'userlisting_section', 'Display WordPress Users With Some Filters', array(), 'userlisting' );
}
public function setup_fields() {
$fields = array(
array(
'id' => 'bb_role_names_filter',
'label' => esc_html__( 'Roles Filter', 'bbioon' ),
'type' => 'select',
'section' => 'userlisting_section',
'options' => bb_get_role_names(),
'desc' => '',
),
array(
'id' => 'bb_sorting_by',
'default' => 'name',
'label' => esc_html__( 'Order By', 'bbioon' ),
'type' => 'select',
'section' => 'userlisting_section',
'options' => array(
'display_name' => esc_html__( 'Name', 'bbioon' ),
'user_login' => esc_html__( 'User Name', 'bbioon' )
),
'desc' => ''
),
array(
'id' => 'bb_sorting_method',
'default' => 'ASC',
'label' => esc_html__( 'Order', 'bbioon' ),
'type' => 'select',
'section' => 'userlisting_section',
'options' => array(
'ASC' => esc_html__( 'Ascending', 'bbioon' ),
'DESC' => esc_html__( 'descending', 'bbioon' )
),
'desc' => ''
)
);
foreach ( $fields as $field ) {
add_settings_field( $field['id'], $field['label'], array(
$this,
'field_callback'
), 'userlisting', $field['section'], $field );
register_setting( 'userlisting', $field['id'] );
}
}
public function field_callback( $field ) {
$value = get_option( $field['id'] );
if ( ! isset( $field['default'] ) ) {
$field['default'] = '';
}
switch ( $field['type'] ) {
case 'select':
if ( empty( $value ) ) {
$value = array( $field['default'] );
}
if ( ! empty ( $field['options'] ) && is_array( $field['options'] ) ) {
$attr = '';
$options = '';
foreach ( $field['options'] as $key => $label ) {
$options .= sprintf( '<option value="%s" %s>%s</option>',
$key,
selected( $value[ array_search( $key, $value, true ) ], $key, false ),
$label
);
}
if ( $field['type'] === 'multiselect' ) {
$attr = ' multiple="multiple" ';
}
printf( '<select name="%1$s[]" id="%1$s" %2$s>%3$s</select>',
$field['id'],
$attr,
$options
);
}
break;
default:
printf( '<input name="%1$s" id="%1$s" type="%2$s" placeholder="%3$s" value="%4$s" />',
$field['id'],
$field['type'],
$field['placeholder'],
$value
);
}
if ( $desc = $field['desc'] ) {
printf( '<p class="description">%s </p>', $desc );
}
}
}
new User_Listing_Settings_Page();