87 lines
3.6 KiB
PHP
87 lines
3.6 KiB
PHP
<?php
|
||
/**
|
||
* Plugin Name: Yoone Subscriptions
|
||
* Description: 为 WooCommerce 提供基础订阅能力:为单个产品配置订阅计划(周期、数量、订阅价、一次性购买选项),在前端展示订阅选项,并在购物车/订单中显示订阅摘要与按订阅规则计算价格。
|
||
* Author: Yoone
|
||
* Version: 0.1.0
|
||
* Requires at least: 6.0
|
||
* Requires PHP: 7.4
|
||
* Requires Plugins: woocommerce
|
||
* WC requires at least: 6.0
|
||
* WC tested up to: 8.x
|
||
* Text Domain: yoone-subscriptions
|
||
*/
|
||
|
||
defined('ABSPATH') || exit;
|
||
|
||
// 常量
|
||
define('YOONE_SUBS_PATH', plugin_dir_path(__FILE__));
|
||
define('YOONE_SUBS_URL', plugin_dir_url(__FILE__));
|
||
define('YOONE_SUBS_VERSION', '0.1.0');
|
||
|
||
// 加载国际化
|
||
add_action('init', function() {
|
||
load_plugin_textdomain('yoone-subscriptions', false, dirname(plugin_basename(__FILE__)) . '/languages');
|
||
});
|
||
|
||
// 激活与卸载钩子
|
||
register_activation_hook(__FILE__, function() {
|
||
// 安装数据库表(订阅实例记录)
|
||
require_once YOONE_SUBS_PATH . 'includes/models/class-yoone-subscriptions-db.php';
|
||
if (class_exists('Yoone_Subscriptions_DB')) {
|
||
Yoone_Subscriptions_DB::install();
|
||
}
|
||
});
|
||
|
||
// 卸载钩子:注意不可使用匿名函数(Closure),因为 WordPress 会序列化回调,PHP 不允许序列化 Closure。
|
||
function yoone_subscriptions_uninstall() {
|
||
// 预留:清理选项/自定义表;当前实现使用 postmeta,不做强制清理。
|
||
// 如需清理,可在此执行:删除自定义表、删除选项等。
|
||
}
|
||
register_uninstall_hook(__FILE__, 'yoone_subscriptions_uninstall');
|
||
|
||
// WooCommerce 依赖检查
|
||
add_action('plugins_loaded', function() {
|
||
if (! class_exists('WooCommerce')) {
|
||
add_action('admin_notices', function() {
|
||
echo '<div class="notice notice-error"><p>' . esc_html__('Yoone Subscriptions 需要启用 WooCommerce 插件。', 'yoone-subscriptions') . '</p></div>';
|
||
});
|
||
return;
|
||
}
|
||
|
||
// 自动加载
|
||
require_once YOONE_SUBS_PATH . 'includes/class-yoone-subscriptions.php';
|
||
require_once YOONE_SUBS_PATH . 'includes/admin/class-yoone-subscriptions-admin.php';
|
||
require_once YOONE_SUBS_PATH . 'includes/frontend/class-yoone-subscriptions-frontend.php';
|
||
require_once YOONE_SUBS_PATH . 'includes/logging/class-yoone-subscriptions-logger.php';
|
||
require_once YOONE_SUBS_PATH . 'includes/models/class-yoone-subscriptions-db.php';
|
||
|
||
// 引导
|
||
Yoone_Subscriptions::instance();
|
||
Yoone_Subscriptions_Admin::instance();
|
||
Yoone_Subscriptions_Frontend::instance();
|
||
|
||
// 兜底:若订阅表不存在则安装(避免早期版本已激活但未创建表的情况)
|
||
global $wpdb;
|
||
$table = Yoone_Subscriptions_DB::table_name();
|
||
$exists = $wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table));
|
||
if ($exists !== $table) {
|
||
Yoone_Subscriptions_DB::install();
|
||
}
|
||
});
|
||
|
||
// 资源
|
||
add_action('wp_enqueue_scripts', function() {
|
||
wp_register_style('yoone-subs-frontend', YOONE_SUBS_URL . 'assets/css/frontend.css', array(), YOONE_SUBS_VERSION);
|
||
wp_register_script('yoone-subs-frontend', YOONE_SUBS_URL . 'assets/js/frontend.js', array('jquery'), YOONE_SUBS_VERSION, true);
|
||
});
|
||
|
||
add_action('admin_enqueue_scripts', function($hook) {
|
||
if (strpos($hook, 'post.php') !== false || strpos($hook, 'post-new.php') !== false) {
|
||
$screen = get_current_screen();
|
||
if ($screen && 'product' === $screen->post_type) {
|
||
wp_enqueue_style('yoone-subs-admin', YOONE_SUBS_URL . 'assets/css/admin.css', array(), YOONE_SUBS_VERSION);
|
||
wp_enqueue_script('yoone-subs-admin', YOONE_SUBS_URL . 'assets/js/admin.js', array('jquery'), YOONE_SUBS_VERSION, true);
|
||
}
|
||
}
|
||
}); |