/**
 * Show edit group form.
 * @param int $group_id group id
 */
function groups_admin_groups_edit($group_id)
{
    global $wpdb;
    $output = '';
    if (!current_user_can(GROUPS_ADMINISTER_GROUPS)) {
        wp_die(__('Access denied.', GROUPS_PLUGIN_DOMAIN));
    }
    $group = Groups_Group::read(intval($group_id));
    if (empty($group)) {
        wp_die(__('No such group.', GROUPS_PLUGIN_DOMAIN));
    }
    $current_url = (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $current_url = remove_query_arg('action', $current_url);
    $current_url = remove_query_arg('group_id', $current_url);
    $name = isset($_POST['name-field']) ? $_POST['name-field'] : $group->name;
    $description = isset($_POST['description-field']) ? $_POST['description-field'] : $group->description;
    $parent_id = isset($_POST['parent-id-field']) ? $_POST['parent-id-field'] : $group->parent_id;
    $group_table = _groups_get_tablename('group');
    $parent_select = '<select name="parent-id-field">';
    $parent_select .= '<option value="">--</option>';
    $groups = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$group_table} WHERE group_id != %d", $group->group_id));
    foreach ($groups as $g) {
        $selected = $g->group_id == $group->parent_id ? ' selected="selected" ' : '';
        $parent_select .= '<option ' . $selected . 'value="' . esc_attr($g->group_id) . '">' . wp_filter_nohtml_kses($g->name) . '</option>';
    }
    $parent_select .= '</select>';
    $name_readonly = $name !== Groups_Registered::REGISTERED_GROUP_NAME ? "" : ' readonly="readonly" ';
    $output .= '<div class="manage-groups">';
    $output .= '<div>';
    $output .= '<h2>';
    $output .= __('Edit a group', GROUPS_PLUGIN_DOMAIN);
    $output .= '</h2>';
    $output .= '</div>';
    $output .= Groups_Admin::render_messages();
    $output .= '<form id="edit-group" action="' . esc_url($current_url) . '" method="post">';
    $output .= '<div class="group edit">';
    $output .= '<input id="group-id-field" name="group-id-field" type="hidden" value="' . esc_attr(intval($group_id)) . '"/>';
    $output .= '<div class="field">';
    $output .= '<label for="name-field" class="field-label first required">';
    $output .= __('Name', GROUPS_PLUGIN_DOMAIN);
    $output .= '</label>';
    $output .= '<input ' . $name_readonly . ' id="name-field" name="name-field" class="namefield" type="text" value="' . esc_attr(stripslashes($name)) . '"/>';
    $output .= '</div>';
    $output .= '<div class="field">';
    $output .= '<label for="parent-id-field" class="field-label">';
    $output .= __('Parent', GROUPS_PLUGIN_DOMAIN);
    $output .= '</label>';
    $output .= $parent_select;
    $output .= '</div>';
    $output .= '<div class="field">';
    $output .= '<label for="description-field" class="field-label description-field">';
    $output .= __('Description', GROUPS_PLUGIN_DOMAIN);
    $output .= '</label>';
    $output .= '<textarea id="description-field" name="description-field" rows="5" cols="45">';
    $output .= stripslashes(wp_filter_nohtml_kses($description));
    $output .= '</textarea>';
    $output .= '</div>';
    $capability_table = _groups_get_tablename('capability');
    $group_capability_table = _groups_get_tablename('group_capability');
    $group_capabilities = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$capability_table} WHERE capability_id IN ( SELECT capability_id FROM {$group_capability_table} WHERE group_id = %d )", Groups_Utility::id($group_id)));
    $group_capabilities_array = array();
    if (count($group_capabilities) > 0) {
        foreach ($group_capabilities as $group_capability) {
            $group_capabilities_array[] = $group_capability->capability_id;
        }
    }
    $capabilities = $wpdb->get_results("SELECT * FROM {$capability_table} ORDER BY capability");
    $output .= '<div class="field">';
    $output .= '<div class="select-capability-container" style="width:62%;">';
    $output .= '<label>';
    $output .= __('Capabilities', GROUPS_PLUGIN_DOMAIN);
    $output .= sprintf('<select class="select capability" name="capability_ids[]" multiple="multiple" placeholder="%s">', __('Choose capabilities &hellip;', GROUPS_PLUGIN_DOMAIN));
    foreach ($capabilities as $capability) {
        $selected = in_array($capability->capability_id, $group_capabilities_array) ? ' selected="selected" ' : '';
        $output .= sprintf('<option value="%s" %s>%s</option>', esc_attr($capability->capability_id), $selected, wp_filter_nohtml_kses($capability->capability));
    }
    $output .= '</select>';
    $output .= '</label>';
    $output .= '</div>';
    // .select-capability-container
    $output .= '<p class="description">';
    $output .= __('The chosen capabilities are assigned to the group.', GROUPS_PLUGIN_DOMAIN);
    $output .= '</p>';
    $output .= '</div>';
    // .field
    $output .= Groups_UIE::render_select('.select.capability');
    $group_object = new Groups_Group($group_id);
    $group_capabilities = $group_object->capabilities;
    $group_capabilities_deep = $group_object->capabilities_deep;
    if (count($group_capabilities_deep) - count($group_capabilities) > 0) {
        usort($group_capabilities_deep, array('Groups_Utility', 'cmp'));
        $output .= '<div class="field">';
        $output .= __('Inherited capabilities:', GROUPS_PLUGIN_DOMAIN);
        $output .= ' ';
        $inherited_caps = array();
        foreach ($group_capabilities_deep as $group_capability) {
            $class = '';
            if (empty($group_capabilities) || !in_array($group_capability, $group_capabilities)) {
                $inherited_caps[] = wp_filter_nohtml_kses($group_capability->capability->capability);
            }
        }
        $output .= implode(' ', $inherited_caps);
        $output .= '</div>';
    }
    $output .= '<div class="field">';
    $output .= wp_nonce_field('groups-edit', GROUPS_ADMIN_GROUPS_NONCE, true, false);
    $output .= '<input class="button button-primary" type="submit" value="' . __('Save', GROUPS_PLUGIN_DOMAIN) . '"/>';
    $output .= '<input type="hidden" value="edit" name="action"/>';
    $output .= '<a class="cancel button" href="' . esc_url($current_url) . '">' . __('Cancel', GROUPS_PLUGIN_DOMAIN) . '</a>';
    $output .= '</div>';
    $output .= '</div>';
    // .group.edit
    $output .= '</form>';
    $output .= '</div>';
    // .manage-groups
    echo $output;
    Groups_Help::footer();
}
/**
 * Show add group form.
 */
function groups_admin_groups_add()
{
    global $wpdb;
    $output = '';
    if (!current_user_can(GROUPS_ADMINISTER_GROUPS)) {
        wp_die(__('Access denied.', GROUPS_PLUGIN_DOMAIN));
    }
    $current_url = (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $current_url = remove_query_arg('paged', $current_url);
    $current_url = remove_query_arg('action', $current_url);
    $current_url = remove_query_arg('group_id', $current_url);
    $parent_id = isset($_POST['parent-id-field']) ? $_POST['parent-id-field'] : '';
    $name = isset($_POST['name-field']) ? $_POST['name-field'] : '';
    $description = isset($_POST['description-field']) ? $_POST['description-field'] : '';
    $group_table = _groups_get_tablename('group');
    $parent_select = '<select name="parent-id-field">';
    $parent_select .= '<option value="">--</option>';
    $groups = $wpdb->get_results("SELECT * FROM {$group_table}");
    foreach ($groups as $group) {
        $parent_select .= '<option value="' . esc_attr($group->group_id) . '">' . wp_filter_nohtml_kses($group->name) . '</option>';
    }
    $parent_select .= '</select>';
    $output .= '<div class="manage-groups wrap">';
    $output .= '<h1>';
    $output .= __('Add a new group', GROUPS_PLUGIN_DOMAIN);
    $output .= '</h1>';
    $output .= Groups_Admin::render_messages();
    $output .= '<form id="add-group" action="' . esc_url($current_url) . '" method="post">';
    $output .= '<div class="group new">';
    $output .= '<div class="field">';
    $output .= '<label for="name-field" class="field-label first required">';
    $output .= __('Name', GROUPS_PLUGIN_DOMAIN);
    $output .= '</label>';
    $output .= '<input id="name-field" name="name-field" class="namefield" type="text" value="' . esc_attr(stripslashes($name)) . '"/>';
    $output .= '</div>';
    $output .= '<div class="field">';
    $output .= '<label for="parent-id-field" class="field-label">';
    $output .= __('Parent', GROUPS_PLUGIN_DOMAIN);
    $output .= '</label>';
    $output .= $parent_select;
    $output .= '</div>';
    $output .= '<div class="field">';
    $output .= '<label for="description-field" class="field-label description-field">';
    $output .= __('Description', GROUPS_PLUGIN_DOMAIN);
    $output .= '</label>';
    $output .= '<textarea id="description-field" name="description-field" rows="5" cols="45">';
    $output .= stripslashes(wp_filter_nohtml_kses($description));
    $output .= '</textarea>';
    $output .= '</div>';
    $output .= '<div class="field">';
    $capability_table = _groups_get_tablename("capability");
    $capabilities = $wpdb->get_results("SELECT * FROM {$capability_table} ORDER BY capability");
    $output .= '<div class="select-capability-container" style="width:62%;">';
    $output .= '<label>';
    $output .= __('Capabilities', GROUPS_PLUGIN_DOMAIN);
    $output .= sprintf('<select class="select capability" name="capability_ids[]" multiple="multiple" placeholder="%s">', __('Choose capabilities &hellip;', GROUPS_PLUGIN_DOMAIN));
    foreach ($capabilities as $capability) {
        $output .= sprintf('<option value="%s">%s</option>', esc_attr($capability->capability_id), wp_filter_nohtml_kses($capability->capability));
    }
    $output .= '</select>';
    $output .= '</label>';
    $output .= '</div>';
    $output .= '<p class="description">';
    $output .= __('These capabilities will be assigned to the group.', GROUPS_PLUGIN_DOMAIN);
    $output .= '</p>';
    $output .= Groups_UIE::render_select('.select.capability');
    $output .= '</div>';
    $output .= apply_filters('groups_admin_groups_add_form_after_fields', '');
    $output .= '<div class="field">';
    $output .= wp_nonce_field('groups-add', GROUPS_ADMIN_GROUPS_NONCE, true, false);
    $output .= '<input class="button button-primary" type="submit" value="' . __('Add', GROUPS_PLUGIN_DOMAIN) . '"/>';
    $output .= '<input type="hidden" value="add" name="action"/>';
    $output .= '<a class="cancel button" href="' . esc_url($current_url) . '">' . __('Cancel', GROUPS_PLUGIN_DOMAIN) . '</a>';
    $output .= '</div>';
    $output .= '</div>';
    // .group.new
    $output .= '</form>';
    $output .= '</div>';
    // .manage-groups
    echo $output;
}
 /**
  * Renders group actions in the users table's extra_tablenav().
  */
 public static function restrict_manage_users()
 {
     global $pagenow, $wpdb, $groups_select_user_groups_index;
     // We don't handle multiple instances so don't render another.
     if (!isset($groups_select_user_groups_index)) {
         $groups_select_user_groups_index = 0;
     } else {
         return '';
     }
     $output = '';
     if ($pagenow == 'users.php' && empty($_GET['page'])) {
         $group_table = _groups_get_tablename("group");
         // groups select
         $groups_table = _groups_get_tablename('group');
         if ($groups = $wpdb->get_results("SELECT * FROM {$groups_table} ORDER BY name")) {
             $groups_select = sprintf('<select id="user-groups" class="groups" name="group_ids[]" multiple="multiple" placeholder="%s" data-placeholder="%s">', esc_attr(__('Choose groups &hellip;', GROUPS_PLUGIN_DOMAIN)), esc_attr(__('Choose groups &hellip;', GROUPS_PLUGIN_DOMAIN)));
             foreach ($groups as $group) {
                 $is_member = false;
                 $groups_select .= sprintf('<option value="%d" %s>%s</option>', Groups_Utility::id($group->group_id), $is_member ? ' selected="selected" ' : '', wp_filter_nohtml_kses($group->name));
             }
             $groups_select .= '</select>';
         }
         // group bulk actions added through extra_tablenav()
         $box = '<div id="group-bulk-actions" class="groups-bulk-container">';
         $box .= '<div class="groups-select-container">';
         $box .= $groups_select;
         $box .= '</div>';
         $box .= '<select class="groups-action" name="groups-action">';
         $box .= '<option selected="selected" value="-1">' . __('Group Actions', GROUPS_PLUGIN_DOMAIN) . '</option>';
         $box .= '<option value="add-group">' . __('Add to group', GROUPS_PLUGIN_DOMAIN) . '</option>';
         $box .= '<option value="remove-group">' . __('Remove from group', GROUPS_PLUGIN_DOMAIN) . '</option>';
         $box .= '</select>';
         $box .= sprintf('<input class="button" type="submit" name="groups" value="%s" />', __('Apply', GROUPS_PLUGIN_DOMAIN));
         $box .= '</div>';
         $box = str_replace('"', "'", $box);
         $nonce = wp_nonce_field('user-group', 'bulk-user-group-nonce', true, false);
         $nonce = str_replace('"', "'", $nonce);
         $box .= $nonce;
         $box .= '<script type="text/javascript">';
         $box .= 'if ( typeof jQuery !== "undefined" ) {';
         $box .= 'jQuery("document").ready(function(){';
         $box .= 'jQuery(".tablenav.top .alignleft.actions:last").after("<div id=\\"groups-bulk-actions-block\\" class=\\"alignleft actions\\"></div>");';
         $box .= 'jQuery("#group-bulk-actions").appendTo(jQuery("#groups-bulk-actions-block"));';
         $box .= '});';
         $box .= '}';
         $box .= '</script>';
         $output .= $box;
         $output .= Groups_UIE::render_select('#user-groups');
     }
     echo $output;
 }
/**
 * Options admin screen.
 */
function groups_admin_options()
{
    global $wpdb, $wp_roles;
    if (!current_user_can(GROUPS_ADMINISTER_OPTIONS)) {
        wp_die(__('Access denied.', GROUPS_PLUGIN_DOMAIN));
    }
    $is_sitewide_plugin = false;
    if (is_multisite()) {
        $active_sitewide_plugins = get_site_option('active_sitewide_plugins', array());
        $active_sitewide_plugins = array_keys($active_sitewide_plugins);
        $is_sitewide_plugin = in_array('groups/groups.php', $active_sitewide_plugins);
    }
    $caps = array(GROUPS_ACCESS_GROUPS => __('Access Groups', GROUPS_PLUGIN_DOMAIN), GROUPS_ADMINISTER_GROUPS => __('Administer Groups', GROUPS_PLUGIN_DOMAIN), GROUPS_ADMINISTER_OPTIONS => __('Administer Groups plugin options', GROUPS_PLUGIN_DOMAIN));
    //
    // handle options form submission
    //
    if (isset($_POST['submit'])) {
        if (wp_verify_nonce($_POST[GROUPS_ADMIN_OPTIONS_NONCE], 'admin')) {
            // admin override
            if (empty($_POST[GROUPS_ADMINISTRATOR_ACCESS_OVERRIDE])) {
                $admin_override = false;
            } else {
                $admin_override = true;
            }
            // Don't move this to the plugin options, access will be faster
            add_option(GROUPS_ADMINISTRATOR_ACCESS_OVERRIDE, $admin_override);
            // WP 3.3.1 : update alone wouldn't create the option when value is false
            update_option(GROUPS_ADMINISTRATOR_ACCESS_OVERRIDE, $admin_override);
            $post_types_option = Groups_Options::get_option(Groups_Post_Access::POST_TYPES, array());
            $post_types = get_post_types(array('public' => true));
            $selected_post_types = is_array($_POST['add_meta_boxes']) ? $_POST['add_meta_boxes'] : array();
            foreach ($post_types as $post_type) {
                $post_types_option[$post_type]['add_meta_box'] = in_array($post_type, $selected_post_types);
            }
            Groups_Options::update_option(Groups_Post_Access::POST_TYPES, $post_types_option);
            $valid_read_caps = array(Groups_Post_Access::READ_POST_CAPABILITY);
            if (!empty($_POST[GROUPS_READ_POST_CAPABILITIES])) {
                $read_caps = $_POST[GROUPS_READ_POST_CAPABILITIES];
                foreach ($read_caps as $read_cap) {
                    if ($valid_cap = Groups_Capability::read($read_cap)) {
                        if (!in_array($valid_cap->capability, $valid_read_caps)) {
                            $valid_read_caps[] = $valid_cap->capability;
                        }
                    }
                }
            }
            Groups_Options::update_option(Groups_Post_Access::READ_POST_CAPABILITIES, $valid_read_caps);
            // tree view
            if (!empty($_POST[GROUPS_SHOW_TREE_VIEW])) {
                Groups_Options::update_option(GROUPS_SHOW_TREE_VIEW, true);
            } else {
                Groups_Options::update_option(GROUPS_SHOW_TREE_VIEW, false);
            }
            // show in user profiles
            Groups_Options::update_option(GROUPS_SHOW_IN_USER_PROFILE, !empty($_POST[GROUPS_SHOW_IN_USER_PROFILE]));
            // roles & capabilities
            $rolenames = $wp_roles->get_names();
            foreach ($rolenames as $rolekey => $rolename) {
                $role = $wp_roles->get_role($rolekey);
                foreach ($caps as $capkey => $capname) {
                    $role_cap_id = $rolekey . '-' . $capkey;
                    if (!empty($_POST[$role_cap_id])) {
                        $role->add_cap($capkey);
                    } else {
                        $role->remove_cap($capkey);
                    }
                }
            }
            Groups_Controller::assure_capabilities();
            if (!$is_sitewide_plugin) {
                // delete data
                if (!empty($_POST['delete-data'])) {
                    Groups_Options::update_option('groups_delete_data', true);
                } else {
                    Groups_Options::update_option('groups_delete_data', false);
                }
            }
            Groups_Admin::add_message(__('Options saved.', GROUPS_PLUGIN_DOMAIN));
        }
    }
    echo '<div class="groups-options">';
    echo '<h1>' . __('Groups options', GROUPS_PLUGIN_DOMAIN) . '</h1>';
    echo Groups_Admin::render_messages();
    $admin_override = get_option(GROUPS_ADMINISTRATOR_ACCESS_OVERRIDE, GROUPS_ADMINISTRATOR_ACCESS_OVERRIDE_DEFAULT);
    $show_tree_view = Groups_Options::get_option(GROUPS_SHOW_TREE_VIEW, GROUPS_SHOW_TREE_VIEW_DEFAULT);
    $show_in_user_profile = Groups_Options::get_option(GROUPS_SHOW_IN_USER_PROFILE, GROUPS_SHOW_IN_USER_PROFILE_DEFAULT);
    $rolenames = $wp_roles->get_names();
    $caps_table = '<table class="groups-permissions">';
    $caps_table .= '<thead>';
    $caps_table .= '<tr>';
    $caps_table .= '<td class="role">';
    $caps_table .= __('Role', GROUPS_PLUGIN_DOMAIN);
    $caps_table .= '</td>';
    foreach ($caps as $cap) {
        $caps_table .= '<td class="cap">';
        $caps_table .= $cap;
        $caps_table .= '</td>';
    }
    $caps_table .= '</tr>';
    $caps_table .= '</thead>';
    $caps_table .= '<tbody>';
    foreach ($rolenames as $rolekey => $rolename) {
        $role = $wp_roles->get_role($rolekey);
        $caps_table .= '<tr>';
        $caps_table .= '<td>';
        $caps_table .= translate_user_role($rolename);
        $caps_table .= '</td>';
        foreach ($caps as $capkey => $capname) {
            if ($role->has_cap($capkey)) {
                $checked = ' checked="checked" ';
            } else {
                $checked = '';
            }
            $caps_table .= '<td class="checkbox">';
            $role_cap_id = $rolekey . '-' . $capkey;
            $caps_table .= '<input type="checkbox" name="' . $role_cap_id . '" id="' . $role_cap_id . '" ' . $checked . '/>';
            $caps_table .= '</td>';
        }
        $caps_table .= '</tr>';
    }
    $caps_table .= '</tbody>';
    $caps_table .= '</table>';
    $delete_data = Groups_Options::get_option('groups_delete_data', false);
    if (isset($_GET['dismiss-groups-extensions-box']) && isset($_GET['groups-extensions-box-nonce']) && wp_verify_nonce($_GET['groups-extensions-box-nonce'], 'dismiss-box')) {
        Groups_Options::update_user_option('show-extensions-box', false);
    }
    $extensions_box = '';
    if (Groups_Options::get_user_option('show-extensions-box', true)) {
        $dismiss_url = wp_nonce_url(add_query_arg('dismiss-groups-extensions-box', '1', admin_url('admin.php?page=groups-admin-options')), 'dismiss-box', 'groups-extensions-box-nonce');
        $extensions_box = '<div id="groups-extensions-box">' . __('Enhanced functionality is available via official <a href="http://www.itthinx.com/shop/">Extensions</a> for Groups.', GROUPS_PLUGIN_DOMAIN) . sprintf('<a class="close" href="%s">x</a>', esc_url($dismiss_url)) . '</div>';
    }
    //
    // print the options form
    //
    echo '<form action="" name="options" method="post">' . '<p>' . '<input class="button button-primary" type="submit" name="submit" value="' . __('Save', GROUPS_PLUGIN_DOMAIN) . '"/>' . $extensions_box . '</p>' . '<div>' . '<h2>' . __('Administrator Access Override', GROUPS_PLUGIN_DOMAIN) . '</h2>' . '<p>' . '<label>' . '<input name="' . GROUPS_ADMINISTRATOR_ACCESS_OVERRIDE . '" type="checkbox" ' . ($admin_override ? 'checked="checked"' : '') . '/>' . __('Administrators override all access permissions derived from Groups capabilities.', GROUPS_PLUGIN_DOMAIN) . '</label>' . '</p>';
    echo '<h2>' . __('Access restricions', GROUPS_PLUGIN_DOMAIN) . '</h2>';
    echo '<h3>' . __('Post types', GROUPS_PLUGIN_DOMAIN) . '</h3>';
    echo '<p class="description">' . __('Show access restrictions for these post types.', GROUPS_PLUGIN_DOMAIN) . '</p>';
    $post_types_option = Groups_Options::get_option(Groups_Post_Access::POST_TYPES, array());
    $post_types = get_post_types(array('public' => true));
    echo '<ul>';
    foreach ($post_types as $post_type) {
        $post_type_object = get_post_type_object($post_type);
        echo '<li>';
        echo '<label>';
        $label = $post_type;
        $labels = isset($post_type_object->labels) ? $post_type_object->labels : null;
        if ($labels !== null && isset($labels->singular_name)) {
            $label = __($labels->singular_name);
        }
        $checked = !isset($post_types_option[$post_type]['add_meta_box']) || $post_types_option[$post_type]['add_meta_box'] ? ' checked="checked" ' : '';
        echo '<input name="add_meta_boxes[]" type="checkbox" value="' . esc_attr($post_type) . '" ' . $checked . '/>';
        echo $label;
        echo '</label>';
        echo '</li>';
    }
    echo '<ul>';
    echo '<p class="description">' . __('This determines for which post types access restriction settings are offered.', GROUPS_PLUGIN_DOMAIN) . '<br/>' . __('Disabling this setting for a post type does not remove existing access restrictions on individual posts of that type.', GROUPS_PLUGIN_DOMAIN) . '<br/>' . '</p>';
    echo '<h3>' . __('Capabilities', GROUPS_PLUGIN_DOMAIN) . '</h3>';
    echo '<p class="description">' . __('Include these capabilities to enforce read access on posts. The selected capabilities will be offered to restrict access to posts.', GROUPS_PLUGIN_DOMAIN) . '</p>';
    $capability_table = _groups_get_tablename("capability");
    $capabilities = $wpdb->get_results("SELECT * FROM {$capability_table} ORDER BY capability");
    $applicable_read_caps = Groups_Options::get_option(Groups_Post_Access::READ_POST_CAPABILITIES, array(Groups_Post_Access::READ_POST_CAPABILITY));
    echo '<div class="select-capability-container" style="width:62%;">';
    printf('<select class="select capability" name="%s" multiple="multiple">', GROUPS_READ_POST_CAPABILITIES . '[]');
    foreach ($capabilities as $capability) {
        $selected = in_array($capability->capability, $applicable_read_caps) ? ' selected="selected" ' : '';
        if ($capability->capability == Groups_Post_Access::READ_POST_CAPABILITY) {
            $selected .= ' disabled="disabled" ';
        }
        printf('<option value="%s" %s>%s</option>', esc_attr($capability->capability_id), $selected, wp_filter_nohtml_kses($capability->capability));
    }
    echo '</select>';
    echo '</div>';
    echo Groups_UIE::render_select('.select.capability');
    echo '<h2>' . __('User profiles', GROUPS_PLUGIN_DOMAIN) . '</h2>' . '<p>' . '<label>' . '<input name="' . GROUPS_SHOW_IN_USER_PROFILE . '" type="checkbox" ' . ($show_in_user_profile ? 'checked="checked"' : '') . '/>' . __('Show groups in user profiles.', GROUPS_PLUGIN_DOMAIN) . '</label>' . '</p>';
    echo '<h2>' . __('Tree view', GROUPS_PLUGIN_DOMAIN) . '</h2>' . '<p>' . '<label>' . '<input name="' . GROUPS_SHOW_TREE_VIEW . '" type="checkbox" ' . ($show_tree_view ? 'checked="checked"' : '') . '/>' . __('Show the Groups tree view.', GROUPS_PLUGIN_DOMAIN) . '</label>' . '</p>';
    echo '<h2>' . __('Permissions', GROUPS_PLUGIN_DOMAIN) . '</h2>' . '<p>' . __('These permissions apply to Groups management. They do not apply to access permissions derived from Groups capabilities.', GROUPS_PLUGIN_DOMAIN) . '</p>' . $caps_table . '<p class="description">' . __('A minimum set of permissions will be preserved.', GROUPS_PLUGIN_DOMAIN) . '<br/>' . __('If you lock yourself out, please ask an administrator to help.', GROUPS_PLUGIN_DOMAIN) . '</p>';
    if (!$is_sitewide_plugin) {
        echo '<h2>' . __('Deactivation and data persistence', GROUPS_PLUGIN_DOMAIN) . '</h2>' . '<p>' . '<label>' . '<input name="delete-data" type="checkbox" ' . ($delete_data ? 'checked="checked"' : '') . '/>' . __('Delete all Groups plugin data on deactivation', GROUPS_PLUGIN_DOMAIN) . '</label>' . '</p>' . '<p class="description warning">' . __('CAUTION: If this option is active while the plugin is deactivated, ALL plugin settings and data will be DELETED. If you are going to use this option, now would be a good time to make a backup. By enabling this option you agree to be solely responsible for any loss of data or any other consequences thereof.', GROUPS_PLUGIN_DOMAIN) . '</p>';
    }
    echo '<p>' . wp_nonce_field('admin', GROUPS_ADMIN_OPTIONS_NONCE, true, false) . '<input class="button button-primary" type="submit" name="submit" value="' . __('Save', GROUPS_PLUGIN_DOMAIN) . '"/>' . '</p>' . '</div>' . '</form>';
    echo '</div>';
    // .groups-options
    Groups_Help::footer();
}
 /**
  * Renders group selections for variations.
  * 
  * @param int $loop
  * @param array $variation_data
  * @param WP_Post $variation
  */
 public static function woocommerce_product_after_variable_attributes($loop, $variation_data, $variation)
 {
     global $post, $wpdb;
     $output = '';
     $output .= '<tr><td><div>';
     $variation_groups = get_post_meta($variation->ID, '_groups_variation_groups', false);
     $variation_groups_remove = get_post_meta($variation->ID, '_groups_variation_groups_remove', false);
     $groups_table = _groups_get_tablename('group');
     if ($groups = $wpdb->get_results("SELECT * FROM {$groups_table} ORDER BY name")) {
         // text style
         $output .= '<style type="text/css">';
         $output .= '.groups-woocommerce .selectize-input { font-size: inherit; }';
         $output .= '</style>';
         // add to groups
         $output .= '<label>';
         $output .= __('Add to Groups', GROUPS_WS_PLUGIN_DOMAIN);
         $output .= ' ';
         $output .= sprintf('<select id="variation-groups-%d" class="groups-woocommerce" name="_groups_variation_groups[%d][]" multiple="multiple" placeholder="%s" data-placeholder="%s">', esc_attr($variation->ID), esc_attr($variation->ID), esc_attr(__('Choose groups &hellip;', GROUPS_WS_PLUGIN_DOMAIN)), esc_attr(__('Choose groups &hellip;', GROUPS_WS_PLUGIN_DOMAIN)));
         foreach ($groups as $group) {
             $selected = is_array($variation_groups) && in_array($group->group_id, $variation_groups);
             $output .= sprintf('<option value="%d" %s>%s</option>', Groups_Utility::id($group->group_id), $selected ? ' selected="selected" ' : '', wp_filter_nohtml_kses($group->name));
         }
         $output .= '</select>';
         $output .= '</label>';
         $output .= Groups_UIE::render_select('#variation-groups-' . esc_attr($variation->ID));
         $output .= '<p class="description">' . __('Add the customer to these groups when purchasing this variation.', GROUPS_WS_PLUGIN_DOMAIN) . '</p>';
         // remove from groups
         $output .= '<label>';
         $output .= __('Remove from Groups', GROUPS_WS_PLUGIN_DOMAIN);
         $output .= ' ';
         $output .= sprintf('<select id="variation-groups-remove-%d" class="groups-woocommerce" name="_groups_variation_groups_remove[%d][]" multiple="multiple" placeholder="%s" data-placeholder="%s">', esc_attr($variation->ID), esc_attr($variation->ID), esc_attr(__('Choose groups &hellip;', GROUPS_WS_PLUGIN_DOMAIN)), esc_attr(__('Choose groups &hellip;', GROUPS_WS_PLUGIN_DOMAIN)));
         foreach ($groups as $group) {
             $selected = is_array($variation_groups_remove) && in_array($group->group_id, $variation_groups_remove);
             $output .= sprintf('<option value="%d" %s>%s</option>', Groups_Utility::id($group->group_id), $selected ? ' selected="selected" ' : '', wp_filter_nohtml_kses($group->name));
         }
         $output .= '</select>';
         $output .= '</label>';
         $output .= Groups_UIE::render_select('#variation-groups-remove-' . esc_attr($variation->ID));
         $output .= '<p class="description">' . __('Remove the customer from these groups when purchasing this variation.', GROUPS_WS_PLUGIN_DOMAIN) . '</p>';
         $is_subscription = isset($post->ID) && class_exists('WC_Subscriptions_Product') && WC_Subscriptions_Product::is_subscription($post->ID);
         $output .= sprintf('<div class="groups-panel-item-simple" style="%s">', $is_subscription ? 'display:none;' : '');
         $output .= '<p>' . __('If set, the duration limitations in the <em>Groups</em> settings of the variable product apply.', GROUPS_WS_PLUGIN_DOMAIN) . '</p>';
         $output .= '</div>';
     }
     $output .= '</div></td></tr>';
     echo $output;
 }
 /**
  * Render capabilities box for attachment post type (Media).
  * @param array $form_fields
  * @param object $post
  * @return array
  */
 public static function attachment_fields_to_edit($form_fields, $post)
 {
     Groups_UIE::enqueue('select');
     $post_types_option = Groups_Options::get_option(Groups_Post_Access::POST_TYPES, array());
     if (!isset($post_types_option['attachment']['add_meta_box']) || $post_types_option['attachment']['add_meta_box']) {
         if (self::user_can_restrict()) {
             $user = new Groups_User(get_current_user_id());
             $output = "";
             $post_singular_name = __('Media', GROUPS_PLUGIN_DOMAIN);
             $output .= __("Enforce read access", GROUPS_PLUGIN_DOMAIN);
             $read_caps = get_post_meta($post->ID, Groups_Post_Access::POSTMETA_PREFIX . Groups_Post_Access::READ_POST_CAPABILITY);
             $valid_read_caps = self::get_valid_read_caps_for_user();
             // On attachments edited within the 'Insert Media' popup, the update is triggered too soon and we end up with only the last capability selected.
             // This occurs when using normal checkboxes as well as the select below (Chosen and Selectize tested).
             // With checkboxes it's even more confusing, it's actually better to have it using a select as below,
             // because the visual feedback corresponds with what is assigned.
             // See http://wordpress.org/support/topic/multiple-access-restrictions-for-media-items-are-not-saved-in-grid-view
             // and https://core.trac.wordpress.org/ticket/28053 - this is an issue with multiple value fields and should
             // be fixed within WordPress.
             // 				$output .= '<div style="padding:0 1em;margin:1em 0;border:1px solid #ccc;border-radius:4px;">';
             // 				$output .= '<ul>';
             // 				foreach( $valid_read_caps as $valid_read_cap ) {
             // 					if ( $capability = Groups_Capability::read_by_capability( $valid_read_cap ) ) {
             // 						$checked = in_array( $capability->capability, $read_caps ) ? ' checked="checked" ' : '';
             // 						$output .= '<li>';
             // 						$output .= '<label>';
             // 						$output .= '<input name="attachments[' . $post->ID . '][' . self::CAPABILITY . '][]" ' . $checked . ' type="checkbox" value="' . esc_attr( $capability->capability_id ) . '" />';
             // 						$output .= wp_filter_nohtml_kses( $capability->capability );
             // 						$output .= '</label>';
             // 						$output .= '</li>';
             // 					}
             // 				}
             // 				$output .= '</ul>';
             // 				$output .= '</div>';
             $show_groups = Groups_Options::get_user_option(self::SHOW_GROUPS, true);
             $output .= '<div class="select-capability-container">';
             $select_id = 'attachments-' . $post->ID . '-' . self::CAPABILITY;
             $output .= sprintf('<select id="%s" class="select capability" name="%s" multiple="multiple" data-placeholder="%s" title="%s">', $select_id, 'attachments[' . $post->ID . '][' . self::CAPABILITY . '][]', __('Type and choose &hellip;', GROUPS_PLUGIN_DOMAIN), __('Choose one or more capabilities to restrict access. Groups that grant access through the capabilities are shown in parenthesis. If no capabilities are available yet, you can use the quick-create box to create a group and capability enabled for access restriction on the fly.', GROUPS_PLUGIN_DOMAIN));
             $output .= '<option value=""></option>';
             foreach ($valid_read_caps as $valid_read_cap) {
                 if ($capability = Groups_Capability::read_by_capability($valid_read_cap)) {
                     if ($user->can($capability->capability)) {
                         $c = new Groups_Capability($capability->capability_id);
                         $groups = $c->groups;
                         $group_names = array();
                         if (!empty($groups)) {
                             foreach ($groups as $group) {
                                 $group_names[] = $group->name;
                             }
                         }
                         if (count($group_names) > 0) {
                             $label_title = sprintf(_n('Members of the %1$s group can access this %2$s through this capability.', 'Members of the %1$s groups can access this %2$s through this capability.', count($group_names), GROUPS_PLUGIN_DOMAIN), wp_filter_nohtml_kses(implode(',', $group_names)), $post_singular_name);
                         } else {
                             $label_title = __('No groups grant access through this capability. To grant access to group members using this capability, you should assign it to a group and enable the capability for access restriction.', GROUPS_PLUGIN_DOMAIN);
                         }
                         $output .= sprintf('<option value="%s" %s>', esc_attr($capability->capability_id), in_array($capability->capability, $read_caps) ? ' selected="selected" ' : '');
                         $output .= wp_filter_nohtml_kses($capability->capability);
                         if ($show_groups) {
                             if (count($group_names) > 0) {
                                 $output .= ' ';
                                 $output .= '(' . wp_filter_nohtml_kses(implode(', ', $group_names)) . ')';
                             }
                         }
                         $output .= '</option>';
                     }
                 }
             }
             $output .= '</select>';
             $output .= Groups_UIE::render_select('#' . $select_id);
             $output .= '</div>';
             $output .= '<p class="description">';
             $output .= sprintf(__("Only groups or users that have one of the selected capabilities are allowed to read this %s.", GROUPS_PLUGIN_DOMAIN), $post_singular_name);
             $output .= '</p>';
             $form_fields['groups_access'] = array('label' => __('Access restrictions', GROUPS_PLUGIN_DOMAIN), 'input' => 'html', 'html' => $output);
         }
     }
     return $form_fields;
 }
/**
 * Manage Groups: table of groups and add, edit, remove actions.
 */
function groups_admin_groups()
{
    global $wpdb;
    $output = '';
    $today = date('Y-m-d', time());
    if (!current_user_can(GROUPS_ADMINISTER_GROUPS)) {
        wp_die(__('Access denied.', GROUPS_PLUGIN_DOMAIN));
    }
    //
    // handle actions
    //
    if (isset($_POST['action'])) {
        //  handle action submit - do it
        switch ($_POST['action']) {
            case 'add':
                if (!($group_id = groups_admin_groups_add_submit())) {
                    return groups_admin_groups_add();
                } else {
                    $group = Groups_Group::read($group_id);
                    Groups_Admin::add_message(sprintf(__("The <em>%s</em> group has been created.", GROUPS_PLUGIN_DOMAIN), stripslashes(wp_filter_nohtml_kses($group->name))));
                }
                break;
            case 'edit':
                if (!($group_id = groups_admin_groups_edit_submit())) {
                    return groups_admin_groups_edit($_POST['group-id-field']);
                } else {
                    $group = Groups_Group::read($group_id);
                    Groups_Admin::add_message(sprintf(__('The <em>%s</em> group has been updated.', GROUPS_PLUGIN_DOMAIN), stripslashes(wp_filter_nohtml_kses($group->name))));
                }
                break;
            case 'remove':
                if ($group_id = groups_admin_groups_remove_submit()) {
                    Groups_Admin::add_message(__('The group has been deleted.', GROUPS_PLUGIN_DOMAIN));
                }
                break;
                // bulk actions on groups: add capabilities, remove capabilities, remove groups
            // bulk actions on groups: add capabilities, remove capabilities, remove groups
            case 'groups-action':
                if (wp_verify_nonce($_POST[GROUPS_ADMIN_GROUPS_ACTION_NONCE], 'admin')) {
                    $group_ids = isset($_POST['group_ids']) ? $_POST['group_ids'] : null;
                    $bulk_action = null;
                    if (isset($_POST['bulk'])) {
                        $bulk_action = $_POST['bulk-action'];
                    }
                    if (is_array($group_ids) && $bulk_action !== null) {
                        foreach ($group_ids as $group_id) {
                            switch ($bulk_action) {
                                case 'add-capability':
                                    $capabilities_id = isset($_POST['capability_id']) ? $_POST['capability_id'] : null;
                                    if ($capabilities_id !== null) {
                                        foreach ($capabilities_id as $capability_id) {
                                            Groups_Group_Capability::create(array('group_id' => $group_id, 'capability_id' => $capability_id));
                                        }
                                    }
                                    break;
                                case 'remove-capability':
                                    $capabilities_id = isset($_POST['capability_id']) ? $_POST['capability_id'] : null;
                                    if ($capabilities_id !== null) {
                                        foreach ($capabilities_id as $capability_id) {
                                            Groups_Group_Capability::delete($group_id, $capability_id);
                                        }
                                    }
                                    break;
                                case 'remove-group':
                                    $bulk_confirm = isset($_POST['confirm']) ? true : false;
                                    if ($bulk_confirm) {
                                        groups_admin_groups_bulk_remove_submit();
                                    } else {
                                        return groups_admin_groups_bulk_remove();
                                    }
                                    break;
                            }
                        }
                    }
                }
                break;
        }
    } else {
        if (isset($_GET['action'])) {
            // handle action request - show form
            switch ($_GET['action']) {
                case 'add':
                    return groups_admin_groups_add();
                    break;
                case 'edit':
                    if (isset($_GET['group_id'])) {
                        return groups_admin_groups_edit($_GET['group_id']);
                    }
                    break;
                case 'remove':
                    if (isset($_GET['group_id'])) {
                        return groups_admin_groups_remove($_GET['group_id']);
                    }
                    break;
            }
        }
    }
    //
    // group table
    //
    if (isset($_POST['clear_filters']) || isset($_POST['group_id']) || isset($_POST['group_name'])) {
        if (!wp_verify_nonce($_POST[GROUPS_ADMIN_GROUPS_FILTER_NONCE], 'admin')) {
            wp_die(__('Access denied.', GROUPS_PLUGIN_DOMAIN));
        }
    }
    // filters
    $group_id = Groups_Options::get_user_option('groups_group_id', null);
    $group_name = Groups_Options::get_user_option('groups_group_name', null);
    if (isset($_POST['clear_filters'])) {
        Groups_Options::delete_user_option('groups_group_id');
        Groups_Options::delete_user_option('groups_group_name');
        $group_id = null;
        $group_name = null;
    } else {
        if (isset($_POST['submitted'])) {
            // filter by name
            if (!empty($_POST['group_name'])) {
                $group_name = $_POST['group_name'];
                Groups_Options::update_user_option('groups_group_name', $group_name);
            }
            // filter by group id
            if (!empty($_POST['group_id'])) {
                $group_id = intval($_POST['group_id']);
                Groups_Options::update_user_option('groups_group_id', $group_id);
            } else {
                if (isset($_POST['group_id'])) {
                    // empty && isset => '' => all
                    $group_id = null;
                    Groups_Options::delete_user_option('groups_group_id');
                }
            }
        }
    }
    if (isset($_POST['row_count'])) {
        if (!wp_verify_nonce($_POST[GROUPS_ADMIN_GROUPS_NONCE_1], 'admin')) {
            wp_die(__('Access denied.', GROUPS_PLUGIN_DOMAIN));
        }
    }
    if (isset($_POST['paged'])) {
        if (!wp_verify_nonce($_POST[GROUPS_ADMIN_GROUPS_NONCE_2], 'admin')) {
            wp_die(__('Access denied.', GROUPS_PLUGIN_DOMAIN));
        }
    }
    $current_url = (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $current_url = remove_query_arg('paged', $current_url);
    $current_url = remove_query_arg('action', $current_url);
    $current_url = remove_query_arg('group_id', $current_url);
    $group_table = _groups_get_tablename('group');
    $output .= '<div class="manage-groups">' . '<div>' . '<h2>' . _x('Groups', 'page-title', GROUPS_PLUGIN_DOMAIN) . '</h2>' . '</div>';
    $output .= Groups_Admin::render_messages();
    $output .= '<div class="manage">' . "<a title='" . __('Click to add a new group', GROUPS_PLUGIN_DOMAIN) . "' class='add button' 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 Group', GROUPS_PLUGIN_DOMAIN) . "</span></a>" . '</div>';
    $row_count = isset($_POST['row_count']) ? intval($_POST['row_count']) : 0;
    if ($row_count <= 0) {
        $row_count = Groups_Options::get_user_option('groups_per_page', GROUPS_GROUPS_PER_PAGE);
    } else {
        Groups_Options::update_user_option('groups_per_page', $row_count);
    }
    $offset = isset($_GET['offset']) ? intval($_GET['offset']) : 0;
    if ($offset < 0) {
        $offset = 0;
    }
    $paged = isset($_REQUEST['paged']) ? intval($_REQUEST['paged']) : 0;
    if ($paged < 0) {
        $paged = 0;
    }
    $orderby = isset($_GET['orderby']) ? $_GET['orderby'] : null;
    switch ($orderby) {
        case 'group_id':
        case 'name':
            break;
        default:
            $orderby = 'name';
    }
    $order = isset($_GET['order']) ? $_GET['order'] : null;
    switch ($order) {
        case 'asc':
        case 'ASC':
            $switch_order = 'DESC';
            break;
        case 'desc':
        case 'DESC':
            $switch_order = 'ASC';
            break;
        default:
            $order = 'ASC';
            $switch_order = 'DESC';
    }
    $filters = array(" 1=%d ");
    $filter_params = array(1);
    if ($group_id) {
        $filters[] = " {$group_table}.group_id = %d ";
        $filter_params[] = $group_id;
    }
    if ($group_name) {
        $filters[] = " {$group_table}.name LIKE '%%%s%%' ";
        $filter_params[] = $group_name;
    }
    if (!empty($filters)) {
        $filters = " WHERE " . implode(" AND ", $filters);
    } else {
        $filters = '';
    }
    $count_query = $wpdb->prepare("SELECT COUNT(*) FROM {$group_table} {$filters}", $filter_params);
    $count = $wpdb->get_var($count_query);
    if ($count > $row_count) {
        $paginate = true;
    } else {
        $paginate = false;
    }
    $pages = ceil($count / $row_count);
    if ($paged > $pages) {
        $paged = $pages;
    }
    if ($paged != 0) {
        $offset = ($paged - 1) * $row_count;
    }
    $query = $wpdb->prepare("SELECT * FROM {$group_table}\n\t\t{$filters}\n\t\tORDER BY {$orderby} {$order}\n\t\tLIMIT {$row_count} OFFSET {$offset}", $filter_params);
    $results = $wpdb->get_results($query, OBJECT);
    $column_display_names = array('group_id' => __('Id', GROUPS_PLUGIN_DOMAIN), 'name' => __('Group', GROUPS_PLUGIN_DOMAIN), 'description' => __('Description', GROUPS_PLUGIN_DOMAIN), 'capabilities' => __('Capabilities', GROUPS_PLUGIN_DOMAIN), 'edit' => __('Edit', GROUPS_PLUGIN_DOMAIN), 'remove' => __('Remove', GROUPS_PLUGIN_DOMAIN));
    $output .= '<div class="groups-overview">';
    $output .= '<div class="filters">' . '<label class="description" for="setfilters">' . __('Filters', GROUPS_PLUGIN_DOMAIN) . '</label>' . '<form id="setfilters" action="" method="post">' . '<p>' . '<label class="group-id-filter" for="group_id">' . __('Group Id', GROUPS_PLUGIN_DOMAIN) . '</label>' . '<input class="group-id-filter" name="group_id" type="text" value="' . esc_attr($group_id) . '"/>' . '<label class="group-name-filter" for="group_name">' . __('Group Name', GROUPS_PLUGIN_DOMAIN) . '</label>' . '<input class="group-name-filter" name="group_name" type="text" value="' . $group_name . '"/>' . '</p>' . '<p>' . wp_nonce_field('admin', GROUPS_ADMIN_GROUPS_FILTER_NONCE, true, false) . '<input class="button" type="submit" value="' . __('Apply', GROUPS_PLUGIN_DOMAIN) . '"/>' . '<input class="button" type="submit" name="clear_filters" value="' . __('Clear', GROUPS_PLUGIN_DOMAIN) . '"/>' . '<input type="hidden" value="submitted" name="submitted"/>' . '</p>' . '</form>' . '</div>';
    if ($paginate) {
        require_once GROUPS_CORE_LIB . '/class-groups-pagination.php';
        $pagination = new Groups_Pagination($count, null, $row_count);
        $output .= '<form id="posts-filter" method="post" action="">';
        $output .= '<div>';
        $output .= wp_nonce_field('admin', GROUPS_ADMIN_GROUPS_NONCE_2, true, false);
        $output .= '</div>';
        $output .= '<div class="tablenav top">';
        $output .= $pagination->pagination('top');
        $output .= '</div>';
        $output .= '</form>';
    }
    $output .= '<div class="page-options right">';
    $output .= '<form id="setrowcount" action="" method="post">';
    $output .= '<div>';
    $output .= '<label for="row_count">' . __('Results per page', GROUPS_PLUGIN_DOMAIN) . '</label>';
    $output .= '<input name="row_count" type="text" size="2" value="' . esc_attr($row_count) . '" />';
    $output .= wp_nonce_field('admin', GROUPS_ADMIN_GROUPS_NONCE_1, true, false);
    $output .= '<input class="button" type="submit" value="' . __('Apply', GROUPS_PLUGIN_DOMAIN) . '"/>';
    $output .= '</div>';
    $output .= '</form>';
    $output .= '</div>';
    $capability_table = _groups_get_tablename("capability");
    $group_capability_table = _groups_get_tablename("group_capability");
    // capabilities select
    $capabilities = $wpdb->get_results("SELECT * FROM {$capability_table} ORDER BY capability");
    $capabilities_select = sprintf('<select class="select capability" name="capability_id[]" multiple="multiple" placeholder="%s" data-placeholder="%s">', esc_attr(__('Capabilities &hellip;', GROUPS_PLUGIN_DOMAIN)), esc_attr(__('Capabilities &hellip;', GROUPS_PLUGIN_DOMAIN)));
    foreach ($capabilities as $capability) {
        $capabilities_select .= sprintf('<option value="%s">%s</option>', esc_attr($capability->capability_id), wp_filter_nohtml_kses($capability->capability));
    }
    $capabilities_select .= '</select>';
    $capabilities_select .= Groups_UIE::render_select('.select.capability');
    $output .= '<form id="groups-action" method="post" action="">';
    $output .= '<div class="tablenav top">';
    $output .= '<div class="groups-bulk-container">';
    $output .= '<div class="capabilities-select-container">';
    $output .= $capabilities_select;
    $output .= wp_nonce_field('admin', GROUPS_ADMIN_GROUPS_ACTION_NONCE, true, false);
    $output .= '</div>';
    $output .= '<select class="bulk-action" name="bulk-action">';
    $output .= '<option selected="selected" value="-1">' . __('Bulk Actions', GROUPS_PLUGIN_DOMAIN) . '</option>';
    $output .= '<option value="remove-group">' . __('Remove group', GROUPS_PLUGIN_DOMAIN) . '</option>';
    $output .= '<option value="add-capability">' . __('Add capability', GROUPS_PLUGIN_DOMAIN) . '</option>';
    $output .= '<option value="remove-capability">' . __('Remove capability', GROUPS_PLUGIN_DOMAIN) . '</option>';
    $output .= '</select>';
    $output .= sprintf('<input class="button" type="submit" name="bulk" value="%s" />', __('Apply', GROUPS_PLUGIN_DOMAIN));
    $output .= '<input type="hidden" name="action" value="groups-action"/>';
    $output .= '</div>';
    $output .= '</div>';
    $output .= '<table id="" class="wp-list-table widefat fixed" cellspacing="0">';
    $output .= '<thead>';
    $output .= '<tr>';
    $output .= '<th id="cb" class="manage-column column-cb check-column" scope="col"><input type="checkbox"></th>';
    foreach ($column_display_names as $key => $column_display_name) {
        $options = array('orderby' => $key, 'order' => $switch_order);
        $class = $key;
        if (!in_array($key, array('capabilities', 'edit', 'remove'))) {
            if (strcmp($key, $orderby) == 0) {
                $lorder = strtolower($order);
                $class = "{$key} manage-column sorted {$lorder}";
            } else {
                $class = "{$key} manage-column sortable";
            }
            $column_display_name = '<a href="' . esc_url(add_query_arg($options, $current_url)) . '"><span>' . $column_display_name . '</span><span class="sorting-indicator"></span></a>';
        }
        $output .= "<th scope='col' class='{$class}'>{$column_display_name}</th>";
    }
    $output .= '</tr>';
    $output .= '</thead>';
    $output .= '<tbody>';
    if (count($results) > 0) {
        for ($i = 0; $i < count($results); $i++) {
            $result = $results[$i];
            $output .= '<tr class="' . ($i % 2 == 0 ? 'even' : 'odd') . '">';
            $output .= '<th class="check-column">';
            $output .= '<input type="checkbox" value="' . esc_attr($result->group_id) . '" name="group_ids[]"/>';
            $output .= '</th>';
            $output .= "<td class='group-id'>";
            $output .= $result->group_id;
            $output .= "</td>";
            $output .= "<td class='group-name'>" . stripslashes(wp_filter_nohtml_kses($result->name)) . "</td>";
            $output .= "<td class='group-description'>" . stripslashes(wp_filter_nohtml_kses($result->description)) . "</td>";
            $output .= '<td class="capabilities">';
            $group = new Groups_Group($result->group_id);
            $group_capabilities = $group->capabilities;
            $group_capabilities_deep = $group->capabilities_deep;
            usort($group_capabilities_deep, array('Groups_Utility', 'cmp'));
            if (count($group_capabilities_deep) > 0) {
                $output .= '<ul>';
                foreach ($group_capabilities_deep as $group_capability) {
                    $output .= '<li>';
                    $class = '';
                    if (empty($group_capabilities) || !in_array($group_capability, $group_capabilities)) {
                        $class = 'inherited';
                    }
                    $output .= sprintf('<span class="%s">', $class);
                    if (isset($group_capability->capability) && isset($group_capability->capability->capability)) {
                        $output .= wp_filter_nohtml_kses($group_capability->capability->capability);
                    }
                    $output .= '</span>';
                    $output .= '</li>';
                }
                $output .= '</ul>';
            } else {
                $output .= __('This group has no capabilities.', GROUPS_PLUGIN_DOMAIN);
            }
            $output .= '</td>';
            $output .= "<td class='edit'>";
            $output .= "<a href='" . esc_url(add_query_arg('paged', $paged, $current_url)) . "&action=edit&group_id=" . $result->group_id . "' alt='" . __('Edit', GROUPS_PLUGIN_DOMAIN) . "'><img src='" . GROUPS_PLUGIN_URL . "images/edit.png'/></a>";
            $output .= "</td>";
            $output .= "<td class='remove'>";
            if ($result->name !== Groups_Registered::REGISTERED_GROUP_NAME) {
                $output .= "<a href='" . esc_url($current_url) . "&action=remove&group_id=" . $result->group_id . "' alt='" . __('Remove', GROUPS_PLUGIN_DOMAIN) . "'><img src='" . GROUPS_PLUGIN_URL . "images/remove.png'/></a>";
            }
            $output .= "</td>";
            $output .= '</tr>';
        }
    } else {
        $output .= '<tr><td colspan="7">' . __('There are no results.', GROUPS_PLUGIN_DOMAIN) . '</td></tr>';
    }
    $output .= '</tbody>';
    $output .= '</table>';
    $output .= Groups_UIE::render_add_titles('.groups-overview table td');
    $output .= '</form>';
    // #groups-action
    if ($paginate) {
        require_once GROUPS_CORE_LIB . '/class-groups-pagination.php';
        $pagination = new Groups_Pagination($count, null, $row_count);
        $output .= '<div class="tablenav bottom">';
        $output .= $pagination->pagination('bottom');
        $output .= '</div>';
    }
    $output .= '</div>';
    // .groups-overview
    $output .= '</div>';
    // .manage-groups
    echo $output;
    Groups_Help::footer();
}
 /**
  * Editing a user profile.
  * @param WP_User $user
  */
 public static function edit_user_profile($user)
 {
     global $wpdb;
     if (current_user_can(GROUPS_ADMINISTER_GROUPS)) {
         $output = '<h3>' . __('Groups', GROUPS_PLUGIN_DOMAIN) . '</h3>';
         $user = new Groups_User($user->ID);
         $user_groups = $user->groups;
         $groups_table = _groups_get_tablename('group');
         if ($groups = $wpdb->get_results("SELECT * FROM {$groups_table} ORDER BY name")) {
             $output .= '<style type="text/css">';
             $output .= '.groups .selectize-input { font-size: inherit; }';
             $output .= '</style>';
             $output .= sprintf('<select id="user-groups" class="groups" name="group_ids[]" multiple="multiple" placeholder="%s" data-placeholder="%s">', esc_attr(__('Choose groups &hellip;', GROUPS_PLUGIN_DOMAIN)), esc_attr(__('Choose groups &hellip;', GROUPS_PLUGIN_DOMAIN)));
             foreach ($groups as $group) {
                 $is_member = Groups_User_Group::read($user->ID, $group->group_id) ? true : false;
                 $output .= sprintf('<option value="%d" %s>%s</option>', Groups_Utility::id($group->group_id), $is_member ? ' selected="selected" ' : '', wp_filter_nohtml_kses($group->name));
             }
             $output .= '</select>';
             $output .= Groups_UIE::render_select('#user-groups');
             $output .= '<p class="description">' . __('The user is a member of the chosen groups.', GROUPS_PLUGIN_DOMAIN) . '</p>';
         }
         echo $output;
     }
 }
 /**
  * Bulk-edit access restriction capabilities.
  * 
  * @param string $column_name
  * @param string $post_type
  */
 public static function bulk_edit_custom_box($column_name, $post_type)
 {
     global $pagenow, $wpdb;
     if ($column_name == 'capabilities') {
         if ($pagenow == 'edit.php') {
             // check that we're on the right screen
             $post_type = isset($_GET['post_type']) ? $_GET['post_type'] : 'post';
             $post_types_option = Groups_Options::get_option(Groups_Post_Access::POST_TYPES, array());
             if (!isset($post_types_option[$post_type]['add_meta_box']) || $post_types_option[$post_type]['add_meta_box']) {
                 $output = '<fieldset class="inline-edit-col-right">';
                 $output .= '<div class="bulk-edit-groups">';
                 // capability/access restriction bulk actions added through extra_tablenav()
                 $output .= '<div id="capability-bulk-actions" class="capabilities-bulk-container" style="display:inline">';
                 $output .= '<label style="display:inline;">';
                 $output .= '<span class="title">';
                 $output .= __('Access Restrictions', GROUPS_PLUGIN_DOMAIN);
                 $output .= '</span>';
                 $output .= '<select class="capabilities-action" name="capabilities-action">';
                 $output .= '<option selected="selected" value="-1">' . __('&mdash; No Change &mdash;', GROUPS_PLUGIN_DOMAIN) . '</option>';
                 $output .= '<option value="add-capability">' . __('Add restriction', GROUPS_PLUGIN_DOMAIN) . '</option>';
                 $output .= '<option value="remove-capability">' . __('Remove restriction', GROUPS_PLUGIN_DOMAIN) . '</option>';
                 $output .= '</select>';
                 $output .= '</label>';
                 $output .= '<div class="groups-capabilities-container">';
                 $valid_read_caps = Groups_Access_Meta_Boxes::get_valid_read_caps_for_user();
                 $output .= sprintf('<select class="select bulk-capability" name="%s[]" multiple="multiple" placeholder="%s" data-placeholder="%s">', esc_attr(Groups_Post_Access::POSTMETA_PREFIX . 'bulk-' . Groups_Post_Access::READ_POST_CAPABILITY), esc_attr(__('Choose access restrictions &hellip;', GROUPS_PLUGIN_DOMAIN)), esc_attr(__('Choose access restrictions &hellip;', GROUPS_PLUGIN_DOMAIN)));
                 foreach ($valid_read_caps as $capability) {
                     $output .= sprintf('<option value="%s" >%s</option>', esc_attr($capability), wp_filter_nohtml_kses($capability));
                 }
                 $output .= '</select>';
                 $output .= '</div>';
                 // .groups-capabilities-container
                 $output .= Groups_UIE::render_select('.select.bulk-capability');
                 $output .= '</div>';
                 // .capabilities-bulk-container
                 $output .= '</div>';
                 // .bulk-edit-groups
                 $output .= '</fieldset>';
                 // .inline-edit-col-right
                 $output .= wp_nonce_field('post-capability', 'bulk-post-capability-nonce', true, false);
                 echo $output;
             }
         }
     }
 }