2019-09-19 17:19:18 +02:00
this [ "wp" ] = this [ "wp" ] || { } ; this [ "wp" ] [ "mediaUtils" ] =
/******/ ( function ( modules ) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = { } ;
/******/
/******/ // The require function
/******/ function _ _webpack _require _ _ ( moduleId ) {
/******/
/******/ // Check if module is in cache
/******/ if ( installedModules [ moduleId ] ) {
/******/ return installedModules [ moduleId ] . exports ;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules [ moduleId ] = {
/******/ i : moduleId ,
/******/ l : false ,
/******/ exports : { }
/******/ } ;
/******/
/******/ // Execute the module function
/******/ modules [ moduleId ] . call ( module . exports , module , module . exports , _ _webpack _require _ _ ) ;
/******/
/******/ // Flag the module as loaded
/******/ module . l = true ;
/******/
/******/ // Return the exports of the module
/******/ return module . exports ;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ _ _webpack _require _ _ . m = modules ;
/******/
/******/ // expose the module cache
/******/ _ _webpack _require _ _ . c = installedModules ;
/******/
/******/ // define getter function for harmony exports
/******/ _ _webpack _require _ _ . d = function ( exports , name , getter ) {
/******/ if ( ! _ _webpack _require _ _ . o ( exports , name ) ) {
/******/ Object . defineProperty ( exports , name , { enumerable : true , get : getter } ) ;
/******/ }
/******/ } ;
/******/
/******/ // define __esModule on exports
/******/ _ _webpack _require _ _ . r = function ( exports ) {
/******/ if ( typeof Symbol !== 'undefined' && Symbol . toStringTag ) {
/******/ Object . defineProperty ( exports , Symbol . toStringTag , { value : 'Module' } ) ;
/******/ }
/******/ Object . defineProperty ( exports , '__esModule' , { value : true } ) ;
/******/ } ;
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ _ _webpack _require _ _ . t = function ( value , mode ) {
/******/ if ( mode & 1 ) value = _ _webpack _require _ _ ( value ) ;
/******/ if ( mode & 8 ) return value ;
/******/ if ( ( mode & 4 ) && typeof value === 'object' && value && value . _ _esModule ) return value ;
/******/ var ns = Object . create ( null ) ;
/******/ _ _webpack _require _ _ . r ( ns ) ;
/******/ Object . defineProperty ( ns , 'default' , { enumerable : true , value : value } ) ;
/******/ if ( mode & 2 && typeof value != 'string' ) for ( var key in value ) _ _webpack _require _ _ . d ( ns , key , function ( key ) { return value [ key ] ; } . bind ( null , key ) ) ;
/******/ return ns ;
/******/ } ;
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ _ _webpack _require _ _ . n = function ( module ) {
/******/ var getter = module && module . _ _esModule ?
/******/ function getDefault ( ) { return module [ 'default' ] ; } :
/******/ function getModuleExports ( ) { return module ; } ;
/******/ _ _webpack _require _ _ . d ( getter , 'a' , getter ) ;
/******/ return getter ;
/******/ } ;
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ _ _webpack _require _ _ . o = function ( object , property ) { return Object . prototype . hasOwnProperty . call ( object , property ) ; } ;
/******/
/******/ // __webpack_public_path__
/******/ _ _webpack _require _ _ . p = "" ;
/******/
/******/
/******/ // Load entry module and return exports
2021-05-20 14:20:04 +02:00
/******/ return _ _webpack _require _ _ ( _ _webpack _require _ _ . s = "Lb+8" ) ;
2019-09-19 17:19:18 +02:00
/******/ } )
/************************************************************************/
/******/ ( {
2021-05-20 14:20:04 +02:00
/***/ "GRId" :
2019-09-19 17:19:18 +02:00
/***/ ( function ( module , exports ) {
2021-01-28 03:04:13 +01:00
( function ( ) { module . exports = window [ "wp" ] [ "element" ] ; } ( ) ) ;
2019-09-19 17:19:18 +02:00
/***/ } ) ,
2021-05-20 14:20:04 +02:00
/***/ "Lb+8" :
2019-09-19 17:19:18 +02:00
/***/ ( function ( module , _ _webpack _exports _ _ , _ _webpack _require _ _ ) {
"use strict" ;
2020-06-29 13:50:29 +02:00
// ESM COMPAT FLAG
2019-09-19 17:19:18 +02:00
_ _webpack _require _ _ . r ( _ _webpack _exports _ _ ) ;
2020-06-29 13:50:29 +02:00
// EXPORTS
_ _webpack _require _ _ . d ( _ _webpack _exports _ _ , "MediaUpload" , function ( ) { return /* reexport */ media _upload ; } ) ;
_ _webpack _require _ _ . d ( _ _webpack _exports _ _ , "uploadMedia" , function ( ) { return /* reexport */ uploadMedia ; } ) ;
2021-01-28 03:04:13 +01:00
// EXTERNAL MODULE: external "lodash"
2021-05-20 14:20:04 +02:00
var external _lodash _ = _ _webpack _require _ _ ( "YLtl" ) ;
2019-09-19 17:19:18 +02:00
2021-01-28 03:04:13 +01:00
// EXTERNAL MODULE: external ["wp","element"]
2021-05-20 14:20:04 +02:00
var external _wp _element _ = _ _webpack _require _ _ ( "GRId" ) ;
2019-09-19 17:19:18 +02:00
2021-01-28 03:04:13 +01:00
// EXTERNAL MODULE: external ["wp","i18n"]
2021-05-20 14:20:04 +02:00
var external _wp _i18n _ = _ _webpack _require _ _ ( "l3Sj" ) ;
2019-09-19 17:19:18 +02:00
// CONCATENATED MODULE: ./node_modules/@wordpress/media-utils/build-module/components/media-upload/index.js
/ * *
* External dependencies
* /
/ * *
* WordPress dependencies
* /
2021-05-19 17:09:27 +02:00
const {
wp
} = window ;
const DEFAULT _EMPTY _GALLERY = [ ] ;
2020-02-18 00:02:14 +01:00
/ * *
* Prepares the Featured Image toolbars and frames .
*
* @ return { wp . media . view . MediaFrame . Select } The default media workflow .
* /
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
const getFeaturedImageMediaFrame = ( ) => {
2019-09-19 17:19:18 +02: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-05-19 17:09:27 +02:00
featuredImageToolbar ( toolbar ) {
2019-09-19 17:19:18 +02:00
this . createSelectToolbar ( toolbar , {
text : wp . media . view . l10n . setFeaturedImage ,
state : this . options . state
} ) ;
} ,
2020-02-18 00:02:14 +01:00
/ * *
* Handle the edit state requirements of selected media item .
*
* @ return { void }
* /
2021-05-19 17:09:27 +02:00
editState ( ) {
const selection = this . state ( 'featured-image' ) . get ( 'selection' ) ;
const view = new wp . media . view . EditImage ( {
2020-02-18 00:02:14 +01:00
model : selection . single ( ) ,
controller : this
} ) . render ( ) ; // Set the view to the EditImage frame using the selected image.
this . content . set ( view ) ; // After bringing in the frame, load the actual editor via an ajax call.
view . loadEditor ( ) ;
} ,
2019-09-19 17:19:18 +02:00
/ * *
* Create the default states .
*
* @ return { void }
* /
createStates : function createStates ( ) {
this . on ( 'toolbar:create:featured-image' , this . featuredImageToolbar , this ) ;
2020-02-18 00:02:14 +01:00
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-09-19 17:19:18 +02:00
}
} ) ;
2020-02-18 00:02:14 +01:00
} ;
/ * *
* Prepares the Gallery toolbars and frames .
*
2020-02-25 01:18:15 +01:00
* @ return { wp . media . view . MediaFrame . Post } The default media workflow .
2020-02-18 00:02:14 +01:00
* /
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
const getGalleryDetailsMediaFrame = ( ) => {
2019-09-19 17:19:18 +02:00
/ * *
* Custom gallery details frame .
*
* @ see https : //github.com/xwp/wp-core-media-widgets/blob/905edbccfc2a623b73a93dac803c5335519d7837/wp-admin/js/widgets/media-gallery-widget.js
* @ class GalleryDetailsMediaFrame
* @ class
* /
2020-02-25 01:18:15 +01:00
return wp . media . view . MediaFrame . Post . extend ( {
2020-02-18 00:02:14 +01:00
/ * *
* Set up gallery toolbar .
*
* @ return { void }
* /
2021-05-19 17:09:27 +02:00
galleryToolbar ( ) {
const editing = this . state ( ) . get ( 'editing' ) ;
2020-02-18 00:02:14 +01:00
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
* /
2021-05-19 17:09:27 +02:00
click ( ) {
const controller = this . controller ,
state = controller . state ( ) ;
2020-02-18 00:02:14 +01:00
controller . close ( ) ;
state . trigger ( 'update' , state . get ( 'library' ) ) ; // Restore and reset the default state.
controller . setState ( controller . options . state ) ;
controller . reset ( ) ;
}
2021-05-19 17:09:27 +02:00
2020-02-18 00:02:14 +01:00
}
}
} ) ) ;
} ,
/ * *
* Handle the edit state requirements of selected media item .
*
* @ return { void }
* /
2021-05-19 17:09:27 +02:00
editState ( ) {
const selection = this . state ( 'gallery' ) . get ( 'selection' ) ;
const view = new wp . media . view . EditImage ( {
2020-02-18 00:02:14 +01:00
model : selection . single ( ) ,
controller : this
} ) . render ( ) ; // Set the view to the EditImage frame using the selected image.
this . content . set ( view ) ; // After bringing in the frame, load the actual editor via an ajax call.
view . loadEditor ( ) ;
} ,
2019-09-19 17:19:18 +02:00
/ * *
* Create the default states .
*
* @ return { void }
* /
createStates : function createStates ( ) {
2020-02-18 00:02:14 +01:00
this . on ( 'toolbar:create:main-gallery' , this . galleryToolbar , this ) ;
this . on ( 'content:render:edit-image' , this . editState , this ) ;
2019-09-19 17:19:18 +02:00
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 ,
2021-01-28 03:04:13 +01:00
library : wp . media . query ( Object ( external _lodash _ [ "defaults" ] ) ( {
2019-09-19 17:19:18 +02:00
type : 'image'
} , this . options . library ) )
2020-02-18 00:02:14 +01:00
} ) , new wp . media . controller . EditImage ( {
model : this . options . editImage
2019-09-19 17:19:18 +02:00
} ) , new wp . media . controller . GalleryEdit ( {
library : this . options . selection ,
editing : this . options . editing ,
menu : 'gallery' ,
displaySettings : false ,
multiple : true
} ) , new wp . media . controller . GalleryAdd ( ) ] ) ;
}
} ) ;
} ; // the media library image object contains numerous attributes
// we only need this set to display the image in the library
2021-05-19 17:09:27 +02:00
const slimImageObject = img => {
const attrSet = [ 'sizes' , 'mime' , 'type' , 'subtype' , 'id' , 'url' , 'alt' , 'link' , 'caption' ] ;
2021-01-28 03:04:13 +01:00
return Object ( external _lodash _ [ "pick" ] ) ( img , attrSet ) ;
2019-09-19 17:19:18 +02:00
} ;
2021-05-19 17:09:27 +02:00
const getAttachmentsCollection = ids => {
2019-09-19 17:19:18 +02:00
return wp . media . query ( {
order : 'ASC' ,
orderby : 'post__in' ,
post _ _in : ids ,
posts _per _page : - 1 ,
query : true ,
type : 'image'
} ) ;
} ;
2021-05-19 17:09:27 +02:00
class media _upload _MediaUpload extends external _wp _element _ [ "Component" ] {
2021-11-15 13:50:17 +01:00
constructor ( _ref ) {
let {
allowedTypes ,
gallery = false ,
unstableFeaturedImageFlow = false ,
modalClass ,
multiple = false ,
title = Object ( external _wp _i18n _ [ "__" ] ) ( 'Select or Upload Media' )
} = _ref ;
2021-05-19 17:09:27 +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-09-19 17:19:18 +02:00
if ( gallery ) {
2021-05-19 17:09:27 +02:00
this . buildAndSetGalleryFrame ( ) ;
2019-09-19 17:19:18 +02:00
} else {
2021-05-19 17:09:27 +02:00
const frameConfig = {
title ,
multiple
2019-09-19 17:19:18 +02:00
} ;
if ( ! ! allowedTypes ) {
frameConfig . library = {
type : allowedTypes
} ;
}
2021-05-19 17:09:27 +02:00
this . frame = wp . media ( frameConfig ) ;
2019-09-19 17:19:18 +02:00
}
if ( modalClass ) {
2021-05-19 17:09:27 +02:00
this . frame . $el . addClass ( modalClass ) ;
2019-09-19 17:19:18 +02:00
}
if ( unstableFeaturedImageFlow ) {
2021-05-19 17:09:27 +02:00
this . buildAndSetFeatureImageFrame ( ) ;
2019-09-19 17:19:18 +02:00
}
2021-05-19 17:09:27 +02:00
this . initializeListeners ( ) ;
2019-09-19 17:19:18 +02:00
}
2021-05-19 17:09:27 +02:00
initializeListeners ( ) {
// When an image is selected in the media frame...
this . frame . on ( 'select' , this . onSelect ) ;
this . frame . on ( 'update' , this . onUpdate ) ;
this . frame . on ( 'open' , this . onOpen ) ;
this . frame . on ( 'close' , this . onClose ) ;
}
/ * *
* Sets the Gallery frame and initializes listeners .
*
* @ return { void }
* /
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
buildAndSetGalleryFrame ( ) {
const {
addToGallery = false ,
allowedTypes ,
multiple = false ,
value = DEFAULT _EMPTY _GALLERY
} = this . props ; // If the value did not changed there is no need to rebuild the frame,
// we can continue to use the existing one.
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
if ( value === this . lastGalleryValue ) {
return ;
2019-09-19 17:19:18 +02:00
}
2020-02-18 00:02:14 +01:00
2021-05-19 17:09:27 +02:00
this . lastGalleryValue = value ; // If a frame already existed remove it.
if ( this . frame ) {
2019-09-19 17:19:18 +02:00
this . frame . remove ( ) ;
}
2021-05-19 17:09:27 +02:00
let currentState ;
if ( addToGallery ) {
currentState = 'gallery-library' ;
} else {
currentState = value && value . length ? 'gallery-edit' : 'gallery' ;
2019-09-19 17:19:18 +02:00
}
2021-05-19 17:09:27 +02:00
if ( ! this . GalleryDetailsMediaFrame ) {
this . GalleryDetailsMediaFrame = getGalleryDetailsMediaFrame ( ) ;
2019-09-19 17:19:18 +02:00
}
2021-04-15 17:19:43 +02:00
2021-05-19 17:09:27 +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 ,
editing : value && value . length ? true : false
} ) ;
wp . media . frame = this . frame ;
this . initializeListeners ( ) ;
}
/ * *
* Initializes the Media Library requirements for the featured image flow .
*
* @ return { void }
* /
2019-09-19 17:19:18 +02:00
2021-04-15 17:19:43 +02:00
2021-05-19 17:09:27 +02:00
buildAndSetFeatureImageFrame ( ) {
const featuredImageFrame = getFeaturedImageMediaFrame ( ) ;
const attachments = getAttachmentsCollection ( this . props . value ) ;
const selection = new wp . media . model . Selection ( attachments . models , {
props : attachments . props . toJSON ( )
} ) ;
this . frame = new featuredImageFrame ( {
mimeType : this . props . allowedTypes ,
state : 'featured-image' ,
multiple : this . props . multiple ,
selection ,
editing : this . props . value ? true : false
} ) ;
wp . media . frame = this . frame ;
}
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
componentWillUnmount ( ) {
this . frame . remove ( ) ;
}
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
onUpdate ( selections ) {
const {
onSelect ,
multiple = false
} = this . props ;
const state = this . frame . state ( ) ;
const selectedImages = selections || state . get ( 'selection' ) ;
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
if ( ! selectedImages || ! selectedImages . models . length ) {
return ;
2019-09-19 17:19:18 +02:00
}
2021-05-19 17:09:27 +02:00
if ( multiple ) {
onSelect ( selectedImages . models . map ( model => slimImageObject ( model . toJSON ( ) ) ) ) ;
} else {
onSelect ( slimImageObject ( selectedImages . models [ 0 ] . toJSON ( ) ) ) ;
2019-09-19 17:19:18 +02:00
}
2021-05-19 17:09:27 +02:00
}
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
onSelect ( ) {
const {
onSelect ,
multiple = false
} = this . props ; // Get media attachment details from the frame state
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
const attachment = this . frame . state ( ) . get ( 'selection' ) . toJSON ( ) ;
onSelect ( multiple ? attachment : attachment [ 0 ] ) ;
}
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
onOpen ( ) {
var _this$props$value ;
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
this . updateCollection ( ) ; // Handle both this.props.value being either (number[]) multiple ids
// (for galleries) or a (number) singular id (e.g. image block).
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
const hasMedia = Array . isArray ( this . props . value ) ? ! ! ( ( _this$props$value = this . props . value ) !== null && _this$props$value !== void 0 && _this$props$value . length ) : ! ! this . props . value ;
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
if ( ! hasMedia ) {
return ;
}
2019-09-19 17:19:18 +02:00
2021-11-08 15:29:21 +01:00
const isGallery = this . props . gallery ;
const selection = this . frame . state ( ) . get ( 'selection' ) ;
if ( ! isGallery ) {
2021-05-19 17:09:27 +02:00
Object ( external _lodash _ [ "castArray" ] ) ( this . props . value ) . forEach ( id => {
selection . add ( wp . media . attachment ( id ) ) ;
} ) ;
2021-11-08 15:29:21 +01:00
} // Load the images so they are available in the media modal.
const attachments = getAttachmentsCollection ( Object ( external _lodash _ [ "castArray" ] ) ( this . props . value ) ) ; // Once attachments are loaded, set the current selection.
2019-09-19 17:19:18 +02:00
2021-11-08 15:29:21 +01:00
attachments . more ( ) . done ( function ( ) {
var _attachments$models ;
2019-09-19 17:19:18 +02:00
2021-11-08 15:29:21 +01:00
if ( isGallery && attachments !== null && attachments !== void 0 && ( _attachments$models = attachments . models ) !== null && _attachments$models !== void 0 && _attachments$models . length ) {
selection . add ( attachments . models ) ;
}
} ) ;
2021-05-19 17:09:27 +02:00
}
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
onClose ( ) {
const {
onClose
} = this . props ;
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
if ( onClose ) {
onClose ( ) ;
}
}
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
updateCollection ( ) {
const frameContent = this . frame . content . get ( ) ;
2021-04-15 17:19:43 +02:00
2021-05-19 17:09:27 +02:00
if ( frameContent && frameContent . collection ) {
const collection = frameContent . collection ; // clean all attachments we have in memory.
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
collection . toArray ( ) . forEach ( model => model . trigger ( 'destroy' , model ) ) ; // reset has more flag, if library had small amount of items all items may have been loaded before.
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
collection . mirroring . _hasMore = true ; // request items
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
collection . more ( ) ;
}
}
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
openModal ( ) {
if ( this . props . gallery ) {
this . buildAndSetGalleryFrame ( ) ;
}
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
this . frame . open ( ) ;
}
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
render ( ) {
return this . props . render ( {
open : this . openModal
} ) ;
}
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
}
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
/* harmony default export */ var media _upload = ( media _upload _MediaUpload ) ;
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
// CONCATENATED MODULE: ./node_modules/@wordpress/media-utils/build-module/components/index.js
2019-09-19 17:19:18 +02:00
2020-02-06 22:03:31 +01:00
2021-05-19 17:09:27 +02:00
// EXTERNAL MODULE: external ["wp","apiFetch"]
2021-05-20 14:20:04 +02:00
var external _wp _apiFetch _ = _ _webpack _require _ _ ( "ywyh" ) ;
2021-05-19 17:09:27 +02:00
var external _wp _apiFetch _default = /*#__PURE__*/ _ _webpack _require _ _ . n ( external _wp _apiFetch _ ) ;
2020-02-06 22:03:31 +01:00
2021-05-19 17:09:27 +02:00
// EXTERNAL MODULE: external ["wp","blob"]
2021-05-20 14:20:04 +02:00
var external _wp _blob _ = _ _webpack _require _ _ ( "xTGt" ) ;
2020-06-26 15:33:47 +02:00
2021-05-19 17:09:27 +02:00
// CONCATENATED MODULE: ./node_modules/@wordpress/media-utils/build-module/utils/upload-media.js
2020-06-26 15:33:47 +02:00
2019-09-19 17:19:18 +02:00
/ * *
* External dependencies
* /
/ * *
* WordPress dependencies
* /
/ * *
* 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 ;
}
2021-05-19 17:09:27 +02:00
return Object ( external _lodash _ [ "flatMap" ] ) ( wpMimeTypesObject , ( mime , extensionsString ) => {
const [ type ] = mime . split ( '/' ) ;
const extensions = extensionsString . split ( '|' ) ;
return [ mime , ... Object ( external _lodash _ [ "map" ] ) ( extensions , extension => ` ${ type } / ${ extension } ` ) ] ;
2019-09-19 17:19:18 +02:00
} ) ;
}
/ * *
* 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 .
*
2021-11-08 15:29:21 +01:00
* @ 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 .
2019-09-19 17:19:18 +02:00
* /
2021-11-15 13:50:17 +01:00
async function uploadMedia ( _ref ) {
let {
allowedTypes ,
additionalData = { } ,
filesList ,
maxUploadFileSize ,
onError = external _lodash _ [ "noop" ] ,
onFileChange ,
wpAllowedMimeTypes = null
} = _ref ;
2021-05-19 17:09:27 +02:00
// Cast filesList to array
const files = [ ... filesList ] ;
const filesSet = [ ] ;
const setAndUpdateFiles = ( idx , value ) => {
Object ( external _wp _blob _ [ "revokeBlobURL" ] ) ( Object ( external _lodash _ [ "get" ] ) ( filesSet , [ idx , 'url' ] ) ) ;
filesSet [ idx ] = value ;
onFileChange ( Object ( external _lodash _ [ "compact" ] ) ( filesSet ) ) ;
} ; // Allowed type specified by consumer
const isAllowedType = fileType => {
if ( ! allowedTypes ) {
return true ;
}
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
return Object ( external _lodash _ [ "some" ] ) ( allowedTypes , allowedType => {
// If a complete mimetype is specified verify if it matches exactly the mime type of the file.
if ( Object ( external _lodash _ [ "includes" ] ) ( allowedType , '/' ) ) {
return allowedType === fileType ;
} // Otherwise a general mime type is used and we should verify if the file mimetype starts with it.
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
return Object ( external _lodash _ [ "startsWith" ] ) ( fileType , ` ${ allowedType } / ` ) ;
} ) ;
} ; // Allowed types for the current WP_User
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
const allowedMimeTypesForUser = getMimeTypesArray ( wpAllowedMimeTypes ) ;
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
const isAllowedMimeTypeForUser = fileType => {
return Object ( external _lodash _ [ "includes" ] ) ( allowedMimeTypesForUser , fileType ) ;
} ; // Build the error message including the filename
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
const triggerError = error => {
error . message = [ Object ( external _wp _element _ [ "createElement" ] ) ( "strong" , {
key : "filename"
} , error . file . name ) , ': ' , error . message ] ;
onError ( error ) ;
} ;
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
const validFiles = [ ] ;
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
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 ) ) {
triggerError ( {
code : 'MIME_TYPE_NOT_ALLOWED_FOR_USER' ,
2021-11-08 15:29:21 +01:00
message : Object ( external _wp _i18n _ [ "__" ] ) ( 'Sorry, you are not allowed to upload this file type.' ) ,
2021-05-19 17:09:27 +02:00
file : mediaFile
} ) ;
continue ;
} // Check if the block supports this mime type.
// Defer to the server when type not detected.
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
if ( mediaFile . type && ! isAllowedType ( mediaFile . type ) ) {
triggerError ( {
code : 'MIME_TYPE_NOT_SUPPORTED' ,
message : Object ( external _wp _i18n _ [ "__" ] ) ( 'Sorry, this file type is not supported here.' ) ,
file : mediaFile
} ) ;
continue ;
} // verify if file is greater than the maximum file upload size allowed for the site.
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
if ( maxUploadFileSize && mediaFile . size > maxUploadFileSize ) {
triggerError ( {
code : 'SIZE_ABOVE_LIMIT' ,
message : Object ( external _wp _i18n _ [ "__" ] ) ( 'This file exceeds the maximum upload size for this site.' ) ,
file : mediaFile
} ) ;
continue ;
} // Don't allow empty files to be uploaded.
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
if ( mediaFile . size <= 0 ) {
triggerError ( {
code : 'EMPTY_FILE' ,
message : Object ( external _wp _i18n _ [ "__" ] ) ( 'This file is empty.' ) ,
file : mediaFile
} ) ;
continue ;
}
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
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
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
filesSet . push ( {
url : Object ( external _wp _blob _ [ "createBlobURL" ] ) ( mediaFile )
} ) ;
onFileChange ( filesSet ) ;
}
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
for ( let idx = 0 ; idx < validFiles . length ; ++ idx ) {
const mediaFile = validFiles [ idx ] ;
2019-09-19 17:19:18 +02:00
2021-05-19 17:09:27 +02:00
try {
const savedMedia = await createMediaFromFile ( mediaFile , additionalData ) ;
const mediaObject = { ... Object ( external _lodash _ [ "omit" ] ) ( savedMedia , [ 'alt_text' , 'source_url' ] ) ,
alt : savedMedia . alt _text ,
caption : Object ( external _lodash _ [ "get" ] ) ( savedMedia , [ 'caption' , 'raw' ] , '' ) ,
title : savedMedia . title . raw ,
url : savedMedia . source _url
} ;
setAndUpdateFiles ( idx , mediaObject ) ;
} catch ( error ) {
// Reset to empty on failure.
setAndUpdateFiles ( idx , null ) ;
let message ;
if ( Object ( external _lodash _ [ "has" ] ) ( error , [ 'message' ] ) ) {
message = Object ( external _lodash _ [ "get" ] ) ( error , [ 'message' ] ) ;
} else {
message = Object ( external _wp _i18n _ [ "sprintf" ] ) ( // translators: %s: file name
Object ( external _wp _i18n _ [ "__" ] ) ( 'Error while uploading file %s to the media library.' ) , mediaFile . name ) ;
2019-09-19 17:19:18 +02:00
}
2021-05-19 17:09:27 +02:00
onError ( {
code : 'GENERAL' ,
message ,
file : mediaFile
} ) ;
}
}
2019-09-19 17:19:18 +02:00
}
2021-05-19 17:09:27 +02:00
/ * *
* @ param { File } file Media File to Save .
* @ param { ? Object } additionalData Additional data to include in the request .
*
* @ return { Promise } Media Object Promise .
* /
2019-09-19 17:19:18 +02:00
function createMediaFromFile ( file , additionalData ) {
// Create upload payload
2021-05-19 17:09:27 +02:00
const data = new window . FormData ( ) ;
2019-09-19 17:19:18 +02:00
data . append ( 'file' , file , file . name || file . type . replace ( '/' , '.' ) ) ;
2021-05-19 17:09:27 +02:00
Object ( external _lodash _ [ "forEach" ] ) ( additionalData , ( value , key ) => data . append ( key , value ) ) ;
2021-01-28 03:04:13 +01:00
return external _wp _apiFetch _default ( ) ( {
2019-09-19 17:19:18 +02:00
path : '/wp/v2/media' ,
body : data ,
method : 'POST'
} ) ;
}
// CONCATENATED MODULE: ./node_modules/@wordpress/media-utils/build-module/utils/index.js
// CONCATENATED MODULE: ./node_modules/@wordpress/media-utils/build-module/index.js
2021-05-20 14:20:04 +02:00
/***/ } ) ,
/***/ "YLtl" :
/***/ ( function ( module , exports ) {
( function ( ) { module . exports = window [ "lodash" ] ; } ( ) ) ;
/***/ } ) ,
/***/ "l3Sj" :
/***/ ( function ( module , exports ) {
( function ( ) { module . exports = window [ "wp" ] [ "i18n" ] ; } ( ) ) ;
/***/ } ) ,
/***/ "xTGt" :
/***/ ( function ( module , exports ) {
( function ( ) { module . exports = window [ "wp" ] [ "blob" ] ; } ( ) ) ;
/***/ } ) ,
/***/ "ywyh" :
/***/ ( function ( module , exports ) {
( function ( ) { module . exports = window [ "wp" ] [ "apiFetch" ] ; } ( ) ) ;
2019-09-19 17:19:18 +02:00
/***/ } )
/******/ } ) ;