Advertisement
Guest User

Untitled

a guest
Jun 6th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.26 KB | None | 0 0
  1. <?php
  2. if (session_status() == PHP_SESSION_NONE) {
  3. session_start();
  4. }
  5. require_once(userpro_vk_path . 'lib/vendor/autoload.php');
  6. use VK\Client\VKApiClient;
  7. use VK\OAuth\VKOAuth;
  8. use VK\OAuth\Scopes\VKOAuthUserScope;
  9. use VK\OAuth\VKOAuthResponseType;
  10. use VK\OAuth\VKOAuthDisplay;
  11.  
  12. class userpro_vk_api
  13. {
  14.  
  15. var $vk;
  16. var $vk_config;
  17. var $client_id, $client_secret, $redirect_uri;
  18.  
  19. public function __construct()
  20. {
  21. add_action('init', array(&$this, 'loadVK'), 9);
  22.  
  23.  
  24. if (isset($_GET['code']))
  25. add_action('init', array(&$this, 'getAccesKey'), 10);
  26.  
  27.  
  28. add_action('userpro_social_connect_buttons', array(&$this, 'vk_button'), 9);
  29.  
  30.  
  31. }
  32.  
  33. public function loadVK()
  34. {
  35. $this->client_id = userpro_vk_get_option('vk_api_id');
  36. $this->client_secret = userpro_vk_get_option('vk_api_secret');
  37. $this->redirect_uri = userpro_vk_get_option('vk_signup_redirect') . '/';
  38.  
  39. if (userpro_vk_get_option('vk_connect') == 1 && userpro_vk_get_option('vk_api_id') && userpro_vk_get_option('vk_api_secret')) {
  40.  
  41.  
  42. $vk = new VKApiClient();
  43.  
  44. }
  45.  
  46. }
  47.  
  48. /* Get VK button */
  49. public function vk_button()
  50. {
  51. if (userpro_vk_get_option('vk_connect') == 1 && userpro_vk_get_option('vk_api_id') && userpro_vk_get_option('vk_api_secret')) {
  52. $url = $this->browserUrl();
  53.  
  54.  
  55. ?>
  56.  
  57. <a href="<?php echo $url ?>" class="userpro-social-vk userpro-tip" title="<?php _e('Login with VK.com', 'userpro-vk'); ?>"></a>
  58.  
  59. <?php
  60. }
  61. }
  62.  
  63. public function browserUrl()
  64. {
  65.  
  66. $oauth = new VKOAuth();
  67. $display = VKOAuthDisplay::PAGE;
  68. $scope = array(VKOAuthUserScope::WALL, VKOAuthUserScope::GROUPS);
  69. $state = rand();
  70.  
  71. $browser_url = $oauth->getAuthorizeUrl(VKOAuthResponseType::CODE, $this->client_id, $this->redirect_uri, $display, $scope, $state);
  72.  
  73.  
  74. return $browser_url;
  75.  
  76. }
  77.  
  78. public function getAccesKey()
  79. {
  80. $oauth = new VKOAuth();
  81. $code = $_GET['code'];
  82.  
  83. $response = $oauth->getAccessToken($this->client_id, $this->client_secret, $this->redirect_uri, $code);
  84.  
  85. $this->getInfo($response['access_token'], $response['user_id']);
  86.  
  87.  
  88. }
  89.  
  90. public function getInfo($access_token, $user_id)
  91. {
  92. global $userpro;
  93. $vk = new VKApiClient();
  94. $response = $vk->users()->get($access_token, array(
  95. 'user_ids' => array($user_id),
  96. 'fields' => array('uid', 'country', 'first_name', 'last_name', 'photo_big', 'email', 'sex',),
  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. $this->update_vk_profile($user[0]->ID, $response);
  111.  
  112.  
  113. $this->vk_auto_login($user[0]->user_login, true);
  114.  
  115. exit;
  116.  
  117. } else {
  118. if (!is_user_logged_in())
  119. $this->register($response);
  120. }
  121.  
  122. }
  123.  
  124. }
  125.  
  126. public function register($user)
  127. {
  128.  
  129. global $userpro;
  130.  
  131. $user_info = [];
  132.  
  133. $user_info['id'] = $id = $user[0]['id'];
  134. $user_info['user_pass'] = wp_generate_password($length = 12, $include_standard_special_chars = false);
  135. $user_info['first_name'] = $user[0]['first_name'];
  136. $user_info['last_name'] = $user[0]['last_name'];
  137. $user_info['photo'] = $user[0]['photo_big'];
  138.  
  139. $login_name = sanitize_text_field($user_info['first_name'] . '_' . $user_info['last_name']);
  140.  
  141.  
  142. $password = wp_generate_password($length = 12, $include_standard_special_chars = false);
  143.  
  144.  
  145. $user_id = $this->new_user($login_name, $user_info['user_pass'], '', $user_info, $type = 'vk');
  146.  
  147. $this->update_vk_profile($user_id, $user);
  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. $this->vk_auto_login($login_name, true);
  154.  
  155. }
  156.  
  157. // Create user
  158. public function new_user($username, $password, $email, $form, $type, $approved = 1)
  159. {
  160. global $userpro;
  161.  
  162. if (username_exists($username))
  163. $username = $username . rand(0, 100);
  164.  
  165. $user_id = wp_create_user($username, $password, $email);
  166.  
  167. $userpro->default_role($user_id, $form);
  168.  
  169. if ($approved == 1) {
  170. userpro_mail($user_id, 'newaccount', $password, $form);
  171. do_action('userpro_after_new_registration', $user_id);
  172. }
  173. return $user_id;
  174. }
  175.  
  176. public function update_vk_profile($user_id, $user_info)
  177. {
  178.  
  179.  
  180. // Display name FirstName_LastName
  181. $display_name = $user_info[0]['first_name'] . '_' . $user_info[0]['last_name'];
  182. // gender 1- male , 2-female
  183. $gender = ($user_info[0]['sex'] == 1) ? 'male' : 'female';
  184.  
  185. if (!empty($user_info[0]['country']['title']))
  186. update_user_meta($user_id, 'country', $user_info[0]['country']['title']);
  187.  
  188. if (!empty($display_name)) {
  189. wp_update_user(array('ID' => $user_id, 'display_name' => $display_name));
  190. update_user_meta($user_id, 'display_name', $display_name);
  191. update_user_meta($user_id, 'first_name', $user_info[0]['first_name']);
  192. update_user_meta($user_id, 'last_name', $user_info[0]['last_name']);
  193. }
  194. if (!empty($gender)) {
  195. update_user_meta($user_id, 'gender', ucfirst($gender));
  196. }
  197.  
  198. }
  199. /* Update profile picture */
  200.  
  201.  
  202. /* update VK ID */
  203.  
  204. public function update_vk_id($user_id, $id)
  205. {
  206.  
  207. update_user_meta($user_id, 'userpro_vk_id', $id);
  208.  
  209. }
  210.  
  211. public function add_profile_picture($user_id, $image)
  212. {
  213.  
  214. global $userpro;
  215.  
  216. $profile_pic = 'profile_picture';
  217.  
  218. $userpro->do_uploads_dir($user_id);
  219.  
  220. if ($image) {
  221. $userpro->move_file($user_id, $image, $profile_pic . '.jpg');
  222. update_user_meta($user_id, 'profilepicture', $userpro->get_uploads_url($user_id) . $profile_pic . '.jpg');
  223. }
  224.  
  225. }
  226.  
  227. /* Check if user is vk user */
  228. public function is_vk_user($user_id)
  229. {
  230. $usermeta = get_user_meta($user_id, 'userpro_vk_id', true);
  231. if ($usermeta)
  232. return true;
  233. return false;
  234. }
  235.  
  236. /* Get/show badge for VK! users */
  237. public function userpro_get_badge($badge, $user_id = null, $tooltip = null)
  238. {
  239. global $userpro;
  240. switch ($badge) {
  241. case 'vk':
  242. return '<img class="userpro-profile-badge" src="' . userpro_vk_url . 'img/badge-vk.png' . '" alt="" title="' . __('VK.com Linked', 'userpro') . '" />';
  243. break;
  244. }
  245. }
  246.  
  247.  
  248. public function userpro_auto_login($username, $remember = true)
  249. {
  250.  
  251. if (!empty($username)) {
  252.  
  253. if (!is_user_logged_in()) {
  254. $user = get_user_by('login', $username);
  255. $user_id = $user->ID;
  256. wp_set_current_user($user_id, $username);
  257. wp_set_auth_cookie($user_id, $remember);
  258. do_action('wp_login', $username, $user);
  259.  
  260. } else {
  261. wp_logout();
  262. $user = get_user_by('login', $username);
  263. $user_id = $user->ID;
  264. wp_set_current_user($user_id, $username);
  265. wp_set_auth_cookie($user_id, $remember);
  266. do_action('wp_login', $username, $user);
  267. }
  268. ob_end_flush();
  269. }
  270. }
  271.  
  272.  
  273. public function vk_auto_login($username, $remember = true)
  274. {
  275.  
  276. if (!is_user_logged_in()) {
  277. up_error('loginas veikia ');
  278. ob_start();
  279. add_action('init', 'app_output_buffer');
  280.  
  281. global $userpro;
  282.  
  283. $user = get_user_by('login', $username);
  284. // Redirect URL //
  285. if (!is_wp_error($user)) {
  286. wp_clear_auth_cookie();
  287. wp_set_current_user($user->ID);
  288. wp_set_auth_cookie($user->ID);
  289.  
  290. wp_safe_redirect($userpro->permalink());
  291.  
  292. exit();
  293. }
  294. ob_end_flush();
  295. }
  296. }
  297.  
  298.  
  299. }
  300.  
  301. $userpro_vk = new userpro_vk_api();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement