hook(); } /** The wired service, for integrations and tests. */ public static function instance(): ?self { return self::$instance; } public const CAPS_OPTION = 'abcreate_wd_caps'; public static function activate(): void { Schema::install(); self::grantCaps(); } /** Administrators and shop managers of this site. Also on sites of a network activation. */ private static function grantCaps(): void { foreach (['administrator', 'shop_manager'] as $roleName) { $role = get_role($roleName); if ($role) { $role->add_cap(self::CAP_VIEW); $role->add_cap(self::CAP_MANAGE); } } update_option(self::CAPS_OPTION, '1'); } public function service(): WarrantyService { return $this->service ?? throw new \LogicException('Warranty Desk is not loaded.'); } public function claims(): WpdbClaimRepository { return $this->claims ?? throw new \LogicException('Warranty Desk is not loaded.'); } public function units(): WpdbUnitRepository { return $this->units ?? throw new \LogicException('Warranty Desk is not loaded.'); } public function orders(): WooOrders { return $this->orders ?? throw new \LogicException('Warranty Desk is not loaded.'); } private function hook(): void { add_action('before_woocommerce_init', function (): void { if (class_exists(\Automattic\WooCommerce\Utilities\FeaturesUtil::class)) { \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('custom_order_tables', $this->file, true); \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('cart_checkout_blocks', $this->file, true); } }); add_action('plugins_loaded', function (): void { if (!class_exists('WooCommerce')) { return; // "Requires Plugins: woocommerce" normally prevents this. } Schema::maybeUpgrade(); if ('1' !== get_option(self::CAPS_OPTION)) { self::grantCaps(); } global $wpdb; $events = new EventWriter($wpdb); $this->units = new WpdbUnitRepository($wpdb, $events); $this->claims = new WpdbClaimRepository($wpdb, $events); $log = new WpdbIdempotencyLog($wpdb); $this->orders = new WooOrders(); $this->service = new WarrantyService($this->units, $this->claims, $log, new WpdbTransaction($wpdb), null, wp_timezone()); $mailer = new InformativaMailer($this->orders); // Woo hooks only enqueue; the queued actions write with transactions. $hooks = new WooHooks($this->service, $this->orders, $this->units, $this->claims); $hooks->register(); $service = $this->service; $claims = $this->claims; $units = $this->units; $orders = $this->orders; add_action('rest_api_init', static function () use ($service, $claims, $units, $hooks, $orders, $mailer): void { (new ClaimsController($service, $claims, $units, $hooks, $orders, $mailer))->register(); }); (new HarmonisedNotice())->register(); if (is_admin()) { (new AdminPage($this->file, $this->claims))->register(); (new OrderMetaBox($this->service, $this->claims, $this->orders))->register(); (new Settings($this->file))->register(); (new ProductField())->register(); (new Export($this->claims, $this->units, $this->orders))->register(); } }); } }