文章目录[隐藏]
基于WordPress构建网络传媒柔性协作平台的教程
概述:为什么选择WordPress?
在当今数字化媒体时代,传媒机构需要一个灵活、可扩展且成本效益高的协作平台。WordPress作为全球最流行的内容管理系统,占据了互联网43%的网站份额,其强大的插件生态系统和高度可定制性使其成为构建网络传媒协作平台的理想选择。
本教程将指导您如何利用WordPress构建一个功能完整的网络传媒柔性协作平台,该平台将支持内容创作、团队协作、工作流管理和多媒体内容发布等功能。我们将从基础环境搭建开始,逐步实现高级协作功能。
环境准备与WordPress安装
服务器要求
- PHP 7.4或更高版本
- MySQL 5.6或更高版本或MariaDB 10.1或更高版本
- HTTPS支持(推荐)
- 至少1GB的RAM
安装WordPress
- 下载最新版WordPress并解压到您的网站根目录
- 创建MySQL数据库和用户
- 通过浏览器访问您的域名,按照安装向导完成安装
<?php
/**
* WordPress配置文件示例
* 将此文件重命名为wp-config.php并填入您的数据库信息
*/
// 数据库配置
define('DB_NAME', 'media_collab_db'); // 数据库名
define('DB_USER', 'media_user'); // 数据库用户名
define('DB_PASSWORD', 'strong_password'); // 数据库密码
define('DB_HOST', 'localhost'); // 数据库主机
define('DB_CHARSET', 'utf8mb4'); // 数据库字符集
// 安全密钥 - 从https://api.wordpress.org/secret-key/1.1/salt/生成
define('AUTH_KEY', 'put your unique phrase here');
define('SECURE_AUTH_KEY', 'put your unique phrase here');
define('LOGGED_IN_KEY', 'put your unique phrase here');
define('NONCE_KEY', 'put your unique phrase here');
define('AUTH_SALT', 'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT', 'put your unique phrase here');
define('NONCE_SALT', 'put your unique phrase here');
// WordPress数据表前缀
$table_prefix = 'wp_media_';
// 开发调试设置
define('WP_DEBUG', true); // 开发环境设为true,生产环境设为false
define('WP_DEBUG_LOG', true); // 将错误日志保存到/wp-content/debug.log
// 绝对路径
if (!defined('ABSPATH')) {
define('ABSPATH', __DIR__ . '/');
}
// 初始化WordPress
require_once ABSPATH . 'wp-settings.php';
?>
核心插件安装与配置
必备插件列表
- 用户角色管理:User Role Editor
- 协作工作流:PublishPress
- 文件管理:FileBird
- 任务管理:WP Project Manager
- 实时通信:Slack集成或Discord集成
用户角色与权限配置
传媒协作平台需要细粒度的用户角色控制。我们将创建以下自定义角色:
<?php
/**
* 自定义用户角色和权限
* 将此代码添加到主题的functions.php文件或自定义插件中
*/
function media_platform_custom_roles() {
// 添加总编辑角色
add_role('chief_editor', '总编辑', array(
'read' => true,
'edit_posts' => true,
'edit_others_posts' => true,
'edit_published_posts' => true,
'publish_posts' => true,
'delete_posts' => true,
'delete_others_posts' => true,
'delete_published_posts' => true,
'manage_categories' => true,
'moderate_comments' => true,
'upload_files' => true,
'edit_pages' => true,
'edit_others_pages' => true,
'edit_published_pages' => true,
'publish_pages' => true,
'delete_pages' => true,
'delete_others_pages' => true,
'delete_published_pages' => true,
'manage_options' => true,
));
// 添加记者角色
add_role('reporter', '记者', array(
'read' => true,
'edit_posts' => true,
'edit_published_posts' => true,
'publish_posts' => false, // 记者不能直接发布
'delete_posts' => true,
'delete_published_posts' => true,
'upload_files' => true,
'edit_pages' => false,
));
// 添加编辑角色
add_role('editor', '编辑', array(
'read' => true,
'edit_posts' => true,
'edit_others_posts' => true,
'edit_published_posts' => true,
'publish_posts' => false, // 需要审核
'delete_posts' => true,
'delete_others_posts' => true,
'delete_published_posts' => true,
'manage_categories' => true,
'upload_files' => true,
'edit_pages' => true,
));
}
add_action('init', 'media_platform_custom_roles');
/**
* 自定义投稿工作流状态
*/
function media_platform_custom_post_status() {
register_post_status('under_review', array(
'label' => '审核中',
'public' => false,
'exclude_from_search' => true,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop('审核中 <span class="count">(%s)</span>', '审核中 <span class="count">(%s)</span>'),
));
register_post_status('fact_checking', array(
'label' => '事实核查',
'public' => false,
'exclude_from_search' => true,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop('事实核查 <span class="count">(%s)</span>', '事实核查 <span class="count">(%s)</span>'),
));
}
add_action('init', 'media_platform_custom_post_status');
?>
协作工作流实现
自定义投稿表单
创建一个前端投稿表单,让记者可以提交新闻稿件:
<?php
/**
* 前端投稿表单
* 使用短代码 [media_submission_form] 在任何页面显示投稿表单
*/
function media_submission_form_shortcode() {
// 仅限登录用户投稿
if (!is_user_logged_in()) {
return '<p>请先<a href="' . wp_login_url(get_permalink()) . '">登录</a>后再投稿。</p>';
}
// 处理表单提交
if (isset($_POST['media_submit_article']) && wp_verify_nonce($_POST['media_nonce_field'], 'media_submit_action')) {
$title = sanitize_text_field($_POST['article_title']);
$content = wp_kses_post($_POST['article_content']);
$category = intval($_POST['article_category']);
$tags = sanitize_text_field($_POST['article_tags']);
// 创建文章
$post_data = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'under_review', // 使用自定义状态
'post_author' => get_current_user_id(),
'post_category' => array($category),
'tags_input' => explode(',', $tags),
'post_type' => 'post',
);
$post_id = wp_insert_post($post_data);
if ($post_id && !is_wp_error($post_id)) {
// 处理文件上传
if (!empty($_FILES['article_attachment']['name'])) {
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attachment_id = media_handle_upload('article_attachment', $post_id);
if (!is_wp_error($attachment_id)) {
set_post_thumbnail($post_id, $attachment_id);
}
}
return '<div class="alert alert-success">稿件已成功提交,正在审核中!</div>';
} else {
return '<div class="alert alert-error">提交失败,请重试。</div>';
}
}
// 输出投稿表单
ob_start();
?>
<div class="media-submission-form">
<h3>新闻投稿</h3>
<form method="post" enctype="multipart/form-data">
<?php wp_nonce_field('media_submit_action', 'media_nonce_field'); ?>
<div class="form-group">
<label for="article_title">标题 *</label>
<input type="text" id="article_title" name="article_title" required class="form-control">
</div>
<div class="form-group">
<label for="article_content">内容 *</label>
<?php
// 简易版编辑器
wp_editor('', 'article_content', array(
'textarea_name' => 'article_content',
'textarea_rows' => 10,
'media_buttons' => true,
'teeny' => true
));
?>
</div>
<div class="form-group">
<label for="article_category">分类</label>
<select id="article_category" name="article_category" class="form-control">
<?php
$categories = get_categories(array('hide_empty' => false));
foreach ($categories as $category) {
echo '<option value="' . $category->term_id . '">' . $category->name . '</option>';
}
?>
</select>
</div>
<div class="form-group">
<label for="article_tags">标签(用逗号分隔)</label>
<input type="text" id="article_tags" name="article_tags" class="form-control">
</div>
<div class="form-group">
<label for="article_attachment">附件(图片或文档)</label>
<input type="file" id="article_attachment" name="article_attachment" class="form-control">
</div>
<div class="form-group">
<input type="submit" name="media_submit_article" value="提交稿件" class="btn btn-primary">
</div>
</form>
</div>
<style>
.media-submission-form {
max-width: 800px;
margin: 0 auto;
padding: 20px;
background: #f9f9f9;
border-radius: 8px;
}
.form-group {
margin-bottom: 15px;
}
.form-control {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
}
.alert {
padding: 15px;
margin-bottom: 20px;
border-radius: 4px;
}
.alert-success {
background-color: #dff0d8;
border-color: #d6e9c6;
color: #3c763d;
}
.alert-error {
background-color: #f2dede;
border-color: #ebccd1;
color: #a94442;
}
</style>
<?php
return ob_get_clean();
}
add_shortcode('media_submission_form', 'media_submission_form_shortcode');
?>
任务管理与进度追踪
集成项目管理功能
使用WP Project Manager插件并扩展其功能:
<?php
/**
* 自定义任务看板功能
* 扩展项目管理插件功能
*/
class MediaTaskBoard {
public function __construct() {
// 初始化任务看板
add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
add_shortcode('media_task_board', array($this, 'render_task_board'));
add_action('wp_ajax_update_task_status', array($this, 'ajax_update_task_status'));
}
// 加载必要的脚本和样式
public function enqueue_scripts() {
wp_enqueue_style('media-task-board', get_template_directory_uri() . '/css/task-board.css');
wp_enqueue_script('media-task-board', get_template_directory_uri() . '/js/task-board.js', array('jquery'), '1.0', true);
wp_localize_script('media-task-board', 'media_ajax', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('media_task_nonce')
));
}
// 渲染任务看板
public function render_task_board() {
if (!is_user_logged_in()) {
return '<p>请登录查看任务看板</p>';
}
$user_id = get_current_user_id();
$tasks = $this->get_user_tasks($user_id);
ob_start();
?>
<div class="media-task-board">
<h3>我的任务看板</h3>
<div class="task-columns">
<div class="task-column todo">
<h4>待处理</h4>
<?php $this->render_tasks($tasks, 'todo'); ?>
</div>
<div class="task-column in-progress">
<h4>进行中</h4>
<?php $this->render_tasks($tasks, 'in-progress'); ?>
</div>
<div class="task-column review">
<h4>审核中</h4>
<?php $this->render_tasks($tasks, 'review'); ?>
</div>
<div class="task-column completed">
<h4>已完成</h4>
<?php $this->render_tasks($tasks, 'completed'); ?>
</div>
</div>
</div>
<?php
return ob_get_clean();
}
// 获取用户任务
private function get_user_tasks($user_id) {
// 这里应连接项目管理插件的数据
// 简化示例,实际应查询数据库
return array(
array('id' => 1, 'title' => '撰写新闻报道', 'status' => 'todo', 'due_date' => '2023-12-15'),
array('id' => 2, 'title' => '编辑视频内容', 'status' => 'in-progress', 'due_date' => '2023-12-10'),
array('id' => 3, 'title' => '审核新闻稿件', 'status' => 'review', 'due_date' => '2023-12-05'),
);
}
// 渲染任务卡片
private function render_tasks($tasks, $status) {
foreach ($tasks as $task) {
if ($task['status'] === $status) {
echo '<div class="task-card" data-task-id="' . $task['id'] . '">';
echo '<h5>' . esc_html($task['title']) . '</h5>';
echo '<p>截止日期: ' . $task['due_date'] . '</p>';
echo '<div class="task-actions">';
echo '<button class="move-task" data-target-status="' . $this->get_next_status($status) . '">→</button>';
echo '</div>';
echo '</div>';
}
}
}
// 获取下一个状态
private function get_next_status($current_status) {
$status_flow = array('todo', 'in-progress', 'review', 'completed');
$current_index = array_search($current_status, $status_flow);
return isset($status_flow[$current_index + 1]) ? $status_flow[$current_index + 1] : $current_status;
}
// AJAX更新任务状态
public function ajax_update_task_status() {
check_ajax_referer('media_task_nonce', 'nonce');
$task_id = intval($_POST['task_id']);
$new_status = sanitize_text_field($_POST['new_status']);
$user_id = get_current_user_id();
// 这里应更新数据库中的任务状态
// 简化示例
$response = array(
'success' => true,
'message' => '任务状态已更新',
'task_id' => $task_id,
'new_status' => $new_status
);
wp_send_json($response);
}
}
// 初始化任务看板
new MediaTaskBoard();
?>
多媒体内容管理
增强媒体库功能
创建专门的多媒体内容管理界面:
<?php
/**
* 多媒体内容管理
* 创建自定义媒体管理界面
*/
function media_platform_media_library() {
add_menu_page(
'多媒体库', // 页面标题
'多媒体库', // 菜单标题
'upload_files', // 权限
'media-library', // 菜单slug
'render_media_library_page', // 回调函数
'dashicons-images-alt2', // 图标
30 // 位置
);
}
add_action('admin_menu', 'media_platform_media_library');
function render_media_library_page() {
?>
<div class="wrap">
<h1>多媒体内容管理</h1>
<div class="media-filters">
<select id="media-type-filter">
<option value="all">所有类型</option>
<option value="image">图片</option>
<option value="video">视频</option>
<option value="audio">音频</option>
<option value="document">文档</option>
</select>
<select id="media-category-filter">
<option value="all">所有分类</option>
<?php
$categories = get_categories(array('taxonomy' => 'media_category', 'hide_empty' => false));
foreach ($categories as $category) {
echo '<option value="' . $category->term_id . '">' . $category->name . '</option>';
}
?>
</select>
<input type="text" id="media-search" placeholder="搜索多媒体内容...">
<button id="upload-new-media" class="button button-primary">上传新文件</button>
</div>
<div id="media-grid" class="media-grid">
<!-- 多媒体内容将通过AJAX加载 -->
</div>
<div id="media-pagination" class="media-pagination">
<!-- 分页控件 -->
</div>
</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
var currentPage = 1;
var itemsPerPage = 20;
// 加载媒体内容
function loadMediaContent() {
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
action: 'load_media_content',
page: currentPage,
per_page: itemsPerPage,
media_type: $('#media-type-filter').val(),
category: $('#media-category-filter').val(),
search: $('#media-search').val()
},
beforeSend: function() {
$('#media-grid').html('<div class="loading">加载中...</div>');
},
success: function(response) {
$('#media-grid').html(response.data.html);
$('#media-pagination').html(response.data.pagination);
}
});
}
// 初始加载
loadMediaContent();
// 筛选器变化时重新加载
$('#media-type-filter, #media-category-filter').change(function() {
currentPage = 1;
loadMediaContent();
});
// 搜索功能
$('#media-search').on('keyup', function(e) {
if (e.keyCode === 13) {
currentPage = 1;
loadMediaContent();
}
});
// 上传新文件
$('#upload-new-media').click(function() {
// 触发WordPress媒体上传器
var mediaUploader = wp.media({
title: '上传多媒体文件',
button: {
text: '选择文件'
},
multiple: true
});
mediaUploader.on('select', function() {
var attachments = mediaUploader.state().get('selection').toJSON();
// 批量上传
$.each(attachments, function(index, attachment) {
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
action: 'process_media_upload',
attachment_id: attachment.id,
category: $('#media-category-filter').val()
},
success: function(response) {
if (response.success) {
loadMediaContent(); // 重新加载内容
}
}
});
});
});
mediaUploader.open();
});
});
</script>
<style>
.media-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 15px;
margin: 20px 0;
}
.media-item {
border: 1px solid #ddd;
border-radius: 5px;
padding: 10px;
background: white;
position: relative;
}
.media-thumbnail {
width: 100%;
height: 150px;
object-fit: cover;
border-radius: 3px;
}
.media-info {
margin-top: 10px;
}
.media-title {
font-weight: bold;
margin-bottom: 5px;
word-break: break-word;
}
.media-actions {
display: flex;
justify-content: space-between;
margin-top: 10px;
}
.media-filters {
display: flex;
gap: 10px;
margin: 20px 0;
flex-wrap: wrap;
align-items: center;
}
.media-filters select,
.media-filters input {
padding: 5px 10px;
border: 1px solid #ddd;
border-radius: 3px;
}
.loading {
text-align: center;
padding: 40px;
grid-column: 1 / -1;
}
</style>
<?php
}
// AJAX处理函数
add_action('wp_ajax_load_media_content', 'ajax_load_media_content');
function ajax_load_media_content() {
$page = intval($_POST['page']);
$per_page = intval($_POST['per_page']);
$media_type = sanitize_text_field($_POST['media_type']);
$category = intval($_POST['category']);
$search = sanitize_text_field($_POST['search']);
$args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'posts_per_page' => $per_page,
'paged' => $page,
's' => $search
);
// 按媒体类型筛选
if ($media_type !== 'all') {
$args['post_mime_type'] = $media_type . '/*';
}
// 按分类筛选
if ($category !== 'all') {
$args['tax_query'] = array(
array(
'taxonomy' => 'media_category',
'field' => 'term_id',
'terms' => $category
)
);
}
$query = new WP_Query($args);
$html = '';
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$attachment_id = get_the_ID();
$thumbnail = wp_get_attachment_image_src($attachment_id, 'medium');
$file_url = wp_get_attachment_url($attachment_id);
$file_type = get_post_mime_type($attachment_id);
$html .= '<div class="media-item">';
$html .= '<img src="' . esc_url($thumbnail[0]) . '" class="media-thumbnail" alt="' . esc_attr(get_the_title()) . '">';
$html .= '<div class="media-info">';
$html .= '<div class="media-title">' . esc_html(get_the_title()) . '</div>';
$html .= '<div class="media-meta">' . human_filesize(filesize(get_attached_file($attachment_id))) . ' • ' . $file_type . '</div>';
$html .= '</div>';
$html .= '<div class="media-actions">';
$html .= '<button class="button button-small insert-media" data-url="' . esc_url($file_url) . '">插入</button>';
$html .= '<button class="button button-small delete-media" data-id="' . $attachment_id . '">删除</button>';
$html .= '</div>';
$html .= '</div>';
}
wp_reset_postdata();
} else {
$html = '<div class="no-media">未找到多媒体内容</div>';
}
// 分页
$total_pages = $query->max_num_pages;
$pagination = '';
if ($total_pages > 1) {
$pagination .= '<div class="pagination">';
if ($page > 1) {
$pagination .= '<button class="page-button" data-page="' . ($page - 1) . '">上一页</button>';
}
for ($i = 1; $i <= $total_pages; $i++) {
if ($i == $page) {
$pagination .= '<span class="current-page">' . $i . '</span>';
} else {
$pagination .= '<button class="page-button" data-page="' . $i . '">' . $i . '</button>';
}
}
if ($page < $total_pages) {
$pagination .= '<button class="page-button" data-page="' . ($page + 1) . '">下一页</button>';
}
$pagination .= '</div>';
}
wp_send_json_success(array(
'html' => $html,
'pagination' => $pagination
));
}
// 文件大小格式化函数
function human_filesize($bytes, $decimals = 2) {
$size = array('B','KB','MB','GB','TB','PB','EB','ZB','YB');
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
}
?>
## 实时协作与通信集成
### Slack/Discord集成
<?php
/**
- Slack集成功能
- 实现WordPress与Slack的实时通信
*/
class MediaPlatformSlackIntegration {
private $webhook_url;
private $channel;
public function __construct() {
$this->webhook_url = get_option('media_slack_webhook');
$this->channel = get_option('media_slack_channel', '#media-team');
// 文章状态变化时发送通知
add_action('transition_post_status', array($this, 'notify_post_status_change'), 10, 3);
// 新评论通知
add_action('comment_post', array($this, 'notify_new_comment'), 10, 2);
// 任务分配通知
add_action('media_task_assigned', array($this, 'notify_task_assigned'), 10, 2);
}
/**
* 发送消息到Slack
*/
public function send_to_slack($message, $attachments = array()) {
if (empty($this->webhook_url)) {
return false;
}
$payload = array(
'channel' => $this->channel,
'username' => '媒体协作平台',
'text' => $message,
'icon_emoji' => ':newspaper:',
'attachments' => $attachments
);
$args = array(
'body' => json_encode($payload),
'headers' => array(
'Content-Type' => 'application/json'
),
'timeout' => 30
);
$response = wp_remote_post($this->webhook_url, $args);
return !is_wp_error($response);
}
/**
* 文章状态变化通知
*/
public function notify_post_status_change($new_status, $old_status, $post) {
if ($post->post_type !== 'post') {
return;
}
$author = get_userdata($post->post_author);
$post_url = get_permalink($post->ID);
$status_labels = array(
'publish' => '已发布',
'pending' => '待审核',
'draft' => '草稿',
'under_review' => '审核中',
'fact_checking' => '事实核查'
);
$new_status_label = isset($status_labels[$new_status]) ? $status_labels[$new_status] : $new_status;
$old_status_label = isset($status_labels[$old_status]) ? $status_labels[$old_status] : $old_status;
$message = sprintf(
"文章状态更新: *%s*n作者: %sn从 %s 变更为 %sn<%s|查看文章>",
$post->post_title,
$author->display_name,
$old_status_label,
$new_status_label,
$post_url
);
$attachments = array(
array(
'color' => $this->get_status_color($new_status),
'fields' => array(
array(
'title' => '文章标题',
'value' => $post->post_title,
'short' => false
),
array(
'title' => '作者',
'value' => $author->display_name,
'short' => true
),
array(
'title' => '状态变更',
'value' => $old_status_label . ' → ' . $new_status_label,
'short' => true
)
),
'footer' => '媒体协作平台',
'ts' => time()
)
);
$this->send_to_slack($message, $attachments);
}
/**
* 新评论通知
*/
public function notify_new_comment($comment_id, $comment_approved) {
$comment = get_comment($comment_id);
$post = get_post($comment->comment_post_ID);
if (!$comment_approved) {
$message = sprintf(
"新评论等待审核: *%s*n文章: %sn评论者: %sn<%s|审核评论>",
wp_trim_words($comment->comment_content, 10),
$post->post_title,
$comment->comment_author,
admin_url("comment.php?action=editcomment&c={$comment_id}")
);
$this->send_to_slack($message);
}
}
/**
* 任务分配通知
*/
public function notify_task_assigned($task_id, $assignee_id) {
$task = get_post($task_id);
$assignee = get_userdata($assignee_id);
$assigner = wp_get_current_user();
$message = sprintf(
"新任务分配: *%s*n分配给: %sn分配者: %sn截止日期: %s",
$task->post_title,
$assignee->display_name,
$assigner->display_name,
get_post_meta($task_id, 'due_date', true)
);
$this->send_to_slack($message);
}
/**
* 获取状态对应的颜色
*/
private function get_status_color($status) {
$colors = array(
'publish' => '#36a64f',
'pending' => '#ff9900',
'draft' => '#cccccc',
'under_review' => '#3498db',
'fact_checking' => '#e74c3c'
);
return isset($colors[$status]) ? $colors[$status] : '#cccccc';
}
/**
* Slack设置页面
*/
public static function settings_page() {
if (isset($_POST['save_slack_settings'])) {
update_option('media_slack_webhook', sanitize_text_field($_POST['slack_webhook']));
update_option('media_slack_channel', sanitize_text_field($_POST['slack_channel']));
echo '<div class="notice notice-success"><p>设置已保存</p></div>';
}
$webhook = get_option('media_slack_webhook');
$channel = get_option('media_slack_channel', '#media-team');
?>
<div class="wrap">
<h1>Slack集成设置</h1>
<form method="post">
<table class="form-table">
<tr>
<th scope="row">Webhook URL</th>
<td>
<input type="text" name="slack_webhook" value="<?php echo esc_attr($webhook); ?>" class="regular-text">
<p class="description">从Slack应用配置中获取的Incoming Webhook URL</p>
</td>
</tr>
<tr>
<th scope="row">默认频道</th>
<td>
<input type="text" name="slack_channel" value="<?php echo esc_attr($channel); ?>" class="regular-text">
<p class="description">例如: #general 或 @username</p>
</td>
</tr>
</table>
<?php submit_button('保存设置', 'primary', 'save_slack_settings'); ?>
</form>
<h3>测试连接</h3>
<button id="test-slack-connection" class="button">发送测试消息</button>
<div id="test-result" style="margin-top: 10px;"></div>
<script>
jQuery(document).ready(function($) {
$('#test-slack-connection').click(function() {
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
action: 'test_slack_connection'
},
beforeSend: function() {
$('#test-result').html('<p>发送中...</p>');
},
success: function(response) {
if (response.success) {
$('#test-result').html('<p style="color: green;">测试消息发送成功!</p>');
} else {
$('#test-result').html('<p style="color: red;">发送失败: ' + response.data + '</p>');
}
}
});
});
});
</script>
</div>
<?php
}
}
// 初始化Slack集成
$slack_integration = new MediaPlatformSlackIntegration();
// 添加设置菜单
add_action('admin_menu', function() {
add_submenu_page(
'options-general.php',
'Slack集成',
'Slack集成',
'manage_options',
'media-slack-settings',
array('MediaPlatformSlackIntegration', 'settings_page')
);
});
// 测试连接AJAX处理
add_action('wp_ajax_test_slack_connection', function() {
$slack = new MediaPlatformSlackIntegration();
$success = $slack->send_to_slack('这是一条来自媒体协作平台的测试消息');
if ($success) {
wp_send_json_success();
} else {
wp_send_json_error('请检查Webhook URL配置');
}
});
?>
## 性能优化与安全加固
### 缓存策略
<?php
/**
- 平台性能优化
- 实现缓存策略和性能优化
*/
class MediaPlatformOptimization {
