*/ public function parse_level_products(string $raw): array { $lines = preg_split('/\r\n|\r|\n/', $raw) ?: []; $out = []; foreach ($lines as $line) { $line = trim((string) $line); if ($line === '') { continue; } $parts = array_map('trim', explode('|', $line)); $price = isset($parts[0]) ? (float) $parts[0] : 0.0; if ($price <= 0) { continue; } $n = count($parts); // New format: price|show_price|time|unit|tag|title (6+) // Or: price|show_price|time|unit|title (5) if ($n >= 4 && (in_array(strtolower((string) ($parts[3] ?? '')), ['day', 'month', 'd', 'm'], true) || (isset($parts[3]) && $parts[3] !== '' && !is_numeric($parts[3])))) { $show_price = isset($parts[1]) ? (float) $parts[1] : 0.0; $time = isset($parts[2]) ? (int) $parts[2] : 0; $unit_raw = strtolower((string) ($parts[3] ?? 'month')); $unit = ($unit_raw === 'day' || $unit_raw === 'd') ? 'day' : 'month'; $tag = ''; $title = ''; if ($n >= 6) { $tag = sanitize_text_field((string) ($parts[4] ?? '')); $title = sanitize_text_field((string) ($parts[5] ?? '')); } elseif ($n === 5) { // price|show|time|unit|title $title = sanitize_text_field((string) ($parts[4] ?? '')); } } else { // Legacy: price|days|title $show_price = 0.0; $days_legacy = isset($parts[1]) ? (int) $parts[1] : 0; $time = $days_legacy; $unit = 'day'; $tag = ''; $title = isset($parts[2]) ? sanitize_text_field($parts[2]) : ''; } $time = max(0, $time); $days = $this->duration_to_days($time, $unit); if ($title === '') { if ($time <= 0) { $title = __('永久会员', 'aksrt'); } elseif ($unit === 'day') { $title = sprintf(__('%d天会员', 'aksrt'), $time); } else { $title = sprintf(__('%d个月会员', 'aksrt'), $time); } } $out[] = [ 'price' => round($price, 2), 'show_price' => max(0.0, round($show_price, 2)), 'time' => $time, 'unit' => $unit, 'tag' => $tag, 'tag_class' => '', 'title' => $title, 'days' => $days, ]; } return $out; } /** * Approximate days for catalog labels / order meta; activate prefers time+unit calendar math. */ public function duration_to_days(int $time, string $unit = 'day'): int { if ($time <= 0) { return 0; } if ($unit === 'month') { return $time * 30; } return $time; } /** * @deprecated Use parse_level_products — kept as thin adapter for upgrade fixed lines. * * @return array */ public function parse_packages(string $raw): array { $parsed = $this->parse_level_products($raw); $out = []; foreach ($parsed as $p) { $out[] = [ 'price' => (float) $p['price'], 'days' => (int) $p['days'], 'title' => (string) $p['title'], ]; } return $out; } /** * Build upgrade package items (zibll vip_upgrade_product parity). * * upgrade.product multi-line formats: * - permanent VIP1: permanent|price|title[|show_price] * - fixed (alias): price|days|title * - timed unit: unit|daily_price|title * - timed jump permanent: jump|price|title[|show_price] * * Rules: * - permanent user: only permanent/fixed (days=0 preferred) * - timed user: unit (keep expiry) + optional jump (to permanent) * * package index: 901 permanent/fixed, 902 unit, 903 jump * * @return array> */ private function build_upgrade_items(int $user_id, int $expires, bool $is_permanent, string $raw): array { $raw = trim($raw); if ($raw === '') { return []; } $items = []; $lines = preg_split('/\r\n|\r|\n/', $raw) ?: []; $has_permanent = false; $has_unit = false; $has_jump = false; foreach ($lines as $line) { $line = trim((string) $line); if ($line === '') { continue; } $parts = array_map('trim', explode('|', $line)); $mode = strtolower((string) ($parts[0] ?? '')); // unit|daily|title — timed VIP1 only. if ($mode === 'unit') { if ($is_permanent || $expires <= time() || $has_unit) { continue; } $daily = isset($parts[1]) ? (float) $parts[1] : 0.0; if ($daily <= 0) { continue; } $remaining = (int) ceil(max(1, ($expires - time()) / DAY_IN_SECONDS)); $price = round(max(0.01, $remaining * $daily), 2); $title = sanitize_text_field((string) ($parts[2] ?? '')); if ($title === '') { $title = sprintf(__('按剩余 %d 天升级', 'aksrt'), $remaining); } $items[] = [ 'package_id' => $this->encode_package_id(2, 902), 'level' => 2, 'index' => 902, 'action' => 'upgrade', 'price' => $price, 'base_price' => $price, 'show_price' => 0.0, 'discount_tag' => __('剩余天数', 'aksrt'), 'days' => $remaining, 'title' => $title, 'price_label' => $this->format_cash_label($price), 'show_price_label' => '', 'days_label' => sprintf(__('%d 天', 'aksrt'), $remaining), 'cta_label' => __('立即升级', 'aksrt'), 'is_upgrade_product' => true, 'upgrade_mode' => 'unit', ]; $has_unit = true; continue; } // jump|price|title[|show_price] — timed VIP1 only → permanent VIP2. if ($mode === 'jump') { if ($is_permanent || $has_jump) { continue; } $price = isset($parts[1]) ? (float) $parts[1] : 0.0; if ($price <= 0) { continue; } $title = sanitize_text_field((string) ($parts[2] ?? '')); if ($title === '') { $title = __('升级为永久会员', 'aksrt'); } $show = isset($parts[3]) ? (float) $parts[3] : 0.0; $items[] = [ 'package_id' => $this->encode_package_id(2, 903), 'level' => 2, 'index' => 903, 'action' => 'upgrade', 'price' => round($price, 2), 'base_price' => round($price, 2), 'show_price' => max(0.0, $show), 'discount_tag' => __('永久升级', 'aksrt'), 'days' => 0, 'title' => $title, 'price_label' => $this->format_cash_label($price), 'show_price_label' => $show > 0 ? $this->format_cash_label($show) : '', 'days_label' => __('永久', 'aksrt'), 'cta_label' => __('立即升级', 'aksrt'), 'is_upgrade_product' => true, 'upgrade_mode' => 'jump', ]; $has_jump = true; continue; } // permanent|price|title[|show_price] if ($mode === 'permanent') { if ($has_permanent) { continue; } $price = isset($parts[1]) ? (float) $parts[1] : 0.0; if ($price <= 0) { continue; } $title = sanitize_text_field((string) ($parts[2] ?? '')); if ($title === '') { $title = __('永久会员升级', 'aksrt'); } $show = isset($parts[3]) ? (float) $parts[3] : 0.0; $items[] = [ 'package_id' => $this->encode_package_id(2, 901), 'level' => 2, 'index' => 901, 'action' => 'upgrade', 'price' => round($price, 2), 'base_price' => round($price, 2), 'show_price' => max(0.0, $show), 'discount_tag' => __('升级专享', 'aksrt'), 'days' => 0, 'title' => $title, 'price_label' => $this->format_cash_label($price), 'show_price_label' => $show > 0 ? $this->format_cash_label($show) : '', 'days_label' => __('永久', 'aksrt'), 'cta_label' => __('立即升级', 'aksrt'), 'is_upgrade_product' => true, 'upgrade_mode' => 'permanent', ]; $has_permanent = true; continue; } // fixed: price|days|title $parsed = $this->parse_packages($line); if (empty($parsed[0]) || $has_permanent) { continue; } // For permanent VIP1, only accept days=0 fixed lines as permanent upgrade. if ($is_permanent && (int) $parsed[0]['days'] > 0) { continue; } $pkg = $parsed[0]; $items[] = [ 'package_id' => $this->encode_package_id(2, 901), 'level' => 2, 'index' => 901, 'action' => 'upgrade', 'price' => (float) $pkg['price'], 'base_price' => (float) $pkg['price'], 'show_price' => 0.0, 'discount_tag' => __('升级专享', 'aksrt'), 'days' => (int) $pkg['days'], 'title' => (string) $pkg['title'], 'price_label' => $this->format_cash_label((float) $pkg['price']), 'show_price_label' => '', 'days_label' => ((int) $pkg['days'] <= 0) ? __('永久', 'aksrt') : sprintf(__('%d 天', 'aksrt'), (int) $pkg['days']), 'cta_label' => __('立即升级', 'aksrt'), 'is_upgrade_product' => true, 'upgrade_mode' => ((int) $pkg['days'] <= 0) ? 'permanent' : 'fixed', ]; $has_permanent = true; } // Permanent VIP1 should not see unit/jump. if ($is_permanent) { $items = array_values(array_filter($items, static function ($it) { $m = (string) ($it['upgrade_mode'] ?? ''); return in_array($m, ['permanent', 'fixed'], true); })); } return $items; } /** * Points exchange packages: level|points|days|title * * @param array $vip * @param array $levels * @return array> */ public function get_points_exchange_packages(array $vip, array $levels): array { $points_ex = is_array($vip['points_exchange'] ?? null) ? $vip['points_exchange'] : []; $raw = (string) ($points_ex['packages_raw'] ?? ''); $currency = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::get_currency_name() : __('积分', 'aksrt'); $out = []; $lines = preg_split('/\r\n|\r|\n/', $raw) ?: []; $i = 0; foreach ($lines as $line) { $line = trim((string) $line); if ($line === '') { continue; } $parts = array_map('trim', explode('|', $line)); if (count($parts) < 3) { continue; } $lv = (int) ($parts[0] ?? 0); $pts = (int) ($parts[1] ?? 0); $days = (int) ($parts[2] ?? 0); $title = sanitize_text_field((string) ($parts[3] ?? '')); if ($lv < 1 || $lv > 2 || $pts <= 0) { continue; } if (empty($levels[$lv]['enabled'])) { continue; } $i++; if ($title === '') { $lv_name = (string) ($levels[$lv]['name'] ?? ('VIP' . $lv)); $title = $days > 0 ? sprintf(__('%1$s · %2$d天', 'aksrt'), $lv_name, $days) : sprintf(__('%s · 永久', 'aksrt'), $lv_name); } $out[] = [ 'package_id' => 5000 + $i, 'level' => $lv, 'index' => $i, 'points' => $pts, 'days' => max(0, $days), 'title' => $title, 'price_label' => $pts . ' ' . $currency, 'days_label' => $days > 0 ? sprintf(__('%d 天', 'aksrt'), $days) : __('永久', 'aksrt'), 'cta_label' => __('立即兑换', 'aksrt'), ]; } return $out; } /** * Single upgrade product line: price|days|title * * @return array{price:float,days:int,title:string}|null */ public function parse_upgrade_product_line(string $raw): ?array { $raw = trim($raw); if ($raw === '') { return null; } $lines = preg_split('/\r\n|\r|\n/', $raw) ?: []; foreach ($lines as $line) { $line = trim((string) $line); if ($line === '' || str_starts_with(strtolower($line), 'unit|')) { continue; } $parsed = $this->parse_packages($line); if (!empty($parsed[0])) { return $parsed[0]; } } return null; } /** * Apply renew discount/reduce to same-level packages. * * @param array $renew * @param array $upgrade * @return array{price:float,show_price:float,tag:string}|null */ private function apply_action_price( float $base_price, string $action, array $renew, array $upgrade, int $target_level, int $user_level, int $expires, bool $is_permanent, int $days ): ?array { if ($base_price <= 0) { return null; } if ($action === 'renew' && $is_permanent) { return null; } if ($action !== 'renew') { return [ 'price' => round($base_price, 2), 'show_price' => 0.0, 'tag' => '', ]; } $type = sanitize_key((string) ($renew['price_type'] ?? 'discount')); $price = $base_price; $show = $base_price; $tag = ''; if ($type === 'discount') { $discount = (float) ($renew['discount'] ?? 8); if ($discount <= 0) { $discount = 8; } if ($discount <= 10) { $price = round($base_price * ($discount / 10), 2); $tag = sprintf( /* translators: %s: discount like 8 */ __('%s折续费', 'aksrt'), rtrim(rtrim(number_format($discount, 1, '.', ''), '0'), '.') ); } else { $price = round($base_price * (min(100, $discount) / 100), 2); $tag = sprintf(__('%s%% 续费价', 'aksrt'), (string) (int) $discount); } } elseif ($type === 'reduce') { $reduce = max(0, (float) ($renew['reduce'] ?? 20)); $price = round(max(0.01, $base_price - $reduce), 2); $tag = sprintf( /* translators: %s: amount */ __('续费立减%s', 'aksrt'), $this->format_cash_label($reduce) ); } else { $show = 0.0; $tag = __('续费同价', 'aksrt'); } if ($price < 0.01) { $price = 0.01; } if ($show > 0 && $price >= $show) { $show = 0.0; $tag = __('续费', 'aksrt'); } return [ 'price' => $price, 'show_price' => $show > $price ? $show : 0.0, 'tag' => $tag, ]; } public function encode_package_id(int $level, int $index): int { return (max(1, min(2, $level)) * 1000) + max(1, $index); } /** * @return array{level:int,index:int}|null */ public function decode_package_id(int $package_id): ?array { if ($package_id < 1001) { return null; } $level = intdiv($package_id, 1000); $index = $package_id % 1000; if ($level < 1 || $level > 2 || $index < 1) { return null; } return ['level' => $level, 'index' => $index]; } /** * @return array */ private function available_payment_methods(): array { if (function_exists('aksrt_list_cash_payment_methods')) { return aksrt_list_cash_payment_methods(true); } $methods = []; if (!class_exists('\AKSRT\Payment\Gateway')) { return $methods; } $gw = \AKSRT\Payment\Gateway::get_instance(); foreach (['balance', 'alipay', 'wechatpay', 'paypal', 'stripe', 'epay_alipay', 'epay_wxpay'] as $id) { $g = $gw->get_gateway($id); if (!$g) { continue; } if (method_exists($g, 'is_configured') && !$g->is_configured()) { continue; } $methods[] = [ 'id' => $id, 'name' => method_exists($g, 'get_name') ? (string) $g->get_name() : $id, ]; } return $methods; } private function format_cash_label(float $amount): string { $symbol = '¥'; if (method_exists($this, 'get_paid_display_context')) { $paid = $this->get_paid_display_context(); $symbol = (string) ($paid['display']['currency_symbol'] ?? '¥'); } elseif (class_exists('\AKSRT\Settings\Settings')) { $paid = \AKSRT\Settings\Settings::get_paid_content_settings(); $symbol = (string) ($paid['display']['currency_symbol'] ?? '¥'); } $n = function_exists('aksrt_format_price') ? aksrt_format_price($amount) : (string) $amount; return $symbol . $n; } private function format_expires_label(int $expires, int $level): string { if ($level <= 0) { return __('未开通', 'aksrt'); } if ($expires <= 0) { return __('永久有效', 'aksrt'); } if ($expires < time()) { return __('已过期', 'aksrt'); } return sprintf( /* translators: %s: date */ __('到期时间:%s', 'aksrt'), wp_date('Y-m-d H:i', $expires) ); } }