Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.00% covered (success)
90.00%
63 / 70
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Site_Health
90.00% covered (success)
90.00%
63 / 70
33.33% covered (danger)
33.33%
1 / 3
11.12
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 add_check
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 do_checks
100.00% covered (success)
100.00%
63 / 63
100.00% covered (success)
100.00%
1 / 1
8
1<?php
2/**
3 * Class to handle the Check in the Site Health admin page
4 *
5 * @package automattic/jetpack-protect-plugin
6 */
7
8namespace Automattic\Jetpack\Protect;
9
10use Automattic\Jetpack\Protect_Status\Status;
11
12/**
13 * Site_Health.
14 *
15 * Displays threats in WordPress site health page.
16 */
17class Site_Health {
18
19    /**
20     * Initialize hooks
21     *
22     * @access public
23     * @return void
24     */
25    public static function init() {
26        if ( ! has_filter( 'site_status_tests', array( __CLASS__, 'add_check' ) ) ) {
27            add_filter( 'site_status_tests', array( __CLASS__, 'add_check' ), 99 );
28        }
29    }
30
31    /**
32     * Add site-health page tests.
33     *
34     * @param array $checks Core checks.
35     *
36     * @access public
37     * @return array
38     */
39    public static function add_check( $checks ) {
40        $checks['direct']['jetpack_protect_checks'] = array(
41            'label' => __( 'Jetpack Protect checks', 'jetpack-protect' ),
42            'test'  => array( __CLASS__, 'do_checks' ),
43        );
44
45        return $checks;
46    }
47
48    /**
49     * Do site-health page checks
50     *
51     * @access public
52     * @return array
53     */
54    public static function do_checks() {
55        $total_threats = Status::get_total_threats();
56        $threats       = Status::get_all_threats();
57        $threats       = array_map(
58            function ( $v ) {
59                return $v->title;
60            },
61            $threats
62        );
63
64        /**
65         * Default, no threats found
66         */
67        $result = array(
68            'label'       => __( 'No known threats found', 'jetpack-protect' ),
69            'status'      => 'good',
70            'badge'       => array(
71                'label' => __( 'Security', 'jetpack-protect' ),
72                'color' => 'gray',
73            ),
74            'description' => sprintf(
75                '<p>%s</p>',
76                __( 'Jetpack Protect did not find any known threats in your site. Threats can be exploited by hackers and cause harm to your website.', 'jetpack-protect' )
77            ),
78            'actions'     => '',
79            'test'        => 'jetpack_protect_checks',
80        );
81
82        $status = Status::get_status();
83        // An unconnected site has no results to load, so it keeps the default result.
84        $fetch_failed = ! empty( $status->error ) && 'site_not_connected' !== $status->error_code;
85        if ( ! $total_threats && ( $fetch_failed || ! empty( $status->last_scan_failed ) ) ) {
86            if ( $fetch_failed ) {
87                $result['label']       = __( 'Jetpack Protect couldn’t check your site for threats', 'jetpack-protect' );
88                $result['description'] = sprintf(
89                    '<p>%s</p>',
90                    esc_html__( 'We couldn’t load your latest scan results, so we can’t confirm your site is free of threats.', 'jetpack-protect' )
91                );
92            } else {
93                $result['label']       = __( 'Your last Jetpack Protect scan didn’t complete', 'jetpack-protect' );
94                $result['description'] = sprintf(
95                    '<p>%s</p>',
96                    esc_html__( 'We can’t confirm your site is free of threats until a scan finishes.', 'jetpack-protect' )
97                );
98            }
99            $result['status']  = 'recommended';
100            $result['actions'] = sprintf(
101                '<p><a href="%s">%s</a></p>',
102                esc_url( admin_url( 'admin.php?page=jetpack-protect' ) ),
103                esc_html__( 'Open Jetpack Protect', 'jetpack-protect' )
104            );
105        }
106
107        /**
108         * If threats found.
109         */
110        if ( $total_threats ) {
111            $result['status'] = 'critical';
112            /* translators: $d is the number of threats found. */
113            $result['label']       = sprintf( _n( 'Your site is affected by %d security threat', 'Your site is affected by %d security threats', $total_threats, 'jetpack-protect' ), $total_threats );
114            $result['description'] = __( 'Jetpack Protect detected the following security threats in your site:', 'jetpack-protect' );
115
116            foreach ( $threats as $threat ) {
117                $result['description'] .= '<p>';
118                $result['description'] .= "<span class='dashicons dashicons-warning' style='color: crimson;'></span> &nbsp";
119                $result['description'] .= wp_kses( $threat, array( 'a' => array( 'href' => array() ) ) ); // Only allow a href HTML tags.
120                $result['description'] .= '</p>';
121            }
122            $result['description'] .= '<p>';
123            $result['description'] .= sprintf(
124                wp_kses(
125                    /* translators: Link to Jetpack Protect. */
126                    __( 'See <a href="%s">Protect overview page</a> for more information.', 'jetpack-protect' ),
127                    array(
128                        'a' => array( 'href' => array() ),
129                    )
130                ),
131                esc_url( admin_url( 'admin.php?page=jetpack-protect' ) )
132            );
133            $result['description'] .= '</p>';
134        }
135
136        return $result;
137    }
138}