Advertisement
Guest User

Untitled

a guest
Jun 4th, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. <?php
  2. session_start();
  3. require_once(userpro_vk_path . 'lib/vendor/autoload.php');
  4. use VK\Client\VKApiClient;
  5. use VK\OAuth\VKOAuth;
  6. use VK\OAuth\Scopes\VKOAuthUserScope;
  7. use VK\OAuth\VKOAuthResponseType;
  8. use VK\OAuth\VKOAuthDisplay;
  9.  
  10. class VkAuth
  11. {
  12.  
  13. var $vk;
  14. var $vk_config;
  15. var $client_id,$client_secret,$redirect_uri;
  16. public function __construct()
  17. {
  18. add_action('init', array(&$this, 'loadVK'), 9);
  19.  
  20. $this->client_id = 6495548;
  21. $this->client_secret = 'yWerZyUwPYCxLyxlYXNb';
  22. $this->redirect_uri = 'http://www.userproplugin.com/profile/';
  23. if(isset($_GET['code']))
  24. add_action('init', array(&$this, 'getAccesKey'), 10);
  25.  
  26.  
  27.  
  28. add_action('userpro_social_connect_buttons', array(&$this, 'vk_button'), 9);
  29.  
  30.  
  31. }
  32.  
  33. public function loadVK()
  34. {
  35.  
  36. if (userpro_vk_get_option('vk_connect') == 1 && userpro_vk_get_option('vk_api_id') && userpro_vk_get_option('vk_api_secret')) {
  37.  
  38.  
  39. $vk = new VKApiClient();
  40.  
  41. }
  42.  
  43. }
  44.  
  45. /* Get VK button */
  46. public function vk_button()
  47. {
  48. if (userpro_vk_get_option('vk_connect') == 1 && userpro_vk_get_option('vk_api_id') && userpro_vk_get_option('vk_api_secret')) {
  49. $url = $this->browserUrl();
  50.  
  51.  
  52. ?>
  53.  
  54. <a href="<?php echo $url ?>" class="userpro-social-vk userpro-tip" title="<?php _e('Login with VK.com', 'userpro-vk'); ?>"></a>
  55.  
  56. <?php
  57. }
  58. }
  59.  
  60. public function browserUrl()
  61. {
  62.  
  63. $oauth = new VKOAuth();
  64. $display = VKOAuthDisplay::PAGE;
  65. $scope = array(VKOAuthUserScope::WALL, VKOAuthUserScope::GROUPS);
  66. $state = 'secret_state_code';
  67.  
  68. $browser_url = $oauth->getAuthorizeUrl(VKOAuthResponseType::CODE, $this->client_id, $this->redirect_uri, $display, $scope, $state);
  69.  
  70.  
  71. return $browser_url;
  72.  
  73. }
  74.  
  75. public function getAccesKey()
  76. {
  77.  
  78. $oauth = new VKOAuth();
  79. $code = $_GET['code'];
  80.  
  81. $response = $oauth->getAccessToken($this->client_id, $this->client_secret, $this->redirect_uri, $code);
  82.  
  83. $this->getInfo($response['access_token'], $response['user_id']);
  84.  
  85.  
  86. // $access_token = $response['access_token'];
  87.  
  88. }
  89.  
  90. public function getInfo($access_token, $user_id)
  91. {
  92.  
  93. $vk = new VKApiClient();
  94. $response = $vk->users()->get($access_token, array(
  95. 'user_ids' => array($user_id),
  96. 'fields' => array('uid', 'screen_name', 'city', 'first_name','last_name','nickname', 'photo_big','email'),
  97. ));
  98.  
  99. if(isset($response)){
  100.  
  101. $uid = isset($response['id']) ? $response['id'] : null;
  102.  
  103. $user = get_users([
  104. 'meta_key' => 'userpro_vk_id',
  105. 'meta_value' => $uid,
  106. 'meta_compare' => '='
  107. ]);
  108.  
  109. if(!empty($user)){
  110. userpro_auto_login( $user[0]->user_login, true );
  111. }else{
  112. $this->register($response);
  113. }
  114.  
  115. }
  116.  
  117. }
  118.  
  119. public function register($user){
  120.  
  121. global $userpro;
  122.  
  123. $user_info = [];
  124.  
  125. $user_info['id'] = $id = $user[0]['id'];
  126. $user_info['user_pass'] = wp_generate_password( $length=12, $include_standard_special_chars=false );
  127. $user_info['first_name'] = $user[0]['first_name'];
  128. $user_info['last_name'] = $user[0]['last_name'];
  129. $user_info['nickname'] = $user[0]['nickname'];
  130. $user_info['screen_name'] = $user[0]['screen_name'];
  131. $user_info['photo'] = $user[0]['photo_big'];
  132.  
  133. $login_name = $user_info['first_name'].'_'.$user_info['last_name'];
  134.  
  135. $password = wp_generate_password( $length=12, $include_standard_special_chars=false );
  136.  
  137.  
  138.  
  139. $user_id = $this->new_user( $login_name, $user_info['user_pass'], '', $user_info, $type='vk' );
  140.  
  141. up_error('user ID ', $user_id);
  142.  
  143. $this->update_vk_id($user_id, $user_info['id'] );
  144. userpro_auto_login( $login_name, true );
  145.  
  146.  
  147. }
  148.  
  149. public function new_user($username, $password, $email, $form, $type, $approved=1) {
  150. global $userpro;
  151.  
  152. $user_id = wp_create_user( $username, $password, $email );
  153.  
  154. $userpro->default_role($user_id, $form);
  155.  
  156. // $this->update_profile_via_vk( $user_id, $form );
  157. // $this->update_profile_pic_vk( $user_id, $form );
  158.  
  159. if ($approved==1){
  160. userpro_mail($user_id, 'newaccount', $password, $form );
  161. do_action('userpro_after_new_registration', $user_id);
  162. }
  163.  
  164. return $user_id;
  165. }
  166.  
  167. /* update VK ID */
  168.  
  169. public function update_vk_id($user_id, $id){
  170. update_user_meta($user_id, 'userpro_vk_id', $id);
  171. }
  172.  
  173.  
  174. }
  175.  
  176. $vk = new VkAuth();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement