Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/jetpack/modules/scan
File: class-admin-sidebar-link.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* A class that adds a scan and backup link to the admin sidebar.
[2] Fix | Delete
*
[3] Fix | Delete
* @package automattic/jetpack
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
namespace Automattic\Jetpack\Scan;
[7] Fix | Delete
[8] Fix | Delete
use Automattic\Jetpack\Admin_UI\Admin_Menu;
[9] Fix | Delete
use Automattic\Jetpack\My_Jetpack\Products\Backup;
[10] Fix | Delete
use Automattic\Jetpack\Redirect;
[11] Fix | Delete
use Automattic\Jetpack\Status\Host;
[12] Fix | Delete
use Jetpack_Core_Json_Api_Endpoints;
[13] Fix | Delete
[14] Fix | Delete
/**
[15] Fix | Delete
* Class Main
[16] Fix | Delete
*
[17] Fix | Delete
* Responsible for showing the link if available.
[18] Fix | Delete
*
[19] Fix | Delete
* @package Automattic\Jetpack\Scan
[20] Fix | Delete
*/
[21] Fix | Delete
class Admin_Sidebar_Link {
[22] Fix | Delete
[23] Fix | Delete
const SCHEDULE_ACTION_HOOK = 'jetpack_scan_refresh_states_event';
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* The singleton instance of this class.
[27] Fix | Delete
*
[28] Fix | Delete
* @var Admin_Sidebar_Link
[29] Fix | Delete
*/
[30] Fix | Delete
protected static $instance;
[31] Fix | Delete
[32] Fix | Delete
/**
[33] Fix | Delete
* Used to check if we need to schedule the refresh or we need to do it.
[34] Fix | Delete
*
[35] Fix | Delete
* @var boolean | null
[36] Fix | Delete
*/
[37] Fix | Delete
private $schedule_refresh_checked;
[38] Fix | Delete
[39] Fix | Delete
/**
[40] Fix | Delete
* Get the singleton instance of the class.
[41] Fix | Delete
*
[42] Fix | Delete
* @return Admin_Sidebar_Link
[43] Fix | Delete
*/
[44] Fix | Delete
public static function instance() {
[45] Fix | Delete
if ( ! isset( self::$instance ) ) {
[46] Fix | Delete
self::$instance = new Admin_Sidebar_Link();
[47] Fix | Delete
self::$instance->init_hooks();
[48] Fix | Delete
}
[49] Fix | Delete
[50] Fix | Delete
return self::$instance;
[51] Fix | Delete
}
[52] Fix | Delete
[53] Fix | Delete
/**
[54] Fix | Delete
* Adds action hooks.
[55] Fix | Delete
*/
[56] Fix | Delete
public function init_hooks() {
[57] Fix | Delete
add_action( 'jetpack_admin_menu', array( $this, 'maybe_add_admin_link' ), 99 );
[58] Fix | Delete
add_action( self::SCHEDULE_ACTION_HOOK, array( $this, 'refresh_state_cache' ) );
[59] Fix | Delete
}
[60] Fix | Delete
[61] Fix | Delete
/**
[62] Fix | Delete
* Adds a link to the Scan and Backup page.
[63] Fix | Delete
*/
[64] Fix | Delete
public function maybe_add_admin_link() {
[65] Fix | Delete
if ( ! $this->should_show_link() ) {
[66] Fix | Delete
return;
[67] Fix | Delete
}
[68] Fix | Delete
[69] Fix | Delete
if ( $this->should_show_scan() ) {
[70] Fix | Delete
Admin_Menu::add_menu(
[71] Fix | Delete
__( 'Scan', 'jetpack' ),
[72] Fix | Delete
__( 'Scan', 'jetpack' ) . ' <span class="dashicons dashicons-external"></span>',
[73] Fix | Delete
'manage_options',
[74] Fix | Delete
esc_url( Redirect::get_url( 'cloud-scan-history-wp-menu' ) ),
[75] Fix | Delete
null,
[76] Fix | Delete
$this->get_link_offset()
[77] Fix | Delete
);
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
// Add scan item which shows history page only. This is mutally exclusive from the scan item above and is only shown for Atomic sitse.
[81] Fix | Delete
if ( $this->should_show_scan_history_only() ) {
[82] Fix | Delete
Admin_Menu::add_menu(
[83] Fix | Delete
__( 'Scan', 'jetpack' ),
[84] Fix | Delete
__( 'Scan', 'jetpack' ) . ' <span class="dashicons dashicons-external"></span>',
[85] Fix | Delete
'manage_options',
[86] Fix | Delete
esc_url( Redirect::get_url( 'cloud-scan-history-wp-menu' ) ),
[87] Fix | Delete
null,
[88] Fix | Delete
$this->get_link_offset()
[89] Fix | Delete
);
[90] Fix | Delete
}
[91] Fix | Delete
[92] Fix | Delete
if ( $this->should_show_backup() ) {
[93] Fix | Delete
Admin_Menu::add_menu(
[94] Fix | Delete
__( 'VaultPress Backup', 'jetpack' ),
[95] Fix | Delete
__( 'VaultPress Backup', 'jetpack' ) . ' <span class="dashicons dashicons-external"></span>',
[96] Fix | Delete
'manage_options',
[97] Fix | Delete
esc_url( Redirect::get_url( 'calypso-backups' ) ),
[98] Fix | Delete
null,
[99] Fix | Delete
$this->get_link_offset()
[100] Fix | Delete
);
[101] Fix | Delete
}
[102] Fix | Delete
}
[103] Fix | Delete
[104] Fix | Delete
/**
[105] Fix | Delete
* We create a menu offset by counting all the pages that have a jetpack_admin_page set as the capability.
[106] Fix | Delete
*
[107] Fix | Delete
* This makes it so that the highlight of the pages works as expected. When you click on the Setting or Dashboard.
[108] Fix | Delete
*
[109] Fix | Delete
* @return int Menu offset.
[110] Fix | Delete
*/
[111] Fix | Delete
private function get_link_offset() {
[112] Fix | Delete
global $submenu;
[113] Fix | Delete
$offset = 9;
[114] Fix | Delete
[115] Fix | Delete
if ( ! array_key_exists( 'jetpack', $submenu ) ) {
[116] Fix | Delete
return $offset;
[117] Fix | Delete
}
[118] Fix | Delete
[119] Fix | Delete
foreach ( $submenu['jetpack'] as $link ) {
[120] Fix | Delete
if ( 'jetpack_admin_page' !== $link[1] ) {
[121] Fix | Delete
break;
[122] Fix | Delete
}
[123] Fix | Delete
++$offset;
[124] Fix | Delete
}
[125] Fix | Delete
[126] Fix | Delete
return $offset;
[127] Fix | Delete
}
[128] Fix | Delete
[129] Fix | Delete
/**
[130] Fix | Delete
* Refreshes the state cache via API call. Called via cron.
[131] Fix | Delete
*/
[132] Fix | Delete
public function refresh_state_cache() {
[133] Fix | Delete
Jetpack_Core_Json_Api_Endpoints::get_scan_state();
[134] Fix | Delete
Jetpack_Core_Json_Api_Endpoints::get_rewind_data();
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
/**
[138] Fix | Delete
* Returns true if the link should appear.
[139] Fix | Delete
*
[140] Fix | Delete
* @return boolean
[141] Fix | Delete
*/
[142] Fix | Delete
private function should_show_link() {
[143] Fix | Delete
// Jetpack Scan/Backup is currently not supported on multisite.
[144] Fix | Delete
if ( is_multisite() ) {
[145] Fix | Delete
return false;
[146] Fix | Delete
}
[147] Fix | Delete
[148] Fix | Delete
// Check if VaultPress is active, the assumption there is that VaultPress is working.
[149] Fix | Delete
// It has its link the adminbar.
[150] Fix | Delete
if ( class_exists( 'VaultPress' ) ) {
[151] Fix | Delete
return false;
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
return $this->should_show_scan() || $this->should_show_backup() || $this->should_show_scan_history_only();
[155] Fix | Delete
}
[156] Fix | Delete
[157] Fix | Delete
/**
[158] Fix | Delete
* Check if we should display the Scan menu item.
[159] Fix | Delete
*
[160] Fix | Delete
* It will only be displayed if site has Scan enabled, is not an Atomic site, and the stand-alone Protect plugin is not active, because it will have a menu item of its own.
[161] Fix | Delete
*
[162] Fix | Delete
* @return boolean
[163] Fix | Delete
*/
[164] Fix | Delete
private function should_show_scan() {
[165] Fix | Delete
return $this->has_scan() && ! $this->has_protect_plugin() && ! ( new Host() )->is_woa_site();
[166] Fix | Delete
}
[167] Fix | Delete
[168] Fix | Delete
/**
[169] Fix | Delete
* Check if we should display the Scan menu item history.
[170] Fix | Delete
*
[171] Fix | Delete
* It will only be displayed if site has Scan enabled, is an Atomic site.
[172] Fix | Delete
*
[173] Fix | Delete
* @return boolean
[174] Fix | Delete
*/
[175] Fix | Delete
private function should_show_scan_history_only() {
[176] Fix | Delete
return $this->has_scan() && ( new Host() )->is_woa_site() && get_option( 'wpcom_admin_interface' ) === 'wp-admin';
[177] Fix | Delete
}
[178] Fix | Delete
[179] Fix | Delete
/**
[180] Fix | Delete
* Check if we should display the Backup menu item.
[181] Fix | Delete
*
[182] Fix | Delete
* It will only be displayed if site has Backup enabled and the stand-alone Backup plugin is not active, because it will have a menu item of its own.
[183] Fix | Delete
*
[184] Fix | Delete
* @return boolean
[185] Fix | Delete
*/
[186] Fix | Delete
private function should_show_backup() {
[187] Fix | Delete
return $this->has_backup() && ! $this->has_backup_plugin();
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
/**
[191] Fix | Delete
* Detects if Scan is enabled.
[192] Fix | Delete
*
[193] Fix | Delete
* @return boolean
[194] Fix | Delete
*/
[195] Fix | Delete
private function has_scan() {
[196] Fix | Delete
$this->maybe_refresh_transient_cache();
[197] Fix | Delete
$scan_state = get_transient( 'jetpack_scan_state' );
[198] Fix | Delete
if ( ! $scan_state ) {
[199] Fix | Delete
return false;
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
return isset( $scan_state->state ) && 'unavailable' !== $scan_state->state;
[203] Fix | Delete
}
[204] Fix | Delete
[205] Fix | Delete
/**
[206] Fix | Delete
* Detects if Protect plugin is active.
[207] Fix | Delete
*
[208] Fix | Delete
* @return boolean
[209] Fix | Delete
*/
[210] Fix | Delete
private function has_protect_plugin() {
[211] Fix | Delete
return class_exists( 'Jetpack_Protect' );
[212] Fix | Delete
}
[213] Fix | Delete
[214] Fix | Delete
/**
[215] Fix | Delete
* Detects if Backup is enabled.
[216] Fix | Delete
*
[217] Fix | Delete
* @return boolean
[218] Fix | Delete
*/
[219] Fix | Delete
private function has_backup() {
[220] Fix | Delete
$this->maybe_refresh_transient_cache();
[221] Fix | Delete
$rewind_state = get_transient( 'jetpack_rewind_state' );
[222] Fix | Delete
if ( ! $rewind_state ) {
[223] Fix | Delete
return false;
[224] Fix | Delete
}
[225] Fix | Delete
[226] Fix | Delete
return isset( $rewind_state->state ) && 'unavailable' !== $rewind_state->state;
[227] Fix | Delete
}
[228] Fix | Delete
[229] Fix | Delete
/**
[230] Fix | Delete
* Detects if Backup plugin is active.
[231] Fix | Delete
*
[232] Fix | Delete
* @return boolean
[233] Fix | Delete
*/
[234] Fix | Delete
private function has_backup_plugin() {
[235] Fix | Delete
return Backup::is_standalone_plugin_active();
[236] Fix | Delete
}
[237] Fix | Delete
[238] Fix | Delete
/**
[239] Fix | Delete
* Triggers a cron job to refresh the Scan and Rewind state cache.
[240] Fix | Delete
*/
[241] Fix | Delete
private function maybe_refresh_transient_cache() {
[242] Fix | Delete
if ( $this->schedule_refresh_checked ) {
[243] Fix | Delete
return;
[244] Fix | Delete
}
[245] Fix | Delete
[246] Fix | Delete
// Do we have a jetpack_scan and jetpack_rewind state set?
[247] Fix | Delete
if ( get_transient( 'jetpack_scan_state' ) && get_transient( 'jetpack_rewind_state' ) ) {
[248] Fix | Delete
return;
[249] Fix | Delete
}
[250] Fix | Delete
[251] Fix | Delete
if ( false === wp_next_scheduled( self::SCHEDULE_ACTION_HOOK ) ) {
[252] Fix | Delete
wp_schedule_single_event( time(), self::SCHEDULE_ACTION_HOOK );
[253] Fix | Delete
}
[254] Fix | Delete
[255] Fix | Delete
$this->schedule_refresh_checked = true;
[256] Fix | Delete
}
[257] Fix | Delete
}
[258] Fix | Delete
[259] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function