Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if (session_status() == PHP_SESSION_NONE) {
- session_start();
- }
- require_once(userpro_vk_path . 'lib/vendor/autoload.php');
- use VK\Client\VKApiClient;
- use VK\OAuth\VKOAuth;
- use VK\OAuth\Scopes\VKOAuthUserScope;
- use VK\OAuth\VKOAuthResponseType;
- use VK\OAuth\VKOAuthDisplay;
- class userpro_vk_api
- {
- var $vk;
- var $vk_config;
- var $client_id, $client_secret, $redirect_uri;
- public function __construct()
- {
- add_action('init', array(&$this, 'loadVK'), 9);
- if (isset($_GET['code']))
- add_action('init', array(&$this, 'getAccesKey'), 10);
- add_action('userpro_social_connect_buttons', array(&$this, 'vk_button'), 9);
- }
- public function loadVK()
- {
- $this->client_id = userpro_vk_get_option('vk_api_id');
- $this->client_secret = userpro_vk_get_option('vk_api_secret');
- $this->redirect_uri = userpro_vk_get_option('vk_signup_redirect') . '/';
- if (userpro_vk_get_option('vk_connect') == 1 && userpro_vk_get_option('vk_api_id') && userpro_vk_get_option('vk_api_secret')) {
- $vk = new VKApiClient();
- }
- }
- /* Get VK button */
- public function vk_button()
- {
- if (userpro_vk_get_option('vk_connect') == 1 && userpro_vk_get_option('vk_api_id') && userpro_vk_get_option('vk_api_secret')) {
- $url = $this->browserUrl();
- ?>
- <a href="<?php echo $url ?>" class="userpro-social-vk userpro-tip" title="<?php _e('Login with VK.com', 'userpro-vk'); ?>"></a>
- <?php
- }
- }
- public function browserUrl()
- {
- $oauth = new VKOAuth();
- $display = VKOAuthDisplay::PAGE;
- $scope = array(VKOAuthUserScope::WALL, VKOAuthUserScope::GROUPS);
- $state = rand();
- $browser_url = $oauth->getAuthorizeUrl(VKOAuthResponseType::CODE, $this->client_id, $this->redirect_uri, $display, $scope, $state);
- return $browser_url;
- }
- public function getAccesKey()
- {
- $oauth = new VKOAuth();
- $code = $_GET['code'];
- $response = $oauth->getAccessToken($this->client_id, $this->client_secret, $this->redirect_uri, $code);
- $this->getInfo($response['access_token'], $response['user_id']);
- }
- public function getInfo($access_token, $user_id)
- {
- global $userpro;
- $vk = new VKApiClient();
- $response = $vk->users()->get($access_token, array(
- 'user_ids' => array($user_id),
- 'fields' => array('uid', 'country', 'first_name', 'last_name', 'photo_big', 'email', 'sex',),
- ));
- if (isset($response)) {
- $uid = isset($response['id']) ? $response['id'] : null;
- $user = get_users([
- 'meta_key' => 'userpro_vk_id',
- 'meta_value' => $uid,
- 'meta_compare' => '='
- ]);
- if (!empty($user)) {
- $this->update_vk_profile($user[0]->ID, $response);
- $this->vk_auto_login($user[0]->user_login, true);
- exit;
- } else {
- if (!is_user_logged_in())
- $this->register($response);
- }
- }
- }
- public function register($user)
- {
- global $userpro;
- $user_info = [];
- $user_info['id'] = $id = $user[0]['id'];
- $user_info['user_pass'] = wp_generate_password($length = 12, $include_standard_special_chars = false);
- $user_info['first_name'] = $user[0]['first_name'];
- $user_info['last_name'] = $user[0]['last_name'];
- $user_info['photo'] = $user[0]['photo_big'];
- $login_name = sanitize_text_field($user_info['first_name'] . '_' . $user_info['last_name']);
- $password = wp_generate_password($length = 12, $include_standard_special_chars = false);
- $user_id = $this->new_user($login_name, $user_info['user_pass'], '', $user_info, $type = 'vk');
- $this->update_vk_profile($user_id, $user);
- // Add profile image
- $this->add_profile_picture($user_id, $user_info['photo']);
- $this->update_vk_id($user_id, $user_info['id']);
- $this->vk_auto_login($login_name, true);
- }
- // Create user
- public function new_user($username, $password, $email, $form, $type, $approved = 1)
- {
- global $userpro;
- if (username_exists($username))
- $username = $username . rand(0, 100);
- $user_id = wp_create_user($username, $password, $email);
- $userpro->default_role($user_id, $form);
- if ($approved == 1) {
- userpro_mail($user_id, 'newaccount', $password, $form);
- do_action('userpro_after_new_registration', $user_id);
- }
- return $user_id;
- }
- public function update_vk_profile($user_id, $user_info)
- {
- // Display name FirstName_LastName
- $display_name = $user_info[0]['first_name'] . '_' . $user_info[0]['last_name'];
- // gender 1- male , 2-female
- $gender = ($user_info[0]['sex'] == 1) ? 'male' : 'female';
- if (!empty($user_info[0]['country']['title']))
- update_user_meta($user_id, 'country', $user_info[0]['country']['title']);
- if (!empty($display_name)) {
- wp_update_user(array('ID' => $user_id, 'display_name' => $display_name));
- update_user_meta($user_id, 'display_name', $display_name);
- update_user_meta($user_id, 'first_name', $user_info[0]['first_name']);
- update_user_meta($user_id, 'last_name', $user_info[0]['last_name']);
- }
- if (!empty($gender)) {
- update_user_meta($user_id, 'gender', ucfirst($gender));
- }
- }
- /* Update profile picture */
- /* update VK ID */
- public function update_vk_id($user_id, $id)
- {
- update_user_meta($user_id, 'userpro_vk_id', $id);
- }
- public function add_profile_picture($user_id, $image)
- {
- global $userpro;
- $profile_pic = 'profile_picture';
- $userpro->do_uploads_dir($user_id);
- if ($image) {
- $userpro->move_file($user_id, $image, $profile_pic . '.jpg');
- update_user_meta($user_id, 'profilepicture', $userpro->get_uploads_url($user_id) . $profile_pic . '.jpg');
- }
- }
- /* Check if user is vk user */
- public function is_vk_user($user_id)
- {
- $usermeta = get_user_meta($user_id, 'userpro_vk_id', true);
- if ($usermeta)
- return true;
- return false;
- }
- /* Get/show badge for VK! users */
- public function userpro_get_badge($badge, $user_id = null, $tooltip = null)
- {
- global $userpro;
- switch ($badge) {
- case 'vk':
- return '<img class="userpro-profile-badge" src="' . userpro_vk_url . 'img/badge-vk.png' . '" alt="" title="' . __('VK.com Linked', 'userpro') . '" />';
- break;
- }
- }
- public function userpro_auto_login($username, $remember = true)
- {
- if (!empty($username)) {
- if (!is_user_logged_in()) {
- $user = get_user_by('login', $username);
- $user_id = $user->ID;
- wp_set_current_user($user_id, $username);
- wp_set_auth_cookie($user_id, $remember);
- do_action('wp_login', $username, $user);
- } else {
- wp_logout();
- $user = get_user_by('login', $username);
- $user_id = $user->ID;
- wp_set_current_user($user_id, $username);
- wp_set_auth_cookie($user_id, $remember);
- do_action('wp_login', $username, $user);
- }
- ob_end_flush();
- }
- }
- public function vk_auto_login($username, $remember = true)
- {
- if (!is_user_logged_in()) {
- up_error('loginas veikia ');
- ob_start();
- add_action('init', 'app_output_buffer');
- global $userpro;
- $user = get_user_by('login', $username);
- // Redirect URL //
- if (!is_wp_error($user)) {
- wp_clear_auth_cookie();
- wp_set_current_user($user->ID);
- wp_set_auth_cookie($user->ID);
- wp_safe_redirect($userpro->permalink());
- exit();
- }
- ob_end_flush();
- }
- }
- }
- $userpro_vk = new userpro_vk_api();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement