Advertisement
Guest User

Untitled

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