Skip to content

Commit bfe2ec9

Browse files
authored
Merge pull request #86 from ConvertKit/v4-api-email-templates
v4 API: Email Templates
2 parents 7bdbfb9 + 9ce7da4 commit bfe2ec9

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

src/ConvertKit_API.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,44 @@ public function get_tag_subscriptions(
839839
);
840840
}
841841

842+
/**
843+
* List email templates.
844+
*
845+
* @param boolean $include_total_count To include the total count of records in the response, use true.
846+
* @param string $after_cursor Return results after the given pagination cursor.
847+
* @param string $before_cursor Return results before the given pagination cursor.
848+
* @param integer $per_page Number of results to return.
849+
*
850+
* @since 2.0.0
851+
*
852+
* @see https://developers.convertkit.com/v4.html#convertkit-api-email-templates
853+
*
854+
* @return false|mixed
855+
*/
856+
public function get_email_templates(
857+
bool $include_total_count = false,
858+
string $after_cursor = '',
859+
string $before_cursor = '',
860+
int $per_page = 100
861+
) {
862+
// Build parameters.
863+
$options = ['include_total_count' => $include_total_count];
864+
865+
// Build pagination parameters.
866+
$options = $this->build_pagination_params(
867+
params: $options,
868+
after_cursor: $after_cursor,
869+
before_cursor: $before_cursor,
870+
per_page: $per_page
871+
);
872+
873+
// Send request.
874+
return $this->get(
875+
endpoint: 'email_templates',
876+
args: $options
877+
);
878+
}
879+
842880
/**
843881
* Gets a resource index
844882
* Possible resources: forms, landing_pages, subscription_forms, tags

tests/ConvertKitAPITest.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3281,6 +3281,105 @@ public function testGetSubscriberTagsPagination()
32813281
$this->assertCount(1, $result->tags);
32823282
}
32833283

3284+
/**
3285+
* Test that get_email_templates() returns the expected data.
3286+
*
3287+
* @since 2.0.0
3288+
*
3289+
* @return void
3290+
*/
3291+
public function testGetEmailTemplates()
3292+
{
3293+
$result = $this->api->get_email_templates();
3294+
3295+
// Assert email templates and pagination exist.
3296+
$this->assertDataExists($result, 'email_templates');
3297+
$this->assertPaginationExists($result);
3298+
}
3299+
3300+
/**
3301+
* Test that get_email_templates() returns the expected data
3302+
* when the total count is included.
3303+
*
3304+
* @since 1.0.0
3305+
*
3306+
* @return void
3307+
*/
3308+
public function testGetEmailTemplatesWithTotalCount()
3309+
{
3310+
$result = $this->api->get_email_templates(
3311+
include_total_count: true
3312+
);
3313+
3314+
// Assert email templates and pagination exist.
3315+
$this->assertDataExists($result, 'email_templates');
3316+
$this->assertPaginationExists($result);
3317+
3318+
// Assert total count is included.
3319+
$this->assertArrayHasKey('total_count', get_object_vars($result->pagination));
3320+
$this->assertGreaterThan(0, $result->pagination->total_count);
3321+
}
3322+
3323+
/**
3324+
* Test that get_email_templates() returns the expected data
3325+
* when pagination parameters and per_page limits are specified.
3326+
*
3327+
* @since 2.0.0
3328+
*
3329+
* @return void
3330+
*/
3331+
public function testGetEmailTemplatesPagination()
3332+
{
3333+
$result = $this->api->get_email_templates(
3334+
per_page: 1
3335+
);
3336+
3337+
// Assert email templates and pagination exist.
3338+
$this->assertDataExists($result, 'email_templates');
3339+
$this->assertPaginationExists($result);
3340+
3341+
// Assert a single email template was returned.
3342+
$this->assertCount(1, $result->email_templates);
3343+
3344+
// Assert has_previous_page and has_next_page are correct.
3345+
$this->assertFalse($result->pagination->has_previous_page);
3346+
$this->assertTrue($result->pagination->has_next_page);
3347+
3348+
// Use pagination to fetch next page.
3349+
$result = $this->api->get_email_templates(
3350+
per_page: 1,
3351+
after_cursor: $result->pagination->end_cursor
3352+
);
3353+
3354+
// Assert email templates and pagination exist.
3355+
$this->assertDataExists($result, 'email_templates');
3356+
$this->assertPaginationExists($result);
3357+
3358+
// Assert a single email template was returned.
3359+
$this->assertCount(1, $result->email_templates);
3360+
3361+
// Assert has_previous_page and has_next_page are correct.
3362+
$this->assertTrue($result->pagination->has_previous_page);
3363+
$this->assertTrue($result->pagination->has_next_page);
3364+
3365+
// Use pagination to fetch previous page.
3366+
$result = $this->api->get_email_templates(
3367+
per_page: 1,
3368+
before_cursor: $result->pagination->start_cursor
3369+
);
3370+
3371+
// Assert email templates and pagination exist.
3372+
$this->assertDataExists($result, 'email_templates');
3373+
$this->assertPaginationExists($result);
3374+
3375+
// Assert a single email template was returned.
3376+
$this->assertCount(1, $result->email_templates);
3377+
3378+
// Assert has_previous_page and has_next_page are correct.
3379+
$this->assertFalse($result->pagination->has_previous_page);
3380+
$this->assertTrue($result->pagination->has_next_page);
3381+
}
3382+
32843383
/**
32853384
* Test that create_broadcast(), update_broadcast() and destroy_broadcast() works
32863385
* when specifying valid published_at and send_at values.

0 commit comments

Comments
 (0)