首页 / 教程文章 / WordPress文创插件实现柔性产品生命周期管理的教程

WordPress文创插件实现柔性产品生命周期管理的教程

WordPress文创插件实现柔性产品生命周期管理的教程

引言:文创产品生命周期管理的挑战

在文创产业快速发展的今天,文创产品从概念构思到市场退出的完整生命周期管理变得日益复杂。传统的产品管理方法往往难以适应文创产品快速迭代、个性化定制和灵活调整的需求。本文将介绍如何通过WordPress插件实现柔性产品生命周期管理,帮助文创企业高效管理产品从创意到退市的每一个阶段。

系统环境与准备工作

1.1 环境要求

  • WordPress 5.8或更高版本
  • PHP 7.4或更高版本
  • MySQL 5.6或更高版本
  • 至少256MB内存

1.2 安装基础插件

在开始之前,请确保已安装以下基础插件:

  • Advanced Custom Fields(用于自定义字段)
  • WooCommerce(如果需要电商功能)

创建文创产品生命周期管理插件

2.1 插件基础结构

<?php
/**
 * Plugin Name: 文创产品生命周期管理器
 * Plugin URI: https://yourwebsite.com/
 * Description: 用于管理文创产品从概念到退市的完整生命周期
 * Version: 1.0.0
 * Author: 你的名字
 * License: GPL v2 or later
 */

// 防止直接访问
if (!defined('ABSPATH')) {
    exit;
}

// 定义插件常量
define('CPLM_VERSION', '1.0.0');
define('CPLM_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('CPLM_PLUGIN_URL', plugin_dir_url(__FILE__));

// 初始化插件
add_action('plugins_loaded', 'cplm_init');

function cplm_init() {
    // 检查必要组件
    if (!class_exists('ACF')) {
        add_action('admin_notices', 'cplm_acf_notice');
        return;
    }
    
    // 加载核心功能
    require_once CPLM_PLUGIN_DIR . 'includes/class-product-lifecycle.php';
    require_once CPLM_PLUGIN_DIR . 'includes/class-product-stages.php';
    require_once CPLM_PLUGIN_DIR . 'includes/class-analytics.php';
    
    // 初始化主类
    $cplm_main = new Product_Lifecycle_Manager();
    $cplm_main->init();
}

function cplm_acf_notice() {
    ?>
    <div class="notice notice-error">
        <p>文创产品生命周期管理器需要Advanced Custom Fields插件。请先安装并激活ACF插件。</p>
    </div>
    <?php
}
?>

2.2 产品生命周期阶段定义

<?php
/**
 * 产品生命周期阶段管理类
 */
class Product_Stages_Manager {
    
    private $stages = array();
    
    public function __construct() {
        $this->define_stages();
    }
    
    /**
     * 定义文创产品的生命周期阶段
     */
    private function define_stages() {
        $this->stages = array(
            'concept' => array(
                'name' => '概念阶段',
                'description' => '创意构思和市场调研',
                'color' => '#3498db',
                'order' => 1,
                'fields' => array('idea_description', 'target_audience', 'feasibility_analysis')
            ),
            'design' => array(
                'name' => '设计开发',
                'description' => '产品设计和原型制作',
                'color' => '#2ecc71',
                'order' => 2,
                'fields' => array('design_files', 'prototype_images', 'material_list')
            ),
            'production' => array(
                'name' => '生产制造',
                'description' => '小批量试产和量产',
                'color' => '#f39c12',
                'order' => 3,
                'fields' => array('production_cost', 'quality_control', 'supplier_info')
            ),
            'marketing' => array(
                'name' => '营销推广',
                'description' => '市场推广和销售',
                'color' => '#9b59b6',
                'order' => 4,
                'fields' => array('marketing_plan', 'sales_channels', 'promotion_budget')
            ),
            'maintenance' => array(
                'name' => '运营维护',
                'description' => '产品维护和用户反馈',
                'color' => '#34495e',
                'order' => 5,
                'fields' => array('customer_feedback', 'update_plans', 'support_requests')
            ),
            'retirement' => array(
                'name' => '退市处理',
                'description' => '产品退市和库存处理',
                'color' => '#e74c3c',
                'order' => 6,
                'fields' => array('retirement_reason', 'inventory_disposal', 'replacement_product')
            )
        );
    }
    
    /**
     * 获取所有阶段
     */
    public function get_stages() {
        return $this->stages;
    }
    
    /**
     * 获取特定阶段
     */
    public function get_stage($stage_id) {
        return isset($this->stages[$stage_id]) ? $this->stages[$stage_id] : false;
    }
    
    /**
     * 获取下一阶段
     */
    public function get_next_stage($current_stage) {
        $stages = $this->stages;
        $current_order = $stages[$current_stage]['order'];
        
        foreach ($stages as $stage_id => $stage) {
            if ($stage['order'] == $current_order + 1) {
                return $stage_id;
            }
        }
        
        return false;
    }
}
?>

实现产品管理功能

3.1 自定义文章类型和字段

<?php
/**
 * 产品生命周期管理主类
 */
class Product_Lifecycle_Manager {
    
    private $stages_manager;
    
    public function init() {
        $this->stages_manager = new Product_Stages_Manager();
        
        // 注册自定义文章类型
        add_action('init', array($this, 'register_post_types'));
        
        // 添加管理页面
        add_action('admin_menu', array($this, 'add_admin_pages'));
        
        // 注册自定义字段
        add_action('acf/init', array($this, 'register_custom_fields'));
        
        // 添加短代码
        add_shortcode('product_lifecycle', array($this, 'lifecycle_shortcode'));
    }
    
    /**
     * 注册文创产品自定义文章类型
     */
    public function register_post_types() {
        $labels = array(
            'name' => '文创产品',
            'singular_name' => '文创产品',
            'menu_name' => '文创产品',
            'add_new' => '添加新产品',
            'add_new_item' => '添加新文创产品',
            'edit_item' => '编辑文创产品',
            'new_item' => '新文创产品',
            'view_item' => '查看文创产品',
            'search_items' => '搜索文创产品',
            'not_found' => '未找到文创产品',
            'not_found_in_trash' => '回收站中无文创产品'
        );
        
        $args = array(
            'labels' => $labels,
            'public' => true,
            'has_archive' => true,
            'menu_icon' => 'dashicons-art',
            'supports' => array('title', 'editor', 'thumbnail', 'excerpt'),
            'rewrite' => array('slug' => 'cultural-products'),
            'show_in_rest' => true,
            'taxonomies' => array('product_category')
        );
        
        register_post_type('cultural_product', $args);
        
        // 注册产品分类
        register_taxonomy(
            'product_category',
            'cultural_product',
            array(
                'label' => '产品分类',
                'rewrite' => array('slug' => 'product-category'),
                'hierarchical' => true,
                'show_admin_column' => true
            )
        );
    }
    
    /**
     * 注册产品生命周期字段
     */
    public function register_custom_fields() {
        if (!function_exists('acf_add_local_field_group')) return;
        
        $stages = $this->stages_manager->get_stages();
        
        $fields = array();
        
        // 添加当前阶段选择字段
        $fields[] = array(
            'key' => 'field_current_stage',
            'label' => '当前阶段',
            'name' => 'current_stage',
            'type' => 'select',
            'choices' => array(),
            'default_value' => 'concept'
        );
        
        // 为每个阶段生成选项
        foreach ($stages as $stage_id => $stage) {
            $fields[0]['choices'][$stage_id] = $stage['name'];
        }
        
        // 添加阶段时间线字段
        $fields[] = array(
            'key' => 'field_stage_timeline',
            'label' => '阶段时间线',
            'name' => 'stage_timeline',
            'type' => 'repeater',
            'sub_fields' => array(
                array(
                    'key' => 'field_stage_id',
                    'label' => '阶段',
                    'name' => 'stage',
                    'type' => 'select',
                    'choices' => $fields[0]['choices']
                ),
                array(
                    'key' => 'field_start_date',
                    'label' => '开始日期',
                    'name' => 'start_date',
                    'type' => 'date_picker'
                ),
                array(
                    'key' => 'field_end_date',
                    'label' => '结束日期',
                    'name' => 'end_date',
                    'type' => 'date_picker'
                ),
                array(
                    'key' => 'field_stage_notes',
                    'label' => '阶段备注',
                    'name' => 'notes',
                    'type' => 'textarea'
                )
            )
        );
        
        // 添加产品数据字段
        $fields[] = array(
            'key' => 'field_product_data',
            'label' => '产品数据',
            'name' => 'product_data',
            'type' => 'group',
            'sub_fields' => array(
                array(
                    'key' => 'field_development_cost',
                    'label' => '开发成本',
                    'name' => 'development_cost',
                    'type' => 'number'
                ),
                array(
                    'key' => 'field_production_cost',
                    'label' => '生产成本',
                    'name' => 'production_cost',
                    'type' => 'number'
                ),
                array(
                    'key' => 'field_expected_sales',
                    'label' => '预期销量',
                    'name' => 'expected_sales',
                    'type' => 'number'
                ),
                array(
                    'key' => 'field_actual_sales',
                    'label' => '实际销量',
                    'name' => 'actual_sales',
                    'type' => 'number'
                )
            )
        );
        
        acf_add_local_field_group(array(
            'key' => 'group_product_lifecycle',
            'title' => '产品生命周期管理',
            'fields' => $fields,
            'location' => array(
                array(
                    array(
                        'param' => 'post_type',
                        'operator' => '==',
                        'value' => 'cultural_product',
                    ),
                ),
            ),
        ));
    }
}
?>

3.2 产品生命周期可视化

<?php
/**
 * 产品生命周期可视化短代码
 */
public function lifecycle_shortcode($atts) {
    $atts = shortcode_atts(array(
        'product_id' => get_the_ID(),
        'show_timeline' => true,
        'show_metrics' => true
    ), $atts);
    
    $product_id = intval($atts['product_id']);
    $output = '';
    
    // 获取产品信息
    $current_stage = get_field('current_stage', $product_id);
    $stage_timeline = get_field('stage_timeline', $product_id);
    $product_data = get_field('product_data', $product_id);
    
    // 获取阶段管理器
    $stages_manager = new Product_Stages_Manager();
    $stages = $stages_manager->get_stages();
    
    // 构建生命周期可视化
    $output .= '<div class="product-lifecycle-container">';
    $output .= '<h3>产品生命周期进度</h3>';
    
    // 阶段进度条
    $output .= '<div class="lifecycle-progress-bar">';
    $total_stages = count($stages);
    $current_order = $stages[$current_stage]['order'];
    $progress_percentage = ($current_order / $total_stages) * 100;
    
    $output .= '<div class="progress-track">';
    foreach ($stages as $stage_id => $stage) {
        $is_active = $stage['order'] <= $current_order;
        $is_current = $stage_id === $current_stage;
        
        $output .= '<div class="progress-stage ' . ($is_active ? 'active' : '') . ' ' . ($is_current ? 'current' : '') . '" style="border-color: ' . $stage['color'] . '">';
        $output .= '<span class="stage-icon" style="background-color: ' . $stage['color'] . '"></span>';
        $output .= '<span class="stage-name">' . $stage['name'] . '</span>';
        $output .= '</div>';
    }
    $output .= '</div>';
    
    $output .= '<div class="progress-bar">';
    $output .= '<div class="progress-fill" style="width: ' . $progress_percentage . '%; background: linear-gradient(90deg, ';
    
    // 创建渐变颜色
    $color_count = 0;
    foreach ($stages as $stage_id => $stage) {
        if ($stage['order'] <= $current_order) {
            if ($color_count > 0) $output .= ', ';
            $output .= $stage['color'] . ' ' . (($stage['order'] - 1) / $total_stages * 100) . '%';
            $color_count++;
        }
    }
    $output .= ')"></div>';
    $output .= '</div>';
    $output .= '</div>';
    
    // 显示当前阶段详情
    if (isset($stages[$current_stage])) {
        $current_stage_info = $stages[$current_stage];
        $output .= '<div class="current-stage-info" style="border-left: 4px solid ' . $current_stage_info['color'] . '">';
        $output .= '<h4>当前阶段: ' . $current_stage_info['name'] . '</h4>';
        $output .= '<p>' . $current_stage_info['description'] . '</p>';
        
        // 显示阶段时间线
        if ($stage_timeline) {
            $output .= '<div class="stage-timeline">';
            foreach ($stage_timeline as $timeline_item) {
                if ($timeline_item['stage'] == $current_stage) {
                    $output .= '<p><strong>开始时间:</strong> ' . $timeline_item['start_date'] . '</p>';
                    if ($timeline_item['end_date']) {
                        $output .= '<p><strong>结束时间:</strong> ' . $timeline_item['end_date'] . '</p>';
                    }
                    if ($timeline_item['notes']) {
                        $output .= '<p><strong>备注:</strong> ' . $timeline_item['notes'] . '</p>';
                    }
                }
            }
            $output .= '</div>';
        }
        $output .= '</div>';
    }
    
    // 显示产品指标
    if ($atts['show_metrics'] && $product_data) {
        $output .= '<div class="product-metrics">';
        $output .= '<h4>产品指标</h4>';
        $output .= '<div class="metrics-grid">';
        
        $metrics = array(
            'development_cost' => '开发成本',
            'production_cost' => '生产成本',
            'expected_sales' => '预期销量',
            'actual_sales' => '实际销量'
        );
        
        foreach ($metrics as $key => $label) {
            if (isset($product_data[$key]) && $product_data[$key] !== '') {
                $value = $product_data[$key];
                $output .= '<div class="metric-item">';
                $output .= '<span class="metric-label">' . $label . '</span>';
                $output .= '<span class="metric-value">' . $value . '</span>';
                $output .= '</div>';
            }
        }
        
        // 计算投资回报率(简化版)
        if (isset($product_data['development_cost']) && isset($product_data['production_cost']) && isset($product_data['actual_sales'])) {
            $total_cost = floatval($product_data['development_cost']) + floatval($product_data['production_cost']);
            $revenue = floatval($product_data['actual_sales']) * 100; // 假设单价100元
            if ($total_cost > 0) {
                $roi = (($revenue - $total_cost) / $total_cost) * 100;
                $output .= '<div class="metric-item">';
                $output .= '<span class="metric-label">投资回报率</span>';
                $output .= '<span class="metric-value">' . number_format($roi, 2) . '%</span>';
                $output .= '</div>';
            }
        }
        
        $output .= '</div>';
        $output .= '</div>';
    }
    
    $output .= '</div>';
    
    // 添加CSS样式
    $output .= '<style>
        .product-lifecycle-container {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
            padding: 20px;
            background: #f8f9fa;
            border-radius: 8px;
            margin: 20px 0;
        }
        .lifecycle-progress-bar {
            margin: 30px 0;
        }
        .progress-track {
            display: flex;
            justify-content: space-between;
    .progress-stage {
        flex: 1;
        text-align: center;
        padding: 10px 5px;
        position: relative;
        border-top: 3px solid #ddd;
        transition: all 0.3s ease;
    }
    .progress-stage.active {
        border-color: #4CAF50;
    }
    .progress-stage.current {
        border-color: #2196F3;
        font-weight: bold;
    }
    .stage-icon {
        display: block;
        width: 20px;
        height: 20px;
        border-radius: 50%;
        margin: 0 auto 5px;
        background-color: #ddd;
    }
    .progress-bar {
        height: 8px;
        background-color: #e0e0e0;
        border-radius: 4px;
        overflow: hidden;
    }
    .progress-fill {
        height: 100%;
        transition: width 0.5s ease;
    }
    .current-stage-info {
        background: white;
        padding: 15px;
        border-radius: 5px;
        margin: 20px 0;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    .product-metrics {
        background: white;
        padding: 15px;
        border-radius: 5px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    .metrics-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 15px;
        margin-top: 15px;
    }
    .metric-item {
        padding: 10px;
        background: #f5f5f5;
        border-radius: 4px;
    }
    .metric-label {
        display: block;
        font-size: 12px;
        color: #666;
        margin-bottom: 5px;
    }
    .metric-value {
        display: block;
        font-size: 18px;
        font-weight: bold;
        color: #333;
    }
</style>';

return $output;

}


## 高级功能实现

### 4.1 数据分析与报告

<?php
/**

  • 产品数据分析类
    */

class Product_Analytics {


/**
 * 获取产品生命周期统计数据
 */
public static function get_lifecycle_stats($product_id = null) {
    global $wpdb;
    
    $stats = array();
    
    // 获取所有文创产品
    $args = array(
        'post_type' => 'cultural_product',
        'posts_per_page' => -1,
        'post_status' => 'publish'
    );
    
    if ($product_id) {
        $args['p'] = $product_id;
    }
    
    $products = get_posts($args);
    
    // 初始化统计数据
    $stage_counts = array(
        'concept' => 0,
        'design' => 0,
        'production' => 0,
        'marketing' => 0,
        'maintenance' => 0,
        'retirement' => 0
    );
    
    $total_development_cost = 0;
    $total_production_cost = 0;
    $total_expected_sales = 0;
    $total_actual_sales = 0;
    $product_count = 0;
    
    foreach ($products as $product) {
        $current_stage = get_field('current_stage', $product->ID);
        $product_data = get_field('product_data', $product->ID);
        
        // 统计各阶段产品数量
        if (isset($stage_counts[$current_stage])) {
            $stage_counts[$current_stage]++;
        }
        
        // 累加财务数据
        if ($product_data) {
            if (isset($product_data['development_cost'])) {
                $total_development_cost += floatval($product_data['development_cost']);
            }
            if (isset($product_data['production_cost'])) {
                $total_production_cost += floatval($product_data['production_cost']);
            }
            if (isset($product_data['expected_sales'])) {
                $total_expected_sales += floatval($product_data['expected_sales']);
            }
            if (isset($product_data['actual_sales'])) {
                $total_actual_sales += floatval($product_data['actual_sales']);
            }
        }
        
        $product_count++;
    }
    
    // 计算平均数据
    $stats['stage_distribution'] = $stage_counts;
    $stats['total_products'] = $product_count;
    $stats['avg_development_cost'] = $product_count > 0 ? $total_development_cost / $product_count : 0;
    $stats['avg_production_cost'] = $product_count > 0 ? $total_production_cost / $product_count : 0;
    $stats['sales_achievement_rate'] = $total_expected_sales > 0 ? ($total_actual_sales / $total_expected_sales) * 100 : 0;
    
    return $stats;
}

/**
 * 生成生命周期报告
 */
public static function generate_lifecycle_report($product_id = null) {
    $stats = self::get_lifecycle_stats($product_id);
    
    $report = array(
        'summary' => array(
            'total_products' => $stats['total_products'],
            'most_common_stage' => array_search(max($stats['stage_distribution']), $stats['stage_distribution']),
            'avg_total_cost' => $stats['avg_development_cost'] + $stats['avg_production_cost'],
            'sales_achievement_rate' => round($stats['sales_achievement_rate'], 2)
        ),
        'stage_analysis' => $stats['stage_distribution'],
        'financial_metrics' => array(
            'avg_development_cost' => round($stats['avg_development_cost'], 2),
            'avg_production_cost' => round($stats['avg_production_cost'], 2),
            'total_investment' => round($stats['avg_development_cost'] + $stats['avg_production_cost'], 2)
        ),
        'recommendations' => array()
    );
    
    // 基于数据分析生成建议
    if ($stats['stage_distribution']['concept'] > $stats['total_products'] * 0.4) {
        $report['recommendations'][] = '概念阶段产品过多,建议加快产品开发进度';
    }
    
    if ($stats['sales_achievement_rate'] < 80) {
        $report['recommendations'][] = '销售达成率较低,建议重新评估市场策略';
    }
    
    return $report;
}

/**
 * 显示分析报告短代码
 */
public static function analytics_report_shortcode($atts) {
    $atts = shortcode_atts(array(
        'type' => 'summary', // summary, detailed, comparison
        'product_id' => null
    ), $atts);
    
    $report = self::generate_lifecycle_report($atts['product_id']);
    
    ob_start();
    ?>
    <div class="analytics-report">
        <h3>文创产品生命周期分析报告</h3>
        
        <div class="report-summary">
            <h4>概要</h4>
            <div class="summary-grid">
                <div class="summary-item">
                    <span class="summary-label">产品总数</span>
                    <span class="summary-value"><?php echo $report['summary']['total_products']; ?></span>
                </div>
                <div class="summary-item">
                    <span class="summary-label">主要阶段</span>
                    <span class="summary-value"><?php echo $report['summary']['most_common_stage']; ?></span>
                </div>
                <div class="summary-item">
                    <span class="summary-label">平均总成本</span>
                    <span class="summary-value">¥<?php echo number_format($report['summary']['avg_total_cost'], 2); ?></span>
                </div>
                <div class="summary-item">
                    <span class="summary-label">销售达成率</span>
                    <span class="summary-value"><?php echo $report['summary']['sales_achievement_rate']; ?>%</span>
                </div>
            </div>
        </div>
        
        <div class="stage-analysis">
            <h4>阶段分布</h4>
            <div class="stage-chart">
                <?php foreach ($report['stage_analysis'] as $stage => $count): ?>
                    <div class="stage-bar">
                        <div class="stage-label"><?php echo $stage; ?></div>
                        <div class="stage-bar-container">
                            <div class="stage-bar-fill" style="width: <?php echo ($count / $report['summary']['total_products']) * 100; ?>%">
                                <span class="stage-count"><?php echo $count; ?></span>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
        
        <?php if (!empty($report['recommendations'])): ?>
        <div class="recommendations">
            <h4>优化建议</h4>
            <ul>
                <?php foreach ($report['recommendations'] as $recommendation): ?>
                    <li><?php echo $recommendation; ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
        <?php endif; ?>
    </div>
    
    <style>
        .analytics-report {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .summary-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
            margin: 15px 0;
        }
        .summary-item {
            padding: 15px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 6px;
            text-align: center;
        }
        .summary-label {
            display: block;
            font-size: 14px;
            opacity: 0.9;
            margin-bottom: 5px;
        }
        .summary-value {
            display: block;
            font-size: 24px;
            font-weight: bold;
        }
        .stage-chart {
            margin: 20px 0;
        }
        .stage-bar {
            margin: 10px 0;
            display: flex;
            align-items: center;
        }
        .stage-label {
            width: 120px;
            font-weight: bold;
        }
        .stage-bar-container {
            flex: 1;
            height: 30px;
            background: #f0f0f0;
            border-radius: 4px;
            overflow: hidden;
        }
        .stage-bar-fill {
            height: 100%;
            background: linear-gradient(90deg, #4CAF50, #8BC34A);
            display: flex;
            align-items: center;
            justify-content: flex-end;
            padding-right: 10px;
            transition: width 1s ease;
        }
        .stage-count {
            color: white;
            font-weight: bold;
            text-shadow: 1px 1px 2px rgba(0,0,0,0.3);
        }
        .recommendations ul {
            list-style-type: none;
            padding-left: 0;
        }
        .recommendations li {
            padding: 10px;
            margin: 5px 0;
            background: #fff3cd;
            border-left: 4px solid #ffc107;
            border-radius: 4px;
        }
    </style>
    <?php
    
    return ob_get_clean();
}

}

// 注册短代码
add_shortcode('product_analytics', array('Product_Analytics', 'analytics_report_shortcode'));
?>


### 4.2 自动化工作流

<?php
/**

  • 自动化工作流管理
    */

class Workflow_Automation {


/**
 * 自动推进产品阶段
 */
public static function auto_advance_stage() {
    $products = get_posts(array(
        'post_type' => 'cultural_product',
        'posts_per_page' => -1,
        'post_status' => 'publish'
    ));
    
    $stages_manager = new Product_Stages_Manager();
    $advanced_count = 0;
    
    foreach ($products as $product) {
        $current_stage = get_field('current_stage', $product->ID);
        $stage_timeline = get_field('stage_timeline', $product->ID);
        
        // 获取当前阶段的时间信息
        $current_stage_data = null;
        if ($stage_timeline) {
            foreach ($stage_timeline as $item) {
                if ($item['stage'] == $current_stage) {
                    $current_stage_data = $item;
                    break;
                }
            }
        }
        
        // 检查是否需要推进阶段
        if ($current_stage_data && $current_stage_data['end_date']) {
            $end_date = DateTime::createFromFormat('Ymd', $current_stage_data['end_date']);
            $today = new DateTime();
            
            // 如果阶段结束日期已过,自动推进到下一阶段
            if ($end_date < $today) {
                $next_stage = $stages_manager->get_next_stage($current_stage);
                
                if ($next_stage) {
                    // 更新当前阶段
                    update_field('current_stage', $next_stage, $product->ID);
                    
                    // 更新阶段时间线
                    if ($stage_timeline) {
                        $stage_timeline[] = array(
                            'stage' => $next_stage,
                            'start_date' => date('Ymd'),
                            'end_date' => '',
                            'notes' => '自动推进到下一阶段'
                        );
                        update_field('stage_timeline', $stage_timeline, $product->ID);
                    }
                    
                    // 发送通知
                    self::send_stage_advance_notification($product->ID, $current_stage, $next_stage);
                    
                    $advanced_count++;
                }
            }
        }
    }
    
    return $advanced_count;
}

/**
 * 发送阶段推进通知
 */
private static function send_stage_advance_notification($product_id, $old_stage, $new_stage) {
    $product = get_post($product_id);
    $stages_manager = new Product_Stages_Manager();
    
    $old_stage_info = $stages_manager->get_stage($old_stage);
    $new_stage_info = $stages_manager->get_stage($new_stage);
    
    // 获取产品负责人(这里假设使用作者)
    $author_id = $product->post_author;
    $author_email = get_the_author_meta('user_email', $author_id);
    
    $subject = '产品阶段已自动推进: ' . $product->post_title;
    $message = "
        <h3>产品阶段更新通知</h3>
        <p>您的文创产品 <strong>{$product->post_title}</strong> 已从 <strong>{$old_stage_info['name']}</strong> 阶段自动推进到 <strong>{$new_stage_info['name']}</strong> 阶段。</p>
        <p><strong>原阶段:</strong> {$old_stage_info['description']}</p>
        <p><strong>新阶段:</strong> {$new_stage_info['description']}</p>
        <p><strong>推进时间:</strong> " . date('Y-m-d H:i:s') . "</p>
        <p>请及时处理新阶段的相关工作。</p>
        <hr>
        <p><a href='" . get_edit_post_link($product_id) . "'>点击此处管理产品</a></p>
    ";
    
    // 发送邮件
    $headers = array('Content-Type: text/html; charset=UTF-8');
    wp_mail($author_email, $subject, $message, $headers);
    
    // 添加管理通知
    add_action('admin_notices', function() use ($product, $new_stage_info) {
        ?>
        <div class="notice notice-info">
            <p>产品 <strong><?php echo $product->post_title; ?></strong> 已自动推进到 <strong><?php echo $new_stage_info['name']; ?></strong> 阶段。</p>
        </div>
        <?php
    });
}

/**
 * 定期检查并推进阶段
 */
public static function schedule_stage_advancement() {
    if (!wp_next_scheduled('cplm_auto_advance_stages')) {
        wp_schedule_event(time(), 'daily', 'cplm_auto_advance_stages');
    }
    
    add_action('cplm_auto_advance_stages', array(__CLASS__, 'auto_advance_stage'));
}

}

// 初始化自动化工作流
add_action('init', array('Workflow_Automation', 'schedule_stage_advancement'));
?>


## 插件配置与使用

### 5.1 管理界面配置

<?php
/**

  • 添加管理页面
    */

public function add_admin_pages() {

// 主管理页面
add_menu_page(
    '文创产品生命周期管理',
    '文创产品管理',
    'manage_options',
    'cultural-product-lifecycle',
    array($this, 'render_admin_dashboard'),
    'dashicons-art',
    30
);

// 子页面:分析报告
add_submenu_page(
    'cultural-product-lifecycle',
    '产品分析报告',
    '分析报告',
    'manage_options',
    'cplm-analytics',
    array($this, 'render_analytics_page')
);

// 子页面:设置
add_submenu_page(
    'cultural-product-lifecycle',
    '插件设置',
    '设置',
    'manage_options',
    'cplm-settings',
    array($this, 'render_settings_page')
);

}

/**

  • 渲染管理仪表板
    */

public function render_admin_dashboard() {

?>
<div class="wrap">
    <h1
本文来自网络,不代表柔性供应链服务中心立场,转载请注明出处:https://mall.org.cn/6040.html

EXCHANGES®作者

上一篇
下一篇

为您推荐

发表回复

联系我们

联系我们

18559313275

在线咨询: QQ交谈

邮箱: vip@exchanges.center

工作时间:周一至周五,9:00-17:30,节假日休息
返回顶部