Completed
Push — add/scan-threat-notifications ( 91d713...f75b67 )
by
unknown
37:11 queued 30:07
created

Admin_Bar_Notice::init_hooks()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * A class that adds the scan notice to the admin bar.
4
 *
5
 * @package automattic/jetpack
6
 */
7
8
namespace Automattic\Jetpack\Scan;
9
10
use function Automattic\Jetpack\enqueue_async_script as jetpack_enqueue_async_script;
11
use Automattic\Jetpack\Redirect;
12
13
/**
14
 * Class Main
15
 *
16
 * Responsible for loading the admin bar notice if threats are found.
17
 *
18
 * @package Automattic\Jetpack\Scan
19
 */
20
class Admin_Bar_Notice {
21
	const SCRIPT_NAME    = 'jetpack-scan-show-notice';
22
	const SCRIPT_VERSION = '1';
23
24
	/**
25
	 * The singleton instance of this class.
26
	 *
27
	 * @var Admin_Bar_Notice
28
	 */
29
	protected static $instance;
30
31
	/**
32
	 * Get the singleton instance of the class.
33
	 *
34
	 * @return Admin_Bar_Notice
35
	 */
36
	public static function instance() {
37
		if ( ! isset( self::$instance ) ) {
38
			self::$instance = new Admin_Bar_Notice();
39
			self::$instance->init_hooks();
40
		}
41
42
		return self::$instance;
43
	}
44
	/**
45
	 * Initalize the hooks as needed.
46
	 */
47
	private function init_hooks() {
48
		if ( is_multisite() ) {
49
			return; // Jetpack Scan is currently not supported on multisite.
50
		}
51
52
		if ( ! current_user_can( 'manage_options' ) ) {
53
			return; // Only show the notice to admins.
54
		}
55
56
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_toolbar_script' ) );
57
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_toolbar_script' ) );
58
		add_action( 'admin_bar_menu', array( $this, 'add_threats_to_toolbar' ), 999 );
59
	}
60
61
	/**
62
	 * Add the inline styles and scripts if they are needed.
63
	 */
64
	public function enqueue_toolbar_script() {
65
		$this->add_inline_styles();
66
67
		if ( ! is_null( $this->has_threats() ) ) {
68
			return;
69
		}
70
71
		// We don't know about threats in the cache lets load the JS that fetches the info and updates the admin bar.
72
		jetpack_enqueue_async_script( self::SCRIPT_NAME, '_inc/build/scan/admin-bar-notice.min.js', 'modules/scan/admin-bar-notice.js', array(), self::SCRIPT_VERSION, true );
0 ignored issues
show
Documentation introduced by
self::SCRIPT_VERSION is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73
74
		$script_data = array(
75
			'nonce'              => wp_create_nonce( 'wp_rest' ),
76
			'scan_endpoint'      => get_rest_url( null, 'jetpack/v4/scan' ),
77
			'scan_dashboard_url' => Redirect::get_url( 'calypso-scanner' ),
78
			/* translators: %s is the alert icon */
79
			'singular'           => sprintf( esc_html__( '%s Threat found', 'jetpack' ), $this->get_icon() ),
80
			/* translators: %s is the alert icon */
81
			'multiple'           => sprintf( esc_html__( '%s Threats found', 'jetpack' ), $this->get_icon() ),
82
		);
83
		wp_localize_script( self::SCRIPT_NAME, 'Jetpack_Scan', $script_data );
84
	}
85
86
	/**
87
	 * Adds the inline styles if they are needed.
88
	 */
89
	public function add_inline_styles() {
90
		// We know there are no threats so lets not include any css.
91
		if ( false === $this->has_threats() ) {
92
			return;
93
		}
94
95
		// We might be showing the threats in the admin bar lets make sure that they look great!
96
		$style = '#wp-admin-bar-jetpack-scan-notice svg { float:left; margin-top: 4px; margin-right: 6px; width: 18px; height: 22px; }';
97
		if ( is_rtl() ) {
98
			$style = '#wp-admin-bar-jetpack-scan-notice svg { float:right; margin-top: 4px; margin-left: 6px; width: 18px; height: 22px; }';
99
		}
100
		wp_add_inline_style( 'admin-bar', $style );
101
	}
102
103
	/**
104
	 * Add the link to the admin bar.
105
	 *
106
	 * @param WP_Admin_Bar $wp_admin_bar WP Admin Bar class object.
107
	 */
108
	public function add_threats_to_toolbar( $wp_admin_bar ) {
109
		$has_threats = $this->has_threats();
110
		if ( false === $has_threats ) {
111
			return;
112
		}
113
		$node = array(
114
			'id'     => 'jetpack-scan-notice',
115
			'title'  => '',
116
			'parent' => 'top-secondary',
117
			'meta'   => array(
118
				'title' => esc_attr__( 'Visit your scan dashboard', 'jetpack' ),
119
				'class' => 'error',
120
			),
121
		);
122
123
		// No need to do anything...
124
		if ( $has_threats ) {
125
			/* translators: %s is the alert icon */
126
			$node['title']           = sprintf( esc_html__( '%s Threats found', 'jetpack' ), $this->get_icon() );
127
			$node['href']            = esc_url( Redirect::get_url( 'calypso-scanner' ) );
128
			$node['meta']['onclick'] = 'window.open( this.href ); return false;';
129
		}
130
131
		$wp_admin_bar->add_node( $node );
132
	}
133
134
	/**
135
	 * Returns the shield icon.
136
	 *
137
	 * @return string
138
	 */
139
	private function get_icon() {
140
		return '<svg width="18" height="22" viewBox="0 0 18 22" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M9 0L0 4V10C0 15.55 3.84 20.74 9 22C14.16 20.74 18 15.55 18 10V4L9 0Z" fill="#D63638"/><path d="M7.99121 6.00894H10.0085V11.9968H7.99121V6.00894Z" fill="#FFF"/><path d="M7.99121 14.014H10.0085V15.9911H7.99121V14.014Z" fill="#FFF"/></svg>';
141
	}
142
143
	/**
144
	 *
145
	 * Return Whether boolean cached threats exist or null if the state is unknown.
146
	 * * @return boolean or null
147
	 */
148
	public function has_threats() {
149
		$scan_state = get_site_transient( 'jetpack_scan_state' );
150
		if ( empty( $scan_state ) ) {
151
			return null;
152
		}
153
154
		// Return true if there is at least one threat found.
155
		return (bool) isset( $scan_state->threats[0] );
156
	}
157
}
158