proUpsell ??= new ProUpsell(); } public function registerHooks(): void { add_action('admin_menu', [$this, 'addMenuPage']); add_action('admin_init', [$this, 'registerSettings']); add_action('admin_enqueue_scripts', [$this, 'enqueueAssets']); $this->proUpsell()->registerHooks(); } public function addMenuPage(): void { $this->hookSuffix = add_submenu_page( 'woocommerce', __('Bundaro: product bundles', 'bundaro'), __('Bundle', 'bundaro'), 'manage_woocommerce', self::PAGE, [$this, 'renderPage'], ) ?: null; } /** * Enqueue the settings-page polish (card styling + accessible help bubbles). * Loaded only on this plugin's settings screen. */ public function enqueueAssets(string $hook): void { if ($this->hookSuffix === null || $hook !== $this->hookSuffix) { return; } wp_enqueue_style( 'bundle-admin', BUNDLE_URL . 'assets/css/admin.css', [], \Bundle\VERSION, ); wp_enqueue_script( 'bundle-admin', BUNDLE_URL . 'assets/js/admin.js', [], \Bundle\VERSION, ['in_footer' => true], ); } /** * Render a "?" help affordance and the accessible popover bubble it controls. * The bubble is a native [popover] wired via aria-describedby, so the help * text is always reachable by keyboard and screen readers. */ private function help(string $id, string $text): string { return sprintf( '' . '%3$s', esc_attr($id), esc_attr__('More information', 'bundaro'), esc_html($text), ); } public function registerSettings(): void { register_setting( self::PAGE, self::OPTION, [ 'type' => 'array', 'sanitize_callback' => [$this, 'sanitize'], ], ); // The menu uses manage_woocommerce; align the options.php save capability // so shop managers (not just admins with manage_options) can save. add_filter( 'option_page_capability_' . self::PAGE, static fn (): string => 'manage_woocommerce', ); } public function renderPage(): void { if (! current_user_can('manage_woocommerce')) { return; } $settings = $this->settings(); $mode = (string) ($settings['discount_mode'] ?? 'fee'); $option = self::OPTION; // Allowed HTML for the help affordances (button + popover span). $help_html = [ 'button' => [ 'type' => true, 'class' => true, 'aria-describedby' => true, 'aria-label' => true, ], 'span' => [ 'id' => true, 'class' => true, 'popover' => true, 'role' => true, ], ]; ?>

proUpsell()->banner(); ?>

[bundle]', '[bundle id="123"]' ); ?>

proUpsell()->aside(); ?>
proUpsell()->cards(); ?>
*/ public function sanitize(mixed $raw): array { if (! is_array($raw)) { $raw = []; } $current = $this->settings(); $mode = isset($raw['discount_mode']) ? sanitize_text_field((string) $raw['discount_mode']) : 'fee'; if (! in_array($mode, ['fee', 'per_item'], true)) { $mode = 'fee'; } $boxTitle = isset($raw['box_title']) ? sanitize_text_field((string) $raw['box_title']) : ''; $addLabel = isset($raw['add_label']) ? sanitize_text_field((string) $raw['add_label']) : ''; $feeLabel = isset($raw['fee_label']) ? sanitize_text_field((string) $raw['fee_label']) : ''; // The three wording fields say "leave blank to use the default", but the // blank used to be backfilled from $this->settings(), which is the // stored value itself. A merchant who cleared "Box title" got their old // title saved straight back, and shoppers kept reading the wording the // merchant had just deleted. Store the blank and let // BundleService::label() resolve the packaged, translated default. return array_merge($current, [ 'enabled' => ! empty($raw['enabled']), 'show_on_single' => ! empty($raw['show_on_single']), 'show_items' => ! empty($raw['show_items']), 'show_savings' => ! empty($raw['show_savings']), 'discount_mode' => $mode, 'box_title' => $boxTitle, 'add_label' => $addLabel, 'fee_label' => $feeLabel, ]); } /** * Stored settings merged over packaged defaults, RAW. * * Deliberately never passed through {@see \Bundle\Service\Texts}: the * wording fields must render empty when the merchant has typed nothing, so * the placeholder shows the translated default and a save stores a blank. * Resolving here would put one language into the input and freeze it into * the option on the next save. * * @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 array_merge($defaults, $stored); } }