| 1 |
<?php |
| 2 |
/** |
| 3 |
* Initialize Assisted Scan. |
| 4 |
* |
| 5 |
* A recovery path for sites the cloud scanner cannot reach (host firewalls that block |
| 6 |
* our external scanner or its domain-verification request, leaving the site with no |
| 7 |
* scanner and no cookie policy). It walks the selected public pages in the admin's own |
| 8 |
* browser, collects browser-visible cookies, scripts and request domains, matches them |
| 9 |
* against the bundled Known Services catalog, and feeds the same ingestion pipeline a |
| 10 |
* cloud scan uses. It needs no SaaS reachability, which is the failure it exists for. |
| 11 |
* |
| 12 |
* A workaround, not a headline feature: no scan history, admin menu entry or plan gating. |
| 13 |
* |
| 14 |
* @package SureCookie\Inc\Modules\AssistedScan |
| 15 |
* @since 1.3.0 |
| 16 |
*/ |
| 17 |
|
| 18 |
namespace SureCookie\Inc\Modules\AssistedScan; |
| 19 |
|
| 20 |
use SureCookie\Inc\Traits\GetInstance; |
| 21 |
|
| 22 |
if ( ! defined( 'ABSPATH' ) ) { |
| 23 |
exit; // Exit if accessed directly. |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Init |
| 28 |
* |
| 29 |
* @since 1.3.0 |
| 30 |
*/ |
| 31 |
class Init { |
| 32 |
use GetInstance; |
| 33 |
|
| 34 |
/** |
| 35 |
* Constructor. |
| 36 |
* |
| 37 |
* @since 1.3.0 |
| 38 |
*/ |
| 39 |
private function __construct() { |
| 40 |
// Kill switch checked before anything registers, so disabling the feature |
| 41 |
// removes the REST routes and front-end collector outright, not just the entry point. |
| 42 |
if ( ! Utils::is_enabled() ) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
Session::get_instance(); |
| 47 |
Collector::get_instance(); |
| 48 |
|
| 49 |
add_filter( 'surecookie_api_controllers', [ $this, 'register_api_controller' ] ); |
| 50 |
|
| 51 |
// Rescue for a walk the browser never finished: Session arms a one-shot event per |
| 52 |
// recorded page, so this fires after the last sign of life and finalizes what was collected. |
| 53 |
add_action( Session::FINALIZE_HOOK, [ $this, 'finalize_abandoned_scan' ] ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Register the module's REST controller with the core API router. |
| 58 |
* |
| 59 |
* @param array<int, string> $controllers Registered controller class names. |
| 60 |
* @since 1.3.0 |
| 61 |
* @return array<int, string> |
| 62 |
*/ |
| 63 |
public function register_api_controller( $controllers = [] ) { |
| 64 |
if ( is_array( $controllers ) ) { |
| 65 |
$controllers[] = Api::class; |
| 66 |
} |
| 67 |
|
| 68 |
return $controllers; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Finalize a session the browser walked away from. |
| 73 |
* |
| 74 |
* Only acts once the session has genuinely gone quiet, so this cannot cut short |
| 75 |
* a slow walk that is still reporting pages. |
| 76 |
* |
| 77 |
* @since 1.3.0 |
| 78 |
* @return void |
| 79 |
*/ |
| 80 |
public function finalize_abandoned_scan(): void { |
| 81 |
$session = Session::get_instance(); |
| 82 |
|
| 83 |
if ( empty( $session->get() ) ) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
if ( ! $session->is_stale() && ! $session->is_complete() ) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
// Flagged abandoned only when the walk went quiet rather than reaching its last |
| 92 |
// page, so analytics can tell "tab closed" from "finished but the finish call never landed". |
| 93 |
Ingest::get_instance()->finalize( $session->is_stale() ); |
| 94 |
} |
| 95 |
} |
| 96 |
|