c320c4d5 by Chris Boden

Cleaning up FB code, added Twitter (front-end only)

1 parent e90c5db6
...@@ -59,7 +59,8 @@ function login($username, $password, $remember = true) { ...@@ -59,7 +59,8 @@ function login($username, $password, $remember = true) {
59 )); 59 ));
60 60
61 if (get_class($auth) == 'WP_User') { 61 if (get_class($auth) == 'WP_User') {
62 _set_current_user($auth->ID); // Not sure why I had to do this, oh well 62 // This is done to ensure the auth'd user is logged in; cookie is not yet readable, redirect needed
63 _set_current_user($auth->ID);
63 64
64 return $auth; 65 return $auth;
65 } 66 }
...@@ -102,6 +103,7 @@ function logout() { ...@@ -102,6 +103,7 @@ function logout() {
102 } 103 }
103 104
104 _logout(); 105 _logout();
106 // I might need to do _set_current_user(0);
105 107
106 return true; 108 return true;
107 } 109 }
...@@ -160,7 +162,7 @@ function register($username, $email, $password, $meta = Array()) { ...@@ -160,7 +162,7 @@ function register($username, $email, $password, $meta = Array()) {
160 * @see wpmu_activate_signup 162 * @see wpmu_activate_signup
161 */ 163 */
162 function activate($key) { 164 function activate($key) {
163 global $wpdb, $current_site; 165 global $wpdb, $current_blog;
164 $signup = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->signups} WHERE activation_key = %s", $key)); 166 $signup = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->signups} WHERE activation_key = %s", $key));
165 167
166 if (empty($signup)) { 168 if (empty($signup)) {
...@@ -183,8 +185,8 @@ function activate($key) { ...@@ -183,8 +185,8 @@ function activate($key) {
183 } 185 }
184 186
185 $wpdb->update($wpdb->signups, Array('active' => 1, 'activated' => current_time('mysql', true)), Array('activation_key' => $key)); 187 $wpdb->update($wpdb->signups, Array('active' => 1, 'activated' => current_time('mysql', true)), Array('activation_key' => $key));
186 $user_site = get_site_option('dashboard_blog', $current_site->blog_id); 188 // $user_site = get_site_option('dashboard_blog', $current_site->blog_id);
187 add_user_to_blog(($user_site ?: 1), $id, get_site_option('default_user_role', 'subscriber')); 189 add_user_to_blog($current_blog->blog_id, $id, get_site_option('default_user_role', 'subscriber'));
188 190
189 // If use these, fix variables, they're wrong 191 // If use these, fix variables, they're wrong
190 //add_new_user_to_blog( $id, $user_email, $meta ); 192 //add_new_user_to_blog( $id, $user_email, $meta );
......
...@@ -29,6 +29,7 @@ const OPTION_NAME = 'tz_auth_fb'; ...@@ -29,6 +29,7 @@ const OPTION_NAME = 'tz_auth_fb';
29 Vars::$options = new Tools\WP_Option(OPTION_NAME, Array('button_title' => 'Login', 'ext_perms' => Array('email' => 1))); 29 Vars::$options = new Tools\WP_Option(OPTION_NAME, Array('button_title' => 'Login', 'ext_perms' => Array('email' => 1)));
30 30
31 Tools\add_actions(__NAMESPACE__ . '\Actions'); 31 Tools\add_actions(__NAMESPACE__ . '\Actions');
32 Tools\add_filters(__NAMESPACE__ . '\Filters');
32 Tools\add_shortcodes(__NAMESPACE__ . '\ShortCodes'); 33 Tools\add_shortcodes(__NAMESPACE__ . '\ShortCodes');
33 34
34 if (is_admin()) { 35 if (is_admin()) {
...@@ -105,15 +106,18 @@ function load() { ...@@ -105,15 +106,18 @@ function load() {
105 106
106 class Actions { 107 class Actions {
107 public static function send_headers() { 108 public static function send_headers() {
109 /*
108 // This SHOULD work, but FB is being stupid and not passing back, so I have to use 2 cookies instead 110 // This SHOULD work, but FB is being stupid and not passing back, so I have to use 2 cookies instead
109 if (isset($_GET['nofb'])) { 111 if (isset($_GET['nofb'])) {
110 return; 112 return;
111 } 113 }
114 */
112 115
113 $sdk = getSDK(); 116 $sdk = getSDK();
114 117
115 // User is not logged in to Facebook 118 // User is not logged in to Facebook
116 if (null === ($sess = $sdk->getSession())) { 119 if (null === ($sess = $sdk->getSession())) {
120 // In case FB enabled, but not used by this user, remove useless cookies
117 setcookie(COOKIE_LOGOUT, '', time() - 3600, '/'); 121 setcookie(COOKIE_LOGOUT, '', time() - 3600, '/');
118 setcookie(COOKIE_DENY, '', time() - 3600, '/'); 122 setcookie(COOKIE_DENY, '', time() - 3600, '/');
119 123
...@@ -138,30 +142,28 @@ class Actions { ...@@ -138,30 +142,28 @@ class Actions {
138 die; 142 die;
139 } 143 }
140 144
141
142 $fb_user = get_user_by_fbuid($sess['uid']); 145 $fb_user = get_user_by_fbuid($sess['uid']);
143 146
144 if (is_user_logged_in()) { 147 if (is_user_logged_in()) {
145 global $current_user; 148 global $current_user;
146 get_currentuserinfo(); 149 get_currentuserinfo();
147 150
148 // User has already logged into WP with his FB acct
149 if ($fb_user->ID == $current_user->ID) {
150 return;
151 }
152
153 // User logged in with a native WP account then logged in with FB, merge 151 // User logged in with a native WP account then logged in with FB, merge
154 if (false === $fb_user) { 152 if (false === $fb_user) {
155 update_user_meta($current_user->ID, 'fbuid', $sess['uid']); 153 update_user_meta($current_user->ID, 'fbuid', $sess['uid']);
156 return; 154 return;
157 } 155 }
158 156
157 // User has already logged into WP with his FB acct
158 if ($fb_user->ID == $current_user->ID) {
159 return;
160 }
161
159 // FB user exists, but the logged in user has different fbuid? 162 // FB user exists, but the logged in user has different fbuid?
160 // user created 2 accounts? 163 // user created 2 accounts?
161 } 164 }
162 165
163 166 // Welcome back!
164 // if (username_exists($username)) {
165 if (false !== $fb_user) { 167 if (false !== $fb_user) {
166 $user = Auth\signin($fb_user->user_login); 168 $user = Auth\signin($fb_user->user_login);
167 } else { 169 } else {
...@@ -195,6 +197,7 @@ class Actions { ...@@ -195,6 +197,7 @@ class Actions {
195 , 'nickname' => $info['name'] 197 , 'nickname' => $info['name']
196 , 'display_name' => $info['name'] 198 , 'display_name' => $info['name']
197 , 'user_url' => ($info['user_website'] ?: '') 199 , 'user_url' => ($info['user_website'] ?: '')
200 , 'facebook' => $info['link']
198 )); 201 ));
199 202
200 update_user_meta($user->ID, 'fbuid', $info['id']); 203 update_user_meta($user->ID, 'fbuid', $info['id']);
...@@ -205,7 +208,7 @@ class Actions { ...@@ -205,7 +208,7 @@ class Actions {
205 } 208 }
206 209
207 public static function wp_enqueue_scripts() { 210 public static function wp_enqueue_scripts() {
208 _enqueue_script('tz-facebook', Tools\url('tz-facebook.js', __FILE__), Array('addEvent','Cookie')); 211 _enqueue_script('tz-facebook', Tools\url('tz-facebook.js', __FILE__), Array('addEvent'));
209 _localize_script('tz-facebook', 'TzFBData', Array('ext_perms' => implode(',', array_keys(Vars::$options['ext_perms'])))); 212 _localize_script('tz-facebook', 'TzFBData', Array('ext_perms' => implode(',', array_keys(Vars::$options['ext_perms']))));
210 } 213 }
211 214
...@@ -219,6 +222,13 @@ class Actions { ...@@ -219,6 +222,13 @@ class Actions {
219 } 222 }
220 } 223 }
221 224
225 class Filters {
226 public static function user_contactmethods($methods) {
227 $methods['facebook'] = 'Facebook';
228 return $methods;
229 }
230 }
231
222 class ShortCodes { 232 class ShortCodes {
223 public static function fb_login_button() { 233 public static function fb_login_button() {
224 $sdk = getSDK(); 234 $sdk = getSDK();
......
...@@ -2,8 +2,6 @@ addEvent(window, 'load', function() { ...@@ -2,8 +2,6 @@ addEvent(window, 'load', function() {
2 var oBtn = document.getElementById('TzFB'); 2 var oBtn = document.getElementById('TzFB');
3 if (oBtn) { 3 if (oBtn) {
4 addEvent(oBtn, 'click', function() { 4 addEvent(oBtn, 'click', function() {
5 // Cookie.create('wpfb_login', 1, 1);
6
7 FB.login(function() {}, {perms: TzFBData.ext_perms}); 5 FB.login(function() {}, {perms: TzFBData.ext_perms});
8 }); 6 });
9 } 7 }
......
...@@ -75,6 +75,10 @@ class Fields { ...@@ -75,6 +75,10 @@ class Fields {
75 public static function Facebook() { 75 public static function Facebook() {
76 echo '<input type="checkbox" id="' . __FUNCTION__ . '" name="' . Auth\OPTION_NAME . '[third_party][' . __FUNCTION__ . ']" value="1" ' . checked('1', Auth\Vars::$options['third_party'][__FUNCTION__], false) . ' />'; 76 echo '<input type="checkbox" id="' . __FUNCTION__ . '" name="' . Auth\OPTION_NAME . '[third_party][' . __FUNCTION__ . ']" value="1" ' . checked('1', Auth\Vars::$options['third_party'][__FUNCTION__], false) . ' />';
77 } 77 }
78
79 public static function Twitter() {
80 echo '<input type="checkbox" id="' . __FUNCTION__ . '" name="' . Auth\OPTION_NAME . '[third_party][' . __FUNCTION__ . ']" value="1" ' . checked('1', Auth\Vars::$options['third_party'][__FUNCTION__], false) . ' />';
81 }
78 } 82 }
79 83
80 class Validation extends Common\Validation { 84 class Validation extends Common\Validation {
......
1 <?php
2
3 namespace Tz\WordPress\Tools\Twitter\Settings;
4
5 use Tz\WordPress\Tools, Tz\WordPress\Tools\Auth, Tz\WordPress\Tools\Auth\Twitter;
6
7 const OPTION_SECTION = 'twit_creds';
8
9 call_user_func(function() {
10 Tools\add_actions(__NAMESPACE__ . '\Actions');
11 });
12
13 function dipslayPage() {
14 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'settings_view.php');
15 }
16
17 function validate($data) {
18 return $data;
19 }
20
21 class Actions {
22 public static function admin_init() {
23 register_setting(Auth\Settings\OPTION_GROUP, Twitter\OPTION_NAME, __NAMESPACE__ . '\validate');
24 add_settings_section(OPTION_SECTION, 'Twitter Credentials', function() {}, Auth\Settings\ADMIN_PAGE);
25
26 Tools\add_settings_fields(__NAMESPACE__ . '\Fields', Auth\Settings\ADMIN_PAGE, OPTION_SECTION);
27 }
28 }
29
30 class Fields {
31 public static function api_key() {
32 echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Twitter\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Twitter\Vars::$options[__FUNCTION__] . '" size="30" />';
33 }
34
35
36 public static function secret() {
37 echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Twitter\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Twitter\Vars::$options[__FUNCTION__] . '" size="45" />';
38 }
39 }
40 ?>
...\ No newline at end of file ...\ No newline at end of file
1 <?php
2 /**
3 *
4 */
5
6 namespace Tz\WordPress\Tools\Auth\Twitter;
7
8 use Tz\WordPress\Tools, Tz\WordPress\Tools\Auth;
9
10 use Exception;
11
12 const VERSION = 0.1;
13 const OPTION_NAME = 'tz_auth_twit';
14
15 call_user_func(function() {
16 Vars::$options = new Tools\WP_Option(OPTION_NAME);
17
18 Tools\add_actions(__NAMESPACE__ . '\Actions');
19 Tools\add_filters(__NAMESPACE__ . '\Filters');
20 Tools\add_shortcodes(__NAMESPACE__ . '\ShortCodes');
21
22 if (is_admin()) {
23 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Settings.php');
24 }
25 });
26
27 class Actions {
28 public static function wp_enqueue_scripts() {
29 _enqueue_script('anywhere', 'http://platform.twitter.com/anywhere.js?id=' . Vars::$options['api_key'] . '&v=1');
30 _enqueue_script('tz-twitter', Tools\url('tz-twitter.js', __FILE__), Array('anywhere'));
31 }
32 }
33
34 class Filters {
35 public static function user_contactmethods($methods) {
36 $methods['twitter'] = 'Twitter';
37 return $methods;
38 }
39 }
40
41 class ShortCodes {
42 public static function twitter_login_button() {
43 return '<div id="TwtLogin"></div>';
44 }
45 }
46
47 class Vars {
48 public static $options;
49 }
50 ?>
...\ No newline at end of file ...\ No newline at end of file
1 twttr.anywhere(function(T) {
2 T('#TwtLogin').connectButton();
3
4 T.bind('authComplete', function(e, user) {
5 console.log(e);
6 console.log(user);
7 });
8
9 if (T.isConnected()) {
10 console.log(T.currentUser.data('screen_name'));
11 }
12 });
...\ No newline at end of file ...\ No newline at end of file