@@ -933,6 +933,155 @@ public function get_resources(string $resource)
933
933
return $ _resource ;
934
934
}
935
935
936
+ /**
937
+ * Get subscribers.
938
+ *
939
+ * @param string $subscriber_state Subscriber State (active|bounced|cancelled|complained|inactive).
940
+ * @param string $email_address Search susbcribers by email address. This is an exact match search.
941
+ * @param \DateTime $created_after Filter subscribers who have been created after this date.
942
+ * @param \DateTime $created_before Filter subscribers who have been created before this date.
943
+ * @param \DateTime $updated_after Filter subscribers who have been updated after this date.
944
+ * @param \DateTime $updated_before Filter subscribers who have been updated before this date.
945
+ * @param string $sort_field Sort Field (id|updated_at|cancelled_at).
946
+ * @param string $sort_order Sort Order (asc|desc).
947
+ * @param string $after_cursor Return results after the given pagination cursor.
948
+ * @param string $before_cursor Return results before the given pagination cursor.
949
+ * @param integer $per_page Number of results to return.
950
+ *
951
+ * @since 2.0.0
952
+ *
953
+ * @see https://developers.convertkit.com/v4.html#list-subscribers
954
+ *
955
+ * @return false|mixed
956
+ */
957
+ public function get_subscribers (
958
+ string $ subscriber_state = 'active ' ,
959
+ string $ email_address = '' ,
960
+ \DateTime $ created_after = null ,
961
+ \DateTime $ created_before = null ,
962
+ \DateTime $ updated_after = null ,
963
+ \DateTime $ updated_before = null ,
964
+ string $ sort_field = 'id ' ,
965
+ string $ sort_order = 'desc ' ,
966
+ string $ after_cursor = '' ,
967
+ string $ before_cursor = '' ,
968
+ int $ per_page = 100
969
+ ) {
970
+ // Build parameters.
971
+ $ options = [];
972
+
973
+ if (!empty ($ subscriber_state )) {
974
+ $ options ['status ' ] = $ subscriber_state ;
975
+ }
976
+ if (!empty ($ email_address )) {
977
+ $ options ['email_address ' ] = $ email_address ;
978
+ }
979
+ if (!is_null ($ created_after )) {
980
+ $ options ['created_after ' ] = $ created_after ->format ('Y-m-d ' );
981
+ }
982
+ if (!is_null ($ created_before )) {
983
+ $ options ['created_before ' ] = $ created_before ->format ('Y-m-d ' );
984
+ }
985
+ if (!is_null ($ updated_after )) {
986
+ $ options ['updated_after ' ] = $ updated_after ->format ('Y-m-d ' );
987
+ }
988
+ if (!is_null ($ updated_before )) {
989
+ $ options ['updated_before ' ] = $ updated_before ->format ('Y-m-d ' );
990
+ }
991
+ if (!empty ($ sort_field )) {
992
+ $ options ['sort_field ' ] = $ sort_field ;
993
+ }
994
+ if (!empty ($ sort_order )) {
995
+ $ options ['sort_order ' ] = $ sort_order ;
996
+ }
997
+
998
+ // Build pagination parameters.
999
+ $ options = $ this ->build_pagination_params (
1000
+ params: $ options ,
1001
+ after_cursor: $ after_cursor ,
1002
+ before_cursor: $ before_cursor ,
1003
+ per_page: $ per_page
1004
+ );
1005
+
1006
+ // Send request.
1007
+ return $ this ->get (
1008
+ endpoint: 'subscribers ' ,
1009
+ args: $ options
1010
+ );
1011
+ }
1012
+
1013
+ /**
1014
+ * Create a subscriber.
1015
+ *
1016
+ * Behaves as an upsert. If a subscriber with the provided email address does not exist,
1017
+ * it creates one with the specified first name and state. If a subscriber with the provided
1018
+ * email address already exists, it updates the first name.
1019
+ *
1020
+ * @param string $email_address Email Address.
1021
+ * @param string $first_name First Name.
1022
+ * @param string $subscriber_state Subscriber State (active|bounced|cancelled|complained|inactive).
1023
+ * @param array<string, string> $fields Custom Fields.
1024
+ *
1025
+ * @since 2.0.0
1026
+ *
1027
+ * @see https://developers.convertkit.com/v4.html#create-a-subscriber
1028
+ *
1029
+ * @return mixed
1030
+ */
1031
+ public function create_subscriber (
1032
+ string $ email_address ,
1033
+ string $ first_name = '' ,
1034
+ string $ subscriber_state = '' ,
1035
+ array $ fields = []
1036
+ ) {
1037
+ // Build parameters.
1038
+ $ options = ['email_address ' => $ email_address ];
1039
+
1040
+ if (!empty ($ first_name )) {
1041
+ $ options ['first_name ' ] = $ first_name ;
1042
+ }
1043
+ if (!empty ($ subscriber_state )) {
1044
+ $ options ['state ' ] = $ subscriber_state ;
1045
+ }
1046
+ if (count ($ fields )) {
1047
+ $ options ['fields ' ] = $ fields ;
1048
+ }
1049
+
1050
+ // Send request.
1051
+ return $ this ->post (
1052
+ endpoint: 'subscribers ' ,
1053
+ args: $ options
1054
+ );
1055
+ }
1056
+
1057
+ /**
1058
+ * Create multiple subscribers.
1059
+ *
1060
+ * @param array<int,array<string,string>> $subscribers Subscribers.
1061
+ * @param string $callback_url URL to notify for large batch size when async processing complete.
1062
+ *
1063
+ * @since 2.0.0
1064
+ *
1065
+ * @see https://developers.convertkit.com/v4.html#bulk-create-subscribers
1066
+ *
1067
+ * @return mixed
1068
+ */
1069
+ public function create_subscribers (array $ subscribers , string $ callback_url = '' )
1070
+ {
1071
+ // Build parameters.
1072
+ $ options = ['subscribers ' => $ subscribers ];
1073
+
1074
+ if (!empty ($ callback_url )) {
1075
+ $ options ['callback_url ' ] = $ callback_url ;
1076
+ }
1077
+
1078
+ // Send request.
1079
+ return $ this ->post (
1080
+ endpoint: 'bulk/subscribers ' ,
1081
+ args: $ options
1082
+ );
1083
+ }
1084
+
936
1085
/**
937
1086
* Get the ConvertKit subscriber ID associated with email address if it exists.
938
1087
* Return false if subscriber not found.
@@ -941,27 +1090,18 @@ public function get_resources(string $resource)
941
1090
*
942
1091
* @throws \InvalidArgumentException If the email address is not a valid email format.
943
1092
*
944
- * @see https://developers.convertkit.com/#list-subscribers
1093
+ * @see https://developers.convertkit.com/v4.html#get-a-subscriber
945
1094
*
946
1095
* @return false|integer
947
1096
*/
948
1097
public function get_subscriber_id (string $ email_address )
949
1098
{
950
- if (!filter_var ($ email_address , FILTER_VALIDATE_EMAIL )) {
951
- throw new \InvalidArgumentException ('Email address is not a valid email format. ' );
952
- }
953
-
954
1099
$ subscribers = $ this ->get (
955
1100
'subscribers ' ,
956
1101
['email_address ' => $ email_address ]
957
1102
);
958
1103
959
- if (!$ subscribers ) {
960
- $ this ->create_log ('No subscribers ' );
961
- return false ;
962
- }
963
-
964
- if ($ subscribers ->total_subscribers === 0 ) {
1104
+ if (!count ($ subscribers ->subscribers )) {
965
1105
$ this ->create_log ('No subscribers ' );
966
1106
return false ;
967
1107
}
@@ -975,7 +1115,7 @@ public function get_subscriber_id(string $email_address)
975
1115
*
976
1116
* @param integer $subscriber_id Subscriber ID.
977
1117
*
978
- * @see https://developers.convertkit.com/#view-a-single -subscriber
1118
+ * @see https://developers.convertkit.com/v4.html#get-a -subscriber
979
1119
*
980
1120
* @return false|integer
981
1121
*/
@@ -992,7 +1132,7 @@ public function get_subscriber(int $subscriber_id)
992
1132
* @param string $email_address New Email Address.
993
1133
* @param array<string, string> $fields Updated Custom Fields.
994
1134
*
995
- * @see https://developers.convertkit.com/#update-subscriber
1135
+ * @see https://developers.convertkit.com/v4.html #update-a -subscriber
996
1136
*
997
1137
* @return false|mixed
998
1138
*/
@@ -1023,56 +1163,64 @@ public function update_subscriber(
1023
1163
}
1024
1164
1025
1165
/**
1026
- * Unsubscribe an email address from all forms and sequences .
1166
+ * Unsubscribe an email address.
1027
1167
*
1028
1168
* @param string $email Email Address.
1029
1169
*
1030
- * @see https://developers.convertkit.com/#unsubscribe-subscriber
1170
+ * @see https://developers.convertkit.com/v4.html #unsubscribe-subscriber
1031
1171
*
1032
1172
* @return false|object
1033
1173
*/
1034
1174
public function unsubscribe (string $ email )
1035
1175
{
1036
- return $ this ->put (
1037
- 'unsubscribe ' ,
1038
- ['email ' => $ email ]
1176
+ return $ this ->post (
1177
+ sprintf (
1178
+ 'subscribers/%s/unsubscribe ' ,
1179
+ $ this ->get_subscriber_id ($ email )
1180
+ )
1039
1181
);
1040
1182
}
1041
1183
1042
1184
/**
1043
- * Remove subscription from a form
1185
+ * Unsubscribe the given subscriber ID.
1044
1186
*
1045
- * @param array<string, string> $options Array of user data (email) .
1187
+ * @param integer $subscriber_id Subscriber ID .
1046
1188
*
1047
- * @see https://developers.convertkit.com/#unsubscribe-subscriber
1189
+ * @see https://developers.convertkit.com/v4.html #unsubscribe-subscriber
1048
1190
*
1049
1191
* @return false|object
1050
1192
*/
1051
- public function form_unsubscribe ( array $ options )
1193
+ public function unsubscribe_by_id ( int $ subscriber_id )
1052
1194
{
1053
- // This function is deprecated in 1.0, as we prefer functions with structured arguments.
1054
- // This function name is also misleading, as it doesn't just unsubscribe the email
1055
- // address from forms.
1056
- trigger_error (
1057
- 'form_unsubscribe() is deprecated in 1.0. Use unsubscribe($email) instead. ' ,
1058
- E_USER_NOTICE
1059
- );
1060
-
1061
- return $ this ->put ('unsubscribe ' , $ options );
1195
+ return $ this ->post (sprintf ('subscribers/%s/unsubscribe ' , $ subscriber_id ));
1062
1196
}
1063
1197
1064
1198
/**
1065
1199
* Get a list of the tags for a subscriber.
1066
1200
*
1067
1201
* @param integer $subscriber_id Subscriber ID.
1202
+ * @param string $after_cursor Return results after the given pagination cursor.
1203
+ * @param string $before_cursor Return results before the given pagination cursor.
1204
+ * @param integer $per_page Number of results to return.
1068
1205
*
1069
- * @see https://developers.convertkit.com/#list-tags-for-a-subscriber
1206
+ * @see https://developers.convertkit.com/v4.html #list-tags-for-a-subscriber
1070
1207
*
1071
1208
* @return false|array<int,\stdClass>
1072
1209
*/
1073
- public function get_subscriber_tags (int $ subscriber_id )
1074
- {
1075
- return $ this ->get (sprintf ('subscribers/%s/tags ' , $ subscriber_id ));
1210
+ public function get_subscriber_tags (
1211
+ int $ subscriber_id ,
1212
+ string $ after_cursor = '' ,
1213
+ string $ before_cursor = '' ,
1214
+ int $ per_page = 100
1215
+ ) {
1216
+ return $ this ->get (
1217
+ endpoint: sprintf ('subscribers/%s/tags ' , $ subscriber_id ),
1218
+ args: $ this ->build_pagination_params (
1219
+ after_cursor: $ after_cursor ,
1220
+ before_cursor: $ before_cursor ,
1221
+ per_page: $ per_page
1222
+ )
1223
+ );
1076
1224
}
1077
1225
1078
1226
/**
0 commit comments