PluginProbe
Groups – Memberships and Access Control / 1.1.5
Groups – Memberships and Access Control v1.1.5
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.1.5, at lib/admin/groups-admin-groups.php

440 lines 14.7 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 // admin defines
23 define( 'GROUPS_GROUPS_PER_PAGE', 10 );
24 define( 'GROUPS_ADMIN_GROUPS_NONCE_1', 'groups-nonce-1');
25 define( 'GROUPS_ADMIN_GROUPS_NONCE_2', 'groups-nonce-2');
26 define( 'GROUPS_ADMIN_GROUPS_ACTION_NONCE', 'groups-action-nonce');
27 define( 'GROUPS_ADMIN_GROUPS_FILTER_NONCE', 'groups-filter-nonce' );
28
29 require_once( GROUPS_CORE_LIB . '/class-groups-pagination.php' );
30 require_once( GROUPS_ADMIN_LIB . '/groups-admin-groups-add.php');
31 require_once( GROUPS_ADMIN_LIB . '/groups-admin-groups-edit.php');
32 require_once( GROUPS_ADMIN_LIB . '/groups-admin-groups-remove.php');
33
34 /**
35 * Manage Groups: table of groups and add, edit, remove actions.
36 */
37 function groups_admin_groups() {
38
39 global $wpdb;
40
41 $output = '';
42 $today = date( 'Y-m-d', time() );
43
44 if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
45 wp_die( __( 'Access denied.', GROUPS_PLUGIN_DOMAIN ) );
46 }
47
48 //
49 // handle actions
50 //
51 if ( isset( $_POST['action'] ) ) {
52 // handle action submit - do it
53 switch( $_POST['action'] ) {
54 case 'add' :
55 if ( !groups_admin_groups_add_submit() ) {
56 return groups_admin_groups_add();
57 }
58 break;
59 case 'edit' :
60 if ( !groups_admin_groups_edit_submit() ) {
61 return groups_admin_groups_edit( $_POST['group-id-field'] );
62 }
63 break;
64 case 'remove' :
65 groups_admin_groups_remove_submit();
66 break;
67 // bulk actions on groups: capabilities
68 case 'groups-action' :
69 if ( wp_verify_nonce( $_POST[GROUPS_ADMIN_GROUPS_ACTION_NONCE], 'admin' ) ) {
70 $group_ids = isset( $_POST['group_ids'] ) ? $_POST['group_ids'] : null;
71 $subaction = isset( $_POST['add'] ) ? $_POST['add'] : ( isset( $_POST['remove'] ) ? $_POST['remove'] : null );
72 $capability_id = isset( $_POST['capability_id'] ) ? $_POST['capability_id'] : null;
73 if ( is_array( $group_ids ) && ( $subaction !== null ) && ( $capability_id !== null ) ) {
74 foreach ( $group_ids as $group_id ) {
75 switch ( $subaction ) {
76 case 'Add' :
77 Groups_Group_Capability::create( array( 'group_id' => $group_id, 'capability_id' => $capability_id ) );
78 break;
79 case 'Remove' :
80 Groups_Group_Capability::delete( $group_id, $capability_id );
81 break;
82 }
83 }
84 }
85 }
86 break;
87 }
88 } else if ( isset ( $_GET['action'] ) ) {
89 // handle action request - show form
90 switch( $_GET['action'] ) {
91 case 'add' :
92 return groups_admin_groups_add();
93 break;
94 case 'edit' :
95 if ( isset( $_GET['group_id'] ) ) {
96 return groups_admin_groups_edit( $_GET['group_id'] );
97 }
98 break;
99 case 'remove' :
100 if ( isset( $_GET['group_id'] ) ) {
101 return groups_admin_groups_remove( $_GET['group_id'] );
102 }
103 break;
104 }
105 }
106
107 //
108 // group table
109 //
110 if (
111 isset( $_POST['clear_filters'] ) ||
112 isset( $_POST['group_id'] ) ||
113 isset( $_POST['group_name'] )
114 ) {
115 if ( !wp_verify_nonce( $_POST[GROUPS_ADMIN_GROUPS_FILTER_NONCE], 'admin' ) ) {
116 wp_die( __( 'Access denied.', GROUPS_PLUGIN_DOMAIN ) );
117 }
118 }
119
120 // filters
121 $group_id = Groups_Options::get_user_option( 'groups_group_id', null );
122 $group_name = Groups_Options::get_user_option( 'groups_group_name', null );
123
124 if ( isset( $_POST['clear_filters'] ) ) {
125 Groups_Options::delete_user_option( 'groups_group_id' );
126 Groups_Options::delete_user_option( 'groups_group_name' );
127 $group_id = null;
128 $group_name = null;
129 } else if ( isset( $_POST['submitted'] ) ) {
130 // filter by name
131 if ( !empty( $_POST['group_name'] ) ) {
132 $group_name = $_POST['group_name'];
133 Groups_Options::update_user_option( 'groups_group_name', $group_name );
134 }
135 // filter by group id
136 if ( !empty( $_POST['group_id'] ) ) {
137 $group_id = intval( $_POST['group_id'] );
138 Groups_Options::update_user_option( 'groups_group_id', $group_id );
139 } else if ( isset( $_POST['group_id'] ) ) { // empty && isset => '' => all
140 $group_id = null;
141 Groups_Options::delete_user_option( 'groups_group_id' );
142 }
143 }
144
145 if ( isset( $_POST['row_count'] ) ) {
146 if ( !wp_verify_nonce( $_POST[GROUPS_ADMIN_GROUPS_NONCE_1], 'admin' ) ) {
147 wp_die( __( 'Access denied.', GROUPS_PLUGIN_DOMAIN ) );
148 }
149 }
150
151 if ( isset( $_POST['paged'] ) ) {
152 if ( !wp_verify_nonce( $_POST[GROUPS_ADMIN_GROUPS_NONCE_2], 'admin' ) ) {
153 wp_die( __( 'Access denied.', GROUPS_PLUGIN_DOMAIN ) );
154 }
155 }
156
157 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
158 $current_url = remove_query_arg( 'paged', $current_url );
159 $current_url = remove_query_arg( 'action', $current_url );
160 $current_url = remove_query_arg( 'group_id', $current_url );
161
162 $group_table = _groups_get_tablename( 'group' );
163
164 $output .=
165 '<div class="manage-groups">' .
166 '<div>' .
167 '<h2>' .
168 __( 'Groups', GROUPS_PLUGIN_DOMAIN ) .
169 '</h2>' .
170 '</div>';
171
172 $output .=
173 '<div class="manage">' .
174 "<a title='" . __( 'Click to add a new group', GROUPS_PLUGIN_DOMAIN ) . "' class='add' href='" . esc_url( $current_url ) . "&action=add'><img class='icon' alt='" . __( 'Add', GROUPS_PLUGIN_DOMAIN) . "' src='". GROUPS_PLUGIN_URL ."images/add.png'/><span class='label'>" . __( 'New Group', GROUPS_PLUGIN_DOMAIN) . "</span></a>" .
175 '</div>';
176
177 $row_count = isset( $_POST['row_count'] ) ? intval( $_POST['row_count'] ) : 0;
178
179 if ($row_count <= 0) {
180 $row_count = Groups_Options::get_user_option( 'groups_per_page', GROUPS_GROUPS_PER_PAGE );
181 } else {
182 Groups_Options::update_user_option('groups_per_page', $row_count );
183 }
184 $offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0;
185 if ( $offset < 0 ) {
186 $offset = 0;
187 }
188 $paged = isset( $_GET['paged'] ) ? intval( $_GET['paged'] ) : 0;
189 if ( $paged < 0 ) {
190 $paged = 0;
191 }
192
193 $orderby = isset( $_GET['orderby'] ) ? $_GET['orderby'] : null;
194 switch ( $orderby ) {
195 case 'group_id' :
196 case 'name' :
197 break;
198 default:
199 $orderby = 'name';
200 }
201
202 $order = isset( $_GET['order'] ) ? $_GET['order'] : null;
203 switch ( $order ) {
204 case 'asc' :
205 case 'ASC' :
206 $switch_order = 'DESC';
207 break;
208 case 'desc' :
209 case 'DESC' :
210 $switch_order = 'ASC';
211 break;
212 default:
213 $order = 'ASC';
214 $switch_order = 'DESC';
215 }
216
217 $filters = array();
218 $filter_params = array();
219 if ( $group_id ) {
220 $filters[] = " $group_table.group_id = %d ";
221 $filter_params[] = $group_id;
222 }
223 if ( $group_name ) {
224 $filters[] = " $group_table.name LIKE '%%%s%%' ";
225 $filter_params[] = $group_name;
226 }
227
228 if ( !empty( $filters ) ) {
229 $filters = " WHERE " . implode( " AND ", $filters );
230 } else {
231 $filters = '';
232 }
233
234 $count_query = $wpdb->prepare( "SELECT COUNT(*) FROM $group_table $filters", $filter_params );
235 $count = $wpdb->get_var( $count_query );
236 if ( $count > $row_count ) {
237 $paginate = true;
238 } else {
239 $paginate = false;
240 }
241 $pages = ceil ( $count / $row_count );
242 if ( $paged > $pages ) {
243 $paged = $pages;
244 }
245 if ( $paged != 0 ) {
246 $offset = ( $paged - 1 ) * $row_count;
247 }
248
249 $query = $wpdb->prepare(
250 "SELECT * FROM $group_table
251 $filters
252 ORDER BY $orderby $order
253 LIMIT $row_count OFFSET $offset",
254 $filter_params
255 );
256
257 $results = $wpdb->get_results( $query, OBJECT );
258
259 $column_display_names = array(
260 'group_id' => __( 'Id', GROUPS_PLUGIN_DOMAIN ),
261 'name' => __( 'Group', GROUPS_PLUGIN_DOMAIN ),
262 'description' => __( 'Description', GROUPS_PLUGIN_DOMAIN ),
263 'capabilities' => __( 'Capabilities', GROUPS_PLUGIN_DOMAIN ),
264 'edit' => __( 'Edit', GROUPS_PLUGIN_DOMAIN ),
265 'remove' => __( 'Remove', GROUPS_PLUGIN_DOMAIN )
266 );
267
268 $output .= '<div class="groups-overview">';
269
270 $output .=
271 '<div class="filters">' .
272 '<label class="description" for="setfilters">' . __( 'Filters', GROUPS_PLUGIN_DOMAIN ) . '</label>' .
273 '<form id="setfilters" action="" method="post">' .
274 '<p>' .
275 '<label class="group-id-filter" for="group_id">' . __( 'Group Id', GROUPS_PLUGIN_DOMAIN ) . '</label>' .
276 '<input class="group-id-filter" name="group_id" type="text" value="' . esc_attr( $group_id ) . '"/>' .
277 '<label class="group-name-filter" for="group_name">' . __( 'Group Name', GROUPS_PLUGIN_DOMAIN ) . '</label>' .
278 '<input class="group-name-filter" name="group_name" type="text" value="' . $group_name . '"/>' .
279 '</p>' .
280 '<p>' .
281 wp_nonce_field( 'admin', GROUPS_ADMIN_GROUPS_FILTER_NONCE, true, false ) .
282 '<input type="submit" value="' . __( 'Apply', GROUPS_PLUGIN_DOMAIN ) . '"/>' .
283 '<input type="submit" name="clear_filters" value="' . __( 'Clear', GROUPS_PLUGIN_DOMAIN ) . '"/>' .
284 '<input type="hidden" value="submitted" name="submitted"/>' .
285 '</p>' .
286 '</form>' .
287 '</div>';
288
289 $output .= '
290 <div class="page-options">
291 <form id="setrowcount" action="" method="post">
292 <div>
293 <label for="row_count">' . __('Results per page', GROUPS_PLUGIN_DOMAIN ) . '</label>' .
294 '<input name="row_count" type="text" size="2" value="' . esc_attr( $row_count ) .'" />
295 ' . wp_nonce_field( 'admin', GROUPS_ADMIN_GROUPS_NONCE_1, true, false ) . '
296 <input type="submit" value="' . __( 'Apply', GROUPS_PLUGIN_DOMAIN ) . '"/>
297 </div>
298 </form>
299 </div>
300 ';
301
302 if ( $paginate ) {
303 require_once( GROUPS_CORE_LIB . '/class-groups-pagination.php' );
304 $pagination = new Groups_Pagination( $count, null, $row_count );
305 $output .= '<form id="posts-filter" method="post" action="">';
306 $output .= '<div>';
307 $output .= wp_nonce_field( 'admin', GROUPS_ADMIN_GROUPS_NONCE_2, true, false );
308 $output .= '</div>';
309 $output .= '<div class="tablenav top">';
310 $output .= $pagination->pagination( 'top' );
311 $output .= '</div>';
312 $output .= '</form>';
313 }
314
315
316 $capability_table = _groups_get_tablename( "capability" );
317 $group_capability_table = _groups_get_tablename( "group_capability" );
318
319 // capabilities select
320 $capabilities_select = '<select name="capability_id">';
321 $capabilities = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $capability_table ORDER BY capability" ) );
322 foreach( $capabilities as $capability ) {
323 $capabilities_select .= '<option value="' . esc_attr( $capability->capability_id ) . '">' . wp_filter_nohtml_kses( $capability->capability ) . '</option>';
324 }
325 $capabilities_select .= '</select>';
326
327
328 $output .= '<form id="groups-action" method="post" action="">';
329
330 $output .= '<div class="tablenav top">';
331 $output .= '<div class="alignleft">';
332 $output .= __( "Apply capability to selected groups:", GROUPS_PLUGIN_DOMAIN );
333 $output .= $capabilities_select;
334 $output .= '<input class="button" type="submit" name="add" value="' . __( "Add", GROUPS_PLUGIN_DOMAIN ) . '"/>';
335 $output .= '<input class="button" type="submit" name="remove" value="' . __( "Remove", GROUPS_PLUGIN_DOMAIN ) . '"/>';
336 $output .= wp_nonce_field( 'admin', GROUPS_ADMIN_GROUPS_ACTION_NONCE, true, false );
337 $output .= '<input type="hidden" name="action" value="groups-action"/>';
338 $output .= '</div>'; // .alignleft
339 $output .= '</div>'; // .tablenav.top
340
341 $output .= '
342 <table id="" class="wp-list-table widefat fixed" cellspacing="0">
343 <thead>
344 <tr>
345 ';
346
347 $output .= '<th id="cb" class="manage-column column-cb check-column" scope="col"><input type="checkbox"></th>';
348
349 foreach ( $column_display_names as $key => $column_display_name ) {
350 $options = array(
351 'orderby' => $key,
352 'order' => $switch_order
353 );
354 $class = $key;
355 if ( !in_array($key, array( 'capabilities', 'edit', 'remove' ) ) ) {
356 if ( strcmp( $key, $orderby ) == 0 ) {
357 $lorder = strtolower( $order );
358 $class = "$key manage-column sorted $lorder";
359 } else {
360 $class = "$key manage-column sortable";
361 }
362 $column_display_name = '<a href="' . esc_url( add_query_arg( $options, $current_url ) ) . '"><span>' . $column_display_name . '</span><span class="sorting-indicator"></span></a>';
363 }
364 $output .= "<th scope='col' class='$class'>$column_display_name</th>";
365 }
366
367 $output .= '</tr>
368 </thead>
369 <tbody>
370 ';
371
372 if ( count( $results ) > 0 ) {
373 for ( $i = 0; $i < count( $results ); $i++ ) {
374
375 $result = $results[$i];
376
377 $output .= '<tr class="' . ( $i % 2 == 0 ? 'even' : 'odd' ) . '">';
378
379 $output .= '<th class="check-column">';
380 $output .= '<input type="checkbox" value="' . esc_attr( $result->group_id ) . '" name="group_ids[]"/>';
381 $output .= '</th>';
382
383 $output .= "<td class='group-id'>";
384 $output .= $result->group_id;
385 $output .= "</td>";
386 $output .= "<td class='group-name'>" . stripslashes( wp_filter_nohtml_kses( $result->name ) ) . "</td>";
387 $output .= "<td class='group-description'>" . stripslashes( wp_filter_nohtml_kses( $result->description ) ) . "</td>";
388
389 $output .= '<td class="capabilities">';
390 $group_capabilities = $wpdb->get_results( $wpdb->prepare(
391 "SELECT * FROM $capability_table WHERE capability_id IN ( SELECT capability_id FROM $group_capability_table WHERE group_id = %d )",
392 Groups_Utility::id( $result->group_id )
393 ) );
394 if ( count( $group_capabilities ) > 0 ) {
395 $output .= '<ul>';
396 foreach ( $group_capabilities as $group_capability ) {
397 $output .= '<li>' . wp_filter_nohtml_kses( $group_capability->capability ) . '</li>';
398 }
399 $output .= '</ul>';
400 } else {
401 $output .= __( 'This group has no capabilities.', GROUPS_PLUGIN_DOMAIN );
402 }
403 $output .= '</td>';
404
405 $output .= "<td class='edit'>";
406 $output .= "<a href='" . esc_url( add_query_arg( 'paged', $paged, $current_url ) ) . "&action=edit&group_id=" . $result->group_id . "' alt='" . __( 'Edit', GROUPS_PLUGIN_DOMAIN) . "'><img src='". GROUPS_PLUGIN_URL ."images/edit.png'/></a>";
407 $output .= "</td>";
408
409 $output .= "<td class='remove'>";
410 if ( $result->name !== Groups_Registered::REGISTERED_GROUP_NAME ) {
411 $output .= "<a href='" . esc_url( $current_url ) . "&action=remove&group_id=" . $result->group_id . "' alt='" . __( 'Remove', GROUPS_PLUGIN_DOMAIN) . "'><img src='". GROUPS_PLUGIN_URL ."images/remove.png'/></a>";
412 }
413 $output .= "</td>";
414
415 $output .= '</tr>';
416 }
417 } else {
418 $output .= '<tr><td colspan="10">' . __( 'There are no results.', GROUPS_PLUGIN_DOMAIN ) . '</td></tr>';
419 }
420
421 $output .= '</tbody>';
422 $output .= '</table>';
423
424 $output .= '</form>'; // #groups-action
425
426 if ( $paginate ) {
427 require_once( GROUPS_CORE_LIB . '/class-groups-pagination.php' );
428 $pagination = new Groups_Pagination($count, null, $row_count);
429 $output .= '<div class="tablenav bottom">';
430 $output .= $pagination->pagination( 'bottom' );
431 $output .= '</div>';
432 }
433
434 $output .= '</div>'; // .groups-overview
435 $output .= '</div>'; // .manage-groups
436
437 echo $output;
438 Groups_Help::footer();
439 } // function groups_admin_groups()
440 ?>