Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.82% covered (warning)
81.82%
18 / 22
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Login_Protection
90.00% covered (success)
90.00%
18 / 20
33.33% covered (danger)
33.33%
1 / 3
6.04
0.00% covered (danger)
0.00%
0 / 1
 get_key
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_state
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
4
 register_routes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Protect dashboard: the Login protection section.
4 *
5 * @package automattic/jetpack-protect
6 */
7
8namespace Automattic\Jetpack\Protect\Sections;
9
10use Automattic\Jetpack\Connection\Manager as Connection_Manager;
11use Automattic\Jetpack\Connection\SSO\Helpers as SSO_Helpers;
12use Automattic\Jetpack\IP\Utils as IP_Utils;
13use Automattic\Jetpack\Protect\Dashboard;
14use Automattic\Jetpack\Protect\Dashboard_Section;
15use Automattic\Jetpack\Status;
16
17if ( ! defined( 'ABSPATH' ) ) {
18    exit( 0 );
19}
20
21/**
22 * Brute force protection, account protection and WordPress.com login.
23 *
24 * @since $$next-version$$
25 */
26class Login_Protection implements Dashboard_Section {
27
28    /**
29     * The key the section's state is printed under.
30     *
31     * @return string
32     */
33    public function get_key() {
34        return 'loginProtection';
35    }
36
37    /**
38     * Each login protection method's state, the blocked attempt count and the visitor's IP.
39     *
40     * @return array
41     */
42    public function get_state() {
43        $is_offline = ( new Status() )->is_offline_mode();
44
45        return array(
46            'bruteForce'        => Dashboard::get_module_state( 'protect' ),
47            'accountProtection' => Dashboard::get_module_state( 'account-protection' ),
48            'sso'               => Dashboard::get_module_state( 'sso' ),
49            'blockedCount'      => number_format_i18n( (int) get_site_option( 'jetpack_protect_blocked_attempts', 0 ) ),
50            // WordPress.com login needs a connected owner and doesn't work in offline mode.
51            'ssoUsable'         => ! $is_offline && ( new Connection_Manager( 'jetpack-protect-pkg' ) )->has_connected_owner(),
52            'isOffline'         => $is_offline,
53            // A filter or constant can force these, and Jetpack Settings then locks the toggle.
54            'ssoLocks'          => array(
55                'matchByEmail' => method_exists( SSO_Helpers::class, 'is_match_by_email_checkbox_disabled' ) && SSO_Helpers::is_match_by_email_checkbox_disabled(),
56                'twoStep'      => method_exists( SSO_Helpers::class, 'is_require_two_step_checkbox_disabled' ) && SSO_Helpers::is_require_two_step_checkbox_disabled(),
57            ),
58            'ssoEffective'      => array(
59                'matchByEmail' => SSO_Helpers::match_by_email(),
60                'twoStep'      => SSO_Helpers::is_two_step_required(),
61            ),
62            'currentIp'         => (string) IP_Utils::get_ip(),
63        );
64    }
65
66    /**
67     * The section uses the core Jetpack settings endpoints, so it has no routes of its own.
68     *
69     * @return void
70     */
71    public function register_routes() {}
72}