Files
advanced-mcp-plugin/includes/Tools/MediaTools.php
T

86 lines
4.2 KiB
PHP

<?php
namespace AskTamMCP\Tools;
defined('ABSPATH') || exit;
class MediaTools {
public static function listMedia(array $args): array {
$qArgs = ['post_type' => 'attachment', 'post_status' => 'inherit', 'posts_per_page' => (int) ($args['per_page'] ?? 20), 'paged' => (int) ($args['page'] ?? 1)];
if (!empty($args['mime_type'])) $qArgs['post_mime_type'] = $args['mime_type'];
if (!empty($args['search'])) $qArgs['s'] = $args['search'];
$query = new \WP_Query($qArgs);
$items = [];
foreach ($query->posts as $p) {
$items[] = ['id' => $p->ID, 'title' => $p->post_title, 'url' => wp_get_attachment_url($p->ID), 'mime_type' => $p->post_mime_type, 'alt' => get_post_meta($p->ID, '_wp_attachment_image_alt', true), 'date' => $p->post_date];
}
return ['media' => $items, 'total' => $query->found_posts];
}
public static function getMedia(array $args): array {
$id = (int) $args['media_id'];
$post = get_post($id);
if (!$post || $post->post_type !== 'attachment') throw new \RuntimeException("Media not found: {$id}");
$meta = wp_get_attachment_metadata($id);
return ['id' => $id, 'title' => $post->post_title, 'url' => wp_get_attachment_url($id), 'mime_type' => $post->post_mime_type, 'alt' => get_post_meta($id, '_wp_attachment_image_alt', true), 'caption' => $post->post_excerpt, 'description' => $post->post_content, 'metadata' => $meta];
}
public static function uploadMedia(array $args): array {
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
$sourceUrl = $args['source_url'];
$tmpFile = download_url($sourceUrl);
if (is_wp_error($tmpFile)) throw new \RuntimeException($tmpFile->get_error_message());
$fileName = basename(parse_url($sourceUrl, PHP_URL_PATH));
$fileArray = ['name' => $fileName, 'tmp_name' => $tmpFile];
$postId = $args['post_id'] ?? 0;
$mediaId = media_handle_sideload($fileArray, (int) $postId);
@unlink($tmpFile);
if (is_wp_error($mediaId)) throw new \RuntimeException($mediaId->get_error_message());
if (!empty($args['title'])) wp_update_post(['ID' => $mediaId, 'post_title' => $args['title']]);
if (!empty($args['alt_text'])) update_post_meta($mediaId, '_wp_attachment_image_alt', $args['alt_text']);
if (!empty($args['caption'])) wp_update_post(['ID' => $mediaId, 'post_excerpt' => $args['caption']]);
return ['id' => $mediaId, 'url' => wp_get_attachment_url($mediaId)];
}
public static function updateMedia(array $args): array {
$id = (int) $args['media_id'];
$data = ['ID' => $id];
if (isset($args['title'])) $data['post_title'] = $args['title'];
if (isset($args['caption'])) $data['post_excerpt'] = $args['caption'];
if (isset($args['description'])) $data['post_content'] = $args['description'];
wp_update_post($data);
if (isset($args['alt_text'])) update_post_meta($id, '_wp_attachment_image_alt', $args['alt_text']);
return ['id' => $id, 'updated' => true];
}
public static function deleteMedia(array $args): array {
$result = wp_delete_attachment((int) $args['media_id'], (bool) ($args['force'] ?? true));
if (!$result) throw new \RuntimeException("Failed to delete media: {$args['media_id']}");
return ['id' => (int) $args['media_id'], 'deleted' => true];
}
public static function countMedia(array $args): array {
global $wpdb;
$rows = $wpdb->get_results("SELECT post_mime_type, COUNT(*) as cnt FROM {$wpdb->posts} WHERE post_type='attachment' AND post_status='inherit' GROUP BY post_mime_type", ARRAY_A);
$counts = ['total'=>0,'image'=>0,'video'=>0,'audio'=>0,'document'=>0,'other'=>0];
foreach ($rows as $r) {
$counts['total'] += (int)$r['cnt'];
$type = explode('/',$r['post_mime_type'])[0];
if (isset($counts[$type])) $counts[$type] += (int)$r['cnt'];
else $counts['other'] += (int)$r['cnt'];
}
return $counts;
}
}