@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
33 lines (32 loc) • 928 B
JavaScript
// packages/editor/src/utils/media-sideload-from-url/index.js
import apiFetch from "@wordpress/api-fetch";
import { select } from "@wordpress/data";
import { transformAttachment } from "@wordpress/media-utils";
import { store as editorStore } from "../../store/index.mjs";
var noop = () => {
};
function mediaSideloadFromUrl({
url,
onSuccess,
onError = noop
}) {
const currentPost = select(editorStore).getCurrentPost();
const currentPostId = typeof currentPost?.id === "number" ? currentPost.id : currentPost?.wp_id;
const postData = currentPostId ? { post: currentPostId } : {};
apiFetch({
path: "/wp/v2/media",
method: "POST",
data: {
url,
...postData
}
}).then((attachment) => {
onSuccess?.(transformAttachment(attachment));
}).catch((error) => {
onError(error?.message || error);
});
}
export {
mediaSideloadFromUrl as default
};
//# sourceMappingURL=index.mjs.map