kollapsminoriteten/wp-content/plugins/jetpack/modules/scan/class-admin-sidebar-link.php

263 lines
6.5 KiB
PHP
Raw Normal View History

2020-06-23 13:49:54 +02:00
<?php
/**
* A class that adds a scan and backup link to the admin sidebar.
*
* @package automattic/jetpack
*/
namespace Automattic\Jetpack\Scan;
2025-02-28 08:42:11 +01:00
use Automattic\Jetpack\Admin_UI\Admin_Menu;
2023-04-26 17:39:43 +02:00
use Automattic\Jetpack\My_Jetpack\Products\Backup;
2020-06-23 13:49:54 +02:00
use Automattic\Jetpack\Redirect;
2023-04-26 17:39:43 +02:00
use Automattic\Jetpack\Status\Host;
2020-06-23 13:49:54 +02:00
use Jetpack_Core_Json_Api_Endpoints;
/**
* Class Main
*
* Responsible for showing the link if available.
*
* @package Automattic\Jetpack\Scan
*/
class Admin_Sidebar_Link {
const SCHEDULE_ACTION_HOOK = 'jetpack_scan_refresh_states_event';
/**
* The singleton instance of this class.
*
* @var Admin_Sidebar_Link
*/
protected static $instance;
/**
* Used to check if we need to schedule the refresh or we need to do it.
*
* @var boolean | null
*/
private $schedule_refresh_checked;
/**
* Get the singleton instance of the class.
*
* @return Admin_Sidebar_Link
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new Admin_Sidebar_Link();
self::$instance->init_hooks();
}
return self::$instance;
}
/**
* Adds action hooks.
*/
public function init_hooks() {
add_action( 'jetpack_admin_menu', array( $this, 'maybe_add_admin_link' ), 99 );
add_action( self::SCHEDULE_ACTION_HOOK, array( $this, 'refresh_state_cache' ) );
}
/**
* Adds a link to the Scan and Backup page.
*/
public function maybe_add_admin_link() {
if ( ! $this->should_show_link() ) {
return;
}
2023-04-26 17:39:43 +02:00
if ( $this->should_show_scan() ) {
2025-02-28 08:42:11 +01:00
Admin_Menu::add_menu(
2025-12-12 13:13:07 +01:00
/** "Scan" is a product name, do not translate. */
'Scan',
'Scan <span class="dashicons dashicons-external"></span>',
2025-02-28 08:42:11 +01:00
'manage_options',
esc_url( Redirect::get_url( 'cloud-scan-history-wp-menu' ) ),
null,
$this->get_link_offset()
);
}
// Add scan item which shows history page only. This is mutally exclusive from the scan item above and is only shown for Atomic sitse.
if ( $this->should_show_scan_history_only() ) {
Admin_Menu::add_menu(
2025-12-12 13:13:07 +01:00
/** "Scan" is a product name, do not translate. */
'Scan',
'Scan <span class="dashicons dashicons-external"></span>',
2025-02-28 08:42:11 +01:00
'manage_options',
esc_url( Redirect::get_url( 'cloud-scan-history-wp-menu' ) ),
null,
$this->get_link_offset()
);
2020-06-23 13:49:54 +02:00
}
2023-04-26 17:39:43 +02:00
if ( $this->should_show_backup() ) {
2025-02-28 08:42:11 +01:00
Admin_Menu::add_menu(
2025-12-12 13:13:07 +01:00
/** "VaultPress Backup" is a product name, do not translate. */
'VaultPress Backup',
'VaultPress Backup <span class="dashicons dashicons-external"></span>',
2025-02-28 08:42:11 +01:00
'manage_options',
esc_url( Redirect::get_url( 'calypso-backups' ) ),
null,
$this->get_link_offset()
);
2023-04-26 17:39:43 +02:00
}
2020-06-23 13:49:54 +02:00
}
/**
2020-12-10 14:04:11 +01:00
* We create a menu offset by counting all the pages that have a jetpack_admin_page set as the capability.
2020-06-23 13:49:54 +02:00
*
* This makes it so that the highlight of the pages works as expected. When you click on the Setting or Dashboard.
*
* @return int Menu offset.
*/
private function get_link_offset() {
global $submenu;
2025-02-28 08:42:11 +01:00
$offset = 9;
2022-04-02 10:26:41 +02:00
if ( ! array_key_exists( 'jetpack', $submenu ) ) {
return $offset;
}
2020-06-23 13:49:54 +02:00
foreach ( $submenu['jetpack'] as $link ) {
if ( 'jetpack_admin_page' !== $link[1] ) {
2020-09-15 14:30:05 +02:00
break;
2020-06-23 13:49:54 +02:00
}
2023-04-26 17:39:43 +02:00
++$offset;
2020-06-23 13:49:54 +02:00
}
2020-09-15 14:30:05 +02:00
return $offset;
2020-06-23 13:49:54 +02:00
}
/**
* Refreshes the state cache via API call. Called via cron.
*/
public function refresh_state_cache() {
Jetpack_Core_Json_Api_Endpoints::get_scan_state();
Jetpack_Core_Json_Api_Endpoints::get_rewind_data();
}
/**
* Returns true if the link should appear.
*
* @return boolean
*/
private function should_show_link() {
// Jetpack Scan/Backup is currently not supported on multisite.
if ( is_multisite() ) {
return false;
}
// Check if VaultPress is active, the assumption there is that VaultPress is working.
// It has its link the adminbar.
if ( class_exists( 'VaultPress' ) ) {
return false;
}
2025-02-28 08:42:11 +01:00
return $this->should_show_scan() || $this->should_show_backup() || $this->should_show_scan_history_only();
2022-12-15 17:41:53 +01:00
}
/**
* Check if we should display the Scan menu item.
*
2023-04-26 17:39:43 +02:00
* 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.
2022-12-15 17:41:53 +01:00
*
* @return boolean
*/
private function should_show_scan() {
2023-04-26 17:39:43 +02:00
return $this->has_scan() && ! $this->has_protect_plugin() && ! ( new Host() )->is_woa_site();
2022-04-02 10:26:41 +02:00
}
2025-02-28 08:42:11 +01:00
/**
* Check if we should display the Scan menu item history.
*
* It will only be displayed if site has Scan enabled, is an Atomic site.
*
* @return boolean
*/
private function should_show_scan_history_only() {
return $this->has_scan() && ( new Host() )->is_woa_site() && get_option( 'wpcom_admin_interface' ) === 'wp-admin';
}
2022-04-02 10:26:41 +02:00
/**
* Check if we should display the Backup menu item.
*
* 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.
*
* @return boolean
*/
private function should_show_backup() {
return $this->has_backup() && ! $this->has_backup_plugin();
2020-06-23 13:49:54 +02:00
}
/**
* Detects if Scan is enabled.
*
* @return boolean
*/
private function has_scan() {
$this->maybe_refresh_transient_cache();
$scan_state = get_transient( 'jetpack_scan_state' );
2023-04-26 17:39:43 +02:00
if ( ! $scan_state ) {
return false;
}
return isset( $scan_state->state ) && 'unavailable' !== $scan_state->state;
2020-06-23 13:49:54 +02:00
}
2022-12-15 17:41:53 +01:00
/**
* Detects if Protect plugin is active.
*
* @return boolean
*/
private function has_protect_plugin() {
return class_exists( 'Jetpack_Protect' );
}
2020-06-23 13:49:54 +02:00
/**
* Detects if Backup is enabled.
*
* @return boolean
*/
private function has_backup() {
$this->maybe_refresh_transient_cache();
$rewind_state = get_transient( 'jetpack_rewind_state' );
2023-04-26 17:39:43 +02:00
if ( ! $rewind_state ) {
return false;
}
return isset( $rewind_state->state ) && 'unavailable' !== $rewind_state->state;
2020-06-23 13:49:54 +02:00
}
2022-04-02 10:26:41 +02:00
/**
* Detects if Backup plugin is active.
*
* @return boolean
*/
private function has_backup_plugin() {
2023-12-07 09:44:11 +01:00
return Backup::is_standalone_plugin_active();
2022-04-02 10:26:41 +02:00
}
2020-06-23 13:49:54 +02:00
/**
* Triggers a cron job to refresh the Scan and Rewind state cache.
*/
private function maybe_refresh_transient_cache() {
if ( $this->schedule_refresh_checked ) {
return;
}
// Do we have a jetpack_scan and jetpack_rewind state set?
if ( get_transient( 'jetpack_scan_state' ) && get_transient( 'jetpack_rewind_state' ) ) {
return;
}
if ( false === wp_next_scheduled( self::SCHEDULE_ACTION_HOOK ) ) {
wp_schedule_single_event( time(), self::SCHEDULE_ACTION_HOOK );
}
$this->schedule_refresh_checked = true;
}
}