| 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 |
* @var int $instance |
| 12 |
*/ |
| 13 |
public static $instance = 0; |
| 14 |
|
| 15 |
/** |
| 16 |
* @since 3.2.0 include v3 block attributes for shortcode. |
| 17 |
* @since 3.1.1 use static instance ID to simulate blockId attribute |
| 18 |
* @since 3.0.0 |
| 19 |
*/ |
| 20 |
public function __invoke(string $output, array $atts): string |
| 21 |
{ |
| 22 |
self::$instance++; |
| 23 |
|
| 24 |
$formId = absint($atts['id']); |
| 25 |
$isV3Form = (bool)give()->form_meta->get_meta($formId, 'formBuilderSettings', true); |
| 26 |
|
| 27 |
if (!$formId || !$isV3Form) { |
| 28 |
return $output; |
| 29 |
} |
| 30 |
|
| 31 |
$controller = new BlockRenderController(); |
| 32 |
$blockAttributes = [ |
| 33 |
'formId' => $formId, |
| 34 |
'blockId' => 'give-form-shortcode-' . self::$instance, |
| 35 |
'formFormat' => $atts['display_style'] ?? null, |
| 36 |
'openFormButton' => $atts['continue_button_title'] ?? null |
| 37 |
]; |
| 38 |
|
| 39 |
$output = $controller->render($blockAttributes); |
| 40 |
|
| 41 |
if (!$output) { |
| 42 |
$viewUrl = (new GenerateDonationFormViewRouteUrl())($formId); |
| 43 |
$output = sprintf( |
| 44 |
"<iframe |
| 45 |
src='%s' |
| 46 |
style='width: 1px;min-width: 100%%;border: 0;transition: height 0.25s;' |
| 47 |
onload='if( \"undefined\" !== typeof Give ) { Give.initializeIframeResize(this) }' |
| 48 |
></iframe>", |
| 49 |
esc_url($viewUrl) |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
return $output; |
| 54 |
} |
| 55 |
} |
| 56 |
|