Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- 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 VkAuth
- {
- var $vk;
- var $vk_config;
- var $client_id,$client_secret,$redirect_uri;
- public function __construct()
- {
- add_action('init', array(&$this, 'loadVK'), 9);
- $this->client_id = 6495548;
- $this->client_secret = 'yWerZyUwPYCxLyxlYXNb';
- $this->redirect_uri = 'http://www.userproplugin.com/profile/';
- 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()
- {
- 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 = 'secret_state_code';
- $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']);
- // $access_token = $response['access_token'];
- }
- public function getInfo($access_token, $user_id)
- {
- $vk = new VKApiClient();
- $response = $vk->users()->get($access_token, array(
- 'user_ids' => array($user_id),
- 'fields' => array('uid', 'screen_name', 'city', 'first_name','last_name','nickname', 'photo_big','email'),
- ));
- 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)){
- userpro_auto_login( $user[0]->user_login, true );
- }else{
- $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['nickname'] = $user[0]['nickname'];
- $user_info['screen_name'] = $user[0]['screen_name'];
- $user_info['photo'] = $user[0]['photo_big'];
- $login_name = $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' );
- up_error('user ID ', $user_id);
- $this->update_vk_id($user_id, $user_info['id'] );
- userpro_auto_login( $login_name, true );
- }
- public function new_user($username, $password, $email, $form, $type, $approved=1) {
- global $userpro;
- $user_id = wp_create_user( $username, $password, $email );
- $userpro->default_role($user_id, $form);
- // $this->update_profile_via_vk( $user_id, $form );
- // $this->update_profile_pic_vk( $user_id, $form );
- if ($approved==1){
- userpro_mail($user_id, 'newaccount', $password, $form );
- do_action('userpro_after_new_registration', $user_id);
- }
- return $user_id;
- }
- /* update VK ID */
- public function update_vk_id($user_id, $id){
- update_user_meta($user_id, 'userpro_vk_id', $id);
- }
- }
- $vk = new VkAuth();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement