Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v4-api' into v4-api-tests-email-…
Browse files Browse the repository at this point in the history
…templates

# Conflicts:
#	tests/wpunit/APITest.php
  • Loading branch information
n7studios committed Apr 22, 2024
2 parents 8a88734 + d8b2f79 commit 496375c
Show file tree
Hide file tree
Showing 2 changed files with 1,470 additions and 271 deletions.
57 changes: 57 additions & 0 deletions src/class-convertkit-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,63 @@ public function exchange_api_key_and_secret_for_access_token( $api_key, $api_sec

}

/**
* Get the ConvertKit subscriber ID associated with email address if it exists.
* Return false if subscriber not found.
*
* @param string $email_address Email Address.
*
* @see https://developers.convertkit.com/v4.html#get-a-subscriber
*
* @return WP_Error|false|integer
*/
public function get_subscriber_id( string $email_address ) {

$subscribers = $this->get(
'subscribers',
array( 'email_address' => $email_address )
);

if ( is_wp_error( $subscribers ) ) {
return $subscribers;
}

if ( ! count( $subscribers['subscribers'] ) ) {
return false;
}

// Return the subscriber's ID.
return $subscribers['subscribers'][0]['id'];

}

/**
* Unsubscribe an email address.
*
* @param string $email_address Email Address.
*
* @see https://developers.convertkit.com/v4.html#unsubscribe-subscriber
*
* @return WP_Error|false|object
*/
public function unsubscribe_by_email( string $email_address ) {

// Get subscriber ID.
$subscriber_id = $this->get_subscriber_id( $email_address );

if ( is_wp_error( $subscriber_id ) ) {
return $subscriber_id;
}

return $this->post(
sprintf(
'subscribers/%s/unsubscribe',
(int) $subscriber_id
)
);

}

/**
* Gets all posts from the API.
*
Expand Down
Loading

0 comments on commit 496375c

Please sign in to comment.