| 1 |
<?php |
| 2 |
|
| 3 |
namespace Codemanas\InactiveLogout; |
| 4 |
|
| 5 |
/** |
| 6 |
* Core Functions for Concurrent Logins |
| 7 |
* |
| 8 |
* Derived from Prevent Concurrent Logins by Frankie Jarrett |
| 9 |
* |
| 10 |
* @since 1.1.0 |
| 11 |
*/ |
| 12 |
class ConcurrentLogin { |
| 13 |
|
| 14 |
/** |
| 15 |
* Inactive_Concurrent_Login_Functions constructor. |
| 16 |
*/ |
| 17 |
public function __construct() { |
| 18 |
add_action( 'wp_loaded', array( $this, 'concurrent_logins' ) ); |
| 19 |
// $this->concurrent_logins(); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Detect if the current user has multiple sessions |
| 24 |
* |
| 25 |
* @return bool |
| 26 |
* @since 1.1.0 |
| 27 |
*/ |
| 28 |
public function user_has_multiple_sessions() { |
| 29 |
return ( is_user_logged_in() && count( wp_get_all_sessions() ) > 1 ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Get the user's current session array |
| 34 |
* |
| 35 |
* @return array |
| 36 |
*/ |
| 37 |
public function get_current_session() { |
| 38 |
$sessions = \WP_Session_Tokens::get_instance( get_current_user_id() ); |
| 39 |
|
| 40 |
return $sessions->get( wp_get_session_token() ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Only allow one session per user |
| 45 |
* |
| 46 |
* If the current user's session has been taken over by a newer |
| 47 |
* session then we will destroy their session automattically and |
| 48 |
* they will have to login again to continue. |
| 49 |
* |
| 50 |
* @action init |
| 51 |
*/ |
| 52 |
public function concurrent_logins() { |
| 53 |
if ( ! $this->user_has_multiple_sessions() ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
$user_id = get_current_user_id(); |
| 58 |
|
| 59 |
/** |
| 60 |
* Filter to allow certain users to have concurrent sessions when necessary |
| 61 |
* |
| 62 |
* @param bool $prevent |
| 63 |
* @param int $user_id ID of the current user |
| 64 |
* |
| 65 |
* @return bool |
| 66 |
*/ |
| 67 |
if ( false === (bool) apply_filters( 'ina_allow_multiple_sessions', true, $user_id ) ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Filter to limit number of concurrent users |
| 73 |
*/ |
| 74 |
if ( false === (bool) apply_filters( 'ina_limit_logins_by_count', $user_id ) ) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
// Finding maximum value of all sessions available. |
| 79 |
$sessions = wp_get_all_sessions(); |
| 80 |
$newest = max( wp_list_pluck( $sessions, 'login' ) ); |
| 81 |
$session = $this->get_current_session(); |
| 82 |
if ( $session['login'] === $newest ) { |
| 83 |
wp_destroy_other_sessions(); |
| 84 |
|
| 85 |
do_action( 'ina_otherSessionsLoggedOut', $user_id ); |
| 86 |
} else { |
| 87 |
wp_destroy_current_session(); |
| 88 |
|
| 89 |
do_action( 'ina_currentSessionsLoggedOut', $user_id ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Instance property |
| 95 |
* |
| 96 |
* @var null |
| 97 |
*/ |
| 98 |
private static $_instance = null; |
| 99 |
|
| 100 |
/** |
| 101 |
* Create only one instance so that it may not Repeat |
| 102 |
* |
| 103 |
* @since 1.0.0 |
| 104 |
*/ |
| 105 |
public static function getInstance() { |
| 106 |
if ( is_null( self::$_instance ) ) { |
| 107 |
self::$_instance = new self(); |
| 108 |
} |
| 109 |
|
| 110 |
return self::$_instance; |
| 111 |
} |
| 112 |
} |
| 113 |
|