Advertisement
jegtheme

class-jeg-account-social.php

Aug 21st, 2016
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 17.04 KB | None | 0 0
  1. <?php
  2.  
  3. use LinkedIn\LinkedIn;
  4.  
  5. Class Jeg_Account_Social
  6. {
  7.     private static $instance;
  8.  
  9.     // facebook integration
  10.     private $fb_app_id;
  11.     private $fb_app_secret;
  12.     private $fb_graph_version;
  13.  
  14.     // google integration
  15.     private $google_client_id;
  16.     private $google_secret;
  17.     private $google_apps_name;
  18.  
  19.     // linked in integration
  20.     private $linkedin_app_id;
  21.     private $linkedin_app_secret;
  22.  
  23.     public static function getInstance()
  24.     {
  25.         if (null === static::$instance)
  26.         {
  27.             static::$instance = new static();
  28.         }
  29.         return static::$instance;
  30.     }
  31.  
  32.     public function __construct()
  33.     {
  34.         $this->includes();
  35.         $this->hook();
  36.     }
  37.  
  38.     public function includes()
  39.     {
  40.         include_once( JOBPLANET_PLUGIN_DIR . 'external/facebook-sdk-v5/autoload.php' );
  41.         include_once( JOBPLANET_PLUGIN_DIR . 'external/google-api-php-client/src/Google/autoload.php' );
  42.         include_once( JOBPLANET_PLUGIN_DIR . 'external/LinkedIn/LinkedIn.php' );
  43.     }
  44.  
  45.     public function hook()
  46.     {
  47.         // social
  48.         add_action('init', array(&$this, 'init_social_integration'));
  49.         add_action('init', array( &$this , 'add_social_endpoint' ));
  50.         add_action('parse_request', array(&$this, 'social_parse_request'));
  51.         add_filter('query_vars', array( &$this , 'social_query_vars' ));
  52.  
  53.         add_action('jeg_login_social', array(&$this, 'print_login_social'));
  54.         add_action('jeg_register_social', array(&$this, 'print_register_social'));
  55.  
  56.         add_filter('jeg_social_url_filter', array(&$this, 'social_url_filter'), 10, 2);
  57.     }
  58.  
  59.     public function print_register_social()
  60.     {
  61.         $showwhere = vp_option('joption.show_social_login');
  62.         if($showwhere === 'register' || $showwhere === 'both') {
  63.  
  64.             if(vp_option('joption.enable_fb_register')) {
  65.             ?>
  66.                 <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>
  67.             <?php
  68.             }
  69.  
  70.             if(vp_option('joption.enable_google_register')) {
  71.             ?>
  72.                 <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>
  73.             <?php
  74.             }
  75.  
  76.             if(vp_option('joption.enable_linkedin_register')) {
  77.             ?>
  78.                 <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>
  79.             <?php
  80.             }
  81.  
  82.         }
  83.  
  84.         if($showwhere === 'register' && (vp_option('joption.enable_fb_register') || vp_option('joption.enable_google_register') || vp_option('joption.enable_linkedin_register'))) {
  85.             ?>
  86.             <div class="white-space-10"></div>
  87.             <p class="text-center"><span class="span-line"><?php _e('OR', 'jobplanet-plugin'); ?></span></p>
  88.             <?php
  89.         }
  90.  
  91.         if($showwhere === 'both' && (vp_option('joption.enable_fb_register') || vp_option('joption.enable_google_register') || vp_option('joption.enable_linkedin_register'))) {
  92.             ?>
  93.             <div class="white-space-10"></div>
  94.             <p class="text-center"><span class="span-line"><?php _e('OR', 'jobplanet-plugin'); ?></span></p>
  95.             <?php
  96.         }
  97.     }
  98.  
  99.     public function print_login_social()
  100.     {
  101.         $showwhere = vp_option('joption.show_social_login');
  102.         if($showwhere === 'login' || $showwhere === 'both') {
  103.  
  104.             if(vp_option('joption.enable_fb_register')) {
  105.             ?>
  106.                 <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>
  107.             <?php
  108.             }
  109.  
  110.             if(vp_option('joption.enable_google_register')) {
  111.             ?>
  112.                 <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>
  113.             <?php
  114.             }
  115.  
  116.             if(vp_option('joption.enable_linkedin_register')) {
  117.             ?>
  118.                 <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>
  119.             <?php
  120.             }
  121.         }
  122.  
  123.         if(vp_option('joption.enable_fb_register') || vp_option('joption.enable_google_register') || vp_option('joption.enable_linkedin_register')) {
  124.             ?>
  125.                 <div class="white-space-10"></div>
  126.                 <p class="text-center"><span class="span-line"><?php _e('OR', 'jobplanet-plugin'); ?></span></p>
  127.             <?php
  128.         }
  129.     }
  130.  
  131.     public function init_social_integration()
  132.     {
  133.         if(vp_option('joption.enable_fb_register')) {
  134.             $this->fb_app_id                = vp_option('joption.fb_app_id');
  135.             $this->fb_app_secret            = vp_option('joption.fb_app_secret');
  136.             $this->fb_graph_version         = 'v2.2';
  137.         }
  138.  
  139.         if(vp_option('joption.enable_google_register')) {
  140.             $this->google_client_id         = vp_option('joption.google_client_id');
  141.             $this->google_secret            = vp_option('joption.google_secret');
  142.             $this->google_apps_name          = vp_option('joption.google_apps_name');
  143.         }
  144.  
  145.         if(vp_option('joption.enable_linkedin_register')) {
  146.             $this->linkedin_app_id          = vp_option('joption.linkedin_app_id');
  147.             $this->linkedin_app_secret      = vp_option('joption.linkedin_app_secret');
  148.         }
  149.  
  150.     }
  151.  
  152.     public function add_social_endpoint()
  153.     {
  154.         add_rewrite_rule( '^social-callback/([^/]*)/?', 'index.php?social-callback=$matches[1]', 'top' );
  155.     }
  156.  
  157.     public function social_query_vars($vars)
  158.     {
  159.         $vars[] = 'social-callback';
  160.         return $vars;
  161.     }
  162.  
  163.     public function social_parse_request($wp)
  164.     {
  165.         if (array_key_exists('social-callback', $wp->query_vars))
  166.         {
  167.             switch($wp->query_vars['social-callback'])
  168.             {
  169.                 case "facebook" :
  170.                     $this->facebook_login();
  171.                     break;
  172.                 case "google" :
  173.                     $this->google_login();
  174.                     break;
  175.                 case "linkedin" :
  176.                     $this->linkedin_login();
  177.                     break;
  178.             }
  179.         }
  180.     }
  181.  
  182.     public function linkedin_login()
  183.     {
  184.         $linkedin = new LinkedIn(array(
  185.             'api_key' => $this->linkedin_app_id,
  186.             'api_secret' => $this->linkedin_app_secret,
  187.             'callback_url' => $this->social_callback_url('linkedin')
  188.         ));
  189.  
  190.         try {
  191.             $token = $linkedin->getAccessToken($_REQUEST['code']);
  192.  
  193.             if(!isset($token))
  194.             {
  195.                 throw new Exception( '<strong>' . esc_html(__( 'Error', 'jobplanet-plugin' )) . ':</strong> ' . esc_html(__( 'Failed to get Linked in access token', 'jobplanet-plugin' )) );
  196.             }
  197.  
  198.             $info = $linkedin->get('/people/~:(email-address,first-name,last-name)');
  199.             $user_email = $info['emailAddress'];
  200.  
  201.             if(email_exists($user_email))
  202.             {
  203.                 $this->log_user_in_social($user_email);
  204.             } else
  205.             {
  206.                 $this->register_user_social(array(
  207.                     'email'             => $user_email,
  208.                     'first_name'        => $info["firstName"],
  209.                     'last_name'         => $info["lastName"]
  210.                 ));
  211.             }
  212.         } catch (Exception $e )
  213.         {
  214.             jeg_flash_message('message', $e->getMessage(), 'alert-danger');
  215.         }
  216.  
  217.         wp_redirect(  jeg_get_account_url() );
  218.         exit;
  219.     }
  220.  
  221.     public function facebook_login()
  222.     {
  223.         $fb = new Facebook\Facebook(array(
  224.             'app_id' => $this->fb_app_id,
  225.             'app_secret' => $this->fb_app_secret,
  226.             'default_graph_version' => $this->fb_graph_version,
  227.         ));
  228.         $helper = $fb->getRedirectLoginHelper();
  229.  
  230.         try {
  231.             $accessToken = $helper->getAccessToken();
  232.  
  233.             if (! isset($accessToken))
  234.             {
  235.                 if ($helper->getError())
  236.                 {
  237.                     header('HTTP/1.0 401 Unauthorized');
  238.                     echo "Error: " . $helper->getError() . "\n";
  239.                     echo "Error Code: " . $helper->getErrorCode() . "\n";
  240.                     echo "Error Reason: " . $helper->getErrorReason() . "\n";
  241.                     echo "Error Description: " . $helper->getErrorDescription() . "\n";
  242.                 } else
  243.                 {
  244.                     header('HTTP/1.0 400 Bad Request');
  245.                     echo 'Bad request';
  246.                 }
  247.                 exit;
  248.             }
  249.  
  250.             $response = $fb->get('/me?fields=id,name,email,first_name,last_name', $accessToken->getValue());
  251.             $user = $response->getGraphUser();
  252.             $user_email = $user['email'];
  253.  
  254.             if(email_exists($user_email))
  255.             { // user exist
  256.                 $this->log_user_in_social($user_email);
  257.             } else
  258.             { // user not exist
  259.                 $this->register_user_social(array(
  260.                     'email'             => $user_email,
  261.                     'first_name'        => $user["first_name"],
  262.                     'last_name'         => $user["last_name"]
  263.                 ));
  264.             }
  265.         } catch (Exception $e )
  266.         {
  267.             if ($e instanceof Facebook\Exceptions\FacebookResponseException)
  268.             {
  269.                 jeg_flash_message('message', 'Graph returned an error: ' . $e->getMessage(), 'alert-danger');
  270.             } else if ($e instanceof Facebook\Exceptions\FacebookSDKException)
  271.             {
  272.                 jeg_flash_message('message', 'Facebook SDK returned an error : ' . $e->getMessage(), 'alert-danger');
  273.             } else
  274.             {
  275.                 jeg_flash_message('message', $e->getMessage(), 'alert-danger');
  276.             }
  277.         }
  278.  
  279.         wp_redirect(  jeg_get_account_url() );
  280.         exit;
  281.     }
  282.  
  283.     public function google_login()
  284.     {
  285.         $client = new Google_Client();
  286.         $client->setApplicationName( $this->google_apps_name );
  287.         $client->setClientId( $this->google_client_id );
  288.         $client->setClientSecret( $this->google_secret );
  289.         $client->setRedirectUri( $this->social_callback_url('google') );
  290.  
  291.         try {
  292.  
  293.             if(!isset($_GET['code']))
  294.             {
  295.                 throw new Exception( '<strong>' . esc_html(__( 'Error', 'jobplanet-plugin' )) . ':</strong> ' . esc_html(__( 'Failed to get Google Code', 'jobplanet-plugin' )) );
  296.             } else
  297.             {
  298.                 if (!isset($_GET['code']))
  299.                 {
  300.                     header('HTTP/1.0 400 Bad Request');
  301.                     echo 'Bad request';
  302.                     exit;
  303.                 }
  304.  
  305.                 // get access token
  306.                 $client->authenticate($_GET['code']);
  307.                 $access_token = $client->getAccessToken();
  308.                 $client->setAccessToken($access_token);
  309.  
  310.                 // use access token to get detail user
  311.                 $plus = new Google_Service_Plus($client);
  312.                 $user = $plus->people->get("me");
  313.  
  314.                 $user_email = $user['emails'][0]['value'];
  315.  
  316.                 if(email_exists($user_email))
  317.                 { // user exist
  318.                     $this->log_user_in_social($user_email);
  319.                 } else
  320.                 { // user not exist
  321.                     $this->register_user_social(array(
  322.                         'email'             => $user_email,
  323.                         'first_name'        => $user['name']['givenName'],
  324.                         'last_name'         => $user['name']['familyName']
  325.                     ));
  326.                 }
  327.             }
  328.         } catch (Exception $e )
  329.         {
  330.             jeg_flash_message('message', $e->getMessage(), 'alert-danger');
  331.         }
  332.  
  333.         wp_redirect(  jeg_get_account_url() );
  334.         exit;
  335.     }
  336.  
  337.     public function log_user_in_social ($user_email)
  338.     {
  339.         // get user detail
  340.         $userdetail = get_user_by('email', $user_email);
  341.  
  342.         // log the new user in
  343.         wp_set_auth_cookie($userdetail->ID, true, is_ssl());
  344.         wp_set_current_user($userdetail->ID);
  345.     }
  346.  
  347.     public function register_user_social($user)
  348.     {
  349.         $generated_password = wp_generate_password();
  350.         $default_member_role = vp_option('joption.default_role_social', Jeg_Account::APPLICANT);
  351.         $new_customer = wp_insert_user(array(
  352.             'user_login'        => $user['email'],
  353.             'user_pass'         => $generated_password,
  354.             'user_email'        => $user['email'],
  355.             'first_name'        => $user["first_name"],
  356.             'last_name'         => $user["last_name"],
  357.             'user_registered'   => date('Y-m-d H:i:s'),
  358.             'role'              => $default_member_role
  359.         ));
  360.  
  361.         if($default_member_role === Jeg_Account::APPLICANT) {
  362.             jeg_flash_message('message',sprintf(
  363.                 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()),
  364.                     $user['email']), 'alert-success');
  365.         } else if($default_member_role === Jeg_Account::EMPLOYER) {
  366.             jeg_flash_message('message', sprintf(
  367.                 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()),
  368.                     $user['email']), 'alert-success');
  369.         }
  370.  
  371.         if ( is_wp_error( $new_customer ) )
  372.         {
  373.             throw new Exception( $new_customer->get_error_message() );
  374.         } else
  375.         {
  376.             // send an email to the admin alerting them of the registration
  377.             wp_new_user_notification($new_customer, $generated_password);
  378.  
  379.             jeg_send_email(
  380.                 esc_html(__('Registration for {site_title}', 'jobplanet-plugin')),
  381.                 'email/register-social-notification',
  382.                 $user['email'],
  383.                 array(
  384.                     'email' => $user['email'],
  385.                     'password' => $generated_password
  386.                 )
  387.             );
  388.  
  389.             // log the new user in
  390.             wp_set_auth_cookie( $new_customer, true, is_ssl() );
  391.             wp_set_current_user($new_customer, $user['email']);
  392.         }
  393.     }
  394.  
  395.  
  396.     public function linkedin_login_url()
  397.     {
  398.         $linkedin = new LinkedIn(array(
  399.             'api_key' => $this->linkedin_app_id,
  400.             'api_secret' => $this->linkedin_app_secret,
  401.             'callback_url' => $this->social_callback_url('linkedin')
  402.         ));
  403.  
  404.         $authUrl = $linkedin->getLoginUrl(
  405.             array(
  406.                 LinkedIn::SCOPE_BASIC_PROFILE,
  407.                 LinkedIn::SCOPE_EMAIL_ADDRESS
  408.             )
  409.         );
  410.  
  411.         return $authUrl;
  412.     }
  413.  
  414.     public function facebook_login_url ()
  415.     {
  416.         $fb = new Facebook\Facebook(array(
  417.             'app_id' => $this->fb_app_id,
  418.             'app_secret' => $this->fb_app_secret,
  419.             'default_graph_version' => $this->fb_graph_version,
  420.         ));
  421.  
  422.         $helper = $fb->getRedirectLoginHelper();
  423.         $loginUrl = $helper->getLoginUrl($this->social_callback_url('facebook'), array('email'));
  424.         return $loginUrl;
  425.     }
  426.  
  427.     public function google_login_url ()
  428.     {
  429.         $client = new Google_Client();
  430.         $client->setApplicationName($this->google_apps_name);
  431.         $client->setClientId($this->google_client_id);
  432.         $client->setClientSecret($this->google_secret);
  433.         $client->addScope("https://www.googleapis.com/auth/userinfo.profile");
  434.         $client->addScope("https://www.googleapis.com/auth/userinfo.email");
  435.  
  436.         $client->setRedirectUri($this->social_callback_url('google'));
  437.         return $client->createAuthUrl();
  438.     }
  439.  
  440.     public function social_callback_url($social)
  441.     {
  442.         if ( get_option( 'permalink_structure' ) ) {
  443.             $url = home_url() . '/social-callback/' . $social;
  444.         } else {
  445.             $url = add_query_arg( array( 'social-callback' => $social ), home_url() );
  446.         }
  447.  
  448.         return $url;
  449.     }
  450.  
  451.     public function social_url_filter($value, $social)
  452.     {
  453.         return $this->social_callback_url($social);
  454.     }
  455. }
  456.  
  457. Jeg_Account_Social::getInstance();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement