| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\DonationForms\Actions; |
| 4 |
|
| 5 |
use Give\DonationForms\Models\DonationForm; |
| 6 |
use Give\DonationForms\Repositories\DonationFormRepository; |
| 7 |
use Give\DonationForms\ValueObjects\DesignSettingsImageStyle; |
| 8 |
use Give\Framework\Blocks\BlockModel; |
| 9 |
|
| 10 |
/** |
| 11 |
* The little the on-page embed needs to sketch a form before it loads: the design, whether there |
| 12 |
* is a header and the two header parts with real height (goal bar, image), the block names in |
| 13 |
* each section and how many gateway rows the donor will see. Every value is already on the form |
| 14 |
* or in options, so this costs no extra queries. The embed only knows how to draw the core |
| 15 |
* designs and falls back to a spinner for any other design id. |
| 16 |
* |
| 17 |
* @since 4.17.0 |
| 18 |
*/ |
| 19 |
class GetFormSkeletonData |
| 20 |
{ |
| 21 |
/** |
| 22 |
* @since 4.17.0 |
| 23 |
*/ |
| 24 |
public function __invoke(DonationForm $donationForm): array |
| 25 |
{ |
| 26 |
$settings = $donationForm->settings; |
| 27 |
|
| 28 |
$sections = array_map(static function (BlockModel $block): array { |
| 29 |
if ($block->name !== 'givewp/section' || !$block->innerBlocks) { |
| 30 |
return [$block->name]; |
| 31 |
} |
| 32 |
|
| 33 |
return array_map(static function (BlockModel $innerBlock): string { |
| 34 |
return $innerBlock->name; |
| 35 |
}, $block->innerBlocks->getBlocks()); |
| 36 |
}, $donationForm->blocks->getBlocks()); |
| 37 |
|
| 38 |
return [ |
| 39 |
'design' => (string)$settings->designId, |
| 40 |
'header' => (bool)$settings->showHeader, |
| 41 |
'goal' => (bool)$settings->enableDonationGoal, |
| 42 |
'image' => $this->imageTakesSpace($settings->designSettingsImageUrl, $settings->designSettingsImageStyle), |
| 43 |
'sections' => array_values($sections), |
| 44 |
'gateways' => count(give(DonationFormRepository::class)->getEnabledPaymentGateways($donationForm->id)), |
| 45 |
]; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Only the "above" and "center" image styles put an image element in the header flow. The |
| 50 |
* background and cover styles paint behind the text and add no height. |
| 51 |
* |
| 52 |
* @since 4.17.0 |
| 53 |
* |
| 54 |
* @param DesignSettingsImageStyle|string|null $style |
| 55 |
*/ |
| 56 |
private function imageTakesSpace(?string $url, $style): bool |
| 57 |
{ |
| 58 |
if (!$url || !$style instanceof DesignSettingsImageStyle) { |
| 59 |
return false; |
| 60 |
} |
| 61 |
|
| 62 |
return $style->isAbove() || $style->isCenter(); |
| 63 |
} |
| 64 |
} |
| 65 |
|