GiveFormShortcode.php
45 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonationForms\Shortcodes; |
| 4 | |
| 5 | use Give\DonationForms\Actions\GenerateDonationFormViewRouteUrl; |
| 6 | use Give\DonationForms\Blocks\DonationFormBlock\Controllers\BlockRenderController; |
| 7 | |
| 8 | class GiveFormShortcode |
| 9 | { |
| 10 | /** |
| 11 | * @since 3.0.0 |
| 12 | */ |
| 13 | public function __invoke(string $output, array $atts): string |
| 14 | { |
| 15 | $formId = absint($atts['id']); |
| 16 | $isV3Form = (bool) give()->form_meta->get_meta($formId, 'formBuilderSettings', true); |
| 17 | |
| 18 | if (!$formId || !$isV3Form) { |
| 19 | return $output; |
| 20 | } |
| 21 | |
| 22 | $controller = new BlockRenderController(); |
| 23 | $blockAttributes = [ |
| 24 | 'formId' => $formId, |
| 25 | 'blockId' => 'give-form-shortcode-' . uniqid(), |
| 26 | ]; |
| 27 | |
| 28 | $output = $controller->render($blockAttributes); |
| 29 | |
| 30 | if (!$output) { |
| 31 | $viewUrl = (new GenerateDonationFormViewRouteUrl())($formId); |
| 32 | $output = sprintf( |
| 33 | "<iframe |
| 34 | src='%s' |
| 35 | style='width: 1px;min-width: 100%%;border: 0;transition: height 0.25s;' |
| 36 | onload='if( \"undefined\" !== typeof Give ) { Give.initializeIframeResize(this) }' |
| 37 | ></iframe>", |
| 38 | esc_url($viewUrl) |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | return $output; |
| 43 | } |
| 44 | } |
| 45 |