Facebook.php
3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* Note: If there is an inconsistent error
* it's due to how I changed the FB load
* process, may need to change how JS is loaded
* Proabably move FB.init and FB.Event.subscribe
* to my init method
*
* This needs to go in the <html tag
* xmlns:fb="http://www.facebook.com/2008/fbml"
*
* http://wpdev.tenzinghost.com
* API Key: 83f54e078b9aa0e303bba959dc0a566f
* App Secret: e542aca35ab698121fa5917211013a41
* App ID: 105917066126941
*
* http://wp.cb
* API Key: 3bcccfd8c28c52197141266d9e417649
* App Secret: 9bfcd828bc6ccef12336dea57df93ecb
* App ID: 138943536118944
*
* Graph API Reference:
* http://developers.facebook.com/docs/reference/api/user
*/
namespace Tz\WordPress\Tools\Auth\Facebook;
use Tz\WordPress\Tools;
use Tz\WordPress\Tools\Auth;
use InvalidArgumentException;
const OPTION_NAME = 'tz_auth_fb';
call_user_func(function() {
Vars::$options = new Tools\WP_Option(OPTION_NAME, Array('button_title' => 'Login'));
Tools\add_actions(__NAMESPACE__ . '\Actions');
Tools\add_shortcodes(__NAMESPACE__ . '\ShortCodes');
if (is_admin()) {
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Settings.php');
}
});
/**
* Generates markup for a Facebook Login button
* @param {Boolean} $echo TRUE to echo the button, false to return a string
* @returns NULL|String
*/
function drawLoginButton($echo = true) {
$btn = '<a id="TzFB" class="fb_button fb_button_medium"><span class="fb_button_text">' . Vars::$options['button_title'] ?: 'Login' . '</span></a>';
if (!$echo) {
return $btn;
}
echo $btn;
}
function loadSDK() {
static $loaded = false;
if ($loaded) {
return;
}
$loaded = true;
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'facebook-sdk.php');
Vars::$sdk = new \FB\Facebook(Array(
'appId' => Vars::$options['application_id']
, 'secret' => Vars::$options['application_secret']
, 'cookie' => true
));
}
class Actions {
/**
* Logs the user in to WP if they logged into FB
* @global $post
*/
public static function wp() {
global $post; // I want a better way to do this
if ($post->ID == Auth\Vars::$options['login_page'] && !is_user_logged_in()) {
loadSDK();
if (Vars::$sdk->getSession()) {
$info = Vars::$sdk->api('/me');
// get email, verify vs database
// register and/or login
}
}
}
/**
* Load the Facebook scripts for login
*/
public static function wp_enqueue_scripts() {
if (is_admin() || is_user_logged_in()) {
return;
}
_enqueue_script('facebook-all', 'http://connect.facebook.net/en_US/all.js');
_enqueue_script('tz-facebook', Tools\url('tz-facebook.js', __FILE__), Array('addEvent'));
_localize_script('tz-facebook', 'TzFBData', Array('AppID' => Vars::$options['application_id'], 'ext_perms' => implode(',', array_keys(Vars::$options['ext_perms']))));
}
/**
* Creates the anchor needed for Facebook scripts
*/
public static function get_footer() {
echo '<div id="fb-root"></div>';
}
/**
* Destroy Facebook session data on site if the log out of WordPress
*/
public static function wp_logout() {
loadSDK();
Vars::$sdk->setSession(); // I think this is how you log them out of Facebook
}
}
class ShortCodes {
public static function fb_login_button() {
if (Vars::$sdk->getSession()) {
ob_start();
print_r(Vars::$sdk->api('/me'));
$data = '<pre>' . ob_get_contents() . '</pre>';
ob_end_clean();
return $data;
} else {
return drawLoginButton(false);
}
}
}
class Vars {
/**
* WordPress option for this module
* @type WP_Option
*/
public static $options;
/**
* An instance of Facebook's PHP SDK
* @type Facebook
*/
public static $sdk;
}
?>