3b272b09 by Chris Boden

Logout of Facebook by URL redirects (PHP SDK) instead of JavaScript, removed jun…

…k code, added comments
1 parent 5d852d0a
......@@ -42,7 +42,7 @@ const OPTION_NAME = 'tz_auth_fb';
function drawLoginButton($echo = true) {
$title = Vars::$options['button_title'] ?: 'Login';
$btn = '<a id="TzFB" class="fb_button fb_button_medium"><span class="fb_button_text">' . $title . '</span></a>';
// $btn = '<fb:login-button></fb:login-button>';
// $btn = '<fb:login-button></fb:login-button>';
if (!$echo) {
return $btn;
......@@ -68,28 +68,12 @@ function getSDK() {
}
function load() {
Vars::$loaded = true;
?>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({appId: '<?php echo Vars::$options['application_id']; ?>', status: true, cookie: true, xfbml: true});
FB.Event.subscribe('auth.login', function(response) { window.location.reload(); });
<?php if (isset($_COOKIE['wpfb_logout'])): ?>
FB.getLoginStatus(function(response) {
if (response.session) {
FB.logout(function() {
var date = new Date();
date.setTime(date.getTime() - 1);
document.cookie = 'wpfb_logout=;expires=' + date.toGMTString() + ';path=/';
// window.location.reload();
});
}
});
<?php endif; ?>
};
(function() {
......@@ -105,13 +89,19 @@ function load() {
class Actions {
public static function set_current_user() {
$sdk = getSDK();
// User is not logged in to Facebook
if (null === ($sess = $sdk->getSession())) {
return;
}
// User logged out of WordPress, log them out of Facebook
if (isset($_COOKIE['wpfb_logout'])) {
setcookie('wpfb_logout', '', time() - 3600, '/', Vars::$options['domain_name']);
$url = $sdk->getLogoutUrl();
$sdk->setSession();
return;
header('Location: ' . $url);
}
// if user is not logged in do the following
......@@ -135,6 +125,9 @@ class Actions {
if (username_exists($username)) {
$user = Auth\signin($username);
} else {
// User logged in via Facebook for the first time, register/activate a linked WordPress account
// Email address is already registered...
if (false !== get_user_by('email', $info['email'])) {
// Not sure if I can throw exception, this is outside the theme stuff...
throw new Exception('email conflict');
......@@ -169,7 +162,7 @@ class Actions {
}
/**
* Destroy Facebook session data on site if the log out of WordPress
* Set a cookie to tell this to logout of Facebook on next pass
*/
public static function wp_logout() {
setcookie('wpfb_logout', 1, 0, '/', Vars::$options['domain_name']);
......@@ -200,6 +193,5 @@ class Vars {
* @type WP_Option
*/
public static $options;
public static $loaded = false;
}
?>
\ No newline at end of file
......