kollapsminoriteten/wp-includes/js/dist/media-utils.js

860 lines
23 KiB
JavaScript
Raw Permalink Normal View History

2024-04-17 11:32:24 +02:00
/******/ (() => { // webpackBootstrap
2022-06-16 14:03:35 +02:00
/******/ "use strict";
/******/ // The require scope
/******/ var __webpack_require__ = {};
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat get default export */
2024-04-17 11:32:24 +02:00
/******/ (() => {
2022-06-16 14:03:35 +02:00
/******/ // getDefaultExport function for compatibility with non-harmony modules
2024-04-17 11:32:24 +02:00
/******/ __webpack_require__.n = (module) => {
2022-06-16 14:03:35 +02:00
/******/ var getter = module && module.__esModule ?
2024-04-17 11:32:24 +02:00
/******/ () => (module['default']) :
/******/ () => (module);
2022-06-16 14:03:35 +02:00
/******/ __webpack_require__.d(getter, { a: getter });
/******/ return getter;
/******/ };
2024-04-17 11:32:24 +02:00
/******/ })();
2022-06-16 14:03:35 +02:00
/******/
/******/ /* webpack/runtime/define property getters */
2024-04-17 11:32:24 +02:00
/******/ (() => {
2022-06-16 14:03:35 +02:00
/******/ // define getter functions for harmony exports
2024-04-17 11:32:24 +02:00
/******/ __webpack_require__.d = (exports, definition) => {
2022-06-16 14:03:35 +02:00
/******/ for(var key in definition) {
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
2019-11-15 22:59:44 +01:00
/******/ };
2024-04-17 11:32:24 +02:00
/******/ })();
2022-06-16 14:03:35 +02:00
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
2024-04-17 11:32:24 +02:00
/******/ (() => {
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ })();
2022-06-16 14:03:35 +02:00
/******/
/******/ /* webpack/runtime/make namespace object */
2024-04-17 11:32:24 +02:00
/******/ (() => {
2022-06-16 14:03:35 +02:00
/******/ // define __esModule on exports
2024-04-17 11:32:24 +02:00
/******/ __webpack_require__.r = (exports) => {
2022-06-16 14:03:35 +02:00
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
2024-04-17 11:32:24 +02:00
/******/ })();
2022-06-16 14:03:35 +02:00
/******/
2019-11-15 22:59:44 +01:00
/************************************************************************/
2022-06-16 14:03:35 +02:00
var __webpack_exports__ = {};
2020-09-15 14:29:22 +02:00
// ESM COMPAT FLAG
2019-11-15 22:59:44 +01:00
__webpack_require__.r(__webpack_exports__);
2020-09-15 14:29:22 +02:00
// EXPORTS
2022-06-16 14:03:35 +02:00
__webpack_require__.d(__webpack_exports__, {
2025-12-12 13:15:55 +01:00
MediaUpload: () => (/* reexport */ media_upload_default),
2025-04-25 12:30:07 +02:00
privateApis: () => (/* reexport */ privateApis),
2025-02-28 08:42:11 +01:00
transformAttachment: () => (/* reexport */ transformAttachment),
uploadMedia: () => (/* reexport */ uploadMedia),
validateFileSize: () => (/* reexport */ validateFileSize),
validateMimeType: () => (/* reexport */ validateMimeType),
validateMimeTypeForUser: () => (/* reexport */ validateMimeTypeForUser)
2022-06-16 14:03:35 +02:00
});
2025-04-25 12:30:07 +02:00
;// external ["wp","element"]
2024-04-17 11:32:24 +02:00
const external_wp_element_namespaceObject = window["wp"]["element"];
2025-04-25 12:30:07 +02:00
;// external ["wp","i18n"]
2024-04-17 11:32:24 +02:00
const external_wp_i18n_namespaceObject = window["wp"]["i18n"];
2025-04-25 12:30:07 +02:00
;// ./node_modules/@wordpress/media-utils/build-module/components/media-upload/index.js
2019-11-15 22:59:44 +01:00
2021-07-23 11:58:50 +02:00
const DEFAULT_EMPTY_GALLERY = [];
const getFeaturedImageMediaFrame = () => {
2025-12-12 13:15:55 +01:00
const { wp } = window;
2019-11-15 22:59:44 +01:00
return wp.media.view.MediaFrame.Select.extend({
/**
* Enables the Set Featured Image Button.
*
* @param {Object} toolbar toolbar for featured image state
* @return {void}
*/
2021-07-23 11:58:50 +02:00
featuredImageToolbar(toolbar) {
2019-11-15 22:59:44 +01:00
this.createSelectToolbar(toolbar, {
text: wp.media.view.l10n.setFeaturedImage,
state: this.options.state
});
},
2020-05-06 17:23:38 +02:00
/**
* Handle the edit state requirements of selected media item.
*
* @return {void}
*/
2021-07-23 11:58:50 +02:00
editState() {
2025-12-12 13:15:55 +01:00
const selection = this.state("featured-image").get("selection");
2021-07-23 11:58:50 +02:00
const view = new wp.media.view.EditImage({
2020-05-06 17:23:38 +02:00
model: selection.single(),
controller: this
2023-12-07 09:44:11 +01:00
}).render();
this.content.set(view);
2020-05-06 17:23:38 +02:00
view.loadEditor();
},
2019-11-15 22:59:44 +01:00
/**
* Create the default states.
*
* @return {void}
*/
createStates: function createStates() {
2025-12-12 13:15:55 +01:00
this.on(
"toolbar:create:featured-image",
this.featuredImageToolbar,
this
);
this.on("content:render:edit-image", this.editState, this);
this.states.add([
new wp.media.controller.FeaturedImage(),
new wp.media.controller.EditImage({
model: this.options.editImage
})
]);
2019-11-15 22:59:44 +01:00
}
});
2020-05-06 17:23:38 +02:00
};
2025-04-25 12:30:07 +02:00
const getSingleMediaFrame = () => {
2025-12-12 13:15:55 +01:00
const { wp } = window;
2025-04-25 12:30:07 +02:00
return wp.media.view.MediaFrame.Select.extend({
/**
* Create the default states on the frame.
*/
createStates() {
const options = this.options;
if (this.options.states) {
return;
}
this.states.add([
2025-12-12 13:15:55 +01:00
// Main states.
new wp.media.controller.Library({
library: wp.media.query(options.library),
multiple: options.multiple,
title: options.title,
priority: 20,
filterable: "uploaded"
// Allow filtering by uploaded images.
}),
new wp.media.controller.EditImage({
model: options.editImage
})
]);
2025-04-25 12:30:07 +02:00
}
});
};
2021-07-23 11:58:50 +02:00
const getGalleryDetailsMediaFrame = () => {
2025-12-12 13:15:55 +01:00
const { wp } = window;
2019-11-15 22:59:44 +01:00
return wp.media.view.MediaFrame.Post.extend({
2020-05-06 17:23:38 +02:00
/**
* Set up gallery toolbar.
*
* @return {void}
*/
2021-07-23 11:58:50 +02:00
galleryToolbar() {
2025-12-12 13:15:55 +01:00
const editing = this.state().get("editing");
this.toolbar.set(
new wp.media.view.Toolbar({
controller: this,
items: {
insert: {
style: "primary",
text: editing ? wp.media.view.l10n.updateGallery : wp.media.view.l10n.insertGallery,
priority: 80,
requires: { library: true },
/**
* @fires wp.media.controller.State#update
*/
click() {
const controller = this.controller, state = controller.state();
controller.close();
state.trigger(
"update",
state.get("library")
);
controller.setState(controller.options.state);
controller.reset();
}
2020-05-06 17:23:38 +02:00
}
}
2025-12-12 13:15:55 +01:00
})
);
2020-05-06 17:23:38 +02:00
},
/**
* Handle the edit state requirements of selected media item.
*
* @return {void}
*/
2021-07-23 11:58:50 +02:00
editState() {
2025-12-12 13:15:55 +01:00
const selection = this.state("gallery").get("selection");
2021-07-23 11:58:50 +02:00
const view = new wp.media.view.EditImage({
2020-05-06 17:23:38 +02:00
model: selection.single(),
controller: this
2023-12-07 09:44:11 +01:00
}).render();
this.content.set(view);
2020-05-06 17:23:38 +02:00
view.loadEditor();
},
2019-11-15 22:59:44 +01:00
/**
* Create the default states.
*
* @return {void}
*/
createStates: function createStates() {
2025-12-12 13:15:55 +01:00
this.on("toolbar:create:main-gallery", this.galleryToolbar, this);
this.on("content:render:edit-image", this.editState, this);
this.states.add([
new wp.media.controller.Library({
id: "gallery",
title: wp.media.view.l10n.createGalleryTitle,
priority: 40,
toolbar: "main-gallery",
filterable: "uploaded",
multiple: "add",
editable: false,
library: wp.media.query({
type: "image",
...this.options.library
})
}),
new wp.media.controller.EditImage({
model: this.options.editImage
}),
new wp.media.controller.GalleryEdit({
library: this.options.selection,
editing: this.options.editing,
menu: "gallery",
displaySettings: false,
multiple: true
}),
new wp.media.controller.GalleryAdd()
]);
2019-11-15 22:59:44 +01:00
}
});
2023-12-07 09:44:11 +01:00
};
2025-12-12 13:15:55 +01:00
const slimImageObject = (img) => {
const attrSet = [
"sizes",
"mime",
"type",
"subtype",
"id",
"url",
"alt",
"link",
"caption"
];
2022-12-15 17:47:31 +01:00
return attrSet.reduce((result, key) => {
2023-09-26 10:33:34 +02:00
if (img?.hasOwnProperty(key)) {
2022-12-15 17:47:31 +01:00
result[key] = img[key];
}
return result;
}, {});
2019-11-15 22:59:44 +01:00
};
2025-12-12 13:15:55 +01:00
const getAttachmentsCollection = (ids) => {
const { wp } = window;
2019-11-15 22:59:44 +01:00
return wp.media.query({
2025-12-12 13:15:55 +01:00
order: "ASC",
orderby: "post__in",
2019-11-15 22:59:44 +01:00
post__in: ids,
posts_per_page: -1,
query: true,
2025-12-12 13:15:55 +01:00
type: "image"
2019-11-15 22:59:44 +01:00
});
};
2022-06-16 14:03:35 +02:00
class MediaUpload extends external_wp_element_namespaceObject.Component {
2024-04-17 11:32:24 +02:00
constructor() {
2021-07-23 11:58:50 +02:00
super(...arguments);
this.openModal = this.openModal.bind(this);
this.onOpen = this.onOpen.bind(this);
this.onSelect = this.onSelect.bind(this);
this.onUpdate = this.onUpdate.bind(this);
this.onClose = this.onClose.bind(this);
2019-11-15 22:59:44 +01:00
}
2021-07-23 11:58:50 +02:00
initializeListeners() {
2025-12-12 13:15:55 +01:00
this.frame.on("select", this.onSelect);
this.frame.on("update", this.onUpdate);
this.frame.on("open", this.onOpen);
this.frame.on("close", this.onClose);
2021-07-23 11:58:50 +02:00
}
/**
* Sets the Gallery frame and initializes listeners.
*
* @return {void}
*/
buildAndSetGalleryFrame() {
const {
addToGallery = false,
allowedTypes,
multiple = false,
value = DEFAULT_EMPTY_GALLERY
2023-12-07 09:44:11 +01:00
} = this.props;
2021-07-23 11:58:50 +02:00
if (value === this.lastGalleryValue) {
return;
}
2025-12-12 13:15:55 +01:00
const { wp } = window;
2023-12-07 09:44:11 +01:00
this.lastGalleryValue = value;
2021-07-23 11:58:50 +02:00
if (this.frame) {
this.frame.remove();
}
let currentState;
if (addToGallery) {
2025-12-12 13:15:55 +01:00
currentState = "gallery-library";
2021-07-23 11:58:50 +02:00
} else {
2025-12-12 13:15:55 +01:00
currentState = value && value.length ? "gallery-edit" : "gallery";
2019-11-15 22:59:44 +01:00
}
2021-07-23 11:58:50 +02:00
if (!this.GalleryDetailsMediaFrame) {
this.GalleryDetailsMediaFrame = getGalleryDetailsMediaFrame();
2019-11-15 22:59:44 +01:00
}
2021-07-23 11:58:50 +02:00
const attachments = getAttachmentsCollection(value);
const selection = new wp.media.model.Selection(attachments.models, {
props: attachments.props.toJSON(),
multiple
});
this.frame = new this.GalleryDetailsMediaFrame({
mimeType: allowedTypes,
state: currentState,
multiple,
selection,
2025-04-25 12:30:07 +02:00
editing: !!value?.length
2021-07-23 11:58:50 +02:00
});
wp.media.frame = this.frame;
this.initializeListeners();
}
/**
* Initializes the Media Library requirements for the featured image flow.
*
* @return {void}
*/
buildAndSetFeatureImageFrame() {
2025-12-12 13:15:55 +01:00
const { wp } = window;
const { value: featuredImageId, multiple, allowedTypes } = this.props;
2021-07-23 11:58:50 +02:00
const featuredImageFrame = getFeaturedImageMediaFrame();
2024-04-17 11:32:24 +02:00
const attachments = getAttachmentsCollection(featuredImageId);
2021-07-23 11:58:50 +02:00
const selection = new wp.media.model.Selection(attachments.models, {
props: attachments.props.toJSON()
});
this.frame = new featuredImageFrame({
2024-04-17 11:32:24 +02:00
mimeType: allowedTypes,
2025-12-12 13:15:55 +01:00
state: "featured-image",
2024-04-17 11:32:24 +02:00
multiple,
2021-07-23 11:58:50 +02:00
selection,
2024-04-17 11:32:24 +02:00
editing: featuredImageId
2021-07-23 11:58:50 +02:00
});
wp.media.frame = this.frame;
2024-04-17 11:32:24 +02:00
wp.media.view.settings.post = {
...wp.media.view.settings.post,
featuredImageId: featuredImageId || -1
};
2021-07-23 11:58:50 +02:00
}
2025-04-25 12:30:07 +02:00
/**
* Initializes the Media Library requirements for the single image flow.
*
* @return {void}
*/
buildAndSetSingleMediaFrame() {
2025-12-12 13:15:55 +01:00
const { wp } = window;
2025-04-25 12:30:07 +02:00
const {
allowedTypes,
multiple = false,
2025-12-12 13:15:55 +01:00
title = (0,external_wp_i18n_namespaceObject.__)("Select or Upload Media"),
2025-04-25 12:30:07 +02:00
value
} = this.props;
const frameConfig = {
title,
multiple
};
if (!!allowedTypes) {
2025-12-12 13:15:55 +01:00
frameConfig.library = { type: allowedTypes };
2025-04-25 12:30:07 +02:00
}
if (this.frame) {
this.frame.remove();
}
const singleImageFrame = getSingleMediaFrame();
const attachments = getAttachmentsCollection(value);
const selection = new wp.media.model.Selection(attachments.models, {
props: attachments.props.toJSON()
});
this.frame = new singleImageFrame({
mimeType: allowedTypes,
multiple,
selection,
...frameConfig
});
wp.media.frame = this.frame;
}
2021-07-23 11:58:50 +02:00
componentWillUnmount() {
2024-04-17 11:32:24 +02:00
this.frame?.remove();
2021-07-23 11:58:50 +02:00
}
onUpdate(selections) {
2025-12-12 13:15:55 +01:00
const { onSelect, multiple = false } = this.props;
2021-07-23 11:58:50 +02:00
const state = this.frame.state();
2025-12-12 13:15:55 +01:00
const selectedImages = selections || state.get("selection");
2021-07-23 11:58:50 +02:00
if (!selectedImages || !selectedImages.models.length) {
return;
2019-11-15 22:59:44 +01:00
}
2021-07-23 11:58:50 +02:00
if (multiple) {
2025-12-12 13:15:55 +01:00
onSelect(
selectedImages.models.map(
(model) => slimImageObject(model.toJSON())
)
);
2021-07-23 11:58:50 +02:00
} else {
onSelect(slimImageObject(selectedImages.models[0].toJSON()));
}
}
onSelect() {
2025-12-12 13:15:55 +01:00
const { onSelect, multiple = false } = this.props;
const attachment = this.frame.state().get("selection").toJSON();
2021-07-23 11:58:50 +02:00
onSelect(multiple ? attachment : attachment[0]);
}
onOpen() {
2025-12-12 13:15:55 +01:00
const { wp } = window;
const { value } = this.props;
2023-12-07 09:44:11 +01:00
this.updateCollection();
2022-12-15 17:47:31 +01:00
if (this.props.mode) {
this.frame.content.mode(this.props.mode);
2023-12-07 09:44:11 +01:00
}
2023-09-26 10:33:34 +02:00
const hasMedia = Array.isArray(value) ? !!value?.length : !!value;
2021-07-23 11:58:50 +02:00
if (!hasMedia) {
return;
}
2022-04-02 10:26:41 +02:00
const isGallery = this.props.gallery;
2025-12-12 13:15:55 +01:00
const selection = this.frame.state().get("selection");
2022-12-15 17:47:31 +01:00
const valueArray = Array.isArray(value) ? value : [value];
2022-04-02 10:26:41 +02:00
if (!isGallery) {
2025-12-12 13:15:55 +01:00
valueArray.forEach((id) => {
2021-07-23 11:58:50 +02:00
selection.add(wp.media.attachment(id));
});
2023-12-07 09:44:11 +01:00
}
const attachments = getAttachmentsCollection(valueArray);
2025-12-12 13:15:55 +01:00
attachments.more().done(function() {
2023-09-26 10:33:34 +02:00
if (isGallery && attachments?.models?.length) {
2022-04-02 10:26:41 +02:00
selection.add(attachments.models);
}
});
2021-07-23 11:58:50 +02:00
}
onClose() {
2025-12-12 13:15:55 +01:00
const { onClose } = this.props;
2021-07-23 11:58:50 +02:00
if (onClose) {
onClose();
}
2025-02-28 08:42:11 +01:00
this.frame.detach();
2021-07-23 11:58:50 +02:00
}
updateCollection() {
const frameContent = this.frame.content.get();
if (frameContent && frameContent.collection) {
2023-12-07 09:44:11 +01:00
const collection = frameContent.collection;
2025-12-12 13:15:55 +01:00
collection.toArray().forEach((model) => model.trigger("destroy", model));
2023-12-07 09:44:11 +01:00
collection.mirroring._hasMore = true;
2021-07-23 11:58:50 +02:00
collection.more();
}
}
openModal() {
2024-04-17 11:32:24 +02:00
const {
gallery = false,
unstableFeaturedImageFlow = false,
2025-04-25 12:30:07 +02:00
modalClass
2024-04-17 11:32:24 +02:00
} = this.props;
if (gallery) {
2021-07-23 11:58:50 +02:00
this.buildAndSetGalleryFrame();
2024-04-17 11:32:24 +02:00
} else {
2025-04-25 12:30:07 +02:00
this.buildAndSetSingleMediaFrame();
2021-07-23 11:58:50 +02:00
}
2024-04-17 11:32:24 +02:00
if (modalClass) {
this.frame.$el.addClass(modalClass);
}
if (unstableFeaturedImageFlow) {
this.buildAndSetFeatureImageFrame();
}
this.initializeListeners();
2021-07-23 11:58:50 +02:00
this.frame.open();
}
render() {
2025-12-12 13:15:55 +01:00
return this.props.render({ open: this.openModal });
2021-07-23 11:58:50 +02:00
}
}
2025-12-12 13:15:55 +01:00
var media_upload_default = MediaUpload;
2020-05-06 17:23:38 +02:00
2025-04-25 12:30:07 +02:00
;// ./node_modules/@wordpress/media-utils/build-module/components/index.js
2020-05-06 17:23:38 +02:00
2020-09-15 14:29:22 +02:00
2025-12-12 13:15:55 +01:00
2025-04-25 12:30:07 +02:00
;// external ["wp","blob"]
2025-02-28 08:42:11 +01:00
const external_wp_blob_namespaceObject = window["wp"]["blob"];
2025-04-25 12:30:07 +02:00
;// external ["wp","apiFetch"]
2024-04-17 11:32:24 +02:00
const external_wp_apiFetch_namespaceObject = window["wp"]["apiFetch"];
2022-06-16 14:03:35 +02:00
var external_wp_apiFetch_default = /*#__PURE__*/__webpack_require__.n(external_wp_apiFetch_namespaceObject);
2025-04-25 12:30:07 +02:00
;// ./node_modules/@wordpress/media-utils/build-module/utils/flatten-form-data.js
2025-02-28 08:42:11 +01:00
function isPlainObject(data) {
2025-12-12 13:15:55 +01:00
return data !== null && typeof data === "object" && Object.getPrototypeOf(data) === Object.prototype;
2025-02-28 08:42:11 +01:00
}
function flattenFormData(formData, key, data) {
if (isPlainObject(data)) {
for (const [name, value] of Object.entries(data)) {
flattenFormData(formData, `${key}[${name}]`, value);
}
2025-12-12 13:15:55 +01:00
} else if (data !== void 0) {
2025-02-28 08:42:11 +01:00
formData.append(key, String(data));
}
}
2025-12-12 13:15:55 +01:00
2025-04-25 12:30:07 +02:00
;// ./node_modules/@wordpress/media-utils/build-module/utils/transform-attachment.js
2025-02-28 08:42:11 +01:00
function transformAttachment(attachment) {
2025-12-12 13:15:55 +01:00
const { alt_text, source_url, ...savedMediaProps } = attachment;
2025-02-28 08:42:11 +01:00
return {
...savedMediaProps,
alt: attachment.alt_text,
2025-12-12 13:15:55 +01:00
caption: attachment.caption?.raw ?? "",
2025-02-28 08:42:11 +01:00
title: attachment.title.raw,
url: attachment.source_url,
2025-12-12 13:15:55 +01:00
poster: attachment._embedded?.["wp:featuredmedia"]?.[0]?.source_url || void 0
2025-02-28 08:42:11 +01:00
};
}
2019-11-15 22:59:44 +01:00
2025-12-12 13:15:55 +01:00
;// ./node_modules/@wordpress/media-utils/build-module/utils/upload-to-server.js
2019-11-15 22:59:44 +01:00
2025-02-28 08:42:11 +01:00
2019-11-15 22:59:44 +01:00
2025-02-28 08:42:11 +01:00
async function uploadToServer(file, additionalData = {}, signal) {
const data = new FormData();
2025-12-12 13:15:55 +01:00
data.append("file", file, file.name || file.type.replace("/", "."));
2025-02-28 08:42:11 +01:00
for (const [key, value] of Object.entries(additionalData)) {
2025-12-12 13:15:55 +01:00
flattenFormData(
data,
key,
value
);
2025-02-28 08:42:11 +01:00
}
2025-12-12 13:15:55 +01:00
return transformAttachment(
await external_wp_apiFetch_default()({
// This allows the video block to directly get a video's poster image.
path: "/wp/v2/media?_embed=wp:featuredmedia",
body: data,
method: "POST",
signal
})
);
2025-02-28 08:42:11 +01:00
}
2023-12-07 09:44:11 +01:00
2025-12-12 13:15:55 +01:00
2025-04-25 12:30:07 +02:00
;// ./node_modules/@wordpress/media-utils/build-module/utils/upload-error.js
2025-02-28 08:42:11 +01:00
class UploadError extends Error {
2025-12-12 13:15:55 +01:00
code;
file;
constructor({ code, message, file, cause }) {
super(message, { cause });
2025-02-28 08:42:11 +01:00
Object.setPrototypeOf(this, new.target.prototype);
this.code = code;
this.file = file;
}
}
2025-12-12 13:15:55 +01:00
;// ./node_modules/@wordpress/media-utils/build-module/utils/validate-mime-type.js
2025-02-28 08:42:11 +01:00
function validateMimeType(file, allowedTypes) {
if (!allowedTypes) {
return;
}
2025-12-12 13:15:55 +01:00
const isAllowedType = allowedTypes.some((allowedType) => {
if (allowedType.includes("/")) {
2025-02-28 08:42:11 +01:00
return allowedType === file.type;
}
return file.type.startsWith(`${allowedType}/`);
});
if (file.type && !isAllowedType) {
throw new UploadError({
2025-12-12 13:15:55 +01:00
code: "MIME_TYPE_NOT_SUPPORTED",
2025-02-28 08:42:11 +01:00
message: (0,external_wp_i18n_namespaceObject.sprintf)(
2025-12-12 13:15:55 +01:00
// translators: %s: file name.
(0,external_wp_i18n_namespaceObject.__)("%s: Sorry, this file type is not supported here."),
file.name
),
2025-02-28 08:42:11 +01:00
file
});
}
}
2025-12-12 13:15:55 +01:00
2025-04-25 12:30:07 +02:00
;// ./node_modules/@wordpress/media-utils/build-module/utils/get-mime-types-array.js
2019-11-15 22:59:44 +01:00
function getMimeTypesArray(wpMimeTypesObject) {
if (!wpMimeTypesObject) {
2025-02-28 08:42:11 +01:00
return null;
2019-11-15 22:59:44 +01:00
}
2025-12-12 13:15:55 +01:00
return Object.entries(wpMimeTypesObject).flatMap(
([extensionsString, mime]) => {
const [type] = mime.split("/");
const extensions = extensionsString.split("|");
return [
mime,
...extensions.map(
(extension) => `${type}/${extension}`
)
];
}
);
2019-11-15 22:59:44 +01:00
}
2023-12-07 09:44:11 +01:00
2025-02-28 08:42:11 +01:00
2025-12-12 13:15:55 +01:00
;// ./node_modules/@wordpress/media-utils/build-module/utils/validate-mime-type-for-user.js
2025-02-28 08:42:11 +01:00
function validateMimeTypeForUser(file, wpAllowedMimeTypes) {
const allowedMimeTypesForUser = getMimeTypesArray(wpAllowedMimeTypes);
if (!allowedMimeTypesForUser) {
return;
}
2025-12-12 13:15:55 +01:00
const isAllowedMimeTypeForUser = allowedMimeTypesForUser.includes(
file.type
);
2025-02-28 08:42:11 +01:00
if (file.type && !isAllowedMimeTypeForUser) {
throw new UploadError({
2025-12-12 13:15:55 +01:00
code: "MIME_TYPE_NOT_ALLOWED_FOR_USER",
2025-02-28 08:42:11 +01:00
message: (0,external_wp_i18n_namespaceObject.sprintf)(
2025-12-12 13:15:55 +01:00
// translators: %s: file name.
(0,external_wp_i18n_namespaceObject.__)(
"%s: Sorry, you are not allowed to upload this file type."
),
file.name
),
2025-02-28 08:42:11 +01:00
file
});
}
}
2025-12-12 13:15:55 +01:00
;// ./node_modules/@wordpress/media-utils/build-module/utils/validate-file-size.js
2025-02-28 08:42:11 +01:00
function validateFileSize(file, maxUploadFileSize) {
if (file.size <= 0) {
throw new UploadError({
2025-12-12 13:15:55 +01:00
code: "EMPTY_FILE",
2025-02-28 08:42:11 +01:00
message: (0,external_wp_i18n_namespaceObject.sprintf)(
2025-12-12 13:15:55 +01:00
// translators: %s: file name.
(0,external_wp_i18n_namespaceObject.__)("%s: This file is empty."),
file.name
),
2025-02-28 08:42:11 +01:00
file
});
}
if (maxUploadFileSize && file.size > maxUploadFileSize) {
throw new UploadError({
2025-12-12 13:15:55 +01:00
code: "SIZE_ABOVE_LIMIT",
2025-02-28 08:42:11 +01:00
message: (0,external_wp_i18n_namespaceObject.sprintf)(
2025-12-12 13:15:55 +01:00
// translators: %s: file name.
(0,external_wp_i18n_namespaceObject.__)(
"%s: This file exceeds the maximum upload size for this site."
),
file.name
),
2025-02-28 08:42:11 +01:00
file
});
}
}
2025-12-12 13:15:55 +01:00
;// ./node_modules/@wordpress/media-utils/build-module/utils/upload-media.js
2025-02-28 08:42:11 +01:00
function uploadMedia({
wpAllowedMimeTypes,
2023-09-26 10:33:34 +02:00
allowedTypes,
additionalData = {},
filesList,
maxUploadFileSize,
2025-02-28 08:42:11 +01:00
onError,
2023-09-26 10:33:34 +02:00
onFileChange,
2025-04-25 12:30:07 +02:00
signal,
multiple = true
2023-09-26 10:33:34 +02:00
}) {
2025-04-25 12:30:07 +02:00
if (!multiple && filesList.length > 1) {
2025-12-12 13:15:55 +01:00
onError?.(new Error((0,external_wp_i18n_namespaceObject.__)("Only one file can be used here.")));
2025-04-25 12:30:07 +02:00
return;
}
2025-02-28 08:42:11 +01:00
const validFiles = [];
2021-07-23 11:58:50 +02:00
const filesSet = [];
2025-02-28 08:42:11 +01:00
const setAndUpdateFiles = (index, value) => {
2025-04-25 12:30:07 +02:00
if (!window.__experimentalMediaProcessing) {
if (filesSet[index]?.url) {
(0,external_wp_blob_namespaceObject.revokeBlobURL)(filesSet[index].url);
}
2021-07-23 11:58:50 +02:00
}
2025-02-28 08:42:11 +01:00
filesSet[index] = value;
2025-12-12 13:15:55 +01:00
onFileChange?.(
filesSet.filter((attachment) => attachment !== null)
);
2021-07-23 11:58:50 +02:00
};
2025-02-28 08:42:11 +01:00
for (const mediaFile of filesList) {
try {
validateMimeTypeForUser(mediaFile, wpAllowedMimeTypes);
} catch (error) {
onError?.(error);
2021-07-23 11:58:50 +02:00
continue;
2023-12-07 09:44:11 +01:00
}
2025-02-28 08:42:11 +01:00
try {
validateMimeType(mediaFile, allowedTypes);
} catch (error) {
onError?.(error);
2021-07-23 11:58:50 +02:00
continue;
2023-12-07 09:44:11 +01:00
}
2025-02-28 08:42:11 +01:00
try {
validateFileSize(mediaFile, maxUploadFileSize);
} catch (error) {
onError?.(error);
2021-07-23 11:58:50 +02:00
continue;
}
2023-12-07 09:44:11 +01:00
validFiles.push(mediaFile);
2025-04-25 12:30:07 +02:00
if (!window.__experimentalMediaProcessing) {
2025-12-12 13:15:55 +01:00
filesSet.push({ url: (0,external_wp_blob_namespaceObject.createBlobURL)(mediaFile) });
2025-04-25 12:30:07 +02:00
onFileChange?.(filesSet);
}
2021-07-23 11:58:50 +02:00
}
2025-02-28 08:42:11 +01:00
validFiles.map(async (file, index) => {
2021-07-23 11:58:50 +02:00
try {
2025-12-12 13:15:55 +01:00
const attachment = await uploadToServer(
file,
additionalData,
signal
);
2025-02-28 08:42:11 +01:00
setAndUpdateFiles(index, attachment);
2021-07-23 11:58:50 +02:00
} catch (error) {
2025-02-28 08:42:11 +01:00
setAndUpdateFiles(index, null);
2021-07-23 11:58:50 +02:00
let message;
2025-12-12 13:15:55 +01:00
if (typeof error === "object" && error !== null && "message" in error) {
message = typeof error.message === "string" ? error.message : String(error.message);
2021-07-23 11:58:50 +02:00
} else {
2023-12-07 09:44:11 +01:00
message = (0,external_wp_i18n_namespaceObject.sprintf)(
2025-12-12 13:15:55 +01:00
// translators: %s: file name
(0,external_wp_i18n_namespaceObject.__)("Error while uploading file %s to the media library."),
file.name
);
2019-11-15 22:59:44 +01:00
}
2025-12-12 13:15:55 +01:00
onError?.(
new UploadError({
code: "GENERAL",
message,
file,
cause: error instanceof Error ? error : void 0
})
);
2021-07-23 11:58:50 +02:00
}
2019-11-15 22:59:44 +01:00
});
}
2025-04-25 12:30:07 +02:00
2025-12-12 13:15:55 +01:00
;// ./node_modules/@wordpress/media-utils/build-module/utils/sideload-to-server.js
2025-04-25 12:30:07 +02:00
async function sideloadToServer(file, attachmentId, additionalData = {}, signal) {
const data = new FormData();
2025-12-12 13:15:55 +01:00
data.append("file", file, file.name || file.type.replace("/", "."));
2025-04-25 12:30:07 +02:00
for (const [key, value] of Object.entries(additionalData)) {
2025-12-12 13:15:55 +01:00
flattenFormData(
data,
key,
value
);
2025-04-25 12:30:07 +02:00
}
2025-12-12 13:15:55 +01:00
return transformAttachment(
await external_wp_apiFetch_default()({
path: `/wp/v2/media/${attachmentId}/sideload`,
body: data,
method: "POST",
signal
})
);
2025-04-25 12:30:07 +02:00
}
2025-12-12 13:15:55 +01:00
;// ./node_modules/@wordpress/media-utils/build-module/utils/sideload-media.js
2025-04-25 12:30:07 +02:00
2025-12-12 13:15:55 +01:00
const noop = () => {
};
2025-04-25 12:30:07 +02:00
async function sideloadMedia({
file,
attachmentId,
additionalData = {},
signal,
onFileChange,
onError = noop
}) {
try {
2025-12-12 13:15:55 +01:00
const attachment = await sideloadToServer(
file,
attachmentId,
additionalData,
signal
);
2025-04-25 12:30:07 +02:00
onFileChange?.([attachment]);
} catch (error) {
let message;
if (error instanceof Error) {
message = error.message;
} else {
message = (0,external_wp_i18n_namespaceObject.sprintf)(
2025-12-12 13:15:55 +01:00
// translators: %s: file name
(0,external_wp_i18n_namespaceObject.__)("Error while sideloading file %s to the server."),
file.name
);
2025-04-25 12:30:07 +02:00
}
2025-12-12 13:15:55 +01:00
onError(
new UploadError({
code: "GENERAL",
message,
file,
cause: error instanceof Error ? error : void 0
})
);
2025-04-25 12:30:07 +02:00
}
}
2025-12-12 13:15:55 +01:00
2025-04-25 12:30:07 +02:00
;// external ["wp","privateApis"]
const external_wp_privateApis_namespaceObject = window["wp"]["privateApis"];
;// ./node_modules/@wordpress/media-utils/build-module/lock-unlock.js
2025-12-12 13:15:55 +01:00
const { lock, unlock } = (0,external_wp_privateApis_namespaceObject.__dangerousOptInToUnstableAPIsOnlyForCoreModules)(
"I acknowledge private features are not for use in themes or plugins and doing so will break in the next version of WordPress.",
"@wordpress/media-utils"
);
2025-04-25 12:30:07 +02:00
2025-12-12 13:15:55 +01:00
;// ./node_modules/@wordpress/media-utils/build-module/private-apis.js
2025-04-25 12:30:07 +02:00
const privateApis = {};
lock(privateApis, {
sideloadMedia: sideloadMedia
});
2025-12-12 13:15:55 +01:00
2025-04-25 12:30:07 +02:00
;// ./node_modules/@wordpress/media-utils/build-module/index.js
2025-02-28 08:42:11 +01:00
2021-04-27 08:32:47 +02:00
2019-11-15 22:59:44 +01:00
2025-12-12 13:15:55 +01:00
2022-06-16 14:03:35 +02:00
(window.wp = window.wp || {}).mediaUtils = __webpack_exports__;
/******/ })()
;