get_results(
"SELECT user_id, count(1) as \"comment_count\" FROM $wpdb->comments WHERE $approved GROUP BY user_id;"
// 20140722 this will only get emails of folks who registered users and ignores whether they changed email address. and only gets logged in comments
// if want general activity by all emails, not just unregistered, consider an add-on
// "SELECT user_id, comment_author_email, count(1) as \"comment_count\" FROM $wpdb->comments WHERE $approved AND user_id > 0 GROUP BY user_id, comment_author_email;"
);
foreach ($comments as $i => $v) {
$c[$v->user_id] = $v->comment_count;
}
unset ($comments);
return $c;
}
}
if (!function_exists('amr_wp_list_format_cell')) {
function amr_wp_list_format_cell($column_name, $text, $user_info) {
if ($column_name == 'user_url') {
$text = ''.$text.'';
}
return $text;
}
}
if (!function_exists('ausers_format_ip_address')) {
function ausers_format_ip_address ($ip) {
if (stristr($ip, '127.'))
return ($ip);
else
return(''.$ip.'');
}
}
if (!function_exists('amr_format_interval')) {
function amr_format_interval($interval, $lowest_time='s') { // rounds up
// interval tests not working unless i var_dump the interval - wtf?
// setting a variable rather than checking directly seems to work
//y for years, m months, d days, h hours, i minutes, s seconds.
$text = array();
if (empty($interval)) {
return false;
}
$y = $interval->y;
if (!empty($y)) {
$text[] = $interval->format('%y '._n('year','years',$y,'amr-users'));
}
$m = $interval->m;
if (!empty($m)) {
$text[] = $interval->format('%m '._n('month','months',$m,'amr-users'));
}
if ((empty($y)) and (empty($m))) { //need more detail
$d = $interval->d;
if (!empty($d)) {
$text[] = $interval->format('%d '._n('day','days',$d,'amr-users'));
}
if (empty($d)) {
if (!($lowest_time === 'd')) { // must at least have d
//var_dump($interval); if I dump the interval I get the detail - why?
$x = $interval->h;
if (!empty($x)) {
$text[] = $interval->format('%h '._n('hour','hours',$x,'amr-users'));
}
if (!($lowest_time == 'h')) { // must be minutes, add them on?
$x = $interval->i;
if (!empty($x)) { //must set variable, not check the object somehow is better
//20170111
$text[] = $interval->format('%i '._n('minute','minutes',$x,'amr-users'));
}
// don't do seconds for now... really
}
}
}
}
if (empty($text)) { // then it practically just happened - now
$text[] = $interval->format('%i '._n('minute','minutes',$interval->i,'amr-users'));
//$text[] = $interval->format('%s '._n('second','seconds',$interval->i,'amr-users'));
}
$textstring = implode(_x(',','Separator in for example years, months, days','amr-users'),$text);
$textstring .= ' '._x('ago','how many days, minutes etc ago and event occured', 'amr-users');
return ($textstring);
}
}
if (!function_exists('amr_human_time_diff')) { // gives a rough feeling of how long with details in the tooltip
function amr_human_time_diff($earlier, $later='', $lowest_time='s') { // uses date time objects
//lowest time options are:
//y for years, m months, d days, h hours, i minutes, s seconds.
if (empty($earlier)) {
return false;
}
if (!is_object($earlier)) {
if (is_numeric($earlier)) {
$earlier = amru_newDateTime('@'.$earlier);
}
else {
$earlier = amru_newDateTime($earlier);
}
}
if (empty($later)) {
$later = amru_newDateTime(); // what is default timezone?
}
if ( method_exists($earlier, 'diff') ) { // if not php 5.3 or above
$interval = $earlier->diff($later);
//var_dump($interval); echo '
';
$text = amr_format_interval($interval, $lowest_time);
}
else {
$text = human_time_diff($earlier->format('U'));
}
$tip = $earlier->format('l, j M Y h:i a e' );
return (ausers_tooltip($text,$tip ));
}
}
if (!function_exists('ausers_format_ausers_last_login')) {
function ausers_format_ausers_last_login($v, $u) {
if (!empty($v))
return (substr($v, 0, 16)); //2011-05-30-11:03:02 EST Australia/Sydney
else return ('');
}
}
if (!function_exists('ausers_tooltip')) { // create a tooltip span
function ausers_tooltip( $text, $tip) {
return(''.$text.'');
}
}
if (!function_exists('ausers_format_user_registered')) { //because I want to see days ago
function ausers_format_user_registered($v, $u) {
$dt = amru_newDateTime($v);
$text = amr_human_time_diff($dt);
return($text);
}
}
if (!function_exists('ausers_format_user_registration_date')) { // this one mimics the user_registered for folks who want to see the actual date, not the days ago
function ausers_format_user_registration_date($v, $u) { // but not localised
global $tzobj;
$dt = new datetime($u->user_registered);
$dt->setTimeZone($tzobj);
$text = $dt->format('Y-m-d h:i:s');
return($text);
}
}
if (!function_exists('ausers_format_datestring')) { //hmmm should this be a special format option? and standardised then
function ausers_format_datestring($v) { // Y-m-d H:i:s .. old may change
if (empty($v)) return ('');
$ts = strtotime($v);
if ($ts < 0) return $v;
$now = strtotime(current_time('mysql'));
if ($ts < $now)
$htd = sprintf( _x('%s ago', 'indicate how long ago something happened','amr-users'), human_time_diff($ts, $now));
else {
$htd = sprintf( _x('in %s time', 'indicate how long ago something happened','amr-users'), human_time_diff($ts, $now));
}
return (
''
.$htd
.'');
}
}
if (!function_exists('ausers_format_timestamp')) {
function ausers_format_timestamp($v) {
if (empty($v)) return ('');
$d = date('Y-m-d H:i:s', (int) $v) ;
if (!$d) $d = $v;
return (
''
.sprintf( _x('%s ago', 'indicate how long ago something happened','amr-users'),
human_time_diff($v, current_time('timestamp')))
.'');
}
}
if (!function_exists('ausers_format_timestamp_as_date')) { //nlr? but is anything using? check first
function ausers_format_timestamp_as_date($v) {
// in the right timezone
global $tzobj ;
if (empty($v)) return ('');
if (empty($tzobj))
$tzobj = amr_getset_timezone ();
// $d = date('Y-m-d H:i:s e', (int) $v) ;
if (is_numeric($v)) {
$dt = new datetime('@'.$v);
$dt->setTimeZone($tzobj);
/* optional if you want to use your sites formats
$date_format = get_option('date_format'); //wp preloads these
$time_format = get_option('time_format');
*/
}
else
return($v); // we got something that is definitely not a timestamp
if (!is_object($dt))
$d = $v ; //if we got bad data - show it - show something anyway
else {
$date_format = 'Y-m-d';
$time_format = 'H:i'; //'H:i:s'
$tz = 'P'; //e or T or P
$d = $dt->format($date_format.' '.$time_format.' '.$tz);
}
return ($d);
}
}
if (!function_exists('amr_get_href_link')) {
function amr_get_href_link ($field, $v, $u, $linktype) {
switch ($linktype) {
case 'none': return '';
case 'mailto': {
if (!empty($u->user_email)) return ('mailto:'.$u->user_email);
else return '';
}
case 'postsbyauthor': { // figure out which post type ?
if (empty($v) or !current_user_can('edit_others_posts')) return( '');
else {
$href = network_admin_url('edit.php?author='.$u->ID);
if (stristr($field, '_count')) { // it is a item count thing, but not a post count
if (is_object($u) and isset ($u->ID) ) {
$ctype = str_replace('_count', '', $field);
$href=add_query_arg(array( // safe - its a network admin url
'post_type'=>$ctype
),
$href
);
} // end if
} // end if stristr
return ($href);
}
return '';
}
case 'edituser': {
if (current_user_can('edit_users') and is_object($u) and isset ($u->ID) ) {
if (is_network_admin())
return ( network_admin_url('user-edit.php?user_id='.$u->ID));
else
return ( admin_url('user-edit.php?user_id='.$u->ID));
}
//return ( network_admin_url('user-edit.php?user_id='.$u->ID));
else return '';
}
case 'authorarchive': { // should do on a post count only else may not be an author template ?
if (is_object($u) and isset ($u->ID) ) {
//return(add_query_arg('author', $u->ID, home_url())); // 201401
return(get_author_posts_url( $u->ID ));
}
else return '';
}
case 'commentsbyauthor': { //20140722
if (empty($v))
return('');
else
return (add_query_arg('s',$u->user_email, admin_url('edit-comments.php'))); //safe its admin url
}
case 'url': {
if (!empty($u->user_url)) return($u->user_url);
}
case 'wplist': { // for multisite
if (current_user_can('edit_users') and is_object($u) and isset ($u->user_login) )
return(network_admin_url('users.php?s='.$u->user_login));
}
case 'bbpressprofile' : {
if (function_exists ('bbp_get_user_profile_url'))
return (bbp_get_user_profile_url($u->ID));
/* 20130702 - use bbpress function instead, it allows filters etc, so who knows what might be there
$slug = get_option('_bbp_user_slug');
$forums = get_option('_bbp_root_slug');
return (home_url('/'
.__( $forums ,'bbpress')
.'/'
.__( $slug, 'bbpress')
.'/'.$u->user_login
));*/
else return '';
}
default:
return(apply_filters('amr-users-linktype-function',
$linktype, // the current value
$u,
$field)); // all the user values
}
}
}
if (!function_exists('auser_multisort')) { // an update attempt // if works well in testing then move to pluggables
function auser_multisort($arraytosort, $cols) { // $ cols has $col (eg: first name) the $order eg: ASC or DESC
if (empty($arraytosort))
return (false);
if (empty($cols))
return $arraytosort;
$cols['ID'] = SORT_ASC; // just in case, lets have this as a fallback
/* Example: $arr2 = array_msort($arr1, array('name'=>array(SORT_DESC,SORT_REGULAR), 'cat'=>SORT_ASC));*/
$colarr = array();
foreach ($cols as $col => $order) {
$colarr[$col] = array(); // eg $colarr[firstname]
foreach ($arraytosort as $k => $row) {
if (!isset($row[$col]))
$colarr[$col]['_'.$k] = '';
else
$colarr[$col]['_'.$k] = strtolower($row[$col]); // to make case insenstice ?
}
}
foreach ($cols as $col => $order) {
$dimensionarr[] = $colarr[$col];
$orderarr[] = $order;
}
if (count($dimensionarr) < 2)
array_multisort($dimensionarr[0], $orderarr[0],
$arraytosort);
elseif (count($dimensionarr) == 2)
array_multisort($dimensionarr[0], $orderarr[0],
$dimensionarr[1], $orderarr[1],
$arraytosort);
elseif (count($dimensionarr) == 3)
array_multisort($dimensionarr[0], $orderarr[0],
$dimensionarr[1], $orderarr[1],
$dimensionarr[2], $orderarr[2],
$arraytosort);
elseif (count($dimensionarr) == 4)
array_multisort($dimensionarr[0], $orderarr[0],
$dimensionarr[1], $orderarr[1],
$dimensionarr[2], $orderarr[2],
$dimensionarr[3], $orderarr[3],
$arraytosort);
else
array_multisort($dimensionarr[0], $orderarr[0],
$dimensionarr[1], $orderarr[1],
$dimensionarr[2], $orderarr[2],
$dimensionarr[3], $orderarr[3],
$dimensionarr[4], $orderarr[4],
$arraytosort);
return($arraytosort);
}
}
if (!function_exists('ausers_format_phone')) {
function ausers_format_phone($v) {
$v2 = str_replace('-','',$v);
$v2 = str_replace(' ','',$v2);
return (''.$v.'');
}
}
if (!function_exists('ausers_format_avatar')) {
function ausers_format_avatar($v, $u) {
global $amain,$amr_current_list;
if (!isset($amain['list_avatar_size'][$amr_current_list])) {
if (!isset($amain['avatar_size']))
$avatar_size = 16;
else
$avatar_size = $amain['avatar_size'];
}
else $avatar_size = $amain['list_avatar_size'][$amr_current_list];
if (!empty($u->user_email))
return (get_avatar( $u->user_email, $avatar_size ));
else return ('');
}
}
if (!function_exists('ausers_format_usersettingstime')) { // why 2 similar - is one old or bbpress ?
function ausers_format_usersettingstime($v, $u) {
return(ausers_format_timestamp($v));
}
}
if (!function_exists('ausers_format_user_settings_time')) { // why 2 similar
function ausers_format_user_settings_time($v, $u) {
return(ausers_format_timestamp($v));
}
}
if (!function_exists('amr_format_user_cell')) {
function amr_format_user_cell($i, $v, $u, $line='') { // thefield, the value, the user object
global $aopt, $amr_current_list, $amr_your_prefixes;
/* receive the key and the value and format accordingly - wordpress has a similar user function function - should we use that? */
$title = '';
$href = '';
$text = '';
// have they asked for a link?
if (isset ($aopt['list'][$amr_current_list]['links'][$i]) ) {
$lt = $aopt['list'][$amr_current_list]['links'][$i];
$href= amr_get_href_link($i, $v, $u, $lt );
if (!empty($href)) {
switch ($lt) { // depending on link type
case 'mailto': $title = __('Email the user','amr-users');
break;
case 'edituser': $title = __('Edit the user','amr-users');
break;
case 'authorarchive': $title = __('Go to author archive','amr-users');
break;
case 'url': $title = __('Go to users website','amr-users');
break;
case 'postsbyauthor': $title = __('View posts in admin','amr-users');
break;
case 'commentsbyauthor': $title = __('See comments by user','amr-users');
break;
case 'wplist': $title = __('Go to wp userlist filtered by user ','amr-users');
break;
default: $title = '';
}//end switch
}
}
/*else { // old one for compatibility with saved options that do not have the link types - NO else will forc even if we do not wnat any
switch ($i) { //href types
case 'user_login': {
if (is_object($u) and isset ($u->ID) ) {
$href= site_url().'/wp-admin/user-edit.php?user_id='.$u->ID;
}
break;
}
case 'post_count': {
if (empty($v)) return( ' ');
else if (is_object($u) and isset ($u->ID) ) {
$href=add_query_arg('author',$u->ID, site_url()); // safe - it is site url
}
break;
}
case 'user_url': {
$href=$v;
break;
}
case 'comment_count': {
if (empty($v))
$href='';
else
$href=add_query_arg('s',$u->user_email, admin_url('edit-comments.php')); // safe
break;
}
default: { $href= '';
}
}//end switch
} */ //end else
// do we have a configured format ? use that before trying other options 20170113
$text = amr_format_field($i, $v, $u); // returns false if could not do
if (!$text) { // then we do not have a configured format, try for function or other logic
// now get the value if special formatting required
$generic_i = str_replace('-','_',$i); // cannot have function with dashes, so any special function must use underscores
// strip all prefixes out, will obviously only be one actaully there, but we may have a shared user db, so may have > 1
foreach ($amr_your_prefixes as $ip=> $tp) {
$generic_i = str_replace($tp, '',$generic_i );
}
if (function_exists('ausers_format_'.$generic_i) ) {
$text = (call_user_func('ausers_format_'.$generic_i, $v, $u, $line));
}
else { //if (WP_DEBUG) echo '
didnt find custom function: ausers_format_'.$generic_i;
switch ($i) {
case 'description': {
$text = (nl2br($v)); break;
}
default: {
if (is_array($v)) {
$text = implode(', ',$v);
}
else $text = $v;
}
} // end switch
}
}
if (!empty($text)) {
if (!empty($href)) {
if (!empty ($title)) {
$title = ' title="'.$title.'"';
}
$text = ''.$text.'';
}
}
else {
//$text = ' '; // else tables with borders dont look right
$text = ''; // remove this ? folks can add back in with a filter ?
}
/* unfortunately - due to fields being in columns and columns being what is cached,
the before/after formatting is done before cacheing - not ideal, should rather be in final format
if (!empty($text)) {
if (!empty($l['before'][$i]))
$text = html_entity_decode($l['before'][$i]).$text;
if (!empty($l['after'][$i]))
$text = $text.html_entity_decode($l['after'][$i]);
}
*/
$text = apply_filters('amr_users_format_value', $text, $i, $v, $u, $line); // to allow for other unusual circumstances eg ym
return(($text));
}
}
if (!function_exists('amr_do_cell')) {
function amr_do_cell($i, $k, $openbracket,$closebracket) {
return ($openbracket.$i.$closebracket);
}
}
//------------------------------------------------------ just one user
if (!function_exists('amr_display_a_line')) {
function amr_display_a_line ($line, $icols, $cols, $user, $ahtm) {
global $amr_odd_even;
if (empty($amr_odd_even)) {
$amr_odd_even = 'odd';
}
elseif ($amr_odd_even == 'even') {
$amr_odd_even = 'odd';
}
else {
$amr_odd_even = 'even';
}
$linehtml = '';
foreach ($icols as $ic => $c) {
$w = amr_format_user_cell($c, $line[$c], $user, $line);// add line in case need those values
if (($c == 'checkbox') )
$linehtml .= $ahtm['th'].' class="manage-column column-cb check-column th">'.$w. $ahtm['thc'];
else
$linehtml .= $ahtm['td'].' class="'.$c.' td td'.$ic.' ">'.$w. $ahtm['tdc'];
// no esc_attr here - messes up hyperlinks
}
$cssclass = apply_filters('amr-users-css-class-row','vcard '.$amr_odd_even, $line, $user); // allow styling by the values
$html = $ahtm['tr'].' class="'.$cssclass.'">'.$linehtml.$ahtm['trc'];
return ($html);
}
}
//------------------------------------------------------
if (!function_exists('amr_add_data_in_line_to_user_object')) {
function amr_add_data_in_line_to_user_object($line, $user) {
if (!is_object($user)) {
if (current_user_can('Administrator') and WP_DEBUG) { // 2014 10 - is this always a data bug?
echo '
Message to admin only Error in data or possible bug - user is not a user object: see user dump and line dump';
var_dump($user);
var_dump($line);
}
}
else {
foreach ($line as $field=> $d) { // if we have data, add it to the user object in case we need it elsewhere on the line.
if ( empty($user->$field) and (!empty($d)))
$user->$field = $d;
}
}
return $user;
}
}
//------------------------------------------------------ just the lines on this page
if (!function_exists('amr_display_a_page')) {
function amr_display_a_page ($linessaved, $icols, $cols, $ahtm ) {
$html = '';
foreach ($linessaved as $il =>$line) { /// have index at this point
$id = $line['ID']; /* always have the id - may not always print it */
$user = amr_get_userdata($id);
$user = amr_add_data_in_line_to_user_object ($line,$user); // in case we wnt to use it
// need this data for links, etc
// hmmm not sure about this, what if want other values in line?
// how to accomodate both line data and maybe other user data ?
$html.= amr_display_a_line ($line, $icols, $cols, $user, $ahtm);
}
return ($html);
}
}
//------------------------------------------------------ now prepare for listing
if (!function_exists('amr_display_final_list')) {
function amr_display_final_list (
$linessaved, $icols, $cols,
$page, $rowsperpage, $totalitems,
$caption,
$search, $ulist, $c,
$filtercol,
$sortedbynow,
$options = array()) {
global $aopt,
$amain,
$amrusers_fieldfiltering,
$amr_current_list,
$amr_search_result_count,
$ahtm; // the display html structure to use
global $amr_refreshed_heading;
$amr_current_list = $ulist;
$html = $hhtml = $fhtml = '';
$filterhtml = '';
$filterhtml_separate = '';
$apply_filter_html = '';
$filter_submit_html = '';
$summary = '';
$explain_filter = '';
$adminoptions = array ( // forced defaults for admin
'show_search' => true,
'show_perpage' => true,
'show_pagination' => true,
'show_headings' => true,
'show_csv' => true,
'show_refresh' => true,
'show_totals' => false, // have other total line
);
if (!is_admin()
//and !empty($amain['public'][$ulist])
) { // set public options to overrwite admin
foreach ($adminoptions as $i => $opt) {
if (isset ( $options[$i]))
$adminoptions[$i] = $options[$i];
else
$adminoptions[$i] = '';
}
}
//if (WP_DEBUG) var_dump($adminoptions);
if (!empty($_REQUEST['headings'])) //or // ie in admin doing headings***
//(!empty($_REQUEST['filtering'])))
{
$adminoptions['show_search'] = false;
$adminoptions['show_csv'] = false;
$adminoptions['show_refresh'] = false;
$adminoptions['show_perpage'] = false;
$adminoptions['show_headings'] = false;
$adminoptions['show_totals'] = false;
$adminoptions['show_pagination'] = true;
$amain['filter_html_type'][$amr_current_list] = 'none';// if editingheadings, then no showingfilter
}
if ( ( is_admin() OR (!isset($amain['html_type'][$amr_current_list])) )) {
// must be after the check above, so will force table in admin
$ahtm = amr_get_html_to_use ('table');
}
else {
$ahtm = amr_get_html_to_use ($amain['html_type'][$amr_current_list]);
}
if (empty($linessaved))
$saveditems = 0;
else
$saveditems = count($linessaved);
if (is_array($caption))
$caption = '