Example #1
0
/**
 * Attempts to get the necessary details from memberful and set them
 * using the wordpress settings API
 *
 * @param $code string The activation code
 */
function memberful_wp_activate($code)
{
    $params = array('requirements' => array('oauth', 'api_key', 'webhook'), 'activation_code' => trim($code), 'app_name' => trim(memberful_wp_site_name()), 'oauth_redirect_url' => memberful_wp_oauth_callback_url(), 'webhook_url' => memberful_wp_webhook_url());
    $response = memberful_wp_post_data_to_api_as_json(memberful_activation_url(), $params);
    if (is_wp_error($response)) {
        return new WP_Error('memberful_activation_request_error', "We had trouble connecting to Memberful, please email info@memberful.com. ({$response->get_error_message()})");
    }
    $response_code = (int) wp_remote_retrieve_response_code($response);
    $response_body = wp_remote_retrieve_body($response);
    if (404 === $response_code) {
        return new WP_Error('memberful_activation_code_invalid', "It looks like your activation code is wrong. Please try again, and if this keeps happening email us at info@memberful.com.");
    }
    if ($response_code !== 200 || empty($response_body)) {
        return new WP_Error('memberful_activation_fail', "We couldn't connect to Memberful, please email info@memberful.com");
    }
    $credentials = json_decode($response_body);
    update_option('memberful_client_id', $credentials->oauth->identifier);
    update_option('memberful_client_secret', $credentials->oauth->secret);
    update_option('memberful_api_key', $credentials->api_key->key);
    update_option('memberful_site', $credentials->site);
    update_option('memberful_webhook_secret', $credentials->webhook->secret);
    // Ideally we'd modify the activation payload to send this info, but it's easier to do this "short-term".
    memberful_wp_send_site_options_to_memberful();
    return TRUE;
}
Example #2
0
 /**
  * Gets the access token and refresh token from an authorization code
  *
  * @param string $auth_code The authorization code returned from OAuth endpoint
  * @return StdObject Access token and Refresh token
  */
 public function get_oauth_tokens($auth_code)
 {
     $params = array('client_id' => get_option('memberful_client_id'), 'client_secret' => get_option('memberful_client_secret'), 'grant_type' => 'authorization_code', 'code' => $auth_code);
     $response = memberful_wp_post_data_to_api_as_json(self::oauth_member_url('token'), $params);
     if (is_wp_error($response)) {
         memberful_wp_record_wp_error($response);
         return $this->_error('could_not_get_tokens', $response);
     }
     $body = json_decode($response['body']);
     $code = $response['response']['code'];
     if ($code != 200 || $body === NULL || empty($body->access_token)) {
         $payload = array('code' => 'oauth_access_fail', 'error' => 'Could not get access token from Memberful', 'response' => $response);
         memberful_wp_record_error($payload);
         return $this->_error($payload['code'], $payload['error']);
     }
     return json_decode($response['body']);
 }