Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 65
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Notices
0.00% covered (danger)
0.00%
0 / 65
0.00% covered (danger)
0.00%
0 / 10
156
0.00% covered (danger)
0.00%
0 / 1
 error_msg_enable_two_step
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
2
 error_msg_email_already_exists
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 error_msg_identity_crisis
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 error_invalid_response_data
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 error_unable_to_create_user
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 disable_default_login_form
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 msg_login_by_jetpack
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 get_sso_required_message
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 cant_find_user
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 sso_not_allowed_in_staging
n/a
0 / 0
n/a
0 / 0
1
 sso_not_allowed_in_safe_mode
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * A collection of helper functions used in the SSO module.
4 *
5 * @package automattic/jetpack-connection
6 */
7
8namespace Automattic\Jetpack\Connection\SSO;
9
10use Automattic\Jetpack\Redirect;
11use WP_Error;
12use WP_User;
13
14/**
15 * A collection of helper functions used in the SSO module.
16 *
17 * @since jetpack-4.4.0
18 */
19class Notices {
20    /**
21     * Error message displayed on the login form when two step is required and
22     * the user's account on WordPress.com does not have two step enabled.
23     *
24     * @since jetpack-2.7
25     * @param string $message Error message.
26     * @return string
27     **/
28    public static function error_msg_enable_two_step( $message ) {
29        $error = sprintf(
30            wp_kses(
31            /* translators: URL to settings page */
32                __(
33                    'Two-Step Authentication is required to access this site. Please visit your <a href="%1$s" rel="noopener noreferrer" target="_blank">Security Settings</a> to configure <a href="%2$s" rel="noopener noreferrer" target="_blank">Two-step Authentication</a> for your account.',
34                    'jetpack-connection'
35                ),
36                array( 'a' => array( 'href' => array() ) )
37            ),
38            Redirect::get_url( 'calypso-me-security-two-step' ),
39            Redirect::get_url( 'wpcom-support-security-two-step-authentication' )
40        );
41
42        $message .= sprintf( '<p class="message" id="login_error">%s</p>', $error );
43
44        return $message;
45    }
46
47    /**
48     * Error message displayed when the user tries to SSO, but match by email
49     * is off and they already have an account with their email address on
50     * this site.
51     *
52     * @param string $message Error message.
53     * @return string
54     */
55    public static function error_msg_email_already_exists( $message ) {
56        $error = sprintf(
57            wp_kses(
58            /* translators: login URL */
59                __(
60                    'You already have an account on this site. Please <a href="%1$s">sign in</a> with your username and password and then connect to WordPress.com.',
61                    'jetpack-connection'
62                ),
63                array( 'a' => array( 'href' => array() ) )
64            ),
65            esc_url_raw( add_query_arg( 'jetpack-sso-show-default-form', '1', wp_login_url() ) )
66        );
67
68        $message .= sprintf( '<p class="message" id="login_error">%s</p>', $error );
69
70        return $message;
71    }
72
73    /**
74     * Error message that is displayed when the current site is in an identity crisis and SSO cannot be used.
75     *
76     * @since jetpack-4.3.2
77     *
78     * @param string $message Error Message.
79     *
80     * @return string
81     */
82    public static function error_msg_identity_crisis( $message ) {
83        $error    = esc_html__( 'Logging in with WordPress.com is not currently available because this site is experiencing connection problems.', 'jetpack-connection' );
84        $message .= sprintf( '<p class="message" id="login_error">%s</p>', $error );
85        return $message;
86    }
87
88    /**
89     * Error message that is displayed when we are not able to verify the SSO nonce due to an XML error or
90     * failed validation. In either case, we prompt the user to try again or log in with username and password.
91     *
92     * @since jetpack-4.3.2
93     *
94     * @param string $message Error message.
95     *
96     * @return string
97     */
98    public static function error_invalid_response_data( $message ) {
99        $error    = esc_html__(
100            'There was an error logging you in via WordPress.com, please try again or try logging in with your username and password.',
101            'jetpack-connection'
102        );
103        $message .= sprintf( '<p class="message" id="login_error">%s</p>', $error );
104        return $message;
105    }
106
107    /**
108     * Error message that is displayed when we were not able to automatically create an account for a user
109     * after a user has logged in via SSO. By default, this message is triggered after trying to create an account 5 times.
110     *
111     * @since jetpack-4.3.2
112     *
113     * @param string $message Error message.
114     *
115     * @return string
116     */
117    public static function error_unable_to_create_user( $message ) {
118        $error    = esc_html__(
119            'There was an error creating a user for you. Please contact the administrator of your site.',
120            'jetpack-connection'
121        );
122        $message .= sprintf( '<p class="message" id="login_error">%s</p>', $error );
123        return $message;
124    }
125
126    /**
127     * When the default login form is hidden, this method is called on the 'authenticate' filter with a priority of 30.
128     * This method disables the ability to submit the default login form.
129     *
130     * @param WP_User|WP_Error $user Either the user attempting to login or an existing authentication failure.
131     *
132     * @return WP_Error
133     */
134    public static function disable_default_login_form( $user ) {
135        if ( is_wp_error( $user ) ) {
136            return $user;
137        }
138
139        /**
140         * Since we're returning an error that will be shown as a red notice, let's remove the
141         * informational "blue" notice.
142         */
143        remove_filter( 'login_message', array( static::class, 'msg_login_by_jetpack' ) );
144        return new WP_Error( 'jetpack_sso_required', self::get_sso_required_message() );
145    }
146
147    /**
148     * Message displayed when the site admin has disabled the default WordPress
149     * login form in Settings > General > Secure Sign On
150     *
151     * @since jetpack-2.7
152     * @param string $message Error message.
153     *
154     * @return string
155     **/
156    public static function msg_login_by_jetpack( $message ) {
157        $message .= sprintf( '<p class="message">%s</p>', self::get_sso_required_message() );
158        return $message;
159    }
160
161    /**
162     * Get the message for SSO required.
163     *
164     * @return string
165     */
166    public static function get_sso_required_message() {
167        $msg = esc_html__(
168            'A WordPress.com account is required to access this site. Click the button below to sign in or create a free WordPress.com account.',
169            'jetpack-connection'
170        );
171
172        /**
173         * Filter the message displayed when the default WordPress login form is disabled.
174         *
175         * @module sso
176         *
177         * @since jetpack-2.8.0
178         *
179         * @param string $msg Disclaimer when default WordPress login form is disabled.
180         */
181        return apply_filters( 'jetpack_sso_disclaimer_message', $msg );
182    }
183
184    /**
185     * Message displayed when the user cannot be found after approving the SSO process on WordPress.com
186     *
187     * @param string $message Error message.
188     *
189     * @return string
190     */
191    public static function cant_find_user( $message ) {
192        $error = __(
193            "We couldn't find your account. If you already have an account, make sure you have connected to WordPress.com.",
194            'jetpack-connection'
195        );
196
197        /**
198         * Filters the "couldn't find your account" notice after an attempted SSO.
199         *
200         * @module sso
201         *
202         * @since jetpack-10.5.0
203         *
204         * @param string $error Error text.
205         */
206        $error = apply_filters( 'jetpack_sso_unknown_user_notice', $error );
207
208        $message .= sprintf( '<p class="message" id="login_error">%s</p>', esc_html( $error ) );
209
210        return $message;
211    }
212
213    /**
214     * Error message that is displayed when the current site is in an identity crisis and SSO cannot be used.
215     *
216     * @since jetpack-4.4.0
217     * @deprecated since 2.10.0
218     *
219     * @param string $message Error message.
220     *
221     * @return string
222     */
223    public static function sso_not_allowed_in_staging( $message ) {
224        _deprecated_function( __FUNCTION__, '2.10.0', 'sso_not_allowed_in_safe_mode' );
225        $error = __(
226            'Logging in with WordPress.com is disabled for sites that are in staging mode.',
227            'jetpack-connection'
228        );
229
230        /**
231         * Filters the disallowed notice for staging sites attempting SSO.
232         *
233         * @module sso
234         *
235         * @since jetpack-10.5.0
236         *
237         * @param string $error Error text.
238         */
239        $error    = apply_filters_deprecated( 'jetpack_sso_disallowed_staging_notice', array( $error ), '2.9.1', 'jetpack_sso_disallowed_safe_mode_notice' );
240        $message .= sprintf( '<p class="message">%s</p>', esc_html( $error ) );
241        return $message;
242    }
243
244    /**
245     * Error message that is displayed when the current site is in an identity crisis and SSO cannot be used.
246     *
247     * @since 2.10.0
248     *
249     * @param string $message Error message.
250     *
251     * @return string
252     */
253    public static function sso_not_allowed_in_safe_mode( $message ) {
254        $error = __(
255            'Logging in with WordPress.com is disabled for sites that are in safe mode.',
256            'jetpack-connection'
257        );
258
259        /**
260         * Filters the disallowed notice for sites in safe mode attempting SSO.
261         *
262         * @module sso
263         *
264         * @since 2.10.0
265         *
266         * @param string $error Error text.
267         */
268        $error    = apply_filters( 'jetpack_sso_disallowed_safe_mode_notice', $error );
269        $message .= sprintf( '<p class="message">%s</p>', esc_html( $error ) );
270        return $message;
271    }
272}