Created
August 22, 2014 14:42
-
-
Save kingkool68/71b81ccf96979f3b4bb3 to your computer and use it in GitHub Desktop.
Makes it easy for your site to use Facebook Connect, Google Connect, in a wholly modular way. Based on Simple Facebook Connect (http://ottopress.com/wordpress-plugins/simple-facebook-connect/) by Otto
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Base | |
* | |
*/ | |
// basic XFBML load into footer | |
function psc_fb_load_args() { | |
/* | |
global $sfc_comm_comments_form; | |
if ($sfc_comm_comments_form != true) { | |
return; // nothing to do, not showing comments | |
} | |
*/ | |
if( comments_open() && !is_user_logged_in() ) { | |
$options = get_option('psc_options'); | |
$defaults = array( | |
'appId' => $options['fb_appid'], | |
'channelUrl' => home_url('fb-channel-file/'), | |
'status' => true, | |
'cookie' => true, | |
'xfbml' => false, | |
'oauth' => true, | |
); | |
$args = apply_filters( 'psc_fb_args', $defaults ); | |
$args = wp_parse_args($args, $defaults); | |
?> | |
var fbArgs = <?=json_encode($args);?> | |
<?php | |
} | |
}; | |
add_action('psc_footer_script','psc_fb_load_args', 20); // 20, to put it at the end of the footer insertions. sub-plugins should use 30 for their code | |
function psc_channel_file() { | |
global $wp; | |
$options = get_option('psc_options'); | |
$wp->add_query_var('fb-channel-file'); | |
add_rewrite_rule('fb-channel-file' . '?$', 'index.php?fb-channel-file=1', 'top'); | |
} | |
add_action('init','psc_channel_file'); | |
function psc_fb_channel_file_catcher() { | |
if ( get_query_var('fb-channel-file') == 1 ) { | |
$cache_expire = 60*60*24*365; | |
header("Pragma: public"); | |
header("Cache-Control: max-age=".$cache_expire); | |
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $cache_expire) . ' GMT'); | |
echo '<script src="//connect.facebook.net/en_US/all.js"></script>'; | |
exit; | |
} | |
} | |
add_action('template_redirect','psc_fb_channel_file_catcher'); | |
// the cookie is signed using our application secret, so it's unfakable as long as you don't give away the secret | |
function psc_fb_cookie_parse() { | |
$options = get_option('psc_options'); | |
$cookie = $_COOKIE['fbsr_' . $options['fb_appid']]; | |
$args = array(); | |
if ( !empty( $cookie ) ) { | |
if ( list($encoded_sig, $payload ) = explode('.', $cookie, 2) ) { | |
$sig = psc_base64_url_decode($encoded_sig); | |
if (hash_hmac('sha256', $payload, $options['fb_app_secret'], true) == $sig) { | |
$args = json_decode(psc_base64_url_decode($payload), true); | |
} | |
} | |
} | |
return $args; | |
} | |
// this is not a hack or a dangerous function.. the base64 decode is required because Facebook is sending back base64 encoded data in the signed_request bits. | |
// See http://developers.facebook.com/docs/authentication/signed_request/ for more info | |
function psc_base64_url_decode($input) { | |
return base64_decode( strtr($input, '-_', '+/') ); | |
} | |
function psc_fb_remote($obj, $connection='', $args=array(), $type = 'GET') { | |
// save the access tokens for later use in the same request | |
static $saved_access_tokens; | |
if ( empty($args['access_token']) && isset($saved_access_tokens[$obj]) && $saved_access_tokens[$obj] = $obj ) { | |
$args['access_token'] = $saved_access_tokens[$obj]; | |
} | |
$options = get_option('psc_options'); | |
// get the access token | |
if ( empty($args['access_token']) && !empty($args['code']) ) { | |
$resp = wp_remote_get("https://graph.facebook.com/oauth/access_token?client_id={$options['fb_appid']}&redirect_uri=&client_secret={$options['fb_app_secret']}&code={$args['code']}"); | |
if ( !is_wp_error($resp) && wp_remote_retrieve_response_code( $resp ) == 200 ) { | |
$args['access_token'] = str_replace( 'access_token=', '', $resp['body'] ); | |
$saved_access_tokens[$obj] = $args['access_token']; | |
} else { | |
return false; | |
} | |
} | |
$type = strtoupper($type); | |
if ( empty($obj) ) { | |
return null; | |
} | |
$url = 'https://graph.facebook.com/' . $obj; | |
if ( !empty($connection) ) { | |
$url .= '/' . $connection; | |
} | |
if ($type == 'GET') { | |
$url .= '?' . http_build_query($args); | |
} | |
$args['sslverify'] = 0; | |
if ($type == 'POST') { | |
$data = wp_remote_post($url, $args); | |
} else if ($type == 'GET') { | |
$data = wp_remote_get($url, $args); | |
} | |
if ( $data && !is_wp_error($data) ) { | |
$resp = json_decode($data['body'], true); | |
return $resp; | |
} | |
return false; | |
} | |
// finds a item from an array in a string | |
if ( !function_exists('straipos') ) : | |
function straipos($haystack, $array, $offset=0) { | |
$occ = array(); | |
for ($i = 0; $i<sizeof($array); $i++) { | |
$pos = strpos($haystack, $array[$i], $offset); | |
if ( is_bool($pos) ) continue; | |
$occ[$pos] = $i; | |
} | |
if ( sizeof($occ) < 1 ) { return false; } | |
ksort($occ); | |
reset($occ); | |
list($key, $value) = each($occ); | |
return array($key, $value); | |
} | |
endif; | |
/* | |
* Facebook Comments (sfc-comments.php) | |
* | |
*/ | |
function psc_fb_button() { | |
?> | |
<div id="facebook-connect" class="button"> | |
<p><a href="#"><span>Log In with </span>Facebook</a></p> | |
</div> | |
<?php | |
} | |
add_action('psc_add_button', 'psc_fb_button'); | |
// generate facebook avatar code for FB user comments | |
function psc_fb_avatar($avatar, $id_or_email, $size = '96', $default = '', $alt = false) { | |
// check to be sure this is for a comment | |
if ( !is_object($id_or_email) || !isset($id_or_email->comment_ID) || $id_or_email->user_id) { | |
return $avatar; | |
} | |
// check for fbuid comment meta | |
$fbuid = get_comment_meta($id_or_email->comment_ID, 'fbuid', true); | |
if ($fbuid) { | |
// return the avatar code | |
return "<img width='{$size}' height='{$size}' class='avatar avatar-{$size} fbavatar' src='https://graph.facebook.com/{$fbuid}/picture?width={$size}&height={$size}' />"; | |
} | |
return $avatar; | |
} | |
add_filter('get_avatar','psc_fb_avatar', 10, 5); | |
// Injects the data from Facebook into the $_POST object sent to the server. | |
function psc_fb_inject_comment_data($comment_post_ID) { | |
if ( is_user_logged_in() ) { | |
return; // do nothing to WP users | |
} | |
$uid = $_POST['psc_fb_user_id']; | |
$token = $_POST['psc_fb_token']; | |
if ( empty($uid) || empty($token) ) { | |
return; // need both of these to get the data from FB | |
} | |
$url = "https://graph.facebook.com/{$uid}/?fields=name,email&access_token={$token}"; | |
$data = wp_remote_get( $url, array('sslverify' => 0) ); | |
if ( !is_wp_error($data) ) { | |
$json = json_decode($data['body'], true); | |
if ($json) { | |
$json = apply_filters('psc_fb_user_data', $json, $uid); | |
//$_POST['author'] = $json['name']; | |
$_POST['url'] = "http://www.facebook.com/profile.php?id={$uid}"; //Even though we're not displaying the URL publically we can use this field to query how many commenters are logging in using Facebook. | |
$_POST['email'] = $json['email']; | |
} | |
} | |
} | |
add_filter('pre_comment_on_post','psc_fb_inject_comment_data'); | |
// store the FB user ID as comment meta data ('fbuid') | |
function psc_fb_store_metadata($comment_id) { | |
$uid = $_POST['psc_fb_user_id']; | |
$token = $_POST['psc_fb_token']; | |
if ( !empty($uid) && !empty($token) ) { | |
// validate token | |
$url = "https://graph.facebook.com/{$uid}/?fields=name,email&access_token={$token}"; | |
$data = wp_remote_get( $url, array('sslverify'=>0) ); | |
if ( !is_wp_error($data) ) { | |
$json = json_decode( $data['body'], true ); | |
if ( !empty( $json['name'] ) ) { | |
update_comment_meta($comment_id, 'fbuid', $uid); | |
} | |
} | |
} | |
} | |
add_action('comment_post','psc_fb_store_metadata', 10, 1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Base | |
* | |
*/ | |
function sgc_oauth_request_link($scope, $action='', $forceprompt=false) { | |
$options = get_option('psc_options'); | |
$args['response_type'] = 'code'; | |
$args['client_id'] = $options['goog_appid']; | |
$args['redirect_uri'] = home_url( $options['goog_oauthcallback'] ); | |
$args['scope'] = $scope; | |
if ($forceprompt) { | |
$args['approval_prompt'] = 'force'; | |
$args['access_type'] = 'offline'; | |
} else { | |
$args['approval_prompt'] = 'auto'; | |
$args['access_type'] = 'online'; | |
} | |
if ( !empty($action) ) { | |
$args['state'] = $action; | |
} | |
$auth = 'https://accounts.google.com/o/oauth2/auth?' . http_build_query($args); | |
return $auth; | |
} | |
function sgc_get_userinfo($token) { | |
$headers['Authorization'] = 'Bearer '.$token; | |
$request['headers'] = $headers; | |
$request['sslverify'] = false; | |
$data = wp_remote_get('https://www.googleapis.com/oauth2/v1/userinfo', $request); | |
if ( is_wp_error( $data ) || 200 != wp_remote_retrieve_response_code( $data ) ) | |
return false; | |
$resp = json_decode( wp_remote_retrieve_body( $data ), true ); | |
return $resp; | |
} | |
function psc_goog_add_rewrite() { | |
global $wp; | |
$options = get_option('psc_options'); | |
$wp->add_query_var($options['goog_oauthcallback']); | |
add_rewrite_rule($options['goog_oauthcallback'] . '?$', 'index.php?oauth2callback=1', 'top'); | |
} | |
add_action('init','psc_goog_add_rewrite'); | |
function psc_goog_oauth_catcher() { | |
if ( get_query_var('oauth2callback') == 1 ) { | |
$oauth = array(); | |
if ( !empty( $_REQUEST['code'] ) ) { | |
$oauth['code'] = $_REQUEST['code']; | |
$oauth['token'] = psc_get_goog_token( $oauth['code'] ); | |
} | |
else if ( !empty($_REQUEST['error']) ) { | |
$oauth['error'] = $_REQUEST['error']; | |
} | |
if ( !empty( $_REQUEST['state'] ) ) | |
do_action('psc_goog_state_' . $_REQUEST['state'], $oauth ); | |
// if we made it here, then the action didn't do anything so redirect to the home page | |
wp_redirect( home_url() ); | |
} | |
} | |
add_action('template_redirect','psc_goog_oauth_catcher'); | |
function psc_get_goog_token($code, $refresh=false) { | |
$options = get_option('psc_options'); | |
if ($refresh) { | |
$req['refresh_token'] = $code; | |
} else { | |
$req['code'] = $code; | |
} | |
$req['client_id'] = $options['goog_appid']; | |
$req['client_secret'] = $options['goog_app_secret']; | |
if (!$refresh) { | |
$req['redirect_uri'] = home_url( $options['goog_oauthcallback'] ); | |
} | |
if ($refresh) { | |
$req['grant_type'] = 'refresh_token'; | |
} else { | |
$req['grant_type'] = 'authorization_code'; | |
} | |
$args['sslverify'] = false; | |
$args['body'] = $req; | |
$data = wp_remote_post('https://accounts.google.com/o/oauth2/token', $args); | |
if ( is_wp_error( $data ) || wp_remote_retrieve_response_code( $data ) != 200 ) { | |
return false; | |
} | |
$resp = json_decode( wp_remote_retrieve_body( $data ), true ); | |
return $resp; | |
} | |
function psc_get_goog_userinfo($token) { | |
$headers['Authorization'] = 'Bearer '.$token; | |
$request['headers'] = $headers; | |
$request['sslverify'] = false; | |
$data = wp_remote_get('https://www.googleapis.com/oauth2/v1/userinfo', $request); | |
if ( is_wp_error( $data ) || 200 != wp_remote_retrieve_response_code( $data ) ) | |
return false; | |
$resp = json_decode( wp_remote_retrieve_body( $data ), true ); | |
return $resp; | |
} | |
/* | |
* Google Comments (sgc-comments.php) | |
* | |
*/ | |
function psc_goog_button() { | |
?> | |
<div id="google-connect" class="button"> | |
<p><a href="#"><span>Sign In with </span>Google</a></p> | |
</div> | |
<?php | |
} | |
add_action('psc_add_button', 'psc_goog_button'); | |
function psc_goog_load_args() { | |
if( comments_open() && !is_user_logged_in() ) { | |
$options = get_option('psc_options'); | |
$defaults = array( | |
'callback' => 'googSignInCallback', | |
'clientid' => $options['goog_appid'], | |
'cookiepolicy' => 'single_host_origin', | |
'requestvisibleactions' => 'http://schemas.google.com/AddActivity', | |
'scope' => 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.login' | |
); | |
$args = apply_filters( 'psc_goog_args', $defaults ); | |
$args = wp_parse_args($args, $defaults); | |
?> | |
var googArgs = <?=json_encode($args);?> | |
<?php | |
} | |
}; | |
add_action('psc_footer_script','psc_goog_load_args', 20); // 20, to put it at the end of the footer insertions. sub-plugins should use 30 for their code | |
// Add user fields for Google based commenters | |
function psc_goog_inject_comment_data($comment_post_ID) { | |
if ( is_user_logged_in() ) { | |
return; // do nothing to WP users | |
} | |
if ( empty( $_POST['psc_goog_token'] ) ) { | |
return; | |
} | |
$token = $_POST['psc_goog_token']; | |
global $goog_user; | |
if ( empty($goog_user) ) { | |
$goog_user = psc_get_goog_userinfo($token); | |
} | |
if ( !$goog_user ) { | |
return; | |
} | |
$goog_user = apply_filters('psc_goog_user_data', $goog_user); | |
//$_POST['author'] = $goog_user['name']; | |
$_POST['url'] = 'https://plus.google.com/' . $goog_user['id'] . '/'; | |
$_POST['email'] = $goog_user['email']; | |
} | |
add_filter('pre_comment_on_post','psc_goog_inject_comment_data'); | |
// store the user ID and picture as comment meta data | |
function psc_goog_store_metadata($comment_id) { | |
global $goog_user; | |
if ( empty($goog_user) || !isset($goog_user['id']) ) { | |
return; | |
} | |
update_comment_meta($comment_id, 'googleid', $goog_user['id']); | |
update_comment_meta($comment_id, 'googlepicture', 'https://plus.google.com/s2/photos/profile/' . $goog_user['id']); | |
} | |
add_action('comment_post','psc_goog_store_metadata', 10, 1); | |
// generate avatar code for Google user comments | |
function psc_goog_avatar($avatar, $id_or_email, $size = '96', $default = '', $alt = false) { | |
// check to be sure this is for a comment | |
if ( !is_object($id_or_email) || !isset($id_or_email->comment_ID) || $id_or_email->user_id) { | |
return $avatar; | |
} | |
// check for google picture comment meta | |
$gp = get_comment_meta($id_or_email->comment_ID, 'googlepicture', true); | |
if ($gp) { | |
// return the avatar code | |
$avatar = "<img class='avatar avatar-{$size} google-avatar' src='{$gp}?sz={$size}' width='{$size}' height='{$size}' />"; | |
} | |
return $avatar; | |
} | |
add_filter('get_avatar','psc_goog_avatar', 10, 5); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment