Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 60
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ConfirmEmailHooks
0.00% covered (danger)
0.00%
0 / 60
0.00% covered (danger)
0.00%
0 / 3
90
0.00% covered (danger)
0.00%
0 / 1
 isConfirmEmailEnabled
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onAuthChangeFormFields
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
42
 onUserSendConfirmationMail
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace GrowthExperiments;
4
5use MediaWiki\Auth\AuthManager;
6use MediaWiki\Context\RequestContext;
7use MediaWiki\Extension\ConfirmEdit\FancyCaptcha\HTMLFancyCaptchaField;
8use MediaWiki\Html\Html;
9use MediaWiki\MediaWikiServices;
10use MediaWiki\Message\Message;
11use MediaWiki\SpecialPage\Hook\AuthChangeFormFieldsHook;
12use MediaWiki\User\Hook\UserSendConfirmationMailHook;
13use MediaWiki\User\User;
14
15class ConfirmEmailHooks implements AuthChangeFormFieldsHook, UserSendConfirmationMailHook {
16
17    /**
18     * @return bool Whether the email confirmation improvements are enabled
19     */
20    public static function isConfirmEmailEnabled() {
21        return MediaWikiServices::getInstance()->getMainConfig()->get( 'GEConfirmEmailEnabled' );
22    }
23
24    /**
25     * AuthChangeFormFields hook
26     * @param array $requests
27     * @param array $fieldInfo
28     * @param array &$formDescriptor HTMLForm form descriptor
29     * @param string $action
30     */
31    public function onAuthChangeFormFields(
32        $requests, $fieldInfo, &$formDescriptor, $action
33    ) {
34        if ( !self::isConfirmEmailEnabled() ) {
35            return;
36        }
37        if ( !in_array( $action, [
38            AuthManager::ACTION_CREATE,
39            AuthManager::ACTION_CREATE_CONTINUE
40        ] ) ) {
41            return;
42        }
43
44        $config = MediaWikiServices::getInstance()->getMainConfig();
45        $context = RequestContext::getMain();
46
47        // Load JS that displays a message informing the user that a verification email is coming
48        $context->getOutput()->addModules( 'ext.growthExperiments.Account' );
49        $context->getOutput()->addJsConfigVars( 'confirmemail', true );
50
51        // If email field exists on the form, change email label from "(optional)" to "
52        // (recommended)", but only if email is optional
53        if ( isset( $formDescriptor['email'] ) && !$config->get( 'EmailConfirmToEdit' ) ) {
54            $formDescriptor['email']['label-message'] = 'growthexperiments-confirmemail-emailrecommended';
55            $formDescriptor['email']['help-message'] = 'growthexperiments-confirmemail-emailhelp';
56            // helpInline doesn't work because this form hasn't been converted to OOUI yet (T85853)
57            $formDescriptor['email']['helpInline'] = true;
58        }
59
60        if ( ( $formDescriptor['captchaWord']['class'] ?? null ) === HTMLFancyCaptchaField::class ) {
61            // Remove long-winded CAPTCHA explanation message
62            unset( $formDescriptor['captchaWord']['label-message'] );
63
64            // HACK: add "what's this" link by duplicating the entire label, then hiding
65            // the original label with CSS. Unfortunately HTMLFancyCaptchaField doesn't let us change
66            // the built-in label message or append to it, instead it prepends it in a <p>.
67            $formDescriptor['captchaWord']['label-raw'] = Html::rawElement(
68                'label',
69                [ 'for' => 'mw-input-captchaWord' ],
70                $context->msg( 'captcha-label' )->escaped() . ' ' .
71                    $context->msg( 'fancycaptcha-captcha' )->escaped() . ' ' .
72                    $context->msg( 'growthexperiments-confirmemail-captcha-help' )->parse()
73            );
74            $formDescriptor['captchaWord']['cssclass'] =
75                ( $formDescriptor['capthaWord']['cssclass'] ?? '' ) . ' mw-ge-confirmemail-captcha';
76
77            $context->getOutput()->addModuleStyles(
78                'ext.growthExperiments.Account.styles'
79            );
80        }
81    }
82
83    /**
84     * Override confirmation email
85     * @param User $user
86     * @param array &$mail
87     * @param array $info
88     */
89    public function onUserSendConfirmationMail( $user, &$mail, $info ) {
90        if ( !self::isConfirmEmailEnabled() ) {
91            return;
92        }
93        $lang = RequestContext::getMain()->getLanguage();
94        $config = MediaWikiServices::getInstance()->getMainConfig();
95
96        // TODO different messages for different $info['type'] values?
97        $textParams = [
98            $info['ip'],
99            $user->getName(),
100            $info['confirmURL'],
101            $lang->userTimeAndDate( $info['expiration'], $user ),
102            $info['invalidateURL']
103        ];
104        $htmlParams = $textParams;
105        $htmlParams[2] = Message::rawParam( Html::element( 'a',
106            [
107                'href' => $info['confirmURL'],
108                'style' => 'background: #36C; color: white; padding: 0.45em 0.6em; font-weight: bold; ' .
109                    'text-align: center; text-decoration: none;'
110            ],
111            wfMessage( 'growthexperiments-confirmemail-confirm-button' )->text()
112        ) );
113        $logoImage = Html::rawElement( 'p', [],
114            Html::element( 'img', [ 'src' => wfExpandUrl( $config->get( 'Logo' ), PROTO_CANONICAL ) ] )
115        );
116
117        $mail['subject'] = wfMessage( 'growthexperiments-confirmemail-confirm-subject' )->text();
118        $mail['body'] = [
119            'text' => wfMessage( 'growthexperiments-confirmemail-confirm-body-plaintext' )
120                ->params( $textParams )->text(),
121            'html' => $logoImage . wfMessage( 'growthexperiments-confirmemail-confirm-body-html' )
122                ->params( $htmlParams )->parse()
123        ];
124    }
125}