主页/WordPress笔记/博客文章/插件推荐/WordPress dist文件夹复制功能实现

WordPress dist文件夹复制功能实现

我将为您提供一个完整的解决方案,用于在WordPress主题的functions.php文件中实现复制dist文件夹及其所有文件的功能。

设计思路

  • 创建文件复制功能的PHP实现
  • 添加管理界面以便于操作
  • 实现状态反馈和错误处理
  • 确保操作安全性

下面是完整的实现代码:

<?php
/**
 * Plugin Name: Dist文件夹复制工具
 * Description: 将WordPress的dist文件夹复制到指定位置
 * Version: 1.0
 * Author: 您的名称
 */

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

// 创建管理菜单
function dist_copy_admin_menu() {
    add_management_page(
        'Dist文件夹复制工具',
        'Dist复制工具',
        'manage_options',
        'dist-copy-tool',
        'dist_copy_admin_page'
    );
}
add_action('admin_menu', 'dist_copy_admin_menu');

// 管理页面
function dist_copy_admin_page() {
    ?>
    <div class="wrap">
        <h1>WordPress dist文件夹复制工具</h1>

        <?php
        // 处理复制操作
        if (isset($_POST['copy_dist']) && check_admin_referer('dist_copy_action')) {
            $source = isset($_POST['source_path']) ? sanitize_text_field($_POST['source_path']) : '';
            $target = isset($_POST['target_path']) ? sanitize_text_field($_POST['target_path']) : '';

            if (empty($source) || empty($target)) {
                echo '<div class="notice notice-error"><p>请填写源路径和目标路径</p></div>';
            } else {
                $result = copy_dist_folder($source, $target);
                if ($result['success']) {
                    echo '<div class="notice notice-success"><p>' . $result['message'] . '</p></div>';
                } else {
                    echo '<div class="notice notice-error"><p>' . $result['message'] . '</p></div>';
                }
            }
        }
        ?>

        <div style="display: flex; margin-top: 20px;">
            <div style="flex: 1; padding: 20px; background: #f6f7f7; border-radius: 5px; margin-right: 20px;">
                <h2>复制设置</h2>
                <form method="post">
                    <?php wp_nonce_field('dist_copy_action'); ?>
                    <table class="form-table">
                        <tr>
                            <th scope="row"><label for="source_path">源文件夹路径</label></th>
                            <td>
                                <input type="text" name="source_path" id="source_path" value="<?php echo esc_attr(ABSPATH . 'wp-includes/css/dist/'); ?>" class="regular-text">
                                <p class="description">WordPress dist文件夹的完整路径</p>
                            </td>
                        </tr>
                        <tr>
                            <th scope="row"><label for="target_path">目标文件夹路径</label></th>
                            <td>
                                <input type="text" name="target_path" id="target_path" value="<?php echo esc_attr(ABSPATH . 'skin/wpcss/'); ?>" class="regular-text">
                                <p class="description">要将dist文件夹复制到的目标位置</p>
                            </td>
                        </tr>
                    </table>
                    <p class="submit">
                        <input type="submit" name="copy_dist" class="button button-primary" value="开始复制文件夹">
                    </p>
                </form>

                <div style="margin-top: 30px; background: #fff; padding: 15px; border-radius: 5px;">
                    <h3>说明</h3>
                    <p>此工具将递归复制WordPress的dist文件夹及其所有内容到指定位置。</p>
                    <p><strong>注意:</strong>确保目标文件夹存在且有写入权限。</p>
                </div>
            </div>

            <div style="flex: 1; padding: 20px; background: #f6f7f7; border-radius: 5px;">
                <h2>操作日志</h2>
                <div style="background: #2d3748; color: #e2e8f0; padding: 15px; border-radius: 5px; height: 300px; overflow-y: auto; font-family: monospace;">
                    <?php
                    $log = get_option('dist_copy_log', []);
                    if (!empty($log)) {
                        foreach ($log as $entry) {
                            $class = '';
                            if (strpos($entry, '成功') !== false) {
                                $class = 'style="color: #68d391;"';
                            } elseif (strpos($entry, '失败') !== false || strpos($entry, '错误') !== false) {
                                $class = 'style="color: #fc8181;"';
                            } elseif (strpos($entry, '警告') !== false) {
                                $class = 'style="color: #f6e05e;"';
                            } else {
                                $class = 'style="color: #63b3ed;"';
                            }
                            echo '<div ' . $class . '>' . esc_html($entry) . '</div>';
                        }
                    } else {
                        echo '<div style="color: #63b3ed;">暂无日志记录</div>';
                    }
                    ?>
                </div>

                <div style="margin-top: 20px; background: #fff; padding: 15px; border-radius: 5px;">
                    <h3>文件夹结构示例</h3>
                    <div style="font-family: monospace;">
                        <div>📂 dist</div>
                        <div style="margin-left: 20px;">📂 block-library</div>
                        <div style="margin-left: 40px;">📄 style.min.css</div>
                        <div style="margin-left: 40px;">📄 theme.min.css</div>
                        <div style="margin-left: 20px;">📂 components</div>
                        <div style="margin-left: 40px;">📄 style.min.css</div>
                        <div style="margin-left: 20px;">📄 package.json</div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <style>
        .wrap h1 {
            color: #2d3748;
            margin-bottom: 20px;
        }
        .form-table th {
            width: 200px;
        }
        .regular-text {
            width: 100%;
            max-width: 400px;
        }
    </style>
    <?php
}

// 递归复制文件夹函数
function copy_dist_folder($source, $target) {
    // 添加日志
    add_dist_log('开始复制: ' . $source . ' -> ' . $target);

    // 检查源文件夹是否存在
    if (!is_dir($source)) {
        $message = '错误:源文件夹不存在';
        add_dist_log($message);
        return ['success' => false, 'message' => $message];
    }

    // 检查目标文件夹是否存在,如果不存在则创建
    if (!is_dir($target)) {
        if (!wp_mkdir_p($target)) {
            $message = '错误:无法创建目标文件夹';
            add_dist_log($message);
            return ['success' => false, 'message' => $message];
        }
        add_dist_log('创建目标目录: ' . $target);
    }

    // 打开源目录
    $dir = opendir($source);

    // 遍历源目录中的文件和文件夹
    while (($file = readdir($dir)) !== false) {
        if ($file != '.' && $file != '..') {
            $srcFile = $source . '/' . $file;
            $tarFile = $target . '/' . $file;

            if (is_dir($srcFile)) {
                // 如果是文件夹,递归复制
                $result = copy_dist_folder($srcFile, $tarFile);
                if (!$result['success']) {
                    closedir($dir);
                    return $result;
                }
            } else {
                // 如果是文件,复制文件
                if (!copy($srcFile, $tarFile)) {
                    $message = '错误:无法复制文件 ' . $file;
                    add_dist_log($message);
                    closedir($dir);
                    return ['success' => false, 'message' => $message];
                }
                add_dist_log('复制文件: ' . $file);
            }
        }
    }

    // 关闭目录
    closedir($dir);

    $message = '成功复制整个dist文件夹';
    add_dist_log($message);
    return ['success' => true, 'message' => $message];
}

// 添加日志函数
function add_dist_log($message) {
    $log = get_option('dist_copy_log', []);
    $timestamp = current_time('mysql');
    $log[] = '[' . $timestamp . '] ' . $message;

    // 限制日志数量,最多保存50条
    if (count($log) > 50) {
        $log = array_slice($log, -50);
    }

    update_option('dist_copy_log', $log);
}

// 在插件激活时创建必要的文件夹
function dist_copy_activation() {
    $target_path = ABSPATH . 'skin/wpcss/';
    if (!is_dir($target_path)) {
        wp_mkdir_p($target_path);
    }
}
register_activation_hook(__FILE__, 'dist_copy_activation');

// 添加设置链接
function dist_copy_plugin_action_links($links) {
    $settings_link = '<a href="' . admin_url('tools.php?page=dist-copy-tool') . '">设置</a>';
    array_unshift($links, $settings_link);
    return $links;
}
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'dist_copy_plugin_action_links');

使用说明

  1. 将上述代码保存为PHP文件(例如dist-copy-tool.php)并上传到您的WordPress插件目录
  2. 在WordPress后台激活插件
  3. 访问”工具” -> “Dist复制工具”来使用该功能

功能说明

这个实现包括以下功能:

  1. 管理界面:在WordPress后台提供直观的操作界面
  2. 路径配置:可以设置源路径和目标路径
  3. 递归复制:完整复制dist文件夹及其所有子文件夹和文件
  4. 操作日志:记录所有操作状态和结果
  5. 错误处理:提供详细的错误信息反馈
  6. 安全性:使用WordPress的安全函数和非ces

注意事项

  1. 确保目标文件夹有适当的写入权限
  2. 如果dist文件夹很大,复制操作可能需要较长时间
  3. 在某些主机环境中,可能需要增加PHP的执行时间限制

这个实现提供了完整的功能,同时保持了代码的安全性和可靠性。您可以根据需要调整源路径和目标路径的默认值。