| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\DonationForms\Actions; |
| 4 |
|
| 5 |
use Give\Framework\Views\View; |
| 6 |
|
| 7 |
/** |
| 8 |
* Renders a grey sketch of a form, drawn from GetFormSkeletonData, that holds the form's space |
| 9 |
* while the real one loads. The block prints it in the page inside its root element, and the form |
| 10 |
* view prints it inside the iframe, so the on-site embed and the external embed draw the same |
| 11 |
* markup from one place without shipping a renderer to the client. |
| 12 |
* |
| 13 |
* The markup lives in the DonationForms views under form-skeleton. Classic shows the header and |
| 14 |
* every section. Multi-step shows only its first step, which is the header when the form has one. |
| 15 |
* Two-panel puts the header beside the first section. Any other design id, including one an |
| 16 |
* add-on registers, renders nothing, and the embed shows a spinner. |
| 17 |
* |
| 18 |
* @since 4.17.0 |
| 19 |
*/ |
| 20 |
class RenderFormSkeleton |
| 21 |
{ |
| 22 |
/** |
| 23 |
* The core designs the sketch knows the spacing of. |
| 24 |
* |
| 25 |
* @since 4.17.0 |
| 26 |
*/ |
| 27 |
private const KNOWN_DESIGNS = ['classic', 'multi-step', 'two-panel-steps']; |
| 28 |
|
| 29 |
/** |
| 30 |
* Blocks whose real height is far from a single input, so the section template gives them |
| 31 |
* their own sketch. Anything else is drawn as a label and one input row. |
| 32 |
* |
| 33 |
* @since 4.17.0 |
| 34 |
*/ |
| 35 |
private const BLOCK_VARIANTS = [ |
| 36 |
'givewp/donation-amount' => 'amount', |
| 37 |
'givewp/payment-gateways' => 'gateways', |
| 38 |
'givewp/donation-summary' => 'summary', |
| 39 |
]; |
| 40 |
|
| 41 |
/** |
| 42 |
* @since 4.17.0 |
| 43 |
* |
| 44 |
* @param array $data The array GetFormSkeletonData returns. |
| 45 |
* |
| 46 |
* @return string Empty for a design the skeleton cannot sketch. |
| 47 |
*/ |
| 48 |
public function __invoke(array $data): string |
| 49 |
{ |
| 50 |
$design = (string)($data['design'] ?? ''); |
| 51 |
|
| 52 |
if (!in_array($design, self::KNOWN_DESIGNS, true)) { |
| 53 |
return ''; |
| 54 |
} |
| 55 |
|
| 56 |
$sections = array_map(static function ($blocks): array { |
| 57 |
return array_map('strval', is_array($blocks) ? $blocks : []); |
| 58 |
}, array_values(is_array($data['sections'] ?? null) ? $data['sections'] : [])); |
| 59 |
|
| 60 |
return trim(View::load('DonationForms.form-skeleton', [ |
| 61 |
'design' => $design, |
| 62 |
'header' => !empty($data['header']), |
| 63 |
'goal' => !empty($data['goal']), |
| 64 |
'image' => !empty($data['image']), |
| 65 |
'sections' => $sections, |
| 66 |
'gateways' => (int)($data['gateways'] ?? 0), |
| 67 |
'variants' => self::BLOCK_VARIANTS, |
| 68 |
])); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* The skeleton's stylesheet, raw, for the caller to inline however its page allows. It has to |
| 73 |
* travel with the markup because the markup must paint before any enqueued stylesheet arrives: |
| 74 |
* the block prints a style element next to it in the content, since anything it enqueued would |
| 75 |
* print in the footer, and the form view adds it to its head through wp_add_inline_style(). |
| 76 |
* |
| 77 |
* @since 4.17.0 |
| 78 |
*/ |
| 79 |
public function css(): string |
| 80 |
{ |
| 81 |
static $css = null; |
| 82 |
|
| 83 |
if ($css === null) { |
| 84 |
$file = GIVE_PLUGIN_DIR . 'build/donationFormSkeletonCss.css'; |
| 85 |
$css = is_readable($file) ? (string)file_get_contents($file) : ''; |
| 86 |
} |
| 87 |
|
| 88 |
return $css; |
| 89 |
} |
| 90 |
} |
| 91 |
|