web-dev-qa-db-ja.com

現在のユーザーではないユーザーがログインしているかどうかを確認する方法

各著者ページ(カスタム著者ページテンプレート)のオンラインステータス(オンライン/オフライン)を表示する必要があります。

is_user_logged_in()は現在のユーザーにのみ適用され、私は現在の著者をターゲットにした適切なアプローチを見つけることができません。 is_author_logged_in()

何か案は?

回答

One Trick Ponyは、トランジェントを使って2〜3つの関数のコーディングを準備するのに十分親切でした。

http://codex.wordpress.org/Transients_API

これをfunctions.phpに追加してください。

add_action('wp', 'update_online_users_status');
function update_online_users_status(){

  if(is_user_logged_in()){

    // get the online users list
    if(($logged_in_users = get_transient('users_online')) === false) $logged_in_users = array();

    $current_user = wp_get_current_user();
    $current_user = $current_user->ID;  
    $current_time = current_time('timestamp');

    if(!isset($logged_in_users[$current_user]) || ($logged_in_users[$current_user] < ($current_time - (15 * 60)))){
      $logged_in_users[$current_user] = $current_time;
      set_transient('users_online', $logged_in_users, 30 * 60);
    }

  }
}

これをauthor.php(または他のページテンプレート)に追加してください。

function is_user_online($user_id) {

  // get the online users list
  $logged_in_users = get_transient('users_online');

  // online, if (s)he is in the list and last activity was less than 15 minutes ago
  return isset($logged_in_users[$user_id]) && ($logged_in_users[$user_id] > (current_time('timestamp') - (15 * 60)));
}

$passthis_id = $curauth->ID;
if(is_user_online($passthis_id)){
echo 'User is online.';}
else {
echo'User is not online.';}

セカンドアンサー(使わないでください)

この回答は参考として含まれています。 One Trick Ponyが指摘したように、データベースはページがロードされるたびに更新されるため、これは望ましくないアプローチです。さらに精査した結果、コードは現在の作者に追加照合するのではなく、現在のユーザーのログインステータスを検出しているようにしか見えませんでした。

1)このプラグインをインストールしてください: http://wordpress.org/extend/plugins/who-is-online/

2)ページテンプレートに以下を追加します。

//Set the $curauth variable
if(isset($_GET['author_name'])) :
$curauth = get_userdatabylogin($author_name);
else :
$curauth = get_userdata(intval($author));
endif;

// Define the ID of whatever authors page is being viewed.
$authortemplate_id = $curauth->ID;

// Connect to database.
global $wpdb;
// Define table as variable.
$who_is_online_table = $wpdb->prefix . 'who_is_online';
// Query: Count the number of user_id's (plugin) that match the author id (author template page).
$onlinestatus_check = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM ".$who_is_online_table." WHERE user_id = '".$authortemplate_id."';" ) );

// If a match is found...
if ($onlinestatus_check == "1"){
echo "<p>User is <strong>online</strong> now!</p>";
}
else{
echo "<p>User is currently <strong>offline</strong>.</p>";
}
22
Dominor Novus

これを行うには transient を使用します。

  • initにフックするuser-online-update関数を作成してください。これは次のようになります。

    // get the user activity the list
    $logged_in_users = get_transient('online_status');
    
    // get current user ID
    $user = wp_get_current_user();
    
    // check if the current user needs to update his online status;
    // he does if he doesn't exist in the list
    $no_need_to_update = isset($logged_in_users[$user->ID])
    
        // and if his "last activity" was less than let's say ...15 minutes ago          
        && $logged_in_users[$user->ID] >  (time() - (15 * 60));
    
    // update the list if needed
    if(!$no_need_to_update){
      $logged_in_users[$user->ID] = time();
      set_transient('online_status', $logged_in_users, $expire_in = (30*60)); // 30 mins 
    }
    

    そのため、これは各ページロードで実行されますが、トランジェントは必要な場合にのみ更新されます。あなたがオンラインで多数のユーザーを持っているなら、あなたはdb書き込みを減らすために "最後の活動"時間枠を増やすことを望むかもしれません、しかし15分はほとんどのサイトのために十分以上です...

  • ユーザーがオンラインになっているかどうかを確認するには、上記のように、特定のユーザーがオンラインになっているかどうかを一時的に調べます。

    // get the user activity the list
    $logged_in_users = get_transient('online_status');
    
    // for eg. on author page
    $user_to_check = get_query_var('author'); 
    
    $online = isset($logged_in_users[$user_to_check])
       && ($logged_in_users[$user_to_check] >  (time() - (15 * 60)));
    

アクティビティがまったくない場合、トランジェントは30分で期限が切れます。ただし、有効期限が切れないようにしてオンラインのユーザーがいる場合は、 1日2回のイベント またはそのようなものに別の関数をフックして、一時的に一時的なものをクリーンアップします。この関数は古い$logged_in_usersエントリを削除します...

15
onetrickpony

私の知る限りでは、組み込みの WordPress関数を使用してこれを実行する方法はありません 。プラグインを書く!

これを実現する1つの方法は、データベース内に新しいテーブルを作成して、そのサイトでユーザーが最後にアクティブになった時間を単純に追跡することです。プラグインの設定ページで登録ユーザーを「ログイン」したと見なす期間を決定することもできます。

WordPressフック を使ってこれを実装します。ユーザーがログインすると、プラグインがデータベースに時間を記録するように、ログインにフックすることから始めます。その後、ログアウトをクリックした場合はステータスを「不在」に設定し、ログイン時間が2時間以上前の場合は「アイドル」に設定するなど、他のことを検討できます。

サイトにログインしてアクティブになっていても、この2時間のウィンドウを過ぎると、問題が発生します。この場合は、wp-adminセクションにフックして、いつでもwp-adminで何かを実行するとデータベースが現在の時刻に更新されるようにする必要があります。

それから、投稿では、2つのことをする必要があるでしょう:現在の投稿の作者を得る:

<?php $user_login = the_author_meta( $user_login ); ?>

次にデータベースに問い合わせて、ログインしているかどうかを確認します。

<?php if your_plugin_function($user_login)... ?>
...display something...
1
brock