Added Cookie as external; updated FB a LOT, committing while working, lots of junk code with it
Showing
10 changed files
with
288 additions
and
137 deletions
| ... | @@ -4,19 +4,29 @@ namespace Tz\WordPress\Tools\Analytics; | ... | @@ -4,19 +4,29 @@ namespace Tz\WordPress\Tools\Analytics; |
| 4 | use Tz; | 4 | use Tz; |
| 5 | use Tz\WordPress\Tools; | 5 | use Tz\WordPress\Tools; |
| 6 | 6 | ||
| 7 | const VERSION = 1; | ||
| 7 | const OPTION_NAME = 'tz_analytics'; | 8 | const OPTION_NAME = 'tz_analytics'; |
| 9 | const OPTION_GROUP = 'reading'; | ||
| 10 | const OPTION_SECTION = 'tz_analytics_main'; | ||
| 8 | 11 | ||
| 9 | call_user_func(function() { | 12 | call_user_func(function() { |
| 10 | Vars::$options = new Tools\WP_Option(OPTION_NAME); | 13 | Vars::$options = new Tools\WP_Option(OPTION_NAME); |
| 11 | 14 | ||
| 12 | Tools\add_actions(__NAMESPACE__ . '\Actions'); | 15 | Tools\add_actions(__NAMESPACE__ . '\Actions'); |
| 13 | |||
| 14 | if (is_admin()) { | ||
| 15 | require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Settings.php'); | ||
| 16 | } | ||
| 17 | }); | 16 | }); |
| 18 | 17 | ||
| 18 | function validate($data) { | ||
| 19 | return $data; | ||
| 20 | } | ||
| 21 | |||
| 19 | class Actions { | 22 | class Actions { |
| 23 | public static function admin_init() { | ||
| 24 | register_setting(OPTION_GROUP, OPTION_NAME, __NAMESPACE__ . '\validate'); | ||
| 25 | add_settings_section(OPTION_SECTION, 'Google Analytics', function() {}, OPTION_GROUP); | ||
| 26 | |||
| 27 | Tools\add_settings_fields(__NAMESPACE__ . '\Fields', OPTION_GROUP, OPTION_SECTION); | ||
| 28 | } | ||
| 29 | |||
| 20 | public static function wp_print_scripts() { | 30 | public static function wp_print_scripts() { |
| 21 | if (Tz\LIVE !== 1 || empty(Vars::$options['api_key'])) { | 31 | if (Tz\LIVE !== 1 || empty(Vars::$options['api_key'])) { |
| 22 | return; | 32 | return; |
| ... | @@ -27,6 +37,8 @@ class Actions { | ... | @@ -27,6 +37,8 @@ class Actions { |
| 27 | _gaq.push(['_setAccount', '<?php echo Vars::$options['api_key']; ?>']); | 37 | _gaq.push(['_setAccount', '<?php echo Vars::$options['api_key']; ?>']); |
| 28 | _gaq.push(['_trackPageview']); | 38 | _gaq.push(['_trackPageview']); |
| 29 | 39 | ||
| 40 | // _gaq.push(['_trackEvent', 'download', 'Membership', 'sub category?']); | ||
| 41 | |||
| 30 | (function() { | 42 | (function() { |
| 31 | var ga = document.createElement('script'); ga.type = 'text/javascript'; | 43 | var ga = document.createElement('script'); ga.type = 'text/javascript'; |
| 32 | ga.async = true; | 44 | ga.async = true; |
| ... | @@ -38,6 +50,12 @@ class Actions { | ... | @@ -38,6 +50,12 @@ class Actions { |
| 38 | } | 50 | } |
| 39 | } | 51 | } |
| 40 | 52 | ||
| 53 | class Fields { | ||
| 54 | public static function api_key() { | ||
| 55 | echo '<input type="text" name="' . OPTION_NAME . '[' . __FUNCTION__ . ']" id="' . __FUNCTION__ . '" value="' . Vars::$options[__FUNCTION__] . '" />'; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 41 | class Vars { | 59 | class Vars { |
| 42 | public static $options; | 60 | public static $options; |
| 43 | } | 61 | } | ... | ... |
com/Analytics/Settings.php
deleted
100644 → 0
| 1 | <?php | ||
| 2 | |||
| 3 | namespace Tz\WordPress\Tools\Analytics\Settings; | ||
| 4 | |||
| 5 | use Tz\WordPress\Tools; | ||
| 6 | use Tz\WordPress\Tools\Analytics; | ||
| 7 | |||
| 8 | const OPTION_GROUP = 'tz_analytics_group'; | ||
| 9 | const OPTION_SECTION = 'tz_analytics_main'; | ||
| 10 | const ADMIN_PAGE = 'tz-tool-analytics'; | ||
| 11 | const CAPABILITY = 'configure_analytics'; | ||
| 12 | |||
| 13 | call_user_func(function() { | ||
| 14 | $role = get_role('administrator'); | ||
| 15 | $role->add_cap(CAPABILITY); | ||
| 16 | |||
| 17 | Tools\add_actions(__NAMESPACE__ . '\Actions'); | ||
| 18 | }); | ||
| 19 | |||
| 20 | function displayPage() { | ||
| 21 | require_once(__DIR__ . DIRECTORY_SEPARATOR . 'settings_view.php'); | ||
| 22 | } | ||
| 23 | |||
| 24 | function validate($data) { | ||
| 25 | return $data; | ||
| 26 | } | ||
| 27 | |||
| 28 | class Actions { | ||
| 29 | public static function admin_menu() { | ||
| 30 | add_options_page('Analytics', 'Analytics', CAPABILITY, ADMIN_PAGE, __NAMESPACE__ . '\displayPage'); | ||
| 31 | } | ||
| 32 | |||
| 33 | public static function admin_init() { | ||
| 34 | register_setting(OPTION_GROUP, Analytics\OPTION_NAME, __NAMESPACE__ . '\validate'); | ||
| 35 | add_settings_section(OPTION_SECTION, '', function() {}, ADMIN_PAGE); | ||
| 36 | |||
| 37 | Tools\add_settings_fields(__NAMESPACE__ . '\Fields', ADMIN_PAGE, OPTION_SECTION); | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | class Fields { | ||
| 42 | public static function api_key() { | ||
| 43 | echo '<input type="text" name="' . Analytics\OPTION_NAME . '[' . __FUNCTION__ . ']" id="' . __FUNCTION__ . '" value="' . Analytics\Vars::$options[__FUNCTION__] . '" />'; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | ?> | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
com/Analytics/settings_view.php
deleted
100644 → 0
| 1 | <?php | ||
| 2 | namespace Tz\WordPress\Tools\Analytics\Settings; | ||
| 3 | ?> | ||
| 4 | <div class="wrap"> | ||
| 5 | <?php screen_icon(); ?> | ||
| 6 | <h2>Analytics Settings</h2> | ||
| 7 | |||
| 8 | <form method="post" action="options.php"> | ||
| 9 | <?php | ||
| 10 | settings_fields(OPTION_GROUP); | ||
| 11 | do_settings_sections(ADMIN_PAGE); | ||
| 12 | ?> | ||
| 13 | <p class="submit"><input type="submit" class="button-primary" value="Save Changes" /></p> | ||
| 14 | </form> | ||
| 15 | </div> | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 1 | <?php | 1 | <?php |
| 2 | 2 | ||
| 3 | /* | ||
| 4 | * TODO: | ||
| 5 | * Test registration/activations system | ||
| 6 | * Test injection - none was done | ||
| 7 | * Possibly create hook for login page | ||
| 8 | */ | ||
| 9 | |||
| 3 | namespace Tz\WordPress\Tools\Auth; | 10 | namespace Tz\WordPress\Tools\Auth; |
| 4 | 11 | ||
| 5 | use Tz\WordPress\Tools; | 12 | use Tz\WordPress\Tools; |
| 6 | use Tz\Common; | 13 | use Tz\Common; |
| 7 | use Exception, LogicException, InvalidArgumentException, BadMethodCallException; | 14 | use Exception, LogicException, InvalidArgumentException, BadMethodCallException; |
| 8 | 15 | ||
| 9 | const REG_METH_AUTO_REG = 1; | 16 | const ACTION_CHECK_AUTH = 'check_auth'; |
| 10 | const REG_METH_VALID_EMAIL = 2; | ||
| 11 | 17 | ||
| 12 | const FORGOT_METH_VALID_EMAIL = 1; | 18 | // These are all WordPress hooks, I put them here for easy reference |
| 13 | const FORGOT_METH_RAND_PASS = 2; | 19 | const ACTION_LOGIN = 'wp_login'; |
| 20 | const ACTION_LOGOUT = 'wp_logout'; | ||
| 21 | const ACTION_ACTIVATE = 'user_register'; | ||
| 14 | 22 | ||
| 15 | // The things with these is they're dynamic but static functions aren't... | 23 | //const ACTION_REGISTER |
| 16 | const ACTION_ACTIVATE = 'auth_activate'; | ||
| 17 | 24 | ||
| 18 | const OPTION_NAME = 'tz_auth'; // Database lookup key (`wp_options`.`option_name`) | 25 | const OPTION_NAME = 'tz_auth'; // Database lookup key (`wp_options`.`option_name`) |
| 19 | 26 | ||
| 20 | call_user_func(function() { | 27 | call_user_func(function() { |
| 21 | Vars::$options = new Tools\WP_Option(OPTION_NAME); | 28 | Vars::$options = new Tools\WP_Option(OPTION_NAME); |
| 22 | 29 | ||
| 30 | Tools\add_actions(__NAMESPACE__ . '\Actions'); | ||
| 31 | |||
| 23 | if (is_admin()) { | 32 | if (is_admin()) { |
| 24 | require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Settings.php'); | 33 | require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Settings.php'); |
| 25 | } | 34 | } |
| ... | @@ -64,6 +73,29 @@ function login($username, $password, $remember = true) { | ... | @@ -64,6 +73,29 @@ function login($username, $password, $remember = true) { |
| 64 | } | 73 | } |
| 65 | 74 | ||
| 66 | /** | 75 | /** |
| 76 | * NOTE: Exerciese EXTREME caution!!! This automatically logs a user user without password verification!!! | ||
| 77 | * Intended use is for third party authentication | ||
| 78 | * @param {String} $username Username of the person to login as | ||
| 79 | * @param {Boolean} $remember Longer session | ||
| 80 | * @throws | ||
| 81 | * @returns {WP_User} of the newly authenticated user | ||
| 82 | */ | ||
| 83 | function signin($username, $remember = true) { | ||
| 84 | // What happens if someone is already signed on? Throw exception? | ||
| 85 | |||
| 86 | $user = get_user_by('login', $username); | ||
| 87 | if (false === $user) { | ||
| 88 | throw new Exception('Invalid username'); | ||
| 89 | } | ||
| 90 | |||
| 91 | _set_auth_cookie($user->ID, $remember); | ||
| 92 | _set_current_user($user->ID); | ||
| 93 | |||
| 94 | do_action('wp_login', $username); | ||
| 95 | return $user; | ||
| 96 | } | ||
| 97 | |||
| 98 | /** | ||
| 67 | * Attempts to log the user out | 99 | * Attempts to log the user out |
| 68 | * @returns Boolean | 100 | * @returns Boolean |
| 69 | * @throws LogicException If HTTP headers have already been sent | 101 | * @throws LogicException If HTTP headers have already been sent |
| ... | @@ -80,56 +112,116 @@ function logout() { | ... | @@ -80,56 +112,116 @@ function logout() { |
| 80 | 112 | ||
| 81 | /** | 113 | /** |
| 82 | * @param {Array} $user_data User data array, requires minimum (username, password, email) | 114 | * @param {Array} $user_data User data array, requires minimum (username, password, email) |
| 83 | * @param {Integer} $registration_method Method of registeration, see constants beginning with REG_METH | ||
| 84 | * @throws {InvalidArgumentException} If an invalid $registration_method is passed | 115 | * @throws {InvalidArgumentException} If an invalid $registration_method is passed |
| 85 | * @throw {BadMethodCallException} If any of the $user_data parameters are invalid | 116 | * @throw {BadMethodCallException} If any of the $user_data parameters are invalid |
| 86 | * @returns {Integer} New user $id if successful | 117 | * @returns {String} Unique key to activate the account |
| 87 | * @uses wp-includes/registration.php | 118 | * @uses wp-includes/registration.php |
| 119 | * @global $wpdb | ||
| 120 | * @see wpmu_signup_user | ||
| 88 | */ | 121 | */ |
| 89 | function register($user_data = Array(), $registration_method = 1) { | 122 | function register($username, $email, $password, $meta = Array()) { |
| 90 | if (!in_array($registration_method, Array(REG_METH_AUTO_REG, REG_METH_VALID_EMAIL))) { | ||
| 91 | throw new InvalidArgumentException("Invalid registration method selected"); | ||
| 92 | } | ||
| 93 | |||
| 94 | require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'registration.php'); | 123 | require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'registration.php'); |
| 95 | 124 | ||
| 125 | $user_data = Array( | ||
| 126 | 'username' => $username | ||
| 127 | , 'password' => $password | ||
| 128 | , 'email' => $email | ||
| 129 | ); | ||
| 130 | $meta['password'] = $password; | ||
| 131 | // array_filter($user_data, 'esc_sql'); | ||
| 132 | |||
| 96 | $valid = new Validation($user_data); | 133 | $valid = new Validation($user_data); |
| 97 | if (count($valid->errors) > 0) { | 134 | if (count($valid->errors) > 0) { |
| 98 | throw new BadMethodCallException(implode("\n", $valid->errors)); | 135 | throw new BadMethodCallException(implode("\n", $valid->errors)); |
| 99 | } | 136 | } |
| 100 | 137 | ||
| 101 | array_filter($user_data, 'esc_sql'); | 138 | global $wpdb; |
| 102 | // $key = substr( md5( time() . rand() . $user_email ), 0, 16 ); | ||
| 103 | 139 | ||
| 104 | // possibly call wpmu_signup_user() if REG_METH_VALID_EMAIL; _insert_user if REG_METH_AUTO_REG | 140 | $username = preg_replace( '/\s+/', '', sanitize_user($username, true)); |
| 105 | // Can't do that without making a database call; the unique registration key is created and destroyed in the function | 141 | $email = sanitize_email($email); |
| 106 | // I'll have to make a database call to retreive it, at the very lest | 142 | $key = substr(md5(time() . rand() . $email ), 0, 16); |
| 107 | // I can't do that at all; the function sends an email to the user with a auto-generated password | 143 | $meta = serialize($meta); |
| 108 | // I'll have to do database manipulation manually | 144 | |
| 109 | $id = (int)_insert_user($user_data); | 145 | $wpdb->insert($wpdb->signups, Array( |
| 146 | 'domain' => '', | ||
| 147 | 'path' => '', | ||
| 148 | 'title' => '', | ||
| 149 | 'user_login' => $username, | ||
| 150 | 'user_email' => $email, | ||
| 151 | 'registered' => current_time('mysql', true), | ||
| 152 | 'activation_key' => $key, | ||
| 153 | 'meta' => $meta | ||
| 154 | )); | ||
| 110 | 155 | ||
| 111 | // should I call ACTION_ACTIVATE if REG_METHOD_AUTO_REG? | 156 | return $key; |
| 157 | } | ||
| 112 | 158 | ||
| 113 | // this is so wrong | 159 | /** |
| 160 | * @param {String} $key Unique key to activate account | ||
| 161 | * @global $wpdb | ||
| 162 | * @see wpmu_activate_signup | ||
| 163 | */ | ||
| 164 | function activate($key) { | ||
| 114 | global $wpdb; | 165 | global $wpdb; |
| 115 | $wpdb->query("UPDATE `{$wpdb->users}` SET `user_status` = 1 WHERE `ID` = {$id}"); | 166 | $signup = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->signups} WHERE activation_key = %s", $key)); |
| 116 | 167 | ||
| 117 | return $id; | 168 | if (empty($signup)) { |
| 118 | } | 169 | throw new Exception("{$key} is not a valid registration key"); |
| 170 | } | ||
| 171 | if ($signup->active) { | ||
| 172 | throw new Exception('Account has already been activated'); | ||
| 173 | } | ||
| 174 | |||
| 175 | // Do I need to do another username_exists() call? | ||
| 176 | // Can 2 users put the same username in the signup table at the same time? | ||
| 177 | |||
| 178 | // Do I need to re-sanatize this? | ||
| 179 | $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); | ||
| 189 | unset($meta['password']); | ||
| 190 | |||
| 191 | if (!$id) { | ||
| 192 | throw new Exception('Unable to create user'); | ||
| 193 | } | ||
| 194 | |||
| 195 | /* Add the user to the appropriate blog | ||
| 196 | $now = current_time('mysql', true); | ||
| 197 | $wpdb->update($wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key)); | ||
| 119 | 198 | ||
| 120 | // Don't think I need $username | 199 | global $current_site; |
| 121 | function activate($username, $activation_key) { | 200 | $user_site = get_site_option( 'dashboard_blog', $current_site->blog_id ); |
| 122 | // wpmu_activate_signup | ||
| 123 | // I can't do that either; that function sends a WordPress email | ||
| 124 | 201 | ||
| 125 | do_action(ACTION_ACTIVATE, $user_id); | 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 | |||
| 211 | return (int)$id; | ||
| 126 | } | 212 | } |
| 127 | 213 | ||
| 128 | // Not sure I need this function | 214 | class Actions { |
| 129 | // Application can just set rand password | 215 | /* |
| 130 | // Or perhapds I do need it, move it to registered again or something??? | 216 | // I forget why I chose wp() instead of set_current_user()... |
| 131 | function forgot_password($username, $forgot_method) { | 217 | public static function wp() { |
| 218 | global $post; // I want a better way to do this | ||
| 132 | 219 | ||
| 220 | if ($post->ID == Vars::$options['login_page'] && !is_user_logged_in()) { | ||
| 221 | do_action(ACTION_CHECK_AUTH); | ||
| 222 | } | ||
| 223 | } | ||
| 224 | */ | ||
| 133 | } | 225 | } |
| 134 | 226 | ||
| 135 | class Validation extends Common\Validation { | 227 | class Validation extends Common\Validation { | ... | ... |
| ... | @@ -30,10 +30,14 @@ use Tz\WordPress\Tools\Auth; | ... | @@ -30,10 +30,14 @@ use Tz\WordPress\Tools\Auth; |
| 30 | 30 | ||
| 31 | use FB; | 31 | use FB; |
| 32 | 32 | ||
| 33 | use Exception; | ||
| 34 | |||
| 33 | use InvalidArgumentException; | 35 | use InvalidArgumentException; |
| 34 | 36 | ||
| 35 | const OPTION_NAME = 'tz_auth_fb'; | 37 | const OPTION_NAME = 'tz_auth_fb'; |
| 36 | 38 | ||
| 39 | //setcookie('wpfb_logout', '', time() - 3600, '/'); | ||
| 40 | |||
| 37 | call_user_func(function() { | 41 | call_user_func(function() { |
| 38 | 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')); |
| 39 | 43 | ||
| ... | @@ -53,6 +57,7 @@ const OPTION_NAME = 'tz_auth_fb'; | ... | @@ -53,6 +57,7 @@ const OPTION_NAME = 'tz_auth_fb'; |
| 53 | function drawLoginButton($echo = true) { | 57 | function drawLoginButton($echo = true) { |
| 54 | $title = Vars::$options['button_title'] ?: 'Login'; | 58 | $title = Vars::$options['button_title'] ?: 'Login'; |
| 55 | $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>'; | ||
| 56 | 61 | ||
| 57 | if (!$echo) { | 62 | if (!$echo) { |
| 58 | return $btn; | 63 | return $btn; |
| ... | @@ -70,12 +75,51 @@ function getSDK() { | ... | @@ -70,12 +75,51 @@ function getSDK() { |
| 70 | 'appId' => Vars::$options['application_id'] | 75 | 'appId' => Vars::$options['application_id'] |
| 71 | , 'secret' => Vars::$options['application_secret'] | 76 | , 'secret' => Vars::$options['application_secret'] |
| 72 | , 'cookie' => true | 77 | , 'cookie' => true |
| 78 | , 'domain' => Vars::$options['domain_name'] | ||
| 73 | )); | 79 | )); |
| 74 | } | 80 | } |
| 75 | 81 | ||
| 76 | return $instance; | 82 | return $instance; |
| 77 | } | 83 | } |
| 78 | 84 | ||
| 85 | function load() { | ||
| 86 | ?> | ||
| 87 | <div id="fb-root"></div> | ||
| 88 | <script> | ||
| 89 | window.fbAsyncInit = function() { | ||
| 90 | FB.init({appId: '<?php echo Vars::$options['application_id']; ?>', status: true, cookie: true, xfbml: true}); | ||
| 91 | |||
| 92 | FB.getLoginStatus(function(response) { | ||
| 93 | console.log(response.status); | ||
| 94 | }); | ||
| 95 | |||
| 96 | FB.Event.subscribe('auth.login', function(response) { window.location.reload(); }); | ||
| 97 | |||
| 98 | <?php if (isset($_COOKIE['wpfb_logout'])): ?> | ||
| 99 | FB.getLoginStatus(function(response) { | ||
| 100 | if (response.session) { | ||
| 101 | FB.logout(function() { | ||
| 102 | var date = new Date(); | ||
| 103 | date.setTime(date.getTime() - 1); | ||
| 104 | document.cookie = 'wpfb_logout=;expires=' + date.toGMTString() + ';path=/'; | ||
| 105 | |||
| 106 | // window.location.reload(); | ||
| 107 | }); | ||
| 108 | } | ||
| 109 | }); | ||
| 110 | <?php endif; ?> | ||
| 111 | }; | ||
| 112 | |||
| 113 | (function() { | ||
| 114 | var e = document.createElement('script'); | ||
| 115 | e.async = true; | ||
| 116 | e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js'; | ||
| 117 | document.getElementById('fb-root').appendChild(e); | ||
| 118 | }()); | ||
| 119 | </script> | ||
| 120 | <?php | ||
| 121 | } | ||
| 122 | |||
| 79 | /* | 123 | /* |
| 80 | * Logic for all these methods needs to be re-thought out | 124 | * Logic for all these methods needs to be re-thought out |
| 81 | * Should only load FB stuff when something happens (decide) | 125 | * Should only load FB stuff when something happens (decide) |
| ... | @@ -88,38 +132,69 @@ class Actions { | ... | @@ -88,38 +132,69 @@ class Actions { |
| 88 | * Logs the user in to WP if they logged into FB | 132 | * Logs the user in to WP if they logged into FB |
| 89 | * @global $post | 133 | * @global $post |
| 90 | */ | 134 | */ |
| 91 | public static function wp() { | 135 | public static function set_current_user() { |
| 92 | global $post; // I want a better way to do this | ||
| 93 | |||
| 94 | if ($post->ID == Auth\Vars::$options['login_page'] && !is_user_logged_in()) { | ||
| 95 | $sdk = getSDK(); | 136 | $sdk = getSDK(); |
| 137 | if (null === ($sess = $sdk->getSession())) { | ||
| 138 | return; | ||
| 139 | } | ||
| 96 | 140 | ||
| 97 | if ($sdk->getSession()) { | 141 | if (isset($_COOKIE['wpfb_logout'])) { |
| 142 | $sdk->setSession(); | ||
| 143 | return; | ||
| 144 | } | ||
| 145 | |||
| 146 | // if user is not logged in do the following | ||
| 147 | // if user is logged in merge account? do checks? | ||
| 148 | |||
| 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 { | ||
| 98 | $info = $sdk->api('/me'); | 154 | $info = $sdk->api('/me'); |
| 99 | // get email, verify vs database | 155 | $username = 'fbc' . $sess['uid']; |
| 100 | // register and/or login | 156 | } catch (FB\FacebookApiException $e) { |
| 157 | return; | ||
| 101 | } | 158 | } |
| 159 | |||
| 160 | require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'registration.php'); | ||
| 161 | if (username_exists($username)) { | ||
| 162 | $user = Auth\signin($username); | ||
| 163 | } else { | ||
| 164 | if (false !== get_user_by('email', $info['email'])) { | ||
| 165 | // Not sure if I can throw exception, this is outside the theme stuff... | ||
| 166 | throw new Exception('email conflict'); | ||
| 167 | } | ||
| 168 | |||
| 169 | try { | ||
| 170 | $key = Auth\register($username, $info['email'], _generate_password()); | ||
| 171 | $id = Auth\activate($key); | ||
| 172 | $user = Auth\signin($username); | ||
| 173 | } catch (Exception $e) { | ||
| 174 | // many types of exceptions | ||
| 175 | } | ||
| 176 | } | ||
| 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]); | ||
| 102 | } | 181 | } |
| 103 | } | 182 | } |
| 104 | 183 | ||
| 105 | /** | 184 | /** |
| 106 | * Load the Facebook scripts for login | 185 | * Load the Facebook scripts for login |
| 107 | */ | 186 | */ |
| 108 | public static function wp_enqueue_scripts() { | 187 | public static function OFF_wp_enqueue_scripts() { |
| 109 | if (is_admin() || is_user_logged_in()) { | ||
| 110 | return; | ||
| 111 | } | ||
| 112 | |||
| 113 | _enqueue_script('facebook-all', 'http://connect.facebook.net/en_US/all.js'); | 188 | _enqueue_script('facebook-all', 'http://connect.facebook.net/en_US/all.js'); |
| 114 | _enqueue_script('tz-facebook', Tools\url('tz-facebook.js', __FILE__), Array('addEvent')); | 189 | _enqueue_script('tz-facebook', Tools\url('tz-facebook.js', __FILE__), Array('addEvent', 'Cookie')); |
| 115 | 190 | ||
| 116 | _localize_script('tz-facebook', 'TzFBData', Array('AppID' => Vars::$options['application_id'], 'ext_perms' => implode(',', array_keys(Vars::$options['ext_perms'])))); | 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']))); |
| 117 | } | 192 | } |
| 118 | 193 | ||
| 119 | /** | 194 | /** |
| 120 | * Creates the anchor needed for Facebook scripts | 195 | * Creates the anchor needed for Facebook scripts |
| 121 | */ | 196 | */ |
| 122 | public static function get_footer() { | 197 | public static function OFF_get_footer() { |
| 123 | echo '<div id="fb-root"></div>'; | 198 | echo '<div id="fb-root"></div>'; |
| 124 | } | 199 | } |
| 125 | 200 | ||
| ... | @@ -127,21 +202,28 @@ class Actions { | ... | @@ -127,21 +202,28 @@ class Actions { |
| 127 | * Destroy Facebook session data on site if the log out of WordPress | 202 | * Destroy Facebook session data on site if the log out of WordPress |
| 128 | */ | 203 | */ |
| 129 | public static function wp_logout() { | 204 | public static function wp_logout() { |
| 130 | $sdk = getSDK(); | 205 | setcookie('wpfb_logout', 1, 0, '/', Vars::$options['domain_name']); |
| 131 | $sdk->setSession(); // I think this is how you log them out of Facebook | ||
| 132 | } | 206 | } |
| 133 | } | 207 | } |
| 134 | 208 | ||
| 135 | class ShortCodes { | 209 | class ShortCodes { |
| 136 | public static function fb_login_button() { | 210 | public static function fb_login_button() { |
| 211 | /* | ||
| 137 | if (is_user_logged_in()) { | 212 | if (is_user_logged_in()) { |
| 138 | return ''; | 213 | return ''; |
| 139 | } | 214 | } |
| 215 | */ | ||
| 140 | 216 | ||
| 141 | $sdk = getSDK(); | 217 | $sdk = getSDK(); |
| 142 | if ($sdk->getSession()) { | 218 | if ($sdk->getSession()) { |
| 143 | ob_start(); | 219 | ob_start(); |
| 220 | print_r($sdk->getSession()); | ||
| 221 | print_r($_COOKIE); | ||
| 222 | try { | ||
| 144 | print_r($sdk->api('/me')); | 223 | print_r($sdk->api('/me')); |
| 224 | } catch (Exception $e) { | ||
| 225 | die('fuck'); | ||
| 226 | } | ||
| 145 | $data = '<pre>' . ob_get_contents() . '</pre>'; | 227 | $data = '<pre>' . ob_get_contents() . '</pre>'; |
| 146 | ob_end_clean(); | 228 | ob_end_clean(); |
| 147 | 229 | ... | ... |
| ... | @@ -51,6 +51,10 @@ class Cred_Fields { | ... | @@ -51,6 +51,10 @@ class Cred_Fields { |
| 51 | public static function button_title() { | 51 | public static function button_title() { |
| 52 | echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Facebook\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Facebook\Vars::$options[__FUNCTION__] . '" />'; | 52 | echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Facebook\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Facebook\Vars::$options[__FUNCTION__] . '" />'; |
| 53 | } | 53 | } |
| 54 | |||
| 55 | public static function domain() { | ||
| 56 | echo '<input type="text" id="' . __FUNCTION__ . '" name="' . Facebook\OPTION_NAME . '[' . __FUNCTION__ . ']" value="' . Facebook\Vars::$options[__FUNCTION__] . '" />'; | ||
| 57 | } | ||
| 54 | } | 58 | } |
| 55 | 59 | ||
| 56 | class Opt_Fields { | 60 | class Opt_Fields { | ... | ... |
| 1 | <?php | 1 | <?php |
| 2 | 2 | ||
| 3 | namespace FB; | 3 | namespace FB; |
| 4 | use Exception; | 4 | use Exception, OAuthException; |
| 5 | 5 | ||
| 6 | if (!function_exists('curl_init')) { | 6 | if (!function_exists('curl_init')) { |
| 7 | throw new Exception('Facebook needs the CURL PHP extension.'); | 7 | throw new Exception('Facebook needs the CURL PHP extension.'); |
| ... | @@ -447,12 +447,15 @@ class Facebook | ... | @@ -447,12 +447,15 @@ class Facebook |
| 447 | 447 | ||
| 448 | // results are returned, errors are thrown | 448 | // results are returned, errors are thrown |
| 449 | if (is_array($result) && isset($result['error'])) { | 449 | if (is_array($result) && isset($result['error'])) { |
| 450 | $e = new FacebookApiException($result); | 450 | try { |
| 451 | throw new FacebookApiException($result); | ||
| 452 | } catch (FacebookApiException $e) { | ||
| 451 | if ($e->getType() === 'OAuthException') { | 453 | if ($e->getType() === 'OAuthException') { |
| 452 | $this->setSession(null); | 454 | $this->setSession(null); |
| 453 | } | 455 | } |
| 454 | throw $e; | 456 | throw $e; |
| 455 | } | 457 | } |
| 458 | } | ||
| 456 | return $result; | 459 | return $result; |
| 457 | } | 460 | } |
| 458 | 461 | ||
| ... | @@ -505,16 +508,18 @@ class Facebook | ... | @@ -505,16 +508,18 @@ class Facebook |
| 505 | curl_setopt_array($ch, $opts); | 508 | curl_setopt_array($ch, $opts); |
| 506 | $result = curl_exec($ch); | 509 | $result = curl_exec($ch); |
| 507 | if ($result === false) { | 510 | if ($result === false) { |
| 508 | $e = new FacebookApiException(array( | 511 | try { |
| 512 | throw new FacebookApiException(array( | ||
| 509 | 'error_code' => curl_errno($ch), | 513 | 'error_code' => curl_errno($ch), |
| 510 | 'error' => array( | 514 | 'error' => array( |
| 511 | 'message' => curl_error($ch), | 515 | 'message' => curl_error($ch), |
| 512 | 'type' => 'CurlException', | 516 | 'type' => 'CurlException', |
| 513 | ), | 517 | ))); |
| 514 | )); | 518 | } catch (Exception $e) { |
| 515 | curl_close($ch); | 519 | curl_close($ch); |
| 516 | throw $e; | 520 | throw $e; |
| 517 | } | 521 | } |
| 522 | } | ||
| 518 | curl_close($ch); | 523 | curl_close($ch); |
| 519 | return $result; | 524 | return $result; |
| 520 | } | 525 | } | ... | ... |
| 1 | window.fbAsyncInit = function() { | 1 | window.fbAsyncInit = function() { |
| 2 | FB.init({appId: TzFBData.AppID, status: true, cookie: true, xfbml: true}); | 2 | FB.init({appId: TzFBData.AppID, status: true, cookie: true, xfbml: true}); |
| 3 | FB.Event.subscribe('auth.login', function(response) { window.location.reload(); }); | ||
| 4 | }; | ||
| 5 | 3 | ||
| 6 | var TzFB = function() { | 4 | FB.Event.subscribe('auth.login', function(response) { console.log('login called'); window.location.href = TzFBData.loginPage; }); |
| 7 | var init = function() { | 5 | |
| 8 | var oBtn = document.getElementById('TzFB'); | 6 | var oBtn = document.getElementById('TzFB'); |
| 9 | if (oBtn) { | 7 | if (oBtn) { |
| 10 | addEvent(oBtn, 'click', api.login); | 8 | addEvent(oBtn, 'click', FB.login); |
| 11 | } | ||
| 12 | } | ||
| 13 | |||
| 14 | var api = { | ||
| 15 | login: function() { | ||
| 16 | FB.login(function() {}, {perms: TzFBData.ext_perms}); | ||
| 17 | } | 9 | } |
| 18 | }; | ||
| 19 | 10 | ||
| 11 | if (Cookie.read('wpfb_logout')) { | ||
| 12 | Cookie.erase('wpfb_logout'); | ||
| 20 | 13 | ||
| 21 | addEvent(window, 'load', init); | ||
| 22 | return api; | ||
| 23 | }(); | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 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 | }); | ||
| 22 | } | ||
| 23 | }; | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| ... | @@ -20,6 +20,7 @@ use Exception; | ... | @@ -20,6 +20,7 @@ use Exception; |
| 20 | _register_script('addEvent', url('scripts/addEvent.js', __FILE__)); | 20 | _register_script('addEvent', url('scripts/addEvent.js', __FILE__)); |
| 21 | _register_script('xmlhttpHandler', url('scripts/xmlhttpHandler.js', __FILE__)); | 21 | _register_script('xmlhttpHandler', url('scripts/xmlhttpHandler.js', __FILE__)); |
| 22 | _register_script('fireEvent', url('scripts/fireEvent.js', __FILE__)); | 22 | _register_script('fireEvent', url('scripts/fireEvent.js', __FILE__)); |
| 23 | _register_script('Cookie', url('scripts/Cookie/Cookie.js', __FILE__)); | ||
| 23 | 24 | ||
| 24 | import('ShortCodes'); | 25 | import('ShortCodes'); |
| 25 | if (defined('Tz\DEBUG') && Tz\DEBUG === true) { | 26 | if (defined('Tz\DEBUG') && Tz\DEBUG === true) { | ... | ... |
| ... | @@ -165,4 +165,14 @@ function _logout_url() { | ... | @@ -165,4 +165,14 @@ function _logout_url() { |
| 165 | $params = func_get_args(); | 165 | $params = func_get_args(); |
| 166 | return call_user_func_array('wp' . __FUNCTION__, $params); | 166 | return call_user_func_array('wp' . __FUNCTION__, $params); |
| 167 | } | 167 | } |
| 168 | |||
| 169 | function _set_auth_cookie() { | ||
| 170 | $params = func_get_args(); | ||
| 171 | return call_user_func_array('wp' . __FUNCTION__, $params); | ||
| 172 | } | ||
| 173 | |||
| 174 | function _create_user() { | ||
| 175 | $params = func_get_args(); | ||
| 176 | return call_user_func_array('wpmu' . __FUNCTION__, $params); | ||
| 177 | } | ||
| 168 | ?> | 178 | ?> |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or sign in to post a comment