/**
 * Handle add capability form submission.
 * @return int new capability's id or false if unsuccessful
 */
function groups_admin_capabilities_add_submit()
{
    global $wpdb;
    if (!current_user_can(GROUPS_ADMINISTER_GROUPS)) {
        wp_die(__('Access denied.', GROUPS_PLUGIN_DOMAIN));
    }
    if (!wp_verify_nonce($_POST[GROUPS_ADMIN_GROUPS_NONCE], 'capabilities-add')) {
        wp_die(__('Access denied.', GROUPS_PLUGIN_DOMAIN));
    }
    $capability = isset($_POST['capability-field']) ? $_POST['capability-field'] : null;
    $description = isset($_POST['description-field']) ? $_POST['description-field'] : '';
    $capability_id = Groups_Capability::create(compact("capability", "description"));
    if (!$capability_id) {
        if (empty($capability)) {
            Groups_Admin::add_message(__('The <em>Capability</em> must not be empty.', GROUPS_PLUGIN_DOMAIN), 'error');
        } else {
            if (Groups_Capability::read_by_capability($capability)) {
                Groups_Admin::add_message(sprintf(__('The <em>%s</em> capability already exists.', GROUPS_PLUGIN_DOMAIN), stripslashes(wp_filter_nohtml_kses($capability))), 'error');
            }
        }
    }
    return $capability_id;
}
/**
 * Handle add group form submission.
 * @return int new group's id or false if unsuccessful
 */
function groups_admin_groups_add_submit()
{
    global $wpdb;
    if (!current_user_can(GROUPS_ADMINISTER_GROUPS)) {
        wp_die(__('Access denied.', GROUPS_PLUGIN_DOMAIN));
    }
    if (!wp_verify_nonce($_POST[GROUPS_ADMIN_GROUPS_NONCE], 'groups-add')) {
        wp_die(__('Access denied.', GROUPS_PLUGIN_DOMAIN));
    }
    $creator_id = get_current_user_id();
    $datetime = date('Y-m-d H:i:s', time());
    $parent_id = isset($_POST['parent-id-field']) ? $_POST['parent-id-field'] : null;
    $description = isset($_POST['description-field']) ? $_POST['description-field'] : '';
    $name = isset($_POST['name-field']) ? $_POST['name-field'] : null;
    $group_id = Groups_Group::create(compact("creator_id", "datetime", "parent_id", "description", "name"));
    if ($group_id) {
        if (!empty($_POST['capability_ids'])) {
            $caps = $_POST['capability_ids'];
            foreach ($caps as $cap) {
                Groups_Group_Capability::create(array('group_id' => $group_id, 'capability_id' => $cap));
            }
        }
        do_action('groups_admin_groups_add_submit_success', $group_id);
    } else {
        if (!$name) {
            Groups_Admin::add_message(__('The name must not be empty.', GROUPS_PLUGIN_DOMAIN), 'error');
        } else {
            if (Groups_Group::read_by_name($name)) {
                Groups_Admin::add_message(sprintf(__('The <em>%s</em> group already exists.', GROUPS_PLUGIN_DOMAIN), stripslashes(wp_filter_nohtml_kses($name))), 'error');
            }
        }
    }
    return $group_id;
}
/**
 * Handle edit form submission.
 */
function groups_admin_capabilities_edit_submit()
{
    $result = false;
    if (!current_user_can(GROUPS_ADMINISTER_GROUPS)) {
        wp_die(__('Access denied.', GROUPS_PLUGIN_DOMAIN));
    }
    if (!wp_verify_nonce($_POST[GROUPS_ADMIN_GROUPS_NONCE], 'capabilities-edit')) {
        wp_die(__('Access denied.', GROUPS_PLUGIN_DOMAIN));
    }
    $capability_id = isset($_POST['capability-id-field']) ? $_POST['capability-id-field'] : null;
    $capability = Groups_Capability::read($capability_id);
    if ($capability) {
        $capability_id = $capability->capability_id;
        if ($capability->capability !== Groups_Post_Access::READ_POST_CAPABILITY) {
            $capability_field = isset($_POST['capability-field']) ? $_POST['capability-field'] : null;
        } else {
            $capability_field = Groups_Post_Access::READ_POST_CAPABILITY;
        }
        if (!empty($capability_field)) {
            $update = true;
            if ($other_capability = Groups_Capability::read_by_capability($capability_field)) {
                if ($other_capability->capability_id != $capability_id) {
                    Groups_Admin::add_message(sprintf(__('The <em>%s</em> capability already exists and cannot be assigned to this one.', GROUPS_PLUGIN_DOMAIN), stripslashes(wp_filter_nohtml_kses($other_capability->capability))), 'error');
                    $update = false;
                }
            }
            if ($update) {
                $description = isset($_POST['description-field']) ? $_POST['description-field'] : '';
                $capability_id = Groups_Capability::update(array('capability_id' => $capability_id, 'capability' => $capability_field, 'description' => $description));
                if ($capability_id) {
                    $result = $capability_id;
                } else {
                    Groups_Admin::add_message(sprintf(__('The <em>%s</em> capability could not be updated.', GROUPS_PLUGIN_DOMAIN), stripslashes(wp_filter_nohtml_kses($capability))), 'error');
                }
            }
        } else {
            Groups_Admin::add_message(__('The <em>Capability</em> must not be empty.', GROUPS_PLUGIN_DOMAIN), 'error');
        }
    }
    return $result;
}
/**
 * Handle edit form submission.
 */
function groups_admin_groups_edit_submit()
{
    global $wpdb;
    if (!current_user_can(GROUPS_ADMINISTER_GROUPS)) {
        wp_die(__('Access denied.', GROUPS_PLUGIN_DOMAIN));
    }
    if (!wp_verify_nonce($_POST[GROUPS_ADMIN_GROUPS_NONCE], 'groups-edit')) {
        wp_die(__('Access denied.', GROUPS_PLUGIN_DOMAIN));
    }
    $group_id = isset($_POST['group-id-field']) ? $_POST['group-id-field'] : null;
    $group = Groups_Group::read($group_id);
    if ($group) {
        $group_id = $group->group_id;
        if ($group->name !== Groups_Registered::REGISTERED_GROUP_NAME) {
            $name = isset($_POST['name-field']) ? $_POST['name-field'] : null;
        } else {
            $name = Groups_Registered::REGISTERED_GROUP_NAME;
        }
        $parent_id = isset($_POST['parent-id-field']) ? $_POST['parent-id-field'] : null;
        $description = isset($_POST['description-field']) ? $_POST['description-field'] : '';
        if (empty($name)) {
            Groups_Admin::add_message(__('The <em>Name</em> must not be empty.', GROUPS_PLUGIN_DOMAIN), 'error');
            return false;
        }
        if ($other_group = Groups_Group::read_by_name($name)) {
            if ($other_group->group_id != $group_id) {
                Groups_Admin::add_message(sprintf(__('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))), 'error');
                return false;
            }
        }
        $group_id = Groups_Group::update(compact("group_id", "name", "parent_id", "description"));
        if ($group_id) {
            $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();
            foreach ($group_capabilities as $group_capability) {
                $group_capabilities_array[] = $group_capability->capability_id;
            }
            $caps = array();
            if (isset($_POST['capability_ids'])) {
                $caps = $_POST['capability_ids'];
            }
            // delete
            foreach ($group_capabilities_array as $group_cap) {
                if (!in_array($group_cap, $caps)) {
                    Groups_Group_Capability::delete($group_id, $group_cap);
                }
            }
            // add
            foreach ($caps as $cap) {
                if (!in_array($cap, $group_capabilities_array)) {
                    Groups_Group_Capability::create(array('group_id' => $group_id, 'capability_id' => $cap));
                }
            }
        }
        return $group_id;
    } else {
        return false;
    }
}
/**
 * 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();
}
/**
 * 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();
}
/**
 * Manage capabilities: table of capabilities and add, edit, remove actions.
 */
function groups_admin_capabilities()
{
    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 (!($capability_id = groups_admin_capabilities_add_submit())) {
                    return groups_admin_capabilities_add();
                } else {
                    $capability = Groups_Capability::read($capability_id);
                    Groups_Admin::add_message(sprintf(__('The <em>%s</em> capability has been created.', GROUPS_PLUGIN_DOMAIN), stripslashes(wp_filter_nohtml_kses($capability->capability))));
                }
                break;
            case 'edit':
                if (!($capability_id = groups_admin_capabilities_edit_submit())) {
                    return groups_admin_capabilities_edit($_POST['capability-id-field']);
                } else {
                    $capability = Groups_Capability::read($capability_id);
                    Groups_Admin::add_message(sprintf(__('The <em>%s</em> capability has been updated.', GROUPS_PLUGIN_DOMAIN), stripslashes(wp_filter_nohtml_kses($capability->capability))));
                }
                break;
            case 'remove':
                if ($capability_id = groups_admin_capabilities_remove_submit()) {
                    Groups_Admin::add_message(__('The capability has been deleted.', GROUPS_PLUGIN_DOMAIN));
                }
                break;
                // bulk actions on groups: capabilities
            // bulk actions on groups: capabilities
            case 'groups-action':
                if (wp_verify_nonce($_POST[GROUPS_ADMIN_GROUPS_ACTION_NONCE], 'admin')) {
                    $capability_ids = isset($_POST['capability_ids']) ? $_POST['capability_ids'] : null;
                    $bulk = isset($_POST['bulk']) ? $_POST['bulk'] : null;
                    if (is_array($capability_ids) && $bulk !== null) {
                        foreach ($capability_ids as $capability_id) {
                            $bulk_action = isset($_POST['bulk-action']) ? $_POST['bulk-action'] : null;
                            switch ($bulk_action) {
                                case 'remove':
                                    if (isset($_POST['confirm'])) {
                                        groups_admin_capabilities_bulk_remove_submit();
                                    } else {
                                        return groups_admin_capabilities_bulk_remove();
                                    }
                                    break;
                            }
                            break;
                        }
                    }
                }
                break;
        }
    } else {
        if (isset($_GET['action'])) {
            // handle action request - show form
            switch ($_GET['action']) {
                case 'add':
                    return groups_admin_capabilities_add();
                    break;
                case 'edit':
                    if (isset($_GET['capability_id'])) {
                        return groups_admin_capabilities_edit($_GET['capability_id']);
                    }
                    break;
                case 'remove':
                    if (isset($_GET['capability_id'])) {
                        return groups_admin_capabilities_remove($_GET['capability_id']);
                    }
                    break;
                case 'refresh':
                    if (check_admin_referer('refresh')) {
                        $n = Groups_WordPress::refresh_capabilities();
                        if ($n > 0) {
                            $output .= '<div class="updated fade"><p>' . sprintf(_n('One capability has been added.', '%d capabilities have been added.', $n, GROUPS_PLUGIN_DOMAIN), $n) . '</p></div>';
                        } else {
                            $output .= '<div class="updated fade"><p>' . __('No new capabilities have been found.', GROUPS_PLUGIN_DOMAIN) . '</p></div>';
                        }
                    } else {
                        wp_die(__('A Duck!', GROUPS_PLUGIN_DOMAIN));
                    }
                    break;
            }
        }
    }
    //
    // capabilities table
    //
    if (isset($_POST['clear_filters']) || isset($_POST['capability_id']) || isset($_POST['capability'])) {
        if (!wp_verify_nonce($_POST[GROUPS_ADMIN_CAPABILITIES_FILTER_NONCE], 'admin')) {
            wp_die(__('Access denied.', GROUPS_PLUGIN_DOMAIN));
        }
    }
    // filters
    $capability_id = Groups_Options::get_user_option('capabilities_capability_id', null);
    $capability = Groups_Options::get_user_option('capabilities_capability', null);
    if (isset($_POST['clear_filters'])) {
        Groups_Options::delete_user_option('capabilities_capability_id');
        Groups_Options::delete_user_option('capabilities_capability');
        $capability_id = null;
        $capability = null;
    } else {
        if (isset($_POST['submitted'])) {
            // filter by name
            if (!empty($_POST['capability'])) {
                $capability = $_POST['capability'];
                Groups_Options::update_user_option('capabilities_capability', $capability);
            }
            // filter by capability id
            if (!empty($_POST['capability_id'])) {
                $capability_id = intval($_POST['capability_id']);
                Groups_Options::update_user_option('capabilities_capability_id', $capability_id);
            } else {
                if (isset($_POST['capability_id'])) {
                    // empty && isset => '' => all
                    $capability_id = null;
                    Groups_Options::delete_user_option('capabilities_capability_id');
                }
            }
        }
    }
    if (isset($_POST['row_count'])) {
        if (!wp_verify_nonce($_POST[GROUPS_ADMIN_CAPABILITIES_NONCE_1], 'admin')) {
            wp_die(__('Access denied.', GROUPS_PLUGIN_DOMAIN));
        }
    }
    if (isset($_POST['paged'])) {
        if (!wp_verify_nonce($_POST[GROUPS_ADMIN_CAPABILITIES_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('capability_id', $current_url);
    $capability_table = _groups_get_tablename('capability');
    $output .= '<div class="manage-capabilities wrap">' . '<h1>' . __('Capabilities', GROUPS_PLUGIN_DOMAIN) . sprintf('<a title="%s" class="add page-title-action" href="%s">', esc_attr(__('Click to add a new capability', GROUPS_PLUGIN_DOMAIN)), esc_url($current_url . '&action=add')) . sprintf('<img class="icon" alt="%s" src="%s" />', esc_attr(__('Add', GROUPS_PLUGIN_DOMAIN)), esc_url(GROUPS_PLUGIN_URL . 'images/add.png')) . sprintf('<span class="label">%s</span>', stripslashes(wp_filter_nohtml_kses(__('New Capability', GROUPS_PLUGIN_DOMAIN)))) . '</a>' . sprintf('<a title="%s" class="refresh page-title-action" href="%s">', esc_attr(__('Click to refresh capabilities', GROUPS_PLUGIN_DOMAIN)), esc_url(wp_nonce_url($current_url . '&action=refresh', 'refresh'))) . sprintf('<img class="icon" alt="%s" src="%s" />', esc_attr(__('Refresh', GROUPS_PLUGIN_DOMAIN)), esc_url(GROUPS_PLUGIN_URL . 'images/refresh.png')) . sprintf('<span class="label">%s</span>', stripslashes(wp_filter_nohtml_kses(__('Refresh', GROUPS_PLUGIN_DOMAIN)))) . '</a>' . '</h1>';
    $output .= Groups_Admin::render_messages();
    $row_count = isset($_POST['row_count']) ? intval($_POST['row_count']) : 0;
    if ($row_count <= 0) {
        $row_count = Groups_Options::get_user_option('capabilities_per_page', GROUPS_CAPABILITIES_PER_PAGE);
    } else {
        Groups_Options::update_user_option('capabilities_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 'capability_id':
        case 'capability':
        case 'description':
            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 ($capability_id) {
        $filters[] = " {$capability_table}.capability_id = %d ";
        $filter_params[] = $capability_id;
    }
    if ($capability) {
        $filters[] = " {$capability_table}.capability LIKE '%%%s%%' ";
        $filter_params[] = $capability;
    }
    if (!empty($filters)) {
        $filters = " WHERE " . implode(" AND ", $filters);
    } else {
        $filters = '';
    }
    $count_query = $wpdb->prepare("SELECT COUNT(*) FROM {$capability_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 {$capability_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('capability_id' => __('ID', GROUPS_PLUGIN_DOMAIN), 'capability' => __('Capability', GROUPS_PLUGIN_DOMAIN), 'description' => __('Description', GROUPS_PLUGIN_DOMAIN));
    $output .= '<div class="capabilities-overview">';
    $output .= '<div class="filters">' . '<form id="setfilters" action="" method="post">' . '<fieldset>' . '<legend>' . __('Filters', GROUPS_PLUGIN_DOMAIN) . '</legend>' . '<label class="capability-id-filter">' . __('Capability ID', GROUPS_PLUGIN_DOMAIN) . ' ' . '<input class="capability-id-filter" name="capability_id" type="text" value="' . esc_attr($capability_id) . '"/>' . '</label>' . ' ' . '<label class="capability-filter">' . __('Capability', GROUPS_PLUGIN_DOMAIN) . ' ' . '<input class="capability-filter" name="capability" type="text" value="' . $capability . '"/>' . '</label>' . ' ' . wp_nonce_field('admin', GROUPS_ADMIN_CAPABILITIES_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"/>' . '</fieldset>' . '</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_CAPABILITIES_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_CAPABILITIES_NONCE_1, true, false);
    $output .= '<input class="button" type="submit" value="' . __('Apply', GROUPS_PLUGIN_DOMAIN) . '"/>';
    $output .= '</div>';
    $output .= '</form>';
    $output .= '</div>';
    $output .= '<form id="groups-action" method="post" action="">';
    $output .= '<div class="tablenav top">';
    $output .= '<div class="capabilities-bulk-container">';
    $output .= '<div class="alignleft actions">';
    $output .= '<select name="bulk-action">';
    $output .= '<option selected="selected" value="-1">' . esc_html(__('Bulk Actions', GROUPS_PLUGIN_DOMAIN)) . '</option>';
    $output .= '<option value="remove">' . esc_html(__('Remove', GROUPS_PLUGIN_DOMAIN)) . '</option>';
    $output .= '</select>';
    $output .= '<input class="button" type="submit" name="bulk" value="' . esc_attr(__("Apply", GROUPS_PLUGIN_DOMAIN)) . '"/>';
    $output .= '</div>';
    $output .= '</div>';
    $output .= '</div>';
    $output .= wp_nonce_field('admin', GROUPS_ADMIN_GROUPS_ACTION_NONCE, true, false);
    $output .= '<input type="hidden" name="action" value="groups-action"/>';
    $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 = sprintf('<a href="%s"><span>%s</span><span class="sorting-indicator"></span></a>', esc_url(add_query_arg($options, $current_url)), esc_html($column_display_name));
        } else {
            $column_display_name = esc_html($column_display_name);
        }
        $output .= sprintf('<th scope="col" class="%s">%s</th>', esc_attr($class), $column_display_name);
    }
    $output .= '</tr>';
    $output .= '</thead>';
    $output .= '<tbody>';
    if (count($results) > 0) {
        for ($i = 0; $i < count($results); $i++) {
            $result = $results[$i];
            // Construct the "edit" URL.
            $edit_url = add_query_arg(array('capability_id' => intval($result->capability_id), 'action' => 'edit', 'paged' => $paged), $current_url);
            // Construct the "delete" URL.
            $delete_url = add_query_arg(array('capability_id' => intval($result->capability_id), 'action' => 'remove', 'paged' => $paged), $current_url);
            // Construct row actions for this group.
            $row_actions = '<div class="row-actions">' . '<span class="edit">' . '<a href="' . esc_url($edit_url) . '">' . '<img src="' . GROUPS_PLUGIN_URL . 'images/edit.png"/>' . __('Edit', GROUPS_PLUGIN_DOMAIN) . '</a>';
            if ($result->capability !== Groups_Post_Access::READ_POST_CAPABILITY) {
                $row_actions .= ' | ' . '</span>' . '<span class="remove trash">' . '<a href="' . esc_url($delete_url) . '" class="submitdelete">' . '<img src="' . GROUPS_PLUGIN_URL . 'images/remove.png"/>' . __('Remove', GROUPS_PLUGIN_DOMAIN) . '</a>' . '</span>';
            }
            $row_actions .= '</div>';
            // .row-actions
            $output .= '<tr class="' . ($i % 2 == 0 ? 'even' : 'odd') . '">';
            $output .= '<th class="check-column">';
            $output .= '<input type="checkbox" value="' . esc_attr($result->capability_id) . '" name="capability_ids[]"/>';
            $output .= '</th>';
            $output .= '<td class="capability-id">';
            $output .= $result->capability_id;
            $output .= '</td>';
            $output .= '<td class="capability">';
            $output .= sprintf('<a href="%s">%s</a>', esc_url($edit_url), stripslashes(wp_filter_nohtml_kses($result->capability)));
            $output .= $row_actions;
            $output .= '</td>';
            $output .= '<td class="description">';
            $output .= stripslashes(wp_filter_nohtml_kses($result->description));
            $output .= '</td>';
            $output .= '</tr>';
        }
    } else {
        $output .= '<tr><td colspan="3">' . __('There are no results.', GROUPS_PLUGIN_DOMAIN) . '</td></tr>';
    }
    $output .= '</tbody>';
    $output .= '</table>';
    $output .= Groups_UIE::render_add_titles('.capabilities-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>';
    // .capabilities-overview
    $output .= '</div>';
    // .manage-capabilities
    echo $output;
}