diff --git a/src/FormMigration/FormMetaDecorator.php b/src/FormMigration/FormMetaDecorator.php index 710adf0eea..d6ecbc2fb2 100644 --- a/src/FormMigration/FormMetaDecorator.php +++ b/src/FormMigration/FormMetaDecorator.php @@ -527,42 +527,40 @@ public function isMailchimpEnabled(): bool /** * @since 3.3.0 */ - public function getMailchimpLabel() + public function getMailchimpLabel(): string { - $value = $this->getMeta('_give_mailchimp_custom_label'); - return $value === '' ? null : $value; + return $this->getMeta('_give_mailchimp_custom_label', give_get_option('give_mailchimp_label', __('Subscribe to newsletter?'))); } /** + * @unreleased add global setting as default. * @since 3.3.0 */ public function getMailchimpDefaultChecked(): bool { - return $this->getMeta('_give_mailchimp_checked_default'); + return give_is_setting_enabled($this->getMeta('_give_mailchimp_checked_default', + give_get_option('give_mailchimp_checked_default', true))); } - /** - * @since 3.3.0 - */ - public function getMailchimpDoubleOptIn(): bool - { - return $this->getMeta('_give_mailchimp_double_opt_in'); - } /** + * @unreleased add global setting as default. * @since 3.3.0 */ public function getMailchimpSendDonationData(): bool { - return $this->getMeta('_give_mailchimp_send_donation'); + return give_is_setting_enabled($this->getMeta('_give_mailchimp_send_donation_data', + give_get_option('give_mailchimp_donation_data', true))); } /** + * @unreleased add global setting as default. * @since 3.3.0 */ public function getMailchimpSendFFMData(): bool { - return $this->getMeta('_give_mailchimp_send_ffm'); + return give_is_setting_enabled($this->getMeta('_give_mailchimp_send_ffm', + give_get_option('give_mailchimp_ffm_pass_field'))); } /** @@ -570,15 +568,15 @@ public function getMailchimpSendFFMData(): bool */ public function getMailchimpDefaultAudiences(): array { - return $this->getMeta('_give_mailchimp'); + return (array)$this->getMeta('_give_mailchimp', give_get_option('give_mailchimp_list', [''])); } /** * @since 3.3.0 */ - public function getMailchimpSubscriberTags(): array + public function getMailchimpSubscriberTags():? array { - return $this->getMeta('_give_mailchimp_tags'); + return (array)$this->getMeta('_give_mailchimp_tags', null); } diff --git a/src/FormMigration/Steps/Mailchimp.php b/src/FormMigration/Steps/Mailchimp.php index 946cba61d9..213c5e8470 100644 --- a/src/FormMigration/Steps/Mailchimp.php +++ b/src/FormMigration/Steps/Mailchimp.php @@ -25,7 +25,7 @@ public function process(): void { $block = BlockModel::make([ 'name' => 'givewp/mailchimp', - 'attributes' => $this->getAttributes() + 'attributes' => $this->getAttributes(), ]); $this->fieldBlocks->insertAfter('givewp/email', $block); @@ -36,21 +36,14 @@ public function process(): void */ private function getAttributes(): array { - return [ - 'label' => $this->formV2->getMailchimpLabel() ?? - give_get_option('give_mailchimp_label', __('Subscribe to newsletter?')), - 'checked' => $this->formV2->getMailchimpDefaultChecked() ?? - give_get_option('give_mailchimp_checked_default', true), - 'doubleOptIn' => $this->formV2->getMailchimpDoubleOptIn() ?? - give_get_option('give_mailchimp_double_opt_in'), - 'subscriberTags' => $this->formV2->getMailchimpSubscriberTags() ?? - [], - 'sendDonationData' => $this->formV2->getMailchimpSendDonationData() ?? - give_get_option('give_mailchimp_donation_data', true), - 'sendFFMData' => $this->formV2->getMailchimpSendFFMData() ?? - give_get_option('give_mailchimp_ffm_pass_field'), - 'defaultAudiences' => $this->formV2->getMailchimpDefaultAudiences() ?? - give_get_option('give_mailchimp_list', []), + return [ + 'label' => $this->formV2->getMailchimpLabel(), + 'checked' => $this->formV2->getMailchimpDefaultChecked(), + 'doubleOptIn' => give_get_option('give_mailchimp_double_opt_in', true), + 'subscriberTags' => $this->formV2->getMailchimpSubscriberTags(), + 'sendDonationData' => $this->formV2->getMailchimpSendDonationData(), + 'sendFFMData' => $this->formV2->getMailchimpSendFFMData(), + 'defaultAudiences' => $this->formV2->getMailchimpDefaultAudiences(), ]; } } diff --git a/tests/Feature/FormMigration/Steps/TestMailchimp.php b/tests/Feature/FormMigration/Steps/TestMailchimp.php new file mode 100644 index 0000000000..23480788e1 --- /dev/null +++ b/tests/Feature/FormMigration/Steps/TestMailchimp.php @@ -0,0 +1,109 @@ + __('Subscribe to newsletter?'), + '_give_mailchimp_tags' => ['Animal-Rescue-Campaign', 'Housing-And-Shelter-Campaign'], + '_give_mailchimp' => ['de73f3f82f'], + '_give_mailchimp_checked_default' => true, + '_give_mailchimp_send_donation_data' => true, + '_give_mailchimp_send_ffm' => true, + ]; + + $formV2 = $this->createSimpleDonationForm(['meta' => $meta]); + + $payload = FormMigrationPayload::fromFormV2($formV2); + + $mailchimp = new Mailchimp($payload); + + $mailchimp->process(); + + $block = $payload->formV3->blocks->findByName('givewp/mailchimp'); + + $this->assertSame($meta['_give_mailchimp_custom_label'], $block->getAttribute('label')); + $this->assertSame($meta['_give_mailchimp_tags'], $block->getAttribute('subscriberTags')); + $this->assertSame($meta['_give_mailchimp'], $block->getAttribute('defaultAudiences')); + $this->assertTrue(true, $block->getAttribute('checked')); + $this->assertTrue(true, $block->getAttribute('sendDonationData')); + $this->assertTrue(true, $block->getAttribute('sendFFMData')); + } + + /** + * @unreleased + */ + public function testProcessShouldUpdateMailchimpBlockAttributesFromGlobalSettings(): void + { + $meta = [ + 'give_mailchimp_label' => __('Subscribe to newsletter?'), + 'give_mailchimp_list' => ['de73f3f82f'], + 'give_mailchimp_checked_default' => true, + 'give_mailchimp_double_opt_in' => true, + 'give_mailchimp_donation_data' => true, + 'give_mailchimp_ffm_pass_field' => true, + ]; + + foreach ($meta as $key => $value) { + give_update_option($key, $value); + } + + $formV2 = $this->createSimpleDonationForm(['meta' => $meta]); + + $payload = FormMigrationPayload::fromFormV2($formV2); + + $mailchimp = new Mailchimp($payload); + + $mailchimp->process(); + + $block = $payload->formV3->blocks->findByName('givewp/mailchimp'); + + $this->assertSame($meta['give_mailchimp_label'], $block->getAttribute('label')); + $this->assertSame($meta['give_mailchimp_list'], $block->getAttribute('defaultAudiences')); + $this->assertNull(null, $block->getAttribute('subscriberTags')); + $this->assertTrue(true, $block->getAttribute('checked')); + $this->assertTrue(true, $block->getAttribute('doubleOptIn')); + $this->assertTrue(true, $block->getAttribute('sendDonationData')); + $this->assertTrue(true, $block->getAttribute('sendFFMData')); + } + + /** + * @unreleased + */ + public function testProcessShouldUpdateMailchimpBlockAttributesWhenNoMeta(): void + { + $formV2 = $this->createSimpleDonationForm(); + + $payload = FormMigrationPayload::fromFormV2($formV2); + + $mailchimp = new Mailchimp($payload); + + $mailchimp->process(); + + $block = $payload->formV3->blocks->findByName('givewp/mailchimp'); + + $this->assertSame(__('Subscribe to newsletter?'), $block->getAttribute('label')); + $this->assertNull(null, $block->getAttribute('subscriberTags')); + $this->assertSame([''], $block->getAttribute('defaultAudiences')); + $this->assertTrue(true, $block->getAttribute('checked')); + $this->assertTrue(true, $block->getAttribute('doubleOptIn')); + $this->assertTrue(true, $block->getAttribute('sendDonationData')); + $this->assertTrue(true, $block->getAttribute('sendFFMData')); + } +}