PluginProbe
Groups – Memberships and Access Control / 1.10.0
Groups – Memberships and Access Control v1.10.0
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-edit.php

groups-admin-groups-edit.php in Groups – Memberships and Access Control 1.10.0, at lib/admin/groups-admin-groups-edit.php

247 lines 9.3 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-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 &hellip;', 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 .= '<div class="field">';
155 $output .= wp_nonce_field( 'groups-edit', GROUPS_ADMIN_GROUPS_NONCE, true, false );
156 $output .= '<input class="button button-primary" type="submit" value="' . __( 'Save', GROUPS_PLUGIN_DOMAIN ) . '"/>';
157 $output .= '<input type="hidden" value="edit" name="action"/>';
158 $output .= '<a class="cancel button" href="' . esc_url( $current_url ) . '">' . __( 'Cancel', GROUPS_PLUGIN_DOMAIN ) . '</a>';
159 $output .= '</div>';
160 $output .= '</div>'; // .group.edit
161 $output .= '</form>';
162 $output .= '</div>'; // .manage-groups
163
164 echo $output;
165 } // function groups_admin_groups_edit
166
167 /**
168 * Handle edit form submission.
169 */
170 function groups_admin_groups_edit_submit() {
171 global $wpdb;
172
173 if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
174 wp_die( __( 'Access denied.', GROUPS_PLUGIN_DOMAIN ) );
175 }
176
177 if ( !wp_verify_nonce( $_POST[GROUPS_ADMIN_GROUPS_NONCE], 'groups-edit' ) ) {
178 wp_die( __( 'Access denied.', GROUPS_PLUGIN_DOMAIN ) );
179 }
180
181 $group_id = isset( $_POST['group-id-field'] ) ? $_POST['group-id-field'] : null;
182 $group = Groups_Group::read( $group_id );
183 if ( $group ) {
184 $group_id = $group->group_id;
185 if ( $group->name !== Groups_Registered::REGISTERED_GROUP_NAME ) {
186 $name = isset( $_POST['name-field'] ) ? $_POST['name-field'] : null;
187 } else {
188 $name = Groups_Registered::REGISTERED_GROUP_NAME;
189 }
190 $parent_id = isset( $_POST['parent-id-field'] ) ? $_POST['parent-id-field'] : null;
191 $description = isset( $_POST['description-field'] ) ? $_POST['description-field'] : '';
192
193 if ( empty( $name ) ) {
194 Groups_Admin::add_message( __( 'The <em>Name</em> must not be empty.', GROUPS_PLUGIN_DOMAIN ), 'error' );
195 return false;
196 }
197
198 if ( $other_group = Groups_Group::read_by_name( $name ) ) {
199 if ( $other_group->group_id != $group_id ) {
200 Groups_Admin::add_message(
201 sprintf(
202 __( '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 ) )
203 ),
204 'error'
205 );
206 return false;
207 }
208 }
209
210 $group_id = Groups_Group::update( compact( "group_id", "name", "parent_id", "description" ) );
211
212 if ( $group_id ) {
213 $capability_table = _groups_get_tablename( "capability" );
214 $group_capability_table = _groups_get_tablename( "group_capability" );
215 $group_capabilities = $wpdb->get_results( $wpdb->prepare(
216 "SELECT * FROM $capability_table WHERE capability_id IN ( SELECT capability_id FROM $group_capability_table WHERE group_id = %d )",
217 Groups_Utility::id( $group_id )
218 ) );
219 $group_capabilities_array = array();
220 foreach ( $group_capabilities as $group_capability ) {
221 $group_capabilities_array[] = $group_capability->capability_id;
222 }
223
224 $caps = array();
225 if ( isset( $_POST['capability_ids'] ) ) {
226 $caps = $_POST['capability_ids'];
227 }
228 // delete
229 foreach( $group_capabilities_array as $group_cap ) {
230 if ( !in_array( $group_cap, $caps ) ) {
231 Groups_Group_Capability::delete( $group_id, $group_cap );
232 }
233 }
234 // add
235 foreach( $caps as $cap ) {
236 if ( !in_array( $cap, $group_capabilities_array ) ) {
237 Groups_Group_Capability::create( array( 'group_id' => $group_id, 'capability_id' => $cap ) );
238 }
239 }
240 }
241 return $group_id;
242 } else {
243 return false;
244 }
245
246 } // function groups_admin_groups_edit_submit
247