Skip to content

Commit 02de911

Browse files
committed
Merge remote-tracking branch 'origin/v4-api-oauth' into v4-api-forms-pagination
2 parents 87e871a + dba401b commit 02de911

File tree

2 files changed

+377
-147
lines changed

2 files changed

+377
-147
lines changed

src/ConvertKit_API.php

+151-80
Original file line numberDiff line numberDiff line change
@@ -1213,39 +1213,59 @@ public function get_subscriber_tags(
12131213
}
12141214

12151215
/**
1216-
* Gets a list of broadcasts.
1216+
* List broadcasts.
12171217
*
1218-
* @see https://developers.convertkit.com/#list-broadcasts
1218+
* @param boolean $include_total_count To include the total count of records in the response, use true.
1219+
* @param string $after_cursor Return results after the given pagination cursor.
1220+
* @param string $before_cursor Return results before the given pagination cursor.
1221+
* @param integer $per_page Number of results to return.
12191222
*
1220-
* @return false|array<int,\stdClass>
1223+
* @see https://developers.convertkit.com/v4.html#list-broadcasts
1224+
*
1225+
* @return false|mixed
12211226
*/
1222-
public function get_broadcasts()
1223-
{
1224-
return $this->get('broadcasts');
1227+
public function get_broadcasts(
1228+
bool $include_total_count = false,
1229+
string $after_cursor = '',
1230+
string $before_cursor = '',
1231+
int $per_page = 100
1232+
) {
1233+
// Send request.
1234+
return $this->get(
1235+
endpoint: 'broadcasts',
1236+
args: $this->build_total_count_and_pagination_params(
1237+
include_total_count: $include_total_count,
1238+
after_cursor: $after_cursor,
1239+
before_cursor: $before_cursor,
1240+
per_page: $per_page
1241+
)
1242+
);
12251243
}
12261244

12271245
/**
12281246
* Creates a broadcast.
12291247
*
1230-
* @param string $subject The broadcast email's subject.
1231-
* @param string $content The broadcast's email HTML content.
1232-
* @param string $description An internal description of this broadcast.
1233-
* @param boolean $public Specifies whether or not this is a public post.
1234-
* @param \DateTime $published_at Specifies the time that this post was published (applicable
1235-
* only to public posts).
1236-
* @param \DateTime $send_at Time that this broadcast should be sent; leave blank to create
1237-
* a draft broadcast. If set to a future time, this is the time that
1238-
* the broadcast will be scheduled to send.
1239-
* @param string $email_address Sending email address; leave blank to use your account's
1240-
* default sending email address.
1241-
* @param string $email_layout_template Name of the email template to use; leave blank to use your
1242-
* account's default email template.
1243-
* @param string $thumbnail_alt Specify the ALT attribute of the public thumbnail image
1244-
* (applicable only to public posts).
1245-
* @param string $thumbnail_url Specify the URL of the thumbnail image to accompany the broadcast
1246-
* post (applicable only to public posts).
1247-
*
1248-
* @see https://developers.convertkit.com/#create-a-broadcast
1248+
* @param string $subject The broadcast email's subject.
1249+
* @param string $content The broadcast's email HTML content.
1250+
* @param string $description An internal description of this broadcast.
1251+
* @param boolean $public Specifies whether or not this is a public post.
1252+
* @param \DateTime $published_at Specifies the time that this post was published (applicable
1253+
* only to public posts).
1254+
* @param \DateTime $send_at Time that this broadcast should be sent; leave blank to create
1255+
* a draft broadcast. If set to a future time, this is the time that
1256+
* the broadcast will be scheduled to send.
1257+
* @param string $email_address Sending email address; leave blank to use your account's
1258+
* default sending email address.
1259+
* @param string $email_template_id ID of the email template to use; leave blank to use your
1260+
* account's default email template.
1261+
* @param string $thumbnail_alt Specify the ALT attribute of the public thumbnail image
1262+
* (applicable only to public posts).
1263+
* @param string $thumbnail_url Specify the URL of the thumbnail image to accompany the broadcast
1264+
* post (applicable only to public posts).
1265+
* @param string $preview_text Specify the preview text of the email.
1266+
* @param array<string,string> $subscriber_filter Filter subscriber(s) to send the email to.
1267+
*
1268+
* @see https://developers.convertkit.com/v4.html#create-a-broadcast
12491269
*
12501270
* @return false|object
12511271
*/
@@ -1257,22 +1277,28 @@ public function create_broadcast(
12571277
\DateTime $published_at = null,
12581278
\DateTime $send_at = null,
12591279
string $email_address = '',
1260-
string $email_layout_template = '',
1280+
string $email_template_id = '',
12611281
string $thumbnail_alt = '',
1262-
string $thumbnail_url = ''
1282+
string $thumbnail_url = '',
1283+
string $preview_text = '',
1284+
array $subscriber_filter = []
12631285
) {
12641286
$options = [
1265-
'content' => $content,
1266-
'description' => $description,
1267-
'email_address' => $email_address,
1268-
'email_layout_template' => $email_layout_template,
1269-
'public' => $public,
1270-
'published_at' => (!is_null($published_at) ? $published_at->format('Y-m-d H:i:s') : ''),
1271-
'send_at' => (!is_null($send_at) ? $send_at->format('Y-m-d H:i:s') : ''),
1272-
'subject' => $subject,
1273-
'thumbnail_alt' => $thumbnail_alt,
1274-
'thumbnail_url' => $thumbnail_url,
1287+
'email_template_id' => $email_template_id,
1288+
'email_address' => $email_address,
1289+
'content' => $content,
1290+
'description' => $description,
1291+
'public' => $public,
1292+
'published_at' => (!is_null($published_at) ? $published_at->format('Y-m-d H:i:s') : ''),
1293+
'send_at' => (!is_null($send_at) ? $send_at->format('Y-m-d H:i:s') : ''),
1294+
'thumbnail_alt' => $thumbnail_alt,
1295+
'thumbnail_url' => $thumbnail_url,
1296+
'preview_text' => $preview_text,
1297+
'subject' => $subject,
12751298
];
1299+
if (count($subscriber_filter)) {
1300+
$options['subscriber_filter'] = $subscriber_filter;
1301+
}
12761302

12771303
// Iterate through options, removing blank entries.
12781304
foreach ($options as $key => $value) {
@@ -1287,15 +1313,18 @@ public function create_broadcast(
12871313
}
12881314

12891315
// Send request.
1290-
return $this->post('broadcasts', $options);
1316+
return $this->post(
1317+
endpoint: 'broadcasts',
1318+
args: $options
1319+
);
12911320
}
12921321

12931322
/**
12941323
* Retrieve a specific broadcast.
12951324
*
12961325
* @param integer $id Broadcast ID.
12971326
*
1298-
* @see https://developers.convertkit.com/#retrieve-a-specific-broadcast
1327+
* @see https://developers.convertkit.com/v4.html#get-a-broadcast
12991328
*
13001329
* @return false|object
13011330
*/
@@ -1310,7 +1339,7 @@ public function get_broadcast(int $id)
13101339
*
13111340
* @param integer $id Broadcast ID.
13121341
*
1313-
* @see https://developers.convertkit.com/#retrieve-a-specific-broadcast
1342+
* @see https://developers.convertkit.com/v4.html#get-stats
13141343
*
13151344
* @return false|object
13161345
*/
@@ -1322,24 +1351,26 @@ public function get_broadcast_stats(int $id)
13221351
/**
13231352
* Updates a broadcast.
13241353
*
1325-
* @param integer $id Broadcast ID.
1326-
* @param string $subject The broadcast email's subject.
1327-
* @param string $content The broadcast's email HTML content.
1328-
* @param string $description An internal description of this broadcast.
1329-
* @param boolean $public Specifies whether or not this is a public post.
1330-
* @param \DateTime $published_at Specifies the time that this post was published (applicable
1331-
* only to public posts).
1332-
* @param \DateTime $send_at Time that this broadcast should be sent; leave blank to create
1333-
* a draft broadcast. If set to a future time, this is the time that
1334-
* the broadcast will be scheduled to send.
1335-
* @param string $email_address Sending email address; leave blank to use your account's
1336-
* default sending email address.
1337-
* @param string $email_layout_template Name of the email template to use; leave blank to use your
1338-
* account's default email template.
1339-
* @param string $thumbnail_alt Specify the ALT attribute of the public thumbnail image
1340-
* (applicable only to public posts).
1341-
* @param string $thumbnail_url Specify the URL of the thumbnail image to accompany the broadcast
1342-
* post (applicable only to public posts).
1354+
* @param integer $id Broadcast ID.
1355+
* @param string $subject The broadcast email's subject.
1356+
* @param string $content The broadcast's email HTML content.
1357+
* @param string $description An internal description of this broadcast.
1358+
* @param boolean $public Specifies whether or not this is a public post.
1359+
* @param \DateTime $published_at Specifies the time that this post was published (applicable
1360+
* only to public posts).
1361+
* @param \DateTime $send_at Time that this broadcast should be sent; leave blank to create
1362+
* a draft broadcast. If set to a future time, this is the time that
1363+
* the broadcast will be scheduled to send.
1364+
* @param string $email_address Sending email address; leave blank to use your account's
1365+
* default sending email address.
1366+
* @param string $email_template_id ID of the email template to use; leave blank to use your
1367+
* account's default email template.
1368+
* @param string $thumbnail_alt Specify the ALT attribute of the public thumbnail image
1369+
* (applicable only to public posts).
1370+
* @param string $thumbnail_url Specify the URL of the thumbnail image to accompany the broadcast
1371+
* post (applicable only to public posts).
1372+
* @param string $preview_text Specify the preview text of the email.
1373+
* @param array<string,string> $subscriber_filter Filter subscriber(s) to send the email to.
13431374
*
13441375
* @see https://developers.convertkit.com/#create-a-broadcast
13451376
*
@@ -1354,22 +1385,28 @@ public function update_broadcast(
13541385
\DateTime $published_at = null,
13551386
\DateTime $send_at = null,
13561387
string $email_address = '',
1357-
string $email_layout_template = '',
1388+
string $email_template_id = '',
13581389
string $thumbnail_alt = '',
1359-
string $thumbnail_url = ''
1390+
string $thumbnail_url = '',
1391+
string $preview_text = '',
1392+
array $subscriber_filter = []
13601393
) {
13611394
$options = [
1362-
'content' => $content,
1363-
'description' => $description,
1364-
'email_address' => $email_address,
1365-
'email_layout_template' => $email_layout_template,
1366-
'public' => $public,
1367-
'published_at' => (!is_null($published_at) ? $published_at->format('Y-m-d H:i:s') : ''),
1368-
'send_at' => (!is_null($send_at) ? $send_at->format('Y-m-d H:i:s') : ''),
1369-
'subject' => $subject,
1370-
'thumbnail_alt' => $thumbnail_alt,
1371-
'thumbnail_url' => $thumbnail_url,
1395+
'email_template_id' => $email_template_id,
1396+
'email_address' => $email_address,
1397+
'content' => $content,
1398+
'description' => $description,
1399+
'public' => $public,
1400+
'published_at' => (!is_null($published_at) ? $published_at->format('Y-m-d H:i:s') : ''),
1401+
'send_at' => (!is_null($send_at) ? $send_at->format('Y-m-d H:i:s') : ''),
1402+
'thumbnail_alt' => $thumbnail_alt,
1403+
'thumbnail_url' => $thumbnail_url,
1404+
'preview_text' => $preview_text,
1405+
'subject' => $subject,
13721406
];
1407+
if (count($subscriber_filter)) {
1408+
$options['subscriber_filter'] = $subscriber_filter;
1409+
}
13731410

13741411
// Iterate through options, removing blank entries.
13751412
foreach ($options as $key => $value) {
@@ -1385,8 +1422,8 @@ public function update_broadcast(
13851422

13861423
// Send request.
13871424
return $this->put(
1388-
sprintf('broadcasts/%s', $id),
1389-
$options
1425+
endpoint: sprintf('broadcasts/%s', $id),
1426+
args: $options
13901427
);
13911428
}
13921429

@@ -1397,15 +1434,47 @@ public function update_broadcast(
13971434
*
13981435
* @since 1.0.0
13991436
*
1400-
* @see https://developers.convertkit.com/#destroy-webhook
1437+
* @see https://developers.convertkit.com/v4.html#delete-a-broadcast
14011438
*
14021439
* @return false|object
14031440
*/
1404-
public function destroy_broadcast(int $id)
1441+
public function delete_broadcast(int $id)
14051442
{
14061443
return $this->delete(sprintf('broadcasts/%s', $id));
14071444
}
14081445

1446+
/**
1447+
* List webhooks.
1448+
*
1449+
* @param boolean $include_total_count To include the total count of records in the response, use true.
1450+
* @param string $after_cursor Return results after the given pagination cursor.
1451+
* @param string $before_cursor Return results before the given pagination cursor.
1452+
* @param integer $per_page Number of results to return.
1453+
*
1454+
* @since 2.0.0
1455+
*
1456+
* @see https://developers.convertkit.com/v4.html#list-webhooks
1457+
*
1458+
* @return false|mixed
1459+
*/
1460+
public function get_webhooks(
1461+
bool $include_total_count = false,
1462+
string $after_cursor = '',
1463+
string $before_cursor = '',
1464+
int $per_page = 100
1465+
) {
1466+
// Send request.
1467+
return $this->get(
1468+
endpoint: 'webhooks',
1469+
args: $this->build_total_count_and_pagination_params(
1470+
include_total_count: $include_total_count,
1471+
after_cursor: $after_cursor,
1472+
before_cursor: $before_cursor,
1473+
per_page: $per_page
1474+
)
1475+
);
1476+
}
1477+
14091478
/**
14101479
* Creates a webhook that will be called based on the chosen event types.
14111480
*
@@ -1415,7 +1484,7 @@ public function destroy_broadcast(int $id)
14151484
*
14161485
* @since 1.0.0
14171486
*
1418-
* @see https://developers.convertkit.com/#create-a-webhook
1487+
* @see https://developers.convertkit.com/v4.html#create-a-webhook
14191488
*
14201489
* @throws \InvalidArgumentException If the event is not supported.
14211490
*
@@ -1427,6 +1496,8 @@ public function create_webhook(string $url, string $event, string $parameter = '
14271496
switch ($event) {
14281497
case 'subscriber.subscriber_activate':
14291498
case 'subscriber.subscriber_unsubscribe':
1499+
case 'subscriber.subscriber_bounce':
1500+
case 'subscriber.subscriber_complain':
14301501
case 'purchase.purchase_create':
14311502
$eventData = ['name' => $event];
14321503
break;
@@ -1474,7 +1545,7 @@ public function create_webhook(string $url, string $event, string $parameter = '
14741545

14751546
// Send request.
14761547
return $this->post(
1477-
'automations/hooks',
1548+
'webhooks',
14781549
[
14791550
'target_url' => $url,
14801551
'event' => $eventData,
@@ -1485,17 +1556,17 @@ public function create_webhook(string $url, string $event, string $parameter = '
14851556
/**
14861557
* Deletes an existing webhook.
14871558
*
1488-
* @param integer $rule_id Rule ID.
1559+
* @param integer $id Webhook ID.
14891560
*
14901561
* @since 1.0.0
14911562
*
1492-
* @see https://developers.convertkit.com/#destroy-webhook
1563+
* @see https://developers.convertkit.com/v4.html#delete-a-webhook
14931564
*
14941565
* @return false|object
14951566
*/
1496-
public function destroy_webhook(int $rule_id)
1567+
public function delete_webhook(int $id)
14971568
{
1498-
return $this->delete(sprintf('automations/hooks/%s', $rule_id));
1569+
return $this->delete(sprintf('webhooks/%s', $id));
14991570
}
15001571

15011572
/**

0 commit comments

Comments
 (0)