PluginProbe
Groups – Memberships and Access Control / 1.10.3
Groups – Memberships and Access Control v1.10.3
4.7.1 4.7.0 4.6.0 4.5.0 4.4.0 4.3.0 trunk 1.0.0-beta-1 1.0.0-beta-2 1.0.0-beta-3 1.0.0-beta-3b 1.0.0-beta-3c 1.0.0-beta-3d 1.1.4 1.1.5 1.10.0 1.10.1 1.10.2 1.10.3 1.11.0 1.11.1 1.11.2 1.11.3 1.12.0 1.13.0 All 131 releases
groups / lib / admin / groups-admin-groups.php

groups-admin-groups.php in Groups – Memberships and Access Control 1.10.3, at lib/admin/groups-admin-groups.php

546 lines 18.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * groups-admin-groups.php
4 *
5 * Copyright (c) "kento" Karim Rahimpur www.itthinx.com
6 *
7 * This code is released under the GNU General Public License.
8 * See COPYRIGHT.txt and LICENSE.txt.
9 *
10 * This code is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * This header and all notices must be kept intact.
16 *
17 * @author Karim Rahimpur
18 * @package groups
19 * @since groups 1.0.0
20 */
21
22 if ( !defined( 'ABSPATH' ) ) {
23 exit;
24 }
25
26 // admin defines
27 define( 'GROUPS_GROUPS_PER_PAGE', 10 );
28 define( 'GROUPS_ADMIN_GROUPS_NONCE_1', 'groups-nonce-1');
29 define( 'GROUPS_ADMIN_GROUPS_NONCE_2', 'groups-nonce-2');
30 define( 'GROUPS_ADMIN_GROUPS_ACTION_NONCE', 'groups-action-nonce');
31 define( 'GROUPS_ADMIN_GROUPS_FILTER_NONCE', 'groups-filter-nonce' );
32
33 require_once( GROUPS_CORE_LIB . '/class-groups-pagination.php' );
34 require_once( GROUPS_ADMIN_LIB . '/groups-admin-groups-add.php');
35 require_once( GROUPS_ADMIN_LIB . '/groups-admin-groups-edit.php');
36 require_once( GROUPS_ADMIN_LIB . '/groups-admin-groups-remove.php');
37
38 /**
39 * Manage Groups: table of groups and add, edit, remove actions.
40 */
41 function groups_admin_groups() {
42
43 global $wpdb;
44
45 $output = '';
46 $today = date( 'Y-m-d', time() );
47
48 if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
49 wp_die( __( 'Access denied.', GROUPS_PLUGIN_DOMAIN ) );
50 }
51
52 //
53 // handle actions
54 //
55 if ( isset( $_POST['action'] ) ) {
56 // handle action submit - do it
57 switch( $_POST['action'] ) {
58 case 'add' :
59 if ( !( $group_id = groups_admin_groups_add_submit() ) ) {
60 return groups_admin_groups_add();
61 } else {
62 $group = Groups_Group::read( $group_id );
63 Groups_Admin::add_message( sprintf( __( "The <em>%s</em> group has been created.", GROUPS_PLUGIN_DOMAIN ), stripslashes( wp_filter_nohtml_kses( $group->name ) ) ) );
64 }
65 break;
66 case 'edit' :
67 if ( !( $group_id = groups_admin_groups_edit_submit() ) ) {
68 return groups_admin_groups_edit( $_POST['group-id-field'] );
69 } else {
70 $group = Groups_Group::read( $group_id );
71 Groups_Admin::add_message( sprintf( __( 'The <em>%s</em> group has been updated.', GROUPS_PLUGIN_DOMAIN ), stripslashes( wp_filter_nohtml_kses( $group->name ) ) ) );
72 }
73 break;
74 case 'remove' :
75 if ( $group_id = groups_admin_groups_remove_submit() ) {
76 Groups_Admin::add_message( __( 'The group has been deleted.', GROUPS_PLUGIN_DOMAIN ) );
77 }
78 break;
79 // bulk actions on groups: add capabilities, remove capabilities, remove groups
80 case 'groups-action' :
81 if ( wp_verify_nonce( $_POST[GROUPS_ADMIN_GROUPS_ACTION_NONCE], 'admin' ) ) {
82 $group_ids = isset( $_POST['group_ids'] ) ? $_POST['group_ids'] : null;
83 $bulk_action = null;
84 if ( isset( $_POST['bulk'] ) ) {
85 $bulk_action = $_POST['bulk-action'];
86 }
87 if ( is_array( $group_ids ) && ( $bulk_action !== null ) ) {
88 foreach ( $group_ids as $group_id ) {
89 switch ( $bulk_action ) {
90 case 'add-capability' :
91 $capabilities_id = isset( $_POST['capability_id'] ) ? $_POST['capability_id'] : null;
92 if ( $capabilities_id !== null ) {
93 foreach ( $capabilities_id as $capability_id ) {
94 Groups_Group_Capability::create( array( 'group_id' => $group_id, 'capability_id' => $capability_id ) );
95 }
96 }
97 break;
98 case 'remove-capability' :
99 $capabilities_id = isset( $_POST['capability_id'] ) ? $_POST['capability_id'] : null;
100 if ( $capabilities_id !== null ) {
101 foreach ( $capabilities_id as $capability_id ) {
102 Groups_Group_Capability::delete( $group_id, $capability_id );
103 }
104 }
105 break;
106 case 'remove-group' :
107 $bulk_confirm = isset( $_POST['confirm'] ) ? true : false;
108 if ( $bulk_confirm ) {
109 groups_admin_groups_bulk_remove_submit();
110 } else {
111 return groups_admin_groups_bulk_remove();
112 }
113 break;
114 }
115 }
116 }
117 }
118 break;
119 }
120 } else if ( isset ( $_GET['action'] ) ) {
121 // handle action request - show form
122 switch( $_GET['action'] ) {
123 case 'add' :
124 return groups_admin_groups_add();
125 break;
126 case 'edit' :
127 if ( isset( $_GET['group_id'] ) ) {
128 return groups_admin_groups_edit( $_GET['group_id'] );
129 }
130 break;
131 case 'remove' :
132 if ( isset( $_GET['group_id'] ) ) {
133 return groups_admin_groups_remove( $_GET['group_id'] );
134 }
135 break;
136 }
137 }
138
139 //
140 // group table
141 //
142 if (
143 isset( $_POST['clear_filters'] ) ||
144 isset( $_POST['group_id'] ) ||
145 isset( $_POST['group_name'] )
146 ) {
147 if ( !wp_verify_nonce( $_POST[GROUPS_ADMIN_GROUPS_FILTER_NONCE], 'admin' ) ) {
148 wp_die( __( 'Access denied.', GROUPS_PLUGIN_DOMAIN ) );
149 }
150 }
151
152 // filters
153 $group_id = Groups_Options::get_user_option( 'groups_group_id', null );
154 $group_name = Groups_Options::get_user_option( 'groups_group_name', null );
155
156 if ( isset( $_POST['clear_filters'] ) ) {
157 Groups_Options::delete_user_option( 'groups_group_id' );
158 Groups_Options::delete_user_option( 'groups_group_name' );
159 $group_id = null;
160 $group_name = null;
161 } else if ( isset( $_POST['submitted'] ) ) {
162 // filter by name
163 if ( !empty( $_POST['group_name'] ) ) {
164 $group_name = $_POST['group_name'];
165 Groups_Options::update_user_option( 'groups_group_name', $group_name );
166 }
167 // filter by group id
168 if ( !empty( $_POST['group_id'] ) ) {
169 $group_id = intval( $_POST['group_id'] );
170 Groups_Options::update_user_option( 'groups_group_id', $group_id );
171 } else if ( isset( $_POST['group_id'] ) ) { // empty && isset => '' => all
172 $group_id = null;
173 Groups_Options::delete_user_option( 'groups_group_id' );
174 }
175 }
176
177 if ( isset( $_POST['row_count'] ) ) {
178 if ( !wp_verify_nonce( $_POST[GROUPS_ADMIN_GROUPS_NONCE_1], 'admin' ) ) {
179 wp_die( __( 'Access denied.', GROUPS_PLUGIN_DOMAIN ) );
180 }
181 }
182
183 if ( isset( $_POST['paged'] ) ) {
184 if ( !wp_verify_nonce( $_POST[GROUPS_ADMIN_GROUPS_NONCE_2], 'admin' ) ) {
185 wp_die( __( 'Access denied.', GROUPS_PLUGIN_DOMAIN ) );
186 }
187 }
188
189 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
190 $current_url = remove_query_arg( 'paged', $current_url );
191 $current_url = remove_query_arg( 'action', $current_url );
192 $current_url = remove_query_arg( 'group_id', $current_url );
193
194 $group_table = _groups_get_tablename( 'group' );
195
196 $output .=
197 '<div class="manage-groups wrap">' .
198 '<h1>' .
199 _x( 'Groups', 'page-title', GROUPS_PLUGIN_DOMAIN ) .
200 sprintf(
201 '<a title="%s" class="add page-title-action" href="%s">',
202 esc_attr( __( 'Click to add a new group', GROUPS_PLUGIN_DOMAIN ) ),
203 esc_url( $current_url . '&action=add' )
204 ) .
205 sprintf(
206 '<img class="icon" alt="%s" src="%s" />',
207 esc_attr( __( 'Add', GROUPS_PLUGIN_DOMAIN ) ),
208 esc_url( GROUPS_PLUGIN_URL . 'images/add.png' )
209 ) .
210 sprintf(
211 '<span class="label">%s</span>',
212 stripslashes( wp_filter_nohtml_kses( __( 'New Group', GROUPS_PLUGIN_DOMAIN ) ) )
213 ) .
214 '</a>' .
215 '</h1>';
216
217 $output .= Groups_Admin::render_messages();
218
219 $row_count = isset( $_POST['row_count'] ) ? intval( $_POST['row_count'] ) : 0;
220
221 if ($row_count <= 0) {
222 $row_count = Groups_Options::get_user_option( 'groups_per_page', GROUPS_GROUPS_PER_PAGE );
223 } else {
224 Groups_Options::update_user_option('groups_per_page', $row_count );
225 }
226 $offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0;
227 if ( $offset < 0 ) {
228 $offset = 0;
229 }
230 $paged = isset( $_REQUEST['paged'] ) ? intval( $_REQUEST['paged'] ) : 0;
231 if ( $paged < 0 ) {
232 $paged = 0;
233 }
234
235 $orderby = isset( $_GET['orderby'] ) ? $_GET['orderby'] : null;
236 switch ( $orderby ) {
237 case 'group_id' :
238 case 'name' :
239 case 'description' :
240 break;
241 default:
242 $orderby = 'name';
243 }
244
245 $order = isset( $_GET['order'] ) ? $_GET['order'] : null;
246 switch ( $order ) {
247 case 'asc' :
248 case 'ASC' :
249 $switch_order = 'DESC';
250 break;
251 case 'desc' :
252 case 'DESC' :
253 $switch_order = 'ASC';
254 break;
255 default:
256 $order = 'ASC';
257 $switch_order = 'DESC';
258 }
259
260 $filters = array( " 1=%d " );
261 $filter_params = array( 1 );
262 if ( $group_id ) {
263 $filters[] = " $group_table.group_id = %d ";
264 $filter_params[] = $group_id;
265 }
266 if ( $group_name ) {
267 $filters[] = " $group_table.name LIKE '%%%s%%' ";
268 $filter_params[] = $group_name;
269 }
270
271 if ( !empty( $filters ) ) {
272 $filters = " WHERE " . implode( " AND ", $filters );
273 } else {
274 $filters = '';
275 }
276
277 $count_query = $wpdb->prepare( "SELECT COUNT(*) FROM $group_table $filters", $filter_params );
278 $count = $wpdb->get_var( $count_query );
279 if ( $count > $row_count ) {
280 $paginate = true;
281 } else {
282 $paginate = false;
283 }
284 $pages = ceil ( $count / $row_count );
285 if ( $paged > $pages ) {
286 $paged = $pages;
287 }
288 if ( $paged != 0 ) {
289 $offset = ( $paged - 1 ) * $row_count;
290 }
291
292 $query = $wpdb->prepare(
293 "SELECT * FROM $group_table
294 $filters
295 ORDER BY $orderby $order
296 LIMIT $row_count OFFSET $offset",
297 $filter_params
298 );
299
300 $results = $wpdb->get_results( $query, OBJECT );
301
302 $column_display_names = array(
303 'group_id' => __( 'ID', GROUPS_PLUGIN_DOMAIN ),
304 'name' => __( 'Group', GROUPS_PLUGIN_DOMAIN ),
305 'description' => __( 'Description', GROUPS_PLUGIN_DOMAIN ),
306 'capabilities' => __( 'Capabilities', GROUPS_PLUGIN_DOMAIN )
307 );
308
309 $output .= '<div class="groups-overview">';
310
311 $output .=
312 '<div class="filters">' .
313 '<form id="setfilters" action="" method="post">' .
314 '<fieldset>' .
315 '<legend>' . __( 'Filters', GROUPS_PLUGIN_DOMAIN ) . '</legend>' .
316 '<label class="group-id-filter">' . __( 'Group ID', GROUPS_PLUGIN_DOMAIN ) . ' ' .
317 '<input class="group-id-filter" name="group_id" type="text" value="' . esc_attr( $group_id ) . '"/>' .
318 '</label>' . ' ' .
319 '<label class="group-name-filter">' . __( 'Group Name', GROUPS_PLUGIN_DOMAIN ) . ' ' .
320 '<input class="group-name-filter" name="group_name" type="text" value="' . $group_name . '"/>' .
321 '</label>' . ' ' .
322 wp_nonce_field( 'admin', GROUPS_ADMIN_GROUPS_FILTER_NONCE, true, false ) .
323 '<input class="button" type="submit" value="' . __( 'Apply', GROUPS_PLUGIN_DOMAIN ) . '"/>' . ' ' .
324 '<input class="button" type="submit" name="clear_filters" value="' . __( 'Clear', GROUPS_PLUGIN_DOMAIN ) . '"/>' .
325 '<input type="hidden" value="submitted" name="submitted"/>' .
326 '</fieldset>' .
327 '</form>' .
328 '</div>';
329
330 if ( $paginate ) {
331 require_once( GROUPS_CORE_LIB . '/class-groups-pagination.php' );
332 $pagination = new Groups_Pagination( $count, null, $row_count );
333 $output .= '<form id="posts-filter" method="post" action="">';
334 $output .= '<div>';
335 $output .= wp_nonce_field( 'admin', GROUPS_ADMIN_GROUPS_NONCE_2, true, false );
336 $output .= '</div>';
337 $output .= '<div class="tablenav top">';
338 $output .= $pagination->pagination( 'top' );
339 $output .= '</div>';
340 $output .= '</form>';
341 }
342
343 $output .= '<div class="page-options right">';
344 $output .= '<form id="setrowcount" action="" method="post">';
345 $output .= '<div>';
346 $output .= '<label for="row_count">' . __('Results per page', GROUPS_PLUGIN_DOMAIN ) . '</label>';
347 $output .= '<input name="row_count" type="text" size="2" value="' . esc_attr( $row_count ) .'" />';
348 $output .= wp_nonce_field( 'admin', GROUPS_ADMIN_GROUPS_NONCE_1, true, false );
349 $output .= '<input class="button" type="submit" value="' . __( 'Apply', GROUPS_PLUGIN_DOMAIN ) . '"/>';
350 $output .= '</div>';
351 $output .= '</form>';
352 $output .= '</div>';
353
354 $capability_table = _groups_get_tablename( "capability" );
355 $group_capability_table = _groups_get_tablename( "group_capability" );
356
357 // capabilities select
358 $capabilities = $wpdb->get_results( "SELECT * FROM $capability_table ORDER BY capability" );
359 $capabilities_select = sprintf(
360 '<select class="select capability" name="capability_id[]" multiple="multiple" placeholder="%s" data-placeholder="%s">',
361 esc_attr( __( 'Capabilities &hellip;', GROUPS_PLUGIN_DOMAIN ) ) ,
362 esc_attr( __( 'Capabilities &hellip;', GROUPS_PLUGIN_DOMAIN ) )
363 );
364 foreach( $capabilities as $capability ) {
365 $capabilities_select .= sprintf( '<option value="%s">%s</option>', esc_attr( $capability->capability_id ), wp_filter_nohtml_kses( $capability->capability ) );
366 }
367 $capabilities_select .= '</select>';
368 $capabilities_select .= Groups_UIE::render_select( '.select.capability' );
369
370 $output .= '<form id="groups-action" method="post" action="">';
371
372 $output .= '<div class="tablenav top">';
373
374 $output .= '<div class="groups-bulk-container">';
375 $output .= '<div class="capabilities-select-container">';
376 $output .= $capabilities_select;
377 $output .= wp_nonce_field( 'admin', GROUPS_ADMIN_GROUPS_ACTION_NONCE, true, false );
378 $output .= '</div>';
379 $output .= '<select class="bulk-action" name="bulk-action">';
380 $output .= '<option selected="selected" value="-1">' . esc_html( __( 'Bulk Actions', GROUPS_PLUGIN_DOMAIN ) ) . '</option>';
381 $output .= '<option value="remove-group">' . esc_html( __( 'Remove group', GROUPS_PLUGIN_DOMAIN ) ) . '</option>';
382 $output .= '<option value="add-capability">' . esc_html( __( 'Add capability', GROUPS_PLUGIN_DOMAIN ) ) . '</option>';
383 $output .= '<option value="remove-capability">' . esc_html( __( 'Remove capability', GROUPS_PLUGIN_DOMAIN ) ) . '</option>';
384 $output .= '</select>';
385 $output .= sprintf( '<input class="button" type="submit" name="bulk" value="%s" />', esc_attr( __( 'Apply', GROUPS_PLUGIN_DOMAIN ) ) );
386 $output .= '<input type="hidden" name="action" value="groups-action"/>';
387 $output .= '</div>';
388 $output .= '</div>';
389
390 $output .= '<table id="" class="wp-list-table widefat fixed" cellspacing="0">';
391 $output .= '<thead>';
392 $output .= '<tr>';
393
394 $output .= '<th id="cb" class="manage-column column-cb check-column" scope="col"><input type="checkbox"></th>';
395
396 foreach ( $column_display_names as $key => $column_display_name ) {
397 $options = array(
398 'orderby' => $key,
399 'order' => $switch_order
400 );
401 $class = $key;
402 if ( !in_array( $key, array( 'capabilities' ) ) ) {
403 if ( strcmp( $key, $orderby ) == 0 ) {
404 $lorder = strtolower( $order );
405 $class = "$key manage-column sorted $lorder";
406 } else {
407 $class = "$key manage-column sortable";
408 }
409 $column_display_name =
410 sprintf(
411 '<a href="%s"><span>%s</span><span class="sorting-indicator"></span></a>',
412 esc_url( add_query_arg( $options, $current_url ) ),
413 esc_html( $column_display_name )
414 );
415 } else {
416 $column_display_name = esc_html( $column_display_name );
417 }
418 $output .= sprintf(
419 '<th scope="col" class="%s">%s</th>',
420 esc_attr( $class ),
421 $column_display_name
422 );
423 }
424
425 $output .= '</tr>';
426 $output .= '</thead>';
427 $output .= '<tbody>';
428
429 if ( count( $results ) > 0 ) {
430 for ( $i = 0; $i < count( $results ); $i++ ) {
431
432 $result = $results[$i];
433
434 // Construct the "edit" URL.
435 $edit_url = add_query_arg(
436 array(
437 'group_id' => intval( $result->group_id ),
438 'action' => 'edit',
439 'paged' => $paged
440 ),
441 $current_url
442 );
443
444 // Construct the "delete" URL.
445 $delete_url = add_query_arg(
446 array(
447 'group_id' => intval( $result->group_id ),
448 'action' => 'remove',
449 'paged' => $paged
450 ),
451 $current_url
452 );
453
454 // Construct row actions for this group.
455 $row_actions =
456 '<div class="row-actions">' .
457 '<span class="edit">' .
458 '<a href="' . esc_url( $edit_url ) . '">' .
459 '<img src="' . GROUPS_PLUGIN_URL . 'images/edit.png"/>' .
460 __( 'Edit', GROUPS_PLUGIN_DOMAIN ) .
461 '</a>';
462 if ( $result->name !== Groups_Registered::REGISTERED_GROUP_NAME ) {
463 $row_actions .=
464 ' | ' .
465 '</span>' .
466 '<span class="remove trash">' .
467 '<a href="' . esc_url( $delete_url ) . '" class="submitdelete">' .
468 '<img src="' . GROUPS_PLUGIN_URL . 'images/remove.png"/>' .
469 __( 'Remove', GROUPS_PLUGIN_DOMAIN ) .
470 '</a>' .
471 '</span>';
472 }
473 $row_actions .= '</div>'; // .row-actions
474
475 $output .= '<tr class="' . ( $i % 2 == 0 ? 'even' : 'odd' ) . '">';
476
477 $output .= '<th class="check-column">';
478 $output .= '<input type="checkbox" value="' . esc_attr( $result->group_id ) . '" name="group_ids[]"/>';
479 $output .= '</th>';
480
481 $output .= '<td class="group-id">';
482 $output .= $result->group_id;
483 $output .= '</td>';
484 $output .= '<td class="group-name">';
485 $output .= sprintf( '<a href="%s">%s</a>', esc_url( $edit_url ), stripslashes( wp_filter_nohtml_kses( $result->name ) ) );
486 $output .= $row_actions;
487 $output .= '</td>';
488 $output .= '<td class="group-description">';
489 $output .= stripslashes( wp_filter_nohtml_kses( $result->description ) );
490 $output .= '</td>';
491
492 $output .= '<td class="capabilities">';
493
494 $group = new Groups_Group( $result->group_id );
495 $group_capabilities = $group->capabilities;
496 $group_capabilities_deep = $group->capabilities_deep;
497 usort( $group_capabilities_deep, array( 'Groups_Utility', 'cmp' ) );
498
499 if ( count( $group_capabilities_deep ) > 0 ) {
500 $output .= '<ul>';
501 foreach ( $group_capabilities_deep as $group_capability ) {
502 $output .= '<li>';
503 $class = '';
504 if ( empty( $group_capabilities ) || !in_array( $group_capability, $group_capabilities ) ) {
505 $class = 'inherited';
506 }
507 $output .= sprintf( '<span class="%s">', $class );
508 if ( isset( $group_capability->capability ) && isset( $group_capability->capability->capability ) ) {
509 $output .= wp_filter_nohtml_kses( $group_capability->capability->capability );
510 }
511 $output .= '</span>';
512 $output .= '</li>';
513 }
514 $output .= '</ul>';
515 } else {
516 $output .= __( 'This group has no capabilities.', GROUPS_PLUGIN_DOMAIN );
517 }
518 $output .= '</td>';
519
520 $output .= '</tr>';
521 }
522 } else {
523 $output .= '<tr><td colspan="4">' . __( 'There are no results.', GROUPS_PLUGIN_DOMAIN ) . '</td></tr>';
524 }
525
526 $output .= '</tbody>';
527 $output .= '</table>';
528
529 $output .= Groups_UIE::render_add_titles( '.groups-overview table td' );
530
531 $output .= '</form>'; // #groups-action
532
533 if ( $paginate ) {
534 require_once( GROUPS_CORE_LIB . '/class-groups-pagination.php' );
535 $pagination = new Groups_Pagination($count, null, $row_count);
536 $output .= '<div class="tablenav bottom">';
537 $output .= $pagination->pagination( 'bottom' );
538 $output .= '</div>';
539 }
540
541 $output .= '</div>'; // .groups-overview
542 $output .= '</div>'; // .manage-groups
543
544 echo $output;
545 } // function groups_admin_groups()
546