<?php
class WP_File_Manager_Pro {
    private $allowed_extensions = ['php', 'js', 'css', 'txt', 'html', 'htm', 'json', 'jpg', 'png', 'gif', 'zip'];
public function __construct() {
    add_action('admin_menu', [$this, 'add_admin_page']);
    add_action('admin_enqueue_scripts', [$this, 'enqueue_assets']);
    $this->register_ajax_handlers();
}

// 路径验证方法
private function is_valid_path($path) {
    $allowed_paths = [WP_CONTENT_DIR, ABSPATH];
    foreach ($allowed_paths as $allowed) {
        if (strpos($path, realpath($allowed)) === 0) {
            return true;
        }
    }
    return false;
}

// 目录删除方法
private function delete_directory($dir) {
    if (!file_exists($dir)) return true;

    $items = scandir($dir);
    foreach ($items as $item) {
        if ($item == '.' || $item == '..') continue;
        
        $path = $dir . DIRECTORY_SEPARATOR . $item;
        is_dir($path) ? $this->delete_directory($path) : unlink($path);
    }

    return rmdir($dir);
}

// 目录复制方法
private function copy_directory($src, $dst) {
    if (!file_exists($dst)) {
        mkdir($dst, 0755, true);
    }

    $items = scandir($src);
    foreach ($items as $item) {
        if ($item == '.' || $item == '..') continue;
        
        $src_path = $src . DIRECTORY_SEPARATOR . $item;
        $dst_path = $dst . DIRECTORY_SEPARATOR . $item;
        
        is_dir($src_path) ? $this->copy_directory($src_path, $dst_path) : copy($src_path, $dst_path);
    }
}

// 插件激活方法
public static function activate() {
    if (!current_user_can('activate_plugins')) return;
    
    $upload_dir = wp_upload_dir();
    $cache_dir = $upload_dir['basedir'] . '/wp-file-manager-cache';
    
    if (!file_exists($cache_dir)) {
        wp_mkdir_p($cache_dir);
    }
}

// 插件停用方法
public static function deactivate() {
    if (!current_user_can('activate_plugins')) return;
}

// 插件卸载方法
public static function uninstall() {
    if (!current_user_can('delete_plugins')) return;
    
    $upload_dir = wp_upload_dir();
    $cache_dir = $upload_dir['basedir'] . '/wp-file-manager-cache';
    
    if (file_exists($cache_dir)) {
        $fm = new self();
        $fm->delete_directory($cache_dir);
    }
}

    

