author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:40:08 +0200 | |
changeset 21 | 48c4eec2b7e6 |
parent 19 | 3d72ae0968f4 |
child 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1 |
/******/ (() => { // webpackBootstrap |
19 | 2 |
/******/ "use strict"; |
3 |
/******/ // The require scope |
|
4 |
/******/ var __webpack_require__ = {}; |
|
5 |
/******/ |
|
6 |
/************************************************************************/ |
|
7 |
/******/ /* webpack/runtime/compat get default export */ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
8 |
/******/ (() => { |
19 | 9 |
/******/ // getDefaultExport function for compatibility with non-harmony modules |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
10 |
/******/ __webpack_require__.n = (module) => { |
19 | 11 |
/******/ var getter = module && module.__esModule ? |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
12 |
/******/ () => (module['default']) : |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
13 |
/******/ () => (module); |
19 | 14 |
/******/ __webpack_require__.d(getter, { a: getter }); |
15 |
/******/ return getter; |
|
16 | 16 |
/******/ }; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
17 |
/******/ })(); |
19 | 18 |
/******/ |
19 |
/******/ /* webpack/runtime/define property getters */ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
20 |
/******/ (() => { |
19 | 21 |
/******/ // define getter functions for harmony exports |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
22 |
/******/ __webpack_require__.d = (exports, definition) => { |
19 | 23 |
/******/ for(var key in definition) { |
24 |
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { |
|
25 |
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); |
|
26 |
/******/ } |
|
27 |
/******/ } |
|
28 |
/******/ }; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
29 |
/******/ })(); |
19 | 30 |
/******/ |
31 |
/******/ /* webpack/runtime/hasOwnProperty shorthand */ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
32 |
/******/ (() => { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
33 |
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
34 |
/******/ })(); |
19 | 35 |
/******/ |
36 |
/******/ /* webpack/runtime/make namespace object */ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
37 |
/******/ (() => { |
19 | 38 |
/******/ // define __esModule on exports |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
39 |
/******/ __webpack_require__.r = (exports) => { |
19 | 40 |
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { |
41 |
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); |
|
42 |
/******/ } |
|
43 |
/******/ Object.defineProperty(exports, '__esModule', { value: true }); |
|
44 |
/******/ }; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
45 |
/******/ })(); |
19 | 46 |
/******/ |
16 | 47 |
/************************************************************************/ |
19 | 48 |
var __webpack_exports__ = {}; |
16 | 49 |
// ESM COMPAT FLAG |
50 |
__webpack_require__.r(__webpack_exports__); |
|
51 |
||
52 |
// EXPORTS |
|
19 | 53 |
__webpack_require__.d(__webpack_exports__, { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
54 |
MediaUpload: () => (/* reexport */ media_upload), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
55 |
uploadMedia: () => (/* reexport */ uploadMedia) |
19 | 56 |
}); |
16 | 57 |
|
19 | 58 |
;// CONCATENATED MODULE: external ["wp","element"] |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
59 |
const external_wp_element_namespaceObject = window["wp"]["element"]; |
19 | 60 |
;// CONCATENATED MODULE: external ["wp","i18n"] |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
61 |
const external_wp_i18n_namespaceObject = window["wp"]["i18n"]; |
19 | 62 |
;// CONCATENATED MODULE: ./node_modules/@wordpress/media-utils/build-module/components/media-upload/index.js |
16 | 63 |
/** |
64 |
* WordPress dependencies |
|
65 |
*/ |
|
66 |
||
67 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
68 |
const DEFAULT_EMPTY_GALLERY = []; |
16 | 69 |
|
70 |
/** |
|
71 |
* Prepares the Featured Image toolbars and frames. |
|
72 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
73 |
* @return {window.wp.media.view.MediaFrame.Select} The default media workflow. |
16 | 74 |
*/ |
18 | 75 |
const getFeaturedImageMediaFrame = () => { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
76 |
const { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
77 |
wp |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
78 |
} = window; |
16 | 79 |
return wp.media.view.MediaFrame.Select.extend({ |
80 |
/** |
|
81 |
* Enables the Set Featured Image Button. |
|
82 |
* |
|
83 |
* @param {Object} toolbar toolbar for featured image state |
|
84 |
* @return {void} |
|
85 |
*/ |
|
18 | 86 |
featuredImageToolbar(toolbar) { |
16 | 87 |
this.createSelectToolbar(toolbar, { |
88 |
text: wp.media.view.l10n.setFeaturedImage, |
|
89 |
state: this.options.state |
|
90 |
}); |
|
91 |
}, |
|
92 |
/** |
|
93 |
* Handle the edit state requirements of selected media item. |
|
94 |
* |
|
95 |
* @return {void} |
|
96 |
*/ |
|
18 | 97 |
editState() { |
98 |
const selection = this.state('featured-image').get('selection'); |
|
99 |
const view = new wp.media.view.EditImage({ |
|
16 | 100 |
model: selection.single(), |
101 |
controller: this |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
102 |
}).render(); |
16 | 103 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
104 |
// Set the view to the EditImage frame using the selected image. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
105 |
this.content.set(view); |
16 | 106 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
107 |
// After bringing in the frame, load the actual editor via an ajax call. |
16 | 108 |
view.loadEditor(); |
109 |
}, |
|
110 |
/** |
|
111 |
* Create the default states. |
|
112 |
* |
|
113 |
* @return {void} |
|
114 |
*/ |
|
115 |
createStates: function createStates() { |
|
116 |
this.on('toolbar:create:featured-image', this.featuredImageToolbar, this); |
|
117 |
this.on('content:render:edit-image', this.editState, this); |
|
118 |
this.states.add([new wp.media.controller.FeaturedImage(), new wp.media.controller.EditImage({ |
|
119 |
model: this.options.editImage |
|
120 |
})]); |
|
121 |
} |
|
122 |
}); |
|
123 |
}; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
124 |
|
16 | 125 |
/** |
126 |
* Prepares the Gallery toolbars and frames. |
|
127 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
128 |
* @return {window.wp.media.view.MediaFrame.Post} The default media workflow. |
16 | 129 |
*/ |
18 | 130 |
const getGalleryDetailsMediaFrame = () => { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
131 |
const { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
132 |
wp |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
133 |
} = window; |
16 | 134 |
/** |
135 |
* Custom gallery details frame. |
|
136 |
* |
|
137 |
* @see https://github.com/xwp/wp-core-media-widgets/blob/905edbccfc2a623b73a93dac803c5335519d7837/wp-admin/js/widgets/media-gallery-widget.js |
|
138 |
* @class GalleryDetailsMediaFrame |
|
139 |
* @class |
|
140 |
*/ |
|
141 |
return wp.media.view.MediaFrame.Post.extend({ |
|
142 |
/** |
|
143 |
* Set up gallery toolbar. |
|
144 |
* |
|
145 |
* @return {void} |
|
146 |
*/ |
|
18 | 147 |
galleryToolbar() { |
148 |
const editing = this.state().get('editing'); |
|
16 | 149 |
this.toolbar.set(new wp.media.view.Toolbar({ |
150 |
controller: this, |
|
151 |
items: { |
|
152 |
insert: { |
|
153 |
style: 'primary', |
|
154 |
text: editing ? wp.media.view.l10n.updateGallery : wp.media.view.l10n.insertGallery, |
|
155 |
priority: 80, |
|
156 |
requires: { |
|
157 |
library: true |
|
158 |
}, |
|
159 |
/** |
|
160 |
* @fires wp.media.controller.State#update |
|
161 |
*/ |
|
18 | 162 |
click() { |
163 |
const controller = this.controller, |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
164 |
state = controller.state(); |
16 | 165 |
controller.close(); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
166 |
state.trigger('update', state.get('library')); |
16 | 167 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
168 |
// Restore and reset the default state. |
16 | 169 |
controller.setState(controller.options.state); |
170 |
controller.reset(); |
|
171 |
} |
|
172 |
} |
|
173 |
} |
|
174 |
})); |
|
175 |
}, |
|
176 |
/** |
|
177 |
* Handle the edit state requirements of selected media item. |
|
178 |
* |
|
179 |
* @return {void} |
|
180 |
*/ |
|
18 | 181 |
editState() { |
182 |
const selection = this.state('gallery').get('selection'); |
|
183 |
const view = new wp.media.view.EditImage({ |
|
16 | 184 |
model: selection.single(), |
185 |
controller: this |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
186 |
}).render(); |
16 | 187 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
188 |
// Set the view to the EditImage frame using the selected image. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
189 |
this.content.set(view); |
16 | 190 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
191 |
// After bringing in the frame, load the actual editor via an ajax call. |
16 | 192 |
view.loadEditor(); |
193 |
}, |
|
194 |
/** |
|
195 |
* Create the default states. |
|
196 |
* |
|
197 |
* @return {void} |
|
198 |
*/ |
|
199 |
createStates: function createStates() { |
|
200 |
this.on('toolbar:create:main-gallery', this.galleryToolbar, this); |
|
201 |
this.on('content:render:edit-image', this.editState, this); |
|
202 |
this.states.add([new wp.media.controller.Library({ |
|
203 |
id: 'gallery', |
|
204 |
title: wp.media.view.l10n.createGalleryTitle, |
|
205 |
priority: 40, |
|
206 |
toolbar: 'main-gallery', |
|
207 |
filterable: 'uploaded', |
|
208 |
multiple: 'add', |
|
209 |
editable: false, |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
210 |
library: wp.media.query({ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
211 |
type: 'image', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
212 |
...this.options.library |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
213 |
}) |
16 | 214 |
}), new wp.media.controller.EditImage({ |
215 |
model: this.options.editImage |
|
216 |
}), new wp.media.controller.GalleryEdit({ |
|
217 |
library: this.options.selection, |
|
218 |
editing: this.options.editing, |
|
219 |
menu: 'gallery', |
|
220 |
displaySettings: false, |
|
221 |
multiple: true |
|
222 |
}), new wp.media.controller.GalleryAdd()]); |
|
223 |
} |
|
224 |
}); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
225 |
}; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
226 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
227 |
// The media library image object contains numerous attributes |
19 | 228 |
// we only need this set to display the image in the library. |
18 | 229 |
const slimImageObject = img => { |
230 |
const attrSet = ['sizes', 'mime', 'type', 'subtype', 'id', 'url', 'alt', 'link', 'caption']; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
231 |
return attrSet.reduce((result, key) => { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
232 |
if (img?.hasOwnProperty(key)) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
233 |
result[key] = img[key]; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
234 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
235 |
return result; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
236 |
}, {}); |
16 | 237 |
}; |
18 | 238 |
const getAttachmentsCollection = ids => { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
239 |
const { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
240 |
wp |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
241 |
} = window; |
16 | 242 |
return wp.media.query({ |
243 |
order: 'ASC', |
|
244 |
orderby: 'post__in', |
|
245 |
post__in: ids, |
|
246 |
posts_per_page: -1, |
|
247 |
query: true, |
|
248 |
type: 'image' |
|
249 |
}); |
|
250 |
}; |
|
19 | 251 |
class MediaUpload extends external_wp_element_namespaceObject.Component { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
252 |
constructor() { |
18 | 253 |
super(...arguments); |
254 |
this.openModal = this.openModal.bind(this); |
|
255 |
this.onOpen = this.onOpen.bind(this); |
|
256 |
this.onSelect = this.onSelect.bind(this); |
|
257 |
this.onUpdate = this.onUpdate.bind(this); |
|
258 |
this.onClose = this.onClose.bind(this); |
|
259 |
} |
|
260 |
initializeListeners() { |
|
261 |
// When an image is selected in the media frame... |
|
262 |
this.frame.on('select', this.onSelect); |
|
263 |
this.frame.on('update', this.onUpdate); |
|
264 |
this.frame.on('open', this.onOpen); |
|
265 |
this.frame.on('close', this.onClose); |
|
266 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
267 |
|
18 | 268 |
/** |
269 |
* Sets the Gallery frame and initializes listeners. |
|
270 |
* |
|
271 |
* @return {void} |
|
272 |
*/ |
|
273 |
buildAndSetGalleryFrame() { |
|
274 |
const { |
|
275 |
addToGallery = false, |
|
276 |
allowedTypes, |
|
277 |
multiple = false, |
|
278 |
value = DEFAULT_EMPTY_GALLERY |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
279 |
} = this.props; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
280 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
281 |
// If the value did not changed there is no need to rebuild the frame, |
18 | 282 |
// we can continue to use the existing one. |
283 |
if (value === this.lastGalleryValue) { |
|
284 |
return; |
|
285 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
286 |
const { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
287 |
wp |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
288 |
} = window; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
289 |
this.lastGalleryValue = value; |
18 | 290 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
291 |
// If a frame already existed remove it. |
18 | 292 |
if (this.frame) { |
293 |
this.frame.remove(); |
|
16 | 294 |
} |
18 | 295 |
let currentState; |
296 |
if (addToGallery) { |
|
297 |
currentState = 'gallery-library'; |
|
298 |
} else { |
|
299 |
currentState = value && value.length ? 'gallery-edit' : 'gallery'; |
|
300 |
} |
|
301 |
if (!this.GalleryDetailsMediaFrame) { |
|
302 |
this.GalleryDetailsMediaFrame = getGalleryDetailsMediaFrame(); |
|
303 |
} |
|
304 |
const attachments = getAttachmentsCollection(value); |
|
305 |
const selection = new wp.media.model.Selection(attachments.models, { |
|
306 |
props: attachments.props.toJSON(), |
|
307 |
multiple |
|
308 |
}); |
|
309 |
this.frame = new this.GalleryDetailsMediaFrame({ |
|
310 |
mimeType: allowedTypes, |
|
311 |
state: currentState, |
|
312 |
multiple, |
|
313 |
selection, |
|
314 |
editing: value && value.length ? true : false |
|
315 |
}); |
|
316 |
wp.media.frame = this.frame; |
|
317 |
this.initializeListeners(); |
|
318 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
319 |
|
18 | 320 |
/** |
321 |
* Initializes the Media Library requirements for the featured image flow. |
|
322 |
* |
|
323 |
* @return {void} |
|
324 |
*/ |
|
325 |
buildAndSetFeatureImageFrame() { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
326 |
const { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
327 |
wp |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
328 |
} = window; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
329 |
const { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
330 |
value: featuredImageId, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
331 |
multiple, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
332 |
allowedTypes |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
333 |
} = this.props; |
18 | 334 |
const featuredImageFrame = getFeaturedImageMediaFrame(); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
335 |
const attachments = getAttachmentsCollection(featuredImageId); |
18 | 336 |
const selection = new wp.media.model.Selection(attachments.models, { |
337 |
props: attachments.props.toJSON() |
|
338 |
}); |
|
339 |
this.frame = new featuredImageFrame({ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
340 |
mimeType: allowedTypes, |
18 | 341 |
state: 'featured-image', |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
342 |
multiple, |
18 | 343 |
selection, |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
344 |
editing: featuredImageId |
18 | 345 |
}); |
346 |
wp.media.frame = this.frame; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
347 |
// In order to select the current featured image when opening |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
348 |
// the media library we have to set the appropriate settings. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
349 |
// Currently they are set in php for the post editor, but |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
350 |
// not for site editor. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
351 |
wp.media.view.settings.post = { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
352 |
...wp.media.view.settings.post, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
353 |
featuredImageId: featuredImageId || -1 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
354 |
}; |
16 | 355 |
} |
18 | 356 |
componentWillUnmount() { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
357 |
this.frame?.remove(); |
18 | 358 |
} |
359 |
onUpdate(selections) { |
|
360 |
const { |
|
361 |
onSelect, |
|
362 |
multiple = false |
|
363 |
} = this.props; |
|
364 |
const state = this.frame.state(); |
|
365 |
const selectedImages = selections || state.get('selection'); |
|
366 |
if (!selectedImages || !selectedImages.models.length) { |
|
367 |
return; |
|
16 | 368 |
} |
18 | 369 |
if (multiple) { |
370 |
onSelect(selectedImages.models.map(model => slimImageObject(model.toJSON()))); |
|
371 |
} else { |
|
372 |
onSelect(slimImageObject(selectedImages.models[0].toJSON())); |
|
373 |
} |
|
374 |
} |
|
375 |
onSelect() { |
|
376 |
const { |
|
377 |
onSelect, |
|
378 |
multiple = false |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
379 |
} = this.props; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
380 |
// Get media attachment details from the frame state. |
18 | 381 |
const attachment = this.frame.state().get('selection').toJSON(); |
382 |
onSelect(multiple ? attachment : attachment[0]); |
|
383 |
} |
|
384 |
onOpen() { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
385 |
const { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
386 |
wp |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
387 |
} = window; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
388 |
const { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
389 |
value |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
390 |
} = this.props; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
391 |
this.updateCollection(); |
16 | 392 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
393 |
//Handle active tab in media model on model open. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
394 |
if (this.props.mode) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
395 |
this.frame.content.mode(this.props.mode); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
396 |
} |
16 | 397 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
398 |
// Handle both this.props.value being either (number[]) multiple ids |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
399 |
// (for galleries) or a (number) singular id (e.g. image block). |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
400 |
const hasMedia = Array.isArray(value) ? !!value?.length : !!value; |
18 | 401 |
if (!hasMedia) { |
402 |
return; |
|
16 | 403 |
} |
19 | 404 |
const isGallery = this.props.gallery; |
405 |
const selection = this.frame.state().get('selection'); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
406 |
const valueArray = Array.isArray(value) ? value : [value]; |
19 | 407 |
if (!isGallery) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
408 |
valueArray.forEach(id => { |
18 | 409 |
selection.add(wp.media.attachment(id)); |
410 |
}); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
411 |
} |
19 | 412 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
413 |
// Load the images so they are available in the media modal. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
414 |
const attachments = getAttachmentsCollection(valueArray); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
415 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
416 |
// Once attachments are loaded, set the current selection. |
19 | 417 |
attachments.more().done(function () { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
418 |
if (isGallery && attachments?.models?.length) { |
19 | 419 |
selection.add(attachments.models); |
420 |
} |
|
421 |
}); |
|
18 | 422 |
} |
423 |
onClose() { |
|
424 |
const { |
|
425 |
onClose |
|
426 |
} = this.props; |
|
427 |
if (onClose) { |
|
428 |
onClose(); |
|
16 | 429 |
} |
18 | 430 |
} |
431 |
updateCollection() { |
|
432 |
const frameContent = this.frame.content.get(); |
|
433 |
if (frameContent && frameContent.collection) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
434 |
const collection = frameContent.collection; |
16 | 435 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
436 |
// Clean all attachments we have in memory. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
437 |
collection.toArray().forEach(model => model.trigger('destroy', model)); |
16 | 438 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
439 |
// Reset has more flag, if library had small amount of items all items may have been loaded before. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
440 |
collection.mirroring._hasMore = true; |
16 | 441 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
442 |
// Request items. |
18 | 443 |
collection.more(); |
16 | 444 |
} |
18 | 445 |
} |
446 |
openModal() { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
447 |
const { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
448 |
allowedTypes, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
449 |
gallery = false, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
450 |
unstableFeaturedImageFlow = false, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
451 |
modalClass, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
452 |
multiple = false, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
453 |
title = (0,external_wp_i18n_namespaceObject.__)('Select or Upload Media') |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
454 |
} = this.props; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
455 |
const { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
456 |
wp |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
457 |
} = window; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
458 |
if (gallery) { |
18 | 459 |
this.buildAndSetGalleryFrame(); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
460 |
} else { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
461 |
const frameConfig = { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
462 |
title, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
463 |
multiple |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
464 |
}; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
465 |
if (!!allowedTypes) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
466 |
frameConfig.library = { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
467 |
type: allowedTypes |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
468 |
}; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
469 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
470 |
this.frame = wp.media(frameConfig); |
18 | 471 |
} |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
472 |
if (modalClass) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
473 |
this.frame.$el.addClass(modalClass); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
474 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
475 |
if (unstableFeaturedImageFlow) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
476 |
this.buildAndSetFeatureImageFrame(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
477 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
478 |
this.initializeListeners(); |
18 | 479 |
this.frame.open(); |
480 |
} |
|
481 |
render() { |
|
482 |
return this.props.render({ |
|
483 |
open: this.openModal |
|
484 |
}); |
|
485 |
} |
|
486 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
487 |
/* harmony default export */ const media_upload = (MediaUpload); |
16 | 488 |
|
19 | 489 |
;// CONCATENATED MODULE: ./node_modules/@wordpress/media-utils/build-module/components/index.js |
16 | 490 |
|
491 |
||
19 | 492 |
;// CONCATENATED MODULE: external ["wp","apiFetch"] |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
493 |
const external_wp_apiFetch_namespaceObject = window["wp"]["apiFetch"]; |
19 | 494 |
var external_wp_apiFetch_default = /*#__PURE__*/__webpack_require__.n(external_wp_apiFetch_namespaceObject); |
495 |
;// CONCATENATED MODULE: external ["wp","blob"] |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
496 |
const external_wp_blob_namespaceObject = window["wp"]["blob"]; |
19 | 497 |
;// CONCATENATED MODULE: ./node_modules/@wordpress/media-utils/build-module/utils/upload-media.js |
16 | 498 |
/** |
499 |
* WordPress dependencies |
|
500 |
*/ |
|
501 |
||
502 |
||
503 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
504 |
const noop = () => {}; |
16 | 505 |
|
506 |
/** |
|
507 |
* Browsers may use unexpected mime types, and they differ from browser to browser. |
|
508 |
* This function computes a flexible array of mime types from the mime type structured provided by the server. |
|
509 |
* Converts { jpg|jpeg|jpe: "image/jpeg" } into [ "image/jpeg", "image/jpg", "image/jpeg", "image/jpe" ] |
|
510 |
* The computation of this array instead of directly using the object, |
|
511 |
* solves the problem in chrome where mp3 files have audio/mp3 as mime type instead of audio/mpeg. |
|
512 |
* https://bugs.chromium.org/p/chromium/issues/detail?id=227004 |
|
513 |
* |
|
514 |
* @param {?Object} wpMimeTypesObject Mime type object received from the server. |
|
515 |
* Extensions are keys separated by '|' and values are mime types associated with an extension. |
|
516 |
* |
|
517 |
* @return {?Array} An array of mime types or the parameter passed if it was "falsy". |
|
518 |
*/ |
|
519 |
function getMimeTypesArray(wpMimeTypesObject) { |
|
520 |
if (!wpMimeTypesObject) { |
|
521 |
return wpMimeTypesObject; |
|
522 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
523 |
return Object.entries(wpMimeTypesObject).map(([extensionsString, mime]) => { |
18 | 524 |
const [type] = mime.split('/'); |
525 |
const extensions = extensionsString.split('|'); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
526 |
return [mime, ...extensions.map(extension => `${type}/${extension}`)]; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
527 |
}).flat(); |
16 | 528 |
} |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
529 |
|
16 | 530 |
/** |
531 |
* Media Upload is used by audio, image, gallery, video, and file blocks to |
|
532 |
* handle uploading a media file when a file upload button is activated. |
|
533 |
* |
|
534 |
* TODO: future enhancement to add an upload indicator. |
|
535 |
* |
|
19 | 536 |
* @param {Object} $0 Parameters object passed to the function. |
537 |
* @param {?Array} $0.allowedTypes Array with the types of media that can be uploaded, if unset all types are allowed. |
|
538 |
* @param {?Object} $0.additionalData Additional data to include in the request. |
|
539 |
* @param {Array} $0.filesList List of files. |
|
540 |
* @param {?number} $0.maxUploadFileSize Maximum upload size in bytes allowed for the site. |
|
541 |
* @param {Function} $0.onError Function called when an error happens. |
|
542 |
* @param {Function} $0.onFileChange Function called each time a file or a temporary representation of the file is available. |
|
543 |
* @param {?Object} $0.wpAllowedMimeTypes List of allowed mime types and file extensions. |
|
16 | 544 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
545 |
async function uploadMedia({ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
546 |
allowedTypes, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
547 |
additionalData = {}, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
548 |
filesList, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
549 |
maxUploadFileSize, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
550 |
onError = noop, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
551 |
onFileChange, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
552 |
wpAllowedMimeTypes = null |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
553 |
}) { |
19 | 554 |
// Cast filesList to array. |
18 | 555 |
const files = [...filesList]; |
556 |
const filesSet = []; |
|
557 |
const setAndUpdateFiles = (idx, value) => { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
558 |
(0,external_wp_blob_namespaceObject.revokeBlobURL)(filesSet[idx]?.url); |
18 | 559 |
filesSet[idx] = value; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
560 |
onFileChange(filesSet.filter(Boolean)); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
561 |
}; |
18 | 562 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
563 |
// Allowed type specified by consumer. |
18 | 564 |
const isAllowedType = fileType => { |
565 |
if (!allowedTypes) { |
|
566 |
return true; |
|
567 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
568 |
return allowedTypes.some(allowedType => { |
18 | 569 |
// If a complete mimetype is specified verify if it matches exactly the mime type of the file. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
570 |
if (allowedType.includes('/')) { |
18 | 571 |
return allowedType === fileType; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
572 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
573 |
// Otherwise a general mime type is used and we should verify if the file mimetype starts with it. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
574 |
return fileType.startsWith(`${allowedType}/`); |
18 | 575 |
}); |
576 |
}; |
|
577 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
578 |
// Allowed types for the current WP_User. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
579 |
const allowedMimeTypesForUser = getMimeTypesArray(wpAllowedMimeTypes); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
580 |
const isAllowedMimeTypeForUser = fileType => { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
581 |
return allowedMimeTypesForUser.includes(fileType); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
582 |
}; |
18 | 583 |
const validFiles = []; |
584 |
for (const mediaFile of files) { |
|
585 |
// Verify if user is allowed to upload this mime type. |
|
586 |
// Defer to the server when type not detected. |
|
587 |
if (allowedMimeTypesForUser && mediaFile.type && !isAllowedMimeTypeForUser(mediaFile.type)) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
588 |
onError({ |
18 | 589 |
code: 'MIME_TYPE_NOT_ALLOWED_FOR_USER', |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
590 |
message: (0,external_wp_i18n_namespaceObject.sprintf)( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
591 |
// translators: %s: file name. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
592 |
(0,external_wp_i18n_namespaceObject.__)('%s: Sorry, you are not allowed to upload this file type.'), mediaFile.name), |
18 | 593 |
file: mediaFile |
594 |
}); |
|
595 |
continue; |
|
596 |
} |
|
597 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
598 |
// Check if the block supports this mime type. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
599 |
// Defer to the server when type not detected. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
600 |
if (mediaFile.type && !isAllowedType(mediaFile.type)) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
601 |
onError({ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
602 |
code: 'MIME_TYPE_NOT_SUPPORTED', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
603 |
message: (0,external_wp_i18n_namespaceObject.sprintf)( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
604 |
// translators: %s: file name. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
605 |
(0,external_wp_i18n_namespaceObject.__)('%s: Sorry, this file type is not supported here.'), mediaFile.name), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
606 |
file: mediaFile |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
607 |
}); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
608 |
continue; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
609 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
610 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
611 |
// Verify if file is greater than the maximum file upload size allowed for the site. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
612 |
if (maxUploadFileSize && mediaFile.size > maxUploadFileSize) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
613 |
onError({ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
614 |
code: 'SIZE_ABOVE_LIMIT', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
615 |
message: (0,external_wp_i18n_namespaceObject.sprintf)( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
616 |
// translators: %s: file name. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
617 |
(0,external_wp_i18n_namespaceObject.__)('%s: This file exceeds the maximum upload size for this site.'), mediaFile.name), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
618 |
file: mediaFile |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
619 |
}); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
620 |
continue; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
621 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
622 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
623 |
// Don't allow empty files to be uploaded. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
624 |
if (mediaFile.size <= 0) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
625 |
onError({ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
626 |
code: 'EMPTY_FILE', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
627 |
message: (0,external_wp_i18n_namespaceObject.sprintf)( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
628 |
// translators: %s: file name. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
629 |
(0,external_wp_i18n_namespaceObject.__)('%s: This file is empty.'), mediaFile.name), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
630 |
file: mediaFile |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
631 |
}); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
632 |
continue; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
633 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
634 |
validFiles.push(mediaFile); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
635 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
636 |
// Set temporary URL to create placeholder media file, this is replaced |
19 | 637 |
// with final file from media gallery when upload is `done` below. |
18 | 638 |
filesSet.push({ |
19 | 639 |
url: (0,external_wp_blob_namespaceObject.createBlobURL)(mediaFile) |
18 | 640 |
}); |
641 |
onFileChange(filesSet); |
|
642 |
} |
|
643 |
for (let idx = 0; idx < validFiles.length; ++idx) { |
|
644 |
const mediaFile = validFiles[idx]; |
|
645 |
try { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
646 |
var _savedMedia$caption$r; |
18 | 647 |
const savedMedia = await createMediaFromFile(mediaFile, additionalData); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
648 |
// eslint-disable-next-line camelcase |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
649 |
const { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
650 |
alt_text, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
651 |
source_url, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
652 |
...savedMediaProps |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
653 |
} = savedMedia; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
654 |
const mediaObject = { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
655 |
...savedMediaProps, |
18 | 656 |
alt: savedMedia.alt_text, |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
657 |
caption: (_savedMedia$caption$r = savedMedia.caption?.raw) !== null && _savedMedia$caption$r !== void 0 ? _savedMedia$caption$r : '', |
18 | 658 |
title: savedMedia.title.raw, |
659 |
url: savedMedia.source_url |
|
660 |
}; |
|
661 |
setAndUpdateFiles(idx, mediaObject); |
|
662 |
} catch (error) { |
|
663 |
// Reset to empty on failure. |
|
664 |
setAndUpdateFiles(idx, null); |
|
665 |
let message; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
666 |
if (error.message) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
667 |
message = error.message; |
18 | 668 |
} else { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
669 |
message = (0,external_wp_i18n_namespaceObject.sprintf)( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
670 |
// translators: %s: file name |
19 | 671 |
(0,external_wp_i18n_namespaceObject.__)('Error while uploading file %s to the media library.'), mediaFile.name); |
18 | 672 |
} |
673 |
onError({ |
|
674 |
code: 'GENERAL', |
|
675 |
message, |
|
676 |
file: mediaFile |
|
677 |
}); |
|
678 |
} |
|
679 |
} |
|
16 | 680 |
} |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
681 |
|
16 | 682 |
/** |
683 |
* @param {File} file Media File to Save. |
|
684 |
* @param {?Object} additionalData Additional data to include in the request. |
|
685 |
* |
|
686 |
* @return {Promise} Media Object Promise. |
|
687 |
*/ |
|
688 |
function createMediaFromFile(file, additionalData) { |
|
19 | 689 |
// Create upload payload. |
18 | 690 |
const data = new window.FormData(); |
16 | 691 |
data.append('file', file, file.name || file.type.replace('/', '.')); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
692 |
if (additionalData) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
693 |
Object.entries(additionalData).forEach(([key, value]) => data.append(key, value)); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
694 |
} |
18 | 695 |
return external_wp_apiFetch_default()({ |
16 | 696 |
path: '/wp/v2/media', |
697 |
body: data, |
|
698 |
method: 'POST' |
|
699 |
}); |
|
700 |
} |
|
701 |
||
19 | 702 |
;// CONCATENATED MODULE: ./node_modules/@wordpress/media-utils/build-module/utils/index.js |
16 | 703 |
|
704 |
||
19 | 705 |
;// CONCATENATED MODULE: ./node_modules/@wordpress/media-utils/build-module/index.js |
16 | 706 |
|
707 |
||
708 |
||
19 | 709 |
(window.wp = window.wp || {}).mediaUtils = __webpack_exports__; |
710 |
/******/ })() |
|
711 |
; |