scanner = $scanner; $this->report = $report; $this->traffic = $traffic; } /** * Hook jobs. */ public function register() { add_action( self::SCAN, array( $this, 'scan' ) ); add_action( self::SCAN_1X, array( $this, 'scan' ) ); add_action( self::CLEANUP, array( $this->traffic, 'cleanup' ) ); add_action( 'init', array( $this, 'ensure_scheduled' ) ); add_action( 'goconsent_setting_updated', array( $this, 'on_setting' ), 10, 1 ); } /** * Run a scan and rebuild the report. */ public function scan() { if ( ! ( new Terms() )->accepted() ) { return; } delete_option( 'goconsent_scan_pending' ); $this->scanner->run(); $this->report->rebuild( true ); ConsentLog::cleanup(); } /** * Keep schedules in place; run the first scan shortly after activation. */ public function ensure_scheduled() { // No background work, and no requests of any kind, until an administrator accepts the terms. if ( ! ( new Terms() )->accepted() ) { if ( wp_next_scheduled( self::SCAN ) || wp_next_scheduled( self::SCAN_1X ) || wp_next_scheduled( self::CLEANUP ) ) { self::unschedule(); } return; } self::schedule(); if ( get_option( 'goconsent_scan_pending' ) && ! wp_next_scheduled( self::SCAN_1X ) ) { wp_schedule_single_event( time() + 10, self::SCAN_1X ); } } /** * Settings that change what the scanner sees trigger a fresh scan. * * @param string $key Changed setting. */ public function on_setting( $key ) { if ( ! ( new Terms() )->accepted() ) { return; } if ( in_array( $key, array( 'block', 'enabled', 'privacy_page', 'local_fonts' ), true ) && ! wp_next_scheduled( self::SCAN_1X ) ) { wp_schedule_single_event( time() + 5, self::SCAN_1X ); } } /** * Schedule recurring jobs. */ public static function schedule() { if ( ! wp_next_scheduled( self::SCAN ) ) { wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', self::SCAN ); } if ( ! wp_next_scheduled( self::CLEANUP ) ) { wp_schedule_event( time() + 300, 'hourly', self::CLEANUP ); } } /** * Remove all jobs. */ public static function unschedule() { foreach ( array( self::SCAN, self::SCAN_1X, self::CLEANUP, \GoConsent\Frontend\LocalFonts::HOOK ) as $hook ) { wp_clear_scheduled_hook( $hook ); } } }