| 1 |
<?php |
| 2 |
/** |
| 3 |
* groups-admin-groups-add.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 add group form. |
| 28 |
*/ |
| 29 |
function groups_admin_groups_add() { |
| 30 |
|
| 31 |
global $wpdb; |
| 32 |
|
| 33 |
$output = ''; |
| 34 |
|
| 35 |
if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) { |
| 36 |
wp_die( __( 'Access denied.', GROUPS_PLUGIN_DOMAIN ) ); |
| 37 |
} |
| 38 |
|
| 39 |
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
| 40 |
$current_url = remove_query_arg( 'paged', $current_url ); |
| 41 |
$current_url = remove_query_arg( 'action', $current_url ); |
| 42 |
$current_url = remove_query_arg( 'group_id', $current_url ); |
| 43 |
|
| 44 |
$parent_id = isset( $_POST['parent-id-field'] ) ? $_POST['parent-id-field'] : ''; |
| 45 |
$name = isset( $_POST['name-field'] ) ? $_POST['name-field'] : ''; |
| 46 |
$description = isset( $_POST['description-field'] ) ? $_POST['description-field'] : ''; |
| 47 |
|
| 48 |
$group_table = _groups_get_tablename( 'group' ); |
| 49 |
$parent_select = '<select name="parent-id-field">'; |
| 50 |
$parent_select .= '<option value="">--</option>'; |
| 51 |
$groups = $wpdb->get_results( "SELECT * FROM $group_table" ); |
| 52 |
foreach ( $groups as $group ) { |
| 53 |
$parent_select .= '<option value="' . esc_attr( $group->group_id ) . '">' . wp_filter_nohtml_kses( $group->name ) . '</option>'; |
| 54 |
} |
| 55 |
$parent_select .= '</select>'; |
| 56 |
|
| 57 |
$output .= '<div class="manage-groups wrap">'; |
| 58 |
$output .= '<h1>'; |
| 59 |
$output .= __( 'Add a new group', GROUPS_PLUGIN_DOMAIN ); |
| 60 |
$output .= '</h1>'; |
| 61 |
|
| 62 |
$output .= Groups_Admin::render_messages(); |
| 63 |
|
| 64 |
$output .= '<form id="add-group" action="' . esc_url( $current_url ) . '" method="post">'; |
| 65 |
$output .= '<div class="group new">'; |
| 66 |
|
| 67 |
$output .= '<div class="field">'; |
| 68 |
$output .= '<label for="name-field" class="field-label first required">'; |
| 69 |
$output .= __( 'Name', GROUPS_PLUGIN_DOMAIN ); |
| 70 |
$output .= '</label>'; |
| 71 |
$output .= '<input id="name-field" name="name-field" class="namefield" type="text" value="' . esc_attr( stripslashes( $name ) ) . '"/>'; |
| 72 |
$output .= '</div>'; |
| 73 |
|
| 74 |
$output .= '<div class="field">'; |
| 75 |
$output .= '<label for="parent-id-field" class="field-label">'; |
| 76 |
$output .= __( 'Parent', GROUPS_PLUGIN_DOMAIN ); |
| 77 |
$output .= '</label>'; |
| 78 |
$output .= $parent_select; |
| 79 |
$output .= '</div>'; |
| 80 |
|
| 81 |
$output .= '<div class="field">'; |
| 82 |
$output .= '<label for="description-field" class="field-label description-field">'; |
| 83 |
$output .= __( 'Description', GROUPS_PLUGIN_DOMAIN ); |
| 84 |
$output .= '</label>'; |
| 85 |
$output .= '<textarea id="description-field" name="description-field" rows="5" cols="45">'; |
| 86 |
$output .= stripslashes( wp_filter_nohtml_kses( $description ) ); |
| 87 |
$output .= '</textarea>'; |
| 88 |
$output .= '</div>'; |
| 89 |
|
| 90 |
$output .= '<div class="field">'; |
| 91 |
|
| 92 |
$capability_table = _groups_get_tablename( "capability" ); |
| 93 |
$capabilities = $wpdb->get_results( "SELECT * FROM $capability_table ORDER BY capability" ); |
| 94 |
|
| 95 |
$output .= '<div class="select-capability-container" style="width:62%;">'; |
| 96 |
$output .= '<label>'; |
| 97 |
$output .= __( 'Capabilities', GROUPS_PLUGIN_DOMAIN ); |
| 98 |
$output .= sprintf( |
| 99 |
'<select class="select capability" name="capability_ids[]" multiple="multiple" placeholder="%s">', |
| 100 |
__( 'Choose capabilities …', GROUPS_PLUGIN_DOMAIN ) |
| 101 |
); |
| 102 |
foreach( $capabilities as $capability ) { |
| 103 |
$output .= sprintf( '<option value="%s">%s</option>', esc_attr( $capability->capability_id ), wp_filter_nohtml_kses( $capability->capability ) ); |
| 104 |
} |
| 105 |
$output .= '</select>'; |
| 106 |
$output .= '</label>'; |
| 107 |
$output .= '</div>'; |
| 108 |
$output .= '<p class="description">'; |
| 109 |
$output .= __( 'These capabilities will be assigned to the group.', GROUPS_PLUGIN_DOMAIN ); |
| 110 |
$output .= '</p>'; |
| 111 |
|
| 112 |
$output .= Groups_UIE::render_select( '.select.capability' ); |
| 113 |
$output .= '</div>'; |
| 114 |
|
| 115 |
$output .= apply_filters( 'groups_admin_groups_add_form_after_fields', '' ); |
| 116 |
|
| 117 |
$output .= '<div class="field">'; |
| 118 |
$output .= wp_nonce_field( 'groups-add', GROUPS_ADMIN_GROUPS_NONCE, true, false ); |
| 119 |
$output .= '<input class="button button-primary" type="submit" value="' . __( 'Add', GROUPS_PLUGIN_DOMAIN ) . '"/>'; |
| 120 |
$output .= '<input type="hidden" value="add" name="action"/>'; |
| 121 |
$output .= '<a class="cancel button" href="' . esc_url( $current_url ) . '">' . __( 'Cancel', GROUPS_PLUGIN_DOMAIN ) . '</a>'; |
| 122 |
$output .= '</div>'; |
| 123 |
$output .= '</div>'; // .group.new |
| 124 |
$output .= '</form>'; |
| 125 |
$output .= '</div>'; // .manage-groups |
| 126 |
|
| 127 |
echo $output; |
| 128 |
} // function groups_admin_groups_add |
| 129 |
|
| 130 |
/** |
| 131 |
* Handle add group form submission. |
| 132 |
* @return int new group's id or false if unsuccessful |
| 133 |
*/ |
| 134 |
function groups_admin_groups_add_submit() { |
| 135 |
|
| 136 |
global $wpdb; |
| 137 |
|
| 138 |
if ( !current_user_can( GROUPS_ADMINISTER_GROUPS ) ) { |
| 139 |
wp_die( __( 'Access denied.', GROUPS_PLUGIN_DOMAIN ) ); |
| 140 |
} |
| 141 |
|
| 142 |
if ( !wp_verify_nonce( $_POST[GROUPS_ADMIN_GROUPS_NONCE], 'groups-add' ) ) { |
| 143 |
wp_die( __( 'Access denied.', GROUPS_PLUGIN_DOMAIN ) ); |
| 144 |
} |
| 145 |
|
| 146 |
$creator_id = get_current_user_id(); |
| 147 |
$datetime = date( 'Y-m-d H:i:s', time() ); |
| 148 |
$parent_id = isset( $_POST['parent-id-field'] ) ? $_POST['parent-id-field'] : null; |
| 149 |
$description = isset( $_POST['description-field'] ) ? $_POST['description-field'] : ''; |
| 150 |
$name = isset( $_POST['name-field'] ) ? $_POST['name-field'] : null; |
| 151 |
|
| 152 |
$group_id = Groups_Group::create( compact( "creator_id", "datetime", "parent_id", "description", "name" ) ); |
| 153 |
if ( $group_id ) { |
| 154 |
if ( !empty( $_POST['capability_ids'] ) ) { |
| 155 |
$caps = $_POST['capability_ids']; |
| 156 |
foreach( $caps as $cap ) { |
| 157 |
Groups_Group_Capability::create( array( 'group_id' => $group_id, 'capability_id' => $cap ) ); |
| 158 |
} |
| 159 |
} |
| 160 |
do_action( 'groups_admin_groups_add_submit_success', $group_id ); |
| 161 |
} else { |
| 162 |
if ( !$name ) { |
| 163 |
Groups_Admin::add_message( __( 'The name must not be empty.', GROUPS_PLUGIN_DOMAIN ), 'error' ); |
| 164 |
} else if ( Groups_Group::read_by_name( $name ) ) { |
| 165 |
Groups_Admin::add_message( sprintf( __( 'The <em>%s</em> group already exists.', GROUPS_PLUGIN_DOMAIN ), stripslashes( wp_filter_nohtml_kses( ( $name ) ) ) ), 'error' ); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
return $group_id; |
| 170 |
} // function groups_admin_groups_add_submit |
| 171 |
|