| 1 |
<?php |
| 2 |
/** |
| 3 |
* groups-admin-capabilities.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_CAPABILITIES_PER_PAGE', 10 ); |
| 22 |
define( 'GROUPS_ADMIN_CAPABILITIES_NONCE_1', 'groups-cap-nonce-1'); |
| 23 |
define( 'GROUPS_ADMIN_CAPABILITIES_NONCE_2', 'groups-cap-nonce-2'); |
| 24 |
define( 'GROUPS_ADMIN_CAPABILITIES_ACTION_NONCE', 'groups-cap-action-nonce'); |
| 25 |
define( 'GROUPS_ADMIN_CAPABILITIES_FILTER_NONCE', 'groups-cap-filter-nonce' ); |
| 26 |
|
| 27 |
require_once( GROUPS_CORE_LIB . '/class-groups-pagination.php' ); |
| 28 |
require_once( GROUPS_ADMIN_LIB . '/groups-admin-capabilities-add.php'); |
| 29 |
require_once( GROUPS_ADMIN_LIB . '/groups-admin-capabilities-edit.php'); |
| 30 |
require_once( GROUPS_ADMIN_LIB . '/groups-admin-capabilities-remove.php'); |
| 31 |
|
| 32 |
/** |
| 33 |
* Manage capabilities: table of capabilities and add, edit, remove actions. |
| 34 |
*/ |
| 35 |
function groups_admin_capabilities() { |
| 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_capabilities_add_submit() ) { |
| 54 |
return groups_admin_capabilities_add(); |
| 55 |
} |
| 56 |
break; |
| 57 |
case 'edit' : |
| 58 |
if ( !groups_admin_capabilities_edit_submit() ) { |
| 59 |
return groups_admin_capabilities_edit( $_POST['capability-id-field'] ); |
| 60 |
} |
| 61 |
break; |
| 62 |
case 'remove' : |
| 63 |
groups_admin_capabilities_remove_submit(); |
| 64 |
break; |
| 65 |
// bulk actions on groups: capabilities |
| 66 |
case 'groups-action' : |
| 67 |
// if ( wp_verify_nonce( $_POST[GROUPS_ADMIN_CAPABILITIES_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_capabilities_add(); |
| 91 |
break; |
| 92 |
case 'edit' : |
| 93 |
if ( isset( $_GET['capability_id'] ) ) { |
| 94 |
return groups_admin_capabilities_edit( $_GET['capability_id'] ); |
| 95 |
} |
| 96 |
break; |
| 97 |
case 'remove' : |
| 98 |
if ( isset( $_GET['capability_id'] ) ) { |
| 99 |
return groups_admin_capabilities_remove( $_GET['capability_id'] ); |
| 100 |
} |
| 101 |
break; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
// |
| 106 |
// capabilities table |
| 107 |
// |
| 108 |
if ( |
| 109 |
isset( $_POST['clear_filters'] ) || |
| 110 |
isset( $_POST['capability_id'] ) || |
| 111 |
isset( $_POST['capability'] ) |
| 112 |
) { |
| 113 |
if ( !wp_verify_nonce( $_POST[GROUPS_ADMIN_CAPABILITIES_FILTER_NONCE], 'admin' ) ) { |
| 114 |
wp_die( __( 'Access denied.', GROUPS_PLUGIN_DOMAIN ) ); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
// filters |
| 119 |
$capability_id = Groups_Options::get_user_option( 'capabilities_capability_id', null ); |
| 120 |
$capability = Groups_Options::get_user_option( 'capabilities_capability', null ); |
| 121 |
|
| 122 |
if ( isset( $_POST['clear_filters'] ) ) { |
| 123 |
Groups_Options::delete_user_option( 'capabilities_capability_id' ); |
| 124 |
Groups_Options::delete_user_option( 'capabilities_capability' ); |
| 125 |
$capability_id = null; |
| 126 |
$capability = null; |
| 127 |
} else if ( isset( $_POST['submitted'] ) ) { |
| 128 |
// filter by name |
| 129 |
if ( !empty( $_POST['capability'] ) ) { |
| 130 |
$capability = $_POST['capability']; |
| 131 |
Groups_Options::update_user_option( 'capabilities_capability', $capability ); |
| 132 |
} |
| 133 |
// filter by capability id |
| 134 |
if ( !empty( $_POST['capability_id'] ) ) { |
| 135 |
$capability_id = intval( $_POST['capability_id'] ); |
| 136 |
Groups_Options::update_user_option( 'capabilities_capability_id', $capability_id ); |
| 137 |
} else if ( isset( $_POST['capability_id'] ) ) { // empty && isset => '' => all |
| 138 |
$capability_id = null; |
| 139 |
Groups_Options::delete_user_option( 'capabilities_capability_id' ); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
if ( isset( $_POST['row_count'] ) ) { |
| 144 |
if ( !wp_verify_nonce( $_POST[GROUPS_ADMIN_CAPABILITIES_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_CAPABILITIES_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( 'capability_id', $current_url ); |
| 159 |
|
| 160 |
$capability_table = _groups_get_tablename( 'capability' ); |
| 161 |
|
| 162 |
$output .= |
| 163 |
'<div class="manage-capabilities">' . |
| 164 |
'<div>' . |
| 165 |
'<h2>' . |
| 166 |
__( 'Capabilities', GROUPS_PLUGIN_DOMAIN ) . |
| 167 |
'</h2>' . |
| 168 |
'</div>'; |
| 169 |
|
| 170 |
$output .= |
| 171 |
'<div class="manage">' . |
| 172 |
"<a title='" . __( 'Click to add a new capability', 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 Capability', 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( 'capabilities_per_page', GROUPS_CAPABILITIES_PER_PAGE ); |
| 179 |
} else { |
| 180 |
Groups_Options::update_user_option('capabilities_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 'capability_id' : |
| 194 |
case 'capability' : |
| 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 ( $capability_id ) { |
| 218 |
$filters[] = " $capability_table.capability_id = %d "; |
| 219 |
$filter_params[] = $capability_id; |
| 220 |
} |
| 221 |
if ( $capability ) { |
| 222 |
$filters[] = " $capability_table.capability LIKE '%%%s%%' "; |
| 223 |
$filter_params[] = $capability; |
| 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 $capability_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 $capability_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 |
'capability_id' => __( 'Id', GROUPS_PLUGIN_DOMAIN ), |
| 259 |
'capability' => __( 'Capability', GROUPS_PLUGIN_DOMAIN ), |
| 260 |
'description' => __( 'Description', GROUPS_PLUGIN_DOMAIN ), |
| 261 |
'edit' => __( 'Edit', GROUPS_PLUGIN_DOMAIN ), |
| 262 |
'remove' => __( 'Remove', GROUPS_PLUGIN_DOMAIN ) |
| 263 |
); |
| 264 |
|
| 265 |
$output .= '<div class="capabilities-overview">'; |
| 266 |
|
| 267 |
$output .= |
| 268 |
'<div class="filters">' . |
| 269 |
'<label class="description" for="setfilters">' . __( 'Filters', GROUPS_PLUGIN_DOMAIN ) . '</label>' . |
| 270 |
'<form id="setfilters" action="" method="post">' . |
| 271 |
'<p>' . |
| 272 |
'<label class="capability-id-filter" for="capability_id">' . __( 'Capability Id', GROUPS_PLUGIN_DOMAIN ) . '</label>' . |
| 273 |
'<input class="capability-id-filter" name="capability_id" type="text" value="' . esc_attr( $capability_id ) . '"/>' . |
| 274 |
'<label class="capability-filter" for="capability">' . __( 'Capability', GROUPS_PLUGIN_DOMAIN ) . '</label>' . |
| 275 |
'<input class="capability-filter" name="capability" type="text" value="' . $capability . '"/>' . |
| 276 |
'</p>' . |
| 277 |
'<p>' . |
| 278 |
wp_nonce_field( 'admin', GROUPS_ADMIN_CAPABILITIES_FILTER_NONCE, true, false ) . |
| 279 |
'<input type="submit" value="' . __( 'Apply', GROUPS_PLUGIN_DOMAIN ) . '"/>' . |
| 280 |
'<input type="submit" name="clear_filters" value="' . __( 'Clear', GROUPS_PLUGIN_DOMAIN ) . '"/>' . |
| 281 |
'<input type="hidden" value="submitted" name="submitted"/>' . |
| 282 |
'</p>' . |
| 283 |
'</form>' . |
| 284 |
'</div>'; |
| 285 |
|
| 286 |
$output .= ' |
| 287 |
<div class="page-options"> |
| 288 |
<form id="setrowcount" action="" method="post"> |
| 289 |
<div> |
| 290 |
<label for="row_count">' . __( 'Results per page', GROUPS_PLUGIN_DOMAIN ) . '</label>' . |
| 291 |
'<input name="row_count" type="text" size="2" value="' . esc_attr( $row_count ) .'" /> |
| 292 |
' . wp_nonce_field( 'admin', GROUPS_ADMIN_CAPABILITIES_NONCE_1, true, false ) . ' |
| 293 |
<input type="submit" value="' . __( 'Apply', GROUPS_PLUGIN_DOMAIN ) . '"/> |
| 294 |
</div> |
| 295 |
</form> |
| 296 |
</div> |
| 297 |
'; |
| 298 |
|
| 299 |
if ( $paginate ) { |
| 300 |
require_once( GROUPS_CORE_LIB . '/class-groups-pagination.php' ); |
| 301 |
$pagination = new Groups_Pagination( $count, null, $row_count ); |
| 302 |
$output .= '<form id="posts-filter" method="post" action="">'; |
| 303 |
$output .= '<div>'; |
| 304 |
$output .= wp_nonce_field( 'admin', GROUPS_ADMIN_CAPABILITIES_NONCE_2, true, false ); |
| 305 |
$output .= '</div>'; |
| 306 |
$output .= '<div class="tablenav top">'; |
| 307 |
$output .= $pagination->pagination( 'top' ); |
| 308 |
$output .= '</div>'; |
| 309 |
$output .= '</form>'; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
// $capability_table = _groups_get_tablename( "capability" ); |
| 314 |
// $group_capability_table = _groups_get_tablename( "group_capability" ); |
| 315 |
|
| 316 |
// // capabilities select |
| 317 |
// $capabilities_select = '<select name="capability_id">'; |
| 318 |
// $capabilities = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $capability_table ORDER BY capability" ) ); |
| 319 |
// foreach( $capabilities as $capability ) { |
| 320 |
// $capabilities_select .= '<option value="' . esc_attr( $capability->capability_id ) . '">' . wp_filter_nohtml_kses( $capability->capability ) . '</option>'; |
| 321 |
// } |
| 322 |
// $capabilities_select .= '</select>'; |
| 323 |
|
| 324 |
|
| 325 |
// $output .= '<form id="groups-action" method="post" action="">'; |
| 326 |
|
| 327 |
// $output .= '<div class="tablenav top">'; |
| 328 |
// $output .= '<div class="alignleft">'; |
| 329 |
// $output .= __( "Apply capability to selected groups:", GROUPS_PLUGIN_DOMAIN ); |
| 330 |
// $output .= $capabilities_select; |
| 331 |
// $output .= '<input class="button" type="submit" name="add" value="' . __( "Add", GROUPS_PLUGIN_DOMAIN ) . '"/>'; |
| 332 |
// $output .= '<input class="button" type="submit" name="remove" value="' . __( "Remove", GROUPS_PLUGIN_DOMAIN ) . '"/>'; |
| 333 |
// $output .= wp_nonce_field( 'admin', GROUPS_ADMIN_CAPABILITIES_ACTION_NONCE, true, false ); |
| 334 |
// $output .= '<input type="hidden" name="action" value="groups-action"/>'; |
| 335 |
// $output .= '</div>'; // .alignleft |
| 336 |
// $output .= '</div>'; // .tablenav.top |
| 337 |
|
| 338 |
$output .= ' |
| 339 |
<table id="" class="wp-list-table widefat fixed" cellspacing="0"> |
| 340 |
<thead> |
| 341 |
<tr> |
| 342 |
'; |
| 343 |
|
| 344 |
$output .= '<th id="cb" class="manage-column column-cb check-column" scope="col"><input type="checkbox"></th>'; |
| 345 |
|
| 346 |
foreach ( $column_display_names as $key => $column_display_name ) { |
| 347 |
$options = array( |
| 348 |
'orderby' => $key, |
| 349 |
'order' => $switch_order |
| 350 |
); |
| 351 |
$class = $key; |
| 352 |
if ( !in_array($key, array( 'capabilities', 'edit', 'remove' ) ) ) { |
| 353 |
if ( strcmp( $key, $orderby ) == 0 ) { |
| 354 |
$lorder = strtolower( $order ); |
| 355 |
$class = "$key manage-column sorted $lorder"; |
| 356 |
} else { |
| 357 |
$class = "$key manage-column sortable"; |
| 358 |
} |
| 359 |
$column_display_name = '<a href="' . esc_url( add_query_arg( $options, $current_url ) ) . '"><span>' . $column_display_name . '</span><span class="sorting-indicator"></span></a>'; |
| 360 |
} |
| 361 |
$output .= "<th scope='col' class='$class'>$column_display_name</th>"; |
| 362 |
} |
| 363 |
|
| 364 |
$output .= '</tr> |
| 365 |
</thead> |
| 366 |
<tbody> |
| 367 |
'; |
| 368 |
|
| 369 |
if ( count( $results ) > 0 ) { |
| 370 |
for ( $i = 0; $i < count( $results ); $i++ ) { |
| 371 |
|
| 372 |
$result = $results[$i]; |
| 373 |
|
| 374 |
$output .= '<tr class="' . ( $i % 2 == 0 ? 'even' : 'odd' ) . '">'; |
| 375 |
|
| 376 |
$output .= '<th class="check-column">'; |
| 377 |
$output .= '<input type="checkbox" value="' . esc_attr( $result->capability_id ) . '" name="capability_ids[]"/>'; |
| 378 |
$output .= '</th>'; |
| 379 |
|
| 380 |
$output .= "<td class='capability-id'>"; |
| 381 |
$output .= $result->capability_id; |
| 382 |
$output .= "</td>"; |
| 383 |
$output .= "<td class='capability'>" . stripslashes( wp_filter_nohtml_kses( $result->capability ) ) . "</td>"; |
| 384 |
$output .= "<td class='description'>" . stripslashes( wp_filter_nohtml_kses( $result->description ) ) . "</td>"; |
| 385 |
|
| 386 |
$output .= "<td class='edit'>"; |
| 387 |
$output .= "<a href='" . esc_url( add_query_arg( 'paged', $paged, $current_url ) ) . "&action=edit&capability_id=" . $result->capability_id . "' alt='" . __( 'Edit', GROUPS_PLUGIN_DOMAIN) . "'><img src='". GROUPS_PLUGIN_URL ."images/edit.png'/></a>"; |
| 388 |
$output .= "</td>"; |
| 389 |
|
| 390 |
$output .= "<td class='remove'>"; |
| 391 |
if ( $result->capability !== Groups_Post_Access::READ_POST_CAPABILITY ) { |
| 392 |
$output .= "<a href='" . esc_url( $current_url ) . "&action=remove&capability_id=" . $result->capability_id . "' alt='" . __( 'Remove', GROUPS_PLUGIN_DOMAIN) . "'><img src='". GROUPS_PLUGIN_URL ."images/remove.png'/></a>"; |
| 393 |
} |
| 394 |
$output .= "</td>"; |
| 395 |
|
| 396 |
$output .= '</tr>'; |
| 397 |
} |
| 398 |
} else { |
| 399 |
$output .= '<tr><td colspan="10">' . __( 'There are no results.', GROUPS_PLUGIN_DOMAIN ) . '</td></tr>'; |
| 400 |
} |
| 401 |
|
| 402 |
$output .= '</tbody>'; |
| 403 |
$output .= '</table>'; |
| 404 |
|
| 405 |
// $output .= '</form>'; // #groups-action |
| 406 |
|
| 407 |
if ( $paginate ) { |
| 408 |
require_once( GROUPS_CORE_LIB . '/class-groups-pagination.php' ); |
| 409 |
$pagination = new Groups_Pagination($count, null, $row_count); |
| 410 |
$output .= '<div class="tablenav bottom">'; |
| 411 |
$output .= $pagination->pagination( 'bottom' ); |
| 412 |
$output .= '</div>'; |
| 413 |
} |
| 414 |
|
| 415 |
$output .= '</div>'; // .capabilities-overview |
| 416 |
$output .= '</div>'; // .manage-capabilities |
| 417 |
|
| 418 |
echo $output; |
| 419 |
Groups_Help::footer(); |
| 420 |
} // function groups_admin_capabilities() |
| 421 |
|