Skip to content

Commit

Permalink
Merge pull request #62 from ConvertKit/set-subscriber-state-inactive-…
Browse files Browse the repository at this point in the history
…forms

Create subscriber with `inactive` state when a Form option is selected
  • Loading branch information
n7studios authored Aug 15, 2024
2 parents fb7479c + cadce8f commit 79d46eb
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 15 deletions.
68 changes: 53 additions & 15 deletions includes/class-integrate-convertkit-wpforms.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,15 @@ public function process_entry( $fields, $entry, $form_data, $entry_id = 0 ) { //
continue;
}

// Determine resource type, resource ID and subscriber state to use.
$resource = $this->get_resource_type_and_id( $connection['list_id'] );
$subscriber_state = $this->get_initial_subscriber_state( $resource['type'] );

// Subscribe the email address.
$subscriber = $api->create_subscriber(
$args['email'],
( isset( $args['name'] ) ? $args['name'] : '' ),
'active',
$subscriber_state,
( isset( $args['fields'] ) ? $args['fields'] : array() )
);

Expand Down Expand Up @@ -311,26 +315,20 @@ public function process_entry( $fields, $entry, $form_data, $entry_id = 0 ) { //
}

// If the subscribe setting isn't 'subscribe', add the subscriber to the resource type.
if ( $connection['list_id'] !== 'subscribe' ) {
// Determine the resource type and ID to assign to the subscriber.
list( $resource_type, $resource_id ) = explode( ':', $connection['list_id'] );

// Cast ID.
$resource_id = absint( $resource_id );

if ( $resource['type'] !== 'subscribe' ) {
// Add the subscriber to the resource type (form, tag etc).
switch ( $resource_type ) {
switch ( $resource['type'] ) {

/**
* Form
*/
case 'form':
// For Legacy Forms, a different endpoint is used.
if ( $resource_forms->is_legacy( $resource_id ) ) {
$response = $api->add_subscriber_to_legacy_form( $resource_id, $subscriber['subscriber']['id'] );
if ( $resource_forms->is_legacy( $resource['id'] ) ) {
$response = $api->add_subscriber_to_legacy_form( $resource['id'], $subscriber['subscriber']['id'] );
} else {
// Add subscriber to form.
$response = $api->add_subscriber_to_form( $resource_id, $subscriber['subscriber']['id'] );
$response = $api->add_subscriber_to_form( $resource['id'], $subscriber['subscriber']['id'] );
}
break;

Expand All @@ -339,15 +337,15 @@ public function process_entry( $fields, $entry, $form_data, $entry_id = 0 ) { //
*/
case 'sequence':
// Add subscriber to sequence.
$response = $api->add_subscriber_to_sequence( $resource_id, $subscriber['subscriber']['id'] );
$response = $api->add_subscriber_to_sequence( $resource['id'], $subscriber['subscriber']['id'] );
break;

/**
* Tag
*/
case 'tag':
// Add subscriber to tag.
$response = $api->tag_subscriber( $resource_id, $subscriber['subscriber']['id'] );
$response = $api->tag_subscriber( $resource['id'], $subscriber['subscriber']['id'] );
break;

/**
Expand All @@ -359,7 +357,7 @@ public function process_entry( $fields, $entry, $form_data, $entry_id = 0 ) { //
sprintf(
/* translators: Resource type */
esc_html__( 'The resource type %s is unsupported.', 'integrate-convertkit-wpforms' ),
$resource_type
$resource['type']
)
);
break;
Expand Down Expand Up @@ -421,6 +419,46 @@ public function process_entry( $fields, $entry, $form_data, $entry_id = 0 ) { //

}

/**
* Returns the subscriber state to use when creating a subscriber.
*
* @since 1.7.3
*
* @param string $resource_type Resource Type (subscriber,form,tag,sequence).
* @return string Subscriber state
*/
private function get_initial_subscriber_state( $resource_type ) {

return ( $resource_type === 'form' ? 'inactive' : 'active' );

}

/**
* Returns an array comprising of the resource type and ID for the given list ID setting.
*
* @since 1.7.3
*
* @param string $setting Setting.
* @return array
*/
private function get_resource_type_and_id( $setting ) {

if ( $setting === 'subscribe' ) {
return array(
'type' => 'subscribe',
'id' => 0,
);
}

list( $resource_type, $resource_id ) = explode( ':', $setting );

return array(
'type' => $resource_type,
'id' => absint( $resource_id ),
);

}

/**
* Returns the value for the given field in a WPForms form entry.
*
Expand Down
4 changes: 4 additions & 0 deletions tests/acceptance/forms/FormBackwardCompatCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function testCreateFormWithTagClass(AcceptanceTester $I)
$I->fillField('.ck-tag input[type=text]', $_ENV['CONVERTKIT_API_TAG_ID']);

// Submit Form.
$I->wait(2);
$I->click('Submit');

// Check that no PHP warnings or notices were output.
Expand Down Expand Up @@ -157,6 +158,7 @@ public function testCreateFormWithInvalidTag(AcceptanceTester $I)
$I->fillField('.ck-tag input[type=text]', '1111');

// Submit Form.
$I->wait(2);
$I->click('Submit');

// Check that no PHP warnings or notices were output.
Expand Down Expand Up @@ -232,6 +234,7 @@ public function testCreateFormWithCustomField(AcceptanceTester $I)
$I->fillField('.ck-custom-' . $_ENV['CONVERTKIT_API_CUSTOM_FIELD_NAME'] . ' textarea', $customFields[ $_ENV['CONVERTKIT_API_CUSTOM_FIELD_NAME'] ]);

// Submit Form.
$I->wait(2);
$I->click('Submit');

// Check that no PHP warnings or notices were output.
Expand Down Expand Up @@ -307,6 +310,7 @@ public function testCreateFormWithInvalidCustomField(AcceptanceTester $I)
$I->fillField('.ck-custom-fakeCustomFieldName textarea', $customFields['fakeCustomFieldName']);

// Submit Form.
$I->wait(2);
$I->click('Submit');

// Check that no PHP warnings or notices were output.
Expand Down
1 change: 1 addition & 0 deletions tests/acceptance/forms/FormCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ private function _wpFormsCompleteAndSubmitForm(AcceptanceTester $I, int $pageID,
}

// Submit Form.
$I->wait(2);
$I->click('Submit');

// Check that no PHP warnings or notices were output.
Expand Down
1 change: 1 addition & 0 deletions tests/acceptance/recommendations/RecommendationsCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ public function testCreatorNetworkRecommendationsWithAJAXEnabled(AcceptanceTeste
$I->fillField('.wpforms-field-email input[type=email]', $emailAddress);

// Submit Form.
$I->wait(2);
$I->click('Submit');

// Wait for Creator Network Recommendations modal to display.
Expand Down

0 comments on commit 79d46eb

Please sign in to comment.