From 238d9fc3429c8c0549faad560670f0f187b36cfc Mon Sep 17 00:00:00 2001 From: Silas Ribas Martins Date: Thu, 10 Jan 2019 12:24:15 -0300 Subject: [PATCH 1/7] Add method to get subscriptions from a form --- src/ConvertKitAPI.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/ConvertKitAPI.php b/src/ConvertKitAPI.php index fe32e2a..3bbbcd1 100644 --- a/src/ConvertKitAPI.php +++ b/src/ConvertKitAPI.php @@ -630,4 +630,22 @@ private function check_if_subscriber_in_array($email_address, $subscribers) { } + /** + * Gets all subscribers from a specific form + * + * @param $form_id + * @return false|mixed + */ + public function get_form_subscriptions($form_id) + { + $request = $this->api_version . sprintf('/forms/%s/subscriptions', (string)$form_id); + + $options = array( + 'api_key' => $this->api_key, + ); + + $this->create_log(sprintf("GET form subscriptions: %s, %s, %s", $request, json_encode($options), $form_id)); + + return $this->make_request( $request, 'GET', $options ); + } } \ No newline at end of file From caabc5febde215301324c0be4bdd1650c3504a75 Mon Sep 17 00:00:00 2001 From: Silas Ribas Martins Date: Thu, 10 Jan 2019 12:44:20 -0300 Subject: [PATCH 2/7] Fix api param in get_form_subscriptions. Add config of logfile path. --- src/ConvertKitAPI.php | 1008 +++++++++++++++++++++-------------------- 1 file changed, 506 insertions(+), 502 deletions(-) diff --git a/src/ConvertKitAPI.php b/src/ConvertKitAPI.php index 3bbbcd1..c3ca60d 100644 --- a/src/ConvertKitAPI.php +++ b/src/ConvertKitAPI.php @@ -8,96 +8,100 @@ use GuzzleHttp\Psr7\Request; -class ConvertKit_API { - - /** - * ConvertKit API Key - * - * @var string - */ - protected $api_key; - - /** - * ConvertKit API Secret - * - * @var string - */ - protected $api_secret; - - /** - * Version of ConvertKit API - * - * @var string - */ - protected $api_version = 'v3'; - - /** - * ConvertKit API URL - * - * @var string - */ - protected $api_url_base = 'https://api.convertkit.com/'; - - /** - * API resources - * - * @var array - */ - protected $resources = array(); - - /** - * Additional markup - * - * @var array - */ - protected $markup = array(); - - /** - * Debug - * - * @var boolean - */ - protected $debug; - - /** - * Debug - * - * @var boolean - */ - protected $debug_logger; - - /** - * Guzzle Http Client - * - * @var object - */ - protected $client; - - /** - * Constructor for ConvertKitAPI instance - * - * @param string $api_key ConvertKit API Key. - * @param string $api_secret ConvertKit API Secret. - * @param boolean $debug if log debug info. - */ - public function __construct( $api_key, $api_secret, $debug = false ) { - - $this->api_key = $api_key; - $this->api_secret = $api_secret; - $this->debug = $debug; - $this->client = new Client(); - - if( $debug ) { - $this->debug_logger = new Logger('ck-debug'); - $this->debug_logger->pushHandler(new StreamHandler(__DIR__.'/logs/debug.log', Logger::DEBUG)); - } - } - - private function create_log($message) { - if($this->debug) { - $this->debug_logger->info($message); - } - } +class ConvertKit_API +{ + + /** + * ConvertKit API Key + * + * @var string + */ + protected $api_key; + + /** + * ConvertKit API Secret + * + * @var string + */ + protected $api_secret; + + /** + * Version of ConvertKit API + * + * @var string + */ + protected $api_version = 'v3'; + + /** + * ConvertKit API URL + * + * @var string + */ + protected $api_url_base = 'https://api.convertkit.com/'; + + /** + * API resources + * + * @var array + */ + protected $resources = array(); + + /** + * Additional markup + * + * @var array + */ + protected $markup = array(); + + /** + * Debug + * + * @var boolean + */ + protected $debug; + + /** + * Debug + * + * @var boolean + */ + protected $debug_logger; + + /** + * Guzzle Http Client + * + * @var object + */ + protected $client; + + /** + * Constructor for ConvertKitAPI instance + * + * @param string $api_key ConvertKit API Key. + * @param string $api_secret ConvertKit API Secret. + * @param boolean $debug if log debug info. + * @throws \Exception + */ + public function __construct($api_key, $api_secret, $debug = false, $logPath = null) + { + $this->api_key = $api_key; + $this->api_secret = $api_secret; + $this->debug = $debug; + $this->client = new Client(); + + if ($debug) { + $logPath = !empty($logPath) ? $logPath : __DIR__ . '/logs/debug.log'; + $this->debug_logger = new Logger('ck-debug'); + $this->debug_logger->pushHandler(new StreamHandler($logPath, Logger::DEBUG)); + } + } + + private function create_log($message) + { + if ($this->debug) { + $this->debug_logger->info($message); + } + } /** * Gets the current account @@ -114,8 +118,8 @@ public function get_account() $this->create_log(sprintf("GET account: %s, %s", $request, json_encode($options))); - return $this->make_request( $request, 'GET', $options ); - } + return $this->make_request($request, 'GET', $options); + } /** * Gets all sequences @@ -132,8 +136,8 @@ public function get_sequences() $this->create_log(sprintf("GET sequences: %s, %s", $request, json_encode($options))); - return $this->make_request( $request, 'GET', $options ); - } + return $this->make_request($request, 'GET', $options); + } /** * Gets subscribers to a sequence @@ -154,8 +158,8 @@ public function get_sequence_subscriptions($sequence_id, $sort_order = 'asc') $this->create_log(sprintf("GET sequence subscriptions: %s, %s, %s", $request, json_encode($options), $sequence_id)); - return $this->make_request( $request, 'GET', $options ); - } + return $this->make_request($request, 'GET', $options); + } /** * Adds a subscriber to a sequence by email address @@ -171,464 +175,464 @@ public function add_subscriber_to_sequence($sequence_id, $email) $options = array( 'api_key' => $this->api_key, - 'email' => $email + 'email' => $email ); $this->create_log(sprintf("POST add subscriber to sequence: %s, %s, %s, %s", $request, json_encode($options), $sequence_id, $email)); - return $this->make_request( $request, 'POST', $options ); - } - - /** - * Adds a tag to a subscriber - * - * @param int $tag Tag ID - * @param array $options Array of user data - * @return false|object - */ - public function add_tag( $tag, $options ) { - - if( !is_int($tag) || !is_array($options) ) { - throw new \InvalidArgumentException; - } - - $request = $this->api_version . sprintf( '/tags/%s/subscribe', $tag ); - - $options['api_key'] = $this->api_key; - - $this->create_log(sprintf("POST add tag: %s, %s, %s", $request, json_encode($options), $tag)); - - return $this->make_request( $request, 'POST', $options ); - } - - /** - * Gets a resource index - * Possible resources: forms, landing_pages, subscription_forms, tags - * - * GET /{$resource}/ - * - * @param string $resource Resource type. - * @return object API response - */ - public function get_resources( $resource ) { - - if( !is_string($resource) ) { - throw new \InvalidArgumentException; - } - - if ( ! array_key_exists( $resource, $this->resources ) ) { - - $options = array( - 'api_key' => $this->api_key, - 'timeout' => 10, - 'Accept-Encoding' => 'gzip', - ); - - $request = sprintf('/%s/%s', $this->api_version, $resource === 'landing_pages' ? 'forms' : $resource); - - $this->create_log(sprintf("GET request %s, %s", $request, json_encode($options))); - - $resources = $this->make_request( $request, 'GET', $options ); - - if(!$resources) { - $this->create_log("No resources"); - $this->resources[ $resource ] = array( - array( - 'id' => '-2', - 'name' => 'Error contacting API', - ), - ); - } else { - $_resource = array(); - - if ( 'forms' === $resource ) { - $response = isset( $resources->forms ) ? $resources->forms : array(); - $this->create_log(sprintf("forms response %s", json_encode($response))); - foreach ( $response as $form ) { - if ( isset( $form->archived ) && $form->archived ) { - continue; - } - $_resource[] = $form; - } - } elseif ( 'landing_pages' === $resource ) { - $response = isset($resources->forms ) ? $resources->forms : array(); - $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[] = $landing_page; - } - } - } elseif ( '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; - } - } elseif ( 'tags' === $resource ) { - $response = isset( $resources->tags ) ? $resources->tags : array(); - $this->create_log(sprintf("tags response %s", json_encode($response))); - foreach ( $response as $tag ) { - $_resource[] = $tag; - } - } - - $this->resources[ $resource ] = $_resource; - } - - } - - return $this->resources[ $resource ]; - } - - /** - * Adds a subscriber to a form. - * - * @param string $form_id Form ID. - * @param array $options Array of user data (email, name). - * - * @return false|object - */ - public function form_subscribe( $form_id, $options ) { - - if( !is_int($form_id) || !is_array($options) ) { - throw new \InvalidArgumentException; - } - - $request = $this->api_version . sprintf( '/forms/%s/subscribe', $form_id ); - - $options['api_key'] = $this->api_key; - - $this->create_log(sprintf("POST form subscribe: %s, %s, %s", $request, json_encode($options), $form_id)); - - return $this->make_request( $request, 'POST', $options ); - - } - - /** - * Remove subscription from a form - * - * @param array $options Array of user data (email). - * - * @return false|object - */ - public function form_unsubscribe( $options ) { - - if( !is_array($options) ) { - throw new \InvalidArgumentException; - } - - $request = $this->api_version . '/unsubscribe'; - - $options['api_secret'] = $this->api_secret; - - $this->create_log(sprintf("PUT form unsubscribe: %s, %s", $request, json_encode($options))); - - return $this->make_request( $request, 'PUT', $options ); - } - - /** - * Get the ConvertKit subscriber ID associated with email address if it exists. - * Return false if subscriber not found. - * - * @param $email_address string - * @return false|int $subscriber_id - */ - public function get_subscriber_id( $email_address ) { - - if( !is_string($email_address) || !filter_var($email_address, FILTER_VALIDATE_EMAIL)) { - throw new \InvalidArgumentException; - } - - $request = $this->api_version . '/subscribers'; - - $options = array( - 'api_secret' => $this->api_secret, - 'status' => 'all', - ); + return $this->make_request($request, 'POST', $options); + } + + /** + * Adds a tag to a subscriber + * + * @param int $tag Tag ID + * @param array $options Array of user data + * @return false|object + */ + public function add_tag($tag, $options) + { + if (!is_int($tag) || !is_array($options)) { + throw new \InvalidArgumentException; + } + + $request = $this->api_version . sprintf('/tags/%s/subscribe', $tag); + + $options['api_key'] = $this->api_key; + + $this->create_log(sprintf("POST add tag: %s, %s, %s", $request, json_encode($options), $tag)); + + return $this->make_request($request, 'POST', $options); + } + + /** + * Gets a resource index + * Possible resources: forms, landing_pages, subscription_forms, tags + * + * GET /{$resource}/ + * + * @param string $resource Resource type. + * @return object API response + */ + public function get_resources($resource) + { + if (!is_string($resource)) { + throw new \InvalidArgumentException; + } + + if (!array_key_exists($resource, $this->resources)) { + + $options = array( + 'api_key' => $this->api_key, + 'timeout' => 10, + 'Accept-Encoding' => 'gzip', + ); + + $request = sprintf('/%s/%s', $this->api_version, $resource === 'landing_pages' ? 'forms' : $resource); + + $this->create_log(sprintf("GET request %s, %s", $request, json_encode($options))); + + $resources = $this->make_request($request, 'GET', $options); + + if (!$resources) { + $this->create_log("No resources"); + $this->resources[$resource] = array( + array( + 'id' => '-2', + 'name' => 'Error contacting API', + ), + ); + } else { + $_resource = array(); + + if ('forms' === $resource) { + $response = isset($resources->forms) ? $resources->forms : array(); + $this->create_log(sprintf("forms response %s", json_encode($response))); + foreach ($response as $form) { + if (isset($form->archived) && $form->archived) { + continue; + } + $_resource[] = $form; + } + } elseif ('landing_pages' === $resource) { + $response = isset($resources->forms) ? $resources->forms : array(); + $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[] = $landing_page; + } + } + } elseif ('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; + } + } elseif ('tags' === $resource) { + $response = isset($resources->tags) ? $resources->tags : array(); + $this->create_log(sprintf("tags response %s", json_encode($response))); + foreach ($response as $tag) { + $_resource[] = $tag; + } + } + + $this->resources[$resource] = $_resource; + } + + } + + return $this->resources[$resource]; + } + + /** + * Adds a subscriber to a form. + * + * @param string $form_id Form ID. + * @param array $options Array of user data (email, name). + * + * @return false|object + */ + public function form_subscribe($form_id, $options) + { + if (!is_int($form_id) || !is_array($options)) { + throw new \InvalidArgumentException; + } + + $request = $this->api_version . sprintf('/forms/%s/subscribe', $form_id); + + $options['api_key'] = $this->api_key; + + $this->create_log(sprintf("POST form subscribe: %s, %s, %s", $request, json_encode($options), $form_id)); - $this->create_log(sprintf("GET subscriber id from all subscribers: %s, %s, %s", $request, json_encode($options), $email_address)); + return $this->make_request($request, 'POST', $options); - $subscribers = $this->make_request( $request, 'GET', $options ); + } - if( !$subscribers ) { - $this->create_log("No subscribers"); - return false; - } + /** + * Remove subscription from a form + * + * @param array $options Array of user data (email). + * + * @return false|object + */ + public function form_unsubscribe($options) + { + if (!is_array($options)) { + throw new \InvalidArgumentException; + } - $subscriber_id = $this::check_if_subscriber_in_array($email_address, $subscribers->subscribers); + $request = $this->api_version . '/unsubscribe'; - if($subscriber_id) { - return $subscriber_id; - } + $options['api_secret'] = $this->api_secret; - $total_pages = $subscribers->total_pages; + $this->create_log(sprintf("PUT form unsubscribe: %s, %s", $request, json_encode($options))); - $this->create_log(sprintf("Total number of pages is %s", $total_pages)); + return $this->make_request($request, 'PUT', $options); + } - for ( $i = 2; $i <= $total_pages; $i++ ) { - $options['page'] = $i; - $this->create_log(sprintf("Go to page %s", $i)); - $subscribers = $this->make_request( $request, 'GET', $options ); + /** + * Get the ConvertKit subscriber ID associated with email address if it exists. + * Return false if subscriber not found. + * + * @param $email_address string + * @return false|int $subscriber_id + */ + public function get_subscriber_id($email_address) + { + if (!is_string($email_address) || !filter_var($email_address, FILTER_VALIDATE_EMAIL)) { + throw new \InvalidArgumentException; + } - if( !$subscribers ) { - return false; - } + $request = $this->api_version . '/subscribers'; - $subscriber_id = $this::check_if_subscriber_in_array($email_address, $subscribers->subscribers); + $options = array( + 'api_secret' => $this->api_secret, + 'status' => 'all', + ); - if($subscriber_id) { - return $subscriber_id; - } - } + $this->create_log(sprintf("GET subscriber id from all subscribers: %s, %s, %s", $request, json_encode($options), $email_address)); - $this->create_log("Subscriber not found anywhere"); + $subscribers = $this->make_request($request, 'GET', $options); - return false; + if (!$subscribers) { + $this->create_log("No subscribers"); + return false; + } - } + $subscriber_id = $this::check_if_subscriber_in_array($email_address, $subscribers->subscribers); - /** - * Get subscriber by id - * - * @param $subscriber_id int - * - * @return false|int - */ - public function get_subscriber( $subscriber_id ) { + if ($subscriber_id) { + return $subscriber_id; + } - if( !is_int($subscriber_id) || $subscriber_id < 1 ) { - throw new \InvalidArgumentException; - } + $total_pages = $subscribers->total_pages; - $request = $this->api_version . sprintf( '/subscribers/%s', $subscriber_id ); + $this->create_log(sprintf("Total number of pages is %s", $total_pages)); - $options = array( - 'api_secret' => $this->api_secret, - ); + for ($i = 2; $i <= $total_pages; $i++) { + $options['page'] = $i; + $this->create_log(sprintf("Go to page %s", $i)); + $subscribers = $this->make_request($request, 'GET', $options); - $this->create_log(sprintf("GET subscriber tags: %s, %s, %s", $request, json_encode($options), $subscriber_id)); + if (!$subscribers) { + return false; + } - return $this->make_request( $request, 'GET', $options ); + $subscriber_id = $this::check_if_subscriber_in_array($email_address, $subscribers->subscribers); - } + if ($subscriber_id) { + return $subscriber_id; + } + } - /** - * Get a list of the tags for a subscriber. - * - * @param $subscriber_id - * @return false|array $subscriber_tags Array of tags for customer with key of tag_id - */ - public function get_subscriber_tags( $subscriber_id ) { + $this->create_log("Subscriber not found anywhere"); - if( !is_int($subscriber_id) || $subscriber_id < 1 ) { - throw new \InvalidArgumentException; - } + return false; - $request = $this->api_version . sprintf( '/subscribers/%s/tags', $subscriber_id ); + } - $options = array( - 'api_key' => $this->api_key, - ); + /** + * Get subscriber by id + * + * @param $subscriber_id int + * + * @return false|int + */ + public function get_subscriber($subscriber_id) + { + if (!is_int($subscriber_id) || $subscriber_id < 1) { + throw new \InvalidArgumentException; + } - $this->create_log(sprintf("GET subscriber tags: %s, %s, %s", $request, json_encode($options), $subscriber_id)); + $request = $this->api_version . sprintf('/subscribers/%s', $subscriber_id); - return $this->make_request( $request, 'GET', $options ); + $options = array( + 'api_secret' => $this->api_secret, + ); - } + $this->create_log(sprintf("GET subscriber tags: %s, %s, %s", $request, json_encode($options), $subscriber_id)); - /** - * @param $options - * - * @return false|object - */ - public function list_purchases($options) { + return $this->make_request($request, 'GET', $options); - if( !is_array($options) ) { - throw new \InvalidArgumentException; - } + } - $request = $this->api_version . '/purchases'; + /** + * Get a list of the tags for a subscriber. + * + * @param $subscriber_id + * @return false|array $subscriber_tags Array of tags for customer with key of tag_id + */ + public function get_subscriber_tags($subscriber_id) + { + if (!is_int($subscriber_id) || $subscriber_id < 1) { + throw new \InvalidArgumentException; + } - $options['api_secret'] = $this->api_secret; + $request = $this->api_version . sprintf('/subscribers/%s/tags', $subscriber_id); - $this->create_log(sprintf("GET list purchases: %s, %s", $request, json_encode($options))); + $options = array( + 'api_key' => $this->api_key, + ); - return $this->make_request( $request, 'GET', $options ); - } + $this->create_log(sprintf("GET subscriber tags: %s, %s, %s", $request, json_encode($options), $subscriber_id)); - /** - * Creates a purchase. - * - * @param array $options - * - * @return false|object - */ - public function create_purchase($options) { + return $this->make_request($request, 'GET', $options); - if( !is_array($options) ) { - throw new \InvalidArgumentException; - } + } - $request = $this->api_version . '/purchases'; + /** + * @param $options + * + * @return false|object + */ + public function list_purchases($options) + { + if (!is_array($options)) { + throw new \InvalidArgumentException; + } - $options['api_secret'] = $this->api_secret; + $request = $this->api_version . '/purchases'; - $this->create_log(sprintf("POST create purchase: %s, %s", $request, json_encode($options))); + $options['api_secret'] = $this->api_secret; - return $this->make_request( $request, 'POST', $options ); - } + $this->create_log(sprintf("GET list purchases: %s, %s", $request, json_encode($options))); - /** - * Get markup from ConvertKit for the provided $url - * - * @param string $url URL of API action. - * @return false|string - */ - public function get_resource( $url ) { + return $this->make_request($request, 'GET', $options); + } - if( !is_string($url) ) { - throw new \InvalidArgumentException; - } + /** + * Creates a purchase. + * + * @param array $options + * + * @return false|object + */ + public function create_purchase($options) + { + if (!is_array($options)) { + throw new \InvalidArgumentException; + } + + $request = $this->api_version . '/purchases'; - if (strpos( $url, 'api_key' ) === false) { - $url .= '?api_key=' . $this->api_key; - } + $options['api_secret'] = $this->api_secret; - $resource = ''; + $this->create_log(sprintf("POST create purchase: %s, %s", $request, json_encode($options))); + + return $this->make_request($request, 'POST', $options); + } + + /** + * Get markup from ConvertKit for the provided $url + * + * @param string $url URL of API action. + * @return false|string + */ + public function get_resource($url) + { + if (!is_string($url)) { + throw new \InvalidArgumentException; + } + + if (strpos($url, 'api_key') === false) { + $url .= '?api_key=' . $this->api_key; + } + + $resource = ''; + + $this->create_log(sprintf("Getting resource %s", $url)); + + if (!empty($url) && isset($this->markup[$url])) { + $this->create_log("Resource already set"); + $resource = $this->markup[$url]; + } elseif (!empty($url)) { + + if (!function_exists('str_get_html')) { + require_once(dirname(__FILE__) . '/lib/simple-html-dom.php'); + } + + if (!function_exists('url_to_absolute')) { + require_once(dirname(__FILE__) . '/lib/url-to-absolute.php'); + } + + $this->create_log("Getting html from url"); + $html = file_get_html($url); + + foreach ($html->find('a, link') as $element) { + if (isset($element->href)) { + $this->create_log(sprintf("To absolute url: %s", $element->href)); + echo url_to_absolute($url, $element->href); + $element->href = url_to_absolute($url, $element->href); + } + } + + foreach ($html->find('img, script') as $element) { + if (isset($element->src)) { + $this->create_log(sprintf("To absolute src: %s", $element->src)); + $element->src = url_to_absolute($url, $element->src); + } + } + + foreach ($html->find('form') as $element) { + if (isset($element->action)) { + $this->create_log(sprintf("To absolute form: %s", $element->action)); + $element->action = url_to_absolute($url, $element->action); + } else { + $element->action = $url; + } + } + + $resource = $html->save(); + $this->markup[$url] = $resource; + + } + + return $resource; + } + + /** + * @param $endpoint string, endpoint for request + * @param $method string, POST, GET, PUT, PATCH, DELETE + * @param array $args array, additional arguments for request + * + * @return false|mixed + */ + private function make_request($endpoint, $method, $args = array()) + { + if (!is_string($endpoint) || !is_string($method) || !is_array($args)) { + throw new \InvalidArgumentException; + } - $this->create_log(sprintf("Getting resource %s", $url)); + $url = $this->api_url_base . $endpoint; - if ( ! empty( $url ) && isset( $this->markup[ $url ] ) ) { - $this->create_log("Resource already set"); - $resource = $this->markup[ $url ]; - } elseif ( ! empty( $url ) ) { + $this->create_log(sprintf("Making request on %s.", $url)); - if ( ! function_exists( 'str_get_html' ) ) { - require_once( dirname( __FILE__ ) . '/lib/simple-html-dom.php' ); - } + $request_body = json_encode($args); - if ( ! function_exists( 'url_to_absolute' ) ) { - require_once( dirname( __FILE__ ) . '/lib/url-to-absolute.php' ); - } - - $this->create_log("Getting html from url"); - $html = file_get_html($url); - - foreach ( $html->find( 'a, link' ) as $element ) { - if ( isset( $element->href ) ) { - $this->create_log(sprintf("To absolute url: %s", $element->href)); - echo url_to_absolute( $url, $element->href ); - $element->href = url_to_absolute( $url, $element->href ); - } - } - - foreach ( $html->find( 'img, script' ) as $element ) { - if ( isset( $element->src ) ) { - $this->create_log(sprintf("To absolute src: %s", $element->src)); - $element->src = url_to_absolute( $url, $element->src ); - } - } + $this->create_log(sprintf("%s, Request body: %s", $method, $request_body)); - foreach ( $html->find( 'form' ) as $element ) { - if ( isset( $element->action ) ) { - $this->create_log(sprintf("To absolute form: %s", $element->action)); - $element->action = url_to_absolute( $url, $element->action ); - } else { - $element->action = $url; - } - } - - $resource = $html->save(); - $this->markup[ $url ] = $resource; - - } + if ($method === "GET") { + if ($args) { + $url .= '?' . http_build_query($args); + } + $request = new Request($method, $url); + } else { + $request = new Request($method, $url, array( + 'Content-Type' => 'application/json', + 'Content-Length' => strlen($request_body) + ), $request_body); + } - return $resource; - } - - /** - * @param $endpoint string, endpoint for request - * @param $method string, POST, GET, PUT, PATCH, DELETE - * @param array $args array, additional arguments for request - * - * @return false|mixed - */ - private function make_request($endpoint, $method, $args = array()) { - - if( !is_string($endpoint) || !is_string($method) || !is_array($args) ) { - throw new \InvalidArgumentException; - } - - $url = $this->api_url_base . $endpoint; - - $this->create_log(sprintf("Making request on %s.", $url)); - - $request_body = json_encode($args); - - $this->create_log(sprintf("%s, Request body: %s", $method, $request_body)); - - if( $method === "GET" ){ - if($args) { - $url .= '?' . http_build_query($args); - } - $request = new Request($method, $url); - } else { - $request = new Request($method, $url, array( - 'Content-Type' => 'application/json', - 'Content-Length' => strlen($request_body) - ), $request_body); - } - - $response = $this->client->send($request, [ - 'exceptions' => false - ]); - - $status_code = $response->getStatusCode(); - - // If not between 200 and 300 - if (!preg_match("/^[2-3][0-9]{2}/", $status_code)) { - $this->create_log(sprintf("Response code is %s.", $status_code)); - return false; - } - - $response_body = json_decode($response->getBody()->getContents()); - - if($response_body) { - $this->create_log("Finish request successfully."); - return $response_body; - } - - $this->create_log("Failed to finish request."); - return false; - - } - - /** - * Looks for subscriber with email in array - * - * @param $email_address - * @param $subscribers - * - * @return false|int false if not found, else subscriber object - */ - private function check_if_subscriber_in_array($email_address, $subscribers) { - - foreach ($subscribers as $subscriber) { - if ($subscriber->email_address === $email_address) { - $this->create_log("Subscriber found!"); - return $subscriber->id; - } - } - - $this->create_log("Subscriber not found on current page."); - return false; - - } + $response = $this->client->send($request, [ + 'exceptions' => false + ]); + + $status_code = $response->getStatusCode(); + + // If not between 200 and 300 + if (!preg_match("/^[2-3][0-9]{2}/", $status_code)) { + $this->create_log(sprintf("Response code is %s.", $status_code)); + return false; + } + + $response_body = json_decode($response->getBody()->getContents()); + + if ($response_body) { + $this->create_log("Finish request successfully."); + return $response_body; + } + + $this->create_log("Failed to finish request."); + return false; + + } + + /** + * Looks for subscriber with email in array + * + * @param $email_address + * @param $subscribers + * + * @return false|int false if not found, else subscriber object + */ + private function check_if_subscriber_in_array($email_address, $subscribers) + { + foreach ($subscribers as $subscriber) { + if ($subscriber->email_address === $email_address) { + $this->create_log("Subscriber found!"); + return $subscriber->id; + } + } + + $this->create_log("Subscriber not found on current page."); + return false; + + } /** * Gets all subscribers from a specific form @@ -641,11 +645,11 @@ public function get_form_subscriptions($form_id) $request = $this->api_version . sprintf('/forms/%s/subscriptions', (string)$form_id); $options = array( - 'api_key' => $this->api_key, + 'api_secret' => $this->api_secret, ); $this->create_log(sprintf("GET form subscriptions: %s, %s, %s", $request, json_encode($options), $form_id)); - return $this->make_request( $request, 'GET', $options ); + return $this->make_request($request, 'GET', $options); } } \ No newline at end of file From 6f0ef6edf98db2b5c41ffbc83420f8a37a6d1e89 Mon Sep 17 00:00:00 2001 From: Silas Ribas Martins Date: Thu, 10 Jan 2019 12:47:49 -0300 Subject: [PATCH 3/7] Add sort_order and subscriber_state to get_form_subscriptions --- src/ConvertKitAPI.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/ConvertKitAPI.php b/src/ConvertKitAPI.php index c3ca60d..3674a1e 100644 --- a/src/ConvertKitAPI.php +++ b/src/ConvertKitAPI.php @@ -638,16 +638,23 @@ private function check_if_subscriber_in_array($email_address, $subscribers) * Gets all subscribers from a specific form * * @param $form_id + * @param string $sort_order + * @param null $subscriber_state * @return false|mixed */ - public function get_form_subscriptions($form_id) + public function get_form_subscriptions($form_id, $sort_order = 'asc', $subscriber_state = null) { $request = $this->api_version . sprintf('/forms/%s/subscriptions', (string)$form_id); $options = array( 'api_secret' => $this->api_secret, + 'sort_order' => $sort_order ); + if (!empty($subscriber_state) && in_array($subscriber_state, ['active', 'cancelled'])) { + $options['subscriber_state'] = $subscriber_state; + } + $this->create_log(sprintf("GET form subscriptions: %s, %s, %s", $request, json_encode($options), $form_id)); return $this->make_request($request, 'GET', $options); From 73799a48a4d7f34aed33d3694d1b31e5e6335d99 Mon Sep 17 00:00:00 2001 From: Silas Ribas Martins Date: Thu, 10 Jan 2019 12:56:02 -0300 Subject: [PATCH 4/7] Change signature of get_form_subscriptions and add page param --- src/ConvertKitAPI.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/ConvertKitAPI.php b/src/ConvertKitAPI.php index 3674a1e..8d2f238 100644 --- a/src/ConvertKitAPI.php +++ b/src/ConvertKitAPI.php @@ -638,17 +638,19 @@ private function check_if_subscriber_in_array($email_address, $subscribers) * Gets all subscribers from a specific form * * @param $form_id - * @param string $sort_order + * @param int $page * @param null $subscriber_state + * @param string $sort_order * @return false|mixed */ - public function get_form_subscriptions($form_id, $sort_order = 'asc', $subscriber_state = null) + public function get_form_subscriptions($form_id, $page = 1, $subscriber_state = null, $sort_order = 'asc') { $request = $this->api_version . sprintf('/forms/%s/subscriptions', (string)$form_id); $options = array( 'api_secret' => $this->api_secret, - 'sort_order' => $sort_order + 'page' => $page, + 'sort_order' => $sort_order, ); if (!empty($subscriber_state) && in_array($subscriber_state, ['active', 'cancelled'])) { From 6bd6e159bf16b1dca2da4f869b79b3f91e7e60f4 Mon Sep 17 00:00:00 2001 From: Silas Ribas Martins Date: Tue, 6 Aug 2019 20:49:00 -0300 Subject: [PATCH 5/7] Add tests --- tests/ConvertKitAPITest.php | 63 +++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index 5a41ea3..972e4a0 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -339,4 +339,67 @@ protected static function isHtml($string) { return preg_match("/<[^<]+>/",$string,$m) != 0; } + /** + * List purchases + */ + public function testFormSubscriptions() { + + $random_email = str_shuffle('1234567890') . 'test@growdevelopment.com'; + + /* + * Subscribe + */ + $options = [ + 'email' => $random_email, + 'name' => 'Full Name', + 'first_name' => 'First Name', + 'tags' => $this->test_tag_id, + 'fields' => [ + 'phone' => 134567891243, + 'shirt_size' => 'M', + 'website_url' => 'testurl.com' + ] + ]; + + $subscribed = $this->api->form_subscribe($this->test_form_id, $options); + + $subscriptions = $this->api->get_form_subscriptions($this->test_form_id); + $this->assertInstanceOf('stdClass', $subscriptions); + $this->assertArrayHasKey('total_subscriptions', get_object_vars($subscriptions)); + $this->assertArrayHasKey('page', get_object_vars($subscriptions)); + $this->assertArrayHasKey('total_pages', get_object_vars($subscriptions)); + $this->assertArrayHasKey('subscriptions', get_object_vars($subscriptions)); + } + + /** + * List purchases + */ + public function testTagSubscriptions() { + + $random_email = str_shuffle('1234567890') . 'test@growdevelopment.com'; + + /* + * Subscribe + */ + $options = [ + 'email' => $random_email, + 'name' => 'Full Name', + 'first_name' => 'First Name', + 'tags' => $this->test_tag_id, + 'fields' => [ + 'phone' => 134567891243, + 'shirt_size' => 'M', + 'website_url' => 'testurl.com' + ] + ]; + + $subscribed = $this->api->form_subscribe($this->test_form_id, $options); + + $subscriptions = $this->api->get_tag_subscriptions($this->test_tag_id); + $this->assertInstanceOf('stdClass', $subscriptions); + $this->assertArrayHasKey('total_subscriptions', get_object_vars($subscriptions)); + $this->assertArrayHasKey('page', get_object_vars($subscriptions)); + $this->assertArrayHasKey('total_pages', get_object_vars($subscriptions)); + $this->assertArrayHasKey('subscriptions', get_object_vars($subscriptions)); + } } \ No newline at end of file From b50995df06cb984bf76bbed1d9aaa8a59656d10f Mon Sep 17 00:00:00 2001 From: Silas Ribas Martins Date: Tue, 6 Aug 2019 20:56:52 -0300 Subject: [PATCH 6/7] Format code --- src/ConvertKitAPI.php | 46 +- tests/ConvertKitAPITest.php | 814 ++++++++++++++++++------------------ 2 files changed, 436 insertions(+), 424 deletions(-) diff --git a/src/ConvertKitAPI.php b/src/ConvertKitAPI.php index 80fa73f..95022df 100644 --- a/src/ConvertKitAPI.php +++ b/src/ConvertKitAPI.php @@ -2,10 +2,10 @@ namespace ConvertKit_API; -use Monolog\Logger; -use Monolog\Handler\StreamHandler; use GuzzleHttp\Client; use GuzzleHttp\Psr7\Request; +use Monolog\Handler\StreamHandler; +use Monolog\Logger; class ConvertKit_API { @@ -82,13 +82,13 @@ class ConvertKit_API */ public function __construct($api_key, $api_secret, $debug = false, $logPath = null) { - $this->api_key = $api_key; + $this->api_key = $api_key; $this->api_secret = $api_secret; - $this->debug = $debug; - $this->client = new Client(); + $this->debug = $debug; + $this->client = new Client(); if ($debug) { - $logPath = !empty($logPath) ? $logPath : __DIR__ . '/logs/debug.log'; + $logPath = !empty($logPath) ? $logPath : __DIR__ . '/logs/debug.log'; $this->debug_logger = new Logger('ck-debug'); $this->debug_logger->pushHandler(new StreamHandler($logPath, Logger::DEBUG)); } @@ -151,7 +151,7 @@ public function get_sequence_subscriptions($sequence_id, $sort_order = 'asc') $options = array( 'api_secret' => $this->api_secret, - 'sort_order' => $sort_order + 'sort_order' => $sort_order, ); $this->create_log(sprintf("GET sequence subscriptions: %s, %s, %s", $request, json_encode($options), $sequence_id)); @@ -173,7 +173,7 @@ public function add_subscriber_to_sequence($sequence_id, $email) $options = array( 'api_key' => $this->api_key, - 'email' => $email + 'email' => $email, ); $this->create_log(sprintf("POST add subscriber to sequence: %s, %s, %s, %s", $request, json_encode($options), $sequence_id, $email)); @@ -221,8 +221,8 @@ public function get_resources($resource) if (!array_key_exists($resource, $this->resources)) { $options = array( - 'api_key' => $this->api_key, - 'timeout' => 10, + 'api_key' => $this->api_key, + 'timeout' => 10, 'Accept-Encoding' => 'gzip', ); @@ -236,7 +236,7 @@ public function get_resources($resource) $this->create_log("No resources"); $this->resources[$resource] = array( array( - 'id' => '-2', + 'id' => '-2', 'name' => 'Error contacting API', ), ); @@ -350,7 +350,7 @@ public function get_subscriber_id($email_address) $options = array( 'api_secret' => $this->api_secret, - 'status' => 'all', + 'status' => 'all', ); $this->create_log(sprintf("GET subscriber id from all subscribers: %s, %s, %s", $request, json_encode($options), $email_address)); @@ -511,11 +511,11 @@ public function get_resource($url) } elseif (!empty($url)) { if (!function_exists('str_get_html')) { - require_once(dirname(__FILE__) . '/lib/simple-html-dom.php'); + require_once dirname(__FILE__) . '/lib/simple-html-dom.php'; } if (!function_exists('url_to_absolute')) { - require_once(dirname(__FILE__) . '/lib/url-to-absolute.php'); + require_once dirname(__FILE__) . '/lib/url-to-absolute.php'; } $this->create_log("Getting html from url"); @@ -545,7 +545,7 @@ public function get_resource($url) } } - $resource = $html->save(); + $resource = $html->save(); $this->markup[$url] = $resource; } @@ -581,13 +581,13 @@ private function make_request($endpoint, $method, $args = array()) $request = new Request($method, $url); } else { $request = new Request($method, $url, array( - 'Content-Type' => 'application/json', - 'Content-Length' => strlen($request_body) + 'Content-Type' => 'application/json', + 'Content-Length' => strlen($request_body), ), $request_body); } $response = $this->client->send($request, [ - 'exceptions' => false + 'exceptions' => false, ]); $status_code = $response->getStatusCode(); @@ -642,11 +642,11 @@ private static function check_if_subscriber_in_array($email_address, $subscriber */ public function get_form_subscriptions($form_id, $page = 1, $subscriber_state = null, $sort_order = 'asc') { - $request = $this->api_version . sprintf('/forms/%s/subscriptions', (string)$form_id); + $request = $this->api_version . sprintf('/forms/%s/subscriptions', (string) $form_id); $options = array( 'api_secret' => $this->api_secret, - 'page' => $page, + 'page' => $page, 'sort_order' => $sort_order, ); @@ -670,11 +670,11 @@ public function get_form_subscriptions($form_id, $page = 1, $subscriber_state = */ public function get_tag_subscriptions($tag_id, $page = 1, $subscriber_state = null, $sort_order = 'asc') { - $request = $this->api_version . sprintf('/tags/%s/subscriptions', (string)$tag_id); + $request = $this->api_version . sprintf('/tags/%s/subscriptions', (string) $tag_id); $options = array( 'api_secret' => $this->api_secret, - 'page' => $page, + 'page' => $page, 'sort_order' => $sort_order, ); @@ -686,4 +686,4 @@ public function get_tag_subscriptions($tag_id, $page = 1, $subscriber_state = nu return $this->make_request($request, 'POST', $options); } -} \ No newline at end of file +} diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index 972e4a0..5a09c87 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -2,404 +2,416 @@ use PHPUnit\Framework\TestCase; -class ConvertKitAPITest extends TestCase { - - /** - * ConvertKit Class Object - * - * @var object - */ - protected $api; - - /** - * Test subscribed user email - * - * @var string - */ - protected $test_email; - - /** - * Test subscribed user id - * - * @var string - */ - protected $test_user_id; - - /** - * Test tag id - * - * @var int - */ - protected $test_tag_id; - - /** - * Form url - * - * @var int - */ - protected $test_form_url; - - /** - * Form id - * - * @var int - */ - protected $test_form_id; - - protected function setUp() { - - include_once( dirname(__FILE__) . "/config.php" ); - - $api_key = CONVERTKIT_PUBLIC_KEY; - $api_secret = CONVERTKIT_SECRET_KEY; - $this->test_email = CONVERTKIT_TESTING_EMAIL; - $this->test_user_id = CONVERTKIT_TESTING_USER_ID; - $this->test_form_id = CONVERTKIT_TESTING_FORM_ID; - $this->test_tag_id = CONVERTKIT_TESTING_TAG_ID; - $this->test_form_url = CONVERTKIT_TESTING_FORM_URL; - - $this->api = new \ConvertKit_API\ConvertKit_API($api_key, $api_secret); - } - - /** - * @dataProvider inputGetResourcesArguments - * @expectedException InvalidArgumentException - * - * @param $input - */ - public function testGetResourcesArguments($input) { - $this->api->get_resources($input); - } - - /** - * Data provider for @testGetResourcesArguments - * - * @return array - */ - public function inputGetResourcesArguments() { - return [ - [2], - [['2', '1']], - [new stdClass()] - ]; - } - - /** - * @dataProvider inputGetSubscriberId - * @expectedException InvalidArgumentException - * - * @param $input - */ - public function testArgumentsGetSubscriberId($input) { - $this->api->get_subscriber_id($input); - } - - /** - * Data provider for @testGetSubscriberId - * - * @return array - */ - public function inputGetSubscriberId() { - return [ - [2], - [['2', '1']], - [new stdClass()], - ['teststring'], - ['teststring@'] - ]; - } - - /** - * @dataProvider inputGetSubscriber - * @expectedException InvalidArgumentException - * - * @param $input - */ - public function testArgumentsGetSubscriber($input) { - $this->api->get_subscriber($input); - } - - /** - * Data provider for @testGetSubscriber - * - * @return array - */ - public function inputGetSubscriber() { - return [ - [['2', '1']], - [new stdClass()], - ['teststring'], - [1.2], - [-10], - ]; - } - - /** - * @dataProvider inputAddTag - * @expectedException InvalidArgumentException - * - * @param $tag - * @param $options - */ - public function testArgumentsAddTag($tag, $options) { - $this->api->add_tag($tag, $options); - } - - /** - * Data provider for @testAddTag - * - * @return array - */ - public function inputAddTag() { - return [ - [['2', '1'], 1], - [new stdClass(), 2], - ['teststring', 3], - [3, 3], - ]; - } - - public function testIncorrectApiData() { - $api_key = 'test'; - $api_secret = 'test'; - - $test_client = new \ConvertKit_API\ConvertKit_API($api_key, $api_secret); - $this->assertFalse($test_client->get_subscriber_id($this->test_email)); - $this->assertFalse($test_client->get_subscriber($this->test_user_id)); - $this->assertFalse($test_client->get_subscriber_tags($this->test_user_id)); - } - - /** - * Get subscriber id by email - */ - public function testGetSubscriberId() { - $subscriber_id = $this->api->get_subscriber_id($this->test_email); - $this->assertInternalType("int", $subscriber_id); - } - - /** - * Get subscriber by id - */ - public function testGetSubscriber() { - $subscriber = $this->api->get_subscriber($this->test_user_id); - $this->assertInstanceOf('stdClass', $subscriber); - $this->assertArrayHasKey('subscriber', get_object_vars($subscriber)); - $this->assertArrayHasKey('id', get_object_vars($subscriber->subscriber)); - $this->assertEquals(get_object_vars($subscriber->subscriber)['id'], $this->test_user_id); - } - - /** - * Get subscriber tags - */ - public function testGetSubscriberTags() { - $subscriber = $this->api->get_subscriber_tags($this->test_user_id); - $this->assertInstanceOf('stdClass', $subscriber); - $this->assertArrayHasKey('tags', get_object_vars($subscriber)); - } - - /** - * Subscribe and unsubscribe from form - */ - public function testUserActions() { - - $random_email = str_shuffle('1234567890') . 'test@growdevelopment.com'; - - /* - * Subscribe - */ - $options = [ - 'email' => $random_email, - 'name' => 'Full Name', - 'first_name' => 'First Name', - 'tags' => $this->test_tag_id, - 'fields' => [ - 'phone' => 134567891243, - 'shirt_size' => 'M', - 'website_url' => 'testurl.com' - ] - ]; - - $subscribed = $this->api->form_subscribe($this->test_form_id, $options); - $this->assertInstanceOf('stdClass', $subscribed); - $this->assertArrayHasKey('subscription', get_object_vars($subscribed)); - $this->assertArrayHasKey('id', get_object_vars($subscribed->subscription)); - $this->assertEquals(get_object_vars($subscribed->subscription)['subscribable_id'], $this->test_form_id); - - /* - * Add tag - */ - $added_tag = $this->api->add_tag($this->test_tag_id, [ - 'email' => $random_email - ]); - $this->assertInstanceOf('stdClass', $added_tag); - $this->assertArrayHasKey('subscription', get_object_vars($added_tag)); - $this->assertArrayHasKey('id', get_object_vars($added_tag->subscription)); - $this->assertEquals(get_object_vars($added_tag->subscription)['subscribable_id'], $this->test_tag_id); - $this->assertEquals(get_object_vars($added_tag->subscription)['subscribable_type'], 'tag'); - - /* - * Purchase - */ - $purchase_options = [ - 'purchase' => [ - 'email_address' => $random_email, - 'transaction_id' => str_shuffle('wfervdrtgsdewrafvwefds'), - 'subtotal' => 20.00, - 'tax' => 2.00, - 'shipping' => 2.00, - 'discount' => 3.00, - 'total' => 21.00, - 'status' => 'paid', - 'products' => array( - 0 => array( - 'name' => 'Floppy Disk (512k)', - 'sku' => '7890-ijkl', - 'unit_price' => 5.00, - 'quantity' => 2 - ) - ) - ] - ]; - $purchase = $this->api->create_purchase($purchase_options); - $this->assertInstanceOf('stdClass', $purchase); - $this->assertArrayHasKey('transaction_id', get_object_vars($purchase)); - - /* - * Unsubscribe - */ - $unsubscribed = $this->api->form_unsubscribe([ - 'email' => $random_email - ]); - - $this->assertInstanceOf('stdClass', $unsubscribed); - $this->assertArrayHasKey('subscriber', get_object_vars($unsubscribed)); - $this->assertArrayHasKey('email_address', get_object_vars($unsubscribed->subscriber)); - $this->assertEquals(get_object_vars($unsubscribed->subscriber)['email_address'], $random_email); - - } - - /** - * List purchases - */ - public function testListPurchases() { - - $list_purchases = $this->api->list_purchases(['page' => 1]); - $this->assertInstanceOf('stdClass', $list_purchases); - $this->assertArrayHasKey('total_purchases', get_object_vars($list_purchases)); - $this->assertArrayHasKey('page', get_object_vars($list_purchases)); - $this->assertArrayHasKey('total_pages', get_object_vars($list_purchases)); - $this->assertArrayHasKey('purchases', get_object_vars($list_purchases)); - - } - - /** - * Get resources - */ - public function testGetResources() { - - $resources = ['forms', 'landing_pages', 'tags']; - - foreach ($resources as $resource) { - $get_resources = $this->api->get_resources($resource); - $this->assertTrue(is_array($get_resources) || empty($get_resources)); - if(count($get_resources) > 0) { - $get_resource = $get_resources[0]; - $this->assertInstanceOf('stdClass', $get_resource); - $this->assertArrayHasKey('id', get_object_vars($get_resource)); - $this->assertArrayHasKey('name', get_object_vars($get_resource)); - } - } - - } - - /** - * Get subscription forms - */ - public function testGetLandingPages() { - $landing_pages = $this->api->get_resources('subscription_forms'); - $this->assertTrue(is_array($landing_pages) || empty($landing_pages)); - } - - /** - * Get resource by url - */ - public function testGetResource() { - $markup = $this->api->get_resource($this->test_form_url); - $this->assertTrue($this->isHtml($markup)); - } - - /** - * Checks if string is html - * - * @param $string - * - * @return bool - */ - protected static function isHtml($string) { - return preg_match("/<[^<]+>/",$string,$m) != 0; - } - - /** - * List purchases - */ - public function testFormSubscriptions() { - - $random_email = str_shuffle('1234567890') . 'test@growdevelopment.com'; - - /* - * Subscribe - */ - $options = [ - 'email' => $random_email, - 'name' => 'Full Name', - 'first_name' => 'First Name', - 'tags' => $this->test_tag_id, - 'fields' => [ - 'phone' => 134567891243, - 'shirt_size' => 'M', - 'website_url' => 'testurl.com' - ] - ]; - - $subscribed = $this->api->form_subscribe($this->test_form_id, $options); - - $subscriptions = $this->api->get_form_subscriptions($this->test_form_id); - $this->assertInstanceOf('stdClass', $subscriptions); - $this->assertArrayHasKey('total_subscriptions', get_object_vars($subscriptions)); - $this->assertArrayHasKey('page', get_object_vars($subscriptions)); - $this->assertArrayHasKey('total_pages', get_object_vars($subscriptions)); - $this->assertArrayHasKey('subscriptions', get_object_vars($subscriptions)); - } - - /** - * List purchases - */ - public function testTagSubscriptions() { - - $random_email = str_shuffle('1234567890') . 'test@growdevelopment.com'; - - /* - * Subscribe - */ - $options = [ - 'email' => $random_email, - 'name' => 'Full Name', - 'first_name' => 'First Name', - 'tags' => $this->test_tag_id, - 'fields' => [ - 'phone' => 134567891243, - 'shirt_size' => 'M', - 'website_url' => 'testurl.com' - ] - ]; - - $subscribed = $this->api->form_subscribe($this->test_form_id, $options); - - $subscriptions = $this->api->get_tag_subscriptions($this->test_tag_id); - $this->assertInstanceOf('stdClass', $subscriptions); - $this->assertArrayHasKey('total_subscriptions', get_object_vars($subscriptions)); - $this->assertArrayHasKey('page', get_object_vars($subscriptions)); - $this->assertArrayHasKey('total_pages', get_object_vars($subscriptions)); - $this->assertArrayHasKey('subscriptions', get_object_vars($subscriptions)); - } -} \ No newline at end of file +class ConvertKitAPITest extends TestCase +{ + /** + * ConvertKit Class Object + * + * @var object + */ + protected $api; + + /** + * Test subscribed user email + * + * @var string + */ + protected $test_email; + + /** + * Test subscribed user id + * + * @var string + */ + protected $test_user_id; + + /** + * Test tag id + * + * @var int + */ + protected $test_tag_id; + + /** + * Form url + * + * @var int + */ + protected $test_form_url; + + /** + * Form id + * + * @var int + */ + protected $test_form_id; + + protected function setUp() + { + include_once dirname(__FILE__) . "/config.php"; + + $api_key = CONVERTKIT_PUBLIC_KEY; + $api_secret = CONVERTKIT_SECRET_KEY; + $this->test_email = CONVERTKIT_TESTING_EMAIL; + $this->test_user_id = CONVERTKIT_TESTING_USER_ID; + $this->test_form_id = CONVERTKIT_TESTING_FORM_ID; + $this->test_tag_id = CONVERTKIT_TESTING_TAG_ID; + $this->test_form_url = CONVERTKIT_TESTING_FORM_URL; + + $this->api = new \ConvertKit_API\ConvertKit_API($api_key, $api_secret); + } + + /** + * @dataProvider inputGetResourcesArguments + * @expectedException InvalidArgumentException + * + * @param $input + */ + public function testGetResourcesArguments($input) + { + $this->api->get_resources($input); + } + + /** + * Data provider for @testGetResourcesArguments + * + * @return array + */ + public function inputGetResourcesArguments() + { + return [ + [2], + [['2', '1']], + [new stdClass()], + ]; + } + + /** + * @dataProvider inputGetSubscriberId + * @expectedException InvalidArgumentException + * + * @param $input + */ + public function testArgumentsGetSubscriberId($input) + { + $this->api->get_subscriber_id($input); + } + + /** + * Data provider for @testGetSubscriberId + * + * @return array + */ + public function inputGetSubscriberId() + { + return [ + [2], + [['2', '1']], + [new stdClass()], + ['teststring'], + ['teststring@'], + ]; + } + + /** + * @dataProvider inputGetSubscriber + * @expectedException InvalidArgumentException + * + * @param $input + */ + public function testArgumentsGetSubscriber($input) + { + $this->api->get_subscriber($input); + } + + /** + * Data provider for @testGetSubscriber + * + * @return array + */ + public function inputGetSubscriber() + { + return [ + [['2', '1']], + [new stdClass()], + ['teststring'], + [1.2], + [-10], + ]; + } + + /** + * @dataProvider inputAddTag + * @expectedException InvalidArgumentException + * + * @param $tag + * @param $options + */ + public function testArgumentsAddTag($tag, $options) + { + $this->api->add_tag($tag, $options); + } + + /** + * Data provider for @testAddTag + * + * @return array + */ + public function inputAddTag() + { + return [ + [['2', '1'], 1], + [new stdClass(), 2], + ['teststring', 3], + [3, 3], + ]; + } + + public function testIncorrectApiData() + { + $api_key = 'test'; + $api_secret = 'test'; + + $test_client = new \ConvertKit_API\ConvertKit_API($api_key, $api_secret); + $this->assertFalse($test_client->get_subscriber_id($this->test_email)); + $this->assertFalse($test_client->get_subscriber($this->test_user_id)); + $this->assertFalse($test_client->get_subscriber_tags($this->test_user_id)); + } + + /** + * Get subscriber id by email + */ + public function testGetSubscriberId() + { + $subscriber_id = $this->api->get_subscriber_id($this->test_email); + $this->assertInternalType("int", $subscriber_id); + } + + /** + * Get subscriber by id + */ + public function testGetSubscriber() + { + $subscriber = $this->api->get_subscriber($this->test_user_id); + $this->assertInstanceOf('stdClass', $subscriber); + $this->assertArrayHasKey('subscriber', get_object_vars($subscriber)); + $this->assertArrayHasKey('id', get_object_vars($subscriber->subscriber)); + $this->assertEquals(get_object_vars($subscriber->subscriber)['id'], $this->test_user_id); + } + + /** + * Get subscriber tags + */ + public function testGetSubscriberTags() + { + $subscriber = $this->api->get_subscriber_tags($this->test_user_id); + $this->assertInstanceOf('stdClass', $subscriber); + $this->assertArrayHasKey('tags', get_object_vars($subscriber)); + } + + /** + * Subscribe and unsubscribe from form + */ + public function testUserActions() + { + $random_email = str_shuffle('1234567890') . 'test@growdevelopment.com'; + + /* + * Subscribe + */ + $options = [ + 'email' => $random_email, + 'name' => 'Full Name', + 'first_name' => 'First Name', + 'tags' => $this->test_tag_id, + 'fields' => [ + 'phone' => 134567891243, + 'shirt_size' => 'M', + 'website_url' => 'testurl.com', + ], + ]; + + $subscribed = $this->api->form_subscribe($this->test_form_id, $options); + $this->assertInstanceOf('stdClass', $subscribed); + $this->assertArrayHasKey('subscription', get_object_vars($subscribed)); + $this->assertArrayHasKey('id', get_object_vars($subscribed->subscription)); + $this->assertEquals(get_object_vars($subscribed->subscription)['subscribable_id'], $this->test_form_id); + + /* + * Add tag + */ + $added_tag = $this->api->add_tag($this->test_tag_id, [ + 'email' => $random_email, + ]); + $this->assertInstanceOf('stdClass', $added_tag); + $this->assertArrayHasKey('subscription', get_object_vars($added_tag)); + $this->assertArrayHasKey('id', get_object_vars($added_tag->subscription)); + $this->assertEquals(get_object_vars($added_tag->subscription)['subscribable_id'], $this->test_tag_id); + $this->assertEquals(get_object_vars($added_tag->subscription)['subscribable_type'], 'tag'); + + /* + * Purchase + */ + $purchase_options = [ + 'purchase' => [ + 'email_address' => $random_email, + 'transaction_id' => str_shuffle('wfervdrtgsdewrafvwefds'), + 'subtotal' => 20.00, + 'tax' => 2.00, + 'shipping' => 2.00, + 'discount' => 3.00, + 'total' => 21.00, + 'status' => 'paid', + 'products' => array( + 0 => array( + 'name' => 'Floppy Disk (512k)', + 'sku' => '7890-ijkl', + 'unit_price' => 5.00, + 'quantity' => 2, + ), + ), + ], + ]; + $purchase = $this->api->create_purchase($purchase_options); + $this->assertInstanceOf('stdClass', $purchase); + $this->assertArrayHasKey('transaction_id', get_object_vars($purchase)); + + /* + * Unsubscribe + */ + $unsubscribed = $this->api->form_unsubscribe([ + 'email' => $random_email, + ]); + + $this->assertInstanceOf('stdClass', $unsubscribed); + $this->assertArrayHasKey('subscriber', get_object_vars($unsubscribed)); + $this->assertArrayHasKey('email_address', get_object_vars($unsubscribed->subscriber)); + $this->assertEquals(get_object_vars($unsubscribed->subscriber)['email_address'], $random_email); + } + + /** + * List purchases + */ + public function testListPurchases() + { + $list_purchases = $this->api->list_purchases(['page' => 1]); + $this->assertInstanceOf('stdClass', $list_purchases); + $this->assertArrayHasKey('total_purchases', get_object_vars($list_purchases)); + $this->assertArrayHasKey('page', get_object_vars($list_purchases)); + $this->assertArrayHasKey('total_pages', get_object_vars($list_purchases)); + $this->assertArrayHasKey('purchases', get_object_vars($list_purchases)); + } + + /** + * Get resources + */ + public function testGetResources() + { + $resources = ['forms', 'landing_pages', 'tags']; + + foreach ($resources as $resource) { + $get_resources = $this->api->get_resources($resource); + $this->assertTrue(is_array($get_resources) || empty($get_resources)); + if (count($get_resources) > 0) { + $get_resource = $get_resources[0]; + $this->assertInstanceOf('stdClass', $get_resource); + $this->assertArrayHasKey('id', get_object_vars($get_resource)); + $this->assertArrayHasKey('name', get_object_vars($get_resource)); + } + } + } + + /** + * Get subscription forms + */ + public function testGetLandingPages() + { + $landing_pages = $this->api->get_resources('subscription_forms'); + $this->assertTrue(is_array($landing_pages) || empty($landing_pages)); + } + + /** + * Get resource by url + */ + public function testGetResource() + { + $markup = $this->api->get_resource($this->test_form_url); + $this->assertTrue($this->isHtml($markup)); + } + + /** + * Checks if string is html + * + * @param $string + * + * @return bool + */ + protected static function isHtml($string) + { + return preg_match("/<[^<]+>/", $string, $m) != 0; + } + + /** + * List purchases + */ + public function testFormSubscriptions() + { + $random_email = str_shuffle('1234567890') . 'test@growdevelopment.com'; + + /* + * Subscribe + */ + $options = [ + 'email' => $random_email, + 'name' => 'Full Name', + 'first_name' => 'First Name', + 'tags' => $this->test_tag_id, + 'fields' => [ + 'phone' => 134567891243, + 'shirt_size' => 'M', + 'website_url' => 'testurl.com', + ], + ]; + + $subscribed = $this->api->form_subscribe($this->test_form_id, $options); + + $subscriptions = $this->api->get_form_subscriptions($this->test_form_id); + $this->assertInstanceOf('stdClass', $subscriptions); + $this->assertArrayHasKey('total_subscriptions', get_object_vars($subscriptions)); + $this->assertArrayHasKey('page', get_object_vars($subscriptions)); + $this->assertArrayHasKey('total_pages', get_object_vars($subscriptions)); + $this->assertArrayHasKey('subscriptions', get_object_vars($subscriptions)); + } + + /** + * List purchases + */ + public function testTagSubscriptions() + { + $random_email = str_shuffle('1234567890') . 'test@growdevelopment.com'; + + /* + * Subscribe + */ + $options = [ + 'email' => $random_email, + 'name' => 'Full Name', + 'first_name' => 'First Name', + 'tags' => $this->test_tag_id, + 'fields' => [ + 'phone' => 134567891243, + 'shirt_size' => 'M', + 'website_url' => 'testurl.com', + ], + ]; + + $subscribed = $this->api->form_subscribe($this->test_form_id, $options); + + $subscriptions = $this->api->get_tag_subscriptions($this->test_tag_id); + $this->assertInstanceOf('stdClass', $subscriptions); + $this->assertArrayHasKey('total_subscriptions', get_object_vars($subscriptions)); + $this->assertArrayHasKey('page', get_object_vars($subscriptions)); + $this->assertArrayHasKey('total_pages', get_object_vars($subscriptions)); + $this->assertArrayHasKey('subscriptions', get_object_vars($subscriptions)); + } +} From 5bff8d581b20da3d50f287720ea5f3a8c9f7631a Mon Sep 17 00:00:00 2001 From: Silas Ribas Martins Date: Wed, 18 Sep 2019 13:53:05 -0300 Subject: [PATCH 7/7] Fix http method --- src/ConvertKitAPI.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ConvertKitAPI.php b/src/ConvertKitAPI.php index 95022df..0949d1b 100644 --- a/src/ConvertKitAPI.php +++ b/src/ConvertKitAPI.php @@ -656,7 +656,7 @@ public function get_form_subscriptions($form_id, $page = 1, $subscriber_state = $this->create_log(sprintf("GET form subscriptions: %s, %s, %s", $request, json_encode($options), $form_id)); - return $this->make_request($request, 'POST', $options); + return $this->make_request($request, 'GET', $options); } /** @@ -684,6 +684,6 @@ public function get_tag_subscriptions($tag_id, $page = 1, $subscriber_state = nu $this->create_log(sprintf("GET tag subscriptions: %s, %s, %s", $request, json_encode($options), $tag_id)); - return $this->make_request($request, 'POST', $options); + return $this->make_request($request, 'GET', $options); } }