Skip to content

Instantly share code, notes, and snippets.

@bulentsakarya
Created March 13, 2023 10:34
Show Gist options
  • Save bulentsakarya/a999a8b8909752e99717e33766f68d19 to your computer and use it in GitHub Desktop.
Save bulentsakarya/a999a8b8909752e99717e33766f68d19 to your computer and use it in GitHub Desktop.
/**
* Check User Status.
*
* @param $user WP_User Object
*/
function check_status( $user, $username, $password ) {
$url = 'https://api.url';
/* 1. user data exist */
if(email_exists( $user->user_email )) {
/* 2. User is admin or subscriber */
if ( !in_array( 'subscriber', (array) $user->roles ) ) {
// Go on
return $user;
} else {
/* 3. Api result true */
if (check_api_credentials($user->user_email, $user->pass, $url)) {
return $user;
} else {
return false;
}
}
} else {
/* 4. Create user */
if (check_api_credentials($user->user_email, $user->pass, $url)) {
print_r(json_decode($result));
exit;
/* 5. Diğer sitede bilgisi varsa wordpress sitesine de ekle */
wp_create_user( $user->user_email, $user->pass, $user->user_email );
wp_set_auth_cookie( $user->ID );
//wp_redirect( home_url() );
return $user;
} else {
$message = esc_html__( 'Abonelik bilgileri bulunamadı.', 'bsauth');
return new WP_Error( 'user_not_verified', $message );
}
}
// devam et
return $user;
}
add_filter( 'wp_authenticate', 'check_status', 10, 2 );
function check_api_credentials($username, $password, $api_url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('username' => $username, 'password' => $password)));
$response = curl_exec($ch);
if(curl_errno($ch)) {
$error_msg = curl_error($ch);
return false;
}
curl_close($ch);
$result = json_decode($response, true);
if ($result['success']) {
return true;
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment