From b4e79cc22248cfc50ce9fd6b1019d24fbe9f6de6 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Fri, 3 Mar 2023 16:57:56 +0000 Subject: [PATCH 1/4] Apply PSR-12 coding standards to API class --- .github/workflows/tests.yml | 4 + phpcs.xml | 23 +++ src/ConvertKit_API.php | 270 ++++++++++++++++++++---------------- 3 files changed, 175 insertions(+), 122 deletions(-) create mode 100644 phpcs.xml diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 6de1565..19f579f 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -64,6 +64,10 @@ jobs: - name: Build PHP Autoloader run: composer dump-autoload + # Run Coding Standards. + - name: Run Coding Standards + run: php vendor/bin/phpcs --standard=phpcs.xml + # Run Coding Standards on Tests. - name: Run Coding Standards on Tests run: php vendor/bin/phpcs --standard=phpcs.tests.xml diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..1b08dff --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,23 @@ + + + Coding Standards + + + src + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/ConvertKit_API.php b/src/ConvertKit_API.php index e2827f2..d321f0d 100644 --- a/src/ConvertKit_API.php +++ b/src/ConvertKit_API.php @@ -7,9 +7,8 @@ use GuzzleHttp\Client; use GuzzleHttp\Psr7\Request; - -class ConvertKit_API { - +class ConvertKit_API +{ /** * ConvertKit API Key * @@ -80,21 +79,23 @@ class ConvertKit_API { * @param string $api_secret ConvertKit API Secret. * @param boolean $debug if log debug info. */ - public function __construct( $api_key, $api_secret, $debug = false ) { + 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 ) { + if ($debug) { $this->debug_logger = new Logger('ck-debug'); - $this->debug_logger->pushHandler(new StreamHandler(__DIR__.'/logs/debug.log', Logger::DEBUG)); + $this->debug_logger->pushHandler(new StreamHandler(__DIR__ . '/logs/debug.log', Logger::DEBUG)); } } - private function create_log($message) { - if($this->debug) { + private function create_log($message) + { + if ($this->debug) { $this->debug_logger->info($message); } } @@ -114,7 +115,7 @@ 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); } /** @@ -132,7 +133,7 @@ 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); } /** @@ -152,9 +153,14 @@ public function get_sequence_subscriptions($sequence_id, $sort_order = 'asc') 'sort_order' => $sort_order ); - $this->create_log(sprintf("GET sequence subscriptions: %s, %s, %s", $request, json_encode($options), $sequence_id)); + $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); } /** @@ -174,9 +180,15 @@ public function add_subscriber_to_sequence($sequence_id, $email) 'email' => $email ); - $this->create_log(sprintf("POST add subscriber to sequence: %s, %s, %s, %s", $request, json_encode($options), $sequence_id, $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 ); + return $this->make_request($request, 'POST', $options); } /** @@ -186,19 +198,20 @@ public function add_subscriber_to_sequence($sequence_id, $email) * @param array $options Array of user data * @return false|object */ - public function add_tag( $tag, $options ) { + public function add_tag($tag, $options) + { - if( !is_int($tag) || !is_array($options) ) { - throw new \InvalidArgumentException; + if (!is_int($tag) || !is_array($options)) { + throw new \InvalidArgumentException(); } - $request = $this->api_version . sprintf( '/tags/%s/subscribe', $tag ); + $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 ); + return $this->make_request($request, 'POST', $options); } /** @@ -210,14 +223,14 @@ public function add_tag( $tag, $options ) { * @param string $resource Resource type. * @return object API response */ - public function get_resources( $resource ) { + public function get_resources($resource) + { - if( !is_string($resource) ) { - throw new \InvalidArgumentException; + if (!is_string($resource)) { + throw new \InvalidArgumentException(); } - if ( ! array_key_exists( $resource, $this->resources ) ) { - + if (! array_key_exists($resource, $this->resources)) { $options = array( 'api_key' => $this->api_key, 'timeout' => 10, @@ -228,9 +241,9 @@ public function get_resources( $resource ) { $this->create_log(sprintf("GET request %s, %s", $request, json_encode($options))); - $resources = $this->make_request( $request, 'GET', $options ); + $resources = $this->make_request($request, 'GET', $options); - if(!$resources) { + if (!$resources) { $this->create_log("No resources"); $this->resources[ $resource ] = array( array( @@ -241,45 +254,44 @@ public function get_resources( $resource ) { } else { $_resource = array(); - if ( 'forms' === $resource ) { - $response = isset( $resources->forms ) ? $resources->forms : 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 ) { + foreach ($response as $form) { + if (isset($form->archived) && $form->archived) { continue; } $_resource[] = $form; } - } elseif ( 'landing_pages' === $resource ) { - $response = isset($resources->forms ) ? $resources->forms : array(); + } 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 ) { + 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 ) { + } elseif ('subscription_forms' === $resource) { $this->create_log("subscription_forms"); - foreach ( $resources as $mapping ) { - if ( isset( $mapping->archived ) && $mapping->archived ) { + 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(); + } elseif ('tags' === $resource) { + $response = isset($resources->tags) ? $resources->tags : array(); $this->create_log(sprintf("tags response %s", json_encode($response))); - foreach ( $response as $tag ) { + foreach ($response as $tag) { $_resource[] = $tag; } } $this->resources[ $resource ] = $_resource; } - } return $this->resources[ $resource ]; @@ -293,20 +305,20 @@ public function get_resources( $resource ) { * * @return false|object */ - public function form_subscribe( $form_id, $options ) { + public function form_subscribe($form_id, $options) + { - if( !is_int($form_id) || !is_array($options) ) { - throw new \InvalidArgumentException; + if (!is_int($form_id) || !is_array($options)) { + throw new \InvalidArgumentException(); } - $request = $this->api_version . sprintf( '/forms/%s/subscribe', $form_id ); + $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 ); - + return $this->make_request($request, 'POST', $options); } /** @@ -316,10 +328,11 @@ public function form_subscribe( $form_id, $options ) { * * @return false|object */ - public function form_unsubscribe( $options ) { + public function form_unsubscribe($options) + { - if( !is_array($options) ) { - throw new \InvalidArgumentException; + if (!is_array($options)) { + throw new \InvalidArgumentException(); } $request = $this->api_version . '/unsubscribe'; @@ -328,7 +341,7 @@ public function form_unsubscribe( $options ) { $this->create_log(sprintf("PUT form unsubscribe: %s, %s", $request, json_encode($options))); - return $this->make_request( $request, 'PUT', $options ); + return $this->make_request($request, 'PUT', $options); } /** @@ -338,10 +351,11 @@ public function form_unsubscribe( $options ) { * @param $email_address string * @return false|int $subscriber_id */ - public function get_subscriber_id( $email_address ) { + public function get_subscriber_id($email_address) + { - if( !is_string($email_address) || !filter_var($email_address, FILTER_VALIDATE_EMAIL)) { - throw new \InvalidArgumentException; + if (!is_string($email_address) || !filter_var($email_address, FILTER_VALIDATE_EMAIL)) { + throw new \InvalidArgumentException(); } $request = $this->api_version . '/subscribers'; @@ -352,25 +366,29 @@ public function get_subscriber_id( $email_address ) { 'email_address' => $email_address, ); - $this->create_log(sprintf("GET subscriber id from all subscribers: %s, %s, %s", $request, json_encode($options), $email_address)); + $this->create_log(sprintf( + "GET subscriber id from all subscribers: %s, %s, %s", + $request, + json_encode($options), + $email_address + )); - $subscribers = $this->make_request( $request, 'GET', $options ); + $subscribers = $this->make_request($request, 'GET', $options); - if( !$subscribers ) { + if (!$subscribers) { $this->create_log("No subscribers"); return false; } $subscriber_id = $this::check_if_subscriber_in_array($email_address, $subscribers->subscribers); - if($subscriber_id) { + if ($subscriber_id) { return $subscriber_id; } $this->create_log("Subscriber not found"); return false; - } /** @@ -380,13 +398,14 @@ public function get_subscriber_id( $email_address ) { * * @return false|int */ - public function get_subscriber( $subscriber_id ) { + public function get_subscriber($subscriber_id) + { - if( !is_int($subscriber_id) || $subscriber_id < 1 ) { - throw new \InvalidArgumentException; + if (!is_int($subscriber_id) || $subscriber_id < 1) { + throw new \InvalidArgumentException(); } - $request = $this->api_version . sprintf( '/subscribers/%s', $subscriber_id ); + $request = $this->api_version . sprintf('/subscribers/%s', $subscriber_id); $options = array( 'api_secret' => $this->api_secret, @@ -394,8 +413,7 @@ public function get_subscriber( $subscriber_id ) { $this->create_log(sprintf("GET subscriber tags: %s, %s, %s", $request, json_encode($options), $subscriber_id)); - return $this->make_request( $request, 'GET', $options ); - + return $this->make_request($request, 'GET', $options); } /** @@ -404,13 +422,14 @@ public function get_subscriber( $subscriber_id ) { * @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 ) { + public function get_subscriber_tags($subscriber_id) + { - if( !is_int($subscriber_id) || $subscriber_id < 1 ) { - throw new \InvalidArgumentException; + if (!is_int($subscriber_id) || $subscriber_id < 1) { + throw new \InvalidArgumentException(); } - $request = $this->api_version . sprintf( '/subscribers/%s/tags', $subscriber_id ); + $request = $this->api_version . sprintf('/subscribers/%s/tags', $subscriber_id); $options = array( 'api_key' => $this->api_key, @@ -418,8 +437,7 @@ public function get_subscriber_tags( $subscriber_id ) { $this->create_log(sprintf("GET subscriber tags: %s, %s, %s", $request, json_encode($options), $subscriber_id)); - return $this->make_request( $request, 'GET', $options ); - + return $this->make_request($request, 'GET', $options); } /** @@ -427,10 +445,11 @@ public function get_subscriber_tags( $subscriber_id ) { * * @return false|object */ - public function list_purchases($options) { + public function list_purchases($options) + { - if( !is_array($options) ) { - throw new \InvalidArgumentException; + if (!is_array($options)) { + throw new \InvalidArgumentException(); } $request = $this->api_version . '/purchases'; @@ -439,7 +458,7 @@ public function list_purchases($options) { $this->create_log(sprintf("GET list purchases: %s, %s", $request, json_encode($options))); - return $this->make_request( $request, 'GET', $options ); + return $this->make_request($request, 'GET', $options); } /** @@ -449,10 +468,11 @@ public function list_purchases($options) { * * @return false|object */ - public function create_purchase($options) { + public function create_purchase($options) + { - if( !is_array($options) ) { - throw new \InvalidArgumentException; + if (!is_array($options)) { + throw new \InvalidArgumentException(); } $request = $this->api_version . '/purchases'; @@ -461,12 +481,12 @@ public function create_purchase($options) { $this->create_log(sprintf("POST create purchase: %s, %s", $request, json_encode($options))); - return $this->make_request( $request, 'POST', $options ); + return $this->make_request($request, 'POST', $options); } /** * Get markup from ConvertKit for the provided $url. - * + * * Supports legacy forms and legacy landing pages. * Forms and Landing Pages should be embedded using the supplied JS embed script in * the API response when using get_resources(). @@ -474,10 +494,11 @@ public function create_purchase($options) { * @param string $url URL of API action. * @return false|string */ - public function get_resource( $url ) { + public function get_resource($url) + { - if( !is_string($url) || !filter_var($url, FILTER_VALIDATE_URL)) { - throw new \InvalidArgumentException; + if (!is_string($url) || !filter_var($url, FILTER_VALIDATE_URL)) { + throw new \InvalidArgumentException(); } $resource = ''; @@ -485,7 +506,7 @@ public function get_resource( $url ) { $this->create_log(sprintf("Getting resource %s", $url)); // If the resource was already fetched, return the cached version now. - if ( isset( $this->markup[ $url ] ) ) { + if (isset($this->markup[ $url ])) { $this->create_log("Resource already set"); return $this->markup[ $url ]; } @@ -500,31 +521,37 @@ public function get_resource( $url ) { $body = $response->getBody()->getContents(); // Forcibly tell DOMDocument that this HTML uses the UTF-8 charset. - // isn't enough, as DOMDocument still interprets the HTML as ISO-8859, which breaks character encoding + // isn't enough, as DOMDocument still interprets the HTML as ISO-8859, + // which breaks character encoding. // Use of mb_convert_encoding() with HTML-ENTITIES is deprecated in PHP 8.2, so we have to use this method. // If we don't, special characters render incorrectly. - $body = str_replace( '', '' . "\n" . '', $body ); + $body = str_replace( + '', + '' . "\n" . '', + $body + ); // Get just the scheme and host from the URL. - $url_scheme = parse_url( $url ); + $url_scheme = parse_url($url); $url_scheme_host_only = $url_scheme['scheme'] . '://' . $url_scheme['host']; // Load the HTML into a DOMDocument. - libxml_use_internal_errors( true ); + libxml_use_internal_errors(true); $html = new \DOMDocument(); - $html->loadHTML( $body ); + $html->loadHTML($body); // Convert any relative URLs to absolute URLs in the HTML DOM. - $this->convert_relative_to_absolute_urls( $html->getElementsByTagName( 'a' ), 'href', $url_scheme_host_only ); - $this->convert_relative_to_absolute_urls( $html->getElementsByTagName( 'link' ), 'href', $url_scheme_host_only ); - $this->convert_relative_to_absolute_urls( $html->getElementsByTagName( 'img' ), 'src', $url_scheme_host_only ); - $this->convert_relative_to_absolute_urls( $html->getElementsByTagName( 'script' ), 'src', $url_scheme_host_only ); - $this->convert_relative_to_absolute_urls( $html->getElementsByTagName( 'form' ), 'action', $url_scheme_host_only ); + $this->convert_relative_to_absolute_urls($html->getElementsByTagName('a'), 'href', $url_scheme_host_only); + $this->convert_relative_to_absolute_urls($html->getElementsByTagName('link'), 'href', $url_scheme_host_only); + $this->convert_relative_to_absolute_urls($html->getElementsByTagName('img'), 'src', $url_scheme_host_only); + $this->convert_relative_to_absolute_urls($html->getElementsByTagName('script'), 'src', $url_scheme_host_only); + $this->convert_relative_to_absolute_urls($html->getElementsByTagName('form'), 'action', $url_scheme_host_only); // Remove some HTML tags that DOMDocument adds, returning the output. - // We do this instead of using LIBXML_HTML_NOIMPLIED in loadHTML(), because Legacy Forms are not always contained in - // a single root / outer element, which is required for LIBXML_HTML_NOIMPLIED to correctly work. - $resource = $this->strip_html_head_body_tags( $html->saveHTML() ); + // We do this instead of using LIBXML_HTML_NOIMPLIED in loadHTML(), because Legacy Forms + // are not always contained in a single root / outer element, which is required for + // LIBXML_HTML_NOIMPLIED to correctly work. + $resource = $this->strip_html_head_body_tags($html->saveHTML()); // Cache and return. $this->markup[ $url ] = $resource; @@ -541,28 +568,28 @@ public function get_resource( $url ) { * @param string $attribute HTML Attribute. * @param string $url Absolute URL to prepend to relative URLs. */ - private function convert_relative_to_absolute_urls( $elements, $attribute, $url ) + private function convert_relative_to_absolute_urls($elements, $attribute, $url) { // Anchor hrefs. - foreach ( $elements as $element ) { + foreach ($elements as $element) { // Skip if the attribute's value is empty. - if ( empty( $element->getAttribute( $attribute ) ) ) { + if (empty($element->getAttribute($attribute))) { continue; } // Skip if the attribute's value is a fully qualified URL. - if ( filter_var( $element->getAttribute( $attribute ), FILTER_VALIDATE_URL ) ) { + if (filter_var($element->getAttribute($attribute), FILTER_VALIDATE_URL)) { continue; } // Skip if this is a Google Font CSS URL. - if ( strpos( $element->getAttribute( $attribute ), '//fonts.googleapis.com' ) !== false ) { + if (strpos($element->getAttribute($attribute), '//fonts.googleapis.com') !== false) { continue; } // If here, the attribute's value is a relative URL, missing the http(s) and domain. // Prepend the URL to the attribute's value. - $element->setAttribute( $attribute, $url . $element->getAttribute( $attribute ) ); + $element->setAttribute($attribute, $url . $element->getAttribute($attribute)); } } @@ -575,15 +602,15 @@ private function convert_relative_to_absolute_urls( $elements, $attribute, $url * @param string $markup HTML Markup. * @return string HTML Markup * */ - private function strip_html_head_body_tags( $markup ) + private function strip_html_head_body_tags($markup) { - $markup = str_replace( '', '', $markup ); - $markup = str_replace( '', '', $markup ); - $markup = str_replace( '', '', $markup ); - $markup = str_replace( '', '', $markup ); - $markup = str_replace( '', '', $markup ); - $markup = str_replace( '', '', $markup ); - $markup = str_replace( '', '', $markup ); + $markup = str_replace('', '', $markup); + $markup = str_replace('', '', $markup); + $markup = str_replace('', '', $markup); + $markup = str_replace('', '', $markup); + $markup = str_replace('', '', $markup); + $markup = str_replace('', '', $markup); + $markup = str_replace('', '', $markup); return $markup; } @@ -595,10 +622,11 @@ private function strip_html_head_body_tags( $markup ) * * @return false|mixed */ - public function make_request($endpoint, $method, $args = array()) { + public function make_request($endpoint, $method, $args = array()) + { - if( !is_string($endpoint) || !is_string($method) || !is_array($args) ) { - throw new \InvalidArgumentException; + if (!is_string($endpoint) || !is_string($method) || !is_array($args)) { + throw new \InvalidArgumentException(); } $url = $this->api_url_base . $endpoint; @@ -609,8 +637,8 @@ public function make_request($endpoint, $method, $args = array()) { $this->create_log(sprintf("%s, Request body: %s", $method, $request_body)); - if( $method === "GET" ){ - if($args) { + if ($method === "GET") { + if ($args) { $url .= '?' . http_build_query($args); } $request = new Request($method, $url); @@ -635,14 +663,13 @@ public function make_request($endpoint, $method, $args = array()) { $response_body = json_decode($response->getBody()->getContents()); - if($response_body) { + if ($response_body) { $this->create_log("Finish request successfully."); return $response_body; } $this->create_log("Failed to finish request."); return false; - } /** @@ -653,7 +680,8 @@ public function make_request($endpoint, $method, $args = array()) { * * @return false|int false if not found, else subscriber object */ - private function check_if_subscriber_in_array($email_address, $subscribers) { + private function check_if_subscriber_in_array($email_address, $subscribers) + { foreach ($subscribers as $subscriber) { if ($subscriber->email_address === $email_address) { @@ -664,7 +692,5 @@ private function check_if_subscriber_in_array($email_address, $subscribers) { $this->create_log("Subscriber not found on current page."); return false; - } - } From e197ec85887bba019c311b2111f1aa14e9c8498e Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Fri, 3 Mar 2023 16:59:26 +0000 Subject: [PATCH 2/4] Remove blank lines after opening function brace --- src/ConvertKit_API.php | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/ConvertKit_API.php b/src/ConvertKit_API.php index d321f0d..a472702 100644 --- a/src/ConvertKit_API.php +++ b/src/ConvertKit_API.php @@ -81,7 +81,6 @@ class ConvertKit_API */ public function __construct($api_key, $api_secret, $debug = false) { - $this->api_key = $api_key; $this->api_secret = $api_secret; $this->debug = $debug; @@ -200,7 +199,6 @@ public function add_subscriber_to_sequence($sequence_id, $email) */ public function add_tag($tag, $options) { - if (!is_int($tag) || !is_array($options)) { throw new \InvalidArgumentException(); } @@ -225,7 +223,6 @@ public function add_tag($tag, $options) */ public function get_resources($resource) { - if (!is_string($resource)) { throw new \InvalidArgumentException(); } @@ -307,7 +304,6 @@ public function get_resources($resource) */ public function form_subscribe($form_id, $options) { - if (!is_int($form_id) || !is_array($options)) { throw new \InvalidArgumentException(); } @@ -330,7 +326,6 @@ public function form_subscribe($form_id, $options) */ public function form_unsubscribe($options) { - if (!is_array($options)) { throw new \InvalidArgumentException(); } @@ -353,7 +348,6 @@ public function form_unsubscribe($options) */ public function get_subscriber_id($email_address) { - if (!is_string($email_address) || !filter_var($email_address, FILTER_VALIDATE_EMAIL)) { throw new \InvalidArgumentException(); } @@ -400,7 +394,6 @@ public function get_subscriber_id($email_address) */ public function get_subscriber($subscriber_id) { - if (!is_int($subscriber_id) || $subscriber_id < 1) { throw new \InvalidArgumentException(); } @@ -424,7 +417,6 @@ public function get_subscriber($subscriber_id) */ public function get_subscriber_tags($subscriber_id) { - if (!is_int($subscriber_id) || $subscriber_id < 1) { throw new \InvalidArgumentException(); } @@ -447,7 +439,6 @@ public function get_subscriber_tags($subscriber_id) */ public function list_purchases($options) { - if (!is_array($options)) { throw new \InvalidArgumentException(); } @@ -470,7 +461,6 @@ public function list_purchases($options) */ public function create_purchase($options) { - if (!is_array($options)) { throw new \InvalidArgumentException(); } @@ -496,7 +486,6 @@ public function create_purchase($options) */ public function get_resource($url) { - if (!is_string($url) || !filter_var($url, FILTER_VALIDATE_URL)) { throw new \InvalidArgumentException(); } @@ -624,7 +613,6 @@ private function strip_html_head_body_tags($markup) */ public function make_request($endpoint, $method, $args = array()) { - if (!is_string($endpoint) || !is_string($method) || !is_array($args)) { throw new \InvalidArgumentException(); } @@ -682,7 +670,6 @@ public function make_request($endpoint, $method, $args = array()) */ 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!"); From 9dc57ff92965cce966e4e6e0d3cb38d3495aa6e6 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Fri, 3 Mar 2023 18:48:43 +0000 Subject: [PATCH 3/4] Updated coding standard rules to favor PSR-12, and import/honor Squiz docblock standards --- phpcs.xml | 45 ++++- src/ConvertKit_API.php | 425 +++++++++++++++++++++++++---------------- 2 files changed, 304 insertions(+), 166 deletions(-) diff --git a/phpcs.xml b/phpcs.xml index 1b08dff..4153cb7 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -11,13 +11,48 @@ - - + + - + + + + + + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/ConvertKit_API.php b/src/ConvertKit_API.php index a472702..cd78078 100644 --- a/src/ConvertKit_API.php +++ b/src/ConvertKit_API.php @@ -1,4 +1,11 @@ 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) { $this->debug_logger = new Logger('ck-debug'); - $this->debug_logger->pushHandler(new StreamHandler(__DIR__ . '/logs/debug.log', Logger::DEBUG)); + $stream_handler = new StreamHandler(__DIR__ . '/logs/debug.log', Logger::DEBUG); + $this->debug_logger->pushHandler( + $stream_handler // phpcs:ignore Squiz.Objects.ObjectInstantiation.NotAssigned + ); } } - private function create_log($message) + /** + * Add an entry to monologger. + * + * @param string $message Message. + * + * @return void + */ + private function create_log(string $message) { if ($this->debug) { $this->debug_logger->info($message); } } + /** * Gets the current account * @@ -108,15 +130,16 @@ public function get_account() { $request = $this->api_version . '/account'; - $options = array( + $options = [ 'api_secret' => $this->api_secret, - ); + ]; - $this->create_log(sprintf("GET account: %s, %s", $request, json_encode($options))); + $this->create_log(sprintf('GET account: %s, %s', $request, json_encode($options))); return $this->make_request($request, 'GET', $options); } + /** * Gets all sequences * @@ -126,78 +149,88 @@ public function get_sequences() { $request = $this->api_version . '/sequences'; - $options = array( + $options = [ 'api_key' => $this->api_key, - ); + ]; - $this->create_log(sprintf("GET sequences: %s, %s", $request, json_encode($options))); + $this->create_log(sprintf('GET sequences: %s, %s', $request, json_encode($options))); return $this->make_request($request, 'GET', $options); } + /** * Gets subscribers to a sequence * - * @param $sequence_id - * @param string $sort_order + * @param integer $sequence_id Sequence ID. + * @param string $sort_order Sort Order (asc|desc). * * @return false|mixed */ - public function get_sequence_subscriptions($sequence_id, $sort_order = 'asc') + public function get_sequence_subscriptions(int $sequence_id, string $sort_order = 'asc') { $request = $this->api_version . sprintf('/sequences/%s/subscriptions', $sequence_id); - $options = array( + $options = [ '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 + ) ); - $this->create_log(sprintf( - "GET sequence subscriptions: %s, %s, %s", - $request, - json_encode($options), - $sequence_id - )); - return $this->make_request($request, 'GET', $options); } + /** * Adds a subscriber to a sequence by email address * - * @param $sequence_id - * @param $email + * @param integer $sequence_id Sequence ID. + * @param string $email Email Address. * * @return false|mixed */ - public function add_subscriber_to_sequence($sequence_id, $email) + public function add_subscriber_to_sequence(int $sequence_id, string $email) { $request = $this->api_version . sprintf('/courses/%s/subscribe', $sequence_id); - $options = array( + $options = [ '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 + ) ); - $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 + * @param integer $tag Tag ID. + * @param array $options Array of user data. + * + * @throws \InvalidArgumentException If the provided arguments are not of the expected type. + * * @return false|object */ - public function add_tag($tag, $options) + public function add_tag(int $tag, array $options) { if (!is_int($tag) || !is_array($options)) { throw new \InvalidArgumentException(); @@ -207,11 +240,12 @@ public function add_tag($tag, $options) $options['api_key'] = $this->api_key; - $this->create_log(sprintf("POST add tag: %s, %s, %s", $request, json_encode($options), $tag)); + $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 @@ -219,90 +253,111 @@ public function add_tag($tag, $options) * GET /{$resource}/ * * @param string $resource Resource type. + * + * @throws \InvalidArgumentException If the provided arguments are not of the expected type. + * * @return object API response */ - public function get_resources($resource) + public function get_resources(string $resource) { if (!is_string($resource)) { throw new \InvalidArgumentException(); } if (! array_key_exists($resource, $this->resources)) { - $options = array( - 'api_key' => $this->api_key, - 'timeout' => 10, + $options = [ + 'api_key' => $this->api_key, + 'timeout' => 10, 'Accept-Encoding' => 'gzip', - ); + ]; - $request = sprintf('/%s/%s', $this->api_version, $resource === 'landing_pages' ? 'forms' : $resource); + $request = sprintf('/%s/%s', $this->api_version, (($resource === 'landing_pages') ? 'forms' : $resource)); - $this->create_log(sprintf("GET request %s, %s", $request, json_encode($options))); + $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', + $this->create_log('No resources'); + $this->resources[$resource] = [ + [ + 'id' => '-2', 'name' => 'Error contacting API', - ), - ); + ], + ]; } else { - $_resource = array(); + $_resource = []; if ('forms' === $resource) { - $response = isset($resources->forms) ? $resources->forms : array(); - $this->create_log(sprintf("forms response %s", json_encode($response))); + $response = []; + if (isset($resources->forms)) { + $response = $resources->forms; + } + + $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))); + } else if ('landing_pages' === $resource) { + $response = []; + if (isset($resources->forms)) { + $response = $resources->forms; + } + + $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"); + } 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; + + $_resource[$mapping->id] = $mapping->form_id; + } + } else if ('tags' === $resource) { + $response = []; + if (isset($resources->tags)) { + $response = $resources->tags; } - } elseif ('tags' === $resource) { - $response = isset($resources->tags) ? $resources->tags : array(); - $this->create_log(sprintf("tags response %s", json_encode($response))); + + $this->create_log(sprintf('tags response %s', json_encode($response))); foreach ($response as $tag) { $_resource[] = $tag; } - } + }//end if - $this->resources[ $resource ] = $_resource; - } - } + $this->resources[$resource] = $_resource; + }//end if + }//end if - return $this->resources[ $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). + * @param integer $form_id Form ID. + * @param array $options Array of user data (email, name). + * + * @throws \InvalidArgumentException If the provided arguments are not of the expected type. * * @return false|object */ - public function form_subscribe($form_id, $options) + public function form_subscribe(int $form_id, array $options) { if (!is_int($form_id) || !is_array($options)) { throw new \InvalidArgumentException(); @@ -312,19 +367,22 @@ public function form_subscribe($form_id, $options) $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('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). * + * @throws \InvalidArgumentException If the provided arguments are not of the expected type. + * * @return false|object */ - public function form_unsubscribe($options) + public function form_unsubscribe(array $options) { if (!is_array($options)) { throw new \InvalidArgumentException(); @@ -334,19 +392,23 @@ public function form_unsubscribe($options) $options['api_secret'] = $this->api_secret; - $this->create_log(sprintf("PUT form unsubscribe: %s, %s", $request, json_encode($options))); + $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 + * @param string $email_address Email Address. + * + * @throws \InvalidArgumentException If the provided arguments are not of the expected type. + * + * @return false|integer */ - public function get_subscriber_id($email_address) + public function get_subscriber_id(string $email_address) { if (!is_string($email_address) || !filter_var($email_address, FILTER_VALIDATE_EMAIL)) { throw new \InvalidArgumentException(); @@ -354,23 +416,25 @@ public function get_subscriber_id($email_address) $request = $this->api_version . '/subscribers'; - $options = array( - 'api_secret' => $this->api_secret, - 'status' => 'all', + $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 + ) ); - $this->create_log(sprintf( - "GET subscriber id from all subscribers: %s, %s, %s", - $request, - json_encode($options), - $email_address - )); - $subscribers = $this->make_request($request, 'GET', $options); if (!$subscribers) { - $this->create_log("No subscribers"); + $this->create_log('No subscribers'); return false; } @@ -380,19 +444,22 @@ public function get_subscriber_id($email_address) return $subscriber_id; } - $this->create_log("Subscriber not found"); + $this->create_log('Subscriber not found'); return false; } + /** * Get subscriber by id * - * @param $subscriber_id int + * @param integer $subscriber_id Subscriber ID. + * + * @throws \InvalidArgumentException If the provided arguments are not of the expected type. * - * @return false|int + * @return false|integer */ - public function get_subscriber($subscriber_id) + public function get_subscriber(int $subscriber_id) { if (!is_int($subscriber_id) || $subscriber_id < 1) { throw new \InvalidArgumentException(); @@ -400,22 +467,26 @@ public function get_subscriber($subscriber_id) $request = $this->api_version . sprintf('/subscribers/%s', $subscriber_id); - $options = array( + $options = [ 'api_secret' => $this->api_secret, - ); + ]; - $this->create_log(sprintf("GET subscriber tags: %s, %s, %s", $request, json_encode($options), $subscriber_id)); + $this->create_log(sprintf('GET subscriber tags: %s, %s, %s', $request, json_encode($options), $subscriber_id)); return $this->make_request($request, 'GET', $options); } + /** * 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 + * @param integer $subscriber_id Subscriber ID. + * + * @throws \InvalidArgumentException If the provided arguments are not of the expected type. + * + * @return false|array */ - public function get_subscriber_tags($subscriber_id) + public function get_subscriber_tags(int $subscriber_id) { if (!is_int($subscriber_id) || $subscriber_id < 1) { throw new \InvalidArgumentException(); @@ -423,21 +494,26 @@ public function get_subscriber_tags($subscriber_id) $request = $this->api_version . sprintf('/subscribers/%s/tags', $subscriber_id); - $options = array( + $options = [ 'api_key' => $this->api_key, - ); + ]; - $this->create_log(sprintf("GET subscriber tags: %s, %s, %s", $request, json_encode($options), $subscriber_id)); + $this->create_log(sprintf('GET subscriber tags: %s, %s, %s', $request, json_encode($options), $subscriber_id)); return $this->make_request($request, 'GET', $options); } + /** - * @param $options + * List purchases. + * + * @param array $options Request options. + * + * @throws \InvalidArgumentException If the provided arguments are not of the expected type. * * @return false|object */ - public function list_purchases($options) + public function list_purchases(array $options) { if (!is_array($options)) { throw new \InvalidArgumentException(); @@ -447,19 +523,22 @@ public function list_purchases($options) $options['api_secret'] = $this->api_secret; - $this->create_log(sprintf("GET list purchases: %s, %s", $request, json_encode($options))); + $this->create_log(sprintf('GET list purchases: %s, %s', $request, json_encode($options))); return $this->make_request($request, 'GET', $options); } + /** * Creates a purchase. * - * @param array $options + * @param array $options Purchase data. + * + * @throws \InvalidArgumentException If the provided arguments are not of the expected type. * * @return false|object */ - public function create_purchase($options) + public function create_purchase(array $options) { if (!is_array($options)) { throw new \InvalidArgumentException(); @@ -469,11 +548,12 @@ public function create_purchase($options) $options['api_secret'] = $this->api_secret; - $this->create_log(sprintf("POST create purchase: %s, %s", $request, json_encode($options))); + $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. * @@ -481,10 +561,13 @@ public function create_purchase($options) * Forms and Landing Pages should be embedded using the supplied JS embed script in * the API response when using get_resources(). * - * @param string $url URL of API action. + * @param string $url URL of HTML page. + * + * @throws \InvalidArgumentException If the provided arguments are not of the expected type. + * * @return false|string */ - public function get_resource($url) + public function get_resource(string $url) { if (!is_string($url) || !filter_var($url, FILTER_VALIDATE_URL)) { throw new \InvalidArgumentException(); @@ -492,18 +575,20 @@ public function get_resource($url) $resource = ''; - $this->create_log(sprintf("Getting resource %s", $url)); + $this->create_log(sprintf('Getting resource %s', $url)); // If the resource was already fetched, return the cached version now. - if (isset($this->markup[ $url ])) { - $this->create_log("Resource already set"); - return $this->markup[ $url ]; + if (isset($this->markup[$url])) { + $this->create_log('Resource already set'); + return $this->markup[$url]; } - // Fetch the resource - $request = new Request('GET', $url, array( - 'Accept-Encoding' => 'gzip', - )); + // Fetch the resource. + $request = new Request( + 'GET', + $url, + ['Accept-Encoding' => 'gzip'] + ); $response = $this->client->send($request); // Fetch HTML. @@ -543,21 +628,24 @@ public function get_resource($url) $resource = $this->strip_html_head_body_tags($html->saveHTML()); // Cache and return. - $this->markup[ $url ] = $resource; + $this->markup[$url] = $resource; return $resource; } + /** * Converts any relative URls to absolute, fully qualified HTTP(s) URLs for the given * DOM Elements. * - * @since 1.0.0 + * @param DOMNodeList $elements Elements. + * @param string $attribute HTML Attribute. + * @param string $url Absolute URL to prepend to relative URLs. + * + * @since 1.0.0 * - * @param DOMNodeList $elements Elements. - * @param string $attribute HTML Attribute. - * @param string $url Absolute URL to prepend to relative URLs. + * @return void */ - private function convert_relative_to_absolute_urls($elements, $attribute, $url) + private function convert_relative_to_absolute_urls(DOMNodeList $elements, string $attribute, string $url) { // Anchor hrefs. foreach ($elements as $element) { @@ -582,16 +670,18 @@ private function convert_relative_to_absolute_urls($elements, $attribute, $url) } } + /** * Strips , and opening and closing tags from the given markup, * as well as the Content-Type meta tag we might have added in get_html(). * - * @since 1.0.0 + * @param string $markup HTML Markup. + * + * @since 1.0.0 * - * @param string $markup HTML Markup. - * @return string HTML Markup - * */ - private function strip_html_head_body_tags($markup) + * @return string HTML Markup + */ + private function strip_html_head_body_tags(string $markup) { $markup = str_replace('', '', $markup); $markup = str_replace('', '', $markup); @@ -604,14 +694,19 @@ private function strip_html_head_body_tags($markup) return $markup; } + /** - * @param $endpoint string, endpoint for request - * @param $method string, POST, GET, PUT, PATCH, DELETE - * @param array $args array, additional arguments for request + * Performs an API request using Guzzle. + * + * @param string $endpoint API Endpoint. + * @param string $method Request method (POST, GET, PUT, PATCH, DELETE). + * @param array $args Request arguments. + * + * @throws \InvalidArgumentException If the provided arguments are not of the expected type. * * @return false|mixed */ - public function make_request($endpoint, $method, $args = array()) + public function make_request(string $endpoint, string $method, array $args = []) { if (!is_string($endpoint) || !is_string($method) || !is_array($args)) { throw new \InvalidArgumentException(); @@ -619,65 +714,73 @@ public function make_request($endpoint, $method, $args = array()) $url = $this->api_url_base . $endpoint; - $this->create_log(sprintf("Making request on %s.", $url)); + $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)); + $this->create_log(sprintf('%s, Request body: %s', $method, $request_body)); - if ($method === "GET") { + 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); + $request = new Request( + $method, + $url, + [ + 'Content-Type' => 'application/json', + 'Content-Length' => strlen($request_body), + ], + $request_body + ); } - $response = $this->client->send($request, [ - 'exceptions' => 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)); + // 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."); + $this->create_log('Finish request successfully.'); return $response_body; } - $this->create_log("Failed to finish request."); + $this->create_log('Failed to finish request.'); return false; } + /** * Looks for subscriber with email in array * - * @param $email_address - * @param $subscribers + * @param string $email_address Email Address. + * @param array $subscribers Subscribers. * - * @return false|int false if not found, else subscriber object + * @return false|integer false if not found, else subscriber object */ - private function check_if_subscriber_in_array($email_address, $subscribers) + private function check_if_subscriber_in_array(string $email_address, array $subscribers) { foreach ($subscribers as $subscriber) { if ($subscriber->email_address === $email_address) { - $this->create_log("Subscriber found!"); + $this->create_log('Subscriber found!'); return $subscriber->id; } } - $this->create_log("Subscriber not found on current page."); + $this->create_log('Subscriber not found on current page.'); return false; } } From 2664982cb3a541b3a031e02d040fd4f418db9770 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Fri, 3 Mar 2023 18:52:35 +0000 Subject: [PATCH 4/4] Fix TypeError on type hinting DOMNodeList --- src/ConvertKit_API.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ConvertKit_API.php b/src/ConvertKit_API.php index cd78078..a816750 100644 --- a/src/ConvertKit_API.php +++ b/src/ConvertKit_API.php @@ -637,15 +637,15 @@ public function get_resource(string $url) * Converts any relative URls to absolute, fully qualified HTTP(s) URLs for the given * DOM Elements. * - * @param DOMNodeList $elements Elements. - * @param string $attribute HTML Attribute. - * @param string $url Absolute URL to prepend to relative URLs. + * @param \DOMNodeList $elements Elements. + * @param string $attribute HTML Attribute. + * @param string $url Absolute URL to prepend to relative URLs. * * @since 1.0.0 * * @return void */ - private function convert_relative_to_absolute_urls(DOMNodeList $elements, string $attribute, string $url) + private function convert_relative_to_absolute_urls(\DOMNodeList $elements, string $attribute, string $url) { // Anchor hrefs. foreach ($elements as $element) {