TemplateHandler.php
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonationForms\FormPage; |
| 4 | |
| 5 | use Give\Helpers\Form\Utils; |
| 6 | use WP_Post as Post; |
| 7 | |
| 8 | /** |
| 9 | * @since 3.0.0 |
| 10 | */ |
| 11 | class TemplateHandler |
| 12 | { |
| 13 | /** |
| 14 | * @var Post|null |
| 15 | */ |
| 16 | private $post; |
| 17 | |
| 18 | /** |
| 19 | * @var string |
| 20 | */ |
| 21 | private $formPageTemplatePath; |
| 22 | |
| 23 | public function __construct( $post, string $formPageTemplatePath ) |
| 24 | { |
| 25 | $this->post = $post; |
| 26 | $this->formPageTemplatePath = $formPageTemplatePath; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @since 3.20.0 Add earlier return for archive pages |
| 31 | * @since 3.0.0 |
| 32 | */ |
| 33 | public function handle($template) |
| 34 | { |
| 35 | if (is_archive()) { |
| 36 | return $template; |
| 37 | } |
| 38 | |
| 39 | return $this->isNextGenForm() |
| 40 | ? $this->formPageTemplatePath |
| 41 | : $template; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @since 3.0.3 Use isV3Form() method instead of 'post_content' to check if the form is built with Visual Builder |
| 46 | * @since 3.0.0 |
| 47 | */ |
| 48 | protected function isNextGenForm(): bool |
| 49 | { |
| 50 | return $this->post |
| 51 | && Utils::isV3Form($this->post->ID) |
| 52 | && 'give_forms' === $this->post->post_type; |
| 53 | } |
| 54 | } |
| 55 |