Validator
2 months ago
BehavioralSignals.php
2 months 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
CaptchaUrlFactory.php
108 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Captcha; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Router\Endpoints\Captcha as CaptchaEndpoint; |
| 9 | use MailPoet\Router\Router; |
| 10 | use MailPoet\Settings\SettingsController; |
| 11 | use MailPoet\WP\Functions as WPFunctions; |
| 12 | |
| 13 | class CaptchaUrlFactory { |
| 14 | private WPFunctions $wp; |
| 15 | private SettingsController $settings; |
| 16 | |
| 17 | const REFERER_MP_FORM = 'mp_form'; |
| 18 | const REFERER_WP_FORM = 'wp_register_form'; |
| 19 | const REFERER_WC_FORM = 'wc_register_form'; |
| 20 | |
| 21 | public function __construct( |
| 22 | WPFunctions $wp, |
| 23 | SettingsController $settings |
| 24 | ) { |
| 25 | $this->wp = $wp; |
| 26 | $this->settings = $settings; |
| 27 | } |
| 28 | |
| 29 | public function getCaptchaUrl(array $data) { |
| 30 | return $this->getUrl(CaptchaEndpoint::ACTION_RENDER, $data); |
| 31 | } |
| 32 | |
| 33 | public function getCaptchaUrlForMPForm(string $sessionId) { |
| 34 | $data = [ |
| 35 | 'captcha_session_id' => $sessionId, |
| 36 | 'referrer_form' => self::REFERER_MP_FORM, |
| 37 | ]; |
| 38 | |
| 39 | return $this->getCaptchaUrl($data); |
| 40 | } |
| 41 | |
| 42 | public function getCaptchaImageUrl(string $sessionId) { |
| 43 | return $this->getUrl( |
| 44 | CaptchaEndpoint::ACTION_IMAGE, |
| 45 | [ |
| 46 | 'captcha_session_id' => $sessionId, |
| 47 | ] |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | public function getCaptchaAudioUrl(string $sessionId) { |
| 52 | return $this->getUrl( |
| 53 | CaptchaEndpoint::ACTION_AUDIO, |
| 54 | [ |
| 55 | 'cacheBust' => time(), |
| 56 | 'captcha_session_id' => $sessionId, |
| 57 | ] |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | public function getCaptchaPreviewUrl($post = null) { |
| 62 | if ($post === null) { |
| 63 | $post = $this->wp->getPost($this->settings->get('subscription.pages.captcha')); |
| 64 | } |
| 65 | if ($post === null) return; |
| 66 | |
| 67 | $url = $this->wp->getPermalink($post); |
| 68 | |
| 69 | // Use preview session ID for preview |
| 70 | $data = [ |
| 71 | 'captcha_session_id' => 'preview', |
| 72 | 'referrer_form' => self::REFERER_MP_FORM, |
| 73 | 'preview' => 1, |
| 74 | ]; |
| 75 | |
| 76 | $params = [ |
| 77 | Router::NAME, |
| 78 | 'endpoint=' . CaptchaEndpoint::ENDPOINT, |
| 79 | 'action=' . CaptchaEndpoint::ACTION_RENDER, |
| 80 | 'data=' . Router::encodeRequestData($data), |
| 81 | ]; |
| 82 | |
| 83 | $url .= (parse_url($url, PHP_URL_QUERY) ? '&' : '?') . join('&', $params); |
| 84 | |
| 85 | $urlParams = parse_url($url); |
| 86 | if (!is_array($urlParams) || empty($urlParams['scheme'])) { |
| 87 | $url = $this->wp->getBloginfo('url') . $url; |
| 88 | } |
| 89 | |
| 90 | return $url; |
| 91 | } |
| 92 | |
| 93 | private function getUrl(string $action, array $data) { |
| 94 | $post = $this->wp->getPost($this->settings->get('subscription.pages.captcha')); |
| 95 | $url = $this->wp->getPermalink($post); |
| 96 | |
| 97 | $params = [ |
| 98 | Router::NAME, |
| 99 | 'endpoint=' . CaptchaEndpoint::ENDPOINT, |
| 100 | 'action=' . $action, |
| 101 | 'data=' . Router::encodeRequestData($data), |
| 102 | ]; |
| 103 | |
| 104 | $url .= (parse_url($url, PHP_URL_QUERY) ? '&' : '?') . join('&', $params); |
| 105 | return $url; |
| 106 | } |
| 107 | } |
| 108 |