Debian Codegraph @

extensions/ConfirmEdit/Turnstile/includes/HTMLTurnstileField.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\ConfirmEdit\Turnstile;
4
5 use MediaWiki\Html\Html;
6 use MediaWiki\HTMLForm\HTMLFormField;
7
8 /**
9 * Creates a Turnstile widget. Does not return any data; handling the data submitted by the
10 * widget is callers' responsibility.
11 */
12 class HTMLTurnstileField extends HTMLFormField {
13 /** @var string Public key parameter to be passed to Turnstile. */
14 protected $key;
15
16 /** @var string Error returned by Turnstile in the previous round. */
17 protected $error;
18
19 /**
20 * Parameters:
21 * - key: (string, required) Turnstile public key
22 * - error: (string) Turnstile error from previous round
23 * @param array $params
24 */
25 public function __construct( array $params ) {
26 $params += [ 'error' => null ];
27 parent::__construct( $params );
28
29 $this->key = $params['key'];
30 $this->error = $params['error'];
31
32 $this->mName = 'cf-turnstile-response';
33 }
34
35 /**
36 * @inheritDoc
37 */
38 public function getInputHTML( $value ) {
39 $out = $this->mParent->getOutput();
40 $lang = htmlspecialchars( urlencode( $this->mParent->getLanguage()->getCode() ) );
41
42 // Insert Turnstile script, in display language, if available.
43 // Language falls back to the browser's display language.
44 // See https://developers.cloudflare.com/turnstile/reference/supported-languages/
45 $out->addHeadItem(
46 'cf-turnstile-script',
47 "<script src=\"https://challenges.cloudflare.com/turnstile/v0/api.js?language={$lang}\" async defer>
48 </script>"
49 );
50 Turnstile::addCSPSources( $out->getCSP() );
51
52 return Html::element( 'div', [
53 'class' => [
54 'cf-turnstile',
55 'mw-confirmedit-captcha-fail' => (bool)$this->error,
56 ],
57 'data-sitekey' => $this->key,
58 ] );
59 }
60 }