Completed
Push — add/cloud-sidebar-links ( b72622...82726c )
by
unknown
07:51
created

Admin_Sidebar_Link::has_scan()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 5
rs 10
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
		$has_scan   = $this->has_scan();
69
		$has_backup = $this->has_backup();
70
71
		$url = Redirect::get_url( 'calypso-backups' );
72
		if ( $has_scan && ! $has_backup ) {
73
			$menu_label = __( 'Scan', 'jetpack' );
74
			$url        = Redirect::get_url( 'calypso-scanner' );
75
		} elseif ( ! $has_scan && $has_backup ) {
76
			$menu_label = __( 'Backup', 'jetpack' );
77
		} else {
78
			// Will be both, as the code won't get this far if neither is true.
79
			$menu_label = __( 'Backup & Scan', 'jetpack' );
80
		}
81
82
		$new_link = array(
83
			esc_html( $menu_label ) . ' <span class="dashicons dashicons-external"></span>',
84
			'manage_options', // Check permissions here.
85
			esc_url( $url ),
86
		);
87
88
		// Splice the nav menu item into the Jetpack nav.
89
		global $submenu;
90
		array_splice( $submenu['jetpack'], 1, 0, array( $new_link ) );
91
	}
92
93
	/**
94
	 * Refreshes the state cache via API call. Called via cron.
95
	 */
96
	public function refresh_state_cache() {
97
		Jetpack_Core_Json_Api_Endpoints::get_scan_state();
98
		Jetpack_Core_Json_Api_Endpoints::get_rewind_data();
99
	}
100
101
	/**
102
	 * Returns true if the link should appear.
103
	 *
104
	 * @return boolean
105
	 */
106
	private function should_show_link() {
107
		// Jetpack Scan/Backup is currently not supported on multisite.
108
		if ( is_multisite() ) {
109
			return false;
110
		}
111
112
		// Check if VaultPress is active, the assumption there is that VaultPress is working.
113
		// It has its link the adminbar.
114
		if ( class_exists( 'VaultPress' ) ) {
115
			return false;
116
		}
117
118
		return $this->has_backup() || $this->has_scan();
119
	}
120
121
	/**
122
	 * Detects if Scan is enabled.
123
	 *
124
	 * @return boolean
125
	 */
126
	private function has_scan() {
127
		$this->maybe_refresh_transient_cache();
128
		$scan_state = get_transient( 'jetpack_scan_state' );
129
		return ! $scan_state || 'unavailable' !== $scan_state->state;
130
	}
131
132
	/**
133
	 * Detects if Backup is enabled.
134
	 *
135
	 * @return boolean
136
	 */
137
	private function has_backup() {
138
		$this->maybe_refresh_transient_cache();
139
		$rewind_state = get_transient( 'jetpack_rewind_state' );
140
		return ! $rewind_state || 'unavailable' !== $rewind_state->state;
141
	}
142
143
	/**
144
	 * Triggers a cron job to refresh the Scan and Rewind state cache.
145
	 */
146
	private function maybe_refresh_transient_cache() {
147
		if ( $this->schedule_refresh_checked ) {
148
			return;
149
		}
150
151
		// Do we have a jetpack_scan and jetpack_rewind state set?
152
		if ( false !== get_transient( 'jetpack_scan_state' ) && false !== get_transient( 'jetpack_rewind_state' ) ) {
153
			return;
154
		}
155
156
		if ( false === wp_next_scheduled( self::SCHEDULE_ACTION_HOOK ) ) {
157
			wp_schedule_single_event( time(), self::SCHEDULE_ACTION_HOOK );
158
		}
159
160
		$this->schedule_refresh_checked = true;
161
	}
162
}
163