|
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
|
|
|
* Constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* Adds action hooks. |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct() { |
|
30
|
|
|
add_action( 'jetpack_admin_menu', array( $this, 'maybe_add_admin_link' ), 99 ); |
|
31
|
|
|
add_action( self::SCHEDULE_ACTION_HOOK, array( $this, 'refresh_state_cache' ) ); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Adds a link to the Scan and Backup page. |
|
36
|
|
|
*/ |
|
37
|
|
|
public function maybe_add_admin_link() { |
|
38
|
|
|
if ( ! $this->should_show_link() ) { |
|
39
|
|
|
return; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$has_scan = $this->has_scan(); |
|
43
|
|
|
$has_backup = $this->has_backup(); |
|
44
|
|
|
|
|
45
|
|
|
if ( $has_scan && ! $has_backup ) { |
|
46
|
|
|
$menu_label = __( 'Scan', 'jetpack' ); |
|
47
|
|
|
} elseif ( ! $has_scan && $has_backup ) { |
|
48
|
|
|
$menu_label = __( 'Backup', 'jetpack' ); |
|
49
|
|
|
} else { |
|
50
|
|
|
// Will be both, as the code won't get this far if neither is true. |
|
51
|
|
|
$menu_label = __( 'Backup & Scan', 'jetpack' ); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$new_link = array( |
|
55
|
|
|
esc_html( $menu_label ) . ' <span class="dashicons dashicons-external"></span>', |
|
56
|
|
|
'manage_options', // Check permissions here. |
|
57
|
|
|
esc_url( Redirect::get_url( 'calypso-backups' ) ), |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
// Splice the nav menu item into the Jetpack nav. |
|
61
|
|
|
global $submenu; |
|
62
|
|
|
array_splice( $submenu['jetpack'], 1, 0, array( $new_link ) ); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Refreshes the state cache via API call. Called via cron. |
|
67
|
|
|
*/ |
|
68
|
|
|
public function refresh_state_cache() { |
|
69
|
|
|
Jetpack_Core_Json_Api_Endpoints::get_scan_state(); |
|
70
|
|
|
Jetpack_Core_Json_Api_Endpoints::get_rewind_data(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Returns true if the link should appear. |
|
75
|
|
|
* |
|
76
|
|
|
* @return boolean |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function should_show_link() { |
|
79
|
|
|
// Jetpack Scan/Backup is currently not supported on multisite. |
|
80
|
|
|
if ( is_multisite() ) { |
|
81
|
|
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// Check if VaultPress is active, the assumption there is that VaultPress is working. |
|
85
|
|
|
// It has its own notice in the admin bar. |
|
86
|
|
|
if ( class_exists( 'VaultPress' ) ) { |
|
87
|
|
|
return false; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $this->has_backup() || $this->has_scan(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Detects if Scan is enabled. |
|
95
|
|
|
* |
|
96
|
|
|
* @return boolean |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function has_scan() { |
|
99
|
|
|
$this->maybe_refresh_transient_cache(); |
|
100
|
|
|
$scan_state = get_transient( 'jetpack_scan_state' ); |
|
101
|
|
|
return ! $scan_state || 'unavailable' !== $scan_state->state; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Detects if Backup is enabled. |
|
106
|
|
|
* |
|
107
|
|
|
* @return boolean |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function has_backup() { |
|
110
|
|
|
$this->maybe_refresh_transient_cache(); |
|
111
|
|
|
$rewind_state = get_transient( 'jetpack_rewind_state' ); |
|
112
|
|
|
return ! $rewind_state || 'unavailable' !== $rewind_state->state; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Triggers a cron job to refresh the Scan and Rewind state cache. |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function maybe_refresh_transient_cache() { |
|
119
|
|
|
if ( false !== get_transient( 'jetpack_scan_state' ) && false !== get_transient( 'jetpack_rewind_state' ) ) { |
|
120
|
|
|
return; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
if ( false === wp_next_scheduled( self::SCHEDULE_ACTION_HOOK ) ) { |
|
124
|
|
|
wp_schedule_single_event( time(), self::SCHEDULE_ACTION_HOOK ); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|