: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Browsers may use unexpected mime types, and they differ from browser to browser.
* This function computes a flexible array of mime types from the mime type structured provided by the server.
* Converts { jpg|jpeg|jpe: "image/jpeg" } into [ "image/jpeg", "image/jpg", "image/jpeg", "image/jpe" ]
* The computation of this array instead of directly using the object,
* solves the problem in chrome where mp3 files have audio/mp3 as mime type instead of audio/mpeg.
* https://bugs.chromium.org/p/chromium/issues/detail?id=227004
* @param {?Object} wpMimeTypesObject Mime type object received from the server.
* Extensions are keys separated by '|' and values are mime types associated with an extension.
* @return {?Array} An array of mime types or the parameter passed if it was "falsy".
function getMimeTypesArray(wpMimeTypesObject) {
if (!wpMimeTypesObject) {
return wpMimeTypesObject;
return Object.entries(wpMimeTypesObject).map(([extensionsString, mime]) => {
const [type] = mime.split('/');
const extensions = extensionsString.split('|');
return [mime, ...extensions.map(extension => `${type}/${extension}`)];
* Media Upload is used by audio, image, gallery, video, and file blocks to
* handle uploading a media file when a file upload button is activated.
* TODO: future enhancement to add an upload indicator.
* @param {Object} $0 Parameters object passed to the function.
* @param {?Array} $0.allowedTypes Array with the types of media that can be uploaded, if unset all types are allowed.
* @param {?Object} $0.additionalData Additional data to include in the request.
* @param {Array} $0.filesList List of files.
* @param {?number} $0.maxUploadFileSize Maximum upload size in bytes allowed for the site.
* @param {Function} $0.onError Function called when an error happens.
* @param {Function} $0.onFileChange Function called each time a file or a temporary representation of the file is available.
* @param {?Object} $0.wpAllowedMimeTypes List of allowed mime types and file extensions.
async function uploadMedia({
wpAllowedMimeTypes = null
// Cast filesList to array.
const files = [...filesList];
const setAndUpdateFiles = (idx, value) => {
(0,external_wp_blob_namespaceObject.revokeBlobURL)(filesSet[idx]?.url);
onFileChange(filesSet.filter(Boolean));
// Allowed type specified by consumer.
const isAllowedType = fileType => {
return allowedTypes.some(allowedType => {
// If a complete mimetype is specified verify if it matches exactly the mime type of the file.
if (allowedType.includes('/')) {
return allowedType === fileType;
// Otherwise a general mime type is used and we should verify if the file mimetype starts with it.
return fileType.startsWith(`${allowedType}/`);
// Allowed types for the current WP_User.
const allowedMimeTypesForUser = getMimeTypesArray(wpAllowedMimeTypes);
const isAllowedMimeTypeForUser = fileType => {
return allowedMimeTypesForUser.includes(fileType);
for (const mediaFile of files) {
// Verify if user is allowed to upload this mime type.
// Defer to the server when type not detected.
if (allowedMimeTypesForUser && mediaFile.type && !isAllowedMimeTypeForUser(mediaFile.type)) {
code: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',
message: (0,external_wp_i18n_namespaceObject.sprintf)(
// translators: %s: file name.
(0,external_wp_i18n_namespaceObject.__)('%s: Sorry, you are not allowed to upload this file type.'), mediaFile.name),
// Check if the block supports this mime type.
// Defer to the server when type not detected.
if (mediaFile.type && !isAllowedType(mediaFile.type)) {
code: 'MIME_TYPE_NOT_SUPPORTED',
message: (0,external_wp_i18n_namespaceObject.sprintf)(
// translators: %s: file name.
(0,external_wp_i18n_namespaceObject.__)('%s: Sorry, this file type is not supported here.'), mediaFile.name),
// Verify if file is greater than the maximum file upload size allowed for the site.
if (maxUploadFileSize && mediaFile.size > maxUploadFileSize) {
code: 'SIZE_ABOVE_LIMIT',
message: (0,external_wp_i18n_namespaceObject.sprintf)(
// translators: %s: file name.
(0,external_wp_i18n_namespaceObject.__)('%s: This file exceeds the maximum upload size for this site.'), mediaFile.name),
// Don't allow empty files to be uploaded.
if (mediaFile.size <= 0) {
message: (0,external_wp_i18n_namespaceObject.sprintf)(
// translators: %s: file name.
(0,external_wp_i18n_namespaceObject.__)('%s: This file is empty.'), mediaFile.name),
validFiles.push(mediaFile);
// Set temporary URL to create placeholder media file, this is replaced
// with final file from media gallery when upload is `done` below.
url: (0,external_wp_blob_namespaceObject.createBlobURL)(mediaFile)
for (let idx = 0; idx < validFiles.length; ++idx) {
const mediaFile = validFiles[idx];
var _savedMedia$caption$r;
const savedMedia = await createMediaFromFile(mediaFile, additionalData);
// eslint-disable-next-line camelcase
alt: savedMedia.alt_text,
caption: (_savedMedia$caption$r = savedMedia.caption?.raw) !== null && _savedMedia$caption$r !== void 0 ? _savedMedia$caption$r : '',
title: savedMedia.title.raw,
url: savedMedia.source_url
setAndUpdateFiles(idx, mediaObject);
// Reset to empty on failure.
setAndUpdateFiles(idx, null);
message = (0,external_wp_i18n_namespaceObject.sprintf)(
// translators: %s: file name
(0,external_wp_i18n_namespaceObject.__)('Error while uploading file %s to the media library.'), mediaFile.name);
* @param {File} file Media File to Save.
* @param {?Object} additionalData Additional data to include in the request.
* @return {Promise} Media Object Promise.
function createMediaFromFile(file, additionalData) {
// Create upload payload.
const data = new window.FormData();
data.append('file', file, file.name || file.type.replace('/', '.'));
Object.entries(additionalData).forEach(([key, value]) => data.append(key, value));
return external_wp_apiFetch_default()({
;// CONCATENATED MODULE: ./node_modules/@wordpress/media-utils/build-module/utils/index.js
;// CONCATENATED MODULE: ./node_modules/@wordpress/media-utils/build-module/index.js
(window.wp = window.wp || {}).mediaUtils = __webpack_exports__;