Skip to content

Commit

Permalink
Feature: add activecampaign add-on to the form migration process (#7336)
Browse files Browse the repository at this point in the history
Co-authored-by: Jon Waldstein <[email protected]>
  • Loading branch information
JoshuaHungDinh and Jon Waldstein authored Apr 29, 2024
1 parent 07afc8d commit beec47e
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/DonationForms/V2/DonationFormsAdminPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public function getSupportedAddons(): array
'Per Form Gateways' => class_exists('Give_Per_Form_Gateways'),
// 'Per Form Confirmations' => class_exists('Per_Form_Confirmations_4_GIVEWP'),
// 'Form Countdown' => class_exists('Give_Form_Countdown'),
// 'ActiveCampaign' => class_exists('Give_ActiveCampaign'),
'ActiveCampaign' => class_exists('Give_ActiveCampaign'),
];

$output = [];
Expand Down
60 changes: 60 additions & 0 deletions src/FormMigration/FormMetaDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -847,4 +847,64 @@ public function getFormFeaturedImage()

return $featuredImage;
}

/**
* @unreleased
*/
public function isActiveCampaignEnabled(): bool
{
$isFormEnabled = give_is_setting_enabled($this->getMeta('activecampaign_per_form_options'), 'customized');

$isFormDisabled = give_is_setting_enabled($this->getMeta('activecampaign_per_form_options'), 'disabled');

$isGloballyEnabled = give_is_setting_enabled(give_get_option('give_activecampaign_globally_enabled'), 'on');

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

/**
* @unreleased
*/
public function getActiveCampaignLabel(): string
{
$defaultMeta = get_option('give_activecampaign_label', __('Subscribe to our newsletter?', 'give'));

return $this->getMeta('give_activecampaign_label', $defaultMeta);
}

/**
* @unreleased
*/
public function getActiveCampaignDefaultChecked(): bool
{
$isFormEnabled = give_is_setting_enabled($this->getMeta('activecampaign_per_form_options'), 'customized');

$isGlobalChecked = give_is_setting_enabled(give_get_option('give_activecampaign_checkbox_default'), 'on');

$isFormChecked = give_is_setting_enabled($this->getMeta('give_activecampaign_checkbox_default'), 'on');

return $isFormEnabled ? $isFormChecked : $isGlobalChecked;
}

/**
* @unreleased
*/
public function getActiveCampaignSelectedLists(): array
{
$defaultMeta = give_get_option('give_activecampaign_lists', []);

return !empty($this->getMeta('give_activecampaign_lists')) ?
$this->getMeta('give_activecampaign_lists') : $defaultMeta;
}

/**
* @unreleased
*/
public function getActiveCampaignTags(): array
{
$defaultMeta = give_get_option('give_activecampaign_tags',[]);

return !empty($this->getMeta('give_activecampaign_tags')) ?
$this->getMeta('give_activecampaign_tags') : $defaultMeta;
}
}
1 change: 1 addition & 0 deletions src/FormMigration/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function register()
Steps\GiftAid::class,
Steps\FormFeaturedImage::class,
Steps\FormExcerpt::class,
Steps\ActiveCampaign::class,
Steps\DoubleTheDonation::class,
]);
});
Expand Down
46 changes: 46 additions & 0 deletions src/FormMigration/Steps/ActiveCampaign.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Give\FormMigration\Steps;

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

/**
* @unreleased
*/
class ActiveCampaign extends FormMigrationStep
{
/**
* @unreleased
*/
public function canHandle(): bool
{
return $this->formV2->isActiveCampaignEnabled();
}

/**
* @unreleased
*/
public function process(): void
{
$block = BlockModel::make([
'name' => 'give-activecampaign/activecampaign',
'attributes' => $this->getAttributes()
]);

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

/**
* @unreleased
*/
private function getAttributes(): array
{
return [
'label' => $this->formV2->getActiveCampaignLabel() ,
'defaultChecked' => $this->formV2->getActiveCampaignDefaultChecked(),
'selectedLists' => $this->formV2->getActiveCampaignSelectedLists(),
'selectedTags' => $this->formV2->getActiveCampaignTags()
];
}
}
97 changes: 97 additions & 0 deletions tests/Feature/FormMigration/Steps/TestActiveCampaign.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Feature\FormMigration\Steps;

namespace Give\Tests\Feature\FormMigration\Steps;

use Give\FormMigration\DataTransferObjects\FormMigrationPayload;
use Give\FormMigration\Steps\ActiveCampaign;
use Give\Tests\TestCase;
use Give\Tests\TestTraits\RefreshDatabase;
use Give\Tests\Unit\DonationForms\TestTraits\LegacyDonationFormAdapter;

class TestActiveCampaign extends TestCase
{
use RefreshDatabase, LegacyDonationFormAdapter;

/**
* @unreleased
*/
public function testProcessShouldUpdateActiveCampaignBlockAttributesFromV2FormMeta(): void
{
$meta = [
'give_activecampaign_label' => __('Subscribe to our newsletter?'),
'give_activecampaign_lists' => ['1', '2'],
'give_activecampaign_tags' => ['tag 1', 'tag 2'],
'give_activecampaign_checkbox_default' => true,
];

$formV2 = $this->createSimpleDonationForm(['meta' => $meta]);

$payload = FormMigrationPayload::fromFormV2($formV2);

$mailchimp = new ActiveCampaign($payload);

$mailchimp->process();

$block = $payload->formV3->blocks->findByName('give-activecampaign/activecampaign');

$this->assertSame($meta['give_activecampaign_label'], $block->getAttribute('label'));
$this->assertSame($meta['give_activecampaign_lists'], $block->getAttribute('selectedLists'));
$this->assertSame($meta['give_activecampaign_tags'], $block->getAttribute('selectedTags'));
$this->assertTrue(true, $block->getAttribute('defaultChecked'));
}

/**
* @unreleased
*/
public function testProcessShouldUpdateActiveCampaignBlockAttributesFromGlobalSettings(): void
{
$meta = [
'give_activecampaign_label' => __('Subscribe to our newsletter?'),
'give_activecampaign_lists' => ['1', '2'],
'give_activecampaign_tags' => ['tag 1', 'tag 2'],
'give_activecampaign_checkbox_default' => true,
];

foreach ($meta as $key => $value) {
give_update_option($key, $value);
}

$formV2 = $this->createSimpleDonationForm(['meta' => $meta]);

$payload = FormMigrationPayload::fromFormV2($formV2);

$mailchimp = new ActiveCampaign($payload);

$mailchimp->process();

$block = $payload->formV3->blocks->findByName('give-activecampaign/activecampaign');

$this->assertSame($meta['give_activecampaign_label'], $block->getAttribute('label'));
$this->assertSame($meta['give_activecampaign_lists'], $block->getAttribute('selectedLists'));
$this->assertSame($meta['give_activecampaign_tags'], $block->getAttribute('selectedTags'));
$this->assertTrue(true, $block->getAttribute('defaultChecked'));
}

/**
* @unreleased
*/
public function testProcessShouldUpdateActiveCampaignBlockAttributesWhenNoMeta(): void
{
$formV2 = $this->createSimpleDonationForm();

$payload = FormMigrationPayload::fromFormV2($formV2);

$mailchimp = new ActiveCampaign($payload);

$mailchimp->process();

$block = $payload->formV3->blocks->findByName('give-activecampaign/activecampaign');

$this->assertSame(__('Subscribe to our newsletter?'), $block->getAttribute('label'));
$this->assertSame([], $block->getAttribute('selectedLists'));
$this->assertNull(null, $block->getAttribute('selectedTags'));
$this->assertTrue(true, $block->getAttribute('defaultChecked'));
}
}

0 comments on commit beec47e

Please sign in to comment.