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