format($format); } /** * 获取订阅状态标签 * * @param string $status 状态 * @return string 状态标签 */ public static function get_subscription_status_label($status) { $labels = array( 'active' => __('活跃', 'yoone-subscriptions'), 'paused' => __('暂停', 'yoone-subscriptions'), 'cancelled' => __('已取消', 'yoone-subscriptions'), 'expired' => __('已过期', 'yoone-subscriptions'), 'pending' => __('待处理', 'yoone-subscriptions'), ); return isset($labels[$status]) ? $labels[$status] : $status; } /** * 获取订阅状态颜色 * * @param string $status 状态 * @return string 颜色类名 */ public static function get_subscription_status_color($status) { $colors = array( 'active' => 'success', 'paused' => 'warning', 'cancelled' => 'danger', 'expired' => 'secondary', 'pending' => 'info', ); return isset($colors[$status]) ? $colors[$status] : 'secondary'; } /** * 格式化计费周期 * * @param string $period 周期 * @param int $interval 间隔 * @return string 格式化后的计费周期 */ public static function format_billing_period($period, $interval = 1) { $periods = array( 'day' => array( 'singular' => __('天', 'yoone-subscriptions'), 'plural' => __('天', 'yoone-subscriptions') ), 'week' => array( 'singular' => __('周', 'yoone-subscriptions'), 'plural' => __('周', 'yoone-subscriptions') ), 'month' => array( 'singular' => __('月', 'yoone-subscriptions'), 'plural' => __('月', 'yoone-subscriptions') ), 'year' => array( 'singular' => __('年', 'yoone-subscriptions'), 'plural' => __('年', 'yoone-subscriptions') ), ); if (!isset($periods[$period])) { return $period; } $period_text = $interval == 1 ? $periods[$period]['singular'] : $periods[$period]['plural']; if ($interval == 1) { return sprintf(__('每%s', 'yoone-subscriptions'), $period_text); } else { return sprintf(__('每%d%s', 'yoone-subscriptions'), $interval, $period_text); } } /** * 获取周期标签 * * @param string $period 周期类型 * @param bool $plural 是否复数形式 * @return string 周期标签 */ public static function get_period_label($period, $plural = false) { $periods = array( 'day' => array( 'singular' => __('天', 'yoone-subscriptions'), 'plural' => __('天', 'yoone-subscriptions') ), 'week' => array( 'singular' => __('周', 'yoone-subscriptions'), 'plural' => __('周', 'yoone-subscriptions') ), 'month' => array( 'singular' => __('月', 'yoone-subscriptions'), 'plural' => __('月', 'yoone-subscriptions') ), 'year' => array( 'singular' => __('年', 'yoone-subscriptions'), 'plural' => __('年', 'yoone-subscriptions') ), ); if (!isset($periods[$period])) { return $period; } return $plural ? $periods[$period]['plural'] : $periods[$period]['singular']; } /** * 计算下次付款日期 * * @param string $last_payment 上次付款日期 * @param string $period 周期 * @param int $interval 间隔 * @return string 下次付款日期 */ public static function calculate_next_payment_date($last_payment, $period, $interval = 1) { $datetime = new DateTime($last_payment); switch ($period) { case 'day': $datetime->add(new DateInterval('P' . $interval . 'D')); break; case 'week': $datetime->add(new DateInterval('P' . ($interval * 7) . 'D')); break; case 'month': $datetime->add(new DateInterval('P' . $interval . 'M')); break; case 'year': $datetime->add(new DateInterval('P' . $interval . 'Y')); break; } return $datetime->format('Y-m-d H:i:s'); } /** * 验证邮箱 * * @param string $email 邮箱 * @return bool 是否有效 */ public static function is_valid_email($email) { return is_email($email); } /** * 生成随机字符串 * * @param int $length 长度 * @return string 随机字符串 */ public static function generate_random_string($length = 32) { return wp_generate_password($length, false); } /** * 获取当前用户的订阅 * * @param int $user_id 用户ID * @return array 订阅列表 */ public static function get_user_subscriptions($user_id = 0) { if (!$user_id) { $user_id = get_current_user_id(); } global $wpdb; $subscriptions = $wpdb->get_results($wpdb->prepare(" SELECT * FROM {$wpdb->prefix}yoone_subscriptions WHERE customer_id = %d ORDER BY created_at DESC ", $user_id)); return $subscriptions; } /** * 检查产品是否启用订阅 * * @param int $product_id 产品ID * @return bool 是否启用订阅 */ public static function is_subscription_product($product_id) { return get_post_meta($product_id, '_subscription_enabled', true) === 'yes'; } /** * 检查产品是否是混装产品 * * @param int $product_id 产品ID * @return bool 是否是混装产品 */ public static function is_bundle_product($product_id) { $product = wc_get_product($product_id); return $product && $product->get_type() === 'yoone_bundle'; } /** * 获取订阅产品的折扣价格 * * @param int $product_id 产品ID * @param float $original_price 原价 * @return float 折扣后价格 */ public static function get_subscription_discounted_price($product_id, $original_price) { $discount_type = get_post_meta($product_id, '_subscription_discount_type', true); $discount_value = floatval(get_post_meta($product_id, '_subscription_discount_value', true)); if (!$discount_value) { return $original_price; } if ($discount_type === 'percentage') { return $original_price * (1 - $discount_value / 100); } else { return max(0, $original_price - $discount_value); } } /** * 记录日志 * * @param string $message 消息 * @param string $level 级别 * @param string $source 来源 */ public static function log($message, $level = 'info', $source = 'yoone-subscriptions') { if (class_exists('WC_Logger')) { $logger = wc_get_logger(); $logger->log($level, $message, array('source' => $source)); } } /** * 发送邮件通知 * * @param string $to 收件人 * @param string $subject 主题 * @param string $message 内容 * @param array $headers 头部 * @return bool 是否发送成功 */ public static function send_email($to, $subject, $message, $headers = array()) { if (!self::is_valid_email($to)) { return false; } return wp_mail($to, $subject, $message, $headers); } /** * 获取货币符号 * * @return string 货币符号 */ public static function get_currency_symbol() { return get_woocommerce_currency_symbol(); } /** * 清理HTML标签 * * @param string $content 内容 * @return string 清理后的内容 */ public static function clean_html($content) { return wp_strip_all_tags($content); } /** * 转义HTML属性 * * @param string $text 文本 * @return string 转义后的文本 */ public static function esc_attr($text) { return esc_attr($text); } /** * 转义HTML * * @param string $text 文本 * @return string 转义后的文本 */ public static function esc_html($text) { return esc_html($text); } /** * 获取插件版本 * * @return string 版本号 */ public static function get_plugin_version() { return YOONE_SUBSCRIPTIONS_VERSION; } /** * 检查是否是管理员 * * @return bool 是否是管理员 */ public static function is_admin() { return current_user_can('manage_woocommerce'); } /** * 获取页面URL * * @param string $page 页面 * @param array $args 参数 * @return string URL */ public static function get_admin_url($page, $args = array()) { $url = admin_url('admin.php?page=' . $page); if (!empty($args)) { $url = add_query_arg($args, $url); } return $url; } }