[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/js/dist/ -> media-utils.js (source)

   1  /******/ (() => { // webpackBootstrap
   2  /******/     "use strict";
   3  /******/     // The require scope
   4  /******/     var __webpack_require__ = {};
   5  /******/     
   6  /************************************************************************/
   7  /******/     /* webpack/runtime/compat get default export */
   8  /******/     (() => {
   9  /******/         // getDefaultExport function for compatibility with non-harmony modules
  10  /******/         __webpack_require__.n = (module) => {
  11  /******/             var getter = module && module.__esModule ?
  12  /******/                 () => (module['default']) :
  13  /******/                 () => (module);
  14  /******/             __webpack_require__.d(getter, { a: getter });
  15  /******/             return getter;
  16  /******/         };
  17  /******/     })();
  18  /******/     
  19  /******/     /* webpack/runtime/define property getters */
  20  /******/     (() => {
  21  /******/         // define getter functions for harmony exports
  22  /******/         __webpack_require__.d = (exports, definition) => {
  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  /******/         };
  29  /******/     })();
  30  /******/     
  31  /******/     /* webpack/runtime/hasOwnProperty shorthand */
  32  /******/     (() => {
  33  /******/         __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
  34  /******/     })();
  35  /******/     
  36  /******/     /* webpack/runtime/make namespace object */
  37  /******/     (() => {
  38  /******/         // define __esModule on exports
  39  /******/         __webpack_require__.r = (exports) => {
  40  /******/             if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  41  /******/                 Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  42  /******/             }
  43  /******/             Object.defineProperty(exports, '__esModule', { value: true });
  44  /******/         };
  45  /******/     })();
  46  /******/     
  47  /************************************************************************/
  48  var __webpack_exports__ = {};
  49  // ESM COMPAT FLAG
  50  __webpack_require__.r(__webpack_exports__);
  51  
  52  // EXPORTS
  53  __webpack_require__.d(__webpack_exports__, {
  54    MediaUpload: () => (/* reexport */ media_upload),
  55    privateApis: () => (/* reexport */ privateApis),
  56    transformAttachment: () => (/* reexport */ transformAttachment),
  57    uploadMedia: () => (/* reexport */ uploadMedia),
  58    validateFileSize: () => (/* reexport */ validateFileSize),
  59    validateMimeType: () => (/* reexport */ validateMimeType),
  60    validateMimeTypeForUser: () => (/* reexport */ validateMimeTypeForUser)
  61  });
  62  
  63  ;// external ["wp","element"]
  64  const external_wp_element_namespaceObject = window["wp"]["element"];
  65  ;// external ["wp","i18n"]
  66  const external_wp_i18n_namespaceObject = window["wp"]["i18n"];
  67  ;// ./node_modules/@wordpress/media-utils/build-module/components/media-upload/index.js
  68  /**
  69   * WordPress dependencies
  70   */
  71  
  72  
  73  const DEFAULT_EMPTY_GALLERY = [];
  74  
  75  /**
  76   * Prepares the Featured Image toolbars and frames.
  77   *
  78   * @return {window.wp.media.view.MediaFrame.Select} The default media workflow.
  79   */
  80  const getFeaturedImageMediaFrame = () => {
  81    const {
  82      wp
  83    } = window;
  84    return wp.media.view.MediaFrame.Select.extend({
  85      /**
  86       * Enables the Set Featured Image Button.
  87       *
  88       * @param {Object} toolbar toolbar for featured image state
  89       * @return {void}
  90       */
  91      featuredImageToolbar(toolbar) {
  92        this.createSelectToolbar(toolbar, {
  93          text: wp.media.view.l10n.setFeaturedImage,
  94          state: this.options.state
  95        });
  96      },
  97      /**
  98       * Handle the edit state requirements of selected media item.
  99       *
 100       * @return {void}
 101       */
 102      editState() {
 103        const selection = this.state('featured-image').get('selection');
 104        const view = new wp.media.view.EditImage({
 105          model: selection.single(),
 106          controller: this
 107        }).render();
 108  
 109        // Set the view to the EditImage frame using the selected image.
 110        this.content.set(view);
 111  
 112        // After bringing in the frame, load the actual editor via an ajax call.
 113        view.loadEditor();
 114      },
 115      /**
 116       * Create the default states.
 117       *
 118       * @return {void}
 119       */
 120      createStates: function createStates() {
 121        this.on('toolbar:create:featured-image', this.featuredImageToolbar, this);
 122        this.on('content:render:edit-image', this.editState, this);
 123        this.states.add([new wp.media.controller.FeaturedImage(), new wp.media.controller.EditImage({
 124          model: this.options.editImage
 125        })]);
 126      }
 127    });
 128  };
 129  
 130  /**
 131   * Prepares the default frame for selecting a single media item.
 132   *
 133   * @return {window.wp.media.view.MediaFrame.Select} The default media workflow.
 134   */
 135  const getSingleMediaFrame = () => {
 136    const {
 137      wp
 138    } = window;
 139  
 140    // Extend the default Select frame, and use the same `createStates` method as in core,
 141    // but with the addition of `filterable: 'uploaded'` to the Library state, so that
 142    // the user can filter the media library by uploaded media.
 143    return wp.media.view.MediaFrame.Select.extend({
 144      /**
 145       * Create the default states on the frame.
 146       */
 147      createStates() {
 148        const options = this.options;
 149        if (this.options.states) {
 150          return;
 151        }
 152  
 153        // Add the default states.
 154        this.states.add([
 155        // Main states.
 156        new wp.media.controller.Library({
 157          library: wp.media.query(options.library),
 158          multiple: options.multiple,
 159          title: options.title,
 160          priority: 20,
 161          filterable: 'uploaded' // Allow filtering by uploaded images.
 162        }), new wp.media.controller.EditImage({
 163          model: options.editImage
 164        })]);
 165      }
 166    });
 167  };
 168  
 169  /**
 170   * Prepares the Gallery toolbars and frames.
 171   *
 172   * @return {window.wp.media.view.MediaFrame.Post} The default media workflow.
 173   */
 174  const getGalleryDetailsMediaFrame = () => {
 175    const {
 176      wp
 177    } = window;
 178    /**
 179     * Custom gallery details frame.
 180     *
 181     * @see https://github.com/xwp/wp-core-media-widgets/blob/905edbccfc2a623b73a93dac803c5335519d7837/wp-admin/js/widgets/media-gallery-widget.js
 182     * @class GalleryDetailsMediaFrame
 183     * @class
 184     */
 185    return wp.media.view.MediaFrame.Post.extend({
 186      /**
 187       * Set up gallery toolbar.
 188       *
 189       * @return {void}
 190       */
 191      galleryToolbar() {
 192        const editing = this.state().get('editing');
 193        this.toolbar.set(new wp.media.view.Toolbar({
 194          controller: this,
 195          items: {
 196            insert: {
 197              style: 'primary',
 198              text: editing ? wp.media.view.l10n.updateGallery : wp.media.view.l10n.insertGallery,
 199              priority: 80,
 200              requires: {
 201                library: true
 202              },
 203              /**
 204               * @fires wp.media.controller.State#update
 205               */
 206              click() {
 207                const controller = this.controller,
 208                  state = controller.state();
 209                controller.close();
 210                state.trigger('update', state.get('library'));
 211  
 212                // Restore and reset the default state.
 213                controller.setState(controller.options.state);
 214                controller.reset();
 215              }
 216            }
 217          }
 218        }));
 219      },
 220      /**
 221       * Handle the edit state requirements of selected media item.
 222       *
 223       * @return {void}
 224       */
 225      editState() {
 226        const selection = this.state('gallery').get('selection');
 227        const view = new wp.media.view.EditImage({
 228          model: selection.single(),
 229          controller: this
 230        }).render();
 231  
 232        // Set the view to the EditImage frame using the selected image.
 233        this.content.set(view);
 234  
 235        // After bringing in the frame, load the actual editor via an ajax call.
 236        view.loadEditor();
 237      },
 238      /**
 239       * Create the default states.
 240       *
 241       * @return {void}
 242       */
 243      createStates: function createStates() {
 244        this.on('toolbar:create:main-gallery', this.galleryToolbar, this);
 245        this.on('content:render:edit-image', this.editState, this);
 246        this.states.add([new wp.media.controller.Library({
 247          id: 'gallery',
 248          title: wp.media.view.l10n.createGalleryTitle,
 249          priority: 40,
 250          toolbar: 'main-gallery',
 251          filterable: 'uploaded',
 252          multiple: 'add',
 253          editable: false,
 254          library: wp.media.query({
 255            type: 'image',
 256            ...this.options.library
 257          })
 258        }), new wp.media.controller.EditImage({
 259          model: this.options.editImage
 260        }), new wp.media.controller.GalleryEdit({
 261          library: this.options.selection,
 262          editing: this.options.editing,
 263          menu: 'gallery',
 264          displaySettings: false,
 265          multiple: true
 266        }), new wp.media.controller.GalleryAdd()]);
 267      }
 268    });
 269  };
 270  
 271  // The media library image object contains numerous attributes
 272  // we only need this set to display the image in the library.
 273  const slimImageObject = img => {
 274    const attrSet = ['sizes', 'mime', 'type', 'subtype', 'id', 'url', 'alt', 'link', 'caption'];
 275    return attrSet.reduce((result, key) => {
 276      if (img?.hasOwnProperty(key)) {
 277        result[key] = img[key];
 278      }
 279      return result;
 280    }, {});
 281  };
 282  const getAttachmentsCollection = ids => {
 283    const {
 284      wp
 285    } = window;
 286    return wp.media.query({
 287      order: 'ASC',
 288      orderby: 'post__in',
 289      post__in: ids,
 290      posts_per_page: -1,
 291      query: true,
 292      type: 'image'
 293    });
 294  };
 295  class MediaUpload extends external_wp_element_namespaceObject.Component {
 296    constructor() {
 297      super(...arguments);
 298      this.openModal = this.openModal.bind(this);
 299      this.onOpen = this.onOpen.bind(this);
 300      this.onSelect = this.onSelect.bind(this);
 301      this.onUpdate = this.onUpdate.bind(this);
 302      this.onClose = this.onClose.bind(this);
 303    }
 304    initializeListeners() {
 305      // When an image is selected in the media frame...
 306      this.frame.on('select', this.onSelect);
 307      this.frame.on('update', this.onUpdate);
 308      this.frame.on('open', this.onOpen);
 309      this.frame.on('close', this.onClose);
 310    }
 311  
 312    /**
 313     * Sets the Gallery frame and initializes listeners.
 314     *
 315     * @return {void}
 316     */
 317    buildAndSetGalleryFrame() {
 318      const {
 319        addToGallery = false,
 320        allowedTypes,
 321        multiple = false,
 322        value = DEFAULT_EMPTY_GALLERY
 323      } = this.props;
 324  
 325      // If the value did not changed there is no need to rebuild the frame,
 326      // we can continue to use the existing one.
 327      if (value === this.lastGalleryValue) {
 328        return;
 329      }
 330      const {
 331        wp
 332      } = window;
 333      this.lastGalleryValue = value;
 334  
 335      // If a frame already existed remove it.
 336      if (this.frame) {
 337        this.frame.remove();
 338      }
 339      let currentState;
 340      if (addToGallery) {
 341        currentState = 'gallery-library';
 342      } else {
 343        currentState = value && value.length ? 'gallery-edit' : 'gallery';
 344      }
 345      if (!this.GalleryDetailsMediaFrame) {
 346        this.GalleryDetailsMediaFrame = getGalleryDetailsMediaFrame();
 347      }
 348      const attachments = getAttachmentsCollection(value);
 349      const selection = new wp.media.model.Selection(attachments.models, {
 350        props: attachments.props.toJSON(),
 351        multiple
 352      });
 353      this.frame = new this.GalleryDetailsMediaFrame({
 354        mimeType: allowedTypes,
 355        state: currentState,
 356        multiple,
 357        selection,
 358        editing: !!value?.length
 359      });
 360      wp.media.frame = this.frame;
 361      this.initializeListeners();
 362    }
 363  
 364    /**
 365     * Initializes the Media Library requirements for the featured image flow.
 366     *
 367     * @return {void}
 368     */
 369    buildAndSetFeatureImageFrame() {
 370      const {
 371        wp
 372      } = window;
 373      const {
 374        value: featuredImageId,
 375        multiple,
 376        allowedTypes
 377      } = this.props;
 378      const featuredImageFrame = getFeaturedImageMediaFrame();
 379      const attachments = getAttachmentsCollection(featuredImageId);
 380      const selection = new wp.media.model.Selection(attachments.models, {
 381        props: attachments.props.toJSON()
 382      });
 383      this.frame = new featuredImageFrame({
 384        mimeType: allowedTypes,
 385        state: 'featured-image',
 386        multiple,
 387        selection,
 388        editing: featuredImageId
 389      });
 390      wp.media.frame = this.frame;
 391      // In order to select the current featured image when opening
 392      // the media library we have to set the appropriate settings.
 393      // Currently they are set in php for the post editor, but
 394      // not for site editor.
 395      wp.media.view.settings.post = {
 396        ...wp.media.view.settings.post,
 397        featuredImageId: featuredImageId || -1
 398      };
 399    }
 400  
 401    /**
 402     * Initializes the Media Library requirements for the single image flow.
 403     *
 404     * @return {void}
 405     */
 406    buildAndSetSingleMediaFrame() {
 407      const {
 408        wp
 409      } = window;
 410      const {
 411        allowedTypes,
 412        multiple = false,
 413        title = (0,external_wp_i18n_namespaceObject.__)('Select or Upload Media'),
 414        value
 415      } = this.props;
 416      const frameConfig = {
 417        title,
 418        multiple
 419      };
 420      if (!!allowedTypes) {
 421        frameConfig.library = {
 422          type: allowedTypes
 423        };
 424      }
 425  
 426      // If a frame already exists, remove it.
 427      if (this.frame) {
 428        this.frame.remove();
 429      }
 430      const singleImageFrame = getSingleMediaFrame();
 431      const attachments = getAttachmentsCollection(value);
 432      const selection = new wp.media.model.Selection(attachments.models, {
 433        props: attachments.props.toJSON()
 434      });
 435      this.frame = new singleImageFrame({
 436        mimeType: allowedTypes,
 437        multiple,
 438        selection,
 439        ...frameConfig
 440      });
 441      wp.media.frame = this.frame;
 442    }
 443    componentWillUnmount() {
 444      this.frame?.remove();
 445    }
 446    onUpdate(selections) {
 447      const {
 448        onSelect,
 449        multiple = false
 450      } = this.props;
 451      const state = this.frame.state();
 452      const selectedImages = selections || state.get('selection');
 453      if (!selectedImages || !selectedImages.models.length) {
 454        return;
 455      }
 456      if (multiple) {
 457        onSelect(selectedImages.models.map(model => slimImageObject(model.toJSON())));
 458      } else {
 459        onSelect(slimImageObject(selectedImages.models[0].toJSON()));
 460      }
 461    }
 462    onSelect() {
 463      const {
 464        onSelect,
 465        multiple = false
 466      } = this.props;
 467      // Get media attachment details from the frame state.
 468      const attachment = this.frame.state().get('selection').toJSON();
 469      onSelect(multiple ? attachment : attachment[0]);
 470    }
 471    onOpen() {
 472      const {
 473        wp
 474      } = window;
 475      const {
 476        value
 477      } = this.props;
 478      this.updateCollection();
 479  
 480      //Handle active tab in media model on model open.
 481      if (this.props.mode) {
 482        this.frame.content.mode(this.props.mode);
 483      }
 484  
 485      // Handle both this.props.value being either (number[]) multiple ids
 486      // (for galleries) or a (number) singular id (e.g. image block).
 487      const hasMedia = Array.isArray(value) ? !!value?.length : !!value;
 488      if (!hasMedia) {
 489        return;
 490      }
 491      const isGallery = this.props.gallery;
 492      const selection = this.frame.state().get('selection');
 493      const valueArray = Array.isArray(value) ? value : [value];
 494      if (!isGallery) {
 495        valueArray.forEach(id => {
 496          selection.add(wp.media.attachment(id));
 497        });
 498      }
 499  
 500      // Load the images so they are available in the media modal.
 501      const attachments = getAttachmentsCollection(valueArray);
 502  
 503      // Once attachments are loaded, set the current selection.
 504      attachments.more().done(function () {
 505        if (isGallery && attachments?.models?.length) {
 506          selection.add(attachments.models);
 507        }
 508      });
 509    }
 510    onClose() {
 511      const {
 512        onClose
 513      } = this.props;
 514      if (onClose) {
 515        onClose();
 516      }
 517      this.frame.detach();
 518    }
 519    updateCollection() {
 520      const frameContent = this.frame.content.get();
 521      if (frameContent && frameContent.collection) {
 522        const collection = frameContent.collection;
 523  
 524        // Clean all attachments we have in memory.
 525        collection.toArray().forEach(model => model.trigger('destroy', model));
 526  
 527        // Reset has more flag, if library had small amount of items all items may have been loaded before.
 528        collection.mirroring._hasMore = true;
 529  
 530        // Request items.
 531        collection.more();
 532      }
 533    }
 534    openModal() {
 535      const {
 536        gallery = false,
 537        unstableFeaturedImageFlow = false,
 538        modalClass
 539      } = this.props;
 540      if (gallery) {
 541        this.buildAndSetGalleryFrame();
 542      } else {
 543        this.buildAndSetSingleMediaFrame();
 544      }
 545      if (modalClass) {
 546        this.frame.$el.addClass(modalClass);
 547      }
 548      if (unstableFeaturedImageFlow) {
 549        this.buildAndSetFeatureImageFrame();
 550      }
 551      this.initializeListeners();
 552      this.frame.open();
 553    }
 554    render() {
 555      return this.props.render({
 556        open: this.openModal
 557      });
 558    }
 559  }
 560  /* harmony default export */ const media_upload = (MediaUpload);
 561  
 562  ;// ./node_modules/@wordpress/media-utils/build-module/components/index.js
 563  
 564  
 565  ;// external ["wp","blob"]
 566  const external_wp_blob_namespaceObject = window["wp"]["blob"];
 567  ;// external ["wp","apiFetch"]
 568  const external_wp_apiFetch_namespaceObject = window["wp"]["apiFetch"];
 569  var external_wp_apiFetch_default = /*#__PURE__*/__webpack_require__.n(external_wp_apiFetch_namespaceObject);
 570  ;// ./node_modules/@wordpress/media-utils/build-module/utils/flatten-form-data.js
 571  /**
 572   * Determines whether the passed argument appears to be a plain object.
 573   *
 574   * @param data The object to inspect.
 575   */
 576  function isPlainObject(data) {
 577    return data !== null && typeof data === 'object' && Object.getPrototypeOf(data) === Object.prototype;
 578  }
 579  
 580  /**
 581   * Recursively flatten data passed to form data, to allow using multi-level objects.
 582   *
 583   * @param {FormData}      formData Form data object.
 584   * @param {string}        key      Key to amend to form data object
 585   * @param {string|Object} data     Data to be amended to form data.
 586   */
 587  function flattenFormData(formData, key, data) {
 588    if (isPlainObject(data)) {
 589      for (const [name, value] of Object.entries(data)) {
 590        flattenFormData(formData, `$key}[$name}]`, value);
 591      }
 592    } else if (data !== undefined) {
 593      formData.append(key, String(data));
 594    }
 595  }
 596  
 597  ;// ./node_modules/@wordpress/media-utils/build-module/utils/transform-attachment.js
 598  /**
 599   * Internal dependencies
 600   */
 601  
 602  /**
 603   * Transforms an attachment object from the REST API shape into the shape expected by the block editor and other consumers.
 604   *
 605   * @param attachment REST API attachment object.
 606   */
 607  function transformAttachment(attachment) {
 608    var _attachment$caption$r;
 609    // eslint-disable-next-line camelcase
 610    const {
 611      alt_text,
 612      source_url,
 613      ...savedMediaProps
 614    } = attachment;
 615    return {
 616      ...savedMediaProps,
 617      alt: attachment.alt_text,
 618      caption: (_attachment$caption$r = attachment.caption?.raw) !== null && _attachment$caption$r !== void 0 ? _attachment$caption$r : '',
 619      title: attachment.title.raw,
 620      url: attachment.source_url,
 621      poster: attachment._embedded?.['wp:featuredmedia']?.[0]?.source_url || undefined
 622    };
 623  }
 624  
 625  ;// ./node_modules/@wordpress/media-utils/build-module/utils/upload-to-server.js
 626  /**
 627   * WordPress dependencies
 628   */
 629  
 630  
 631  /**
 632   * Internal dependencies
 633   */
 634  
 635  
 636  async function uploadToServer(file, additionalData = {}, signal) {
 637    // Create upload payload.
 638    const data = new FormData();
 639    data.append('file', file, file.name || file.type.replace('/', '.'));
 640    for (const [key, value] of Object.entries(additionalData)) {
 641      flattenFormData(data, key, value);
 642    }
 643    return transformAttachment(await external_wp_apiFetch_default()({
 644      // This allows the video block to directly get a video's poster image.
 645      path: '/wp/v2/media?_embed=wp:featuredmedia',
 646      body: data,
 647      method: 'POST',
 648      signal
 649    }));
 650  }
 651  
 652  ;// ./node_modules/@wordpress/media-utils/build-module/utils/upload-error.js
 653  /**
 654   * MediaError class.
 655   *
 656   * Small wrapper around the `Error` class
 657   * to hold an error code and a reference to a file object.
 658   */
 659  class UploadError extends Error {
 660    constructor({
 661      code,
 662      message,
 663      file,
 664      cause
 665    }) {
 666      super(message, {
 667        cause
 668      });
 669      Object.setPrototypeOf(this, new.target.prototype);
 670      this.code = code;
 671      this.file = file;
 672    }
 673  }
 674  
 675  ;// ./node_modules/@wordpress/media-utils/build-module/utils/validate-mime-type.js
 676  /**
 677   * WordPress dependencies
 678   */
 679  
 680  
 681  /**
 682   * Internal dependencies
 683   */
 684  
 685  
 686  /**
 687   * Verifies if the caller (e.g. a block) supports this mime type.
 688   *
 689   * @param file         File object.
 690   * @param allowedTypes List of allowed mime types.
 691   */
 692  function validateMimeType(file, allowedTypes) {
 693    if (!allowedTypes) {
 694      return;
 695    }
 696  
 697    // Allowed type specified by consumer.
 698    const isAllowedType = allowedTypes.some(allowedType => {
 699      // If a complete mimetype is specified verify if it matches exactly the mime type of the file.
 700      if (allowedType.includes('/')) {
 701        return allowedType === file.type;
 702      }
 703      // Otherwise a general mime type is used, and we should verify if the file mimetype starts with it.
 704      return file.type.startsWith(`$allowedType}/`);
 705    });
 706    if (file.type && !isAllowedType) {
 707      throw new UploadError({
 708        code: 'MIME_TYPE_NOT_SUPPORTED',
 709        message: (0,external_wp_i18n_namespaceObject.sprintf)(
 710        // translators: %s: file name.
 711        (0,external_wp_i18n_namespaceObject.__)('%s: Sorry, this file type is not supported here.'), file.name),
 712        file
 713      });
 714    }
 715  }
 716  
 717  ;// ./node_modules/@wordpress/media-utils/build-module/utils/get-mime-types-array.js
 718  /**
 719   * Browsers may use unexpected mime types, and they differ from browser to browser.
 720   * This function computes a flexible array of mime types from the mime type structured provided by the server.
 721   * Converts { jpg|jpeg|jpe: "image/jpeg" } into [ "image/jpeg", "image/jpg", "image/jpeg", "image/jpe" ]
 722   *
 723   * @param {?Object} wpMimeTypesObject Mime type object received from the server.
 724   *                                    Extensions are keys separated by '|' and values are mime types associated with an extension.
 725   *
 726   * @return An array of mime types or null
 727   */
 728  function getMimeTypesArray(wpMimeTypesObject) {
 729    if (!wpMimeTypesObject) {
 730      return null;
 731    }
 732    return Object.entries(wpMimeTypesObject).flatMap(([extensionsString, mime]) => {
 733      const [type] = mime.split('/');
 734      const extensions = extensionsString.split('|');
 735      return [mime, ...extensions.map(extension => `$type}/$extension}`)];
 736    });
 737  }
 738  
 739  ;// ./node_modules/@wordpress/media-utils/build-module/utils/validate-mime-type-for-user.js
 740  /**
 741   * WordPress dependencies
 742   */
 743  
 744  
 745  /**
 746   * Internal dependencies
 747   */
 748  
 749  
 750  
 751  /**
 752   * Verifies if the user is allowed to upload this mime type.
 753   *
 754   * @param file               File object.
 755   * @param wpAllowedMimeTypes List of allowed mime types and file extensions.
 756   */
 757  function validateMimeTypeForUser(file, wpAllowedMimeTypes) {
 758    // Allowed types for the current WP_User.
 759    const allowedMimeTypesForUser = getMimeTypesArray(wpAllowedMimeTypes);
 760    if (!allowedMimeTypesForUser) {
 761      return;
 762    }
 763    const isAllowedMimeTypeForUser = allowedMimeTypesForUser.includes(file.type);
 764    if (file.type && !isAllowedMimeTypeForUser) {
 765      throw new UploadError({
 766        code: 'MIME_TYPE_NOT_ALLOWED_FOR_USER',
 767        message: (0,external_wp_i18n_namespaceObject.sprintf)(
 768        // translators: %s: file name.
 769        (0,external_wp_i18n_namespaceObject.__)('%s: Sorry, you are not allowed to upload this file type.'), file.name),
 770        file
 771      });
 772    }
 773  }
 774  
 775  ;// ./node_modules/@wordpress/media-utils/build-module/utils/validate-file-size.js
 776  /**
 777   * WordPress dependencies
 778   */
 779  
 780  
 781  /**
 782   * Internal dependencies
 783   */
 784  
 785  
 786  /**
 787   * Verifies whether the file is within the file upload size limits for the site.
 788   *
 789   * @param file              File object.
 790   * @param maxUploadFileSize Maximum upload size in bytes allowed for the site.
 791   */
 792  function validateFileSize(file, maxUploadFileSize) {
 793    // Don't allow empty files to be uploaded.
 794    if (file.size <= 0) {
 795      throw new UploadError({
 796        code: 'EMPTY_FILE',
 797        message: (0,external_wp_i18n_namespaceObject.sprintf)(
 798        // translators: %s: file name.
 799        (0,external_wp_i18n_namespaceObject.__)('%s: This file is empty.'), file.name),
 800        file
 801      });
 802    }
 803    if (maxUploadFileSize && file.size > maxUploadFileSize) {
 804      throw new UploadError({
 805        code: 'SIZE_ABOVE_LIMIT',
 806        message: (0,external_wp_i18n_namespaceObject.sprintf)(
 807        // translators: %s: file name.
 808        (0,external_wp_i18n_namespaceObject.__)('%s: This file exceeds the maximum upload size for this site.'), file.name),
 809        file
 810      });
 811    }
 812  }
 813  
 814  ;// ./node_modules/@wordpress/media-utils/build-module/utils/upload-media.js
 815  /**
 816   * WordPress dependencies
 817   */
 818  
 819  
 820  
 821  /**
 822   * Internal dependencies
 823   */
 824  
 825  
 826  
 827  
 828  
 829  
 830  /**
 831   * Upload a media file when the file upload button is activated
 832   * or when adding a file to the editor via drag & drop.
 833   *
 834   * @param $0                    Parameters object passed to the function.
 835   * @param $0.allowedTypes       Array with the types of media that can be uploaded, if unset all types are allowed.
 836   * @param $0.additionalData     Additional data to include in the request.
 837   * @param $0.filesList          List of files.
 838   * @param $0.maxUploadFileSize  Maximum upload size in bytes allowed for the site.
 839   * @param $0.onError            Function called when an error happens.
 840   * @param $0.onFileChange       Function called each time a file or a temporary representation of the file is available.
 841   * @param $0.wpAllowedMimeTypes List of allowed mime types and file extensions.
 842   * @param $0.signal             Abort signal.
 843   * @param $0.multiple           Whether to allow multiple files to be uploaded.
 844   */
 845  function uploadMedia({
 846    wpAllowedMimeTypes,
 847    allowedTypes,
 848    additionalData = {},
 849    filesList,
 850    maxUploadFileSize,
 851    onError,
 852    onFileChange,
 853    signal,
 854    multiple = true
 855  }) {
 856    if (!multiple && filesList.length > 1) {
 857      onError?.(new Error((0,external_wp_i18n_namespaceObject.__)('Only one file can be used here.')));
 858      return;
 859    }
 860    const validFiles = [];
 861    const filesSet = [];
 862    const setAndUpdateFiles = (index, value) => {
 863      // For client-side media processing, this is handled by the upload-media package.
 864      if (!window.__experimentalMediaProcessing) {
 865        if (filesSet[index]?.url) {
 866          (0,external_wp_blob_namespaceObject.revokeBlobURL)(filesSet[index].url);
 867        }
 868      }
 869      filesSet[index] = value;
 870      onFileChange?.(filesSet.filter(attachment => attachment !== null));
 871    };
 872    for (const mediaFile of filesList) {
 873      // Verify if user is allowed to upload this mime type.
 874      // Defer to the server when type not detected.
 875      try {
 876        validateMimeTypeForUser(mediaFile, wpAllowedMimeTypes);
 877      } catch (error) {
 878        onError?.(error);
 879        continue;
 880      }
 881  
 882      // Check if the caller (e.g. a block) supports this mime type.
 883      // Defer to the server when type not detected.
 884      try {
 885        validateMimeType(mediaFile, allowedTypes);
 886      } catch (error) {
 887        onError?.(error);
 888        continue;
 889      }
 890  
 891      // Verify if file is greater than the maximum file upload size allowed for the site.
 892      try {
 893        validateFileSize(mediaFile, maxUploadFileSize);
 894      } catch (error) {
 895        onError?.(error);
 896        continue;
 897      }
 898      validFiles.push(mediaFile);
 899  
 900      // For client-side media processing, this is handled by the upload-media package.
 901      if (!window.__experimentalMediaProcessing) {
 902        // Set temporary URL to create placeholder media file, this is replaced
 903        // with final file from media gallery when upload is `done` below.
 904        filesSet.push({
 905          url: (0,external_wp_blob_namespaceObject.createBlobURL)(mediaFile)
 906        });
 907        onFileChange?.(filesSet);
 908      }
 909    }
 910    validFiles.map(async (file, index) => {
 911      try {
 912        const attachment = await uploadToServer(file, additionalData, signal);
 913        setAndUpdateFiles(index, attachment);
 914      } catch (error) {
 915        // Reset to empty on failure.
 916        setAndUpdateFiles(index, null);
 917  
 918        // @wordpress/api-fetch throws any response that isn't in the 200 range as-is.
 919        let message;
 920        if (typeof error === 'object' && error !== null && 'message' in error) {
 921          message = typeof error.message === 'string' ? error.message : String(error.message);
 922        } else {
 923          message = (0,external_wp_i18n_namespaceObject.sprintf)(
 924          // translators: %s: file name
 925          (0,external_wp_i18n_namespaceObject.__)('Error while uploading file %s to the media library.'), file.name);
 926        }
 927        onError?.(new UploadError({
 928          code: 'GENERAL',
 929          message,
 930          file,
 931          cause: error instanceof Error ? error : undefined
 932        }));
 933      }
 934    });
 935  }
 936  
 937  ;// ./node_modules/@wordpress/media-utils/build-module/utils/sideload-to-server.js
 938  /**
 939   * WordPress dependencies
 940   */
 941  
 942  
 943  /**
 944   * Internal dependencies
 945   */
 946  
 947  
 948  
 949  
 950  /**
 951   * Uploads a file to the server without creating an attachment.
 952   *
 953   * @param file           Media File to Save.
 954   * @param attachmentId   Parent attachment ID.
 955   * @param additionalData Additional data to include in the request.
 956   * @param signal         Abort signal.
 957   *
 958   * @return The saved attachment.
 959   */
 960  async function sideloadToServer(file, attachmentId, additionalData = {}, signal) {
 961    // Create upload payload.
 962    const data = new FormData();
 963    data.append('file', file, file.name || file.type.replace('/', '.'));
 964    for (const [key, value] of Object.entries(additionalData)) {
 965      flattenFormData(data, key, value);
 966    }
 967    return transformAttachment(await external_wp_apiFetch_default()({
 968      path: `/wp/v2/media/$attachmentId}/sideload`,
 969      body: data,
 970      method: 'POST',
 971      signal
 972    }));
 973  }
 974  
 975  ;// ./node_modules/@wordpress/media-utils/build-module/utils/sideload-media.js
 976  /**
 977   * WordPress dependencies
 978   */
 979  
 980  
 981  /**
 982   * Internal dependencies
 983   */
 984  
 985  
 986  
 987  const noop = () => {};
 988  /**
 989   * Uploads a file to the server without creating an attachment.
 990   *
 991   * @param $0                Parameters object passed to the function.
 992   * @param $0.file           Media File to Save.
 993   * @param $0.attachmentId   Parent attachment ID.
 994   * @param $0.additionalData Additional data to include in the request.
 995   * @param $0.signal         Abort signal.
 996   * @param $0.onFileChange   Function called each time a file or a temporary representation of the file is available.
 997   * @param $0.onError        Function called when an error happens.
 998   */
 999  async function sideloadMedia({
1000    file,
1001    attachmentId,
1002    additionalData = {},
1003    signal,
1004    onFileChange,
1005    onError = noop
1006  }) {
1007    try {
1008      const attachment = await sideloadToServer(file, attachmentId, additionalData, signal);
1009      onFileChange?.([attachment]);
1010    } catch (error) {
1011      let message;
1012      if (error instanceof Error) {
1013        message = error.message;
1014      } else {
1015        message = (0,external_wp_i18n_namespaceObject.sprintf)(
1016        // translators: %s: file name
1017        (0,external_wp_i18n_namespaceObject.__)('Error while sideloading file %s to the server.'), file.name);
1018      }
1019      onError(new UploadError({
1020        code: 'GENERAL',
1021        message,
1022        file,
1023        cause: error instanceof Error ? error : undefined
1024      }));
1025    }
1026  }
1027  
1028  ;// external ["wp","privateApis"]
1029  const external_wp_privateApis_namespaceObject = window["wp"]["privateApis"];
1030  ;// ./node_modules/@wordpress/media-utils/build-module/lock-unlock.js
1031  /**
1032   * WordPress dependencies
1033   */
1034  
1035  const {
1036    lock,
1037    unlock
1038  } = (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');
1039  
1040  ;// ./node_modules/@wordpress/media-utils/build-module/private-apis.js
1041  /**
1042   * Internal dependencies
1043   */
1044  
1045  
1046  
1047  /**
1048   * Private @wordpress/media-utils APIs.
1049   */
1050  const privateApis = {};
1051  lock(privateApis, {
1052    sideloadMedia: sideloadMedia
1053  });
1054  
1055  ;// ./node_modules/@wordpress/media-utils/build-module/index.js
1056  
1057  
1058  
1059  
1060  
1061  
1062  
1063  
1064  (window.wp = window.wp || {}).mediaUtils = __webpack_exports__;
1065  /******/ })()
1066  ;


Generated : Thu Apr 3 08:20:01 2025 Cross-referenced by PHPXref