| 1 |
<?php |
| 2 |
/** |
| 3 |
* groups-admin-groups-edit.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.1.0 |
| 20 |
*/ |
| 21 |
|
| 22 |
if ( !defined( 'ABSPATH' ) ) { |
| 23 |
exit; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Show edit group form. |
| 28 |
* @param int $group_id group id |
| 29 |
*/ |
| 30 |
function groups_admin_groups_edit( $group_id ) { |
| 31 |
|
| 32 |
global $wpdb; |
| 33 |
|
| 34 |
$output = ''; |
| 35 |
|
| 36 |
if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) { |
| 37 |
wp_die( __( 'Access denied.', GROUPS_PLUGIN_DOMAIN ) ); |
| 38 |
} |
| 39 |
|
| 40 |
$group = Groups_Group::read( intval( $group_id ) ); |
| 41 |
|
| 42 |
if ( empty( $group ) ) { |
| 43 |
wp_die( __( 'No such group.', GROUPS_PLUGIN_DOMAIN ) ); |
| 44 |
} |
| 45 |
|
| 46 |
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
| 47 |
$current_url = remove_query_arg( 'action', $current_url ); |
| 48 |
$current_url = remove_query_arg( 'group_id', $current_url ); |
| 49 |
|
| 50 |
$name = isset( $_POST['name-field'] ) ? $_POST['name-field'] : $group->name; |
| 51 |
$description = isset( $_POST['description-field'] ) ? $_POST['description-field'] : $group->description; |
| 52 |
$parent_id = isset( $_POST['parent-id-field'] ) ? $_POST['parent-id-field'] : $group->parent_id; |
| 53 |
|
| 54 |
$group_table = _groups_get_tablename( 'group' ); |
| 55 |
$parent_select = '<select name="parent-id-field">'; |
| 56 |
$parent_select .= '<option value="">--</option>'; |
| 57 |
$groups = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $group_table WHERE group_id != %d", $group->group_id ) ); |
| 58 |
foreach ( $groups as $g ) { |
| 59 |
$selected = ( $g->group_id == $group->parent_id ? ' selected="selected" ' : '' ); |
| 60 |
$parent_select .= '<option ' . $selected . 'value="' . esc_attr( $g->group_id ) . '">' . wp_filter_nohtml_kses( $g->name ) . '</option>'; |
| 61 |
} |
| 62 |
$parent_select .= '</select>'; |
| 63 |
|
| 64 |
$name_readonly = ( $name !== Groups_Registered::REGISTERED_GROUP_NAME ) ? "" : ' readonly="readonly" '; |
| 65 |
|
| 66 |
$output .= '<div class="manage-groups wrap">'; |
| 67 |
$output .= '<h1>'; |
| 68 |
$output .= __( 'Edit a group', GROUPS_PLUGIN_DOMAIN ); |
| 69 |
$output .= '</h1>'; |
| 70 |
|
| 71 |
$output .= Groups_Admin::render_messages(); |
| 72 |
|
| 73 |
$output .= '<form id="edit-group" action="' . esc_url( $current_url ) . '" method="post">'; |
| 74 |
$output .= '<div class="group edit">'; |
| 75 |
$output .= '<input id="group-id-field" name="group-id-field" type="hidden" value="' . esc_attr( intval( $group_id ) ) . '"/>'; |
| 76 |
|
| 77 |
$output .= '<div class="field">'; |
| 78 |
$output .= '<label for="name-field" class="field-label first required">'; |
| 79 |
$output .= __( 'Name', GROUPS_PLUGIN_DOMAIN ); |
| 80 |
$output .= '</label>'; |
| 81 |
$output .= '<input ' . $name_readonly . ' id="name-field" name="name-field" class="namefield" type="text" value="' . esc_attr( stripslashes( $name ) ) . '"/>'; |
| 82 |
$output .= '</div>'; |
| 83 |
|
| 84 |
$output .= '<div class="field">'; |
| 85 |
$output .= '<label for="parent-id-field" class="field-label">'; |
| 86 |
$output .= __( 'Parent', GROUPS_PLUGIN_DOMAIN ); |
| 87 |
$output .= '</label>'; |
| 88 |
$output .= $parent_select; |
| 89 |
$output .= '</div>'; |
| 90 |
|
| 91 |
$output .= '<div class="field">'; |
| 92 |
$output .= '<label for="description-field" class="field-label description-field">'; |
| 93 |
$output .= __( 'Description', GROUPS_PLUGIN_DOMAIN ); |
| 94 |
$output .= '</label>'; |
| 95 |
$output .= '<textarea id="description-field" name="description-field" rows="5" cols="45">'; |
| 96 |
$output .= stripslashes( wp_filter_nohtml_kses( $description ) ); |
| 97 |
$output .= '</textarea>'; |
| 98 |
$output .= '</div>'; |
| 99 |
|
| 100 |
$capability_table = _groups_get_tablename( 'capability' ); |
| 101 |
$group_capability_table = _groups_get_tablename( 'group_capability' ); |
| 102 |
$group_capabilities = $wpdb->get_results( $wpdb->prepare( |
| 103 |
"SELECT * FROM $capability_table WHERE capability_id IN ( SELECT capability_id FROM $group_capability_table WHERE group_id = %d )", |
| 104 |
Groups_Utility::id( $group_id ) |
| 105 |
) ); |
| 106 |
$group_capabilities_array = array(); |
| 107 |
if ( count( $group_capabilities ) > 0 ) { |
| 108 |
foreach ( $group_capabilities as $group_capability ) { |
| 109 |
$group_capabilities_array[] = $group_capability->capability_id; |
| 110 |
} |
| 111 |
} |
| 112 |
$capabilities = $wpdb->get_results( "SELECT * FROM $capability_table ORDER BY capability" ); |
| 113 |
|
| 114 |
$output .= '<div class="field">'; |
| 115 |
$output .= '<div class="select-capability-container" style="width:62%;">'; |
| 116 |
$output .= '<label>'; |
| 117 |
$output .= __( 'Capabilities', GROUPS_PLUGIN_DOMAIN ); |
| 118 |
$output .= sprintf( |
| 119 |
'<select class="select capability" name="capability_ids[]" multiple="multiple" placeholder="%s">', |
| 120 |
__( 'Choose capabilities …', GROUPS_PLUGIN_DOMAIN ) |
| 121 |
); |
| 122 |
foreach( $capabilities as $capability ) { |
| 123 |
$selected = in_array( $capability->capability_id, $group_capabilities_array ) ? ' selected="selected" ' : ''; |
| 124 |
$output .= sprintf( '<option value="%s" %s>%s</option>', esc_attr( $capability->capability_id ), $selected, wp_filter_nohtml_kses( $capability->capability ) ); |
| 125 |
} |
| 126 |
$output .= '</select>'; |
| 127 |
$output .= '</label>'; |
| 128 |
$output .= '</div>'; // .select-capability-container |
| 129 |
$output .= '<p class="description">'; |
| 130 |
$output .= __( 'The chosen capabilities are assigned to the group.', GROUPS_PLUGIN_DOMAIN ); |
| 131 |
$output .= '</p>'; |
| 132 |
$output .= '</div>'; // .field |
| 133 |
$output .= Groups_UIE::render_select( '.select.capability' ); |
| 134 |
|
| 135 |
$group_object = new Groups_Group( $group_id ); |
| 136 |
$group_capabilities = $group_object->capabilities; |
| 137 |
$group_capabilities_deep = $group_object->capabilities_deep; |
| 138 |
if ( ( count( $group_capabilities_deep ) - count( $group_capabilities ) ) > 0 ) { |
| 139 |
usort( $group_capabilities_deep, array( 'Groups_Utility', 'cmp' ) ); |
| 140 |
$output .= '<div class="field">'; |
| 141 |
$output .= __( 'Inherited capabilities:', GROUPS_PLUGIN_DOMAIN ); |
| 142 |
$output .= ' '; |
| 143 |
$inherited_caps = array(); |
| 144 |
foreach ( $group_capabilities_deep as $group_capability ) { |
| 145 |
$class = ''; |
| 146 |
if ( empty( $group_capabilities ) || !in_array( $group_capability, $group_capabilities ) ) { |
| 147 |
$inherited_caps[] = wp_filter_nohtml_kses( $group_capability->capability->capability ); |
| 148 |
} |
| 149 |
} |
| 150 |
$output .= implode( ' ', $inherited_caps ); |
| 151 |
$output .= '</div>'; |
| 152 |
} |
| 153 |
|
| 154 |
$output .= apply_filters( 'groups_admin_groups_edit_form_after_fields', '', $group_id ); |
| 155 |
|
| 156 |
$output .= '<div class="field">'; |
| 157 |
$output .= wp_nonce_field( 'groups-edit', GROUPS_ADMIN_GROUPS_NONCE, true, false ); |
| 158 |
$output .= '<input class="button button-primary" type="submit" value="' . __( 'Save', GROUPS_PLUGIN_DOMAIN ) . '"/>'; |
| 159 |
$output .= '<input type="hidden" value="edit" name="action"/>'; |
| 160 |
$output .= '<a class="cancel button" href="' . esc_url( $current_url ) . '">' . __( 'Cancel', GROUPS_PLUGIN_DOMAIN ) . '</a>'; |
| 161 |
$output .= '</div>'; |
| 162 |
$output .= '</div>'; // .group.edit |
| 163 |
$output .= '</form>'; |
| 164 |
$output .= '</div>'; // .manage-groups |
| 165 |
|
| 166 |
echo $output; |
| 167 |
} // function groups_admin_groups_edit |
| 168 |
|
| 169 |
/** |
| 170 |
* Handle edit form submission. |
| 171 |
*/ |
| 172 |
function groups_admin_groups_edit_submit() { |
| 173 |
global $wpdb; |
| 174 |
|
| 175 |
if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) { |
| 176 |
wp_die( __( 'Access denied.', GROUPS_PLUGIN_DOMAIN ) ); |
| 177 |
} |
| 178 |
|
| 179 |
if ( !wp_verify_nonce( $_POST[GROUPS_ADMIN_GROUPS_NONCE], 'groups-edit' ) ) { |
| 180 |
wp_die( __( 'Access denied.', GROUPS_PLUGIN_DOMAIN ) ); |
| 181 |
} |
| 182 |
|
| 183 |
$group_id = isset( $_POST['group-id-field'] ) ? $_POST['group-id-field'] : null; |
| 184 |
$group = Groups_Group::read( $group_id ); |
| 185 |
if ( $group ) { |
| 186 |
$group_id = $group->group_id; |
| 187 |
if ( $group->name !== Groups_Registered::REGISTERED_GROUP_NAME ) { |
| 188 |
$name = isset( $_POST['name-field'] ) ? $_POST['name-field'] : null; |
| 189 |
} else { |
| 190 |
$name = Groups_Registered::REGISTERED_GROUP_NAME; |
| 191 |
} |
| 192 |
$parent_id = isset( $_POST['parent-id-field'] ) ? $_POST['parent-id-field'] : null; |
| 193 |
$description = isset( $_POST['description-field'] ) ? $_POST['description-field'] : ''; |
| 194 |
|
| 195 |
if ( empty( $name ) ) { |
| 196 |
Groups_Admin::add_message( __( 'The <em>Name</em> must not be empty.', GROUPS_PLUGIN_DOMAIN ), 'error' ); |
| 197 |
return false; |
| 198 |
} |
| 199 |
|
| 200 |
if ( $other_group = Groups_Group::read_by_name( $name ) ) { |
| 201 |
if ( $other_group->group_id != $group_id ) { |
| 202 |
Groups_Admin::add_message( |
| 203 |
sprintf( |
| 204 |
__( 'The <em>%s</em> group already exists and cannot be used to name this one.', GROUPS_PLUGIN_DOMAIN ), stripslashes( wp_filter_nohtml_kses( $other_group->name ) ) |
| 205 |
), |
| 206 |
'error' |
| 207 |
); |
| 208 |
return false; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
$group_id = Groups_Group::update( compact( "group_id", "name", "parent_id", "description" ) ); |
| 213 |
if ( $group_id ) { |
| 214 |
$capability_table = _groups_get_tablename( "capability" ); |
| 215 |
$group_capability_table = _groups_get_tablename( "group_capability" ); |
| 216 |
$group_capabilities = $wpdb->get_results( $wpdb->prepare( |
| 217 |
"SELECT * FROM $capability_table WHERE capability_id IN ( SELECT capability_id FROM $group_capability_table WHERE group_id = %d )", |
| 218 |
Groups_Utility::id( $group_id ) |
| 219 |
) ); |
| 220 |
$group_capabilities_array = array(); |
| 221 |
foreach ( $group_capabilities as $group_capability ) { |
| 222 |
$group_capabilities_array[] = $group_capability->capability_id; |
| 223 |
} |
| 224 |
|
| 225 |
$caps = array(); |
| 226 |
if ( isset( $_POST['capability_ids'] ) ) { |
| 227 |
$caps = $_POST['capability_ids']; |
| 228 |
} |
| 229 |
// delete |
| 230 |
foreach( $group_capabilities_array as $group_cap ) { |
| 231 |
if ( !in_array( $group_cap, $caps ) ) { |
| 232 |
Groups_Group_Capability::delete( $group_id, $group_cap ); |
| 233 |
} |
| 234 |
} |
| 235 |
// add |
| 236 |
foreach( $caps as $cap ) { |
| 237 |
if ( !in_array( $cap, $group_capabilities_array ) ) { |
| 238 |
Groups_Group_Capability::create( array( 'group_id' => $group_id, 'capability_id' => $cap ) ); |
| 239 |
} |
| 240 |
} |
| 241 |
do_action( 'groups_admin_groups_edit_submit_success', $group_id ); |
| 242 |
} |
| 243 |
return $group_id; |
| 244 |
} else { |
| 245 |
return false; |
| 246 |
} |
| 247 |
|
| 248 |
} // function groups_admin_groups_edit_submit |
| 249 |
|