| 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 |  |  | 		$has_scan   = $this->has_scan(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		$has_backup = $this->has_backup(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		$url = Redirect::get_url( 'calypso-backups' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		if ( $has_scan && ! $has_backup ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 			$menu_label = __( 'Scan', 'jetpack' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 			$url        = Redirect::get_url( 'calypso-scanner' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		} elseif ( ! $has_scan && $has_backup ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 			$menu_label = __( 'Backup', 'jetpack' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 			// Will be both, as the code won't get this far if neither is true. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 			$menu_label = __( 'Backup & Scan', 'jetpack' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		add_submenu_page( 'jetpack', $menu_label, esc_html( $menu_label ) . ' <span class="dashicons dashicons-external"></span>', 'manage_options', esc_url( $url ), null, $this->get_link_offset() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	 * We create a menu offset by counting all the pages that have a jetpack_admin_page set as the capability. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 * This makes it so that the highlight of the pages works as expected. When you click on the Setting or Dashboard. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	 * @return int Menu offset. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	private function get_link_offset() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		global $submenu; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		$offset = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		foreach ( $submenu['jetpack'] as $link ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 			if ( 'jetpack_admin_page' !== $link[1] ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 				break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 			$offset++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		return $offset; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	 * Refreshes the state cache via API call. Called via cron. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	public function refresh_state_cache() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		Jetpack_Core_Json_Api_Endpoints::get_scan_state(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		Jetpack_Core_Json_Api_Endpoints::get_rewind_data(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	 * Returns true if the link should appear. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	 * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	private function should_show_link() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		// Jetpack Scan/Backup is currently not supported on multisite. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 		if ( is_multisite() ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 		// Check if VaultPress is active, the assumption there is that VaultPress is working. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 		// It has its link the adminbar. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 		if ( class_exists( 'VaultPress' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 		return $this->has_backup() || $this->has_scan(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 	 * Detects if Scan is enabled. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 	 * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	private function has_scan() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 		$this->maybe_refresh_transient_cache(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 		$scan_state = get_transient( 'jetpack_scan_state' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		return ! $scan_state || 'unavailable' !== $scan_state->state; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 	 * Detects if Backup is enabled. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 	 * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 	private function has_backup() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 		$this->maybe_refresh_transient_cache(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 		$rewind_state = get_transient( 'jetpack_rewind_state' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 		return ! $rewind_state || 'unavailable' !== $rewind_state->state; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 	 * Triggers a cron job to refresh the Scan and Rewind state cache. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 	private function maybe_refresh_transient_cache() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 		if ( $this->schedule_refresh_checked ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 			return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 		// Do we have a jetpack_scan and jetpack_rewind state set? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 		if ( get_transient( 'jetpack_scan_state' ) && get_transient( 'jetpack_rewind_state' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 			return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 		if ( false === wp_next_scheduled( self::SCHEDULE_ACTION_HOOK ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 			wp_schedule_single_event( time(), self::SCHEDULE_ACTION_HOOK ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 		$this->schedule_refresh_checked = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 177 |  |  |  |