Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v4 API: Add Subscriber to Legacy Forms / Landing Pages #69

Merged
merged 3 commits into from
Jun 13, 2024
Merged
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
15 changes: 15 additions & 0 deletions src/class-convertkit-api-traits.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,21 @@ public function add_subscriber_to_form(int $form_id, int $subscriber_id)
return $this->post(sprintf('forms/%s/subscribers/%s', $form_id, $subscriber_id));
}

/**
* Adds a subscriber to a legacy form by subscriber ID
*
* @param integer $form_id Legacy Form ID.
* @param integer $subscriber_id Subscriber ID.
*
* @since 2.0.0
*
* @return WP_Error|array
*/
public function add_subscriber_to_legacy_form(int $form_id, int $subscriber_id)
{
return $this->post(sprintf('landing_pages/%s/subscribers/%s', $form_id, $subscriber_id));
}

/**
* List subscribers for a form
*
Expand Down
26 changes: 26 additions & 0 deletions src/class-convertkit-api-v4.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,32 @@ public function form_subscribe( $form_id, $email, $first_name = '', $custom_fiel

}

/**
* Creates the given subscriber, assiging them to the given ConvertKit Legacy Form.
*
* @since 2.0.0
*
* @param int $form_id Form ID.
* @param string $email Email Address.
* @param string $first_name First Name.
* @param array $custom_fields Custom Fields.
* @return WP_Error|array
*/
public function legacy_form_subscribe( $form_id, $email, $first_name = '', $custom_fields = array() ) {

// Create subscriber.
$subscriber = $this->create_subscriber( $email, $first_name, 'active', $custom_fields );

// Bail if an error occured.
if ( is_wp_error( $subscriber ) ) {
return $subscriber;
}

// Add subscriber to legacy form.
return $this->add_subscriber_to_legacy_form( $form_id, $subscriber['subscriber']['id'] );

}

/**
* Creates the given subscriber, assiging them to the given ConvertKit Tag.
*
Expand Down
206 changes: 206 additions & 0 deletions tests/wpunit/APITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,24 @@ public function testAddSubscriberToFormWithInvalidFormID()
$this->assertEquals($result->get_error_code(), $this->errorCode);
}

/**
* Test that add_subscriber_to_form() returns a WP_Error when a legacy
* form ID is specified.
*
* @since 2.0.0
*
* @return void
*/
public function testAddSubscriberToFormWithLegacyFormID()
{
$result = $this->api->add_subscriber_to_form(
$_ENV['CONVERTKIT_API_LEGACY_FORM_ID'],
$_ENV['CONVERTKIT_API_SUBSCRIBER_ID']
);
$this->assertInstanceOf(WP_Error::class, $result);
$this->assertEquals($result->get_error_code(), $this->errorCode);
}

/**
* Test that add_subscriber_to_form() returns a WP_Error when an invalid
* email address is specified.
Expand All @@ -1564,6 +1582,92 @@ public function testAddSubscriberToformWithInvalidSubscriberID()
$this->assertEquals($result->get_error_code(), $this->errorCode);
}

/**
* Test that add_subscriber_to_legacy_form() returns the expected data.
*
* @since 2.0.0
*
* @return void
*/
public function testAddSubscriberToLegacyForm()
{
// Create subscriber.
$subscriber = $this->api->create_subscriber(
$this->generateEmailAddress()
);

$this->assertNotInstanceOf(WP_Error::class, $subscriber);
$this->assertIsArray($subscriber);

// Set subscriber_id to ensure subscriber is unsubscribed after test.
$this->subscriber_ids[] = $subscriber['subscriber']['id'];

// Add subscriber to legacy form.
$result = $this->api->add_subscriber_to_legacy_form(
(int) $_ENV['CONVERTKIT_API_LEGACY_FORM_ID'],
$subscriber['subscriber']['id']
);
$this->assertNotInstanceOf(WP_Error::class, $result);
$this->assertIsArray($result);
$this->assertArrayHasKey('subscriber', $result);
$this->assertArrayHasKey('id', $result['subscriber']);
$this->assertEquals($result['subscriber']['id'], $subscriber['subscriber']['id']);
}

/**
* Test that add_subscriber_to_legacy_form() returns a WP_Error when an invalid
* form ID is specified.
*
* @since 2.0.0
*
* @return void
*/
public function testAddSubscriberToLegacyFormWithInvalidFormID()
{
$result = $this->api->add_subscriber_to_legacy_form(
12345,
$_ENV['CONVERTKIT_API_SUBSCRIBER_ID']
);
$this->assertInstanceOf(WP_Error::class, $result);
$this->assertEquals($result->get_error_code(), $this->errorCode);
}

/**
* Test that add_subscriber_to_legacy_form() returns a WP_Error when a non-legacy
* form ID is specified.
*
* @since 2.0.0
*
* @return void
*/
public function testAddSubscriberToLegacyFormWithNonLegacyFormID()
{
$result = $this->api->add_subscriber_to_legacy_form(
$_ENV['CONVERTKIT_API_FORM_ID'],
$_ENV['CONVERTKIT_API_SUBSCRIBER_ID']
);
$this->assertInstanceOf(WP_Error::class, $result);
$this->assertEquals($result->get_error_code(), $this->errorCode);
}

/**
* Test that add_subscriber_to_legacy_form() returns a WP_Error when an invalid
* email address is specified.
*
* @since 2.0.0
*
* @return void
*/
public function testAddSubscriberToLegacyFormWithInvalidSubscriberID()
{
$result = $this->api->add_subscriber_to_legacy_form(
$_ENV['CONVERTKIT_API_LEGACY_FORM_ID'],
12345
);
$this->assertInstanceOf(WP_Error::class, $result);
$this->assertEquals($result->get_error_code(), $this->errorCode);
}

/**
* Test that get_sequences() returns the expected data.
*
Expand Down Expand Up @@ -4914,6 +5018,24 @@ public function testFormSubscribeWithInvalidFormID()
$this->assertEquals($result->get_error_code(), $this->errorCode);
}

/**
* Test that the `form_subscribe()` function returns a WP_Error
* when a legacy Form ID is specified.
*
* @since 2.0.0
*
* @return void
*/
public function testFormSubscribeWithLegacyFormID()
{
$result = $this->api->form_subscribe(
$_ENV['CONVERTKIT_API_LEGACY_FORM_ID'],
$this->generateEmailAddress()
);
$this->assertInstanceOf(WP_Error::class, $result);
$this->assertEquals($result->get_error_code(), $this->errorCode);
}

/**
* Test that the `form_subscribe()` function returns a WP_Error
* when an invalid email address is specified.
Expand All @@ -4932,6 +5054,90 @@ public function testFormSubscribeWithInvalidEmailAddress()
$this->assertEquals($result->get_error_code(), $this->errorCode);
}

/**
* Test that the `legacy_form_subscribe()` function returns the expected data.
*
* @since 2.0.0
*
* @return void
*/
public function testLegacyFormSubscribe()
{
// Make request.
$emailAddress = $this->generateEmailAddress();
$result = $this->api->legacy_form_subscribe(
$_ENV['CONVERTKIT_API_LEGACY_FORM_ID'],
$emailAddress,
'First',
[
'last_name' => 'Last',
]
);

// Test array was returned.
$this->assertNotInstanceOf(WP_Error::class, $result);
$this->assertIsArray($result);

// Assert subscriber created.
$this->assertArrayHasKey('subscriber', $result);
$this->assertArrayHasKey('email_address', $result['subscriber']);
$this->assertEquals($emailAddress, $result['subscriber']['email_address']);
}

/**
* Test that the `legacy_form_subscribe()` function returns a WP_Error
* when an invalid Form ID is specified.
*
* @since 2.0.0
*
* @return void
*/
public function testLegacyFormSubscribeWithInvalidFormID()
{
$result = $this->api->legacy_form_subscribe(
12345,
$this->generateEmailAddress()
);
$this->assertInstanceOf(WP_Error::class, $result);
$this->assertEquals($result->get_error_code(), $this->errorCode);
}

/**
* Test that the `legacy_form_subscribe()` function returns a WP_Error
* when a non-legacy Form ID is specified.
*
* @since 2.0.0
*
* @return void
*/
public function testLegacyFormSubscribeWithNonLegacyFormID()
{
$result = $this->api->legacy_form_subscribe(
$_ENV['CONVERTKIT_API_FORM_ID'],
$this->generateEmailAddress()
);
$this->assertInstanceOf(WP_Error::class, $result);
$this->assertEquals($result->get_error_code(), $this->errorCode);
}

/**
* Test that the `legacy_form_subscribe()` function returns a WP_Error
* when an invalid email address is specified.
*
* @since 2.0.0
*
* @return void
*/
public function testLegacyFormSubscribeWithInvalidEmailAddress()
{
$result = $this->api->legacy_form_subscribe(
$_ENV['CONVERTKIT_API_LEGACY_FORM_ID'],
'not-a-valid-email'
);
$this->assertInstanceOf(WP_Error::class, $result);
$this->assertEquals($result->get_error_code(), $this->errorCode);
}

/**
* Test that the `tag_subscribe()` function returns the expected data.
*
Expand Down
Loading