diff --git a/.env.dist.testing b/.env.dist.testing index f69b10f..f7cfb50 100644 --- a/.env.dist.testing +++ b/.env.dist.testing @@ -6,6 +6,7 @@ CONVERTKIT_API_LEGACY_FORM_URL="https://app.convertkit.com/landing_pages/470099" CONVERTKIT_API_LANDING_PAGE_URL="https://cheerful-architect-3237.ck.page/cc5eb21744" CONVERTKIT_API_LEGACY_LANDING_PAGE_URL="https://app.convertkit.com/landing_pages/470103" CONVERTKIT_API_SEQUENCE_ID="1030824" +CONVERTKIT_API_SEQUENCE_EMAIL_ID="4533458" CONVERTKIT_API_TAG_NAME="wordpress" CONVERTKIT_API_TAG_ID="2744672" CONVERTKIT_API_TAG_NAME_2="gravityforms-tag-1" diff --git a/.env.example b/.env.example index a8ce412..7cc34b4 100644 --- a/.env.example +++ b/.env.example @@ -18,6 +18,7 @@ CONVERTKIT_API_LEGACY_FORM_URL="https://app.convertkit.com/landing_pages/470099" CONVERTKIT_API_LANDING_PAGE_URL="https://cheerful-architect-3237.ck.page/cc5eb21744" CONVERTKIT_API_LEGACY_LANDING_PAGE_URL="https://app.convertkit.com/landing_pages/470103" CONVERTKIT_API_SEQUENCE_ID="1030824" +CONVERTKIT_API_SEQUENCE_EMAIL_ID="4533458" CONVERTKIT_API_TAG_NAME="wordpress" CONVERTKIT_API_TAG_ID="2744672" CONVERTKIT_API_TAG_NAME_2="gravityforms-tag-1" diff --git a/src/ConvertKit_API_Traits.php b/src/ConvertKit_API_Traits.php index b629c56..ef5982f 100644 --- a/src/ConvertKit_API_Traits.php +++ b/src/ConvertKit_API_Traits.php @@ -399,25 +399,34 @@ public function get_form_subscriptions( /** * List sequences * - * @param boolean $include_total_count To include the total count of records in the response, use true. - * @param string $after_cursor Return results after the given pagination cursor. - * @param string $before_cursor Return results before the given pagination cursor. - * @param integer $per_page Number of results to return. + * @param array $include Additional fields to include: stats. + * @param boolean $include_total_count To include the total count of records in the response, use true. + * @param string $after_cursor Return results after the given pagination cursor. + * @param string $before_cursor Return results before the given pagination cursor. + * @param integer $per_page Number of results to return. * * @see https://developers.kit.com/api-reference/sequences/list-sequences * * @return false|mixed */ public function get_sequences( + array $include = [], bool $include_total_count = false, string $after_cursor = '', string $before_cursor = '', int $per_page = 100 ) { + // Build parameters. + $options = []; + + if (!empty($include)) { + $options['include'] = implode(',', $include); + } + return $this->get( 'sequences', $this->build_total_count_and_pagination_params( - [], + $options, $include_total_count, $after_cursor, $before_cursor, @@ -490,15 +499,25 @@ public function create_sequence( /** * Get a sequence. * - * @param integer $id Sequence ID. + * @param integer $id Sequence ID. + * @param array $include Additional fields to include: stats. * * @see https://developers.kit.com/api-reference/sequences/get-a-sequence * * @return mixed|object */ - public function get_sequence(int $id) - { - return $this->get(sprintf('sequences/%s', $id)); + public function get_sequence( + int $id, + array $include = [] + ) { + // Build parameters. + $options = []; + + if (!empty($include)) { + $options['include'] = implode(',', $include); + } + + return $this->get(sprintf('sequences/%s', $id), $options); } /** @@ -676,11 +695,12 @@ public function get_sequence_subscriptions( /** * List sequence emails * - * @param integer $sequence_id Sequence ID. - * @param boolean $include_total_count To include the total count of records in the response, use true. - * @param string $after_cursor Return results after the given pagination cursor. - * @param string $before_cursor Return results before the given pagination cursor. - * @param integer $per_page Number of results to return. + * @param integer $sequence_id Sequence ID. + * @param array $include Additional fields to include: stats. + * @param boolean $include_total_count To include the total count of records in the response, use true. + * @param string $after_cursor Return results after the given pagination cursor. + * @param string $before_cursor Return results before the given pagination cursor. + * @param integer $per_page Number of results to return. * * @see https://developers.kit.com/api-reference/sequence-emails/list-sequence-emails * @@ -688,15 +708,23 @@ public function get_sequence_subscriptions( */ public function get_sequence_emails( int $sequence_id, + array $include = [], bool $include_total_count = false, string $after_cursor = '', string $before_cursor = '', int $per_page = 100 ) { + // Build parameters. + $options = []; + + if (!empty($include)) { + $options['include'] = implode(',', $include); + } + return $this->get( sprintf('sequences/%s/emails', $sequence_id), $this->build_total_count_and_pagination_params( - [], + $options, $include_total_count, $after_cursor, $before_cursor, @@ -766,16 +794,27 @@ public function create_sequence_email( /** * Get a sequence email. * - * @param integer $sequence_id Sequence ID. - * @param integer $email_id Email ID. + * @param integer $sequence_id Sequence ID. + * @param integer $email_id Email ID. + * @param array $include Additional fields to include: stats. * * @see https://developers.kit.com/api-reference/sequence-emails/get-a-sequence-email * * @return mixed|object */ - public function get_sequence_email(int $sequence_id, int $email_id) - { - return $this->get(sprintf('sequences/%s/emails/%s', $sequence_id, $email_id)); + public function get_sequence_email( + int $sequence_id, + int $email_id, + array $include = [] + ) { + // Build parameters. + $options = []; + + if (!empty($include)) { + $options['include'] = implode(',', $include); + } + + return $this->get(sprintf('sequences/%s/emails/%s', $sequence_id, $email_id), $options); } /** diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index e6ed190..5122610 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -1026,6 +1026,28 @@ public function testGetSequences() $this->assertArrayHasKey('created_at', $sequence); } + /** + * Test that get_sequences() returns the expected data + * when the include parameter is used. + * + * @since 2.6.0 + * + * @return void + */ + public function testGetSequencesWithIncludeParam() + { + $result = $this->api->get_sequences( + include: ['stats'] + ); + + // Assert sequences and pagination exist. + $this->assertDataExists($result, 'sequences'); + $this->assertPaginationExists($result); + + // Assert fields are included. + $this->assertArrayHasKey('stats', get_object_vars($result->sequences[0])); + } + /** * Test that get_sequences() returns the expected data * when the total count is included. @@ -1193,17 +1215,24 @@ public function testGetSequence() } /** - * Test that get_sequence() throws a ClientException when an invalid - * sequence ID is specified. + * Test that get_sequence() returns the expected data. * - * @since 2.5.0 + * @since 2.6.0 * * @return void */ - public function testGetSequenceWithInvalidSequenceID() + public function testGetSequenceWithIncludeParam() { - $this->expectException(ClientException::class); - $this->api->get_sequence(12345); + $result = $this->api->get_sequence( + (int) $_ENV['CONVERTKIT_API_SEQUENCE_ID'], + include: ['stats'] + ); + $this->assertInstanceOf('stdClass', $result); + $this->assertArrayHasKey('sequence', get_object_vars($result)); + $this->assertArrayHasKey('id', get_object_vars($result->sequence)); + + // Assert stats are included. + $this->assertArrayHasKey('stats', get_object_vars($result->sequence)); } /** @@ -1679,6 +1708,28 @@ public function testGetSequenceEmails() $this->assertArrayHasKey('send_days', $email); } + /** + * Test that get_sequence_emails() returns the expected data + * when the include parameter is used. + * + * @since 2.6.0 + * + * @return void + */ + public function testGetSequenceEmailsWithIncludeParam() + { + $result = $this->api->get_sequence_emails( + sequence_id: (int) $_ENV['CONVERTKIT_API_SEQUENCE_ID'], + include: ['stats'] + ); + $this->assertInstanceOf('stdClass', $result); + $this->assertArrayHasKey('emails', get_object_vars($result)); + $this->assertArrayHasKey('id', get_object_vars($result->emails[0])); + + // Assert stats are included. + $this->assertArrayHasKey('stats', get_object_vars($result->emails[0])); + } + /** * Test that get_sequence_emails() returns the expected data * when the total count is included. @@ -1823,7 +1874,8 @@ public function testCreateGetUpdateAndDeleteSequenceEmail() // Get the sequence email. $result = $this->api->get_sequence_email( sequence_id: (int) $_ENV['CONVERTKIT_API_SEQUENCE_ID'], - email_id: $sequenceEmailID + email_id: $sequenceEmailID, + include: ['stats'] ); // Update the existing sequence email. @@ -1859,6 +1911,45 @@ public function testCreateGetUpdateAndDeleteSequenceEmail() $this->assertEquals(204, $this->api->getResponseInterface()->getStatusCode()); } + /** + * Test that get_sequence_email() returns the expected data. + * + * @since 2.6.0 + * + * @return void + */ + public function testGetSequenceEmail() + { + $result = $this->api->get_sequence_email( + sequence_id: (int) $_ENV['CONVERTKIT_API_SEQUENCE_ID'], + email_id: (int) $_ENV['CONVERTKIT_API_SEQUENCE_EMAIL_ID'] + ); + $this->assertInstanceOf('stdClass', $result); + $this->assertArrayHasKey('id', get_object_vars($result->email)); + } + + /** + * Test that get_sequence_email() returns the expected data + * when the include parameter is used. + * + * @since 2.6.0 + * + * @return void + */ + public function testGetSequenceEmailWithIncludeParam() + { + $result = $this->api->get_sequence_email( + sequence_id: (int) $_ENV['CONVERTKIT_API_SEQUENCE_ID'], + email_id: (int) $_ENV['CONVERTKIT_API_SEQUENCE_EMAIL_ID'], + include: ['stats'] + ); + $this->assertInstanceOf('stdClass', $result); + $this->assertArrayHasKey('id', get_object_vars($result->email)); + + // Assert stats are included. + $this->assertArrayHasKey('stats', get_object_vars($result->email)); + } + /** * Test that get_sequence_email() throws a ClientException when an invalid * sequence ID is specified.