文章目录[隐藏]
WordPress文创数字藏品柔性发行与管理插件开发教程
一、项目概述与需求分析
在数字文创产业蓬勃发展的今天,数字藏品作为文化创意产品的新形态,正受到越来越多创作者和收藏家的青睐。本教程将指导您开发一个WordPress插件,实现文创数字藏品的柔性发行与管理功能。
核心需求分析:
- 支持数字藏品的创建、编辑和分类管理
- 实现灵活的发行策略(限量发行、定时发行、分阶段发行)
- 集成区块链元数据记录(模拟实现)
- 提供用户收藏、展示和交易功能
- 确保数据安全和权限控制
二、开发环境搭建与插件基础结构
首先,我们需要创建插件的基本结构。在WordPress的wp-content/plugins/目录下创建新文件夹digital-collections。
<?php
/**
* Plugin Name: 文创数字藏品管理系统
* Plugin URI: https://yourwebsite.com/
* Description: WordPress文创数字藏品柔性发行与管理插件
* Version: 1.0.0
* Author: 您的名称
* License: GPL v2 or later
* Text Domain: digital-collections
*/
// 防止直接访问
if (!defined('ABSPATH')) {
exit;
}
// 定义插件常量
define('DC_VERSION', '1.0.0');
define('DC_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('DC_PLUGIN_URL', plugin_dir_url(__FILE__));
// 初始化插件
class DigitalCollections {
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('init', array($this, 'init'));
// 管理菜单
add_action('admin_menu', array($this, 'add_admin_menu'));
// 加载脚本和样式
add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'));
add_action('wp_enqueue_scripts', array($this, 'enqueue_frontend_scripts'));
}
public function activate() {
// 创建必要的数据库表
$this->create_tables();
// 刷新重写规则
flush_rewrite_rules();
}
public function deactivate() {
// 清理临时数据
flush_rewrite_rules();
}
public function init() {
// 注册自定义文章类型
$this->register_post_types();
// 注册短代码
$this->register_shortcodes();
// 加载文本域
load_plugin_textdomain('digital-collections', false, dirname(plugin_basename(__FILE__)) . '/languages');
}
// 其他方法将在后续部分实现
}
// 启动插件
DigitalCollections::get_instance();
?>
三、自定义文章类型与分类系统
数字藏品需要自定义的文章类型来存储和管理。我们将创建digital_collection文章类型和相应的分类法。
// 在DigitalCollections类中添加以下方法
/**
* 注册数字藏品自定义文章类型
*/
public function register_post_types() {
// 数字藏品主类型
$labels = array(
'name' => __('数字藏品', 'digital-collections'),
'singular_name' => __('数字藏品', 'digital-collections'),
'menu_name' => __('数字藏品', 'digital-collections'),
'add_new' => __('添加新藏品', 'digital-collections'),
'add_new_item' => __('添加新数字藏品', 'digital-collections'),
'edit_item' => __('编辑数字藏品', 'digital-collections'),
'new_item' => __('新数字藏品', 'digital-collections'),
'view_item' => __('查看数字藏品', 'digital-collections'),
'search_items' => __('搜索数字藏品', 'digital-collections'),
'not_found' => __('未找到数字藏品', 'digital-collections'),
'not_found_in_trash' => __('回收站中未找到数字藏品', 'digital-collections'),
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug' => 'collection'),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 5,
'menu_icon' => 'dashicons-format-image',
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'comments'),
'show_in_rest' => true, // 支持Gutenberg编辑器
);
register_post_type('digital_collection', $args);
// 注册数字藏品分类
$category_labels = array(
'name' => __('藏品分类', 'digital-collections'),
'singular_name' => __('藏品分类', 'digital-collections'),
'search_items' => __('搜索分类', 'digital-collections'),
'all_items' => __('所有分类', 'digital-collections'),
'parent_item' => __('父分类', 'digital-collections'),
'parent_item_colon' => __('父分类:', 'digital-collections'),
'edit_item' => __('编辑分类', 'digital-collections'),
'update_item' => __('更新分类', 'digital-collections'),
'add_new_item' => __('添加新分类', 'digital-collections'),
'new_item_name' => __('新分类名称', 'digital-collections'),
'menu_name' => __('藏品分类', 'digital-collections'),
);
$category_args = array(
'hierarchical' => true,
'labels' => $category_labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'collection-category'),
'show_in_rest' => true,
);
register_taxonomy('collection_category', array('digital_collection'), $category_args);
// 注册创作者分类法
$creator_labels = array(
'name' => __('创作者', 'digital-collections'),
'singular_name' => __('创作者', 'digital-collections'),
'search_items' => __('搜索创作者', 'digital-collections'),
'all_items' => __('所有创作者', 'digital-collections'),
'edit_item' => __('编辑创作者', 'digital-collections'),
'update_item' => __('更新创作者', 'digital-collections'),
'add_new_item' => __('添加新创作者', 'digital-collections'),
'new_item_name' => __('新创作者名称', 'digital-collections'),
'menu_name' => __('创作者', 'digital-collections'),
);
$creator_args = array(
'hierarchical' => false,
'labels' => $creator_labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'collection-creator'),
'show_in_rest' => true,
);
register_taxonomy('collection_creator', array('digital_collection'), $creator_args);
}
四、元数据管理与发行策略
数字藏品需要存储额外的元数据,如发行数量、发行时间、区块链信息等。我们将创建自定义元字段和发行策略管理。
// 在DigitalCollections类中添加以下方法
/**
* 添加数字藏品元数据框
*/
public function add_meta_boxes() {
add_meta_box(
'digital_collection_meta',
__('藏品发行信息', 'digital-collections'),
array($this, 'render_meta_box'),
'digital_collection',
'normal',
'high'
);
}
/**
* 渲染元数据框内容
*/
public function render_meta_box($post) {
// 添加安全验证
wp_nonce_field('digital_collection_meta_box', 'digital_collection_meta_box_nonce');
// 获取现有值
$total_supply = get_post_meta($post->ID, '_dc_total_supply', true);
$minted_count = get_post_meta($post->ID, '_dc_minted_count', true) ?: 0;
$price = get_post_meta($post->ID, '_dc_price', true);
$release_date = get_post_meta($post->ID, '_dc_release_date', true);
$release_type = get_post_meta($post->ID, '_dc_release_type', true) ?: 'immediate';
$blockchain_hash = get_post_meta($post->ID, '_dc_blockchain_hash', true);
$contract_address = get_post_meta($post->ID, '_dc_contract_address', true);
// 发行类型选项
$release_types = array(
'immediate' => __('立即发行', 'digital-collections'),
'scheduled' => __('定时发行', 'digital-collections'),
'phased' => __('分阶段发行', 'digital-collections'),
'auction' => __('拍卖发行', 'digital-collections'),
);
?>
<div class="dc-meta-box">
<table class="form-table">
<tr>
<th><label for="dc_total_supply"><?php _e('发行总量', 'digital-collections'); ?></label></th>
<td>
<input type="number" id="dc_total_supply" name="dc_total_supply"
value="<?php echo esc_attr($total_supply); ?>" min="1" max="100000" />
<p class="description"><?php _e('该藏品的总发行数量', 'digital-collections'); ?></p>
</td>
</tr>
<tr>
<th><label for="dc_minted_count"><?php _e('已铸造数量', 'digital-collections'); ?></label></th>
<td>
<input type="number" id="dc_minted_count" name="dc_minted_count"
value="<?php echo esc_attr($minted_count); ?>" readonly />
<p class="description"><?php _e('已发行的藏品数量', 'digital-collections'); ?></p>
</td>
</tr>
<tr>
<th><label for="dc_price"><?php _e('价格', 'digital-collections'); ?></label></th>
<td>
<input type="text" id="dc_price" name="dc_price"
value="<?php echo esc_attr($price); ?>" />
<p class="description"><?php _e('藏品价格(单位:元)', 'digital-collections'); ?></p>
</td>
</tr>
<tr>
<th><label for="dc_release_type"><?php _e('发行类型', 'digital-collections'); ?></label></th>
<td>
<select id="dc_release_type" name="dc_release_type">
<?php foreach ($release_types as $value => $label): ?>
<option value="<?php echo esc_attr($value); ?>"
<?php selected($release_type, $value); ?>>
<?php echo esc_html($label); ?>
</option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr class="release-date-row"
style="<?php echo $release_type !== 'scheduled' ? 'display:none;' : ''; ?>">
<th><label for="dc_release_date"><?php _e('发行时间', 'digital-collections'); ?></label></th>
<td>
<input type="datetime-local" id="dc_release_date" name="dc_release_date"
value="<?php echo esc_attr($release_date); ?>" />
</td>
</tr>
<tr>
<th><label for="dc_blockchain_hash"><?php _e('区块链哈希', 'digital-collections'); ?></label></th>
<td>
<input type="text" id="dc_blockchain_hash" name="dc_blockchain_hash"
value="<?php echo esc_attr($blockchain_hash); ?>" />
<p class="description"><?php _e('藏品在区块链上的交易哈希', 'digital-collections'); ?></p>
</td>
</tr>
<tr>
<th><label for="dc_contract_address"><?php _e('智能合约地址', 'digital-collections'); ?></label></th>
<td>
<input type="text" id="dc_contract_address" name="dc_contract_address"
value="<?php echo esc_attr($contract_address); ?>" />
</td>
</tr>
</table>
</div>
<script>
jQuery(document).ready(function($) {
// 显示/隐藏发行时间字段
$('#dc_release_type').change(function() {
if ($(this).val() === 'scheduled') {
$('.release-date-row').show();
} else {
$('.release-date-row').hide();
}
});
});
</script>
<?php
}
/**
* 保存元数据
*/
public function save_meta_box($post_id) {
// 安全检查
if (!isset($_POST['digital_collection_meta_box_nonce']) ||
!wp_verify_nonce($_POST['digital_collection_meta_box_nonce'], 'digital_collection_meta_box')) {
return;
}
// 检查自动保存
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
// 检查权限
if (!current_user_can('edit_post', $post_id)) {
return;
}
// 保存字段
$fields = array(
'dc_total_supply',
'dc_price',
'dc_release_date',
'dc_release_type',
'dc_blockchain_hash',
'dc_contract_address'
);
foreach ($fields as $field) {
if (isset($_POST[$field])) {
update_post_meta($post_id, '_' . $field, sanitize_text_field($_POST[$field]));
}
}
}
// 在init_hooks方法中添加
add_action('add_meta_boxes', array($this, 'add_meta_boxes'));
add_action('save_post_digital_collection', array($this, 'save_meta_box'));
五、用户收藏与交易系统
创建用户收藏功能和简单的交易记录系统。
// 创建用户收藏功能
class DC_User_Collections {
/**
* 用户收藏藏品
*/
public static function add_to_collection($user_id, $collection_id) {
global $wpdb;
$table_name = $wpdb->prefix . 'dc_user_collections';
// 检查是否已收藏
$exists = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM $table_name WHERE user_id = %d AND collection_id = %d",
$user_id, $collection_id
));
if ($exists) {
return new WP_Error('already_collected', __('您已经收藏了此藏品', 'digital-collections'));
}
// 获取藏品信息
$collection = get_post($collection_id);
if (!$collection || $collection->post_type !== 'digital_collection') {
return new WP_Error('invalid_collection', __('无效的藏品', 'digital-collections'));
}
// 检查发行限制
$total_supply = get_post_meta($collection_id, '_dc_total_supply', true);
$minted_count = get_post_meta($collection_id, '_dc_minted_count', true) ?: 0;
if ($minted_count >= $total_supply) {
return new WP_Error('sold_out', __('该藏品已售罄', 'digital-collections'));
}
// 创建收藏记录
$result = $wpdb->insert(
$table_name,
array(
'user_id' => $user_id,
'collection_id' => $collection_id,
'acquired_date' => current_time('mysql'),
'transaction_hash' => self::generate_transaction_hash(),
'status' => 'owned'
),
array('%d', '%d', '%s', '%s', '%s')
);
if ($result) {
// 更新已铸造数量
update_post_meta($collection_id, '_dc_minted_count', $minted_count + 1);
// 记录交易
self::record_transaction($user_id, $collection_id, 'purchase');
return true;
}
return false;
}
/**
* 生成交易哈希(模拟)
*/
private static function generate_transaction_hash() {
return '0x' . bin2hex(random_bytes(16));
}
/**
* 记录交易
*/
private static function record_transaction($user_id, $collection_id, $type) {
global $wpdb;
$table_name = $wpdb->prefix . 'dc_transactions';
$price = get_post_meta($collection_id, '_dc_price', true);
$wpdb->insert(
$table_name,
array(
'user_id' => $user_id,
'collection_id' => $collection_id,
'transaction_type' => $type,
'amount' => $price,
'transaction_date' => current_time('mysql'),
'status' => 'completed'
),
'%s')
);
}
/**
* 获取用户收藏
*/
public static function get_user_collections($user_id, $limit = 20, $offset = 0) {
global $wpdb;
$table_name = $wpdb->prefix . 'dc_user_collections';
$results = $wpdb->get_results($wpdb->prepare(
"SELECT * FROM $table_name
WHERE user_id = %d AND status = 'owned'
ORDER BY acquired_date DESC
LIMIT %d OFFSET %d",
$user_id, $limit, $offset
));
return $results;
}
/**
* 创建数据库表
*/
public static function create_tables() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
// 用户收藏表
$user_collections_table = $wpdb->prefix . 'dc_user_collections';
$sql1 = "CREATE TABLE IF NOT EXISTS $user_collections_table (
id bigint(20) NOT NULL AUTO_INCREMENT,
user_id bigint(20) NOT NULL,
collection_id bigint(20) NOT NULL,
acquired_date datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
transaction_hash varchar(100) DEFAULT '',
status varchar(20) DEFAULT 'owned',
PRIMARY KEY (id),
KEY user_id (user_id),
KEY collection_id (collection_id),
UNIQUE KEY user_collection (user_id, collection_id)
) $charset_collate;";
// 交易记录表
$transactions_table = $wpdb->prefix . 'dc_transactions';
$sql2 = "CREATE TABLE IF NOT EXISTS $transactions_table (
id bigint(20) NOT NULL AUTO_INCREMENT,
user_id bigint(20) NOT NULL,
collection_id bigint(20) NOT NULL,
transaction_type varchar(50) NOT NULL,
amount decimal(10,2) DEFAULT 0.00,
transaction_date datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
status varchar(20) DEFAULT 'pending',
metadata text,
PRIMARY KEY (id),
KEY user_id (user_id),
KEY collection_id (collection_id),
KEY transaction_date (transaction_date)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql1);
dbDelta($sql2);
}
}
// 在DigitalCollections类的activate方法中调用
public function create_tables() {
DC_User_Collections::create_tables();
}
## 六、前端展示与短代码系统
创建前端展示功能和短代码,让用户可以在网站任何位置展示数字藏品。
// 在DigitalCollections类中添加短代码功能
/**
- 注册短代码
*/
public function register_shortcodes() {
// 藏品展示短代码
add_shortcode('digital_collections', array($this, 'collections_shortcode'));
// 用户收藏展示短代码
add_shortcode('my_collections', array($this, 'my_collections_shortcode'));
// 最新藏品短代码
add_shortcode('latest_collections', array($this, 'latest_collections_shortcode'));
}
/**
- 藏品展示短代码
*/
public function collections_shortcode($atts) {
$atts = shortcode_atts(array(
'category' => '',
'creator' => '',
'limit' => 12,
'columns' => 3,
'show_price' => 'yes',
'show_status' => 'yes'
), $atts, 'digital_collections');
// 构建查询参数
$args = array(
'post_type' => 'digital_collection',
'posts_per_page' => intval($atts['limit']),
'post_status' => 'publish'
);
// 添加分类筛选
if (!empty($atts['category'])) {
$args['tax_query'][] = array(
'taxonomy' => 'collection_category',
'field' => 'slug',
'terms' => explode(',', $atts['category'])
);
}
// 添加创作者筛选
if (!empty($atts['creator'])) {
$args['tax_query'][] = array(
'taxonomy' => 'collection_creator',
'field' => 'slug',
'terms' => explode(',', $atts['creator'])
);
}
// 执行查询
$query = new WP_Query($args);
if (!$query->have_posts()) {
return '<p>' . __('暂无数字藏品', 'digital-collections') . '</p>';
}
// 生成HTML输出
ob_start();
?>
<div class="dc-collections-grid dc-columns-<?php echo esc_attr($atts['columns']); ?>">
<?php while ($query->have_posts()) : $query->the_post();
$post_id = get_the_ID();
$total_supply = get_post_meta($post_id, '_dc_total_supply', true);
$minted_count = get_post_meta($post_id, '_dc_minted_count', true) ?: 0;
$price = get_post_meta($post_id, '_dc_price', true);
$release_type = get_post_meta($post_id, '_dc_release_type', true);
$available = $minted_count < $total_supply;
?>
<div class="dc-collection-item">
<div class="dc-collection-thumbnail">
<?php if (has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('medium'); ?>
</a>
<?php endif; ?>
<?php if (!$available) : ?>
<span class="dc-sold-out-badge"><?php _e('已售罄', 'digital-collections'); ?></span>
<?php endif; ?>
</div>
<div class="dc-collection-info">
<h3 class="dc-collection-title">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h3>
<div class="dc-collection-meta">
<?php if ($atts['show_price'] === 'yes' && $price) : ?>
<div class="dc-price">
<span class="dc-price-label"><?php _e('价格:', 'digital-collections'); ?></span>
<span class="dc-price-value">¥<?php echo number_format(floatval($price), 2); ?></span>
</div>
<?php endif; ?>
<?php if ($atts['show_status'] === 'yes') : ?>
<div class="dc-supply">
<span class="dc-supply-label"><?php _e('发行:', 'digital-collections'); ?></span>
<span class="dc-supply-value"><?php echo esc_html($minted_count); ?>/<?php echo esc_html($total_supply); ?></span>
</div>
<?php endif; ?>
</div>
<div class="dc-collection-actions">
<?php if (is_user_logged_in() && $available) : ?>
<button class="dc-collect-btn"
data-collection-id="<?php echo esc_attr($post_id); ?>"
data-nonce="<?php echo wp_create_nonce('collect_nonce_' . $post_id); ?>">
<?php _e('立即收藏', 'digital-collections'); ?>
</button>
<?php elseif (!is_user_logged_in()) : ?>
<a href="<?php echo wp_login_url(get_permalink()); ?>" class="dc-login-btn">
<?php _e('登录后收藏', 'digital-collections'); ?>
</a>
<?php endif; ?>
</div>
</div>
</div>
<?php endwhile; wp_reset_postdata(); ?>
</div>
<style>
.dc-collections-grid {
display: grid;
grid-gap: 20px;
margin: 20px 0;
}
.dc-columns-2 { grid-template-columns: repeat(2, 1fr); }
.dc-columns-3 { grid-template-columns: repeat(3, 1fr); }
.dc-columns-4 { grid-template-columns: repeat(4, 1fr); }
.dc-collection-item {
border: 1px solid #ddd;
border-radius: 8px;
overflow: hidden;
transition: transform 0.3s ease;
}
.dc-collection-item:hover {
transform: translateY(-5px);
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}
.dc-collection-thumbnail {
position: relative;
overflow: hidden;
}
.dc-collection-thumbnail img {
width: 100%;
height: 200px;
object-fit: cover;
transition: transform 0.3s ease;
}
.dc-collection-item:hover .dc-collection-thumbnail img {
transform: scale(1.05);
}
.dc-sold-out-badge {
position: absolute;
top: 10px;
right: 10px;
background: #ff4757;
color: white;
padding: 5px 10px;
border-radius: 4px;
font-size: 12px;
}
.dc-collection-info {
padding: 15px;
}
.dc-collection-title {
margin: 0 0 10px 0;
font-size: 16px;
}
.dc-collection-meta {
display: flex;
justify-content: space-between;
margin-bottom: 15px;
font-size: 14px;
}
.dc-price-value {
color: #ff6b6b;
font-weight: bold;
}
.dc-collection-actions {
text-align: center;
}
.dc-collect-btn, .dc-login-btn {
background: #4ecdc4;
color: white;
border: none;
padding: 8px 15px;
border-radius: 4px;
cursor: pointer;
width: 100%;
transition: background 0.3s ease;
}
.dc-collect-btn:hover {
background: #3dbcb4;
}
@media (max-width: 768px) {
.dc-columns-2, .dc-columns-3, .dc-columns-4 {
grid-template-columns: 1fr;
}
}
</style>
<script>
jQuery(document).ready(function($) {
$('.dc-collect-btn').on('click', function() {
var button = $(this);
var collectionId = button.data('collection-id');
var nonce = button.data('nonce');
button.prop('disabled', true).text('处理中...');
$.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'POST',
data: {
action: 'dc_collect_collection',
collection_id: collectionId,
nonce: nonce
},
success: function(response) {
if (response.success) {
button.text('已收藏').css('background', '#2ecc71');
alert('收藏成功!');
} else {
button.prop('disabled', false).text('立即收藏');
alert(response.data || '收藏失败,请重试');
}
},
error: function() {
button.prop('disabled', false).text('立即收藏');
alert('网络错误,请重试');
}
});
});
});
</script>
<?php
return ob_get_clean();
}
/**
- AJAX收藏处理
*/
public function ajax_collect_collection() {
// 验证nonce
if (!isset($_POST['nonce']) || !isset($_POST['collection_id'])) {
wp_die('Invalid request');
}
$collection_id = intval($_POST['collection_id']);
$nonce = sanitize_text_field($_POST['nonce']);
if (!wp_verify_nonce($nonce, 'collect_nonce_' . $collection_id)) {
wp_die('Security check failed');
}
// 检查用户登录状态
if (!is_user_logged_in()) {
wp_send_json_error(__('请先登录', 'digital-collections'));
}
$user_id = get_current_user_id();
$result = DC_User_Collections::add_to_collection($user_id, $collection_id);
if (is_wp_error($result)) {
wp_send_json_error($result->get_error_message());
} else {
wp_send_json_success(__('收藏成功', 'digital-collections'));
}
}
// 在init_hooks方法中添加AJAX处理
add_action('wp_ajax_dc_collect_collection', array($this, 'ajax_collect_collection'));
add_action('wp_ajax_nopriv_dc_collect_collection', array($this, 'ajax_collect_collection'));
## 七、管理界面与统计功能
创建完善的管理后台界面和统计功能。
// 在DigitalCollections类中添加管理菜单方法
/**
- 添加管理菜单
*/
public function add_admin_menu() {
// 主菜单
add_menu_page(
__('数字藏品管理', 'digital-collections'),
__('数字藏品', 'digital-collections'),
'manage_options',
'digital-collections',
array($this, 'admin_dashboard_page'),
'dashicons-format-image',
30
);
// 子菜单
add_submenu_page(
'digital-collections',
__('藏品统计', 'digital-collections'),
__('统计', 'digital-collections'),
'manage_options',
'dc-statistics',
array($this, 'statistics_page')
);
add_submenu_page(
'digital-collections',
__('发行设置', 'digital-collections'),
__('发行设置', 'digital-collections'),
'manage_options',
'dc-settings',
array($this, 'settings_page')
);
add_submenu_page(
'digital-collections',
__('交易记录', 'digital-collections'),
__('交易记录', 'digital-collections'),
'manage_options',
'dc-transactions',
array($this, 'transactions_page')
);
}
/**
- 管理仪表盘页面
*/
public function admin_dashboard_page() {
global $wpdb;
// 获取统计数据
$total_collections = wp_count_posts('digital_collection')->publish;
$total_sales = $wpdb->get_var(
"SELECT COUNT(*) FROM {$wpdb->prefix}dc_transactions WHERE status = 'completed'"
);
$total_revenue = $wpdb->get_var(
"SELECT SUM(amount) FROM {$wpdb->prefix}dc_transactions WHERE status = 'completed'"
);
$total_users = $wpdb->get_var(
"SELECT COUNT(DISTINCT user_id) FROM {$wpdb->prefix}dc_user_collections"
);
// 获取最近交易
$recent_transactions = $wpdb->get_results(
"SELECT t.*, u.user_login, p.post_title
FROM {$wpdb->prefix}dc_transactions t
LEFT JOIN {$wpdb->users} u ON t.user_id = u.ID
LEFT JOIN {$wpdb->posts} p ON t.collection_id = p.ID
ORDER BY t.transaction_date DESC LIMIT 10"
);
?>
<div class="wrap">
<h1><?php _e('数字藏品管理系统', 'digital-collections'); ?></h1>
<div class="dc-admin-stats">
<div class="dc-stat-card">
<h3><?php _e('藏品总数', 'digital-collections'); ?></h3>
<div class="dc-stat-value"><?php echo esc_html($total_collections); ?></div>
</div>
<div class="dc-stat-card">
<h3><?php _e('总销售额', 'digital-collections'); ?></h3>
<div class="dc-stat-value">¥<?php echo number_format($total_revenue ?: 0, 2); ?></div>
</div>
<div class="dc-stat-card">
<h3><?php _e('总交易数', 'digital-collections'); ?></h3>
<div class="dc-stat-value"><?php echo esc_html($total_sales); ?></div>
</div>
<div class="dc-stat-card">
<h3><?php _e('收藏用户', 'digital-collections'); ?></h3>
<div class="dc-stat-value"><?php echo esc_html($total_users); ?></div>
</div>
</div>
<div class="dc-admin-content">
<div class="dc-recent-transactions">
<h2><?php _e('最近交易', 'digital-collections'); ?></h2>
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<th><?php _e('用户', 'digital-collections'); ?></th>
<th><?php _e('藏品', 'digital-collections'); ?></th>
<th><?php _e('类型', 'digital-collections'); ?></th>
<th><?php _e('金额', 'digital-collections'); ?></th>
<th><?php _e('时间', 'digital-collections'); ?></th>
<th><?php _e('状态', 'digital-collections'); ?></th>
</tr>
</thead>
<tbody>
<?php if ($recent_transactions) : ?>
<?php foreach ($recent_transactions as $transaction) : ?>
<tr>
<td><?php echo esc_html($transaction->user_login); ?></td>
<td><?php echo esc_html($transaction->post_title); ?></td>
<td><?php echo esc_html($transaction->transaction_type); ?></td>
<td>¥<?php echo number_format($transaction->amount, 2); ?></td>
