Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use LinkedIn\LinkedIn;
- Class Jeg_Account_Social
- {
- private static $instance;
- // facebook integration
- private $fb_app_id;
- private $fb_app_secret;
- private $fb_graph_version;
- // google integration
- private $google_client_id;
- private $google_secret;
- private $google_apps_name;
- // linked in integration
- private $linkedin_app_id;
- private $linkedin_app_secret;
- public static function getInstance()
- {
- if (null === static::$instance)
- {
- static::$instance = new static();
- }
- return static::$instance;
- }
- public function __construct()
- {
- $this->includes();
- $this->hook();
- }
- public function includes()
- {
- include_once( JOBPLANET_PLUGIN_DIR . 'external/facebook-sdk-v5/autoload.php' );
- include_once( JOBPLANET_PLUGIN_DIR . 'external/google-api-php-client/src/Google/autoload.php' );
- include_once( JOBPLANET_PLUGIN_DIR . 'external/LinkedIn/LinkedIn.php' );
- }
- public function hook()
- {
- // social
- add_action('init', array(&$this, 'init_social_integration'));
- add_action('init', array( &$this , 'add_social_endpoint' ));
- add_action('parse_request', array(&$this, 'social_parse_request'));
- add_filter('query_vars', array( &$this , 'social_query_vars' ));
- add_action('jeg_login_social', array(&$this, 'print_login_social'));
- add_action('jeg_register_social', array(&$this, 'print_register_social'));
- add_filter('jeg_social_url_filter', array(&$this, 'social_url_filter'), 10, 2);
- }
- public function print_register_social()
- {
- $showwhere = vp_option('joption.show_social_login');
- if($showwhere === 'register' || $showwhere === 'both') {
- if(vp_option('joption.enable_fb_register')) {
- ?>
- <p><a href="<?php echo esc_url($this->facebook_login_url()); ?>" class="btn btn-fb btn-theme btn-block"><i class="fa fa-facebook pull-left bordered-right"></i> <?php _e('Register with Facebook', 'jobplanet-plugin'); ?></a></p>
- <?php
- }
- if(vp_option('joption.enable_google_register')) {
- ?>
- <p><a href="<?php echo esc_url($this->google_login_url()); ?>" class="btn btn-danger btn-theme btn-block"><i class="fa fa-google-plus pull-left bordered-right"></i> <?php _e('Register with Google', 'jobplanet-plugin'); ?></a></p>
- <?php
- }
- if(vp_option('joption.enable_linkedin_register')) {
- ?>
- <p><a href="<?php echo esc_url($this->linkedin_login_url()); ?>" class="btn btn-linkedin btn-theme btn-block"><i class="fa fa-linkedin pull-left bordered-right"></i> <?php _e('Register with Linked In', 'jobplanet-plugin'); ?></a></p>
- <?php
- }
- }
- if($showwhere === 'register' && (vp_option('joption.enable_fb_register') || vp_option('joption.enable_google_register') || vp_option('joption.enable_linkedin_register'))) {
- ?>
- <div class="white-space-10"></div>
- <p class="text-center"><span class="span-line"><?php _e('OR', 'jobplanet-plugin'); ?></span></p>
- <?php
- }
- if($showwhere === 'both' && (vp_option('joption.enable_fb_register') || vp_option('joption.enable_google_register') || vp_option('joption.enable_linkedin_register'))) {
- ?>
- <div class="white-space-10"></div>
- <p class="text-center"><span class="span-line"><?php _e('OR', 'jobplanet-plugin'); ?></span></p>
- <?php
- }
- }
- public function print_login_social()
- {
- $showwhere = vp_option('joption.show_social_login');
- if($showwhere === 'login' || $showwhere === 'both') {
- if(vp_option('joption.enable_fb_register')) {
- ?>
- <p><a href="<?php echo esc_url($this->facebook_login_url()); ?>" class="btn btn-fb btn-theme btn-block"><i class="fa fa-facebook pull-left bordered-right"></i> <?php _e('Login with Facebook', 'jobplanet-plugin'); ?></a></p>
- <?php
- }
- if(vp_option('joption.enable_google_register')) {
- ?>
- <p><a href="<?php echo esc_url($this->google_login_url()); ?>" class="btn btn-danger btn-theme btn-block"><i class="fa fa-google-plus pull-left bordered-right"></i> <?php _e('Login with Google', 'jobplanet-plugin'); ?></a></p>
- <?php
- }
- if(vp_option('joption.enable_linkedin_register')) {
- ?>
- <p><a href="<?php echo esc_url($this->linkedin_login_url()); ?>" class="btn btn-linkedin btn-theme btn-block"><i class="fa fa-linkedin pull-left bordered-right"></i> <?php _e('Login with Linked In', 'jobplanet-plugin'); ?></a></p>
- <?php
- }
- }
- if(vp_option('joption.enable_fb_register') || vp_option('joption.enable_google_register') || vp_option('joption.enable_linkedin_register')) {
- ?>
- <div class="white-space-10"></div>
- <p class="text-center"><span class="span-line"><?php _e('OR', 'jobplanet-plugin'); ?></span></p>
- <?php
- }
- }
- public function init_social_integration()
- {
- if(vp_option('joption.enable_fb_register')) {
- $this->fb_app_id = vp_option('joption.fb_app_id');
- $this->fb_app_secret = vp_option('joption.fb_app_secret');
- $this->fb_graph_version = 'v2.2';
- }
- if(vp_option('joption.enable_google_register')) {
- $this->google_client_id = vp_option('joption.google_client_id');
- $this->google_secret = vp_option('joption.google_secret');
- $this->google_apps_name = vp_option('joption.google_apps_name');
- }
- if(vp_option('joption.enable_linkedin_register')) {
- $this->linkedin_app_id = vp_option('joption.linkedin_app_id');
- $this->linkedin_app_secret = vp_option('joption.linkedin_app_secret');
- }
- }
- public function add_social_endpoint()
- {
- add_rewrite_rule( '^social-callback/([^/]*)/?', 'index.php?social-callback=$matches[1]', 'top' );
- }
- public function social_query_vars($vars)
- {
- $vars[] = 'social-callback';
- return $vars;
- }
- public function social_parse_request($wp)
- {
- if (array_key_exists('social-callback', $wp->query_vars))
- {
- switch($wp->query_vars['social-callback'])
- {
- case "facebook" :
- $this->facebook_login();
- break;
- case "google" :
- $this->google_login();
- break;
- case "linkedin" :
- $this->linkedin_login();
- break;
- }
- }
- }
- public function linkedin_login()
- {
- $linkedin = new LinkedIn(array(
- 'api_key' => $this->linkedin_app_id,
- 'api_secret' => $this->linkedin_app_secret,
- 'callback_url' => $this->social_callback_url('linkedin')
- ));
- try {
- $token = $linkedin->getAccessToken($_REQUEST['code']);
- if(!isset($token))
- {
- throw new Exception( '<strong>' . esc_html(__( 'Error', 'jobplanet-plugin' )) . ':</strong> ' . esc_html(__( 'Failed to get Linked in access token', 'jobplanet-plugin' )) );
- }
- $info = $linkedin->get('/people/~:(email-address,first-name,last-name)');
- $user_email = $info['emailAddress'];
- if(email_exists($user_email))
- {
- $this->log_user_in_social($user_email);
- } else
- {
- $this->register_user_social(array(
- 'email' => $user_email,
- 'first_name' => $info["firstName"],
- 'last_name' => $info["lastName"]
- ));
- }
- } catch (Exception $e )
- {
- jeg_flash_message('message', $e->getMessage(), 'alert-danger');
- }
- wp_redirect( jeg_get_account_url() );
- exit;
- }
- public function facebook_login()
- {
- $fb = new Facebook\Facebook(array(
- 'app_id' => $this->fb_app_id,
- 'app_secret' => $this->fb_app_secret,
- 'default_graph_version' => $this->fb_graph_version,
- ));
- $helper = $fb->getRedirectLoginHelper();
- try {
- $accessToken = $helper->getAccessToken();
- if (! isset($accessToken))
- {
- if ($helper->getError())
- {
- header('HTTP/1.0 401 Unauthorized');
- echo "Error: " . $helper->getError() . "\n";
- echo "Error Code: " . $helper->getErrorCode() . "\n";
- echo "Error Reason: " . $helper->getErrorReason() . "\n";
- echo "Error Description: " . $helper->getErrorDescription() . "\n";
- } else
- {
- header('HTTP/1.0 400 Bad Request');
- echo 'Bad request';
- }
- exit;
- }
- $response = $fb->get('/me?fields=id,name,email,first_name,last_name', $accessToken->getValue());
- $user = $response->getGraphUser();
- $user_email = $user['email'];
- if(email_exists($user_email))
- { // user exist
- $this->log_user_in_social($user_email);
- } else
- { // user not exist
- $this->register_user_social(array(
- 'email' => $user_email,
- 'first_name' => $user["first_name"],
- 'last_name' => $user["last_name"]
- ));
- }
- } catch (Exception $e )
- {
- if ($e instanceof Facebook\Exceptions\FacebookResponseException)
- {
- jeg_flash_message('message', 'Graph returned an error: ' . $e->getMessage(), 'alert-danger');
- } else if ($e instanceof Facebook\Exceptions\FacebookSDKException)
- {
- jeg_flash_message('message', 'Facebook SDK returned an error : ' . $e->getMessage(), 'alert-danger');
- } else
- {
- jeg_flash_message('message', $e->getMessage(), 'alert-danger');
- }
- }
- wp_redirect( jeg_get_account_url() );
- exit;
- }
- public function google_login()
- {
- $client = new Google_Client();
- $client->setApplicationName( $this->google_apps_name );
- $client->setClientId( $this->google_client_id );
- $client->setClientSecret( $this->google_secret );
- $client->setRedirectUri( $this->social_callback_url('google') );
- try {
- if(!isset($_GET['code']))
- {
- throw new Exception( '<strong>' . esc_html(__( 'Error', 'jobplanet-plugin' )) . ':</strong> ' . esc_html(__( 'Failed to get Google Code', 'jobplanet-plugin' )) );
- } else
- {
- if (!isset($_GET['code']))
- {
- header('HTTP/1.0 400 Bad Request');
- echo 'Bad request';
- exit;
- }
- // get access token
- $client->authenticate($_GET['code']);
- $access_token = $client->getAccessToken();
- $client->setAccessToken($access_token);
- // use access token to get detail user
- $plus = new Google_Service_Plus($client);
- $user = $plus->people->get("me");
- $user_email = $user['emails'][0]['value'];
- if(email_exists($user_email))
- { // user exist
- $this->log_user_in_social($user_email);
- } else
- { // user not exist
- $this->register_user_social(array(
- 'email' => $user_email,
- 'first_name' => $user['name']['givenName'],
- 'last_name' => $user['name']['familyName']
- ));
- }
- }
- } catch (Exception $e )
- {
- jeg_flash_message('message', $e->getMessage(), 'alert-danger');
- }
- wp_redirect( jeg_get_account_url() );
- exit;
- }
- public function log_user_in_social ($user_email)
- {
- // get user detail
- $userdetail = get_user_by('email', $user_email);
- // log the new user in
- wp_set_auth_cookie($userdetail->ID, true, is_ssl());
- wp_set_current_user($userdetail->ID);
- }
- public function register_user_social($user)
- {
- $generated_password = wp_generate_password();
- $default_member_role = vp_option('joption.default_role_social', Jeg_Account::APPLICANT);
- $new_customer = wp_insert_user(array(
- 'user_login' => $user['email'],
- 'user_pass' => $generated_password,
- 'user_email' => $user['email'],
- 'first_name' => $user["first_name"],
- 'last_name' => $user["last_name"],
- 'user_registered' => date('Y-m-d H:i:s'),
- 'role' => $default_member_role
- ));
- if($default_member_role === Jeg_Account::APPLICANT) {
- jeg_flash_message('message',sprintf(
- wp_kses(__("Thank you for registering <strong>%s</strong>. You can start your job search or create your resume now.", 'jobplanet-plugin'), jeg_allow_strong()),
- $user['email']), 'alert-success');
- } else if($default_member_role === Jeg_Account::EMPLOYER) {
- jeg_flash_message('message', sprintf(
- wp_kses(__("Thank you for registering <strong>%s</strong>. You can start posting jobs or search for potential candidates now.", 'jobplanet-plugin'), jeg_allow_strong()),
- $user['email']), 'alert-success');
- }
- if ( is_wp_error( $new_customer ) )
- {
- throw new Exception( $new_customer->get_error_message() );
- } else
- {
- // send an email to the admin alerting them of the registration
- wp_new_user_notification($new_customer, $generated_password);
- jeg_send_email(
- esc_html(__('Registration for {site_title}', 'jobplanet-plugin')),
- 'email/register-social-notification',
- $user['email'],
- array(
- 'email' => $user['email'],
- 'password' => $generated_password
- )
- );
- // log the new user in
- wp_set_auth_cookie( $new_customer, true, is_ssl() );
- wp_set_current_user($new_customer, $user['email']);
- }
- }
- public function linkedin_login_url()
- {
- $linkedin = new LinkedIn(array(
- 'api_key' => $this->linkedin_app_id,
- 'api_secret' => $this->linkedin_app_secret,
- 'callback_url' => $this->social_callback_url('linkedin')
- ));
- $authUrl = $linkedin->getLoginUrl(
- array(
- LinkedIn::SCOPE_BASIC_PROFILE,
- LinkedIn::SCOPE_EMAIL_ADDRESS
- )
- );
- return $authUrl;
- }
- public function facebook_login_url ()
- {
- $fb = new Facebook\Facebook(array(
- 'app_id' => $this->fb_app_id,
- 'app_secret' => $this->fb_app_secret,
- 'default_graph_version' => $this->fb_graph_version,
- ));
- $helper = $fb->getRedirectLoginHelper();
- $loginUrl = $helper->getLoginUrl($this->social_callback_url('facebook'), array('email'));
- return $loginUrl;
- }
- public function google_login_url ()
- {
- $client = new Google_Client();
- $client->setApplicationName($this->google_apps_name);
- $client->setClientId($this->google_client_id);
- $client->setClientSecret($this->google_secret);
- $client->addScope("https://www.googleapis.com/auth/userinfo.profile");
- $client->addScope("https://www.googleapis.com/auth/userinfo.email");
- $client->setRedirectUri($this->social_callback_url('google'));
- return $client->createAuthUrl();
- }
- public function social_callback_url($social)
- {
- if ( get_option( 'permalink_structure' ) ) {
- $url = home_url() . '/social-callback/' . $social;
- } else {
- $url = add_query_arg( array( 'social-callback' => $social ), home_url() );
- }
- return $url;
- }
- public function social_url_filter($value, $social)
- {
- return $this->social_callback_url($social);
- }
- }
- Jeg_Account_Social::getInstance();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement