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'; ...@@ -42,7 +42,7 @@ const OPTION_NAME = 'tz_auth_fb';
42 function drawLoginButton($echo = true) { 42 function drawLoginButton($echo = true) {
43 $title = Vars::$options['button_title'] ?: 'Login'; 43 $title = Vars::$options['button_title'] ?: 'Login';
44 $btn = '<a id="TzFB" class="fb_button fb_button_medium"><span class="fb_button_text">' . $title . '</span></a>'; 44 $btn = '<a id="TzFB" class="fb_button fb_button_medium"><span class="fb_button_text">' . $title . '</span></a>';
45 // $btn = '<fb:login-button></fb:login-button>'; 45 // $btn = '<fb:login-button></fb:login-button>';
46 46
47 if (!$echo) { 47 if (!$echo) {
48 return $btn; 48 return $btn;
...@@ -68,28 +68,12 @@ function getSDK() { ...@@ -68,28 +68,12 @@ function getSDK() {
68 } 68 }
69 69
70 function load() { 70 function load() {
71 Vars::$loaded = true;
72 ?> 71 ?>
73 <div id="fb-root"></div> 72 <div id="fb-root"></div>
74 <script> 73 <script>
75 window.fbAsyncInit = function() { 74 window.fbAsyncInit = function() {
76 FB.init({appId: '<?php echo Vars::$options['application_id']; ?>', status: true, cookie: true, xfbml: true}); 75 FB.init({appId: '<?php echo Vars::$options['application_id']; ?>', status: true, cookie: true, xfbml: true});
77
78 FB.Event.subscribe('auth.login', function(response) { window.location.reload(); }); 76 FB.Event.subscribe('auth.login', function(response) { window.location.reload(); });
79
80 <?php if (isset($_COOKIE['wpfb_logout'])): ?>
81 FB.getLoginStatus(function(response) {
82 if (response.session) {
83 FB.logout(function() {
84 var date = new Date();
85 date.setTime(date.getTime() - 1);
86 document.cookie = 'wpfb_logout=;expires=' + date.toGMTString() + ';path=/';
87
88 // window.location.reload();
89 });
90 }
91 });
92 <?php endif; ?>
93 }; 77 };
94 78
95 (function() { 79 (function() {
...@@ -105,13 +89,19 @@ function load() { ...@@ -105,13 +89,19 @@ function load() {
105 class Actions { 89 class Actions {
106 public static function set_current_user() { 90 public static function set_current_user() {
107 $sdk = getSDK(); 91 $sdk = getSDK();
92
93 // User is not logged in to Facebook
108 if (null === ($sess = $sdk->getSession())) { 94 if (null === ($sess = $sdk->getSession())) {
109 return; 95 return;
110 } 96 }
111 97
98 // User logged out of WordPress, log them out of Facebook
112 if (isset($_COOKIE['wpfb_logout'])) { 99 if (isset($_COOKIE['wpfb_logout'])) {
100 setcookie('wpfb_logout', '', time() - 3600, '/', Vars::$options['domain_name']);
101 $url = $sdk->getLogoutUrl();
113 $sdk->setSession(); 102 $sdk->setSession();
114 return; 103
104 header('Location: ' . $url);
115 } 105 }
116 106
117 // if user is not logged in do the following 107 // if user is not logged in do the following
...@@ -135,6 +125,9 @@ class Actions { ...@@ -135,6 +125,9 @@ class Actions {
135 if (username_exists($username)) { 125 if (username_exists($username)) {
136 $user = Auth\signin($username); 126 $user = Auth\signin($username);
137 } else { 127 } else {
128 // User logged in via Facebook for the first time, register/activate a linked WordPress account
129
130 // Email address is already registered...
138 if (false !== get_user_by('email', $info['email'])) { 131 if (false !== get_user_by('email', $info['email'])) {
139 // Not sure if I can throw exception, this is outside the theme stuff... 132 // Not sure if I can throw exception, this is outside the theme stuff...
140 throw new Exception('email conflict'); 133 throw new Exception('email conflict');
...@@ -169,7 +162,7 @@ class Actions { ...@@ -169,7 +162,7 @@ class Actions {
169 } 162 }
170 163
171 /** 164 /**
172 * Destroy Facebook session data on site if the log out of WordPress 165 * Set a cookie to tell this to logout of Facebook on next pass
173 */ 166 */
174 public static function wp_logout() { 167 public static function wp_logout() {
175 setcookie('wpfb_logout', 1, 0, '/', Vars::$options['domain_name']); 168 setcookie('wpfb_logout', 1, 0, '/', Vars::$options['domain_name']);
...@@ -200,6 +193,5 @@ class Vars { ...@@ -200,6 +193,5 @@ class Vars {
200 * @type WP_Option 193 * @type WP_Option
201 */ 194 */
202 public static $options; 195 public static $options;
203 public static $loaded = false;
204 } 196 }
205 ?> 197 ?>
...\ No newline at end of file ...\ No newline at end of file
......