bf226ee4 by Chris Boden

Facebook with WP registration working, login/out working

1 parent 90891653
...@@ -12,23 +12,19 @@ namespace Tz\WordPress\Tools\Auth; ...@@ -12,23 +12,19 @@ namespace Tz\WordPress\Tools\Auth;
12 use Tz\WordPress\Tools; 12 use Tz\WordPress\Tools;
13 use Tz\Common; 13 use Tz\Common;
14 use Exception, LogicException, InvalidArgumentException, BadMethodCallException; 14 use Exception, LogicException, InvalidArgumentException, BadMethodCallException;
15 15 use WP_User;
16 const ACTION_CHECK_AUTH = 'check_auth';
17 16
18 // These are all WordPress hooks, I put them here for easy reference 17 // These are all WordPress hooks, I put them here for easy reference
19 const ACTION_LOGIN = 'wp_login'; 18 const ACTION_LOGIN = 'wp_login';
20 const ACTION_LOGOUT = 'wp_logout'; 19 const ACTION_LOGOUT = 'wp_logout';
21 const ACTION_ACTIVATE = 'user_register'; 20 const ACTION_REGISTER = 'user_register';
22 21 const ACTION_ACTIVATE = 'wpmu_activate_user';
23 //const ACTION_REGISTER
24 22
25 const OPTION_NAME = 'tz_auth'; // Database lookup key (`wp_options`.`option_name`) 23 const OPTION_NAME = 'tz_auth'; // Database lookup key (`wp_options`.`option_name`)
26 24
27 call_user_func(function() { 25 call_user_func(function() {
28 Vars::$options = new Tools\WP_Option(OPTION_NAME); 26 Vars::$options = new Tools\WP_Option(OPTION_NAME);
29 27
30 Tools\add_actions(__NAMESPACE__ . '\Actions');
31
32 if (is_admin()) { 28 if (is_admin()) {
33 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Settings.php'); 29 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Settings.php');
34 } 30 }
...@@ -92,7 +88,7 @@ function signin($username, $remember = true) { ...@@ -92,7 +88,7 @@ function signin($username, $remember = true) {
92 _set_current_user($user->ID); 88 _set_current_user($user->ID);
93 89
94 do_action('wp_login', $username); 90 do_action('wp_login', $username);
95 return $user; 91 return new WP_User($user->ID);
96 } 92 }
97 93
98 /** 94 /**
...@@ -153,6 +149,8 @@ function register($username, $email, $password, $meta = Array()) { ...@@ -153,6 +149,8 @@ function register($username, $email, $password, $meta = Array()) {
153 'meta' => $meta 149 'meta' => $meta
154 )); 150 ));
155 151
152 // do_action('ACTION_REGISTER'); ???
153
156 return $key; 154 return $key;
157 } 155 }
158 156
...@@ -162,7 +160,7 @@ function register($username, $email, $password, $meta = Array()) { ...@@ -162,7 +160,7 @@ function register($username, $email, $password, $meta = Array()) {
162 * @see wpmu_activate_signup 160 * @see wpmu_activate_signup
163 */ 161 */
164 function activate($key) { 162 function activate($key) {
165 global $wpdb; 163 global $wpdb, $current_site;
166 $signup = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->signups} WHERE activation_key = %s", $key)); 164 $signup = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->signups} WHERE activation_key = %s", $key));
167 165
168 if (empty($signup)) { 166 if (empty($signup)) {
...@@ -177,14 +175,6 @@ function activate($key) { ...@@ -177,14 +175,6 @@ function activate($key) {
177 175
178 // Do I need to re-sanatize this? 176 // Do I need to re-sanatize this?
179 $meta = unserialize($signup->meta); 177 $meta = unserialize($signup->meta);
180 /*
181 $user_data = Array(
182 'user_login' => $signup->user_login
183 , 'user_email' => $signup->user_email
184 , 'user_pass' => $meta['password']
185 );
186 $id = (int)_insert_user($user_data);
187 */
188 $id = _create_user($signup->user_login, $meta['password'], $signup->user_email); 178 $id = _create_user($signup->user_login, $meta['password'], $signup->user_email);
189 unset($meta['password']); 179 unset($meta['password']);
190 180
...@@ -192,38 +182,17 @@ function activate($key) { ...@@ -192,38 +182,17 @@ function activate($key) {
192 throw new Exception('Unable to create user'); 182 throw new Exception('Unable to create user');
193 } 183 }
194 184
195 /* Add the user to the appropriate blog 185 $wpdb->update($wpdb->signups, Array('active' => 1, 'activated' => current_time('mysql', true)), Array('activation_key' => $key));
196 $now = current_time('mysql', true); 186 $user_site = get_site_option('dashboard_blog', $current_site->blog_id);
197 $wpdb->update($wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key)); 187 add_user_to_blog(($user_site ?: 1), $id, get_site_option('default_user_role', 'subscriber'));
198 188
199 global $current_site; 189 // If use these, fix variables, they're wrong
200 $user_site = get_site_option( 'dashboard_blog', $current_site->blog_id ); 190 // add_new_user_to_blog( $id, $user_email, $meta );
201 191 // do_action(ACTION_ACTIVATE, $id, $password, $meta);
202 if ( $user_site == false )
203 add_user_to_blog( '1', $user_id, get_site_option( 'default_user_role', 'subscriber' ) );
204 else
205 add_user_to_blog( $user_site, $user_id, get_site_option( 'default_user_role', 'subscriber' ) );
206
207 add_new_user_to_blog( $user_id, $user_email, $meta );
208 do_action('wpmu_activate_user', $user_id, $password, $meta);
209 */
210 192
211 return (int)$id; 193 return (int)$id;
212 } 194 }
213 195
214 class Actions {
215 /*
216 // I forget why I chose wp() instead of set_current_user()...
217 public static function wp() {
218 global $post; // I want a better way to do this
219
220 if ($post->ID == Vars::$options['login_page'] && !is_user_logged_in()) {
221 do_action(ACTION_CHECK_AUTH);
222 }
223 }
224 */
225 }
226
227 class Validation extends Common\Validation { 196 class Validation extends Common\Validation {
228 /** 197 /**
229 * @rule Not blank 198 * @rule Not blank
......
...@@ -39,7 +39,7 @@ const OPTION_NAME = 'tz_auth_fb'; ...@@ -39,7 +39,7 @@ const OPTION_NAME = 'tz_auth_fb';
39 //setcookie('wpfb_logout', '', time() - 3600, '/'); 39 //setcookie('wpfb_logout', '', time() - 3600, '/');
40 40
41 call_user_func(function() { 41 call_user_func(function() {
42 Vars::$options = new Tools\WP_Option(OPTION_NAME, Array('button_title' => 'Login')); 42 Vars::$options = new Tools\WP_Option(OPTION_NAME, Array('button_title' => 'Login', 'ext_perms' => Array('email' => 1)));
43 43
44 Tools\add_actions(__NAMESPACE__ . '\Actions'); 44 Tools\add_actions(__NAMESPACE__ . '\Actions');
45 Tools\add_shortcodes(__NAMESPACE__ . '\ShortCodes'); 45 Tools\add_shortcodes(__NAMESPACE__ . '\ShortCodes');
...@@ -57,7 +57,7 @@ const OPTION_NAME = 'tz_auth_fb'; ...@@ -57,7 +57,7 @@ const OPTION_NAME = 'tz_auth_fb';
57 function drawLoginButton($echo = true) { 57 function drawLoginButton($echo = true) {
58 $title = Vars::$options['button_title'] ?: 'Login'; 58 $title = Vars::$options['button_title'] ?: 'Login';
59 $btn = '<a id="TzFB" class="fb_button fb_button_medium"><span class="fb_button_text">' . $title . '</span></a>'; 59 $btn = '<a id="TzFB" class="fb_button fb_button_medium"><span class="fb_button_text">' . $title . '</span></a>';
60 $btn = '<fb:login-button></fb:login-button>'; 60 // $btn = '<fb:login-button></fb:login-button>';
61 61
62 if (!$echo) { 62 if (!$echo) {
63 return $btn; 63 return $btn;
...@@ -83,16 +83,13 @@ function getSDK() { ...@@ -83,16 +83,13 @@ function getSDK() {
83 } 83 }
84 84
85 function load() { 85 function load() {
86 Vars::$loaded = true;
86 ?> 87 ?>
87 <div id="fb-root"></div> 88 <div id="fb-root"></div>
88 <script> 89 <script>
89 window.fbAsyncInit = function() { 90 window.fbAsyncInit = function() {
90 FB.init({appId: '<?php echo Vars::$options['application_id']; ?>', status: true, cookie: true, xfbml: true}); 91 FB.init({appId: '<?php echo Vars::$options['application_id']; ?>', status: true, cookie: true, xfbml: true});
91 92
92 FB.getLoginStatus(function(response) {
93 console.log(response.status);
94 });
95
96 FB.Event.subscribe('auth.login', function(response) { window.location.reload(); }); 93 FB.Event.subscribe('auth.login', function(response) { window.location.reload(); });
97 94
98 <?php if (isset($_COOKIE['wpfb_logout'])): ?> 95 <?php if (isset($_COOKIE['wpfb_logout'])): ?>
...@@ -120,18 +117,7 @@ function load() { ...@@ -120,18 +117,7 @@ function load() {
120 <?php 117 <?php
121 } 118 }
122 119
123 /*
124 * Logic for all these methods needs to be re-thought out
125 * Should only load FB stuff when something happens (decide)
126 * 1) drawLoginButton() has been called
127 * 2) User is on the login_page
128 * 3) Some other clever way of deciding if FB stuff should load
129 */
130 class Actions { 120 class Actions {
131 /**
132 * Logs the user in to WP if they logged into FB
133 * @global $post
134 */
135 public static function set_current_user() { 121 public static function set_current_user() {
136 $sdk = getSDK(); 122 $sdk = getSDK();
137 if (null === ($sess = $sdk->getSession())) { 123 if (null === ($sess = $sdk->getSession())) {
...@@ -146,17 +132,20 @@ class Actions { ...@@ -146,17 +132,20 @@ class Actions {
146 // if user is not logged in do the following 132 // if user is not logged in do the following
147 // if user is logged in merge account? do checks? 133 // if user is logged in merge account? do checks?
148 134
149 // User is not logged into WP and has just logged in via FB
150
151 // need try/catch here - I think I got an OAuthException at one point
152
153 try { 135 try {
154 $info = $sdk->api('/me'); 136 $info = $sdk->api('/me');
155 $username = 'fbc' . $sess['uid']; 137 $username = 'fbc' . $sess['uid'];
156 } catch (FB\FacebookApiException $e) { 138 } catch (FB\FacebookApiException $e) {
139 // Load up an error thingie
157 return; 140 return;
158 } 141 }
159 142
143 if (is_user_logged_in()) {
144 // was user already logged in from Facebook/other or were they logged in and then linked with facebook
145 // merge account
146 // return
147 }
148
160 require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'registration.php'); 149 require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'registration.php');
161 if (username_exists($username)) { 150 if (username_exists($username)) {
162 $user = Auth\signin($username); 151 $user = Auth\signin($username);
...@@ -170,32 +159,28 @@ class Actions { ...@@ -170,32 +159,28 @@ class Actions {
170 $key = Auth\register($username, $info['email'], _generate_password()); 159 $key = Auth\register($username, $info['email'], _generate_password());
171 $id = Auth\activate($key); 160 $id = Auth\activate($key);
172 $user = Auth\signin($username); 161 $user = Auth\signin($username);
162
163 _update_user(Array(
164 'ID' => $user->ID
165 , 'user_nicename' => $info['name']
166 , 'first_name' => $info['first_name']
167 , 'last_name' => $info['last_name']
168 , 'nickname' => $info['name']
169 , 'display_name' => $info['name']
170 , 'user_url' => ($info['user_website'] ?: '')
171 ));
172
173 update_user_meta($user->ID, 'fbuid', $info['id']);
173 } catch (Exception $e) { 174 } catch (Exception $e) {
174 // many types of exceptions 175 // many types of exceptions
175 } 176 }
176 } 177 }
177
178 foreach (Vars::$options['ext_perms'] as $key => $on) {
179 // I need to map some keys to WordPress presets
180 // update_user_meta($user->ID, $key, $info[$key]);
181 }
182 } 178 }
183 179
184 /** 180 public static function wp_enqueue_scripts() {
185 * Load the Facebook scripts for login 181 _enqueue_script('tz-facebook', Tools\url('tz-facebook.js', __FILE__), Array('addEvent'));
186 */
187 public static function OFF_wp_enqueue_scripts() {
188 _enqueue_script('facebook-all', 'http://connect.facebook.net/en_US/all.js');
189 _enqueue_script('tz-facebook', Tools\url('tz-facebook.js', __FILE__), Array('addEvent', 'Cookie'));
190 182
191 _localize_script('tz-facebook', 'TzFBData', Array('AppID' => Vars::$options['application_id'], 'ext_perms' => implode(',', array_keys(Vars::$options['ext_perms'])), 'loginPage' => get_permalink(Auth\Vars::$options['login_page']))); 183 _localize_script('tz-facebook', 'TzFBData', Array('ext_perms' => implode(',', array_keys(Vars::$options['ext_perms']))));
192 }
193
194 /**
195 * Creates the anchor needed for Facebook scripts
196 */
197 public static function OFF_get_footer() {
198 echo '<div id="fb-root"></div>';
199 } 184 }
200 185
201 /** 186 /**
...@@ -208,22 +193,12 @@ class Actions { ...@@ -208,22 +193,12 @@ class Actions {
208 193
209 class ShortCodes { 194 class ShortCodes {
210 public static function fb_login_button() { 195 public static function fb_login_button() {
211 /*
212 if (is_user_logged_in()) {
213 return '';
214 }
215 */
216
217 $sdk = getSDK(); 196 $sdk = getSDK();
218 if ($sdk->getSession()) { 197 if ($sdk->getSession()) {
219 ob_start(); 198 ob_start();
220 print_r($sdk->getSession()); 199 print_r($sdk->getSession());
221 print_r($_COOKIE); 200 print_r($_COOKIE);
222 try {
223 print_r($sdk->api('/me')); 201 print_r($sdk->api('/me'));
224 } catch (Exception $e) {
225 die('fuck');
226 }
227 $data = '<pre>' . ob_get_contents() . '</pre>'; 202 $data = '<pre>' . ob_get_contents() . '</pre>';
228 ob_end_clean(); 203 ob_end_clean();
229 204
...@@ -240,5 +215,6 @@ class Vars { ...@@ -240,5 +215,6 @@ class Vars {
240 * @type WP_Option 215 * @type WP_Option
241 */ 216 */
242 public static $options; 217 public static $options;
218 public static $loaded = false;
243 } 219 }
244 ?> 220 ?>
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -18,7 +18,7 @@ function validate($data) { ...@@ -18,7 +18,7 @@ function validate($data) {
18 } 18 }
19 19
20 class Vars { 20 class Vars {
21 public static $data_permissions = Array('email', 'read_insights', 'read_stream', 'read_mailbox', 'ads_management', 'xmpp_login', 'user_about_me', 'user_activities', 'user_birthday', 'user_education_history', 'user_events', 'user_groups', 'user_hometown', 'user_interests', 'user_likes', 'user_location', 'user_notes', 'user_online_presence', 'user_photo_video_tags', 'user_photos', 'user_relationships', 'user_religion_politics', 'user_status', 'user_videos', 'user_website', 'user_work_history', 'read_friendlists', 'read_requests'); 21 public static $data_permissions = Array('read_insights', 'read_stream', 'read_mailbox', 'ads_management', 'xmpp_login', 'user_about_me', 'user_activities', 'user_birthday', 'user_education_history', 'user_events', 'user_groups', 'user_hometown', 'user_interests', 'user_likes', 'user_location', 'user_notes', 'user_online_presence', 'user_photo_video_tags', 'user_photos', 'user_relationships', 'user_religion_politics', 'user_status', 'user_videos', 'user_website', 'user_work_history', 'read_friendlists', 'read_requests');
22 } 22 }
23 23
24 class Actions { 24 class Actions {
...@@ -30,8 +30,19 @@ class Actions { ...@@ -30,8 +30,19 @@ class Actions {
30 30
31 add_settings_section(OPTION_SECTION_SEL, 'Facebook Extended Data Permissions', function() { echo '<p>Select which additional data you wish to collect from the user</p>'; }, Auth\Settings\ADMIN_PAGE); 31 add_settings_section(OPTION_SECTION_SEL, 'Facebook Extended Data Permissions', function() { echo '<p>Select which additional data you wish to collect from the user</p>'; }, Auth\Settings\ADMIN_PAGE);
32 foreach (Vars::$data_permissions as $option) { 32 foreach (Vars::$data_permissions as $option) {
33 add_settings_field($option, ucwords(str_replace('_', ' ', $option)), Array(new Opt_Fields(), $option), Auth\Settings\ADMIN_PAGE, OPTION_SECTION_SEL); 33 add_settings_field(
34 $option
35 , ucwords(str_replace('_', ' ', $option))
36 , function() use ($option) {
37 echo '<input type="checkbox" id="' . $option . '" name="' . Facebook\OPTION_NAME . '[ext_perms][' . $option . ']" value="1" ' . checked('1', Facebook\Vars::$options['ext_perms'][$option], false) . ' />';
34 } 38 }
39 , Auth\Settings\ADMIN_PAGE
40 , OPTION_SECTION_SEL
41 );
42 }
43 add_settings_field('email', '', function() {
44 echo '<input type="hidden" id="email" name="' . Facebook\OPTION_NAME . '[ext_perms][email]" value="1" />';
45 }, Auth\Settings\ADMIN_PAGE, OPTION_SECTION_SEL);
35 } 46 }
36 } 47 }
37 48
...@@ -56,10 +67,4 @@ class Cred_Fields { ...@@ -56,10 +67,4 @@ class Cred_Fields {
56 echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Facebook\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Facebook\Vars::$options[__FUNCTION__] . '" />'; 67 echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Facebook\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Facebook\Vars::$options[__FUNCTION__] . '" />';
57 } 68 }
58 } 69 }
59
60 class Opt_Fields {
61 public function __call($fn, $args) {
62 echo '<input type="checkbox" id="' . $fn . '" name="' . Facebook\OPTION_NAME . '[ext_perms][' . $fn . ']" value="1" ' . checked('1', Facebook\Vars::$options['ext_perms'][$fn], false) . ' />';
63 }
64 }
65 ?> 70 ?>
...\ No newline at end of file ...\ No newline at end of file
......
1 window.fbAsyncInit = function() { 1 addEvent(window, 'load', function() {
2 FB.init({appId: TzFBData.AppID, status: true, cookie: true, xfbml: true});
3
4 FB.Event.subscribe('auth.login', function(response) { console.log('login called'); window.location.href = TzFBData.loginPage; });
5
6 var oBtn = document.getElementById('TzFB'); 2 var oBtn = document.getElementById('TzFB');
7 if (oBtn) { 3 if (oBtn) {
8 addEvent(oBtn, 'click', FB.login); 4 addEvent(oBtn, 'click', function() {
9 } 5 FB.login(function() {}, {perms: TzFBData.ext_perms});
10
11 if (Cookie.read('wpfb_logout')) {
12 Cookie.erase('wpfb_logout');
13
14 FB.getLoginStatus(function(response) {
15 if (response.session) {
16 FB.logout(function() {
17 Cookie.erase('wpfb_logout');
18 window.location.reload();
19 });
20 }
21 }); 6 });
22 } 7 }
23 };
...\ No newline at end of file ...\ No newline at end of file
8 });
...\ No newline at end of file ...\ No newline at end of file
......