| 1 |
<?php |
| 2 |
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped, WordPress.Security.EscapeOutput.HeredocOutputNotEscaped, PluginCheck.CodeAnalysis.Heredoc -- PFBC framework extension, HTML output is controlled |
| 3 |
class AccuaForm_Element_Captcha3 extends Element { |
| 4 |
protected $privateKey = ""; |
| 5 |
protected $publicKey = ""; |
| 6 |
protected $captchaAction = ""; |
| 7 |
protected $spamAction = ""; |
| 8 |
protected $scoreThreshold = 0.5; |
| 9 |
|
| 10 |
/** Whether the badge stylesheet was already printed on this page. */ |
| 11 |
protected static $badgeStylePrinted = false; |
| 12 |
|
| 13 |
public function __construct($label = "", $unused = null, ?array $properties = null) { |
| 14 |
if ($properties === null && is_array($unused)) { |
| 15 |
$properties = $unused; |
| 16 |
} |
| 17 |
parent::__construct($label, "accua-forms-recaptcha3-response", $properties); |
| 18 |
$validator = new AccuaForm_Validation_Captcha3(__("Spam check failed. Please reload the page and retry.", 'contact-forms')); |
| 19 |
$validator->configure(array( |
| 20 |
'privateKey' => $this->privateKey, |
| 21 |
'captchaAction' => $this->captchaAction, |
| 22 |
'spamAction' => $this->spamAction, |
| 23 |
'scoreThreshold' => $this->scoreThreshold, |
| 24 |
)); |
| 25 |
$this->setValidation($validator); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* CSS that hides Google's floating badge, printed once per page. |
| 30 |
* |
| 31 |
* Only the 'hidden' choice needs CSS. 'bottomleft' is a badge parameter |
| 32 |
* Google itself understands (see recaptcha3.js), and api.js then mirrors |
| 33 |
* the strip so the logo sits against the left edge and the text slides |
| 34 |
* out of it. CSS cannot do that: the collapsed badge is the same 256px |
| 35 |
* strip with 186px of it pushed off screen, so moving the whole strip to |
| 36 |
* the left edge leaves the text half on screen instead of the logo. |
| 37 |
* |
| 38 |
* @param string $badge 'hidden' hides the badge, anything else prints nothing. |
| 39 |
* @return string |
| 40 |
*/ |
| 41 |
protected function badgeStyle($badge) { |
| 42 |
if (self::$badgeStylePrinted || $badge !== 'hidden') { |
| 43 |
return ''; |
| 44 |
} |
| 45 |
self::$badgeStylePrinted = true; |
| 46 |
return "<style>.grecaptcha-badge{visibility:hidden!important}</style>\n"; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* The reCAPTCHA notice Google requires when the badge is hidden. |
| 51 |
* |
| 52 |
* @return string |
| 53 |
*/ |
| 54 |
protected function badgeNotice() { |
| 55 |
$notice = strtr( |
| 56 |
// translators: %PRIVACYLINK% and %TERMSLINK% are links to Google's Privacy Policy and Terms of Service |
| 57 |
__('This site is protected by reCAPTCHA and the Google %PRIVACYLINK% and %TERMSLINK% apply.', 'contact-forms'), |
| 58 |
array( |
| 59 |
'%PRIVACYLINK%' => '<a href="https://policies.google.com/privacy" target="_blank" rel="noopener noreferrer">' |
| 60 |
. esc_html__('Privacy Policy', 'contact-forms') . '</a>', |
| 61 |
'%TERMSLINK%' => '<a href="https://policies.google.com/terms" target="_blank" rel="noopener noreferrer">' |
| 62 |
. esc_html__('Terms of Service', 'contact-forms') . '</a>', |
| 63 |
) |
| 64 |
); |
| 65 |
return '<p class="accua-forms-recaptcha3-notice">' . $notice . '</p>' . "\n"; |
| 66 |
} |
| 67 |
|
| 68 |
public function render() { |
| 69 |
wp_enqueue_script( |
| 70 |
'accua-forms-recaptcha3', |
| 71 |
ACCUA_FORMS_DIR_URL . 'assets/js/frontend/recaptcha3.js', |
| 72 |
array('jquery'), |
| 73 |
ACCUA_FORMS_JS_VERSION, |
| 74 |
true |
| 75 |
); |
| 76 |
|
| 77 |
// Site-wide setting, read at render time: element properties do not |
| 78 |
// survive Element::__sleep() on the encrypted-form round trip. The badge |
| 79 |
// travels to the JS loader too: placing it on the left is a parameter of |
| 80 |
// Google's own explicit render, not something CSS can do afterwards. |
| 81 |
$badge = function_exists('accua_forms_recaptcha3_badge') ? accua_forms_recaptcha3_badge() : 'bottomright'; |
| 82 |
$badge_style = $this->badgeStyle($badge); |
| 83 |
$badge_notice = ($badge === 'hidden') ? $this->badgeNotice() : ''; |
| 84 |
|
| 85 |
$js_registration = _accua_forms_json_encode(array( |
| 86 |
$this->attributes["id"], |
| 87 |
array('sitekey' => $this->publicKey, 'action' => $this->captchaAction, 'badge' => $badge), |
| 88 |
$this->form->getLanguage(), |
| 89 |
)); |
| 90 |
$field_id = htmlspecialchars($this->attributes["id"], ENT_QUOTES); |
| 91 |
|
| 92 |
echo <<<EOT |
| 93 |
{$badge_style}<input type="hidden" name="accua-forms-recaptcha3-response" id="{$field_id}" class="accua_forms_recaptcha3_input" value="" /> |
| 94 |
{$badge_notice}<script type="text/javascript"> |
| 95 |
(window.accuaformRecaptcha3Queue = window.accuaformRecaptcha3Queue || []).push( $js_registration ); |
| 96 |
</script> |
| 97 |
EOT; |
| 98 |
} |
| 99 |
} |
| 100 |
|