| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately; |
| 4 |
|
| 5 |
class Login { |
| 6 |
|
| 7 |
/** |
| 8 |
* Save global sign in flag based on user choice. |
| 9 |
* |
| 10 |
* @param $choice |
| 11 |
* |
| 12 |
* @return bool true|false |
| 13 |
*/ |
| 14 |
public static function set_user_login_choice( $user, $choice ) { |
| 15 |
if( ! self::already_signedin_globally() ) { |
| 16 |
return update_option( '_templately_user_login_choice', array ( |
| 17 |
'id' => $user, |
| 18 |
'choice' => $choice |
| 19 |
) ); |
| 20 |
} |
| 21 |
return false; |
| 22 |
} |
| 23 |
|
| 24 |
public static function get_user_login_choice() { |
| 25 |
return get_option( '_templately_user_login_choice', array () ); |
| 26 |
} |
| 27 |
|
| 28 |
public static function user_can_logout( $user = null ) { |
| 29 |
$userdata = self::get_user_login_choice(); |
| 30 |
if( ! empty( $userdata ) ) { |
| 31 |
if( isset( $userdata['choice'] ) ) { |
| 32 |
return isset( $userdata['id'] ) ? $userdata['id'] === get_current_user_id() : false; |
| 33 |
} |
| 34 |
} |
| 35 |
return false; |
| 36 |
} |
| 37 |
|
| 38 |
public static function already_signedin_globally() { |
| 39 |
$userdata = self::get_user_login_choice(); |
| 40 |
return isset( $userdata['choice'] ) && \boolval( $userdata['choice'] ); |
| 41 |
} |
| 42 |
|
| 43 |
public static function get_global_signed_in_by_user( $user_id = null ) { |
| 44 |
if( is_null( $user_id ) ) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
$userdata = self::get_user_login_choice(); |
| 48 |
return isset( $userdata['id'] ) && $userdata['id'] == $user_id; |
| 49 |
} |
| 50 |
|
| 51 |
public static function set_link_my_account() { |
| 52 |
return update_user_meta( get_current_user_id(), '_templately_link_my_account', true ); |
| 53 |
} |
| 54 |
|
| 55 |
public static function get_link_my_account() { |
| 56 |
$user_meta = get_user_meta( get_current_user_id(), '_templately_link_my_account', true ); |
| 57 |
return ! empty( $user_meta ) ? $user_meta : false; |
| 58 |
} |
| 59 |
|
| 60 |
public static function delete_link_my_account() { |
| 61 |
return delete_user_meta( get_current_user_id(), '_templately_link_my_account' ); |
| 62 |
} |
| 63 |
|
| 64 |
} |