|
<?php |
|
|
|
class Socialite |
|
{ |
|
private $linkedin_client_id; |
|
private $linkedin_client_secret; |
|
private $redirect_uri = "http://localhost:3000/water/wp-admin/admin-post.php?action=linkedin_callback"; |
|
private $linkedin_scope = "r_basicprofile r_emailaddress"; |
|
private $linkedin_auth_uri = "https://www.linkedin.com/oauth/v2/authorization"; |
|
private $linkedin_token_uri = "https://www.linkedin.com/oauth/v2/accessToken"; |
|
private $linkedin_people_uri = "https://api.linkedin.com/v1/people/"; |
|
public $state = "bhjbbcjheghjbcvjhcbbjcjnsfece"; |
|
|
|
public function __construct() |
|
{ |
|
$options = get_option( 'ath_auth_settings' ); |
|
$this->linkedin_client_id = ( isset( $options['linkedin_client_id'] ) ) ? $options['linkedin_client_id'] : ""; |
|
$this->linkedin_client_secret = ( isset( $options['linkedin_client_secret'] ) ) ? $options['linkedin_client_secret'] : ""; |
|
} |
|
|
|
public function get_authorization_code() |
|
{ |
|
$curl = curl_init( $this->linkedin_auth_uri."?".http_build_query( array( |
|
"response_type" => "code", |
|
"client_id" => $this->linkedin_client_id, |
|
"redirect_uri" => $this->redirect_uri, |
|
"state" => $this->state, |
|
"scope" => $this->linkedin_scope |
|
) ) |
|
); |
|
|
|
$response = curl_exec( $curl ); |
|
$info = curl_getinfo( $curl ); |
|
curl_close( $curl ); |
|
wp_redirect( $info['redirect_url'] ); |
|
|
|
} |
|
|
|
public function get_access_token( $code ) |
|
{ |
|
$curl = curl_init(); |
|
curl_setopt_array($curl, array( |
|
CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded'), |
|
CURLOPT_RETURNTRANSFER => 1, |
|
CURLOPT_URL => $this->linkedin_token_uri, |
|
CURLOPT_POST => 1, |
|
CURLOPT_POSTFIELDS => http_build_query ( array( |
|
"client_id" => $this->linkedin_client_id, |
|
"client_secret" => $this->linkedin_client_secret, |
|
"grant_type" => "authorization_code", |
|
"redirect_uri" => $this->redirect_uri, |
|
"code" => $code |
|
)), |
|
)); |
|
|
|
$response = json_decode( curl_exec( $curl ) ); |
|
$info = curl_getinfo( $curl ); |
|
if( $info['http_code'] != 200 ) |
|
{ |
|
return false; |
|
} |
|
curl_close( $curl ); |
|
return $response->access_token; |
|
} |
|
|
|
public function get_users_data( $access_token ) |
|
{ |
|
$curl = curl_init(); |
|
$login_url = network_site_url( '/login' ); |
|
|
|
curl_setopt_array($curl, array( |
|
CURLOPT_HTTPHEADER => array( 'Authorization: Bearer'.$access_token ), |
|
CURLOPT_RETURNTRANSFER => 1, |
|
CURLOPT_URL => $this->linkedin_people_uri.'~:(email-address,first-name,last-name,picture-url)?format=json' |
|
)); |
|
$response = json_decode( curl_exec( $curl ) ); |
|
$info = curl_getinfo( $curl ); |
|
curl_close( $curl ); |
|
if( $info['http_code'] == 401 ) |
|
{ |
|
$_SESSION["errors"]["login_error_url"] = "Something went wrong. Please try again or contact us."; |
|
wp_redirect( $login_url ); |
|
exit; |
|
} |
|
return $response; |
|
} |
|
} |