Skip to content

Commit

Permalink
Refactor: create new forms using block models for translatable strings (
Browse files Browse the repository at this point in the history
#7053)

Co-authored-by: Jon Waldstein <[email protected]>
  • Loading branch information
jonwaldstein and Jon Waldstein authored Oct 26, 2023
1 parent e63e340 commit cb14120
Show file tree
Hide file tree
Showing 4 changed files with 326 additions and 13 deletions.
157 changes: 157 additions & 0 deletions src/FormBuilder/Actions/GenerateDefaultDonationFormBlockCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

namespace Give\FormBuilder\Actions;

use Give\Framework\Blocks\BlockCollection;
use Give\Framework\Blocks\BlockModel;

/**
* @unreleased
*/
class GenerateDefaultDonationFormBlockCollection
{
/**
* @unreleased
*/
public function __invoke(): BlockCollection
{
$section1 = $this->createSection(
__('How much would you like to donate today?', 'give'),
__('All donations directly impact our organization and help us further our mission.?', 'give'),
$this->createAmountBlock()
);

$section2 = $this->createSection(
__('Who\'s Giving Today?', 'give'),
__('We\'ll never share this information with anyone', 'give'),
$this->createDonorNameBlock(),
$this->createEmailBlock()
);

$section3 = $this->createSection(
__('Payment Details', 'give'),
__('How would you like to pay for your donation?', 'give'),
$this->createDonationSummaryBlock(),
$this->createPaymentGatewaysBlock()
);

return BlockCollection::make([
$section1,
$section2,
$section3
]);
}

/**
* @unreleased
*/
protected function createSection(string $title, string $description, BlockModel ...$innerBlocks): BlockModel
{
return BlockModel::make([
'name' => 'givewp/section',
'attributes' => [
'title' => $title,
'description' => $description,
],
'innerBlocks' => new BlockCollection($innerBlocks),
]);
}

/**
* @unreleased
*/
protected function createAmountBlock(): BlockModel
{
return BlockModel::make([
'name' => 'givewp/donation-amount',
'attributes' => [
"label" => __("Donation Amount", 'give'),
"levels" => [
10,
25,
50,
100,
250,
500
],
"defaultLevel" => 10,
"priceOption" => "multi",
"setPrice" => 25,
"customAmount" => true,
"customAmountMin" => 1,
"recurringBillingPeriodOptions" => [
"month"
],
"recurringBillingInterval" => 1,
"recurringEnabled" => false,
"recurringLengthOfTime" => "0",
"recurringOptInDefaultBillingPeriod" => "month",
"recurringEnableOneTimeDonations" => true
],
'innerBlocks' => [],
]);
}

/**
* @unreleased
*/
protected function createDonorNameBlock(): BlockModel
{
return BlockModel::make([
'name' => 'givewp/donor-name',
'attributes' => [
"showHonorific" => false,
"honorifics" => [
__("Mr", 'give'),
__("Ms", 'give'),
__("Mrs", 'give')
],
"firstNameLabel" => __("First name", 'give'),
"firstNamePlaceholder" => __("First name", 'give'),
"lastNameLabel" => __("Last name", 'give'),
"lastNamePlaceholder" => __("Last name", 'give'),
"requireLastName" => false
],
"innerBlocks" => []
]);
}

/**
* @unreleased
*/
protected function createEmailBlock(): BlockModel
{
return BlockModel::make([
'name' => 'givewp/email',
'attributes' => [
"label" => __("Email Address", 'give'),
"isRequired" => true,
],
"innerBlocks" => []
]);
}

/**
* @unreleased
*/
protected function createDonationSummaryBlock(): BlockModel
{
return BlockModel::make([
'name' => 'givewp/donation-summary',
'attributes' => [],
'innerBlocks' => []
]);
}

/**
* @unreleased
*/
protected function createPaymentGatewaysBlock(): BlockModel
{
return BlockModel::make([
'name' => 'givewp/payment-gateways',
'attributes' => [],
'innerBlocks' => []
]);
}
}
9 changes: 3 additions & 6 deletions src/FormBuilder/Routes/CreateFormRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use Give\DonationForms\Models\DonationForm;
use Give\DonationForms\Properties\FormSettings;
use Give\DonationForms\ValueObjects\DonationFormStatus;
use Give\FormBuilder\Actions\GenerateDefaultDonationFormBlockCollection;
use Give\FormBuilder\FormBuilderRouteBuilder;
use Give\Framework\Blocks\BlockCollection;
use Give\Helpers\Hooks;

/**
Expand All @@ -16,6 +16,7 @@
class CreateFormRoute
{
/**
* @unreleased updated default form blocks to be generated from block models instead of json
* @since 3.0.0
*
* @return void
Expand All @@ -30,18 +31,14 @@ public function __invoke()
exit();
}
if ('new' === $_GET['donationFormID']) {
$blocksJson = file_get_contents(
GIVE_PLUGIN_DIR . 'src/FormBuilder/resources/js/form-builder/src/blocks.json'
);

$form = new DonationForm([
'title' => __('GiveWP Donation Form', 'give'),
'status' => DonationFormStatus::DRAFT(),
'settings' => FormSettings::fromArray([
'enableDonationGoal' => true,
'goalAmount' => 1000,
]),
'blocks' => BlockCollection::fromJson($blocksJson)
'blocks' => (new GenerateDefaultDonationFormBlockCollection())(),
]);

Hooks::doAction('givewp_form_builder_new_form', $form);
Expand Down
33 changes: 26 additions & 7 deletions src/Framework/Blocks/BlockModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class BlockModel implements Arrayable
public $innerBlocks;

/**
* @unreleased added innerBlocks sanitization
* @since 3.0.0
* @param string $name
* @param string $clientId
* @param bool $isValid
Expand All @@ -46,7 +48,28 @@ public function __construct(
$this->clientId = $clientId ?? wp_generate_uuid4();
$this->isValid = $isValid;
$this->attributes = $attributes;
$this->innerBlocks = $innerBlocks ?: new BlockCollection([]);
$this->innerBlocks = $this->sanitizeInnerBlocks($innerBlocks);
}

/**
* @unreleased
*
* @param array|BlockCollection|null $innerBlocks
*/
public function sanitizeInnerBlocks($innerBlocks): BlockCollection
{
if (empty($innerBlocks)) {
return new BlockCollection([]);
}

if (is_a($innerBlocks, BlockCollection::class)) {
return $innerBlocks;
}

return new BlockCollection(
array_map([__CLASS__, 'make'],
$innerBlocks)
);
}

/**
Expand Down Expand Up @@ -94,24 +117,20 @@ public function getShortName(): string
}

/**
* @unreleased simplified innerBlocks param
* @since 3.0.0
*
* @param array $blockData
* @return BlockModel
*/
public static function make( array $blockData ): BlockModel
{
$innerBlocks = !empty($blockData['innerBlocks']) ? new BlockCollection(
array_map([__CLASS__, 'make'],
$blockData['innerBlocks'])
) : new BlockCollection([]);

return new BlockModel(
$blockData['name'],
!empty($blockData['clientId']) ? $blockData['clientId'] : wp_generate_uuid4(),
!empty($blockData['isValid']) ? $blockData['isValid'] : true,
!empty($blockData['attributes']) ? $blockData['attributes'] : [],
$innerBlocks
$blockData['innerBlocks'] ?? []
);
}

Expand Down
Loading

0 comments on commit cb14120

Please sign in to comment.