| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\Framework\Support; |
| 4 |
|
| 5 |
use RuntimeException; |
| 6 |
use InvalidArgumentException; |
| 7 |
|
| 8 |
class MediaUploader |
| 9 |
{ |
| 10 |
/** |
| 11 |
* upload a local file array (e.g. from $_FILES) or a remote file URL. |
| 12 |
* |
| 13 |
* @param array|string $resource File array from $_FILES or remote file URL |
| 14 |
* @param int|null $postId Optional post ID to attach the media to |
| 15 |
* @param string|null $filename Optional file name for remote URLs |
| 16 |
* |
| 17 |
* @return array |
| 18 |
* @throws InvalidArgumentException |
| 19 |
* @throws RuntimeException |
| 20 |
* @see https://developer.wordpress.org/reference/functions/media_handle_sideload/ |
| 21 |
*/ |
| 22 |
public static function upload($resource, $postId = 0, $filename = null) |
| 23 |
{ |
| 24 |
[$fileArray, $tmp] = static::prepareMedia($resource, $filename); |
| 25 |
|
| 26 |
$attachmentId = media_handle_sideload($fileArray, $postId); |
| 27 |
|
| 28 |
if ($tmp && file_exists($tmp)) { |
| 29 |
@unlink($tmp); |
| 30 |
} |
| 31 |
|
| 32 |
if (is_wp_error($attachmentId)) { |
| 33 |
throw new RuntimeException( |
| 34 |
'Failed to sideload media: ' . $attachmentId->get_error_message() |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
return [ |
| 39 |
'success' => true, |
| 40 |
'id' => $attachmentId, |
| 41 |
'url' => wp_get_attachment_url($attachmentId), |
| 42 |
]; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Prepare the file array to upload. |
| 47 |
* |
| 48 |
* @param string|array $resource url or $_FILES |
| 49 |
* @param string|null $filename |
| 50 |
* @return array |
| 51 |
*/ |
| 52 |
protected static function prepareMedia($resource, $filename = null) |
| 53 |
{ |
| 54 |
static::includeMediaFunctions(); |
| 55 |
|
| 56 |
if (is_array($resource)) { |
| 57 |
if (empty($resource['tmp_name'])) { |
| 58 |
throw new InvalidArgumentException('Invalid file array: tmp_name is required.'); |
| 59 |
} |
| 60 |
|
| 61 |
return [[ |
| 62 |
'name' => $resource['name'] ?? 'file', |
| 63 |
'tmp_name' => $resource['tmp_name'], |
| 64 |
'type' => $resource['type'] ?? null, |
| 65 |
'error' => $resource['error'] ?? 0, |
| 66 |
'size' => $resource['size_in_bytes'] ?? $resource['size'] ?? null, |
| 67 |
], null]; |
| 68 |
} |
| 69 |
|
| 70 |
if (is_string($resource) && filter_var($resource, FILTER_VALIDATE_URL)) { |
| 71 |
$tmp = download_url($resource); |
| 72 |
|
| 73 |
if (is_wp_error($tmp)) { |
| 74 |
throw new RuntimeException( |
| 75 |
'Failed to download remote file: ' . $tmp->get_error_message() |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
return [[ |
| 80 |
'tmp_name' => $tmp, |
| 81 |
'name' => $filename ?: basename( |
| 82 |
parse_url($resource, PHP_URL_PATH) |
| 83 |
), |
| 84 |
], $tmp]; |
| 85 |
} |
| 86 |
|
| 87 |
throw new InvalidArgumentException('Invalid file array or URL provided.'); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Ensure required WordPress media functions are loaded. |
| 92 |
*/ |
| 93 |
protected static function includeMediaFunctions() |
| 94 |
{ |
| 95 |
if (!function_exists('media_handle_sideload')) { |
| 96 |
require_once ABSPATH . 'wp-admin/includes/image.php'; |
| 97 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 98 |
require_once ABSPATH . 'wp-admin/includes/media.php'; |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|