public function activate() { $response = wp_remote_post(memberful_activation_url(), array('headers' => array('Content-Type' => 'application/json'), 'body' => json_encode($this->params), 'sslverify' => MEMBERFUL_SSL_VERIFY)); $response_code = (int) wp_remote_retrieve_response_code($response); $response_body = wp_remote_retrieve_body($response); 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()})"); } 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 (200 !== $response_code or empty($response_body)) { return new WP_Error('memberful_activation_fail', "We couldn't connect to Memberful (response code: {$response_code}), please email info@memberful.com"); } return json_decode($response_body); }
/** * 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; }