Skip to content

Commit

Permalink
Merge pull request #689 from ConvertKit/forminator-subscribe-tag-sequ…
Browse files Browse the repository at this point in the history
…ence

Forminator: Option to Subscribe; add Tag / Sequence to Subscriber
  • Loading branch information
n7studios authored Jul 25, 2024
2 parents f164930 + e6acdcb commit c7e2e21
Show file tree
Hide file tree
Showing 5 changed files with 810 additions and 108 deletions.
54 changes: 53 additions & 1 deletion includes/class-convertkit-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public function update() {
return;
}

/**
* 2.5.2: Migrate Forminator to ConvertKit Form Mappings
*/
if ( ! $current_version || version_compare( $current_version, '2.5.2', '<' ) ) {
$this->migrate_forminator_form_mapping_settings();
}

/**
* 2.5.2: Migrate Contact Form 7 to ConvertKit Form Mappings
*/
Expand Down Expand Up @@ -98,7 +105,52 @@ public function update() {
}

/**
* 2.5.2: Prefix any Contact Form 7 to ConvertKit Form ID mappings with `form_`, now that
* 2.5.2: Prefix any Forminator to ConvertKit Form ID mappings with `form:`, now that
* the Plugin supports adding a subscriber to a Form, Tag or Sequence.
*
* @since 2.5.2
*/
private function migrate_forminator_form_mapping_settings() {

$convertkit_forminator_settings = new ConvertKit_Forminator_Settings();

// Bail if no settings exist.
if ( ! $convertkit_forminator_settings->has_settings() ) {
return;
}

// Get settings.
$settings = $convertkit_forminator_settings->get();

// Iterate through settings.
foreach ( $settings as $forminator_form_id => $convertkit_form_id ) {
// Skip keys that are non-numeric e.g. `creator_network_recommendations_*`.
if ( ! is_numeric( $forminator_form_id ) ) {
continue;
}

// Skip values that are blank i.e. no ConvertKit Form ID specified.
if ( empty( $convertkit_form_id ) ) {
continue;
}

// Skip values that are non-numeric i.e. the `form_` prefix was already added.
// This should never happen as this routine runs once, but this is a sanity check.
if ( ! is_numeric( $convertkit_form_id ) ) {
continue;
}

// Prefix the ConvertKit Form ID with `form_`.
$settings[ $forminator_form_id ] = 'form:' . $convertkit_form_id;
}

// Update settings.
update_option( $convertkit_forminator_settings::SETTINGS_NAME, $settings );

}

/**
* 2.5.2: Prefix any Contact Form 7 to ConvertKit Form ID mappings with `form:`, now that
* the Plugin supports adding a subscriber to a Form, Tag or Sequence.
*
* @since 2.5.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,6 @@ public function render() {

do_settings_sections( $this->settings_key );

// Get Forms.
$forms = new ConvertKit_Resource_Forms( 'forminator' );
$creator_network_recommendations = new ConvertKit_Resource_Creator_Network_Recommendations( 'forminator' );

// Bail with an error if no ConvertKit Forms exist.
if ( ! $forms->exist() ) {
$this->output_error( __( 'No Forms exist on ConvertKit.', 'convertkit' ) );
$this->render_container_end();
return;
}

// Get Creator Network Recommendations script.
$creator_network_recommendations_enabled = $creator_network_recommendations->enabled();

// Get Forminator Forms.
$forminator_forms = $this->get_forminator_forms();

Expand All @@ -120,25 +106,27 @@ public function render() {
return;
}

// Get Creator Network Recommendations script.
$creator_network_recommendations = new ConvertKit_Resource_Creator_Network_Recommendations( 'forminator' );
$creator_network_recommendations_enabled = $creator_network_recommendations->enabled();

// Setup WP_List_Table.
$table = new Multi_Value_Field_Table();
$table->add_column( 'title', __( 'Forminator Form', 'convertkit' ), true );
$table->add_column( 'form', __( 'ConvertKit Form', 'convertkit' ), false );
$table->add_column( 'form', __( 'ConvertKit', 'convertkit' ), false );
$table->add_column( 'creator_network_recommendations', __( 'Enable Creator Network Recommendations', 'convertkit' ), false );

// Iterate through Forminator Forms, adding a table row for each Forminator Form.
foreach ( $forminator_forms as $forminator_form ) {
// Build row.
$table_row = array(
'title' => $forminator_form['name'],
'form' => $forms->get_select_field_all(
'form' => convertkit_get_subscription_dropdown_field(
'_wp_convertkit_integration_forminator_settings[' . $forminator_form['id'] . ']',
'_wp_convertkit_integration_forminator_settings_' . $forminator_form['id'] . '',
false,
(string) $this->settings->get_convertkit_form_id_by_forminator_form_id( $forminator_form['id'] ),
array(
'' => __( 'None', 'convertkit' ),
)
(string) $this->settings->get_convertkit_subscribe_setting_by_forminator_form_id( $forminator_form['id'] ),
'_wp_convertkit_integration_forminator_settings_' . $forminator_form['id'],
'widefat',
'forminator'
),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ public function has_settings() {
* @since 2.3.0
*
* @param int $forminator_form_id Forminator Form ID.
* @return bool|int
* @return bool|string|int
*/
public function get_convertkit_form_id_by_forminator_form_id( $forminator_form_id ) {
public function get_convertkit_subscribe_setting_by_forminator_form_id( $forminator_form_id ) {

// Bail if no settings exist.
if ( ! $this->has_settings() ) {
Expand Down
67 changes: 50 additions & 17 deletions includes/integrations/forminator/class-convertkit-forminator.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ public function maybe_subscribe( $entry, $form_id, $form_data_array ) {
// Get ConvertKit Form ID mapped to this Forminator Form.
// We deliberately use the entry's form ID, as $form_id for a Quiz will point to a lead generation form, which
// has a different Form ID.
$forminator_settings = new ConvertKit_Forminator_Settings();
$convertkit_form_id = $forminator_settings->get_convertkit_form_id_by_forminator_form_id( $entry->form_id );
$forminator_settings = new ConvertKit_Forminator_Settings();
$convertkit_subscribe_setting = $forminator_settings->get_convertkit_subscribe_setting_by_forminator_form_id( $entry->form_id );

// If no ConvertKit Form is mapped to this Forminator Form, bail.
if ( ! $convertkit_form_id ) {
// If no ConvertKit subscribe setting is defined, bail.
if ( ! $convertkit_subscribe_setting ) {
return;
}

Expand Down Expand Up @@ -132,21 +132,54 @@ public function maybe_subscribe( $entry, $form_id, $form_data_array ) {
'forminator'
);

// For Legacy Forms, a different endpoint is used.
$forms = new ConvertKit_Resource_Forms();
if ( $forms->is_legacy( $convertkit_form_id ) ) {
return $api->legacy_form_subscribe(
$convertkit_form_id,
$email,
$first_name
);
// Subscribe the email address.
$subscriber = $api->create_subscriber( $email, $first_name );
if ( is_wp_error( $subscriber ) ) {
return;
}

return $api->form_subscribe(
$convertkit_form_id,
$email,
$first_name
);
// If the setting is 'Subscribe', no Form needs to be assigned to the subscriber.
if ( $convertkit_subscribe_setting === 'subscribe' ) {
return;
}

// Determine the resource type and ID to assign to the subscriber.
list( $resource_type, $resource_id ) = explode( ':', $convertkit_subscribe_setting );

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

// Add the subscriber to the resource type (form, tag etc).
switch ( $resource_type ) {

/**
* Form
*/
case 'form':
// For Legacy Forms, a different endpoint is used.
$forms = new ConvertKit_Resource_Forms();
if ( $forms->is_legacy( $resource_id ) ) {
return $api->add_subscriber_to_legacy_form( $resource_id, $subscriber['subscriber']['id'] );
}

// Add subscriber to form.
return $api->add_subscriber_to_form( $resource_id, $subscriber['subscriber']['id'] );

/**
* Sequence
*/
case 'sequence':
// Add subscriber to sequence.
return $api->add_subscriber_to_sequence( $resource_id, $subscriber['subscriber']['id'] );

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

}

}

Expand Down
Loading

0 comments on commit c7e2e21

Please sign in to comment.