| 1 |
<?php |
| 2 |
|
| 3 |
namespace ReviewX\Utilities; |
| 4 |
|
| 5 |
\defined('ABSPATH') || exit; |
| 6 |
class UploadMimeSupport |
| 7 |
{ |
| 8 |
private static int $activeScopes = 0; |
| 9 |
public static function bootstrapGlobalHooks() : void |
| 10 |
{ |
| 11 |
\add_filter('upload_mimes', [self::class, 'filterUploadMimes']); |
| 12 |
\add_filter('wp_check_filetype_and_ext', [self::class, 'fixSvgFiletype'], 10, 4); |
| 13 |
\add_filter('wp_prepare_attachment_for_js', [self::class, 'prepareAttachmentForJs'], 10, 3); |
| 14 |
} |
| 15 |
public static function withAllowedUploads(callable $callback) |
| 16 |
{ |
| 17 |
self::enable(); |
| 18 |
try { |
| 19 |
return $callback(); |
| 20 |
} finally { |
| 21 |
self::disable(); |
| 22 |
} |
| 23 |
} |
| 24 |
public static function getAllowedUploadMimes() : array |
| 25 |
{ |
| 26 |
return ['jpg|jpeg|jpe' => 'image/jpeg', 'png' => 'image/png', 'gif' => 'image/gif', 'webp' => 'image/webp', 'svg' => 'image/svg+xml', 'svgz' => 'image/svg+xml', 'mp4' => 'video/mp4', 'webm' => 'video/webm', 'ogg' => 'video/ogg']; |
| 27 |
} |
| 28 |
public static function getAllowedAttachmentMimeTypesForValidation() : array |
| 29 |
{ |
| 30 |
return \array_values(\array_unique(\array_values(self::getAllowedUploadMimes()))); |
| 31 |
} |
| 32 |
public static function isAllowedAttachmentFile(string $fileName = '', string $mimeType = '') : bool |
| 33 |
{ |
| 34 |
$mimeType = \strtolower(\trim($mimeType)); |
| 35 |
if ($mimeType !== '' && \in_array($mimeType, self::getAllowedAttachmentMimeTypesForValidation(), \true)) { |
| 36 |
return \true; |
| 37 |
} |
| 38 |
if ($fileName === '') { |
| 39 |
return \false; |
| 40 |
} |
| 41 |
$fileType = \wp_check_filetype($fileName, self::getAllowedUploadMimes()); |
| 42 |
return !empty($fileType['type']); |
| 43 |
} |
| 44 |
public static function getWpHandleUploadOverrides(array $overrides = []) : array |
| 45 |
{ |
| 46 |
return \array_merge(['test_form' => \false, 'mimes' => self::getAllowedUploadMimes()], $overrides); |
| 47 |
} |
| 48 |
public static function generateAttachmentMetadata(int $attachmentId, string $filePath, ?string $mimeType = null) : array |
| 49 |
{ |
| 50 |
$mimeType = \is_string($mimeType) ? $mimeType : ''; |
| 51 |
if (self::isSvgMimeType($mimeType) || self::hasSvgExtension($filePath)) { |
| 52 |
return []; |
| 53 |
} |
| 54 |
$metadata = \wp_generate_attachment_metadata($attachmentId, $filePath); |
| 55 |
return \is_array($metadata) ? $metadata : []; |
| 56 |
} |
| 57 |
public static function filterUploadMimes(array $mimes) : array |
| 58 |
{ |
| 59 |
return \array_merge($mimes, self::getAllowedUploadMimes()); |
| 60 |
} |
| 61 |
public static function fixSvgFiletype($data, $file, $filename, $mimes) |
| 62 |
{ |
| 63 |
unset($file, $mimes); |
| 64 |
if (!\is_string($filename)) { |
| 65 |
return $data; |
| 66 |
} |
| 67 |
$extension = \strtolower((string) \pathinfo($filename, \PATHINFO_EXTENSION)); |
| 68 |
if ($extension === 'svg' || $extension === 'svgz') { |
| 69 |
$data['ext'] = $extension; |
| 70 |
$data['type'] = 'image/svg+xml'; |
| 71 |
$data['proper_filename'] = $filename; |
| 72 |
} |
| 73 |
if ($extension === 'webp') { |
| 74 |
$data['ext'] = 'webp'; |
| 75 |
$data['type'] = 'image/webp'; |
| 76 |
$data['proper_filename'] = $filename; |
| 77 |
} |
| 78 |
return $data; |
| 79 |
} |
| 80 |
public static function prepareAttachmentForJs(array $response, $attachment, $meta) : array |
| 81 |
{ |
| 82 |
unset($meta); |
| 83 |
if (!isset($attachment->ID)) { |
| 84 |
return $response; |
| 85 |
} |
| 86 |
$mimeType = (string) \get_post_mime_type($attachment->ID); |
| 87 |
if (!self::isSvgMimeType($mimeType)) { |
| 88 |
return $response; |
| 89 |
} |
| 90 |
$url = \wp_get_attachment_url($attachment->ID); |
| 91 |
if (!$url) { |
| 92 |
return $response; |
| 93 |
} |
| 94 |
$response['url'] = $url; |
| 95 |
$response['icon'] = $url; |
| 96 |
$response['type'] = 'image'; |
| 97 |
$response['subtype'] = 'svg+xml'; |
| 98 |
$response['mime'] = 'image/svg+xml'; |
| 99 |
$response['filename'] = $response['filename'] ?? \basename((string) \get_attached_file($attachment->ID)); |
| 100 |
$response['sizes'] = $response['sizes'] ?? []; |
| 101 |
$response['sizes']['full'] = ['url' => $url, 'width' => $response['width'] ?? 0, 'height' => $response['height'] ?? 0, 'orientation' => 'landscape']; |
| 102 |
return $response; |
| 103 |
} |
| 104 |
private static function enable() : void |
| 105 |
{ |
| 106 |
if (self::$activeScopes === 0) { |
| 107 |
\add_filter('upload_mimes', [self::class, 'filterUploadMimes']); |
| 108 |
\add_filter('wp_check_filetype_and_ext', [self::class, 'fixSvgFiletype'], 10, 4); |
| 109 |
} |
| 110 |
self::$activeScopes++; |
| 111 |
} |
| 112 |
private static function disable() : void |
| 113 |
{ |
| 114 |
if (self::$activeScopes <= 0) { |
| 115 |
self::$activeScopes = 0; |
| 116 |
return; |
| 117 |
} |
| 118 |
self::$activeScopes--; |
| 119 |
if (self::$activeScopes === 0) { |
| 120 |
\remove_filter('upload_mimes', [self::class, 'filterUploadMimes']); |
| 121 |
\remove_filter('wp_check_filetype_and_ext', [self::class, 'fixSvgFiletype'], 10); |
| 122 |
} |
| 123 |
} |
| 124 |
private static function isSvgMimeType(string $mimeType) : bool |
| 125 |
{ |
| 126 |
return \strtolower($mimeType) === 'image/svg+xml'; |
| 127 |
} |
| 128 |
private static function hasSvgExtension(string $filePath) : bool |
| 129 |
{ |
| 130 |
$extension = \strtolower((string) \pathinfo($filePath, \PATHINFO_EXTENSION)); |
| 131 |
return $extension === 'svg' || $extension === 'svgz'; |
| 132 |
} |
| 133 |
} |
| 134 |
|