文章目录[隐藏]
网络传媒WordPress多账号柔性管理插件开发教程
一、项目概述与需求分析
在当今网络传媒行业,内容创作者和团队经常需要管理多个WordPress账号。手动切换账号、分别发布内容不仅效率低下,还容易出错。因此,开发一款多账号柔性管理插件显得尤为重要。
核心需求:
- 支持多个WordPress账号的统一管理
- 实现一键切换和批量操作
- 提供安全可靠的账号凭证存储
- 支持内容同步和差异化发布
- 具备灵活的权限管理系统
二、开发环境搭建与插件基础结构
首先,我们需要创建插件的基本文件结构:
<?php
/**
* Plugin Name: 网络传媒多账号管理器
* Plugin URI: https://yourwebsite.com/
* Description: 用于管理多个WordPress账号的柔性管理插件
* Version: 1.0.0
* Author: 你的名字
* License: GPL v2 or later
*/
// 防止直接访问
if (!defined('ABSPATH')) {
exit;
}
// 定义插件常量
define('WMM_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('WMM_PLUGIN_URL', plugin_dir_url(__FILE__));
define('WMM_VERSION', '1.0.0');
// 初始化插件
add_action('plugins_loaded', 'wmm_init_plugin');
function wmm_init_plugin() {
// 检查必要组件
if (!class_exists('WP_List_Table')) {
require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
}
// 加载核心类
require_once WMM_PLUGIN_DIR . 'includes/class-account-manager.php';
require_once WMM_PLUGIN_DIR . 'includes/class-content-scheduler.php';
require_once WMM_PLUGIN_DIR . 'includes/class-api-handler.php';
// 初始化组件
if (is_admin()) {
require_once WMM_PLUGIN_DIR . 'admin/class-admin-interface.php';
new WMM_Admin_Interface();
}
}
?>
三、数据库设计与账号管理模块
创建账号管理的数据表结构:
<?php
// 文件路径: includes/class-database-setup.php
class WMM_Database_Setup {
public static function create_tables() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . 'wmm_accounts';
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
account_name varchar(100) NOT NULL,
site_url varchar(255) NOT NULL,
username varchar(100) NOT NULL,
password varchar(255) NOT NULL,
api_key varchar(255) DEFAULT '',
account_type varchar(50) DEFAULT 'wordpress',
status tinyint(1) DEFAULT 1,
created_at datetime DEFAULT CURRENT_TIMESTAMP,
last_used datetime DEFAULT NULL,
PRIMARY KEY (id),
UNIQUE KEY site_url (site_url(191))
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
// 创建内容同步日志表
$log_table = $wpdb->prefix . 'wmm_sync_logs';
$sql_log = "CREATE TABLE IF NOT EXISTS $log_table (
log_id mediumint(9) NOT NULL AUTO_INCREMENT,
account_id mediumint(9) NOT NULL,
post_id bigint(20) NOT NULL,
action varchar(50) NOT NULL,
status varchar(50) NOT NULL,
message text,
created_at datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (log_id),
KEY account_id (account_id),
KEY post_id (post_id)
) $charset_collate;";
dbDelta($sql_log);
}
// 加密存储密码
public static function encrypt_password($password) {
$key = wp_salt();
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
$encrypted = openssl_encrypt($password, 'aes-256-cbc', $key, 0, $iv);
return base64_encode($encrypted . '::' . $iv);
}
// 解密密码
public static function decrypt_password($encrypted_password) {
$key = wp_salt();
list($encrypted_data, $iv) = explode('::', base64_decode($encrypted_password), 2);
return openssl_decrypt($encrypted_data, 'aes-256-cbc', $key, 0, $iv);
}
}
// 插件激活时创建表
register_activation_hook(__FILE__, array('WMM_Database_Setup', 'create_tables'));
?>
四、账号管理类实现
<?php
// 文件路径: includes/class-account-manager.php
class WMM_Account_Manager {
private $wpdb;
private $table_name;
public function __construct() {
global $wpdb;
$this->wpdb = $wpdb;
$this->table_name = $wpdb->prefix . 'wmm_accounts';
}
/**
* 添加新账号
* @param array $account_data 账号数据
* @return int|false 插入ID或false
*/
public function add_account($account_data) {
// 验证必填字段
$required_fields = ['account_name', 'site_url', 'username', 'password'];
foreach ($required_fields as $field) {
if (empty($account_data[$field])) {
return new WP_Error('missing_field', sprintf(__('缺少必填字段: %s'), $field));
}
}
// 加密密码
$account_data['password'] = WMM_Database_Setup::encrypt_password($account_data['password']);
// 准备插入数据
$insert_data = [
'account_name' => sanitize_text_field($account_data['account_name']),
'site_url' => esc_url_raw($account_data['site_url']),
'username' => sanitize_user($account_data['username']),
'password' => $account_data['password'],
'api_key' => isset($account_data['api_key']) ? sanitize_text_field($account_data['api_key']) : '',
'account_type' => isset($account_data['account_type']) ? sanitize_text_field($account_data['account_type']) : 'wordpress',
'status' => isset($account_data['status']) ? intval($account_data['status']) : 1,
];
// 插入数据库
$result = $this->wpdb->insert($this->table_name, $insert_data);
if ($result) {
return $this->wpdb->insert_id;
}
return false;
}
/**
* 获取所有账号
* @param array $args 查询参数
* @return array 账号列表
*/
public function get_accounts($args = []) {
$defaults = [
'status' => 1,
'orderby' => 'account_name',
'order' => 'ASC',
'limit' => 0,
'offset' => 0,
];
$args = wp_parse_args($args, $defaults);
$where = "WHERE status = " . intval($args['status']);
$orderby = "ORDER BY " . sanitize_sql_orderby($args['orderby'] . ' ' . $args['order']);
$limit = $args['limit'] > 0 ? "LIMIT " . intval($args['offset']) . ", " . intval($args['limit']) : '';
$query = "SELECT * FROM {$this->table_name} {$where} {$orderby} {$limit}";
return $this->wpdb->get_results($query, ARRAY_A);
}
/**
* 测试账号连接
* @param int $account_id 账号ID
* @return array 测试结果
*/
public function test_connection($account_id) {
$account = $this->get_account_by_id($account_id);
if (!$account) {
return ['success' => false, 'message' => '账号不存在'];
}
// 解密密码
$password = WMM_Database_Setup::decrypt_password($account['password']);
// 尝试通过XML-RPC连接
if ($this->test_xmlrpc_connection($account['site_url'], $account['username'], $password)) {
return ['success' => true, 'message' => '连接成功'];
}
// 尝试通过REST API连接
if (!empty($account['api_key']) && $this->test_restapi_connection($account['site_url'], $account['api_key'])) {
return ['success' => true, 'message' => 'REST API连接成功'];
}
return ['success' => false, 'message' => '连接失败,请检查凭证'];
}
/**
* 测试XML-RPC连接
*/
private function test_xmlrpc_connection($site_url, $username, $password) {
include_once(ABSPATH . WPINC . '/class-IXR.php');
$client = new IXR_Client(trailingslashit($site_url) . 'xmlrpc.php');
$client->timeout = 10;
return $client->query('wp.getUsersBlogs', $username, $password);
}
}
?>
五、内容发布与同步模块
<?php
// 文件路径: includes/class-content-publisher.php
class WMM_Content_Publisher {
private $account_manager;
public function __construct() {
$this->account_manager = new WMM_Account_Manager();
}
/**
* 发布内容到多个账号
* @param int $local_post_id 本地文章ID
* @param array $account_ids 目标账号ID数组
* @param array $options 发布选项
* @return array 发布结果
*/
public function publish_to_accounts($local_post_id, $account_ids, $options = []) {
$results = [];
$local_post = get_post($local_post_id);
if (!$local_post) {
return ['success' => false, 'message' => '本地文章不存在'];
}
foreach ($account_ids as $account_id) {
$result = $this->publish_to_single_account($local_post, $account_id, $options);
$results[$account_id] = $result;
// 记录日志
$this->log_sync_action($account_id, $local_post_id,
$result['success'] ? 'publish' : 'error',
$result['success'] ? 'success' : 'failed',
$result['message']
);
}
return $results;
}
/**
* 发布到单个账号
*/
private function publish_to_single_account($post, $account_id, $options) {
$account = $this->account_manager->get_account_by_id($account_id);
if (!$account) {
return ['success' => false, 'message' => '账号不存在'];
}
// 准备发布数据
$post_data = [
'title' => $this->customize_content($post->post_title, $account_id, 'title', $options),
'content' => $this->customize_content($post->post_content, $account_id, 'content', $options),
'excerpt' => $this->customize_content($post->post_excerpt, $account_id, 'excerpt', $options),
'status' => isset($options['status']) ? $options['status'] : 'publish',
'categories' => $this->map_categories($post->ID, $account_id, $options),
'tags' => $this->get_post_tags($post->ID),
];
// 根据账号类型选择发布方式
if (!empty($account['api_key'])) {
return $this->publish_via_restapi($account, $post_data);
} else {
return $this->publish_via_xmlrpc($account, $post_data);
}
}
/**
* 通过REST API发布
*/
private function publish_via_restapi($account, $post_data) {
$api_url = trailingslashit($account['site_url']) . 'wp-json/wp/v2/posts';
$response = wp_remote_post($api_url, [
'headers' => [
'Authorization' => 'Bearer ' . $account['api_key'],
'Content-Type' => 'application/json',
],
'body' => json_encode($post_data),
'timeout' => 30,
]);
if (is_wp_error($response)) {
return ['success' => false, 'message' => $response->get_error_message()];
}
$body = json_decode(wp_remote_retrieve_body($response), true);
if (isset($body['id'])) {
return ['success' => true, 'message' => '发布成功', 'remote_id' => $body['id']];
}
return ['success' => false, 'message' => '发布失败: ' . json_encode($body)];
}
/**
* 内容定制化处理
*/
private function customize_content($content, $account_id, $content_type, $options) {
// 这里可以实现内容差异化逻辑
// 例如:替换关键词、添加账号特定信息等
$account = $this->account_manager->get_account_by_id($account_id);
// 示例:在标题末尾添加账号名称
if ($content_type === 'title' && isset($options['append_account_name']) && $options['append_account_name']) {
$content .= ' - ' . $account['account_name'];
}
// 示例:替换内容中的占位符
if (isset($options['replacements']) && is_array($options['replacements'])) {
foreach ($options['replacements'] as $search => $replace) {
$content = str_replace($search, $replace, $content);
}
}
return $content;
}
}
?>
六、后台管理界面开发
<?php
// 文件路径: admin/class-admin-interface.php
class WMM_Admin_Interface {
public function __construct() {
add_action('admin_menu', [$this, 'add_admin_menus']);
add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_scripts']);
}
public function add_admin_menus() {
// 主菜单
add_menu_page(
'多账号管理',
'多账号管理',
'manage_options',
'wmm-dashboard',
[$this, 'render_dashboard'],
'dashicons-admin-multisite',
30
);
// 子菜单
add_submenu_page(
'wmm-dashboard',
'账号管理',
'账号管理',
'manage_options',
'wmm-accounts',
[$this, 'render_accounts_page']
);
add_submenu_page(
'wmm-dashboard',
'内容发布',
'内容发布',
'manage_options',
'wmm-publish',
[$this, 'render_publish_page']
);
add_submenu_page(
'wmm-dashboard',
'同步日志',
'同步日志',
'manage_options',
'wmm-logs',
[$this, 'render_logs_page']
);
}
public function render_dashboard() {
?>
<div class="wrap wmm-dashboard">
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
<div class="wmm-stats-container">
<div class="wmm-stat-box">
<h3>总账号数</h3>
<p class="stat-number"><?php echo $this->get_account_count(); ?></p>
</div>
<div class="wmm-stat-box">
<h3>今日发布</h3>
<p class="stat-number"><?php echo $this->get_today_posts_count(); ?></p>
</div>
<div class="wmm-stat-box">
<h3>同步成功率</h3>
<p class="stat-number"><?php echo $this->get_success_rate(); ?>%</p>
</div>
</div>
<div class="wmm-quick-actions">
<h2>快速操作</h2>
<a href="<?php echo admin_url('admin.php?page=wmm-publish'); ?>" class="button button-primary">发布新内容</a>
<a href="<?php echo admin_url('admin.php?page=wmm-accounts&action=add'); ?>" class="button">添加新账号</a>
<a href="<?php echo admin_url('admin.php?page=wmm-logs'); ?>" class="button">查看同步日志</a>
</div>
</div>
<style>
.wmm-stat-box {
background: white;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
display: inline-block;
margin-right: 20px;
min-width: 150px;
}
.stat-number {
font-size: 2em;
font-weight: bold;
color: #0073aa;
}
</style>
<?php
}
public function render_accounts_page() {
$action = isset($_GET['action']) ? $_GET['action'] : 'list';
switch ($action) {
case 'add':
case 'edit':
$this->render_account_form();
break;
case 'test':
$this->test_account_connection();
break;
default:
$this->render_accounts_list();
break;
}
}
public function render_accounts_list() {
// 实例化账号列表表类
$accounts_table = new WMM_Accounts_Table();
$accounts_table->prepare_items();
?>
<div class="wrap">
<h1 class="wp-heading-inline">账号管理</h1>
<a href="<?php echo admin_url('admin.php?page=wmm-accounts&action=add'); ?>" class="page-title-action">添加新账号</a>
<form method="post">
<?php $accounts_table->display(); ?>
</form>
</div>
<?php
}
public function render_account_form() {
$account_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$account_data = [];
if ($account_id > 0) {
$account_manager = new WMM_Account_Manager();
$account_data = $account_manager->get_account_by_id($account_id);
}
?>
<div class="wrap">
<h1><?php echo $account_id ? '编辑账号' : '添加新账号'; ?></h1>
<form method="post" action="<?php echo admin_url('admin.php?page=wmm-accounts'); ?>">
<?php wp_nonce_field('wmm_save_account', 'wmm_account_nonce'); ?>
<table class="form-table">
<tr>
<th scope="row"><label for="account_name">账号名称</label></th>
<td>
<input type="text" id="account_name" name="account_name"
value="<?php echo isset($account_data['account_name']) ? esc_attr($account_data['account_name']) : ''; ?>"
class="regular-text" required>
<p class="description">用于识别的账号别名</p>
</td>
</tr>
<tr>
<th scope="row"><label for="site_url">网站地址</label></th>
<td>
<input type="url" id="site_url" name="site_url"
value="<?php echo isset($account_data['site_url']) ? esc_url($account_data['site_url']) : ''; ?>"
class="regular-text" required>
<p class="description">WordPress网站的完整URL</p>
</td>
</tr>
<tr>
<th scope="row"><label for="username">用户名</label></th>
<td>
<input type="text" id="username" name="username"
value="<?php echo isset($account_data['username']) ? esc_attr($account_data['username']) : ''; ?>"
class="regular-text" required>
</td>
</tr>
<tr>
<th scope="row"><label for="password">密码</label></th>
<td>
<input type="password" id="password" name="password"
value=""
class="regular-text" <?php echo $account_id ? '' : 'required'; ?>>
<p class="description"><?php echo $account_id ? '留空则不修改密码' : '请输入密码'; ?></p>
</td>
</tr>
<tr>
<th scope="row"><label for="api_key">API密钥</label></th>
<td>
<input type="text" id="api_key" name="api_key"
value="<?php echo isset($account_data['api_key']) ? esc_attr($account_data['api_key']) : ''; ?>"
class="regular-text">
<p class="description">REST API密钥(可选)</p>
</td>
</tr>
<tr>
<th scope="row"><label for="account_type">账号类型</label></th>
<td>
<select id="account_type" name="account_type">
<option value="wordpress" <?php selected(isset($account_data['account_type']) && $account_data['account_type'] == 'wordpress'); ?>>WordPress</option>
<option value="multisite" <?php selected(isset($account_data['account_type']) && $account_data['account_type'] == 'multisite'); ?>>多站点</option>
</select>
</td>
</tr>
</table>
<input type="hidden" name="account_id" value="<?php echo $account_id; ?>">
<input type="hidden" name="action" value="save_account">
<?php submit_button($account_id ? '更新账号' : '添加账号'); ?>
<?php if ($account_id): ?>
<a href="<?php echo admin_url('admin.php?page=wmm-accounts&action=test&id=' . $account_id); ?>" class="button">测试连接</a>
<?php endif; ?>
</form>
</div>
<?php
}
public function enqueue_admin_scripts($hook) {
if (strpos($hook, 'wmm-') === false) {
return;
}
wp_enqueue_style('wmm-admin-style', WMM_PLUGIN_URL . 'assets/css/admin.css', [], WMM_VERSION);
wp_enqueue_script('wmm-admin-script', WMM_PLUGIN_URL . 'assets/js/admin.js', ['jquery'], WMM_VERSION, true);
wp_localize_script('wmm-admin-script', 'wmm_ajax', [
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('wmm_ajax_nonce')
]);
}
}
// 账号列表表格类
class WMM_Accounts_Table extends WP_List_Table {
public function __construct() {
parent::__construct([
'singular' => '账号',
'plural' => '账号',
'ajax' => false
]);
}
public function get_columns() {
return [
'cb' => '<input type="checkbox" />',
'account_name' => '账号名称',
'site_url' => '网站地址',
'username' => '用户名',
'account_type' => '类型',
'status' => '状态',
'last_used' => '最后使用',
'actions' => '操作'
];
}
public function prepare_items() {
$account_manager = new WMM_Account_Manager();
$per_page = 20;
$current_page = $this->get_pagenum();
$args = [
'limit' => $per_page,
'offset' => ($current_page - 1) * $per_page
];
$this->items = $account_manager->get_accounts($args);
$total_items = count($account_manager->get_accounts(['limit' => 0]));
$this->set_pagination_args([
'total_items' => $total_items,
'per_page' => $per_page,
'total_pages' => ceil($total_items / $per_page)
]);
}
public function column_default($item, $column_name) {
return isset($item[$column_name]) ? $item[$column_name] : '—';
}
public function column_account_name($item) {
$actions = [
'edit' => sprintf('<a href="?page=wmm-accounts&action=edit&id=%s">编辑</a>', $item['id']),
'test' => sprintf('<a href="?page=wmm-accounts&action=test&id=%s">测试</a>', $item['id']),
'delete' => sprintf('<a href="?page=wmm-accounts&action=delete&id=%s" onclick="return confirm('确定删除?')">删除</a>', $item['id'])
];
return sprintf('<strong>%1$s</strong> %2$s',
$item['account_name'],
$this->row_actions($actions)
);
}
public function column_status($item) {
return $item['status'] == 1 ?
'<span class="dashicons dashicons-yes" style="color:green"></span> 启用' :
'<span class="dashicons dashicons-no" style="color:red"></span> 禁用';
}
public function column_actions($item) {
return sprintf(
'<button class="button wmm-quick-publish" data-account-id="%s">快速发布</button>',
$item['id']
);
}
}
?>
## 七、内容发布界面与批量操作
<?php
// 文件路径: admin/class-publish-interface.php
class WMM_Publish_Interface {
public function render_publish_page() {
$account_manager = new WMM_Account_Manager();
$accounts = $account_manager->get_accounts();
// 获取最近的文章
$recent_posts = get_posts([
'numberposts' => 10,
'orderby' => 'date',
'order' => 'DESC'
]);
?>
<div class="wrap">
<h1>内容发布</h1>
<div class="wmm-publish-container">
<div class="wmm-publish-left">
<h2>选择发布内容</h2>
<div class="wmm-content-selector">
<h3>发布新内容</h3>
<textarea id="wmm-new-content" rows="10" class="large-text" placeholder="输入要发布的内容..."></textarea>
<h3>或选择已有文章</h3>
<select id="wmm-existing-post" class="regular-text">
<option value="">选择文章...</option>
<?php foreach ($recent_posts as $post): ?>
<option value="<?php echo $post->ID; ?>">
<?php echo esc_html($post->post_title); ?> (<?php echo get_the_date('', $post); ?>)
</option>
<?php endforeach; ?>
</select>
</div>
<div class="wmm-content-options">
<h3>发布选项</h3>
<table class="form-table">
<tr>
<th><label for="wmm-post-status">发布状态</label></th>
<td>
<select id="wmm-post-status">
<option value="publish">立即发布</option>
<option value="draft">保存为草稿</option>
<option value="pending">待审核</option>
</select>
</td>
</tr>
<tr>
<th><label for="wmm-append-account">添加账号名称</label></th>
<td>
<input type="checkbox" id="wmm-append-account" checked>
<label for="wmm-append-account">在标题末尾添加账号名称</label>
</td>
</tr>
<tr>
<th><label>内容替换</label></th>
<td>
<div id="wmm-replacements-container">
<div class="wmm-replacement-row">
<input type="text" placeholder="查找内容" class="regular-text">
<input type="text" placeholder="替换为" class="regular-text">
<button type="button" class="button wmm-remove-replacement">删除</button>
</div>
</div>
<button type="button" id="wmm-add-replacement" class="button">添加替换规则</button>
</td>
</tr>
</table>
</div>
</div>
<div class="wmm-publish-right">
<h2>选择目标账号</h2>
<div class="wmm-account-selector">
<div class="wmm-account-select-all">
<label>
<input type="checkbox" id="wmm-select-all-accounts">
<strong>全选</strong>
</label>
</div>
<div class="wmm-account-list">
<?php foreach ($accounts as $account): ?>
<div class="wmm-account-item">
<label>
<input type="checkbox" name="target_accounts[]"
value="<?php echo $account['id']; ?>"
class="wmm-account-checkbox"
data-account-name="<?php echo esc_attr($account['account_name']); ?>">
<?php echo esc_html($account['account_name']); ?>
<span class="wmm-site-url">(<?php echo esc_html($account['site_url']); ?>)</span>
</label>
<span class="wmm-account-status <?php echo $account['status'] ? 'active' : 'inactive'; ?>">
<?php echo $account['status'] ? '启用' : '禁用'; ?>
</span>
</div>
<?php endforeach; ?>
</div>
</div>
<div class="wmm-publish-actions">
<button id="wmm-test-publish" class="button">测试发布</button>
<button id="wmm-preview-publish" class="button">预览</button>
<button id="wmm-execute-publish" class="button button-primary">执行发布</button>
</div>
<div id="wmm-publish-results" style="display:none;">
<h3>发布结果</h3>
<div id="wmm-results-container"></div>
</div>
</div>
</div>
</div>
<script>
jQuery(document).ready(function($) {
// 全选/取消全选
$('#wmm-select-all-accounts').on('change', function() {
$('.wmm-account-checkbox').prop('checked', $(this).prop('checked'));
});
// 添加替换规则
$('#wmm-add-replacement').on('click', function() {
var row = $('<div class="wmm-replacement-row">' +
'<input type="text" placeholder="查找内容" class="regular-text">' +
'<input type="text" placeholder="替换为" class="regular-text">' +
'<button type="button" class="button wmm-remove-replacement">删除</button>' +
'</div>');
$('#wmm-replacements-container').append(row);
});
// 删除替换规则
$(document).on('click', '.wmm-remove-replacement', function() {
$(this).closest('.wmm-replacement-row').remove();
});
// 执行发布
$('#wmm-execute-publish').on('click', function() {
var postData = collectPublishData();
if (!validatePublishData(postData)) {
return;
}
$(this).prop('disabled', true).text('发布中...');
$.ajax({
url: wmm_ajax.ajax_url,
type: 'POST',
data: {
action: 'wmm_publish_content',
nonce: wmm_ajax.nonce,
data: postData
},
success: function(response) {
displayPublishResults(response.data);
$('#wmm-execute-publish').prop('disabled', false).text('执行发布');
}
});
});
function collectPublishData() {
var data = {
accounts: [],
options: {},
content: {}
};
// 收集选中的账号
$('.wmm-account-checkbox:checked').each(function() {
data.accounts.push($(this).val());
});
// 收集发布选项
data.options = {
status: $('#wmm-post-status').val(),
append_account_name: $('#wmm-append-account').prop('checked'),
replacements: {}
};
// 收集替换规则
$('.wmm-replacement-row').each(function() {
var find = $(this).find('input:first').val();
var replace = $(this).find('input:last').val();
if (find && replace) {
data.options.replacements[find] = replace;
}
});
// 收集内容
var existingPost = $('#wmm-existing-post').val();
if (existingPost) {
data.content.post_id = existingPost;
} else {
data.content.text = $('#wmm-new-content').val();
data.content.title = '新发布的内容'; // 实际应用中可以从内容中提取或让用户输入
}
return data;
}
function validatePublishData(data) {
if (data.accounts.length === 0) {
alert('请至少选择一个目标账号');
return false;
}
if (!data.content.post_id && !data.content.text) {
alert('请输入要发布的内容或选择已有文章');
return false;
}
return true;
}
function displayPublishResults(results) {
var container = $('#wmm-results-container');
container.empty();
for (var accountId in results) {
var result = results[accountId];
var statusClass = result.success ? 'success' : 'error';
var statusIcon = result.success ? '✓' : '✗';
container.append(
'<div class="wmm-result-item ' + statusClass + '">' +
'<span class="result-icon">' + statusIcon + '</span>' +
'<span class="result-account">账号 ' + accountId + '</span>' +
'<span class="result-message">' + result.message + '</span>' +
'</div>'
);
}
$('#wmm-publish-results').show();
}
});
</script>
<style>
.wmm-publish-container {
display: flex;
gap: 30px;
margin-top: 20px;
}
.wmm-publish-left {
flex: 2;
}
.wmm-publish-right {
flex: 1;
}
.wmm-account-list {
max-height: 400px;
overflow-y: auto;
border: 1px solid #ddd;
padding: 10px;
margin
