From 81804648049b15ee7d3ee8b959237af0b06e3f00 Mon Sep 17 00:00:00 2001 From: Berend de Jong Date: Mon, 4 Aug 2014 00:20:42 +0700 Subject: [PATCH 1/4] Save Twitter avatar to user meta & show with get_avatar --- social-connect.php | 50 +++++++++++++++++++++++++------------------- twitter/callback.php | 20 +++++++++++------- 2 files changed, 40 insertions(+), 30 deletions(-) diff --git a/social-connect.php b/social-connect.php index 81e1811..be8aba0 100644 --- a/social-connect.php +++ b/social-connect.php @@ -9,7 +9,7 @@ License: GPL2 */ -/** +/** * Check technical requirements are fulfilled before activating. **/ function sc_activate(){ @@ -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'; @@ -95,7 +95,7 @@ function sc_parse_request($wp) { default: break; } - + wp_die(); } } @@ -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() ); @@ -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; } } @@ -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 */ @@ -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 { @@ -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)) { @@ -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) { @@ -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 = ''.$alt.''; @@ -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 ) { diff --git a/twitter/callback.php b/twitter/callback.php index eafab74..b670240 100644 --- a/twitter/callback.php +++ b/twitter/callback.php @@ -36,21 +36,25 @@ $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 ); ?> - + @@ -58,7 +62,7 @@ function init() { - + Date: Mon, 4 Aug 2014 00:33:44 +0700 Subject: [PATCH 2/4] Makes the URL protocol independent --- social-connect.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/social-connect.php b/social-connect.php index be8aba0..a9d67fa 100644 --- a/social-connect.php +++ b/social-connect.php @@ -136,7 +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_avatar = isset($_REQUEST[ 'social_connect_avatar' ]) ? str_replace('http:', '', $_REQUEST[ 'social_connect_avatar' ]) : ''; $sc_profile_url = ''; // Get host name from URL $site_url = parse_url( site_url() ); From 235bcf83a26063aa0454c31ceb44b84bd1b19c32 Mon Sep 17 00:00:00 2001 From: Berend de Jong Date: Mon, 4 Aug 2014 11:59:12 +0700 Subject: [PATCH 3/4] Following WP coding standards regarding spaces --- social-connect.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/social-connect.php b/social-connect.php index c102c43..2d71f20 100644 --- a/social-connect.php +++ b/social-connect.php @@ -127,7 +127,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' ]) ? str_replace('http:', '', $_REQUEST[ 'social_connect_avatar' ]) : ''; + $sc_avatar = isset( $_REQUEST['social_connect_avatar'] ) ? str_replace( 'http:', '', $_REQUEST['social_connect_avatar'] ) : ''; $sc_profile_url = ''; // Get host name from URL $site_url = parse_url( site_url() ); From a5ac09517a095232876c1d98ba6a78648e15dae8 Mon Sep 17 00:00:00 2001 From: Berend de Jong Date: Mon, 4 Aug 2014 12:02:56 +0700 Subject: [PATCH 4/4] Following WP coding standards regarding spaces again --- social-connect.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/social-connect.php b/social-connect.php index 2d71f20..6217f5a 100644 --- a/social-connect.php +++ b/social-connect.php @@ -218,8 +218,8 @@ function sc_social_connect_process_login( $is_ajax = false ) { 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); + 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' );