Add media support: image uploads, gallery rendering, video oEmbed

- New class-media.php: handles image upload via REST, stores in post meta, renders Twitter-style gallery
- Added POST /beep/v1/media/{id} and DELETE routes for media management
- Updated composer: media upload button, preview area
- Updated beep_render_post: displays image gallery + video embed
- Supports YouTube, Vimeo, Twitter/X video URLs via oEmbed
- Max 4 images per beep, 10MB each (JPEG/PNG/GIF/WebP)
This commit is contained in:
Krystie
2026-05-18 04:01:41 -07:00
parent 0807eb74d9
commit 6c9b06ccb2
4 changed files with 305 additions and 0 deletions
+220
View File
@@ -0,0 +1,220 @@
<?php
if (!defined('ABSPATH')) {
exit;
}
/**
* Beep_Media — handles image uploads, video URLs, and media rendering.
* Uses WordPress built-in media library (no extra server requirements).
*/
class Beep_Media {
const META_KEY_IMAGES = 'beep_images';
const META_KEY_VIDEO = 'beep_video_url';
private static $image_types = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
private static $max_images = 4;
private static $max_file_size = 10 * 1024 * 1024; // 10 MB
public static function init() {}
/**
* Handle a media upload from the REST API.
*
* @param int $post_id The beep post ID to attach media to.
* @param array $file $_FILES entry.
* @return array|WP_Error
*/
public static function handle_upload($post_id, $file) {
if (!is_array($file) || $file['error'] !== UPLOAD_ERR_OK) {
return new WP_Error('upload_error', 'Upload failed.');
}
if ($file['size'] > self::$max_file_size) {
return new WP_Error('file_too_large', 'File exceeds 10 MB limit.');
}
$mime = mime_content_type($file['tmp_name']);
if (!in_array($mime, self::$image_types, true)) {
return new WP_Error('invalid_type', 'Only JPEG, PNG, GIF, and WebP are allowed.');
}
$existing = self::get_images($post_id);
if (count($existing) >= self::$max_images) {
return new WP_Error('too_many', 'Maximum ' . self::$max_images . ' images per beep.');
}
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
$file_return = wp_handle_sideload($file, [
'test_form' => false,
'mimes' => [
'jpg|jpeg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
'webp' => 'image/webp',
],
]);
if (isset($file_return['error'])) {
return new WP_Error('sideload_error', $file_return['error']);
}
$attachment = [
'post_mime_type' => $file_return['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($file_return['file'])),
'post_content' => '',
'post_status' => 'inherit',
'guid' => $file_return['url'],
];
$attachment_id = wp_insert_attachment($attachment, $file_return['file'], $post_id);
if (is_wp_error($attachment_id)) {
return $attachment_id;
}
$attach_data = wp_generate_attachment_metadata($attachment_id, $file_return['file']);
wp_update_attachment_metadata($attachment_id, $attach_data);
add_post_meta($post_id, self::META_KEY_IMAGES, $attachment_id);
return [
'id' => $attachment_id,
'url' => wp_get_attachment_url($attachment_id),
'thumb' => self::get_image_url($attachment_id, 'thumbnail'),
'sizes' => self::get_image_sizes($attachment_id),
];
}
/**
* Remove a single image from a beep post.
*/
public static function remove_image($post_id, $attachment_id) {
$ids = self::get_images($post_id);
$ids = array_values(array_filter($ids, fn($id) => (int) $id !== (int) $attachment_id));
delete_post_meta($post_id, self::META_KEY_IMAGES);
foreach ($ids as $id) {
add_post_meta($post_id, self::META_KEY_IMAGES, $id);
}
wp_delete_attachment($attachment_id, true);
}
/**
* Get image attachment IDs for a beep post.
*/
public static function get_images($post_id) {
$ids = get_post_meta($post_id, self::META_KEY_IMAGES, false);
return array_filter(array_map('intval', (array) $ids));
}
/**
* Get image srcset-ready sizes for an attachment.
*/
public static function get_image_sizes($attachment_id) {
$sizes = [];
foreach (['thumbnail', 'medium', 'large', 'full'] as $size) {
$img = wp_get_attachment_image_src($attachment_id, $size);
if ($img) {
$sizes[$size] = ['url' => $img[0], 'width' => $img[1], 'height' => $img[2]];
}
}
return $sizes;
}
/**
* Get image URL at a given size.
*/
public static function get_image_url($attachment_id, $size = 'medium') {
$img = wp_get_attachment_image_src($attachment_id, $size);
return $img ? $img[0] : '';
}
/**
* Set a video URL for a beep post.
*/
public static function set_video_url($post_id, $url) {
$url = esc_url_raw($url);
if (!$url) return new WP_Error('invalid_url', 'Invalid video URL.');
update_post_meta($post_id, self::META_KEY_VIDEO, $url);
return true;
}
/**
* Get the video URL for a beep post.
*/
public static function get_video_url($post_id) {
return esc_url_raw(get_post_meta($post_id, self::META_KEY_VIDEO, true));
}
/**
* Detect if a URL is a supported video oEmbed.
*/
public static function detect_video_url($url) {
$patterns = [
'youtube' => '#https?://(?:www\.)?youtube\.com/watch\?v=[^\s&#]+#',
'yt_s' => '#https?://youtu\.be/[^\s&#]+#',
'vimeo' => '#https?://vimeo\.com/\d+#',
'twitter' => '#https?://(?:twitter\.com|x\.com)/[^\s#/]+/status/\d+#',
];
foreach ($patterns as $name => $pattern) {
if (preg_match($pattern, $url)) return $name;
}
return false;
}
/**
* Render the Twitter-style image gallery.
*
* @param int[] $image_ids
* @return string HTML
*/
public static function render_image_gallery($image_ids) {
if (empty($image_ids)) return '';
$count = count($image_ids);
$grid_class = 'beep-gallery';
if ($count === 1) $grid_class .= ' beep-gallery-1';
elseif ($count === 2) $grid_class .= ' beep-gallery-2';
elseif ($count === 3) $grid_class .= ' beep-gallery-3';
else $grid_class .= ' beep-gallery-4';
$html = '<div class="' . esc_attr($grid_class) . '">';
foreach ($image_ids as $i => $id) {
$full = self::get_image_url($id, 'large');
$thumb = self::get_image_url($id, 'medium');
if (!$thumb) $thumb = $full;
if (!$full) continue;
$alt = get_post_meta($id, '_wp_attachment_image_alt', true);
$html .= '<div class="beep-gallery-item">';
$html .= '<a href="' . esc_url($full) . '" target="_blank" rel="noopener" data-beep-lightbox>';
$html .= '<img src="' . esc_url($thumb) . '" alt="' . esc_attr($alt ?: "Beep image") . '" loading="lazy">';
$html .= '</a>';
$html .= '</div>';
}
$html .= '</div>';
return $html;
}
/**
* Render video embed via oEmbed.
*/
public static function render_video_embed($url) {
if (!$url) return '';
$embed = wp_oembed_get($url, ['width' => 560, 'height' => 315]);
if ($embed) {
return '<div class="beep-video-embed">' . $embed . '</div>';
}
return '';
}
/**
* Delete all media for a beep post (when beep is deleted).
*/
public static function delete_media($post_id) {
foreach (self::get_images($post_id) as $id) {
wp_delete_attachment($id, true);
}
delete_post_meta($post_id, self::META_KEY_IMAGES);
delete_post_meta($post_id, self::META_KEY_VIDEO);
}
}