Set user status to "Away" in Wordpress

0

Recently I asked a question here on Stack: #, with the help of the user #

But another question arose as to how to add the status of "Away"?

The function (functions.php) below was the solution that Heathcliff suggested and worked:

// Atualiza o Status de Atividade do usuário
add_action('init', 'riverlab_users_status_init');
add_action('admin_init', 'riverlab_users_status_init');
function riverlab_users_status_init(){
$logged_in_users = get_transient('users_status'); // Captura as atividades dos usuários pelos transients do wordpress
$user = wp_get_current_user(); // Captura dos dados do usuário atual
// Atualiza o usuário se ele não estiver na lista, ou se ele não estiver online durantes os ultimos 3 minutos (180 segundos)
if ( !isset($logged_in_users[$user->ID]['last']) || $logged_in_users[$user->ID]['last'] <= time()-180 ){
$logged_in_users[$user->ID] = array(
'id' => $user->ID,
'username' => $user->user_login,
'last' => time(),
);
set_transient('users_status', $logged_in_users, 180); // Setar para que expire de 3 em 3 minutos (180 segundos)
}
}
// Checar se há alguém online nos ultimos 3 minutos
function riverlab_is_user_online($id){
$logged_in_users = get_transient('users_status');

return isset($logged_in_users[$id]['last']) && $logged_in_users[$id]['last'] > time()-180;
}
// Checa a ultima vez que alguém esteve online
function riverlab_user_last_online($id){
$logged_in_users = get_transient('users_status');
if ( isset($logged_in_users[$id]['last']) ){
return $logged_in_users[$id]['last'];
} else {
return false;
}
}
?>

Add in single.php:

<?php $id = get_the_author_meta( 'ID' ); // isso deve estar dentro de um post (single.php por exemplo)
if ( riverlab_is_user_online($id) ) {
        echo '<span class="tag-status tag-status-online"></span>';
    } else {
        echo '<span class="tag-status tag-status-offline"></span>';
    }
?>

By analyzing the give function to get an idea of how to solve the question, I do not know if it would work but what if the function did two checks.

Let's say that the function first checks in the first 3 minutes (180 seconds) and detects that the user is offline / inactive, until then the function would interpret that the user is just missing.

When the function performs a second check and it is detected that the user is inactive for more than 6 minutes (360 seconds), the user will automatically go from offline to offline status.

I do not PHP handle so I have no idea how to do this: (

    
asked by anonymous 17.08.2018 / 23:47

0 answers