diff --git a/phpcs.xml b/phpcs.xml
index 4153cb7..b3bde84 100644
--- a/phpcs.xml
+++ b/phpcs.xml
@@ -54,5 +54,8 @@
+
+
+
\ No newline at end of file
diff --git a/src/ConvertKit_API.php b/src/ConvertKit_API.php
index 48ce836..3f1a187 100644
--- a/src/ConvertKit_API.php
+++ b/src/ConvertKit_API.php
@@ -19,6 +19,13 @@
*/
class ConvertKit_API
{
+ /**
+ * The SDK version.
+ *
+ * @var string
+ */
+ public const VERSION = '1.0.0';
+
/**
* ConvertKit API Key
*
@@ -95,7 +102,15 @@ public function __construct(string $api_key, string $api_secret, bool $debug = f
$this->api_key = $api_key;
$this->api_secret = $api_secret;
$this->debug = $debug;
- $this->client = new Client();
+
+ // Specify a User-Agent for API requests.
+ $this->client = new Client(
+ [
+ 'headers' => [
+ 'User-Agent' => 'ConvertKitPHPSDK/' . self::VERSION . ';PHP/' . phpversion(),
+ ],
+ ]
+ );
if ($debug) {
$this->debug_logger = new Logger('ck-debug');
@@ -128,15 +143,12 @@ private function create_log(string $message)
*/
public function get_account()
{
- $request = 'account';
-
- $options = [
- 'api_secret' => $this->api_secret,
- ];
-
- $this->create_log(sprintf('GET account: %s, %s', $request, json_encode($options)));
-
- return $this->get($request, $options);
+ return $this->get(
+ 'account',
+ [
+ 'api_secret' => $this->api_secret,
+ ]
+ );
}
@@ -147,15 +159,12 @@ public function get_account()
*/
public function get_sequences()
{
- $request = 'sequences';
-
- $options = [
- 'api_key' => $this->api_key,
- ];
-
- $this->create_log(sprintf('GET sequences: %s, %s', $request, json_encode($options)));
-
- return $this->get($request, $options);
+ return $this->get(
+ 'sequences',
+ [
+ 'api_key' => $this->api_key,
+ ]
+ );
}
@@ -169,23 +178,13 @@ public function get_sequences()
*/
public function get_sequence_subscriptions(int $sequence_id, string $sort_order = 'asc')
{
- $request = sprintf('sequences/%s/subscriptions', $sequence_id);
-
- $options = [
- 'api_secret' => $this->api_secret,
- 'sort_order' => $sort_order,
- ];
-
- $this->create_log(
- sprintf(
- 'GET sequence subscriptions: %s, %s, %s',
- $request,
- json_encode($options),
- $sequence_id
- )
+ return $this->get(
+ sprintf('sequences/%s/subscriptions', $sequence_id),
+ [
+ 'api_secret' => $this->api_secret,
+ 'sort_order' => $sort_order,
+ ]
);
-
- return $this->get($request, $options);
}
@@ -199,24 +198,13 @@ public function get_sequence_subscriptions(int $sequence_id, string $sort_order
*/
public function add_subscriber_to_sequence(int $sequence_id, string $email)
{
- $request = sprintf('courses/%s/subscribe', $sequence_id);
-
- $options = [
- 'api_key' => $this->api_key,
- 'email' => $email,
- ];
-
- $this->create_log(
- sprintf(
- 'POST add subscriber to sequence: %s, %s, %s, %s',
- $request,
- json_encode($options),
- $sequence_id,
- $email
- )
+ return $this->post(
+ sprintf('courses/%s/subscribe', $sequence_id),
+ [
+ 'api_key' => $this->api_key,
+ 'email' => $email,
+ ]
);
-
- return $this->post($request, $options);
}
@@ -236,13 +224,13 @@ public function add_tag(int $tag, array $options)
throw new \InvalidArgumentException();
}
- $request = sprintf('tags/%s/subscribe', $tag);
-
+ // Add API Key to array of options.
$options['api_key'] = $this->api_key;
- $this->create_log(sprintf('POST add tag: %s, %s, %s', $request, json_encode($options), $tag));
-
- return $this->post($request, $options);
+ return $this->post(
+ sprintf('tags/%s/subscribe', $tag),
+ $options
+ );
}
@@ -264,90 +252,111 @@ public function get_resources(string $resource)
throw new \InvalidArgumentException();
}
- if (! array_key_exists($resource, $this->resources)) {
- $options = [
+ // Return cached resource if it exists.
+ if (array_key_exists($resource, $this->resources)) {
+ return $this->resources[$resource];
+ }
+
+ // Assign the resource to the request variable.
+ $request = $resource;
+
+ // Landing pages are included in the /forms endpoint.
+ if ($resource === 'landing_pages') {
+ $request = 'forms';
+ }
+
+ // Fetch resources.
+ $resources = $this->get(
+ $request,
+ [
'api_key' => $this->api_key,
'timeout' => 10,
'Accept-Encoding' => 'gzip',
- ];
+ ]
+ );
- // Assign the resource to the request variable.
- $request = $resource;
+ $this->create_log(sprintf('%s response %s', $resource, json_encode($resources)));
- // Landing pages are included in the /forms endpoint.
- if ($resource === 'landing_pages') {
- $request = 'forms';
- }
+ // Return a blank array if no resources exist.
+ if (!$resources) {
+ $this->create_log('No resources');
+ return [];
+ }
- $this->create_log(sprintf('GET request %s, %s', $request, json_encode($options)));
-
- $resources = $this->get($request, $options);
-
- if (!$resources) {
- $this->create_log('No resources');
- $this->resources[$resource] = [
- [
- 'id' => '-2',
- 'name' => 'Error contacting API',
- ],
- ];
- } else {
- $_resource = [];
-
- if ('forms' === $resource) {
- $response = [];
- if (isset($resources->forms)) {
- $response = $resources->forms;
+ // Build array of resources.
+ $_resource = [];
+ switch ($resource) {
+ // Forms.
+ case 'forms':
+ // Bail if no forms are set.
+ if (!isset($resources->forms)) {
+ $this->create_log('No form resources');
+ return [];
+ }
+
+ // Exclude archived forms.
+ foreach ($resources->forms as $form) {
+ if (isset($form->archived) && $form->archived) {
+ continue;
}
- $this->create_log(sprintf('forms response %s', json_encode($response)));
- foreach ($response as $form) {
- if (isset($form->archived) && $form->archived) {
- continue;
- }
-
- $_resource[] = $form;
+ $_resource[] = $form;
+ }
+ break;
+
+ // Landing Pages.
+ case 'landing_pages':
+ // Bail if no landing pages are set.
+ if (!isset($resources->forms)) {
+ $this->create_log('No landing page resources');
+ return [];
+ }
+
+ // Exclude forms and archived forms/landing pages.
+ foreach ($resources->forms as $form) {
+ if (isset($form->archived) && $form->archived) {
+ continue;
}
- } else if ('landing_pages' === $resource) {
- $response = [];
- if (isset($resources->forms)) {
- $response = $resources->forms;
+
+ if ($form->type !== 'hosted') {
+ continue;
}
- $this->create_log(sprintf('landing_pages response %s', json_encode($response)));
- foreach ($response as $landing_page) {
- if ('hosted' === $landing_page->type) {
- if (isset($landing_page->archived) && $landing_page->archived) {
- continue;
- }
+ $_resource[] = $form;
+ }
+ break;
- $_resource[] = $landing_page;
- }
- }
- } else if ('subscription_forms' === $resource) {
- $this->create_log('subscription_forms');
- foreach ($resources as $mapping) {
- if (isset($mapping->archived) && $mapping->archived) {
- continue;
- }
-
- $_resource[$mapping->id] = $mapping->form_id;
- }
- } else if ('tags' === $resource) {
- $response = [];
- if (isset($resources->tags)) {
- $response = $resources->tags;
+ // Subscription Forms.
+ case 'subscription_forms':
+ // Exclude archived subscription forms.
+ foreach ($resources as $mapping) {
+ if (isset($mapping->archived) && $mapping->archived) {
+ continue;
}
- $this->create_log(sprintf('tags response %s', json_encode($response)));
- foreach ($response as $tag) {
- $_resource[] = $tag;
- }
- }//end if
+ $_resource[$mapping->id] = $mapping->form_id;
+ }
+ break;
+
+ // Tags.
+ case 'tags':
+ // Bail if no tags are set.
+ if (!isset($resources->tags)) {
+ $this->create_log('No tag resources');
+ return [];
+ }
+
+ foreach ($resources->tags as $tag) {
+ $_resource[] = $tag;
+ }
+ break;
+
+ default:
+ throw new \InvalidArgumentException('An unsupported resource was specified.');
+ }//end switch
- $this->resources[$resource] = $_resource;
- }//end if
- }//end if
+ // Cache resources and return.
+ $this->resources[$resource] = $_resource;
return $this->resources[$resource];
}
@@ -369,13 +378,13 @@ public function form_subscribe(int $form_id, array $options)
throw new \InvalidArgumentException();
}
- $request = sprintf('forms/%s/subscribe', $form_id);
-
+ // Add API Key to array of options.
$options['api_key'] = $this->api_key;
- $this->create_log(sprintf('POST form subscribe: %s, %s, %s', $request, json_encode($options), $form_id));
-
- return $this->post($request, $options);
+ return $this->post(
+ sprintf('forms/%s/subscribe', $form_id),
+ $options
+ );
}
@@ -394,13 +403,10 @@ public function form_unsubscribe(array $options)
throw new \InvalidArgumentException();
}
- $request = 'unsubscribe';
-
+ // Add API Secret to array of options.
$options['api_secret'] = $this->api_secret;
- $this->create_log(sprintf('PUT form unsubscribe: %s, %s', $request, json_encode($options)));
-
- return $this->put($request, $options);
+ return $this->put('unsubscribe', $options);
}
@@ -420,25 +426,15 @@ public function get_subscriber_id(string $email_address)
throw new \InvalidArgumentException();
}
- $request = 'subscribers';
-
- $options = [
- 'api_secret' => $this->api_secret,
- 'status' => 'all',
- 'email_address' => $email_address,
- ];
-
- $this->create_log(
- sprintf(
- 'GET subscriber id from all subscribers: %s, %s, %s',
- $request,
- json_encode($options),
- $email_address
- )
+ $subscribers = $this->get(
+ 'subscribers',
+ [
+ 'api_secret' => $this->api_secret,
+ 'status' => 'all',
+ 'email_address' => $email_address,
+ ]
);
- $subscribers = $this->get($request, $options);
-
if (!$subscribers) {
$this->create_log('No subscribers');
return false;
@@ -469,15 +465,12 @@ public function get_subscriber(int $subscriber_id)
throw new \InvalidArgumentException();
}
- $request = sprintf('subscribers/%s', $subscriber_id);
-
- $options = [
- 'api_secret' => $this->api_secret,
- ];
-
- $this->create_log(sprintf('GET subscriber tags: %s, %s, %s', $request, json_encode($options), $subscriber_id));
-
- return $this->get($request, $options);
+ return $this->get(
+ sprintf('subscribers/%s', $subscriber_id),
+ [
+ 'api_secret' => $this->api_secret,
+ ]
+ );
}
@@ -496,15 +489,12 @@ public function get_subscriber_tags(int $subscriber_id)
throw new \InvalidArgumentException();
}
- $request = sprintf('subscribers/%s/tags', $subscriber_id);
-
- $options = [
- 'api_key' => $this->api_key,
- ];
-
- $this->create_log(sprintf('GET subscriber tags: %s, %s, %s', $request, json_encode($options), $subscriber_id));
-
- return $this->get($request, $options);
+ return $this->get(
+ sprintf('subscribers/%s/tags', $subscriber_id),
+ [
+ 'api_key' => $this->api_key,
+ ]
+ );
}
@@ -523,13 +513,10 @@ public function list_purchases(array $options)
throw new \InvalidArgumentException();
}
- $request = 'purchases';
-
+ // Add API Secret to array of options.
$options['api_secret'] = $this->api_secret;
- $this->create_log(sprintf('GET list purchases: %s, %s', $request, json_encode($options)));
-
- return $this->get($request, $options);
+ return $this->get('purchases', $options);
}
@@ -548,13 +535,10 @@ public function create_purchase(array $options)
throw new \InvalidArgumentException();
}
- $request = 'purchases';
-
+ // Add API Secret to array of options.
$options['api_secret'] = $this->api_secret;
- $this->create_log(sprintf('POST create purchase: %s, %s', $request, json_encode($options)));
-
- return $this->post($request, $options);
+ return $this->post('purchases', $options);
}
@@ -710,6 +694,10 @@ private function strip_html_head_body_tags(string $markup)
*/
public function get(string $endpoint, array $args = [])
{
+ // Log if debugging enabled.
+ $this->create_log(sprintf('GET %s: %s', $endpoint, json_encode($args)));
+
+ // Make request and return results.
return $this->make_request($endpoint, 'GET', $args);
}
@@ -725,6 +713,10 @@ public function get(string $endpoint, array $args = [])
*/
public function post(string $endpoint, array $args = [])
{
+ // Log if debugging enabled.
+ $this->create_log(sprintf('POST %s: %s', $endpoint, json_encode($args)));
+
+ // Make request and return results.
return $this->make_request($endpoint, 'POST', $args);
}
@@ -740,6 +732,10 @@ public function post(string $endpoint, array $args = [])
*/
public function put(string $endpoint, array $args = [])
{
+ // Log if debugging enabled.
+ $this->create_log(sprintf('PUT %s: %s', $endpoint, json_encode($args)));
+
+ // Make request and return results.
return $this->make_request($endpoint, 'PUT', $args);
}
@@ -755,6 +751,10 @@ public function put(string $endpoint, array $args = [])
*/
public function delete(string $endpoint, array $args = [])
{
+ // Log if debugging enabled.
+ $this->create_log(sprintf('DELETE %s: %s', $endpoint, json_encode($args)));
+
+ // Make request and return results.
return $this->make_request($endpoint, 'DELETE', $args);
}
@@ -775,10 +775,12 @@ public function make_request(string $endpoint, string $method, array $args = [])
throw new \InvalidArgumentException();
}
+ // Build URL.
$url = $this->api_url_base . $this->api_version . '/' . $endpoint;
$this->create_log(sprintf('Making request on %s.', $url));
+ // Build request body.
$request_body = json_encode($args);
$this->create_log(sprintf('%s, Request body: %s', $method, $request_body));
@@ -801,11 +803,13 @@ public function make_request(string $endpoint, string $method, array $args = [])
);
}
+ // Send request.
$response = $this->client->send(
$request,
['exceptions' => false]
);
+ // Inspect response.
$status_code = $response->getStatusCode();
// If not between 200 and 300.
@@ -814,6 +818,7 @@ public function make_request(string $endpoint, string $method, array $args = [])
return false;
}
+ // Inspect response body.
$response_body = json_decode($response->getBody()->getContents());
if ($response_body) {