Skip to content

Commit

Permalink
Save Twitter avatar to user meta & show with get_avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
Frique committed Aug 3, 2014
1 parent 4eadeaa commit 8180464
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
50 changes: 28 additions & 22 deletions social-connect.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
License: GPL2
*/

/**
/**
* Check technical requirements are fulfilled before activating.
**/
function sc_activate(){
Expand Down Expand Up @@ -72,7 +72,7 @@ function sc_parse_request($wp) {
if (!session_id()) {
session_start();
}

switch ($wp->query_vars['social-connect']) {
case 'twitter':
require_once 'twitter/connect.php';
Expand All @@ -95,7 +95,7 @@ function sc_parse_request($wp) {
default:
break;
}

wp_die();
}
}
Expand Down Expand Up @@ -136,6 +136,7 @@ function sc_social_connect_process_login( $is_ajax = false ) {
$sc_first_name = $names[0];
$sc_last_name = $names[1];
$sc_screen_name = $_REQUEST[ 'social_connect_screen_name' ];
$sc_avatar = isset($_REQUEST[ 'social_connect_avatar' ]) ? $_REQUEST[ 'social_connect_avatar' ] : '';
$sc_profile_url = '';
// Get host name from URL
$site_url = parse_url( site_url() );
Expand Down Expand Up @@ -216,18 +217,22 @@ function sc_social_connect_process_login( $is_ajax = false ) {
} else { // Create new user and associate provider identity
if ( get_option( 'users_can_register' ) ) {
$user_login = sc_get_unique_username($user_login);

$userdata = array( 'user_login' => $user_login, 'user_email' => $sc_email, 'first_name' => $sc_first_name, 'last_name' => $sc_last_name, 'user_url' => $sc_profile_url, 'user_pass' => wp_generate_password() );

// Create a new user
$user_id = wp_insert_user( $userdata );

if ( $user_id && is_integer( $user_id ) ) {
update_user_meta( $user_id, $sc_provider_identity_key, $sc_provider_identity );
}

if(isset($sc_avatar) && $sc_avatar){
update_user_meta($user_id, 'social_connect_twitter_avatar', $sc_avatar);
}
} else {
add_filter( 'wp_login_errors', 'sc_login_errors' );

return;
}
}
Expand All @@ -241,14 +246,14 @@ function sc_social_connect_process_login( $is_ajax = false ) {
} else {
wp_safe_redirect( $redirect_to );
}

exit();
}

/**
* Add error message when user try to login
* with an nonexistent e-mail and registration is disabled
*
*
* @param WP_Error $errors
* @return WP_Error
*/
Expand All @@ -265,7 +270,7 @@ function sc_get_unique_username($user_login, $c = 1) {
$append = '_'.substr(md5($user_login),0,3) . $c;
else
$append = $c;

$user_login = apply_filters( 'social_connect_username_exists', $user_login . $append );
return sc_get_unique_username($user_login,++$c);
} else {
Expand Down Expand Up @@ -295,17 +300,17 @@ function sc_filter_avatar($avatar, $id_or_email, $size, $default, $alt) {
$social_id = '';
$provider_id = '';
$user_id = (!is_integer($id_or_email) && !is_string($id_or_email) && get_class($id_or_email)) ? $id_or_email->user_id : $id_or_email;

if (!empty($user_id)) {
// Providers to search for (assume user prefers their current logged in service)
// Note: OpenID providers use gravatars
$providers = array('facebook', 'twitter');

$social_connect_provider = isset( $_COOKIE['social_connect_current_provider']) ? $_COOKIE['social_connect_current_provider'] : '';
if (!empty($social_connect_provider) && $social_connect_provider == 'twitter') {
$providers = array('twitter', 'facebook');
}

foreach($providers as $search_provider) {
$social_id = get_user_meta($user_id, 'social_connect_'.$search_provider.'_id', true);
if (!empty($social_id)) {
Expand All @@ -314,7 +319,7 @@ function sc_filter_avatar($avatar, $id_or_email, $size, $default, $alt) {
}
}
}

// At least one social ID was found
if (!empty($social_id)) {
switch($provider_id) {
Expand All @@ -325,32 +330,33 @@ function sc_filter_avatar($avatar, $id_or_email, $size, $default, $alt) {
// large (about 200 pixels wide, variable height)

$size_label = 'large';

if($size <= 100)
$size_label = 'normal';
else if($size <= 50)
$size_label = 'small';

$custom_avatar = "http://graph.facebook.com/$social_id/picture?type=$size_label";
break;
case 'twitter':
// bigger - 73px by 73px
// normal - 48px by 48px
// mini - 24px by 24px

$size_label = 'bigger';

if ($size <= 48) {
$size_label = 'normal';
} else if ($size <= 24) {
$size_label = 'mini';
}

$custom_avatar = "http://api.twitter.com/1/users/profile_image?id=$social_id&size=$size_label";

$custom_avatar = get_user_meta($user_id, 'social_connect_twitter_avatar', true);
$custom_avatar = str_replace('_normal', '_'.$size_label, $custom_avatar);
break;
}
}

if (!empty($custom_avatar)) {
// return the custom avatar from the social network
$return = '<img class="avatar" src="'.$custom_avatar.'" style="width:'.$size.'px" alt="'.$alt.'" />';
Expand All @@ -368,7 +374,7 @@ function sc_filter_avatar($avatar, $id_or_email, $size, $default, $alt) {

/**
* Add link to Social Connect settings page in the plugins page
*
*
* @return array plugin links
*/
function sc_add_settings_link( $default_links ) {
Expand Down
20 changes: 12 additions & 8 deletions twitter/callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,33 @@
$name = $user->name;
$screen_name = $user->screen_name;
$twitter_id = $user->id;
$avatar = $user->profile_image_url;
$signature = social_connect_generate_signature($twitter_id);

do_action( 'social_connect_before_register_twitter', $twitter_id, $signature );
?>

<html>
<head>
<script>
function init() {
window.opener.wp_social_connect({'action' : 'social_connect', 'social_connect_provider' : 'twitter',
'social_connect_signature' : '<?php echo $signature ?>',
'social_connect_twitter_identity' : '<?php echo $twitter_id ?>',
'social_connect_screen_name' : '<?php echo $screen_name ?>',
'social_connect_name' : '<?php echo $name ?>'});

window.opener.wp_social_connect({
'action' : 'social_connect',
'social_connect_provider' : 'twitter',
'social_connect_signature' : '<?php echo $signature; ?>',
'social_connect_twitter_identity' : '<?php echo $twitter_id; ?>',
'social_connect_screen_name' : '<?php echo $screen_name; ?>',
'social_connect_avatar' : '<?php echo $avatar; ?>',
'social_connect_name' : '<?php echo $name; ?>'
});
window.close();
}
</script>
</head>
<body onload="init();">
</body>
</html>

<?php
} else {
/* Save HTTP status for error dialog on connnect page.*/
Expand Down

0 comments on commit 8180464

Please sign in to comment.