From 9ed5696f45197a788c3ed298f1ac4922a0f46ac9 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Mon, 13 Jul 2026 14:44:53 +0800 Subject: [PATCH] Forms & Tags: Add `include` parameter support --- src/ConvertKit_API_Traits.php | 46 ++++++++++++++++++++++++----------- tests/ConvertKitAPITest.php | 44 +++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 14 deletions(-) diff --git a/src/ConvertKit_API_Traits.php b/src/ConvertKit_API_Traits.php index 5810cdb..5cd57e4 100644 --- a/src/ConvertKit_API_Traits.php +++ b/src/ConvertKit_API_Traits.php @@ -158,11 +158,12 @@ public function get_growth_stats(\DateTime|null $starting = null, \DateTime|null /** * List forms. * - * @param string $status Form status (active|archived|trashed|all). - * @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 string $status Form status (active|archived|trashed|all). + * @param array $include Additional fields to include: subscriber_count. + * @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/forms/list-forms * @@ -170,18 +171,26 @@ public function get_growth_stats(\DateTime|null $starting = null, \DateTime|null */ public function get_forms( string $status = 'active', + array $include = [], bool $include_total_count = false, string $after_cursor = '', string $before_cursor = '', int $per_page = 100 ) { + // Build parameters. + $options = [ + 'type' => 'embed', + 'status' => $status, + ]; + + if (!empty($include)) { + $options['include'] = implode(',', $include); + } + return $this->get( 'forms', $this->build_total_count_and_pagination_params( - [ - 'type' => 'embed', - 'status' => $status, - ], + $options, $include_total_count, $after_cursor, $before_cursor, @@ -1004,10 +1013,11 @@ public function update_snippet( /** * List tags. * - * @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: subscriber_count. + * @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/tags/list-tags * @@ -1016,15 +1026,23 @@ public function update_snippet( * @return mixed|array */ public function get_tags( + 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( 'tags', $this->build_total_count_and_pagination_params( - [], + $options, $include_total_count, $after_cursor, $before_cursor, diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index 19846f3..6a3f0e6 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -445,6 +445,28 @@ public function testGetFormsWithArchivedStatus() } } + /** + * Test that get_forms() returns the subscriber count + * when included in the `include` argument. + * + * @since 2.6.0 + * + * @return void + */ + public function testGetFormsWithSubscriberCount() + { + $result = $this->api->get_forms( + include: ['subscriber_count'] + ); + + // Assert forms and pagination exist. + $this->assertDataExists($result, 'forms'); + $this->assertPaginationExists($result); + + // Assert subscriber count is included. + $this->assertArrayHasKey('subscriber_count', get_object_vars($result->forms[0])); + } + /** * Test that get_forms() returns the expected data * when the total count is included. @@ -2220,6 +2242,28 @@ public function testGetTags() $this->assertArrayHasKey('created_at', $tag); } + /** + * Test that get_tags() returns the subscriber count + * when included in the `include` argument. + * + * @since 2.6.0 + * + * @return void + */ + public function testGetTagsWithSubscriberCount() + { + $result = $this->api->get_tags( + include: ['subscriber_count'] + ); + + // Assert forms and pagination exist. + $this->assertDataExists($result, 'tags'); + $this->assertPaginationExists($result); + + // Assert subscriber count is included. + $this->assertArrayHasKey('subscriber_count', get_object_vars($result->tags[0])); + } + /** * Test that get_tags() returns the expected data * when the total count is included.