From 483c0f9224cdac5ad50732cc5db16afcf27ab952 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Sat, 16 May 2026 13:06:41 +0800 Subject: [PATCH 1/3] Add Posts Methods --- src/ConvertKit_API_Traits.php | 51 +++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/ConvertKit_API_Traits.php b/src/ConvertKit_API_Traits.php index b1131f8..1405d22 100644 --- a/src/ConvertKit_API_Traits.php +++ b/src/ConvertKit_API_Traits.php @@ -961,6 +961,57 @@ public function get_email_templates( ); } + /** + * List posts. + * + * @param boolean $include_content To include the content field on each post in the response, use true. + * @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. + * + * @since 2.5.0 + * + * @see https://developers.kit.com/api-reference/posts/list-posts + * + * @return false|mixed + */ + public function get_posts( + bool $include_content = false, + bool $include_total_count = false, + string $after_cursor = '', + string $before_cursor = '', + int $per_page = 100 + ) { + // Send request. + return $this->get( + 'posts', + $this->build_total_count_and_pagination_params( + ['include_content' => $include_content], + $include_total_count, + $after_cursor, + $before_cursor, + $per_page + ) + ); + } + + /** + * Get a post. + * + * @param integer $id Post ID. + * + * @since 2.5.0 + * + * @see https://developers.kit.com/api-reference/posts/get-a-post + * + * @return mixed|object + */ + public function get_post(int $id) + { + return $this->get(sprintf('posts/%s', $id)); + } + /** * List subscribers. * From e20ec54cd0a786a4480703edceb4310d934bbf0b Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Sat, 16 May 2026 13:06:44 +0800 Subject: [PATCH 2/3] Added Tests --- tests/ConvertKitAPITest.php | 125 ++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index 8b0be8d..d683d32 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -4467,6 +4467,131 @@ public function testGetEmailTemplatesPagination() $this->assertTrue($result->pagination->has_next_page); } + /** + * Test that get_posts() returns the expected data. + * + * @since 2.5.0 + * + * @return void + */ + public function testGetPosts() + { + $result = $this->api->get_posts(); + + // Assert posts and pagination exist. + $this->assertDataExists($result, 'posts'); + $this->assertPaginationExists($result); + + // Assert content is not included. + $this->assertArrayNotHasKey('content', get_object_vars($result->posts[0])); + } + + /** + * Test that get_posts() returns the expected data + * when the post content is included. + * + * @since 2.5.0 + * + * @return void + */ + public function testGetPostsWithIncludeContent() + { + $result = $this->api->get_posts( + include_content: true, + per_page: 1 + ); + + // Assert posts and pagination exist. + $this->assertDataExists($result, 'posts'); + $this->assertPaginationExists($result); + + // Assert content is included. + $this->assertArrayHasKey('content', get_object_vars($result->posts[0])); + } + + /** + * Test that get_posts() returns the expected data + * when the total count is included. + * + * @since 2.5.0 + * + * @return void + */ + public function testGetPostsWithTotalCount() + { + $result = $this->api->get_posts( + include_total_count: true + ); + + // Assert posts and pagination exist. + $this->assertDataExists($result, 'posts'); + $this->assertPaginationExists($result); + + // Assert total count is included. + $this->assertArrayHasKey('total_count', get_object_vars($result->pagination)); + $this->assertGreaterThan(0, $result->pagination->total_count); + } + + /** + * Test that get_posts() returns the expected data + * when pagination parameters and per_page limits are specified. + * + * @since 2.5.0 + * + * @return void + */ + public function testGetPostsPagination() + { + $result = $this->api->get_posts( + per_page: 1 + ); + + // Assert posts and pagination exist. + $this->assertDataExists($result, 'posts'); + $this->assertPaginationExists($result); + + // Assert a single post was returned. + $this->assertCount(1, $result->posts); + + // Assert has_previous_page and has_next_page are correct. + $this->assertFalse($result->pagination->has_previous_page); + $this->assertTrue($result->pagination->has_next_page); + + // Use pagination to fetch next page. + $result = $this->api->get_posts( + per_page: 1, + after_cursor: $result->pagination->end_cursor + ); + + // Assert posts and pagination exist. + $this->assertDataExists($result, 'posts'); + $this->assertPaginationExists($result); + + // Assert a single post was returned. + $this->assertCount(1, $result->posts); + + // Assert has_previous_page and has_next_page are correct. + $this->assertTrue($result->pagination->has_previous_page); + $this->assertTrue($result->pagination->has_next_page); + + // Use pagination to fetch previous page. + $result = $this->api->get_posts( + per_page: 1, + before_cursor: $result->pagination->start_cursor + ); + + // Assert posts and pagination exist. + $this->assertDataExists($result, 'posts'); + $this->assertPaginationExists($result); + + // Assert a single post was returned. + $this->assertCount(1, $result->posts); + + // Assert has_previous_page and has_next_page are correct. + $this->assertFalse($result->pagination->has_previous_page); + $this->assertTrue($result->pagination->has_next_page); + } + /** * Test that get_broadcasts() returns the expected data * when a valid sent_after date is specified. From 6f1c0cfc58df1b9aa0b24d6aea0709b9c5dd2279 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Sat, 16 May 2026 13:15:31 +0800 Subject: [PATCH 3/3] Finished tests --- .env.dist.testing | 1 + tests/ConvertKitAPITest.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/.env.dist.testing b/.env.dist.testing index 3acd54e..134fe91 100644 --- a/.env.dist.testing +++ b/.env.dist.testing @@ -13,3 +13,4 @@ CONVERTKIT_API_TAG_ID_2="2907192" CONVERTKIT_API_SUBSCRIBER_EMAIL="optin@n7studios.com" CONVERTKIT_API_SUBSCRIBER_ID="1579118532" CONVERTKIT_API_EMAIL_TEMPLATE_ID="2201089" +CONVERTKIT_API_POST_ID="3175837" diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index d683d32..b909eae 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -4592,6 +4592,34 @@ public function testGetPostsPagination() $this->assertTrue($result->pagination->has_next_page); } + /** + * Test that get_post() returns the expected data. + * + * @since 2.5.0 + * + * @return void + */ + public function testGetPost() + { + $result = $this->api->get_post($_ENV['CONVERTKIT_API_POST_ID']); + $result = get_object_vars($result->post); + $this->assertEquals($result['id'], $_ENV['CONVERTKIT_API_POST_ID']); + } + + /** + * Test that get_post() throws a ClientException when an invalid + * post ID is specified. + * + * @since 2.5.0 + * + * @return void + */ + public function testGetPostWithInvalidPostID() + { + $this->expectException(ClientException::class); + $this->api->get_post(12345); + } + /** * Test that get_broadcasts() returns the expected data * when a valid sent_after date is specified.