PHP Classes

File: public/quick-media.php

Recommend this page to a friend!
  Packages of Adrian M   QuickWP   public/quick-media.php   Download  
File: public/quick-media.php
Role: Application script
Content type: text/plain
Description: Application script
Class: QuickWP
Manage WordPress sites using the REST API
Author: By
Last change: feat(i18n): add zero-dependency UI localization (EN/RO) and language switcher
v2.3: add User/Comment/Health services, ACF support, Menus/Comments/Media UI, CSRF session fix, PHPUnit 11 & PHPStan 2
v2.2: refactor services, add retry/pagination, shared UI layout, PHP 8.3+, PHPStan L8, and CI
Date: 12 hours ago
Size: 10,498 bytes
 

 

Contents

Class file image Download
<?php /** * QuickWP v2 - Upload Media Controller */ require_once __DIR__ . '/partials/bootstrap.php'; use QuickWP\QuickWP; use QuickWP\Http\CsrfProtection; $message = null; $error = null; // Handle upload form submission if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') !== 'delete_media') { if (!CsrfProtection::validate()) { $error = $t->get('common.csrf_error'); } else { $title = trim($_POST['title'] ?? ''); $altText = trim($_POST['alt_text'] ?? ''); $caption = trim($_POST['caption'] ?? ''); $description = trim($_POST['description'] ?? ''); $attachPost = (int)($_POST['attach_post_id'] ?? 0); $featuredFor = (int)($_POST['featured_for_post_id'] ?? 0); if (empty($_FILES['media_file']['name'])) { $error = 'Please select a file to upload.'; } else { $data = []; if ($title !== '') $data['title'] = $title; if ($altText !== '') $data['alt_text'] = $altText; if ($caption !== '') $data['caption'] = $caption; if ($description !== '') $data['description'] = $description; if ($attachPost > 0) $data['post'] = $attachPost; $result = $qwp->media()->upload( $_FILES['media_file'], $data ); if (QuickWP::isSuccess($result)) { $mediaId = QuickWP::getId($result); $mediaUrl = $result['json']['source_url'] ?? ($result['json']['guid']['rendered'] ?? null); $messageParts = []; if ($mediaUrl) { $messageParts[] = 'Media uploaded: <a href="' . htmlspecialchars($mediaUrl, ENT_QUOTES, 'UTF-8') . '" target="_blank">View File</a>'; } if ($mediaId) { $messageParts[] = 'ID: ' . $mediaId; } // Set as featured image if ($featuredFor > 0 && $mediaId) { $featuredResult = $qwp->media()->setFeaturedImage( $featuredFor, $mediaId ); if (QuickWP::isSuccess($featuredResult)) { $messageParts[] = 'Featured image set for Post ID ' . $featuredFor; } else { $messageParts[] = 'Could not set featured image: ' . QuickWP::getError($featuredResult); } } $message = implode(' | ', $messageParts); } else { $error = 'Error: ' . QuickWP::getError($result); } } } } $mediaSearch = trim($_GET['media_search'] ?? ''); $mediaPage = max(1, (int) ($_GET['media_page'] ?? 1)); // Handle delete media action if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'delete_media') { if (!CsrfProtection::validate()) { $error = $t->get('common.csrf_error'); } else { $delId = (int) ($_POST['media_id'] ?? 0); if ($delId > 0) { $delRes = $qwp->media()->delete($delId, true); if (QuickWP::isSuccess($delRes)) { $message = 'Media #' . $delId . ' deleted permanently.'; } else { $error = 'Delete failed: ' . QuickWP::getError($delRes); } } } } // Fetch recent media library items $mediaItems = []; $mediaParams = ['per_page' => 12, 'page' => $mediaPage, '_fields' => 'id,title,source_url,mime_type,media_details']; if ($mediaSearch !== '') { $mediaParams['search'] = $mediaSearch; } $mediaListResult = $qwp->media()->list($mediaParams); if (QuickWP::isSuccess($mediaListResult) && is_array($mediaListResult['json'])) { $mediaItems = $mediaListResult['json']; } $pageTitle = $t->get('media.heading'); $heading = $t->get('media.heading'); $activeNav = 'media'; $containerClass = 'wide'; $extraHead = '<style>textarea { min-height: 80px; } .media-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 14px; margin-top: 14px; } .media-card { border: 1px solid var(--border); border-radius: 6px; padding: 10px; background: #fafafa; font-size: 0.85rem; } .media-thumb { width: 100%; height: 120px; object-fit: cover; border-radius: 4px; background: #eee; margin-bottom: 8px; }</style>'; require __DIR__ . '/partials/header.php'; ?> <?php if ($message): ?> <div class="message success"><?= $message ?></div> <?php endif; ?> <?php if ($error): ?> <div class="message error"><?= htmlspecialchars($error) ?></div> <?php endif; ?> <form method="post" enctype="multipart/form-data"> <?= CsrfProtection::field() ?> <?php if ($siteKey !== ''): ?> <input type="hidden" name="site" value="<?= htmlspecialchars($siteKey) ?>"> <?php endif; ?> <div class="file-upload"> <label for="media_file"><?= $t->e('media.select_file') ?></label> <input type="file" id="media_file" name="media_file" required> </div> <div class="row"> <div> <label for="title"><?= $t->e('common.title') ?></label> <input type="text" id="title" name="title" value="<?= htmlspecialchars($_POST['title'] ?? '') ?>"> </div> <div> <label for="alt_text"><?= $t->e('media.alt_text') ?></label> <input type="text" id="alt_text" name="alt_text" value="<?= htmlspecialchars($_POST['alt_text'] ?? '') ?>"> </div> </div> <label for="caption"><?= $t->e('media.caption') ?></label> <textarea id="caption" name="caption"><?= htmlspecialchars($_POST['caption'] ?? '') ?></textarea> <label for="description"><?= $t->e('media.description') ?></label> <textarea id="description" name="description"><?= htmlspecialchars($_POST['description'] ?? '') ?></textarea> <div class="row"> <div> <label for="attach_post_id"><?= $t->e('media.attach_post') ?></label> <input type="number" id="attach_post_id" name="attach_post_id" min="0" value="<?= htmlspecialchars($_POST['attach_post_id'] ?? '0') ?>"> <p class="hint"><?= $t->e('media.attach_hint') ?></p> </div> <div> <label for="featured_for_post_id"><?= $t->e('media.featured_for') ?></label> <input type="number" id="featured_for_post_id" name="featured_for_post_id" min="0" value="<?= htmlspecialchars($_POST['featured_for_post_id'] ?? '0') ?>"> <p class="hint"><?= $t->e('media.featured_hint') ?></p> </div> </div> <button type="submit"><?= $t->e('media.upload_btn') ?></button> </form> <div style="margin-top: 28px; padding-top: 20px; border-top: 1px solid var(--border);"> <h2 style="margin: 0 0 12px; font-size: 1.15rem;"><?= $t->e('media.library_heading') ?></h2> <form method="get" class="filters"> <?php if ($siteKey !== ''): ?> <input type="hidden" name="site" value="<?= htmlspecialchars($siteKey, ENT_QUOTES, 'UTF-8') ?>"> <?php endif; ?> <div class="filter-group"> <label for="media_search"><?= $t->e('media.search_label') ?></label> <input type="search" id="media_search" name="media_search" value="<?= htmlspecialchars($mediaSearch, ENT_QUOTES, 'UTF-8') ?>" placeholder="<?= $t->e('media.search_placeholder') ?>"> </div> <button type="submit"><?= $t->e('common.search') ?></button> </form> <?php if (empty($mediaItems)): ?> <p class="hint"><?= $t->e('editor.no_media') ?></p> <?php else: ?> <div class="media-grid"> <?php foreach ($mediaItems as $m): ?> <?php $mid = (int) ($m['id'] ?? 0); $mTitle = is_array($m['title'] ?? null) ? ($m['title']['rendered'] ?? '#' . $mid) : ($m['title'] ?? '#' . $mid); $mUrl = (string) ($m['source_url'] ?? ''); $mThumb = (string) ($m['media_details']['sizes']['thumbnail']['source_url'] ?? $mUrl); $isImage = strpos((string) ($m['mime_type'] ?? ''), 'image/') === 0; ?> <div class="media-card"> <?php if ($isImage && $mThumb !== ''): ?> <img class="media-thumb" src="<?= htmlspecialchars($mThumb, ENT_QUOTES, 'UTF-8') ?>" alt="<?= htmlspecialchars((string) $mTitle, ENT_QUOTES, 'UTF-8') ?>"> <?php else: ?> <div class="media-thumb" style="display:flex;align-items:center;justify-content:center;color:var(--text-muted);">FILE</div> <?php endif; ?> <div><strong>ID: <?= $mid ?></strong></div> <div style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis;" title="<?= htmlspecialchars((string) $mTitle, ENT_QUOTES, 'UTF-8') ?>"> <?= htmlspecialchars((string) $mTitle, ENT_QUOTES, 'UTF-8') ?> </div> <div style="margin-top: 8px; display: flex; gap: 6px; align-items: center;"> <?php if ($mUrl !== ''): ?> <a href="<?= htmlspecialchars($mUrl, ENT_QUOTES, 'UTF-8') ?>" target="_blank" style="font-size: 0.8rem;"><?= $t->e('media.view') ?></a> <?php endif; ?> <form method="post" style="display: inline; margin-left: auto;" onsubmit="return confirm('Delete media #<?= $mid ?> permanently?');"> <?= CsrfProtection::field() ?> <input type="hidden" name="action" value="delete_media"> <input type="hidden" name="media_id" value="<?= $mid ?>"> <?php if ($siteKey !== ''): ?> <input type="hidden" name="site" value="<?= htmlspecialchars($siteKey, ENT_QUOTES, 'UTF-8') ?>"> <?php endif; ?> <button type="submit" style="margin: 0; padding: 3px 8px; background: var(--error); font-size: 0.75rem;"><?= $t->e('common.delete') ?></button> </form> </div> </div> <?php endforeach; ?> </div> <?php endif; ?> </div> <?php require __DIR__ . '/partials/footer.php'; ?>