Validator
1 month ago
BehavioralSignals.php
1 month ago
CaptchaConstants.php
2 months ago
CaptchaFormRenderer.php
2 weeks ago
CaptchaHooks.php
2 months ago
CaptchaPhrase.php
1 year ago
CaptchaRenderer.php
4 months ago
CaptchaSession.php
1 year ago
CaptchaUrlFactory.php
4 months ago
PageRenderer.php
5 months ago
ReCaptchaHooks.php
2 months ago
ReCaptchaRenderer.php
1 year ago
ReCaptchaValidator.php
1 year ago
TurnstileHooks.php
2 months ago
TurnstileRenderer.php
2 months ago
TurnstileValidator.php
2 months ago
index.php
1 year ago
PageRenderer.php
100 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Captcha; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Form\AssetsController; |
| 9 | use MailPoet\Settings\Pages; |
| 10 | use MailPoet\WP\Functions as WPFunction; |
| 11 | |
| 12 | class PageRenderer { |
| 13 | private array $data = []; |
| 14 | private WPFunction $wp; |
| 15 | private CaptchaFormRenderer $formRenderer; |
| 16 | private AssetsController $assetsController; |
| 17 | |
| 18 | public function __construct( |
| 19 | WPFunction $wp, |
| 20 | CaptchaFormRenderer $formRenderer, |
| 21 | AssetsController $assetsController |
| 22 | ) { |
| 23 | $this->wp = $wp; |
| 24 | $this->formRenderer = $formRenderer; |
| 25 | $this->assetsController = $assetsController; |
| 26 | } |
| 27 | |
| 28 | public function render($data) { |
| 29 | $this->data = $data; |
| 30 | $this->wp->addFilter('wp_title', [$this, 'setWindowTitle'], 10, 3); |
| 31 | $this->wp->addFilter('document_title_parts', [$this, 'setWindowTitleParts']); |
| 32 | $this->wp->removeAction('wp_head', 'noindex', 1); |
| 33 | $this->wp->addAction('wp_head', [$this, 'setMetaRobots'], 1); |
| 34 | $this->wp->addFilter('the_title', [$this, 'setPageTitle']); |
| 35 | $this->wp->addFilter('single_post_title', [$this, 'setPageTitle']); |
| 36 | $this->wp->addFilter('the_content', [$this, 'setPageContent']); |
| 37 | } |
| 38 | |
| 39 | public function setWindowTitle($title, $separator = '', $separatorLocation = 'right') { |
| 40 | // If no separator is provided, just modify the entire title |
| 41 | if (empty($separator)) { |
| 42 | return $this->getPageTitle(); |
| 43 | } |
| 44 | $titleParts = explode(" $separator ", $title); |
| 45 | if (!is_array($titleParts)) { |
| 46 | return $title; |
| 47 | } |
| 48 | |
| 49 | if ($separatorLocation === 'right') { |
| 50 | // first part |
| 51 | $titleParts[0] = $this->getPageTitle(); |
| 52 | } else { |
| 53 | // last part |
| 54 | $lastIndex = count($titleParts) - 1; |
| 55 | $titleParts[$lastIndex] = $this->getPageTitle(); |
| 56 | } |
| 57 | |
| 58 | return implode(" $separator ", $titleParts); |
| 59 | } |
| 60 | |
| 61 | public function setWindowTitleParts($meta = []) { |
| 62 | $meta['title'] = $this->getPageTitle(); |
| 63 | return $meta; |
| 64 | } |
| 65 | |
| 66 | public function setMetaRobots() { |
| 67 | echo '<meta name="robots" content="noindex,nofollow">'; |
| 68 | } |
| 69 | |
| 70 | public function setPageTitle($title = '') { |
| 71 | if ($title === Pages::PAGE_TITLE || $title === __('MailPoet Page', 'mailpoet')) { |
| 72 | return $this->getPageTitle(); |
| 73 | } |
| 74 | return $title; |
| 75 | } |
| 76 | |
| 77 | public function setPageContent($pageContent) { |
| 78 | $this->assetsController->setupFrontEndDependencies(); |
| 79 | |
| 80 | // For preview, show a placeholder message since we don't have a real captcha session |
| 81 | if (isset($this->data['preview']) && $this->data['preview']) { |
| 82 | $content = '<div class="mailpoet_captcha_preview">' . |
| 83 | '<p>' . __('This is a preview of the CAPTCHA page.', 'mailpoet') . '</p>' . |
| 84 | '<p>' . __('When users need to verify they’re not a robot, the CAPTCHA form will be displayed here.', 'mailpoet') . '</p>' . |
| 85 | '</div>'; |
| 86 | } else { |
| 87 | $content = $this->formRenderer->render($this->data); |
| 88 | if (!$content) { |
| 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return str_replace('[mailpoet_page]', trim($content), $pageContent); |
| 94 | } |
| 95 | |
| 96 | private function getPageTitle() { |
| 97 | return __('Confirm you’re not a robot', 'mailpoet'); |
| 98 | } |
| 99 | } |
| 100 |