options->isUsable()) { return; } if (! function_exists('is_checkout') || ! is_checkout()) { return; } wp_enqueue_style( 'tipping', \TIPPING_URL . 'assets/css/tipping.css', [], \Tipping\VERSION, ); wp_enqueue_script( 'tipping', \TIPPING_URL . 'assets/js/tipping.js', ['jquery'], \Tipping\VERSION, ['in_footer' => true, 'strategy' => 'defer'], ); wp_localize_script('tipping', 'TippingData', [ 'ajaxUrl' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('tipping_set'), ]); } /** * Render the control. Guards every exit so a misconfigured or disabled state * shows nothing rather than a broken widget. */ public function render(): void { if (! $this->options->isUsable()) { return; } $presets = $this->options->presets(); $current = $this->selection->current(); $template = \TIPPING_DIR . 'templates/tip-control.php'; if (! is_readable($template)) { return; } $context = [ 'options' => $this->options, 'presets' => $presets, 'current' => $current, 'recipients' => $this->recipients->all(), 'isPercent' => $this->options->isPercent(), 'label' => $this->options->label(), 'description' => $this->options->description(), ]; $this->renderTemplate($template, $context); } /** * Format a preset value for display: "10%" or "7.5%" for percentages, a * currency amount for fixed presets. */ public static function formatPreset(float $value, bool $isPercent): string { if ($isPercent) { // The presets field takes decimals and the admin preview shows them, // so a merchant saving 2.5 saw "2.5%" there. This cast the value to an // int, so the shopper pressed a button labelled "3%" and was charged // 2.5% of the subtotal. Format it the way the admin already does. /* translators: %s: a percentage, e.g. 10 or 7.5. */ return sprintf(__('%s%%', 'dankon'), Options::formatNumber($value)); } return wp_strip_all_tags(wc_price($value)); } /** * @param array $context */ private function renderTemplate(string $file, array $context): void { extract($context, EXTR_SKIP); require $file; } }