` of a * page opened for a scan — through WordPress' script API, like the guard. * The probe watches what the page loads and stores, and tells the screen * that opened it; it is never part of a page a visitor gets. */ final class Probe { public const HANDLE = 'workmore-cookie-crumb-scan'; /** * One of Request::MODES. * * @var string */ private $mode; /** * The page's place in the scan. * * @var int */ private $index; /** * Absolute path to the plugin root, trailing slash. * * @var string */ private $base_dir; /** * Constructor. * * @param string $mode One of Request::MODES. * @param int $index The page's place in the scan. * @param string $base_dir Absolute plugin directory path. */ public function __construct( string $mode, int $index, string $base_dir = WMCC_PLUGIN_DIR ) { $this->mode = $mode; $this->index = $index; $this->base_dir = trailingslashit( $base_dir ); } /** * Attach hooks. * * @return void */ public function register(): void { add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ), 0 ); // wp_enqueue_scripts runs at wp_head 1; right after it, and — being // registered first — before the guard. add_action( 'wp_head', array( $this, 'printNow' ), 2 ); } /** * Register the probe with what it has to know. * * @return void */ public function enqueue(): void { $probe = $this->base_dir . 'assets/probe.js'; // A development checkout before `npm run build`: nothing to print. if ( ! is_readable( $probe ) ) { return; } $config = array( 'mode' => $this->mode, 'index' => $this->index, // The arguments of the scan, for the probe to take out of the address. 'strip' => array( Request::QUERY, Request::TOKEN, Request::INDEX, '_wpnonce' ), ); wp_register_script( self::HANDLE, false, array(), WMCC_VERSION, false ); wp_add_inline_script( self::HANDLE, 'window.wmccScanPage=' . (string) wp_json_encode( $config, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ) . ';' . (string) file_get_contents( $probe ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- A local file of this plugin, read to print it inline. } /** * Print the probe now, ahead of the head's other scripts. * * @return void */ public function printNow(): void { if ( wp_script_is( self::HANDLE, 'registered' ) ) { wp_scripts()->do_items( array( self::HANDLE ) ); } } }