| 1 |
<?php |
| 2 |
|
| 3 |
namespace AcyMailing\WpInit; |
| 4 |
|
| 5 |
defined('ABSPATH') || die('Restricted Access'); |
| 6 |
|
| 7 |
use AcyMailing\Classes\FormClass; |
| 8 |
|
| 9 |
class Forms |
| 10 |
{ |
| 11 |
private array $formToDisplay = []; |
| 12 |
|
| 13 |
public function __construct() |
| 14 |
{ |
| 15 |
$isPreview = acym_getVar('bool', 'acym_preview', false); |
| 16 |
if ($isPreview) return; |
| 17 |
|
| 18 |
if (acym_level(ACYM_ENTERPRISE)) { |
| 19 |
add_action('wp_head', [$this, 'prepareFormsToDisplay']); |
| 20 |
add_action('wp_footer', [$this, 'displayForms']); |
| 21 |
} |
| 22 |
$this->registerShortcodes(); |
| 23 |
} |
| 24 |
|
| 25 |
public function prepareFormsToDisplay() |
| 26 |
{ |
| 27 |
$formClass = new FormClass(); |
| 28 |
$forms = $formClass->getAllFormsToDisplay(); |
| 29 |
|
| 30 |
if (empty($forms)) return; |
| 31 |
|
| 32 |
$menu = acym_getMenu(); |
| 33 |
if (empty($menu)) return; |
| 34 |
|
| 35 |
foreach ($forms as $form) { |
| 36 |
if (!empty($form->pages) && (in_array($menu->ID, $form->pages) || in_array('all', $form->pages))) { |
| 37 |
$this->formToDisplay[] = $formClass->renderForm($form); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
if (!empty($this->formToDisplay)) { |
| 42 |
acym_initModule(); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
public function displayForms() |
| 47 |
{ |
| 48 |
if (empty($this->formToDisplay)) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Generated in partial folder "forms", escaped there. |
| 53 |
echo implode('', $this->formToDisplay); |
| 54 |
} |
| 55 |
|
| 56 |
public function registerShortcodes() |
| 57 |
{ |
| 58 |
add_shortcode('acymailing_form_shortcode', [$this, 'replaceShortcode']); |
| 59 |
} |
| 60 |
|
| 61 |
public function replaceShortcode($params) |
| 62 |
{ |
| 63 |
extract( |
| 64 |
shortcode_atts( |
| 65 |
[ |
| 66 |
'id' => 0, |
| 67 |
], |
| 68 |
$params |
| 69 |
) |
| 70 |
); |
| 71 |
|
| 72 |
if (empty($params['id'])) return; |
| 73 |
|
| 74 |
$formClass = new FormClass(); |
| 75 |
$form = $formClass->getOneById($params['id']); |
| 76 |
|
| 77 |
if (empty($form->active)) return; |
| 78 |
|
| 79 |
return $formClass->renderForm($form, false, true); |
| 80 |
} |
| 81 |
} |
| 82 |
|