urlHelper = $urlHelper; $this->captchaSession = $captchaSession; $this->captchaPhrase = $captchaPhrase; $this->captchaUrlFactory = $urlFactory; $this->formRenderer = $formRenderer; $this->formsRepository = $formsRepository; $this->styles = $styles; } public function render(array $data) { $sessionId = (isset($data['captcha_session_id']) && is_string($data['captcha_session_id'])) ? $data['captcha_session_id'] : null; if (!$sessionId) { return false; } if ($data['referrer_form'] == CaptchaUrlFactory::REFERER_MP_FORM) { return $this->renderFormInSubscriptionForm($sessionId); } elseif ($data['referrer_form'] == CaptchaUrlFactory::REFERER_WP_FORM) { return $this->renderFormInWPRegisterForm($data, 'wp-submit'); } elseif ($data['referrer_form'] == CaptchaUrlFactory::REFERER_WC_FORM) { return $this->renderFormInWPRegisterForm($data, 'register'); } return false; } private function renderFormInSubscriptionForm($sessionId) { $captchaSessionForm = $this->captchaSession->getFormData($sessionId); $showSuccessMessage = !empty($_GET['mailpoet_success']); $showErrorMessage = !empty($_GET['mailpoet_error']); $formId = 0; if (isset($captchaSessionForm['form_id'])) { $formId = (int)$captchaSessionForm['form_id']; } elseif ($showSuccessMessage) { $formId = (int)$_GET['mailpoet_success']; } elseif ($showErrorMessage) { $formId = (int)$_GET['mailpoet_error']; } $formModel = $this->formsRepository->findOneById($formId); if (!$formModel instanceof FormEntity) { return false; } elseif ($showSuccessMessage) { // Display a success message in a no-JS flow return $this->renderFormMessages($formModel, true); } $redirectUrl = htmlspecialchars($this->urlHelper->getCurrentUrl(), ENT_QUOTES); $hiddenFields = ''; $hiddenFields .= ''; $hiddenFields .= ''; $hiddenFields .= ''; $hiddenFields .= ''; $hiddenFields .= ''; $actionUrl = admin_url('admin-post.php?action=mailpoet_subscription_form'); $submitBlocks = $formModel->getBlocksByTypes(['submit']); $submitLabel = count($submitBlocks) && $submitBlocks[0]['params']['label'] ? $submitBlocks[0]['params']['label'] : __('Subscribe', 'mailpoet'); $afterSubmitElement = $this->renderFormMessages($formModel, false, $showErrorMessage); $styles = $this->styles->renderFormMessageStyles($formModel, '#mailpoet_captcha_form'); $styles = ''; return $this->renderForm($sessionId, $hiddenFields, $actionUrl, $submitLabel, $afterSubmitElement, $styles); } private function renderFormInWPRegisterForm(array $data, string $submitLabelKey) { $sessionId = $data['captcha_session_id']; unset($data['captcha_session_id']); // The 'name' attr is required in this format for the refresh button to work $hiddenFields = ''; $actionUrl = $data['referrer_form_url']; unset($data['referrer_form_url']); unset($data['referrer_form']); foreach ($data as $key => $value) { $hiddenFields .= ''; } $submitLabel = $data[$submitLabelKey] ?? esc_attr_e('Register'); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain return $this->renderForm($sessionId, $hiddenFields, $actionUrl, $submitLabel); } private function renderForm( $sessionId, $hiddenFields, $actionUrl, $submitLabel, $afterSubmitElement = null, $styles = null ) { $this->captchaPhrase->createPhrase($sessionId); $fields = [ [ 'id' => 'captcha', 'type' => 'text', 'params' => [ 'label' => __('Type in the characters you see in the picture above:', 'mailpoet'), 'value' => '', 'obfuscate' => false, ], ], ]; $form = array_merge( $fields, [ [ 'id' => 'submit', 'type' => 'submit', 'params' => [ 'label' => $submitLabel, ], ], ], ); if ($afterSubmitElement) { // The 'mailpoet_form' class alter the form's submission behavior // Refer to mailpoet/assets/js/src/public.tsx $classes = 'mailpoet_form mailpoet_captcha_form'; } else { $classes = 'mailpoet_captcha_form'; } $formHtml = '
'; if ($styles) { $formHtml .= $styles; } return $formHtml; } private function renderFormMessages( FormEntity $formModel, $showSuccessMessage = false, $showErrorMessage = false ) { $settings = $formModel->getSettings() ?? []; $errorMessage = __('The characters you entered did not match the CAPTCHA image. Please try again with this new image.', 'mailpoet'); $formHtml = ' '; return $formHtml; } }