Skip to content

Commit

Permalink
Fixed user registration
Browse files Browse the repository at this point in the history
  • Loading branch information
UmeshSingla committed Jul 21, 2015
1 parent ef3d2bf commit 437e0ec
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 153 deletions.
32 changes: 0 additions & 32 deletions css/wdfb.css
Original file line number Diff line number Diff line change
@@ -1,35 +1,3 @@
a.wdfb_register_button, a.wdfb_register_button:hover, a.wdfb_register_button:active, a.wdfb_register_button:visited {
text-decoration: none;
color: #FFF;

background: url("//static.ak.fbcdn.net/rsrc.php/v2/yx/r/j_i0CTUUUEe.png") no-repeat scroll 0 0 #29447E;
cursor: pointer;
display: inline-block;
outline: medium none;
padding: 0 0 0 1px;
margin-left: 1em;
}

a.wdfb_register_button span {
display: block;
height: 31px;
line-height: 31px !important;
font-size: 14px;
font-family: "lucida grande",tahoma,verdana,arial,sans-serif;
font-weight: bold;
text-align: center;

background: #5F78AB;
border-bottom: 1px solid #1A356E;
border-top: 1px solid #879AC0;
color: #FFFFFF;
display: block;
font-family: "lucida grande",tahoma,verdana,arial,sans-serif;
font-weight: bold;
margin: 1px 1px 0 38px;
padding: 2px 6px 3px 12px;
text-shadow: none;
}
.wdfb-album-image-row a {
display: inline-block;
}
Expand Down
9 changes: 2 additions & 7 deletions lib/class_wdfb_admin_form_renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,6 @@ function create_login_redirect_box() {
echo '<div><small>' . sprintf( __( 'This is what will happen upon login: my users will be redirected to %s.', 'wdfb' ), $url ) . '</small></div>';
}

function create_captcha_box() {
$opt = $this->_get_option( 'wdfb_connect' );
echo $this->_create_checkbox( 'connect', 'no_captcha', @$opt['no_captcha'] );
}

function create_autologin_box() {
$opt = $this->_get_option( 'wdfb_connect' );
echo $this->_create_checkbox( 'connect', 'autologin_after_registration', @$opt['autologin_after_registration'] );
Expand Down Expand Up @@ -375,7 +370,7 @@ function create_do_not_show_button_box() {
echo '<label>' . ucfirst( $type->labels->name ) . '</label><br />';
}
echo '<div id="wdfb-like_button-special_cases">';
if ( defined( 'BP_VERSION' ) ) {
if ( defined( 'BP_VERSION' ) && class_exists('BuddyPress') ) {
echo '<div id="wdfb-like_button-bp_activity-anchor">';
echo '<label for="not_in_post_types-_buddypress_activity">' . __( 'Allow &quot;Like&quot; button for BuddyPress Activities', 'wdfb' ) . '</label>: ';
echo $this->_create_subcheckbox( 'button', 'not_in_post_types', '_buddypress_activity', @in_array( '_buddypress_activity', $opt['not_in_post_types'] ) );
Expand Down Expand Up @@ -809,7 +804,7 @@ function create_autopost_map_box() {
}

// BP Activities mappings
if ( defined( 'BP_VERSION' ) ) {
if ( defined( 'BP_VERSION' ) && class_exists('BuddyPress') ) {
$pname = 'bp_activity';
$pval = __( 'BuddyPress Activity update', 'wdfb' );
$fb_action = @$opts["type_{$pname}_fb_type"];
Expand Down
40 changes: 18 additions & 22 deletions lib/class_wdfb_admin_pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function register_site_settings() {
$form,
'create_easy_facebook_registration_box'
), 'wdfb_options_page', 'wdfb_connect' );
if ( defined( 'BP_VERSION' ) ) {
if ( defined( 'BP_VERSION' ) && class_exists('BuddyPress') ) {
add_settings_field( 'wdfb_update_bp_activity', __( 'Update Activity feed', 'wdfb' ), array(
$form,
'create_update_bp_activity_box'
Expand All @@ -115,15 +115,11 @@ function register_site_settings() {
$form,
'create_login_redirect_box'
), 'wdfb_options_page', 'wdfb_connect' );
add_settings_field( 'wdfb_captcha', __( 'Do not show CAPTCHA on registration pages', 'wdfb' ), array(
$form,
'create_captcha_box'
), 'wdfb_options_page', 'wdfb_connect' );
add_settings_field( 'wdfb_autologin', __( 'Auto-login after registration', 'wdfb' ), array(
$form,
'create_autologin_box'
), 'wdfb_options_page', 'wdfb_connect' );
if ( defined( 'BP_VERSION' ) ) { // BuddyPress
if ( defined( 'BP_VERSION' ) && class_exists('BuddyPress') ) { // BuddyPress
add_settings_field( 'wdfb_buddypress_registration_fields', __( 'Map BuddyPress profile to Facebook', 'wdfb' ), array(
$form,
'create_buddypress_registration_fields_box'
Expand Down Expand Up @@ -202,7 +198,7 @@ function register_site_settings() {
), 'wdfb_options_page', 'wdfb_opengraph' );
add_settings_field( '', '', array( $form, 'next_step' ), 'wdfb_options_page', 'wdfb_opengraph' );

if ( defined( 'BP_VERSION' ) ) {
if ( defined( 'BP_VERSION' ) && class_exists('BuddyPress') ) {
register_setting( 'wdfb', 'wdfb_groups' );
add_settings_section( 'wdfb_groups', __( 'Facebook Groups', 'wdfb' ), create_function( '', '' ), 'wdfb_options_page' );
add_settings_field( 'wdfb_allow_bp_groups_sync', __( 'BuddyPress groups info sync', 'wdfb' ), array(
Expand Down Expand Up @@ -236,7 +232,7 @@ function register_site_settings() {
$form,
'create_use_fb_comments_box'
), 'wdfb_options_page', 'wdfb_comments' );
if ( ! defined( 'BP_VERSION' ) ) {
if ( ! defined( 'BP_VERSION' ) && class_exists('BuddyPress') ) {
add_settings_field( 'wdfb_override_wp_comments_settings', __( 'Override WordPress discussion settings', 'wdfb' ), array(
$form,
'create_override_wp_comments_settings_box'
Expand Down Expand Up @@ -301,7 +297,7 @@ function register_site_settings() {
$form,
'image_size_autopost_facebook'
), 'wdfb_options_page', 'wdfb_autopost' );
if ( defined( 'BP_VERSION' ) ) {
if ( defined( 'BP_VERSION' ) && class_exists('BuddyPress') ) {
add_settings_field( 'wdfb_allow_bp_activity_switch', __( 'Do not allow individual Activity updates to Facebook', 'wdfb' ), array(
$form,
'create_allow_bp_activity_switch_box'
Expand Down Expand Up @@ -399,15 +395,15 @@ function register_blog_settings() {
if ( ! is_multisite() || current_user_can( 'manage_network_options' ) ) {
register_setting( 'wdfb', 'wdfb_connect' );
add_settings_section( 'wdfb_connect', __( 'Facebook Connect', 'wdfb' ), create_function( '', '' ), 'wdfb_options_page' );
add_settings_field( 'wdfb_allow_facebook_registration', __( 'Allow users to register with Facebook', 'wdfb' ), array(
add_settings_field( 'wdfb_allow_facebook_registration', __( 'Allow users to login/register with Facebook', 'wdfb' ), array(
$form,
'create_allow_facebook_registration_box'
), 'wdfb_options_page', 'wdfb_connect' );
add_settings_field( 'wdfb_easy_facebook_registration', __( 'Allow single-click registration', 'wdfb' ), array(
$form,
'create_easy_facebook_registration_box'
), 'wdfb_options_page', 'wdfb_connect' );
if ( defined( 'BP_VERSION' ) ) {
if ( defined( 'BP_VERSION' ) && class_exists('BuddyPress') ) {
add_settings_field( 'wdfb_update_bp_activity', __( 'Update Activity feed', 'wdfb' ), array(
$form,
'create_update_bp_activity_box'
Expand All @@ -421,15 +417,11 @@ function register_blog_settings() {
$form,
'create_login_redirect_box'
), 'wdfb_options_page', 'wdfb_connect' );
add_settings_field( 'wdfb_captcha', __( 'Do not show CAPTCHA on registration pages', 'wdfb' ), array(
$form,
'create_captcha_box'
), 'wdfb_options_page', 'wdfb_connect' );
add_settings_field( 'wdfb_autologin', __( 'Auto-login after registration', 'wdfb' ), array(
$form,
'create_autologin_box'
), 'wdfb_options_page', 'wdfb_connect' );
if ( defined( 'BP_VERSION' ) ) { // BuddyPress
if ( defined( 'BP_VERSION' ) && class_exists('BuddyPress') ) { // BuddyPress
add_settings_field( 'wdfb_buddypress_registration_fields', __( 'Map BuddyPress profile to Facebook', 'wdfb' ), array(
$form,
'create_buddypress_registration_fields_box'
Expand Down Expand Up @@ -986,7 +978,7 @@ function handle_fb_session_state() {
}
}

function handle_fb_auth_tokens() {
function handle_fb_auth_tokens( $return = false ) {
$log = new Wdfb_ErrorLog;
$tokens = $this->data->get_option( 'wdfb_api', 'auth_tokens' );

Expand Down Expand Up @@ -1067,9 +1059,13 @@ function handle_fb_auth_tokens() {
$api['auth_accounts'][ $ptk['id'] ] = $ptk['name'];
}

$user = wp_get_current_user();
update_user_meta( $user->ID, 'wdfb_api_accounts', $api );
$this->merge_api_tokens();
if( !$return ) {
$user = wp_get_current_user();
update_user_meta( $user->ID, 'wdfb_api_accounts', $api );
$this->merge_api_tokens();
}else{
return $ptk;
}

return true;
}
Expand Down Expand Up @@ -1407,7 +1403,7 @@ function json_import_comments() {

function json_populate_profile() {
$user = wp_get_current_user();
if ( defined( 'BP_VERSION' ) ) {
if ( defined( 'BP_VERSION' ) && class_exists('BuddyPress')) {
$status = $this->model->populate_bp_fields_from_fb( $user->ID );
} else {
$status = $this->model->populate_wp_fields_from_fb( $user->ID );
Expand Down Expand Up @@ -1454,7 +1450,7 @@ function json_perhaps_create_wp_user() {
}
$this->handle_fb_session_state();
if ( empty( $user_id ) ) {
$message = apply_filters( 'wdfb_warn_disabled_registration', __( "User registration is currently not allowed.", 'wdfb' ) );
$message = apply_filters( 'wdfb_warn_disabled_registration', __( "User registration is currently not allowed.", 'wdfb' ) );
wp_send_json_error( $message );
} else {
wp_send_json_success( "Logging in" );
Expand Down
20 changes: 0 additions & 20 deletions lib/class_wdfb_marker_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,6 @@ function get_button_tag( $b ) {
return '[' . $this->buttons[ $b ] . ']';
}

function process_register_button_code( $atts, $content = '' ) {
if ( is_user_logged_in() ) {
return '';
}
if ( ! $this->data->get_option( 'wdfb_connect', 'allow_facebook_registration' ) ) {
return '';
}

$content = ! empty( $content ) ? $content : __( 'Register with Facebook', 'wdfb' );

$base_url = defined( 'BP_VERSION' )
? bp_get_signup_page()
: apply_filters( 'wdfb-registration-registration_page', site_url( '/wp-signup.php', 'login' ) );
$url = add_query_arg( array(
'fb_registration_page' => 1
), $base_url );

return '<p><a class="wdfb_register_button" href="' . $url . '"><span>' . $content . '</span></a></p>';
}

function process_connect_code( $atts, $content = '' ) {
if ( ! $this->data->get_option( 'wdfb_connect', 'allow_facebook_registration' ) ) {
return $content;
Expand Down
86 changes: 83 additions & 3 deletions lib/class_wdfb_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ function set_bp_xprofile_field( $field_id, $user_id, $data ) {
* Gets FB profile image and sets it as BuddyPress avatar.
*/
function set_fb_image_as_bp_avatar( $user_id, $me ) {
if ( ! defined( 'BP_VERSION' ) ) {
if ( ! defined( 'BP_VERSION' ) || !class_exists('BuddyPress') ) {
return true;
}
if ( ! function_exists( 'bp_core_avatar_upload_path' ) ) {
Expand All @@ -225,6 +225,10 @@ function set_fb_image_as_bp_avatar( $user_id, $me ) {
@wp_mkdir_p( $path );
}
}
//If directory not exists
if( !empty( $path) && !file_exists( $path ) ) {
@wp_mkdir_p($path);
}

// Get FB picture
//$fb_img = file_get_contents("http://graph.facebook.com/{$fb_uid}/picture?type=large");
Expand Down Expand Up @@ -473,6 +477,62 @@ function delete_wp_user( $uid ) {
$this->db->query( "DELETE FROM {$this->db->users} WHERE ID={$uid}" );
$this->db->query( "DELETE FROM {$this->db->usermeta} WHERE user_id={$uid}" );
}
function get_user_token() {
$this->data = Wdfb_OptionsRegistry::get_instance();
$fb_uid = $this->fb->getUser();
$app_id = trim( $this->data->get_option( 'wdfb_api', 'app_key' ) );
$app_secret = trim( $this->data->get_option( 'wdfb_api', 'secret_key' ) );
if ( ! $app_id || ! $app_secret ) {
return false;
} // Plugin not yet configured

// Token is now long-term token
$token = $this->get_user_api_token( $fb_uid );

// Make sure it is
$user_token = preg_match( '/^' . preg_quote( "{$app_id}|" ) . '/', $token ) ? false : $token;

// Just force the token reset, for now
$token = false;
if ( ! $token ) {
// Get temporary token
$token = $this->fb->getAccessToken();

$user_token = preg_match( '/^' . preg_quote( "{$app_id}|" ) . '/', $token ) ? $user_token : $token;

if ( ! $token ) {
return false;
}

// Exchange it for the actual long-term token
$url = "https://graph.facebook.com/oauth/access_token?client_id={$app_id}&client_secret={$app_secret}&grant_type=fb_exchange_token&fb_exchange_token={$token}&access_token={$user_token}";
$args = array(
'method' => 'GET',
'timeout' => '5',
'redirection' => '5',
'user-agent' => 'wdfb',
'blocking' => true,
'compress' => false,
'decompress' => true,
'sslverify' => false
);
$page = wp_remote_get( $url, $args );
if ( is_wp_error( $page ) ) {
return false;
} // Request fail
if ( (int) $page['response']['code'] != 200 ) {

return false;
} // Request fail

parse_str( $page['body'], $response );
$token = isset( $response['access_token'] ) ? $response['access_token'] : false;
if ( ! $token ) {
return false;
}
return $token;
}
}

function create_new_wp_user_from_fb() {
$send_email = false;
Expand All @@ -485,9 +545,23 @@ function create_new_wp_user_from_fb() {
$send_email = true; // we'll need to notify the user
$me['id'] = $this->fb->user_id;
}
if ( ! $me ) {

if ( ! $me || empty( $me['id'] ) ) {
return false;
}
//Get token
$user_token = $this->get_user_token();

//Get User email
try {
$me = $this->fb->api( '/' . $me['id'],
array(
'access_token' => $user_token,
'fields' => 'email, first_name, last_name, name, id'
) );
} catch ( Exception $e ) {
$this->log->error( __FUNCTION__, new Exception( $e->get_error_message() ) );
}
//Validate email
if ( empty( $me['email'] ) || ! filter_var( $me['email'], FILTER_VALIDATE_EMAIL ) || email_exists( $me['email'] ) ) {
return false;
Expand All @@ -501,6 +575,12 @@ function create_new_wp_user_from_fb() {

return false;
} else if ( !empty( $user_id ) ) {
if( !empty( $me['first_name'] ) ) {
update_user_meta($user_id, 'first_name', $me['first_name']);
}
if( !empty( $me['last_name'] ) ) {
update_user_meta($user_id, 'first_name', $me['last_name']);
}
wp_new_user_notification( $user_id, $password );
do_action( 'wdfb-registration_email_sent' );
}
Expand All @@ -511,7 +591,7 @@ function create_new_wp_user_from_fb() {
// Allow other actions - e.g. posting to Facebook, upon registration
do_action( 'wdfb-user_registered-postprocess', $user_id, $me, $registration, $this );

if ( defined( 'BP_VERSION' ) ) {
if ( defined( 'BP_VERSION' ) && class_exists('BuddyPress') ) {
$this->populate_bp_fields_from_fb( $user_id, $me );
} // BuddyPress
else {
Expand Down
Loading

0 comments on commit 437e0ec

Please sign in to comment.