Completed
Push — add/cloud-sidebar-links ( 3a5138...50cf30 )
by
unknown
12:07 queued 04:15
created

Admin_Sidebar_Link::get_new_link()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 3
nop 0
dl 0
loc 22
rs 9.2568
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'], 2, 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
	 * Refreshes the state cache via API call. Called via cron.
105
	 */
106
	public function refresh_state_cache() {
107
		Jetpack_Core_Json_Api_Endpoints::get_scan_state();
108
		Jetpack_Core_Json_Api_Endpoints::get_rewind_data();
109
	}
110
111
	/**
112
	 * Returns true if the link should appear.
113
	 *
114
	 * @return boolean
115
	 */
116
	private function should_show_link() {
117
		// Jetpack Scan/Backup is currently not supported on multisite.
118
		if ( is_multisite() ) {
119
			return false;
120
		}
121
122
		// Check if VaultPress is active, the assumption there is that VaultPress is working.
123
		// It has its link the adminbar.
124
		if ( class_exists( 'VaultPress' ) ) {
125
			return false;
126
		}
127
128
		return $this->has_backup() || $this->has_scan();
129
	}
130
131
	/**
132
	 * Detects if Scan is enabled.
133
	 *
134
	 * @return boolean
135
	 */
136
	private function has_scan() {
137
		$this->maybe_refresh_transient_cache();
138
		$scan_state = get_transient( 'jetpack_scan_state' );
139
		return ! $scan_state || 'unavailable' !== $scan_state->state;
140
	}
141
142
	/**
143
	 * Detects if Backup is enabled.
144
	 *
145
	 * @return boolean
146
	 */
147
	private function has_backup() {
148
		$this->maybe_refresh_transient_cache();
149
		$rewind_state = get_transient( 'jetpack_rewind_state' );
150
		return ! $rewind_state || 'unavailable' !== $rewind_state->state;
151
	}
152
153
	/**
154
	 * Triggers a cron job to refresh the Scan and Rewind state cache.
155
	 */
156
	private function maybe_refresh_transient_cache() {
157
		if ( $this->schedule_refresh_checked ) {
158
			return;
159
		}
160
161
		// Do we have a jetpack_scan and jetpack_rewind state set?
162
		if ( get_transient( 'jetpack_scan_state' ) && get_transient( 'jetpack_rewind_state' ) ) {
163
			return;
164
		}
165
166
		if ( false === wp_next_scheduled( self::SCHEDULE_ACTION_HOOK ) ) {
167
			wp_schedule_single_event( time(), self::SCHEDULE_ACTION_HOOK );
168
		}
169
170
		$this->schedule_refresh_checked = true;
171
	}
172
}
173