文章目录[隐藏]
WordPress文创IP授权链路柔性追踪插件开发详解
引言:文创IP授权追踪的重要性
在数字文创产业蓬勃发展的今天,IP授权管理已成为文创企业的核心业务之一。一个IP可能同时授权给多个合作伙伴用于不同产品开发,如何有效追踪授权链路、监控授权状态、防止授权滥用,成为文创企业面临的重要挑战。本文将详细介绍如何开发一个WordPress文创IP授权链路柔性追踪插件,帮助文创企业实现数字化授权管理。
插件功能规划与架构设计
核心功能模块
- IP授权信息管理 - 记录IP基本信息、版权归属、授权范围
- 授权链路追踪 - 可视化展示授权层级关系
- 授权状态监控 - 实时监控授权有效期、使用状态
- 柔性规则配置 - 支持自定义授权规则和条件
- 数据分析报表 - 生成授权使用统计报告
数据库设计
<?php
/**
* 文创IP授权追踪插件数据库表结构
*/
// 创建IP基本信息表
function create_ip_base_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'ip_licensing_base';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
ip_name varchar(200) NOT NULL COMMENT 'IP名称',
ip_code varchar(100) NOT NULL COMMENT 'IP编码',
copyright_owner varchar(200) NOT NULL COMMENT '版权方',
create_date date NOT NULL COMMENT '创建日期',
status tinyint(1) DEFAULT 1 COMMENT '状态:1-有效,0-无效',
description text COMMENT 'IP描述',
PRIMARY KEY (id),
UNIQUE KEY ip_code (ip_code)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
// 创建授权记录表
function create_licensing_records_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'ip_licensing_records';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
ip_id mediumint(9) NOT NULL COMMENT 'IP ID',
licensee_name varchar(200) NOT NULL COMMENT '被授权方',
license_type varchar(50) NOT NULL COMMENT '授权类型:exclusive-独家,non-exclusive-非独家',
start_date date NOT NULL COMMENT '授权开始日期',
end_date date NOT NULL COMMENT '授权结束日期',
parent_license_id mediumint(9) DEFAULT 0 COMMENT '上级授权ID,0表示直接授权',
authorized_products text COMMENT '授权产品范围',
royalty_rate decimal(5,2) DEFAULT 0.00 COMMENT ' royalty费率',
contract_no varchar(100) COMMENT '合同编号',
tracking_code varchar(100) NOT NULL COMMENT '追踪码',
created_at datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (id),
KEY ip_id (ip_id),
KEY tracking_code (tracking_code)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
// 创建授权使用记录表
function create_usage_records_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'ip_usage_records';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
license_id mediumint(9) NOT NULL COMMENT '授权记录ID',
product_name varchar(200) NOT NULL COMMENT '产品名称',
usage_type varchar(50) NOT NULL COMMENT '使用类型:production-生产,promotion-宣传,other-其他',
usage_date date NOT NULL COMMENT '使用日期',
quantity int DEFAULT 0 COMMENT '使用数量',
revenue decimal(10,2) DEFAULT 0.00 COMMENT '产生收入',
notes text COMMENT '备注',
recorded_by mediumint(9) COMMENT '记录人用户ID',
recorded_at datetime DEFAULT CURRENT_TIMESTAMP COMMENT '记录时间',
PRIMARY KEY (id),
KEY license_id (license_id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
?>
核心功能实现
1. IP授权信息管理模块
<?php
/**
* IP授权管理类
*/
class IP_License_Manager {
/**
* 添加新的IP授权记录
* @param array $data 授权数据
* @return int|false 成功返回记录ID,失败返回false
*/
public function add_license_record($data) {
global $wpdb;
$table_name = $wpdb->prefix . 'ip_licensing_records';
// 生成唯一追踪码
$tracking_code = $this->generate_tracking_code($data['ip_id']);
$license_data = array(
'ip_id' => intval($data['ip_id']),
'licensee_name' => sanitize_text_field($data['licensee_name']),
'license_type' => sanitize_text_field($data['license_type']),
'start_date' => sanitize_text_field($data['start_date']),
'end_date' => sanitize_text_field($data['end_date']),
'parent_license_id' => isset($data['parent_license_id']) ? intval($data['parent_license_id']) : 0,
'authorized_products' => sanitize_textarea_field($data['authorized_products']),
'royalty_rate' => floatval($data['royalty_rate']),
'contract_no' => sanitize_text_field($data['contract_no']),
'tracking_code' => $tracking_code
);
// 验证授权日期
if (strtotime($license_data['start_date']) > strtotime($license_data['end_date'])) {
return new WP_Error('invalid_date', '授权结束日期不能早于开始日期');
}
// 检查授权冲突(独家授权检查)
if ($license_data['license_type'] === 'exclusive') {
$conflict = $this->check_exclusive_conflict(
$license_data['ip_id'],
$license_data['start_date'],
$license_data['end_date']
);
if ($conflict) {
return new WP_Error('exclusive_conflict', '该时间段内已存在独家授权');
}
}
$result = $wpdb->insert($table_name, $license_data);
if ($result) {
// 记录操作日志
$this->log_activity('添加授权记录', $wpdb->insert_id);
return $wpdb->insert_id;
}
return false;
}
/**
* 生成唯一追踪码
* @param int $ip_id IP ID
* @return string 追踪码
*/
private function generate_tracking_code($ip_id) {
$prefix = 'IP' . str_pad($ip_id, 6, '0', STR_PAD_LEFT);
$timestamp = time();
$random = substr(md5(uniqid()), 0, 6);
return $prefix . '-' . date('Ymd', $timestamp) . '-' . strtoupper($random);
}
/**
* 检查独家授权冲突
*/
private function check_exclusive_conflict($ip_id, $start_date, $end_date) {
global $wpdb;
$table_name = $wpdb->prefix . 'ip_licensing_records';
$query = $wpdb->prepare(
"SELECT COUNT(*) FROM $table_name
WHERE ip_id = %d
AND license_type = 'exclusive'
AND status = 1
AND (
(start_date <= %s AND end_date >= %s) OR
(start_date <= %s AND end_date >= %s) OR
(start_date >= %s AND end_date <= %s)
)",
$ip_id, $start_date, $start_date, $end_date, $end_date, $start_date, $end_date
);
$count = $wpdb->get_var($query);
return $count > 0;
}
}
?>
2. 授权链路追踪可视化
<?php
/**
* 授权链路追踪类
*/
class License_Chain_Tracker {
/**
* 获取完整的授权链路
* @param int $license_id 授权记录ID
* @return array 授权链路数组
*/
public function get_license_chain($license_id) {
$chain = array();
$current_id = $license_id;
// 向上追溯直到根授权
while ($current_id > 0) {
$license = $this->get_license_by_id($current_id);
if (!$license) break;
array_unshift($chain, $license);
$current_id = $license->parent_license_id;
}
// 向下查找子授权
if ($license_id > 0) {
$children = $this->get_child_licenses($license_id);
foreach ($children as $child) {
$child_chain = $this->get_license_chain($child->id);
$chain = array_merge($chain, $child_chain);
}
}
return $chain;
}
/**
* 生成授权链路可视化HTML
*/
public function render_license_chain_html($license_id) {
$chain = $this->get_license_chain($license_id);
if (empty($chain)) {
return '<p>未找到授权链路信息</p>';
}
$html = '<div class="license-chain-container">';
$html .= '<h3>授权链路追踪</h3>';
$html .= '<div class="license-chain-timeline">';
foreach ($chain as $index => $license) {
$level_class = $license->parent_license_id == 0 ? 'root-license' : 'sub-license';
$status_class = $this->get_license_status($license) ? 'active' : 'expired';
$html .= '<div class="license-node ' . $level_class . ' ' . $status_class . '">';
$html .= '<div class="node-header">';
$html .= '<span class="node-title">' . esc_html($license->licensee_name) . '</span>';
$html .= '<span class="node-code">' . esc_html($license->tracking_code) . '</span>';
$html .= '</div>';
$html .= '<div class="node-content">';
$html .= '<p>授权类型: ' . esc_html($license->license_type) . '</p>';
$html .= '<p>有效期: ' . esc_html($license->start_date) . ' 至 ' . esc_html($license->end_date) . '</p>';
$html .= '</div>';
$html .= '</div>';
// 添加连接线(除了最后一个节点)
if ($index < count($chain) - 1) {
$html .= '<div class="chain-connector"></div>';
}
}
$html .= '</div></div>';
return $html;
}
/**
* 检查授权状态
*/
private function get_license_status($license) {
$current_time = current_time('timestamp');
$start_time = strtotime($license->start_date);
$end_time = strtotime($license->end_date);
return ($current_time >= $start_time && $current_time <= $end_time);
}
}
?>
3. 柔性规则配置系统
<?php
/**
* 柔性规则配置类
*/
class Flexible_Rule_Manager {
/**
* 验证授权使用是否符合规则
* @param int $license_id 授权ID
* @param array $usage_data 使用数据
* @return array 验证结果
*/
public function validate_usage($license_id, $usage_data) {
$rules = $this->get_rules_for_license($license_id);
$results = array(
'is_valid' => true,
'violations' => array(),
'warnings' => array()
);
foreach ($rules as $rule) {
$validation_result = $this->apply_rule($rule, $usage_data);
if (!$validation_result['passed']) {
if ($rule['severity'] === 'block') {
$results['is_valid'] = false;
$results['violations'][] = $validation_result['message'];
} else {
$results['warnings'][] = $validation_result['message'];
}
}
}
return $results;
}
/**
* 获取适用于特定授权的规则
*/
private function get_rules_for_license($license_id) {
global $wpdb;
$table_name = $wpdb->prefix . 'ip_licensing_rules';
$query = $wpdb->prepare(
"SELECT * FROM $table_name
WHERE license_id = %d OR apply_to_all = 1
AND status = 1
ORDER BY priority DESC",
$license_id
);
return $wpdb->get_results($query, ARRAY_A);
}
/**
* 应用单个规则
*/
private function apply_rule($rule, $usage_data) {
$result = array('passed' => true, 'message' => '');
switch ($rule['rule_type']) {
case 'usage_limit':
$current_usage = $this->get_current_usage($rule['license_id'], $rule['period']);
if ($current_usage + $usage_data['quantity'] > $rule['threshold']) {
$result['passed'] = false;
$result['message'] = sprintf(
'超出使用限制:已使用%d,本次%d,限制%d',
$current_usage,
$usage_data['quantity'],
$rule['threshold']
);
}
break;
case 'revenue_threshold':
if ($usage_data['revenue'] > 0 && $usage_data['revenue'] < $rule['min_revenue']) {
$result['passed'] = false;
$result['message'] = '收入低于最低阈值要求';
}
break;
case 'time_restriction':
$usage_date = strtotime($usage_data['usage_date']);
$start_date = strtotime($rule['start_date']);
$end_date = strtotime($rule['end_date']);
if ($usage_date < $start_date || $usage_date > $end_date) {
$result['passed'] = false;
$result['message'] = '使用时间不在允许范围内';
}
break;
}
return $result;
}
}
?>
插件集成与使用
后台管理界面集成
<?php
/**
* WordPress后台管理界面
*/
class IP_Tracking_Admin {
public function __construct() {
add_action('admin_menu', array($this, 'add_admin_menus'));
add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'));
}
/**
* 添加管理菜单
*/
public function add_admin_menus() {
// 主菜单
add_menu_page(
'文创IP授权管理',
'IP授权追踪',
'manage_options',
'ip-licensing',
array($this, 'render_dashboard'),
'dashicons-admin-site-alt3',
30
);
// 子菜单
add_submenu_page(
'ip-licensing',
'IP库管理',
'IP库',
'manage_options',
'ip-library',
array($this, 'render_ip_library')
);
add_submenu_page(
'ip-licensing',
'授权链路追踪',
'链路追踪',
'manage_options',
'license-chain',
array($this, 'render_license_chain')
);
add_submenu_page(
'ip-licensing',
'规则配置',
'规则配置',
'manage_options',
'rule-config',
array($this, 'render_rule_config')
);
add_submenu_page(
'ip-licensing',
'数据分析报表',
'数据报表',
'manage_options',
'analytics',
array($this, 'render_analytics')
);
}
/**
* 渲染仪表板
*/
public function render_dashboard() {
?>
<div class="wrap">
<h1>文创IP授权追踪系统</h1>
<div class="dashboard-widgets">
<div class="dashboard-card">
<h3>活跃授权数</h3>
<p class="stat-number"><?php echo $this->get_active_licenses_count(); ?></p>
</div>
<div class="dashboard-card">
<h3>即将到期授权</h3>
<p class="stat-number"><?php echo $this->get_expiring_licenses_count(); ?></p>
</div>
<div class="dashboard-card">
<h3>本月授权收入</h3>
<p class="stat-number">¥<?php echo number_format($this->get_monthly_revenue(), 2); ?></p>
</div>
</div>
<div class="recent-activity">
<h2>最近活动</h2>
<?php echo $this->get_recent_activities(); ?>
</div>
</div>
<style>
.dashboard-widgets {
display: flex;
gap: 20px;
margin: 20px 0;
}
.dashboard-card {
flex: 1;
background: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.stat-number {
font-size: 2em;
font-weight: bold;
color: #2271b1;
}
</style>
<?php
}
}
?>
### 前端短代码集成
<?php
/**
- 前端短代码功能
*/
class IP_Tracking_Shortcodes {
public function __construct() {
add_shortcode('ip_license_info', array($this, 'render_license_info'));
add_shortcode('ip_usage_form', array($this, 'render_usage_form'));
add_shortcode('license_chain', array($this, 'render_license_chain'));
}
/**
* 显示授权信息短代码
*/
public function render_license_info($atts) {
$atts = shortcode_atts(array(
'tracking_code' => '',
'show_details' => 'true'
), $atts);
if (empty($atts['tracking_code'])) {
return '<p class="error">请提供追踪码</p>';
}
$license = $this->get_license_by_tracking_code($atts['tracking_code']);
if (!$license) {
return '<p class="error">未找到授权信息</p>';
}
ob_start();
?>
<div class="ip-license-info-card">
<div class="license-header">
<h3>授权信息验证</h3>
<span class="license-status <?php echo $this->get_license_status_class($license); ?>">
<?php echo $this->get_license_status_text($license); ?>
</span>
</div>
<div class="license-details">
<p><strong>被授权方:</strong><?php echo esc_html($license->licensee_name); ?></p>
<p><strong>授权类型:</strong><?php echo esc_html($license->license_type); ?></p>
<p><strong>有效期:</strong><?php echo esc_html($license->start_date); ?> 至 <?php echo esc_html($license->end_date); ?></p>
<p><strong>追踪码:</strong><code><?php echo esc_html($license->tracking_code); ?></code></p>
<?php if ($atts['show_details'] === 'true') : ?>
<div class="additional-info">
<p><strong>授权产品范围:</strong></p>
<p><?php echo nl2br(esc_html($license->authorized_products)); ?></p>
</div>
<?php endif; ?>
</div>
</div>
<style>
.ip-license-info-card {
border: 1px solid #ddd;
border-radius: 8px;
padding: 20px;
margin: 20px 0;
background: #f9f9f9;
}
.license-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px;
border-bottom: 2px solid #2271b1;
padding-bottom: 10px;
}
.license-status {
padding: 4px 12px;
border-radius: 4px;
font-size: 0.9em;
font-weight: bold;
}
.status-active {
background: #d4edda;
color: #155724;
}
.status-expired {
background: #f8d7da;
color: #721c24;
}
.license-details p {
margin: 8px 0;
}
</style>
<?php
return ob_get_clean();
}
/**
* 使用记录表单短代码
*/
public function render_usage_form($atts) {
if (!is_user_logged_in()) {
return '<p>请先登录以提交使用记录</p>';
}
ob_start();
?>
<div class="ip-usage-form-container">
<h3>IP使用记录提交</h3>
<form id="ip-usage-form" method="post">
<?php wp_nonce_field('submit_ip_usage', 'ip_usage_nonce'); ?>
<div class="form-group">
<label for="tracking_code">授权追踪码 *</label>
<input type="text" id="tracking_code" name="tracking_code" required
placeholder="输入授权追踪码">
</div>
<div class="form-group">
<label for="product_name">产品名称 *</label>
<input type="text" id="product_name" name="product_name" required
placeholder="输入使用IP的产品名称">
</div>
<div class="form-group">
<label for="usage_type">使用类型 *</label>
<select id="usage_type" name="usage_type" required>
<option value="">请选择使用类型</option>
<option value="production">生产制造</option>
<option value="promotion">宣传推广</option>
<option value="digital">数字产品</option>
<option value="event">活动使用</option>
<option value="other">其他</option>
</select>
</div>
<div class="form-row">
<div class="form-group">
<label for="usage_date">使用日期 *</label>
<input type="date" id="usage_date" name="usage_date" required
value="<?php echo date('Y-m-d'); ?>">
</div>
<div class="form-group">
<label for="quantity">使用数量</label>
<input type="number" id="quantity" name="quantity" min="0"
placeholder="0">
</div>
</div>
<div class="form-group">
<label for="revenue">产生收入(元)</label>
<input type="number" id="revenue" name="revenue" min="0" step="0.01"
placeholder="0.00">
</div>
<div class="form-group">
<label for="notes">备注说明</label>
<textarea id="notes" name="notes" rows="3"
placeholder="可在此添加使用详情说明"></textarea>
</div>
<button type="submit" class="submit-button">提交使用记录</button>
<div id="form-message" class="form-message"></div>
</form>
</div>
<script>
jQuery(document).ready(function($) {
$('#ip-usage-form').on('submit', function(e) {
e.preventDefault();
var formData = $(this).serialize();
var $message = $('#form-message');
$message.removeClass('success error').html('提交中...');
$.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'POST',
data: formData + '&action=submit_ip_usage',
success: function(response) {
if (response.success) {
$message.addClass('success').html(response.data.message);
$('#ip-usage-form')[0].reset();
} else {
$message.addClass('error').html(response.data);
}
},
error: function() {
$message.addClass('error').html('提交失败,请稍后重试');
}
});
});
});
</script>
<style>
.ip-usage-form-container {
max-width: 600px;
margin: 20px auto;
padding: 20px;
background: #fff;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.form-group {
margin-bottom: 15px;
}
.form-row {
display: flex;
gap: 15px;
}
.form-row .form-group {
flex: 1;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input, select, textarea {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
}
.submit-button {
background: #2271b1;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 1em;
}
.form-message {
margin-top: 15px;
padding: 10px;
border-radius: 4px;
}
.form-message.success {
background: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.form-message.error {
background: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
</style>
<?php
return ob_get_clean();
}
}
?>
## 数据安全与权限控制
<?php
/**
- 安全与权限控制类
*/
class IP_Tracking_Security {
/**
* 初始化权限控制
*/
public function init_capabilities() {
$roles = array('administrator', 'editor', 'author');
foreach ($roles as $role_name) {
$role = get_role($role_name);
if ($role) {
// 管理员拥有所有权限
if ($role_name === 'administrator') {
$role->add_cap('manage_ip_licensing');
$role->add_cap('view_ip_licensing');
$role->add_cap('edit_ip_licensing');
$role->add_cap('delete_ip_licensing');
$role->add_cap('export_ip_data');
}
// 编辑者可以查看和编辑
if ($role_name === 'editor') {
$role->add_cap('view_ip_licensing');
$role->add_cap('edit_ip_licensing');
}
// 作者只能查看
if ($role_name === 'author') {
$role->add_cap('view_ip_licensing');
}
}
}
}
/**
* 数据加密函数
*/
public function encrypt_data($data, $key = '') {
if (empty($key)) {
$key = $this->get_encryption_key();
}
$method = 'AES-256-CBC';
$iv_length = openssl_cipher_iv_length($method);
$iv = openssl_random_pseudo_bytes($iv_length);
$encrypted = openssl_encrypt(
$data,
$method,
$key,
OPENSSL_RAW_DATA,
$iv
);
return base64_encode($iv . $encrypted);
}
/**
* 数据解密函数
*/
public function decrypt_data($encrypted_data, $key = '') {
if (empty($key)) {
$key = $this->get_encryption_key();
}
$data = base64_decode($encrypted_data);
$method = 'AES-256-CBC';
$iv_length = openssl_cipher_iv_length($method);
$iv = substr($data, 0, $iv_length);
$encrypted = substr($data, $iv_length);
return openssl_decrypt(
$encrypted,
$method,
$key,
OPENSSL_RAW_DATA,
$iv
);
}
/**
* 获取加密密钥
*/
private function get_encryption_key() {
$key = get_option('ip_tracking_encryption_key');
if (empty($key)) {
$key = bin2hex(random_bytes(32));
update_option('ip_tracking_encryption_key', $key);
}
return $key;
}
/**
* 审计日志记录
*/
public function log_audit_trail($action, $details, $user_id = null) {
global $wpdb;
if (is_null($user_id)) {
$user_id = get_current_user_id();
}
$table_name = $wpdb->prefix . 'ip_audit_logs';
$log_data = array(
'user_id' => $user_id,
'action' => sanitize_text_field($action),
'details' => wp_json_encode($details),
'ip_address' => $this->get_client_ip(),
'user_agent' => sanitize_text_field($_SERVER['HTTP_USER_AGENT'] ?? ''),
'created_at' => current_time('mysql')
);
$wpdb->insert($table_name, $log_data);
}
/**
* 获取客户端IP
*/
private function get_client_ip() {
$ip_keys = array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED',
'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED',
'REMOTE_ADDR');
foreach ($ip_keys as $key) {
if (array_key_exists($key, $_SERVER) === true) {
foreach (explode(',', $_SERVER[$key]) as $ip) {
$ip = trim($ip);
if (filter_var($ip, FILTER_VALIDATE_IP)) {
return $ip;
}
}
}
}
return '0.0.0.0';
}
}
?>
## 插件部署与配置
### 主插件文件
<?php
/**
- Plugin Name: WordPress文创IP授权链路柔性追踪插件
- Plugin URI: https://yourwebsite.com/
- Description: 专业的文创IP授权链路追踪和管理解决方案
- Version: 1.0.0
- Author: 您的名称
- License: GPL v2 or later
- Text Domain: ip-tracking
*/
// 防止直接访问
if (!defined('ABSPATH')) {
exit;
}
// 定义插件常量
define('IP_TRACKING_VERSION', '1.0.0');
define('IP_TRACKING_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('IP_TRACKING_PLUGIN_URL', plugin_dir_url(__FILE__));
// 自动加载类文件
spl_autoload_register(function ($class_name) {
$prefix = 'IP_Tracking_';
$base_dir = IP_TRACKING_PLUGIN_DIR . 'includes/';
$len = strlen($prefix);
if (strncmp($prefix, $class_name, $len) !== 0) {
return;
}
$relative_class = substr($class_name, $len);
$file = $base_dir . str_replace('_', '/', $relative_class) . '.php';
if (file_exists($file)) {
require $file;
}
});
// 初始化插件
class IP_Tracking_Plugin {
private static $instance = null;
public static function get_instance() {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
$this->init_hooks();
}
private function init_hooks() {
// 激活/停用钩子
register_activation_hook(__FILE__, array($this, 'activate'));
register_deactivation_hook(__FILE__, array($this, 'deactivate'));
// 初始化
add_action('plugins_loaded', array($this, 'init'));
// 加载文本域
add_action('init', array($this, 'load_textdomain'));
}
public function activate() {
// 创建数据库表
require_once IP_TRACKING_PLUGIN_DIR . 'includes/class-database.php';
IP_Tracking_Database::create_tables();
// 初始化权限
require_once IP_TRACKING_PLUGIN_DIR . 'includes/class-security.php';
$security = new IP_Tracking_Security();
$security->init_capabilities();
// 设置默认选项
$default_options = array(
'enable_auto_tracking' => true,
'require_approval' => false,
'default_royalty_rate' => 5.00,
'notification_email' => get_option('admin_email'),
'data_retention_days' => 365
);
update_option('ip_tracking_settings', $default_options);
// 记录激活
$security->log_audit_trail('plugin_activated', array('version' => IP_TRACKING_VERSION));
}
public function deactivate() {
// 清理临时数据
wp_clear_scheduled_hook('ip_tracking_daily_maintenance');
// 记录停用
require_once IP_TRACKING_PLUGIN_DIR . 'includes/class-security.php';
$security = new IP_Tracking_Security();
$security->log_audit_trail('plugin_deactivated', array());
}
public function init() {
// 只在后台加载管理类
if (is_admin()) {
require_once IP_TRACKING_PLUGIN_DIR . 'admin/class-admin.php';
new IP_Tracking_Admin();
}
// 加载短代码
require_once IP_TRACKING_PLUGIN_DIR . 'includes/class-shortcodes.php';
new IP_Tracking_Shortcodes();
// 加载AJAX处理器
require_once IP_TRACKING_PLUGIN_DIR . 'includes/class-ajax.php';
new IP_Tracking_Ajax();
// 加载小工具
require_once IP_TRACKING_PLUGIN_DIR . 'includes/class-widgets.php';
add_action('widgets_init', function() {
register_widget('IP_Tracking_Widget');
});
// 设置定时任务
