Skip to content

Commit

Permalink
feature: add mailchimp migration step
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaHungDinh committed Dec 6, 2023
1 parent b52e979 commit 1825f2e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/FormMigration/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function register()
Steps\FormMeta::class,
Steps\PdfSettings::class,
Steps\FeeRecovery::class,
Steps\MailchimpSettings::class,
]);
});
}
Expand Down
72 changes: 72 additions & 0 deletions src/FormMigration/Steps/MailchimpSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Give\FormMigration\Steps;

use Give\FormMigration\Contracts\FormMigrationStep;
use Give\Framework\Blocks\BlockModel;
use Give\Log\Log;

class MailchimpSettings extends FormMigrationStep
{
/**
* @unreleased
*/
public function process()
{
$prevFormId = $this->formV2->id;

$isFormEnabled = $this->getFormMetaValue($prevFormId, '_give_mailchimp_enable') === 'true';
$isFormDisabled = $this->getFormMetaValue($prevFormId, '_give_mailchimp_disable') === 'true';
$isGloballyEnabled = give_get_option( 'give_mailchimp_show_checkout_signup') === 'on';

if ($isFormDisabled || (!$isGloballyEnabled && !$isFormEnabled)) {
return;
}

$block = BlockModel::make([
'name' => 'givewp/mailchimp',
'attributes' => $this->getAttributes($prevFormId)
]);

$this->fieldBlocks->insertAfter('givewp/email', $block);
}

/**
* @unreleased
*/
private function getFormMetaValue(int $prevFormId, string $metaKey)
{
$meta = give()->form_meta->get_meta($prevFormId, $metaKey, true);

return $meta === '' ? null : $meta;
}

/**
* @unreleased
*/
private function getAttributes($prevFormId): array
{
return [
'label' => $this->getFormMetaValue($prevFormId, '_give_mailchimp_custom_label') ??
give_get_option('give_mailchimp_label', __('Subscribe to newsletter?')),

'checked' => $this->getFormMetaValue($prevFormId, '_give_mailchimp_checked_default') ??
give_get_option('give_mailchimp_checked_default', true),

'doubleOptIn' => $this->getFormMetaValue($prevFormId, '_give_mailchimp_double_opt_in') ??
give_get_option('give_mailchimp_double_opt_in', false),

'subscriberTags' => $this->getFormMetaValue($prevFormId, '_give_mailchimp_tags') ?? [],

'sendDonationData' => $this->getFormMetaValue($prevFormId, '_give_mailchimp_send_donation') ??
give_get_option('give_mailchimp_donation_data', true),

'sendFFMData' => $this->getFormMetaValue($prevFormId, '_give_mailchimp_send_ffm') ??
give_get_option('give_mailchimp_ffm_pass_field', false),

'defaultAudiences' => $this->getFormMetaValue($prevFormId, '_give_mailchimp') ??
give_get_option('give_mailchimp_list', []),

];
}
}

0 comments on commit 1825f2e

Please sign in to comment.