Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.dist.testing
Original file line number Diff line number Diff line change
Expand Up @@ -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"
51 changes: 51 additions & 0 deletions src/ConvertKit_API_Traits.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
153 changes: 153 additions & 0 deletions tests/ConvertKitAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4473,6 +4473,159 @@ 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_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.
Expand Down
Loading