BlockRenderController.php
132 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonationForms\Blocks\DonationFormBlock\Controllers; |
| 4 | |
| 5 | use Give\DonationForms\Actions\GenerateDonationConfirmationReceiptViewRouteUrl; |
| 6 | use Give\DonationForms\Actions\GenerateDonationFormViewRouteUrl; |
| 7 | use Give\DonationForms\Blocks\DonationFormBlock\DataTransferObjects\BlockAttributes; |
| 8 | use Give\DonationForms\DataTransferObjects\DonationConfirmationReceiptViewRouteData; |
| 9 | use Give\DonationForms\Models\DonationForm; |
| 10 | use Give\Framework\EnqueueScript; |
| 11 | use Give\Framework\Routes\RouteListener; |
| 12 | |
| 13 | class BlockRenderController |
| 14 | { |
| 15 | /** |
| 16 | * @since 3.2.0 include form url for new tab format. |
| 17 | * @since 3.0.0 |
| 18 | * |
| 19 | * @return string|null |
| 20 | */ |
| 21 | public function render(array $attributes) |
| 22 | { |
| 23 | // return early if we're still inside the editor to avoid server side effects |
| 24 | if (!empty($_REQUEST['post']) || !empty($_REQUEST['action']) || !empty($_REQUEST['_locale'])) { |
| 25 | return null; |
| 26 | } |
| 27 | |
| 28 | $blockAttributes = BlockAttributes::fromArray($attributes); |
| 29 | |
| 30 | if (!$blockAttributes->formId) { |
| 31 | return null; |
| 32 | } |
| 33 | |
| 34 | $this->loadEmbedScript(); |
| 35 | |
| 36 | /** @var DonationForm $donationForm */ |
| 37 | $donationForm = DonationForm::find($blockAttributes->formId); |
| 38 | |
| 39 | $embedId = $blockAttributes->blockId ?? ''; |
| 40 | |
| 41 | $viewUrl = $this->getViewUrl($donationForm, $embedId); |
| 42 | $formUrl = esc_url(add_query_arg(['p' => $blockAttributes->formId], site_url('?post_type=give_forms'))); |
| 43 | $formViewUrl = $this->getFormViewUrl($donationForm); |
| 44 | |
| 45 | /** |
| 46 | * Note: iframe-resizer uses querySelectorAll so using a data attribute makes the most sense to target. |
| 47 | * It will also generate a dynamic ID - so when we have multiple embeds on a page there will be no conflict. |
| 48 | */ |
| 49 | return "<div class='root-data-givewp-embed' data-form-url='$formUrl' data-form-view-url='$formViewUrl' data-src='$viewUrl' data-givewp-embed-id='$embedId' data-form-format='$blockAttributes->formFormat' data-open-form-button='$blockAttributes->openFormButton'></div>"; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * If the page loads with our receipt route listener args then we need to render the receipt. |
| 54 | * |
| 55 | * @since 3.0.0 |
| 56 | */ |
| 57 | protected function shouldDisplayDonationConfirmationReceipt(string $embedId): bool |
| 58 | { |
| 59 | $routeListener = new RouteListener( |
| 60 | 'donation-completed', |
| 61 | 'show-donation-confirmation-receipt' |
| 62 | ); |
| 63 | |
| 64 | return $routeListener->isValid($_GET, function ($request) use ($embedId) { |
| 65 | $isset = isset($request['givewp-embed-id'], $request['givewp-receipt-id']); |
| 66 | |
| 67 | return $isset && $request['givewp-embed-id'] === $embedId && DonationConfirmationReceiptViewRouteData::isReceiptIdValid( |
| 68 | $request['givewp-receipt-id'] |
| 69 | ); |
| 70 | }); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Get the iframe URL. |
| 75 | * This could either be the donation form view or the donation confirmation receipt view. |
| 76 | * |
| 77 | * @since 3.0.0 |
| 78 | */ |
| 79 | private function getViewUrl(DonationForm $donationForm, string $embedId): string |
| 80 | { |
| 81 | if ($this->shouldDisplayDonationConfirmationReceipt($embedId)) { |
| 82 | $receiptId = give_clean($_GET['givewp-receipt-id']); |
| 83 | |
| 84 | return (new GenerateDonationConfirmationReceiptViewRouteUrl())($receiptId); |
| 85 | } |
| 86 | |
| 87 | return $this->getFormViewUrl($donationForm); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @since 3.4.0 |
| 92 | */ |
| 93 | private function getFormViewUrl(DonationForm $donationForm): string |
| 94 | { |
| 95 | return (new GenerateDonationFormViewRouteUrl())($donationForm->id); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * |
| 100 | * Load embed givewp script to resize iframe |
| 101 | * @see https://github.com/davidjbradshaw/iframe-resizer |
| 102 | * |
| 103 | * @since 3.0.0 |
| 104 | */ |
| 105 | protected function loadEmbedScript() |
| 106 | { |
| 107 | (new EnqueueScript( |
| 108 | 'givewp-donation-form-embed', |
| 109 | 'build/donationFormEmbed.js', |
| 110 | GIVE_PLUGIN_DIR, |
| 111 | GIVE_PLUGIN_URL, |
| 112 | 'give' |
| 113 | ))->loadInFooter()->enqueue(); |
| 114 | |
| 115 | (new EnqueueScript( |
| 116 | 'givewp-donation-form-embed-app', |
| 117 | 'build/donationFormBlockApp.js', |
| 118 | GIVE_PLUGIN_DIR, |
| 119 | GIVE_PLUGIN_URL, |
| 120 | 'give' |
| 121 | )) |
| 122 | ->dependencies(['jquery']) |
| 123 | ->loadInFooter() |
| 124 | ->enqueue(); |
| 125 | |
| 126 | wp_enqueue_style( |
| 127 | 'givewp-donation-form-embed-app', |
| 128 | GIVE_PLUGIN_URL . 'build/donationFormBlockApp.css' |
| 129 | ); |
| 130 | } |
| 131 | } |
| 132 |