PluginProbe
Contact Forms by Cimatti / 2.3.0
Contact Forms by Cimatti v2.3.0
2.3.6 2.3.5 2.3.0 2.2.32 2.2.4 2.2.0 2.1.2 2.1.1 trunk 1.0 1.1 1.2 1.2.1 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 All 62 releases
contact-forms / classes / Element / Captcha3.php

Captcha3.php in Contact Forms by Cimatti 2.3.0, at classes/Element/Captcha3.php

101 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 repositions or hides Google's floating badge, printed once per
30 * page. The badge is injected into <body> by api.js with inline styles, so
31 * every rule needs !important; on the left it keeps the collapse/expand
32 * behaviour by animating a transform instead of Google's own `right`.
33 *
34 * @param string $badge 'bottomleft' or 'hidden' ('bottomright' prints nothing).
35 * @return string
36 */
37 protected function badgeStyle($badge) {
38 if (self::$badgeStylePrinted || ($badge !== 'bottomleft' && $badge !== 'hidden')) {
39 return '';
40 }
41 self::$badgeStylePrinted = true;
42 if ($badge === 'hidden') {
43 $css = '.grecaptcha-badge{visibility:hidden!important}';
44 } else {
45 $css = '.grecaptcha-badge{left:4px!important;right:auto!important;'
46 . 'transform:translateX(-186px)!important;transition:transform .3s ease!important}'
47 . '.grecaptcha-badge:hover,.grecaptcha-badge:focus-within{transform:translateX(0)!important}';
48 }
49 return "<style>{$css}</style>\n";
50 }
51
52 /**
53 * The reCAPTCHA notice Google requires when the badge is hidden.
54 *
55 * @return string
56 */
57 protected function badgeNotice() {
58 $notice = strtr(
59 // translators: %PRIVACYLINK% and %TERMSLINK% are links to Google's Privacy Policy and Terms of Service
60 __('This site is protected by reCAPTCHA and the Google %PRIVACYLINK% and %TERMSLINK% apply.', 'contact-forms'),
61 array(
62 '%PRIVACYLINK%' => '<a href="https://policies.google.com/privacy" target="_blank" rel="noopener noreferrer">'
63 . esc_html__('Privacy Policy', 'contact-forms') . '</a>',
64 '%TERMSLINK%' => '<a href="https://policies.google.com/terms" target="_blank" rel="noopener noreferrer">'
65 . esc_html__('Terms of Service', 'contact-forms') . '</a>',
66 )
67 );
68 return '<p class="accua-forms-recaptcha3-notice">' . $notice . '</p>' . "\n";
69 }
70
71 public function render() {
72 wp_enqueue_script(
73 'accua-forms-recaptcha3',
74 ACCUA_FORMS_DIR_URL . 'assets/js/frontend/recaptcha3.js',
75 array('jquery'),
76 ACCUA_FORMS_JS_VERSION,
77 true
78 );
79
80 $js_registration = _accua_forms_json_encode(array(
81 $this->attributes["id"],
82 array('sitekey' => $this->publicKey, 'action' => $this->captchaAction),
83 $this->form->getLanguage(),
84 ));
85 $field_id = htmlspecialchars($this->attributes["id"], ENT_QUOTES);
86
87 // Site-wide setting, read at render time: element properties do not
88 // survive Element::__sleep() on the encrypted-form round trip.
89 $badge = function_exists('accua_forms_recaptcha3_badge') ? accua_forms_recaptcha3_badge() : 'bottomright';
90 $badge_style = $this->badgeStyle($badge);
91 $badge_notice = ($badge === 'hidden') ? $this->badgeNotice() : '';
92
93 echo <<<EOT
94 {$badge_style}<input type="hidden" name="accua-forms-recaptcha3-response" id="{$field_id}" class="accua_forms_recaptcha3_input" value="" />
95 {$badge_notice}<script type="text/javascript">
96 (window.accuaformRecaptcha3Queue = window.accuaformRecaptcha3Queue || []).push( $js_registration );
97 </script>
98 EOT;
99 }
100 }
101