Пример #1
0
 /**
  * Callback for the `authenticate` hook.
  *
  * @return WP_User The user to be logged in or NULL if user couldn't be
  * determined
  */
 public function init($user, $username, $password)
 {
     // If another authentication system has handled this request
     if ($user instanceof WP_User) {
         return $user;
     }
     // This is the OAuth response
     if (isset($_GET['code'])) {
         $tokens = $this->get_oauth_tokens($_GET['code']);
         $account = $this->get_member_data($tokens->access_token);
         $user = memberful_wp_sync_member_account($account, array('refresh_token' => $tokens->refresh_token));
         if (is_wp_error($user) && $user->get_error_code() === 'user_already_exists') {
             $error_data = $user->get_error_data();
             return $this->ask_user_to_verify_they_want_to_sync_accounts($error_data['existing_user'], $error_data['member'], $error_data['context']);
         }
         return $user;
     } elseif (isset($_GET['error'])) {
         // For some reason we got an error code.
         return $this->_error('memberful_oauth_error', isset($_GET['error']) ? $_GET['error'] : __("Unknown error.", "memberful"));
     }
     // Store where the user came from in a cookie
     $expire = time() + 30 * 60;
     // 30 minutes
     $referer = $_SERVER['HTTP_REFERER'];
     // Allow overriding of redirect location
     if (isset($_REQUEST['redirect_to'])) {
         $referer = $_REQUEST['redirect_to'];
     }
     setcookie('memberful_redirect', $referer, $expire, '/', COOKIE_DOMAIN, is_ssl(), true);
     // Send the user to Memberful
     wp_redirect(self::oauth_auth_url(), 302);
     exit;
 }
Пример #2
0
function memberful_wp_sync_member_from_memberful($member_id, $mapping_context = array())
{
    $member_id = (int) $member_id;
    $account = memberful_api_member($member_id);
    if (is_wp_error($account)) {
        memberful_wp_record_error(array('error' => $account->get_error_messages()));
        return $account;
    }
    return memberful_wp_sync_member_account($account, $mapping_context);
}