Ejemplo n.º 1
0
function acf_get_grouped_users($args = array())
{
    // vars
    $r = array();
    // defaults
    $args = wp_parse_args($args, array('users_per_page' => -1, 'paged' => 0, 'role' => '', 'orderby' => 'login', 'order' => 'ASC'));
    // offset
    $i = 0;
    $min = 0;
    $max = 0;
    $users_per_page = acf_extract_var($args, 'users_per_page');
    $paged = acf_extract_var($args, 'paged');
    if ($users_per_page > 0) {
        // prevent paged from being -1
        $paged = max(0, $paged);
        // set min / max
        $min = ($paged - 1) * $users_per_page + 1;
        // 	1, 	11
        $max = $paged * $users_per_page;
        // 			10,	20
    }
    // find array of post_type
    $user_roles = acf_get_pretty_user_roles($args['role']);
    // fix role
    if (is_array($args['role'])) {
        // global
        global $wp_version, $wpdb;
        // vars
        $roles = acf_extract_var($args, 'role');
        // new WP has role__in
        if (version_compare($wp_version, '4.4', '>=')) {
            $args['role__in'] = $roles;
            // old WP doesn't have role__in
        } else {
            // vars
            $blog_id = get_current_blog_id();
            $meta_query = array('relation' => 'OR');
            // loop
            foreach ($roles as $role) {
                $meta_query[] = array('key' => $wpdb->get_blog_prefix($blog_id) . 'capabilities', 'value' => '"' . $role . '"', 'compare' => 'LIKE');
            }
            // append
            $args['meta_query'] = $meta_query;
        }
    }
    // get posts
    $users = get_users($args);
    // loop
    foreach ($user_roles as $user_role_name => $user_role_label) {
        // vars
        $this_users = array();
        $this_group = array();
        // populate $this_posts
        foreach (array_keys($users) as $key) {
            // bail ealry if not correct role
            if (!in_array($user_role_name, $users[$key]->roles)) {
                continue;
            }
            // extract user
            $user = acf_extract_var($users, $key);
            // increase
            $i++;
            // bail ealry if too low
            if ($min && $i < $min) {
                continue;
            }
            // bail early if too high (don't bother looking at any more users)
            if ($max && $i > $max) {
                break;
            }
            // group by post type
            $this_users[$user->ID] = $user;
        }
        // bail early if no posts for this post type
        if (empty($this_users)) {
            continue;
        }
        // append
        $r[$user_role_label] = $this_users;
    }
    // return
    return $r;
}
Ejemplo n.º 2
0
 function render_field_settings($field)
 {
     acf_render_field_setting($field, array('label' => __('Filter by role', 'acf'), 'instructions' => '', 'type' => 'select', 'name' => 'role', 'choices' => acf_get_pretty_user_roles(), 'multiple' => 1, 'ui' => 1, 'allow_null' => 1, 'placeholder' => __("All user roles", 'acf')));
     // allow_null
     acf_render_field_setting($field, array('label' => __('Allow Null?', 'acf'), 'instructions' => '', 'type' => 'radio', 'name' => 'allow_null', 'choices' => array(1 => __("Yes", 'acf'), 0 => __("No", 'acf')), 'layout' => 'horizontal'));
     // multiple
     acf_render_field_setting($field, array('label' => __('Select multiple values?', 'acf'), 'instructions' => '', 'type' => 'radio', 'name' => 'multiple', 'choices' => array(1 => __("Yes", 'acf'), 0 => __("No", 'acf')), 'layout' => 'horizontal'));
 }