-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Media Library: Implement upload media from URL (#41089)
Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/13170764545 Upstream-Ref: Automattic/jetpack@d6f2cb4
- Loading branch information
Showing
12 changed files
with
292 additions
and
63 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
...mattic/jetpack-mu-wpcom/src/build/wpcom-media-url-upload/wpcom-media-url-upload.asset.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<?php return array('dependencies' => array('react', 'react-dom', 'wp-i18n', 'wp-polyfill'), 'version' => '10268ed3f972a83340f1'); |
1 change: 1 addition & 0 deletions
1
...r/automattic/jetpack-mu-wpcom/src/build/wpcom-media-url-upload/wpcom-media-url-upload.css
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
...or/automattic/jetpack-mu-wpcom/src/build/wpcom-media-url-upload/wpcom-media-url-upload.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
...tomattic/jetpack-mu-wpcom/src/build/wpcom-media-url-upload/wpcom-media-url-upload.rtl.css
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
...utomattic/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-upload-form/index.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { __ } from '@wordpress/i18n'; | ||
import clsx from 'clsx'; | ||
import { useState } from 'react'; | ||
|
||
import './style.scss'; | ||
|
||
const WpcomMediaUrlUploadForm = ( { ajaxUrl, action, nonce, isEditor } ) => { | ||
const [ url, setUrl ] = useState( '' ); | ||
|
||
const [ show, setShow ] = useState( false ); | ||
const [ isUploading, setIsUploading ] = useState( false ); | ||
|
||
const handleUrlChange = e => { | ||
setUrl( e.target.value ); | ||
}; | ||
|
||
const handleSubmit = async e => { | ||
if ( isUploading ) { | ||
return false; | ||
} | ||
try { | ||
new URL( url ); // eslint-disable-line no-new | ||
} catch { | ||
return false; | ||
} | ||
e.preventDefault(); | ||
|
||
const formData = new FormData(); | ||
formData.append( 'action', action ); | ||
formData.append( 'url', url ); | ||
formData.append( '_ajax_nonce', nonce ); | ||
|
||
setIsUploading( true ); | ||
|
||
const response = await fetch( ajaxUrl, { | ||
method: 'POST', | ||
body: formData, | ||
} ); | ||
|
||
const { success, data } = await response.json(); | ||
|
||
if ( success ) { | ||
window.wp.media.model.Attachment.get( data.attachment_id ).fetch( { | ||
success: function ( attachment ) { | ||
const addAttachment = attachmentToAdd => { | ||
( window.wp.media.frame.controller || window.wp.media.frame ).content | ||
.get() | ||
.collection.add( attachmentToAdd ); | ||
}; | ||
|
||
if ( isEditor ) { | ||
const mediaLibraryTab = window.wp.media.frame.state( 'library' ); | ||
mediaLibraryTab.trigger( 'open' ); | ||
|
||
addAttachment( attachment ); | ||
|
||
const selection = mediaLibraryTab.get( 'selection' ); | ||
selection.reset(); | ||
selection.add( [ attachment ] ); | ||
} else { | ||
addAttachment( attachment ); | ||
} | ||
|
||
setIsUploading( false ); | ||
setUrl( '' ); | ||
}, | ||
} ); | ||
} else { | ||
setIsUploading( false ); | ||
window.wp.Uploader.errors.add( { file: { name: url }, message: data[ 0 ].message } ); | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
const renderLink = () => { | ||
return ( | ||
<button | ||
className="button wpcom-media-url-upload-form__pre-upload-button" | ||
onClick={ () => setShow( true ) } | ||
> | ||
{ __( 'Upload from URL', 'jetpack-mu-wpcom' ) } | ||
</button> | ||
); | ||
}; | ||
|
||
const renderForm = () => { | ||
let buttonText = __( 'Upload', 'jetpack-mu-wpcom' ); | ||
if ( isUploading ) { | ||
buttonText = __( 'Uploading…', 'jetpack-mu-wpcom' ); | ||
} | ||
return ( | ||
<form onSubmit={ handleSubmit }> | ||
<input | ||
type="url" | ||
value={ url } | ||
onChange={ handleUrlChange } | ||
placeholder={ __( 'Enter media URL', 'jetpack-mu-wpcom' ) } | ||
required | ||
readOnly={ isUploading } | ||
/> | ||
<button | ||
type="submit" | ||
className={ clsx( 'button', 'button-primary', { | ||
'updating-message': isUploading, | ||
} ) } | ||
readOnly={ isUploading } | ||
> | ||
{ buttonText } | ||
</button> | ||
</form> | ||
); | ||
}; | ||
|
||
return <div className="wpcom-media-url-upload-form">{ show ? renderForm() : renderLink() }</div>; | ||
}; | ||
|
||
export default WpcomMediaUrlUploadForm; |
23 changes: 23 additions & 0 deletions
23
...ack_vendor/automattic/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-upload.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom/client'; | ||
import WpcomMediaUrlUploadForm from './wpcom-media-url-upload-form'; | ||
|
||
const props = typeof window === 'object' ? window.JETPACK_MU_WPCOM_MEDIA_URL_UPLOAD : {}; | ||
|
||
document.addEventListener( 'DOMContentLoaded', function () { | ||
if ( window.wp?.media?.view?.UploaderInline ) { | ||
const originalUploaderInline = window.wp.media.view.UploaderInline; | ||
|
||
window.wp.media.view.UploaderInline = originalUploaderInline.extend( { | ||
ready: function () { | ||
originalUploaderInline.prototype.ready.apply( this, arguments ); | ||
|
||
const container = document.getElementById( 'wpcom-media-url-upload' ); | ||
if ( container ) { | ||
const root = ReactDOM.createRoot( container ); | ||
root.render( <WpcomMediaUrlUploadForm { ...props } /> ); | ||
} | ||
}, | ||
} ); | ||
} | ||
} ); |
82 changes: 82 additions & 0 deletions
82
...ck_vendor/automattic/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-upload.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
/** | ||
* Allows uploading media from URL in Media Library. | ||
* | ||
* @package automattic/jetpack-mu-wpcom | ||
*/ | ||
|
||
/** | ||
* Appends the wpcom media URL upload form. | ||
*/ | ||
function wpcom_media_url_upload() { | ||
global $pagenow; | ||
|
||
if ( empty( $_GET['untangling-media'] ) ) { // phpcs:disable WordPress.Security.NonceVerification.Recommended | ||
return; | ||
} | ||
|
||
?> | ||
<div id="wpcom-media-url-upload"></div> | ||
<?php | ||
|
||
$handle = jetpack_mu_wpcom_enqueue_assets( 'wpcom-media-url-upload', array( 'js', 'css' ) ); | ||
|
||
$data = wp_json_encode( | ||
array( | ||
'ajaxUrl' => admin_url( 'admin-ajax.php' ), | ||
'action' => 'wpcom_media_url_upload', | ||
'nonce' => wp_create_nonce( 'wpcom_media_url_upload' ), | ||
'isEditor' => $pagenow !== 'upload.php', | ||
) | ||
); | ||
|
||
wp_add_inline_script( | ||
$handle, | ||
"window.JETPACK_MU_WPCOM_MEDIA_URL_UPLOAD = $data;", | ||
'before' | ||
); | ||
} | ||
|
||
/** | ||
* AJAX handler for the wpcom media URL upload. | ||
*/ | ||
function wpcom_handle_media_url_upload() { | ||
check_ajax_referer( 'wpcom_media_url_upload' ); | ||
|
||
if ( ! isset( $_POST['url'] ) ) { | ||
return; | ||
} | ||
|
||
$url = esc_url_raw( wp_unslash( $_POST['url'] ) ); | ||
|
||
$tmp_file = download_url( $url ); | ||
if ( is_wp_error( $tmp_file ) ) { | ||
return wp_send_json_error( $tmp_file ); | ||
} | ||
|
||
if ( is_multisite() ) { | ||
add_filter( 'wp_handle_sideload_prefilter', 'check_upload_size' ); | ||
} | ||
|
||
$attachment_id = media_handle_sideload( | ||
array( | ||
'name' => basename( wp_parse_url( $url, PHP_URL_PATH ) ), | ||
'tmp_name' => $tmp_file, | ||
) | ||
); | ||
|
||
if ( file_exists( $tmp_file ) ) { | ||
wp_delete_file( $tmp_file ); | ||
} | ||
|
||
if ( is_wp_error( $attachment_id ) ) { | ||
return wp_send_json_error( $attachment_id ); | ||
} else { | ||
return wp_send_json_success( array( 'attachment_id' => $attachment_id ) ); | ||
} | ||
} | ||
|
||
if ( current_user_can( 'upload_files' ) ) { | ||
add_action( 'pre-upload-ui', 'wpcom_media_url_upload', 9 ); | ||
add_action( 'wp_ajax_wpcom_media_url_upload', 'wpcom_handle_media_url_upload' ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.