proUpsell ??= new ProUpsell(); } /** * The settings page / option-group slug. * * Exposed so PRO add-ons can register their own settings against the same * options.php group and render under the same WooCommerce submenu. */ public static function pageSlug(): string { return self::PAGE; } 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 enqueueAssets(string $hook): void { if ($hook !== 'woocommerce_page_' . self::PAGE) { return; } wp_enqueue_style( 'tipping-admin', \TIPPING_URL . 'assets/css/admin.css', [], \Tipping\VERSION, ); } public function addMenuPage(): void { add_submenu_page( 'woocommerce', __('Dankon Tips', 'dankon'), __('Dankon Tips', 'dankon'), 'manage_woocommerce', self::PAGE, [$this, 'renderPage'], ); } public function registerSettings(): void { register_setting( self::PAGE, Options::OPTION, [ 'type' => 'array', 'sanitize_callback' => [$this, 'sanitize'], ], ); // The menu uses manage_woocommerce; align the options.php save capability. add_filter( 'option_page_capability_' . self::PAGE, static fn (): string => 'manage_woocommerce', ); } public function renderPage(): void { if (! current_user_can('manage_woocommerce')) { return; } // Deliberately the RAW settings, never Options::resolved(): an empty text // field must stay empty here. Rendering the resolved default into the // input would save the merchant's current language back into the option // on the next submit and freeze it there. $settings = $this->options->all(); $type = $this->options->type(); $presets = $this->options->presets(); // Shown as placeholders instead, so the merchant can read the default // they get when they leave a field empty, in their own language. $textDefaults = Texts::defaults(); ?>

proUpsell()->banner(); ?>

proUpsell()->aside(); ?>
proUpsell()->cards(); ?>
$presets Cleaned preset values. */ private function renderPresetPreview(string $type, array $presets): void { if ([] === $presets) { return; } ?> */ public function sanitize(mixed $raw): array { if (! is_array($raw)) { $raw = []; } $type = isset($raw['type']) ? sanitize_key((string) $raw['type']) : 'percent'; if (! in_array($type, Options::TYPES, true)) { $type = 'percent'; } $sanitized = [ 'enabled' => ! empty($raw['enabled']), 'label' => isset($raw['label']) ? sanitize_text_field((string) $raw['label']) : '', 'description' => isset($raw['description']) ? sanitize_textarea_field((string) $raw['description']) : '', 'show_description' => ! empty($raw['show_description']), 'type' => $type, 'presets' => $this->parsePresets($raw['presets'] ?? ''), ]; $this->options->flush(); return $sanitized; } /** * Parse a comma-separated preset string into a clean list of positive * numbers, capped to keep the control usable. * * @param mixed $raw * @return list */ private function parsePresets(mixed $raw): array { if (is_array($raw)) { $parts = $raw; } else { $parts = explode(',', (string) $raw); } $out = []; foreach ($parts as $part) { $value = (float) wc_format_decimal((string) $part); if ($value > 0) { $out[] = round($value, 2); } if (count($out) >= 8) { break; } } return array_values($out); } /** * Format a stored number without trailing zeros for the admin text field. */ public function formatNumber(float $value): string { return Options::formatNumber($value); } }