get_charset_collate();
// 订阅表
$subscriptions_table = "
CREATE TABLE {$wpdb->prefix}yoone_subscriptions (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
customer_id bigint(20) unsigned NOT NULL,
parent_order_id bigint(20) unsigned DEFAULT NULL,
status varchar(20) NOT NULL DEFAULT 'pending',
billing_period varchar(20) NOT NULL DEFAULT 'month',
billing_interval int(11) NOT NULL DEFAULT 1,
start_date datetime NOT NULL,
next_payment_date datetime DEFAULT NULL,
end_date datetime DEFAULT NULL,
trial_end_date datetime DEFAULT NULL,
total decimal(10,2) NOT NULL DEFAULT 0.00,
currency varchar(3) NOT NULL DEFAULT 'CAD',
payment_method varchar(50) DEFAULT NULL,
payment_token varchar(255) DEFAULT NULL,
created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY customer_id (customer_id),
KEY parent_order_id (parent_order_id),
KEY status (status),
KEY next_payment_date (next_payment_date)
) $charset_collate;
";
// 订阅商品表
$subscription_items_table = "
CREATE TABLE {$wpdb->prefix}yoone_subscription_items (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
subscription_id bigint(20) unsigned NOT NULL,
product_id bigint(20) unsigned NOT NULL,
variation_id bigint(20) unsigned DEFAULT NULL,
quantity int(11) NOT NULL DEFAULT 1,
line_total decimal(10,2) NOT NULL DEFAULT 0.00,
line_subtotal decimal(10,2) NOT NULL DEFAULT 0.00,
created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY subscription_id (subscription_id),
KEY product_id (product_id)
) $charset_collate;
";
// 混装组合表
$bundles_table = "
CREATE TABLE {$wpdb->prefix}yoone_bundles (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
description text,
status varchar(20) NOT NULL DEFAULT 'active',
discount_type varchar(20) DEFAULT 'percentage',
discount_value decimal(10,2) DEFAULT 0.00,
min_quantity int(11) DEFAULT 1,
max_quantity int(11) DEFAULT NULL,
created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY status (status)
) $charset_collate;
";
// 混装商品表
$bundle_items_table = "
CREATE TABLE {$wpdb->prefix}yoone_bundle_items (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
bundle_id bigint(20) unsigned NOT NULL,
product_id bigint(20) unsigned NOT NULL,
quantity int(11) NOT NULL DEFAULT 1,
discount_type varchar(20) DEFAULT 'none',
discount_value decimal(10,2) DEFAULT 0.00,
is_required tinyint(1) NOT NULL DEFAULT 0,
sort_order int(11) DEFAULT 0,
created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY bundle_id (bundle_id),
KEY product_id (product_id)
) $charset_collate;
";
// 支付令牌表
$payment_tokens_table = "
CREATE TABLE {$wpdb->prefix}yoone_payment_tokens (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
customer_id bigint(20) unsigned NOT NULL,
gateway_id varchar(50) NOT NULL,
token varchar(255) NOT NULL,
token_type varchar(20) NOT NULL DEFAULT 'credit_card',
card_type varchar(20) DEFAULT NULL,
last_four varchar(4) DEFAULT NULL,
expiry_month varchar(2) DEFAULT NULL,
expiry_year varchar(4) DEFAULT NULL,
is_default tinyint(1) NOT NULL DEFAULT 0,
created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY customer_id (customer_id),
KEY gateway_id (gateway_id),
UNIQUE KEY unique_token (gateway_id, token)
) $charset_collate;
";
// 订阅日志表
$subscription_logs_table = "
CREATE TABLE {$wpdb->prefix}yoone_subscription_logs (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
subscription_id bigint(20) unsigned NOT NULL,
type varchar(50) NOT NULL,
message text NOT NULL,
data longtext DEFAULT NULL,
created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY subscription_id (subscription_id),
KEY type (type),
KEY created_at (created_at)
) $charset_collate;
";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($subscriptions_table);
dbDelta($subscription_items_table);
dbDelta($bundles_table);
dbDelta($bundle_items_table);
dbDelta($payment_tokens_table);
dbDelta($subscription_logs_table);
// 更新数据库版本
update_option('yoone_subscriptions_db_version', self::DB_VERSION);
}
/**
* 创建默认页面
*/
private static function create_pages() {
$pages = array(
'subscriptions' => array(
'name' => _x('subscriptions', 'Page slug', 'yoone-subscriptions'),
'title' => _x('我的订阅', 'Page title', 'yoone-subscriptions'),
'content' => '[yoone_my_subscriptions]'
)
);
foreach ($pages as $key => $page) {
$option_key = 'yoone_subscriptions_' . $key . '_page_id';
if (!get_option($option_key)) {
$page_id = wp_insert_post(array(
'post_title' => $page['title'],
'post_content' => $page['content'],
'post_status' => 'publish',
'post_type' => 'page',
'post_name' => $page['name'],
'comment_status' => 'closed'
));
if ($page_id) {
update_option($option_key, $page_id);
}
}
}
}
/**
* 设置默认选项
*/
private static function set_default_options() {
$default_options = array(
'yoone_subscriptions_enable_bundles' => 'yes',
'yoone_subscriptions_enable_trials' => 'yes',
'yoone_subscriptions_default_period' => 'month',
'yoone_subscriptions_default_interval' => 1,
'yoone_subscriptions_retry_failed_payments' => 'yes',
'yoone_subscriptions_max_retry_attempts' => 3,
'yoone_subscriptions_retry_interval' => 1
);
foreach ($default_options as $option => $value) {
if (!get_option($option)) {
update_option($option, $value);
}
}
}
/**
* 创建订阅状态
*/
private static function create_subscription_statuses() {
$statuses = array(
'yoone-pending' => _x('待处理', 'Subscription status', 'yoone-subscriptions'),
'yoone-active' => _x('活跃', 'Subscription status', 'yoone-subscriptions'),
'yoone-paused' => _x('暂停', 'Subscription status', 'yoone-subscriptions'),
'yoone-cancelled' => _x('已取消', 'Subscription status', 'yoone-subscriptions'),
'yoone-expired' => _x('已过期', 'Subscription status', 'yoone-subscriptions'),
'yoone-trial' => _x('试用期', 'Subscription status', 'yoone-subscriptions')
);
foreach ($statuses as $status => $label) {
register_post_status($status, array(
'label' => $label,
'public' => false,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop($label . ' (%s)', $label . ' (%s)', 'yoone-subscriptions')
));
}
}
/**
* 检查数据库版本
*/
public static function check_version() {
if (get_option('yoone_subscriptions_db_version') !== self::DB_VERSION) {
self::create_tables();
}
}
}