Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: GiveWP BlockTypes #7195

Merged
merged 16 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/FormBuilder/BlockTypes/Concerns/HasDefaultFieldAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Give\FormBuilder\BlockTypes\Concerns;

/**
* @unreleased
*
* @property string $label
* @property string $description
* @property string $placeholder
* @property bool $isRequired
* @property bool $displayInAdmin
* @property bool $displayInReceipt
* @property string $defaultValue
* @property string $emailTag
* @property string $fieldName
* @property array $conditionalLogic
* @property bool $storeAsDonorMeta
*/
trait HasDefaultFieldAttributes
{
/**
* @unreleased
*/
protected function setDefaultProperties(): array
{
return [
'label' => 'string',
'description' => 'string',
'placeholder' => 'string',
'isRequired' => 'bool',
'conditionalLogic' => 'array',
'storeAsDonorMeta' => 'bool',
'displayInAdmin' => 'bool',
'displayInReceipt' => 'bool',
'defaultValue' => 'string',
'emailTag' => 'string',
'fieldName' => 'string',
];
}
}
148 changes: 148 additions & 0 deletions src/FormBuilder/BlockTypes/DonationAmountBlockType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

namespace Give\FormBuilder\BlockTypes;

use Give\Framework\Blocks\BlockType;
use Give\Subscriptions\ValueObjects\SubscriptionPeriod;

/**
* @unreleased
*
* @property string $label
* @property array $levels
* @property float $defaultLevel
* @property string $priceOption
* @property int $setPrice
* @property bool $customAmount
* @property int $customAmountMin
* @property int $customAmountMax
* @property bool $recurringEnabled
* @property int $recurringBillingInterval
* @property array $recurringBillingPeriodOptions
* @property int $recurringLengthOfTime
* @property bool $recurringEnableOneTimeDonations
* @property string $recurringOptInDefaultBillingPeriod
*/
class DonationAmountBlockType extends BlockType
{
/**
* @unreleased
*/
public static function name(): string
{
return 'givewp/donation-amount';
}

/**
* @unreleased
*/
protected $properties = [
'label' => 'string',
'levels' => 'array',
'defaultLevel' => 'float',
'priceOption' => 'string',
'setPrice' => 'int',
'customAmount' => 'bool',
'customAmountMin' => 'int',
'customAmountMax' => 'int',
'recurringEnabled' => 'bool',
'recurringBillingInterval' => 'int',
'recurringBillingPeriodOptions' => 'array',
'recurringLengthOfTime' => 'int',
'recurringEnableOneTimeDonations' => 'bool',
'recurringOptInDefaultBillingPeriod' => 'string',
];

/**
* @unreleased
*
* @return float[]
*/
public function getLevels(): array
{
return array_map(static function ($level) {
return (float)filter_var($level, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
}, $this->levels);
}

/**
* @return bool
*/
public function isRecurringFixed(): bool
{
return count($this->recurringBillingPeriodOptions) === 1 && $this->recurringEnableOneTimeDonations === false;
}

/**
* @unreleased
*/
public function setRecurringEnabled(bool $enabled = true): self
{
$this->recurringEnabled = $enabled;

return $this;
}

/**
* @unreleased
*/
public function setRecurringEnableOneTimeDonations(bool $enabled = true): self
{
$this->recurringEnableOneTimeDonations = $enabled;

return $this;
}

/**
* @unreleased
*/
public function setRecurringBillingInterval(int $interval): self
{
$this->recurringBillingInterval = $interval;

return $this;
}

/**
* @unreleased
*/
public function setRecurringLengthOfTime(int $lengthOfTime): self
{
$this->recurringLengthOfTime = $lengthOfTime;

return $this;
}

/**
* @unreleased
*/
public function setRecurringBillingPeriodOptions(SubscriptionPeriod ...$options): self
{
$this->recurringBillingPeriodOptions =
array_values(
array_map(static function (SubscriptionPeriod $option) {
return $option->getValue();
}, $options)
);

return $this;
}

/**
* @unreleased
*/
public function setRecurringOptInDefaultBillingPeriod(SubscriptionPeriod $period): self
{
$this->recurringOptInDefaultBillingPeriod = $period->getValue();

return $this;
}

/**
* @since 3.0.0
*/
public function isCustomAmountEnabled(): bool
{
return $this->customAmount === true;
}
}
22 changes: 22 additions & 0 deletions src/FormBuilder/BlockTypes/TextBlockType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Give\FormBuilder\BlockTypes;

use Give\FormBuilder\BlockTypes\Concerns\HasDefaultFieldAttributes;
use Give\Framework\Blocks\BlockType;

/**
* @unreleased
*/
class TextBlockType extends BlockType
{
use HasDefaultFieldAttributes;

/**
* @unreleased
*/
public static function name(): string
{
return 'givewp/text';
}
}
Loading
Loading