Ejemplo n.º 1
0
function amr_get_alluserdata($list)
{
    /*  get all user data and attempt to extract out any object values into arrays for listing  */
    global $excluded_nicenames, $amain, $aopt, $orig_mk, $amr_current_list;
    $amr_current_list = $list;
    $main_fields = amr_get_usermasterfields();
    // mainwpuser fields less any excluded in nice names
    // 	maybe use, but no major improvement for normal usage add_filter( 'pre_user_query', 'amr_add_where');
    if (!($orig_mk = ausers_get_option('amr-users-original-keys'))) {
        $orig_mk = array();
    }
    //
    //	track_progress ('Meta fields we could use to improve selection: '.print_r($orig_mk, true));
    $combofields = amr_get_combo_fields($list);
    $role = '';
    $mkeys = array();
    if (!empty($aopt['list'][$list]['included'])) {
        // if we have fields that are in main user table, we could add - but unliket as selection criteria - more in search
        foreach ($aopt['list'][$list]['included'] as $newk => $choose) {
            if (isset($orig_mk[$newk])) {
                $keys[$orig_mk[$newk]] = true;
            }
            if ($newk == 'first_role') {
                if (is_array($choose)) {
                    $role = array_pop($choose);
                } else {
                    $role = $choose;
                }
            }
            if (isset($orig_mk[$newk]) and $newk == $orig_mk[$newk]) {
                // ie it is an original meta field
                if (is_array($choose)) {
                    if (count($choose) == 1) {
                        $choose = array_pop($choose);
                        $compare = '=';
                    } else {
                        $compare = 'IN';
                    }
                } else {
                    $compare = '=';
                }
                $meta_query[] = array('key' => $newk, 'value' => $choose, 'compare' => $compare);
            }
        }
    }
    // now try for exclusions
    if (!empty($aopt['list'][$list]['excluded'])) {
        foreach ($aopt['list'][$list]['excluded'] as $newk => $choose) {
            if (isset($orig_mk[$newk])) {
                $keys[$orig_mk[$newk]] = true;
                // we need to fetch a meta value
                if ($newk == $orig_mk[$newk]) {
                    // ie it is an original meta field 1 to 1
                    if (is_array($choose)) {
                        if (count($choose) == 1) {
                            $choose = array_pop($choose);
                            $compare = '!=';
                        } else {
                            $compare = 'NOT IN';
                        }
                    } else {
                        $compare = '!=';
                    }
                    $meta_query[] = array('key' => $newk, 'value' => $choose, 'compare' => $compare);
                }
            }
        }
        // end for each
    }
    // now need to make sure we find all the meta keys we need
    foreach (array('selected', 'excludeifblank', 'includeonlyifblank', 'sortby') as $v) {
        if (!empty($aopt['list'][$list][$v])) {
            foreach ($aopt['list'][$list][$v] as $newk => $choose) {
                if (isset($orig_mk[$newk])) {
                    // ie it is FROM an original meta field
                    $keys[$orig_mk[$newk]] = true;
                }
            }
        }
    }
    if (!empty($aopt['list'][$list]['grouping'])) {
        foreach ($aopt['list'][$list]['grouping'] as $i => $newk) {
            if (isset($orig_mk[$newk])) {
                // ie it is FROM an original meta field
                $keys[$orig_mk[$newk]] = true;
            }
        }
    }
    $args = array();
    $users = array();
    // to handle in weird situation of no users - eg if db corrupt!
    if (!empty($role)) {
        $args['role'] = $role;
    }
    if (!empty($meta_query)) {
        $args['meta_query'] = $meta_query;
    }
    //if (!empty ($fields) ) $args['fields'] = $fields;
    //$args['fields'] = 'all_with_meta'; //might be too huge , but fast - DOES NOT GET META DATA ?? and/or only gets single values
    //track_progress ('Simple meta selections to pass to query: '.print_r($args, true));
    if (is_network_admin() or amr_is_network_admin()) {
        //if (WP_DEBUG) {echo '<br/>';if (is_network_admin()) echo 'network admin'; else echo 'NOT network admin but treating as is';}
        $args['blog_id'] = '0';
    }
    if (isset($amain['use_wp_query'])) {
        //hmm always doing this
        $all = get_users($args);
        // later - add selection if possible here to reduce memory requirements
        //if (WP_DEBUG) {echo '<br/>Fetched with wordpress query.  No. of records found: <b>'.count($all).'</b><br /> using args: '; var_dump($args); }
    } else {
        //if (WP_DEBUG) echo '<br/>if WP_DEBUG: Fetching with own query ';
        $all = amru_get_users($args);
        // later - add selection if possible here to reduce memory requirements
        //if (WP_DEBUG) {echo '<br/>Fetched with own query.  No. of records found: <b>'.count($all).'</b><br /> using args: '; var_dump($args); }
    }
    //track_progress('after get wp users, we have '.count($all));
    foreach ($all as $i => $userobj) {
        // build our user array and add any missing meta
        // save the main data, toss the rest
        foreach ($main_fields as $i2 => $v2) {
            //$users[$i][$v2] = $userobj->$v2;
            if (!empty($userobj->{$v2})) {
                $users[$userobj->ID][$v2] = $userobj->{$v2};
            }
            //OBJECT_K does not always seem to key the array correctly
        }
        // -------------------------------------------------------------------
        // we just need to expand the meta data
        if (!empty($keys)) {
            // - the list of metadata keys.  If we have some meta data requested, and most of the time we will
            foreach ($keys as $i2 => $v2) {
                //if (!isset($userobj->$i2)) {  // in some versions the overloading does not work - only fetches 1
                //$userobj->$i2 = get_user_meta($userobj->ID, $i2, false);
                //wordpress does some kind of overloading to fetch meta data  BUT above only fetches single
                $test = get_user_meta($userobj->ID, $i2, false);
                // get as array in case there are multiple values
                if (!empty($test)) {
                    //if (WP_DEBUG) echo 'i2='.$i2;var_dump($test);
                    if (is_array($test)) {
                        // because we are now checking for multiple values so it returns an array
                        if (count($test) == 1) {
                            // one record, single value returned
                            $temp = current($test);
                            // there is only one - get it without taking it out of array
                            //$temp = array_pop($test);  // get that one record
                            //if (WP_DEBUG) {var_dump($temp);}
                            // oh dear next code broke those nasty complex s2membercustom fields
                            // but it's the way to deal with non associative arrays
                            if (is_array($temp)) {
                                // if that one record is an array - hope to hell that's the end of the nested arrays, but now it wont be
                                if (!amr_is_assoc($temp)) {
                                    // if it is a numeric keyed array, cannot handle as per associative array
                                    // ideally no spaces here BUT if there is no custom formatting routine to explode and re-implode, then folks complain about lack of space between.   NB Check impact on filter values.  (explode with spaces perhaps?)
                                    //$temp = implode (',',$temp);  // 20140305 space sinformatting only - not here
                                    $temp = implode(', ', $temp);
                                    // must be a list of values ? implode here or later?
                                    // or should we force it into a mulit meta array ?
                                }
                                // else	leave as is for further processing
                                //else var_dump($temp);
                            }
                            $userobj->{$i2} = $temp;
                            // save it as our value
                            //$userobj->$i2 = array_pop($test); // cannot indirectly update an overloaded value
                            //if (WP_DEBUG) {echo '<br />save obj: ';var_dump($userobj->$i2);}
                        } else {
                            // we got multple meta records - ASSUME for now it is a good implementation and the values are 'simple'
                            // otherwise they really should create their meta data a better way. Can't solve everyones problems.
                            $userobj->{$i2} = implode(', ', $test);
                        }
                    } else {
                        $userobj->{$i2} = $test;
                    }
                    $temp = maybe_unserialize($userobj->{$i2});
                    // in case anyone done anything weird
                    //gravity forms has weird serialised nested array - argghh
                    $temp = objectToArray($temp);
                    /* must do all so can cope with incomplete objects  eg: if the creating plugin has been uninstalled*/
                    $key = str_replace(' ', '_', $i2);
                    /* html does not like spaces in the names*/
                    if (is_array($temp)) {
                        if (count($temp) == 1) {
                            // one record, single value returned - will fix that annoying gravity form emergency contact thing
                            // oh dear but broke the single capability thing
                            if (!current($temp) == true and !current($temp) == '1') {
                                // ie not a capability thing
                                $temp = array_pop($temp);
                            }
                            // its a usable value and
                        }
                    }
                    if (is_array($temp)) {
                        // if it is still an array inside
                        //if (WP_DEBUG) {echo '<br/>Got an array'; var_dump($temp);}
                        foreach ($temp as $i3 => $v3) {
                            $key = $i2 . '-' . str_replace(' ', '_', $i3);
                            /* html does not like spaces in the names*/
                            //if (WP_DEBUG) {echo '<br/>Got an array - key'; var_dump($key);}
                            if (is_array($v3)) {
                                //if (WP_DEBUG) {echo '<br/>Got an nested array'; }
                                // code just in case another plugin nests deeper, until we know tehre is one, let us be more efficient
                                if (amr_is_assoc($v3)) {
                                    // does not yet handle, just dump values for now
                                    // really shouldn't be nested this deep associativey - bad
                                    $users[$i][$key] = implode(", ", $v3);
                                    //if (WP_DEBUG) {echo '<br/>Got associative array:'.$i2.' '.$i3; var_dump($users[$i][$key]);}
                                } else {
                                    // is numeric array eg s2member custom multi choice
                                    $users[$userobj->ID][$key] = implode(", ", $v3);
                                }
                            } else {
                                $users[$userobj->ID][$key] = $v3;
                            }
                        }
                    } else {
                        $users[$userobj->ID][$key] = $temp;
                        //if (WP_DEBUG) {echo '<br/>Not an array'; var_dump($temp);}
                    }
                    unset($temp);
                    // we could add some include / exclude checking here?
                    //if (WP_DEBUG) var_dump($users[$userobj->ID]);
                }
            }
            /// end for each keys
        }
        //
        unset($all[$i]);
    }
    // end for each all
    unset($all);
    $users = apply_filters('amr_get_users', $users);
    // allow addition or removal of normal wp users who will have userid, and /or any other data
    //track_progress('after get users meta check '.(count($users)));
    $post_types = get_post_types();
    /* get the extra count data */
    if (amr_need_the_field($list, 'comment_count')) {
        $c = get_commentnumbers_by_author();
    } else {
        $c = array();
    }
    //track_progress('after get comments check');
    if (!empty($users)) {
        foreach ($users as $iu => $u) {
            // do the comments
            if (isset($u['ID']) and isset($c[$u['ID']])) {
                $users[$iu]['comment_count'] = $c[$u['ID']]++;
                /*** would like to cope with situation of no userid, but awkward here */
            }
            // do the post counts
            foreach ($post_types as $post_type) {
                if (amr_need_the_field($list, $post_type . '_count')) {
                    $users[$iu][$post_type . '_count'] = amr_count_user_posts($u['ID'], $post_type);
                    //					if ()WP_DEBUG) echo '<br />**'.$post_type.' '.$list[$iu][$post_type.'_count'];
                    //					$list[$iu]['post_count'] = get_usernumposts($u['ID']); /* wordpress function */
                    if ($users[$iu][$post_type . '_count'] == 0) {
                        unset($users[$iu][$post_type . '_count']);
                    }
                }
            }
            if (amr_need_the_field($list, 'first_role')) {
                $user_object = new WP_User($u['ID']);
                if (!empty($user_object->roles)) {
                    $users[$iu]['first_role'] = amr_which_role($user_object);
                }
                if (empty($users[$iu]['first_role'])) {
                    unset($users[$iu]['first_role']);
                }
            }
        }
    }
    //track_progress('after post types and roles:'.count($users));
    unset($c);
    $users = apply_filters('amr_get_users_with_meta', $users);
    // allow addition of users from other tables with own meta data
    //track_progress('after user filter, have'.count($users));
    if (empty($users)) {
        return false;
    }
    return $users;
}
Ejemplo n.º 2
0
function amr_get_alluserdata($list)
{
    /*  get all user data and attempt to extract out any object values into arrays for listing  */
    global $excluded_nicenames, $amain, $aopt, $orig_mk, $amr_current_list;
    $amr_current_list = $list;
    $main_fields = amr_get_usermasterfields();
    // mainwpuser fields less any excluded in nice names
    // 	maybe use, but no major improvement for normal usage add_filter( 'pre_user_query', 'amr_add_where');
    if (!($orig_mk = ausers_get_option('amr-users-original-keys'))) {
        $orig_mk = array();
    }
    //
    //	track_progress ('Meta fields we could use to improve selection: '.print_r($orig_mk, true));
    $combofields = amr_get_combo_fields($list);
    $role = '';
    $mkeys = array();
    if (!empty($aopt['list'][$list]['included'])) {
        // if we have fields that are in main user table, we could add - but unliket as selection criateria - more in search
        foreach ($aopt['list'][$list]['included'] as $newk => $choose) {
            if (isset($orig_mk[$newk])) {
                $keys[$orig_mk[$newk]] = true;
            }
            if ($newk == 'first_role') {
                if (is_array($choose)) {
                    $role = array_pop($choose);
                } else {
                    $role = $choose;
                }
            }
            if (isset($orig_mk[$newk]) and $newk == $orig_mk[$newk]) {
                // ie it is an original meta field
                if (is_array($choose)) {
                    if (count($choose) == 1) {
                        $choose = array_pop($choose);
                        $compare = '=';
                    } else {
                        $compare = 'IN';
                    }
                } else {
                    $compare = '=';
                }
                $meta_query[] = array('key' => $newk, 'value' => $choose, 'compare' => $compare);
            }
        }
    }
    // now try for exclusions
    if (!empty($aopt['list'][$list]['excluded'])) {
        foreach ($aopt['list'][$list]['excluded'] as $newk => $choose) {
            if (isset($orig_mk[$newk])) {
                $keys[$orig_mk[$newk]] = true;
                // we need to fetch a meta value
                if ($newk == $orig_mk[$newk]) {
                    // ie it is an original meta field 1 to 1
                    if (is_array($choose)) {
                        if (count($choose) == 1) {
                            $choose = array_pop($choose);
                            $compare = '!=';
                        } else {
                            $compare = 'NOT IN';
                        }
                    } else {
                        $compare = '!=';
                    }
                    $meta_query[] = array('key' => $newk, 'value' => $choose, 'compare' => $compare);
                }
            }
        }
        // end for each
    }
    // now need to make sure we find all the meta keys we need
    foreach (array('selected', 'excludeifblank', 'includeifblank', 'sortby') as $v) {
        if (!empty($aopt['list'][$list][$v])) {
            foreach ($aopt['list'][$list][$v] as $newk => $choose) {
                if (isset($orig_mk[$newk])) {
                    // ie it is FROM an original meta field
                    $keys[$orig_mk[$newk]] = true;
                }
            }
        }
    }
    if (!empty($aopt['list'][$list]['grouping'])) {
        foreach ($aopt['list'][$list]['grouping'] as $i => $newk) {
            if (isset($orig_mk[$newk])) {
                // ie it is FROM an original meta field
                $keys[$orig_mk[$newk]] = true;
            }
        }
    }
    $args = array();
    if (!empty($role)) {
        $args['role'] = $role;
    }
    if (!empty($meta_query)) {
        $args['meta_query'] = $meta_query;
    }
    //if (!empty ($fields) ) $args['fields'] = $fields;
    //$args['fields'] = 'all_with_meta'; //might be too huge , but fast - DOES NOT GET META DATA ?? and/or only gets single values
    //track_progress ('Simple meta selections to pass to query: '.print_r($args, true));
    if (is_network_admin() or amr_is_network_admin()) {
        //if (WP_DEBUG) {echo '<br/>';if (is_network_admin()) echo 'network admin'; else echo 'NOT network admin but treating as is';}
        $args['blog_id'] = '0';
    }
    if (isset($amain['use_wp_query'])) {
        $all = get_users($args);
        // later - add selection if possible here to reduce memory requirements
        if (WP_DEBUG) {
            echo '<br/>Fetched with wordpress query ';
        }
    } else {
        if (WP_DEBUG) {
            echo '<br/>if WP_DEBUG: Fetching with own query ';
        }
        $all = amru_get_users($args);
        // later - add selection if possible here to reduce memory requirements
    }
    //track_progress('after get wp users, we have '.count($all));
    foreach ($all as $i => $userobj) {
        // build our user array and add any missing meta
        // save the main data, toss the rest
        foreach ($main_fields as $i2 => $v2) {
            //$users[$i][$v2] = $userobj->$v2;
            if (!empty($userobj->{$v2})) {
                $users[$userobj->ID][$v2] = $userobj->{$v2};
            }
            //OBJECT_K does not always seem to key the array correctly
        }
        // we just need to expand the meta data
        if (!empty($keys)) {
            // if some meta request
            foreach ($keys as $i2 => $v2) {
                //if (WP_DEBUG) {echo '<br /> Key:'.$i2;}
                //if (!isset($userobj->$i2)) {  // in some versions the overloading does not work - only fetches 1
                //$userobj->$i2 = get_user_meta($userobj->ID, $i2, false);
                //wordpress does some kind of overloading to fetch meta data  BUT above only fetches single
                //				$userobj->$i2  = get_user_meta($userobj->ID, $i2, false); // get as array in case there are multiple values
                $test = get_user_meta($userobj->ID, $i2, false);
                // get as array in case there are multiple values
                if (!empty($test)) {
                    if (is_array($test)) {
                        if (count($test) == 1) {
                            // single value returned
                            $userobj->{$i2} = array_pop($test);
                            // cannot indirectly update an overloaded value
                            //if (WP_DEBUG) {echo '<br /> convert array to 1 value '.$i2.'  ='; var_dump($userobj->$i2);}
                        } else {
                            // we got multple meta records - ASSUME for now it is a good implementation and the values are 'simple'
                            $userobj->{$i2} = implode(',', $test);
                            //if (WP_DEBUG) {echo '<br /> convert array to strings for display '.$i2.'  ='; var_dump($userobj->$i2);}
                        }
                    } else {
                        $userobj->{$i2} = $test;
                    }
                    $temp = maybe_unserialize($userobj->{$i2});
                    // in case anyone done anything weird
                    $temp = objectToArray($temp);
                    /* must do all so can cope with incomplete objects  eg: if creatingplugin has been uninstalled*/
                    $key = str_replace(' ', '_', $i2);
                    /* html does not like spaces in the names*/
                    if (is_array($temp)) {
                        //if (WP_DEBUG) echo '<br/>It is an array now - maybe was object';
                        foreach ($temp as $i3 => $v3) {
                            $key = $i2 . '-' . str_replace(' ', '_', $i3);
                            /* html does not like spaces in the names*/
                            if (is_array($v3)) {
                                // code just in case another plugin nests deeper, until we know tehre is one, let us be more efficient
                                //								if (amr_is_assoc($v3)) { // does not yet handle, just dump values for now
                                //									$users[$i][$key] = implode(", ", $v3);
                                //								}
                                //								else { // is numeric array eg s2member custom multi choice
                                $users[$userobj->ID][$key] = implode(", ", $v3);
                                //								}
                            } else {
                                $users[$userobj->ID][$key] = $v3;
                            }
                        }
                    } else {
                        $users[$userobj->ID][$key] = $temp;
                        //if (WP_DEBUG) {echo '<br/>Not an array'; var_dump($temp);}
                    }
                    unset($temp);
                    // we could add some include / exclude checking here?
                }
            }
            /// end for each keys
        }
        //
        unset($all[$i]);
    }
    // end for each all
    unset($all);
    $users = apply_filters('amr_get_users', $users);
    // allow addition or removal of normal wp users who will have userid, and /or any other data
    //track_progress('after get users meta check '.(count($users)));
    $post_types = get_post_types();
    /* get the extra count data */
    if (amr_need_the_field($list, 'comment_count')) {
        $c = get_commentnumbers_by_author();
    } else {
        $c = array();
    }
    //track_progress('after get comments check');
    if (!empty($users)) {
        foreach ($users as $iu => $u) {
            // do the comments
            //if (WP_DEBUG) {echo '<br />user='******'ID']])) {
                $users[$iu]['comment_count'] = $c[$u['ID']];
                /*** would like to cope with situation of no userid */
            }
            // do the post counts
            foreach ($post_types as $post_type) {
                if (amr_need_the_field($list, $post_type . '_count')) {
                    $users[$iu][$post_type . '_count'] = amr_count_user_posts($u['ID'], $post_type);
                    //					if ()WP_DEBUG) echo '<br />**'.$post_type.' '.$list[$iu][$post_type.'_count'];
                    //					$list[$iu]['post_count'] = get_usernumposts($u['ID']); /* wordpress function */
                    if ($users[$iu][$post_type . '_count'] == 0) {
                        unset($users[$iu][$post_type . '_count']);
                    }
                }
            }
            if (amr_need_the_field($list, 'first_role')) {
                $user_object = new WP_User($u['ID']);
                if (!empty($user_object->roles)) {
                    $users[$iu]['first_role'] = amr_which_role($user_object);
                }
                if (empty($users[$iu]['first_role'])) {
                    unset($users[$iu]['first_role']);
                }
            }
        }
    }
    //track_progress('after post types and roles:'.count($users));
    unset($c);
    $users = apply_filters('amr_get_users_with_meta', $users);
    // allow addition of users from other tables with own meta data
    //track_progress('after user filter, have'.count($users));
    if (empty($users)) {
        return false;
    }
    return $users;
}