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