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

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