← Back
Editing: class-admin-sidebar-link.php
<?php /** * A class that adds a scan link to the admin sidebar. * * @package automattic/jetpack */ namespace Automattic\Jetpack\Scan; use Automattic\Jetpack\Admin_UI\Admin_Menu; use Automattic\Jetpack\My_Jetpack\Products\Protect; use Automattic\Jetpack\Redirect; use Automattic\Jetpack\Status; use Automattic\Jetpack\Status\Host; 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 page. */ public function maybe_add_admin_link() { if ( ! $this->should_show_link() ) { return; } $position = defined( Admin_Menu::class . '::POSITION_EXTERNAL' ) ? Admin_Menu::POSITION_EXTERNAL : 100; if ( $this->should_show_scan() ) { Admin_Menu::add_menu( /** "Scan" is a product name, do not translate. */ 'Scan', 'Scan <span aria-hidden="true">↗</span>', 'manage_options', esc_url( Redirect::get_url( 'cloud-scan-history-wp-menu' ) ), null, $position, array( 'key' => 'jetpack-scan-cloud' ) ); } // 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( /** "Scan" is a product name, do not translate. */ 'Scan', 'Scan <span aria-hidden="true">↗</span>', 'manage_options', esc_url( Redirect::get_url( 'cloud-scan-history-wp-menu' ) ), null, $position, array( 'key' => 'jetpack-scan-cloud' ) ); } } /** * Refreshes the state cache via API call. Called via cron. * * Still warms the rewind state, which this class no longer reads itself: * Jetpack_Core_Json_Api_Endpoints::rewind_data() serves it from that cache. */ public function refresh_state_cache() { if ( ( new Status() )->is_offline_mode() ) { return; } 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 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; } return $this->should_show_scan() || $this->should_show_scan_history_only(); } /** * Check if we should display the Scan menu item. * * It will only be displayed if site has Scan enabled, is not an Atomic site, and no Protect page is registered, because that has a menu item of its own. * * @return boolean */ private function should_show_scan() { return $this->has_scan() && ! $this->has_protect_page() && ! ( new Host() )->is_woa_site(); } /** * 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'; } /** * Detects if Scan is enabled. * * @return boolean */ private function has_scan() { $this->maybe_refresh_transient_cache(); $scan_state = get_transient( 'jetpack_scan_state' ); if ( ! $scan_state ) { return false; } return isset( $scan_state->state ) && 'unavailable' !== $scan_state->state; } /** * Detects if the Protect plugin or the Protect dashboard module provides the Protect page. * * @return boolean */ private function has_protect_page() { if ( method_exists( Protect::class, 'has_protect_page' ) ) { return Protect::has_protect_page(); } return class_exists( 'Jetpack_Protect' ) || did_action( 'jetpack_protect_dashboard_initialized' ); } /** * Triggers a cron job to refresh the Scan and Rewind state cache. */ private function maybe_refresh_transient_cache() { if ( ( new Status() )->is_offline_mode() || $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; } }
Save File
Cancel