Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Site_Health
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 3
42
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
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 1
12
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        /**
83         * If threats found.
84         */
85        if ( $total_threats ) {
86            $result['status'] = 'critical';
87            /* translators: $d is the number of threats found. */
88            $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 );
89            $result['description'] = __( 'Jetpack Protect detected the following security threats in your site:', 'jetpack-protect' );
90
91            foreach ( $threats as $threat ) {
92                $result['description'] .= '<p>';
93                $result['description'] .= "<span class='dashicons dashicons-warning' style='color: crimson;'></span> &nbsp";
94                $result['description'] .= wp_kses( $threat, array( 'a' => array( 'href' => array() ) ) ); // Only allow a href HTML tags.
95                $result['description'] .= '</p>';
96            }
97            $result['description'] .= '<p>';
98            $result['description'] .= sprintf(
99                wp_kses(
100                    /* translators: Link to Jetpack Protect. */
101                    __( 'See <a href="%s">Protect overview page</a> for more information.', 'jetpack-protect' ),
102                    array(
103                        'a' => array( 'href' => array() ),
104                    )
105                ),
106                esc_url( admin_url( 'admin.php?page=jetpack-protect' ) )
107            );
108            $result['description'] .= '</p>';
109        }
110
111        return $result;
112    }
113}