Completed
Push — test/e2e-playwright-migration ( 3230cd )
by
unknown
08:23
created

Admin_Sidebar_Link::get_link_offset()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/**
3
 * A class that adds a scan and backup link to the admin sidebar.
4
 *
5
 * @package automattic/jetpack
6
 */
7
8
namespace Automattic\Jetpack\Scan;
9
10
use Automattic\Jetpack\Redirect;
11
use Jetpack_Core_Json_Api_Endpoints;
12
13
/**
14
 * Class Main
15
 *
16
 * Responsible for showing the link if available.
17
 *
18
 * @package Automattic\Jetpack\Scan
19
 */
20
class Admin_Sidebar_Link {
21
22
	const SCHEDULE_ACTION_HOOK = 'jetpack_scan_refresh_states_event';
23
24
	/**
25
	 * The singleton instance of this class.
26
	 *
27
	 * @var Admin_Sidebar_Link
28
	 */
29
	protected static $instance;
30
31
	/**
32
	 * Used to check if we need to schedule the refresh or we need to do it.
33
	 *
34
	 * @var boolean | null
35
	 */
36
	private $schedule_refresh_checked;
37
38
	/**
39
	 * Get the singleton instance of the class.
40
	 *
41
	 * @return Admin_Sidebar_Link
42
	 */
43
	public static function instance() {
44
		if ( ! isset( self::$instance ) ) {
45
			self::$instance = new Admin_Sidebar_Link();
46
			self::$instance->init_hooks();
47
		}
48
49
		return self::$instance;
50
	}
51
52
	/**
53
	 * Adds action hooks.
54
	 */
55
	public function init_hooks() {
56
		add_action( 'jetpack_admin_menu', array( $this, 'maybe_add_admin_link' ), 99 );
57
		add_action( self::SCHEDULE_ACTION_HOOK, array( $this, 'refresh_state_cache' ) );
58
	}
59
60
	/**
61
	 * Adds a link to the Scan and Backup page.
62
	 */
63
	public function maybe_add_admin_link() {
64
		if ( ! $this->should_show_link() ) {
65
			return;
66
		}
67
68
		$new_link = $this->get_new_link();
69
70
		// Splice the nav menu item into the Jetpack nav.
71
		global $submenu;
72
		array_splice( $submenu['jetpack'], $this->get_link_offset(), 0, array( $new_link ) );
73
	}
74
75
	/**
76
	 * Retuns the new link.
77
	 *
78
	 *  @return array Link array to be added to the sidebar.
79
	 */
80
	private function get_new_link() {
81
		$has_scan   = $this->has_scan();
82
		$has_backup = $this->has_backup();
83
84
		$url = Redirect::get_url( 'calypso-backups' );
85
		if ( $has_scan && ! $has_backup ) {
86
			$menu_label = __( 'Scan', 'jetpack' );
87
			$url        = Redirect::get_url( 'calypso-scanner' );
88
		} elseif ( ! $has_scan && $has_backup ) {
89
			$menu_label = __( 'Backup', 'jetpack' );
90
		} else {
91
			// Will be both, as the code won't get this far if neither is true.
92
			$menu_label = __( 'Backup & Scan', 'jetpack' );
93
		}
94
95
		return array(
96
			esc_html( $menu_label ) . ' <span class="dashicons dashicons-external"></span>',
97
			'manage_options', // Check permissions here.
98
			esc_url( $url ),
99
		);
100
101
	}
102
103
	/**
104
	 * We create a menu offset by counting all the pages that have a jetpack_admin_page set as the link.
105
	 *
106
	 * This makes it so that the highlight of the pages works as expected. When you click on the Setting or Dashboard.
107
	 *
108
	 * @return int Menu offset.
109
	 */
110
	private function get_link_offset() {
111
		global $submenu;
112
		$offset = 0;
113
		foreach ( $submenu['jetpack'] as $link ) {
114
			if ( 'jetpack_admin_page' !== $link[1] ) {
115
				break;
116
			}
117
			$offset++;
118
		}
119
120
		return $offset;
121
	}
122
123
	/**
124
	 * Refreshes the state cache via API call. Called via cron.
125
	 */
126
	public function refresh_state_cache() {
127
		Jetpack_Core_Json_Api_Endpoints::get_scan_state();
128
		Jetpack_Core_Json_Api_Endpoints::get_rewind_data();
129
	}
130
131
	/**
132
	 * Returns true if the link should appear.
133
	 *
134
	 * @return boolean
135
	 */
136
	private function should_show_link() {
137
		// Jetpack Scan/Backup is currently not supported on multisite.
138
		if ( is_multisite() ) {
139
			return false;
140
		}
141
142
		// Check if VaultPress is active, the assumption there is that VaultPress is working.
143
		// It has its link the adminbar.
144
		if ( class_exists( 'VaultPress' ) ) {
145
			return false;
146
		}
147
148
		return $this->has_backup() || $this->has_scan();
149
	}
150
151
	/**
152
	 * Detects if Scan is enabled.
153
	 *
154
	 * @return boolean
155
	 */
156
	private function has_scan() {
157
		$this->maybe_refresh_transient_cache();
158
		$scan_state = get_transient( 'jetpack_scan_state' );
159
		return ! $scan_state || 'unavailable' !== $scan_state->state;
160
	}
161
162
	/**
163
	 * Detects if Backup is enabled.
164
	 *
165
	 * @return boolean
166
	 */
167
	private function has_backup() {
168
		$this->maybe_refresh_transient_cache();
169
		$rewind_state = get_transient( 'jetpack_rewind_state' );
170
		return ! $rewind_state || 'unavailable' !== $rewind_state->state;
171
	}
172
173
	/**
174
	 * Triggers a cron job to refresh the Scan and Rewind state cache.
175
	 */
176
	private function maybe_refresh_transient_cache() {
177
		if ( $this->schedule_refresh_checked ) {
178
			return;
179
		}
180
181
		// Do we have a jetpack_scan and jetpack_rewind state set?
182
		if ( get_transient( 'jetpack_scan_state' ) && get_transient( 'jetpack_rewind_state' ) ) {
183
			return;
184
		}
185
186
		if ( false === wp_next_scheduled( self::SCHEDULE_ACTION_HOOK ) ) {
187
			wp_schedule_single_event( time(), self::SCHEDULE_ACTION_HOOK );
188
		}
189
190
		$this->schedule_refresh_checked = true;
191
	}
192
}
193
194
195