|null */ private ?array $refused = null; /** * The path-D gate, once made. */ private ?PathDGate $gate = null; /** * Constructor. * * @param string $plugin_file Main plugin file. * @param (Closure(): Requirements)|null $requirements Requirements lookup; defaults to the running site. */ public function __construct( private readonly string $plugin_file, ?Closure $requirements = null ) { $this->requirements = $requirements ?? static fn (): Requirements => Requirements::from_environment(); } /** * The running instance. * * @codeCoverageIgnore Wires the singleton to the main file; covered by the integration suite. */ public static function instance(): self { if ( null === self::$instance ) { self::$instance = new self( (string) ADAFWC_PLUGIN_FILE ); } return self::$instance; } /** * Declare compatibility now and start once all plugins are loaded. */ public function boot(): void { ( new FeatureCompatibility( $this->plugin_file ) )->register(); add_action( 'plugins_loaded', array( $this, 'start' ), self::START_PRIORITY ); // The parent fires this during its own plugins_loaded wiring, then // seals the registry - before start() runs. A parent without the // Extension API never fires it. add_action( 'adaf_register_extensions', array( $this, 'register_extensions' ) ); } /** * The path-D gate, shared by the classifier it feeds and the hooks it * runs on. */ public function gate(): PathDGate { $this->gate ??= new PathDGate( new PathDBindings() ); return $this->gate; } /** * Register with the parent's Extension API, when every requirement * holds; otherwise register nothing, and start() explains why. * * @param \AgentFirewall\Extension\Registry $registry The parent's registry. */ public function register_extensions( \AgentFirewall\Extension\Registry $registry ): void { if ( ! ( $this->requirements )()->met() ) { return; } // This runs inside the parent's own plugins_loaded wiring: a throw // here would take the firewall down with it. try { $this->refused = ( new WooExtension( $this->gate() ) )->register( $registry ); } catch ( \Throwable $failure ) { $this->refused = array( get_class( $failure ) ); } } /** * What the parent refused at registration, or null when the add-on * never registered. * * @return list|null */ public function refused(): ?array { return $this->refused; } /** * Start the add-on, or explain why it cannot. */ public function start(): void { $unmet = ( $this->requirements )()->unmet(); if ( array() !== $unmet ) { ( new RequirementsNotice( $unmet ) )->register(); return; } $this->running = true; $this->warn(); ( new RefundGuard() )->register(); ( new CrudBackstop() )->register(); ( new MetaBackstop() )->register(); ( new SchedulerAttribution() )->register(); $bindings = new KeyBindings(); $mcp = new McpKeyAuth(); $mcp->register(); ( new KeyAuth( $bindings, static fn(): ?object => KeyAuth::woocommerce_key() ?? $mcp->key() ) )->register(); ( new KeyBindingsController( $bindings ) )->register(); ( new RecommendedRulesController() )->register(); ( new Capabilities() )->register(); ( new StoreContextController() )->register(); ( new OrderPanel() )->register(); ( new AdminAssets( plugin_dir_path( $this->plugin_file ), plugin_dir_url( $this->plugin_file ) ) )->register(); $this->gate()->register(); ( new McpBindingsController( new PathDBindings() ) )->register(); ( new PathDNotice() )->register(); /** * Fires once the add-on's requirements are met and it is running. * Feature epics register their Extension API services here. * * @param Plugin $plugin The running add-on. */ do_action( 'adafwc_ready', $this ); } /** * Tell the administrator what a running add-on cannot do on its own: a * protection the parent refused, or never took, and store price decimals * that changed under rules written in minor units. */ private function warn(): void { if ( null === $this->refused || array() !== $this->refused ) { ( new AdminNotice( __( 'Agent Firewall for WooCommerce could not register all of its protections with Agent Firewall. Its WooCommerce rules may not apply; update both plugins.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), $this->refused ?? array( __( 'Agent Firewall did not ask the add-on to register.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ) ), 'error' ) )->register(); } $currency = new StoreCurrency(); if ( $currency->decimals_changed() ) { ( new AdminNotice( __( 'Your store\'s price decimals changed.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), array( sprintf( /* translators: %d: number of decimals the firewall rules use. */ __( 'Agent Firewall rules store amounts in minor units, so the add-on keeps measuring amounts with %d decimals, as when the rules were written. Review your amount rules before relying on them.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), $currency->decimals() ), ) ) )->register(); } } /** * Whether the add-on started. */ public function is_running(): bool { return $this->running; } }