From 1825f2e88a650a51aabba9eb36a36a0011d519c1 Mon Sep 17 00:00:00 2001 From: Joshua Dinh Date: Wed, 6 Dec 2023 15:07:53 +0900 Subject: [PATCH] feature: add mailchimp migration step --- src/FormMigration/ServiceProvider.php | 1 + src/FormMigration/Steps/MailchimpSettings.php | 72 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 src/FormMigration/Steps/MailchimpSettings.php diff --git a/src/FormMigration/ServiceProvider.php b/src/FormMigration/ServiceProvider.php index 45aadfb231..3a6114081b 100644 --- a/src/FormMigration/ServiceProvider.php +++ b/src/FormMigration/ServiceProvider.php @@ -45,6 +45,7 @@ public function register() Steps\FormMeta::class, Steps\PdfSettings::class, Steps\FeeRecovery::class, + Steps\MailchimpSettings::class, ]); }); } diff --git a/src/FormMigration/Steps/MailchimpSettings.php b/src/FormMigration/Steps/MailchimpSettings.php new file mode 100644 index 0000000000..c4c8c18fd8 --- /dev/null +++ b/src/FormMigration/Steps/MailchimpSettings.php @@ -0,0 +1,72 @@ +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', []), + + ]; + } +}