= 1.5.0. When present, wire it // with this plugin's text-domain / request keys / meta key. Otherwise // leave the service inert (see registerHooks()). if (! class_exists(ProductBundleEngine::class)) { return; } $this->engine = new ProductBundleEngine( requestKey: self::REQUEST_KEY, nonceAction: self::NONCE_ACTION, cartFlag: self::CART_FLAG, boxTemplate: self::BOX_TEMPLATE, labels: $this->labels(), isEnabled: fn (): bool => $this->isEnabled(), settings: fn (): array => $this->settings(), productMeta: fn (\WC_Product $product): mixed => $this->bundleMeta($product), renderTemplate: function (string $template, array $context): void { $this->renderTemplate($template, $context); }, ); } public function registerHooks(): void { if (! $this->engine instanceof ProductBundleEngine) { // storefront-kit < 1.5.0 has no ProductBundleEngine. Bump the // `wppoland/storefront-kit` constraint (composer update) to enable // product bundles. No hooks are registered until the engine exists. return; } $this->engine->registerHooks(); add_action('wp_enqueue_scripts', [$this, 'enqueueAssets']); add_shortcode(self::SHORTCODE, [$this, 'renderShortcode']); add_shortcode('bundaro', [$this, 'renderShortcode']); } public function enqueueAssets(): void { if (! $this->isEnabled() || (! is_product() && ! $this->postHasShortcode())) { return; } wp_enqueue_style( 'bundle', BUNDLE_URL . 'assets/css/bundle.css', [], \Bundle\VERSION, ); } /** * `[bundle]` / `[bundle id="123"]`, render a bundle box for the given * product (defaults to the current product in the loop). Lets merchants * place the box anywhere (page, post, block) rather than only after the * product summary. Returns an empty string when the product has no bundle. * * @param array|string $atts */ public function renderShortcode(array|string $atts = []): string { if (! $this->engine instanceof ProductBundleEngine || ! $this->isEnabled()) { return ''; } $atts = shortcode_atts(['id' => 0], is_array($atts) ? $atts : [], self::SHORTCODE); $productId = absint($atts['id']); if ($productId === 0) { $productId = (int) get_the_ID(); } $product = $productId > 0 && $this->isViewable($productId) ? wc_get_product($productId) : null; $context = $product instanceof \WC_Product ? $this->engine->boxContext($product) : null; if ($context === null) { return ''; } ob_start(); $this->renderTemplate(self::BOX_TEMPLATE, $context); return (string) ob_get_clean(); } /** * Whether the queried post embeds the `[bundle]` shortcode, so the stylesheet * is enqueued on pages that place the box outside the product template. */ private function postHasShortcode(): bool { $post = get_post(); return $post instanceof \WP_Post && has_shortcode((string) $post->post_content, self::SHORTCODE); } /** * The bundle box wording under the keys the engine expects: the merchant's * own chrome where they set it, the translated default from * {@see Texts} where they did not. * * @return array{box_title: string, add_bundle: string, fee_label: string, add_failed: string, expired: string} */ private function labels(): array { $settings = $this->settings(); return [ 'box_title' => (string) $settings['box_title'], 'add_bundle' => (string) $settings['add_label'], 'fee_label' => (string) $settings['fee_label'], 'add_failed' => (string) $settings['add_failed_text'], 'expired' => __('Your session expired, please try again.', 'bundaro'), ]; } private function isEnabled(): bool { return (bool) ($this->settings()['enabled'] ?? false); } /** * Read the raw bundle definition stored as product meta. * * Returns the stored array (`items` => list, `discount_percent` => * float) or null when no bundle is configured. The engine normalises it. * * @return array|null */ private function bundleMeta(\WC_Product $product): ?array { $raw = $product->get_meta(self::META_BUNDLE); if (! is_array($raw) || $raw === []) { return null; } // A draft or private companion is never listed, linked or added. $raw['items'] = array_values(array_filter( (array) ($raw['items'] ?? []), fn (mixed $itemId): bool => $this->isViewable(absint($itemId)), )); return $raw; } /** * Whether the current visitor may see this product. A variation answers * for its parent. A password-protected product needs its password from * everyone, as in core: read_post on a published post is the plain * 'read' cap every customer holds, so it cannot stand in for the password. * Otherwise the product must be published, or readable by the current * user (their own draft, a private product they may read). */ private function isViewable(int $productId): bool { $postId = get_post_type($productId) === 'product_variation' ? (int) wp_get_post_parent_id($productId) : $productId; if ($postId <= 0 || post_password_required($postId)) { return false; } return get_post_status($postId) === 'publish' || current_user_can('read_post', $postId); } /** * Stored settings merged over packaged defaults, resolved for RENDERING. * * Every customer-facing string left empty is filled from {@see Texts} here, * on the way out. Nothing resolved is ever written back to the option; the * admin screen reads the raw values so a save cannot freeze one language in. * * @return array */ private function settings(): array { $stored = get_option(self::OPTION, []); if (! is_array($stored)) { $stored = []; } /** @var array $defaults */ $defaults = require BUNDLE_DIR . 'config/defaults.php'; return Texts::apply(array_merge($defaults, $stored)); } /** * @param array $context */ private function renderTemplate(string $template, array $context): void { $file = BUNDLE_DIR . 'templates/' . $template . '.php'; if (! is_readable($file)) { return; } extract($context, EXTR_SKIP); require $file; } }