diff --git a/projects/packages/jetpack-mu-wpcom/.phan/baseline.php b/projects/packages/jetpack-mu-wpcom/.phan/baseline.php index 6865fcfd0af51..979145e532f7a 100644 --- a/projects/packages/jetpack-mu-wpcom/.phan/baseline.php +++ b/projects/packages/jetpack-mu-wpcom/.phan/baseline.php @@ -17,9 +17,9 @@ // PhanNoopNew : 5 occurrences // PhanTypeMismatchArgumentInternal : 4 occurrences // PhanTypeMismatchReturnProbablyReal : 4 occurrences + // PhanParamTooMany : 3 occurrences // PhanTypePossiblyInvalidDimOffset : 3 occurrences // PhanEmptyFQSENInCallable : 2 occurrences - // PhanParamTooMany : 2 occurrences // PhanTypeArraySuspicious : 2 occurrences // PhanTypeArraySuspiciousNullable : 2 occurrences // PhanTypeMismatchDefault : 2 occurrences @@ -37,6 +37,8 @@ // PhanTypeMismatchReturnNullable : 1 occurrence // PhanTypeNonVarPassByRef : 1 occurrence // PhanTypeVoidArgument : 1 occurrence + // PhanUndeclaredExtendedClass : 1 occurrence + // PhanUndeclaredMethod : 1 occurrence // Currently, file_suppressions and directory_suppressions are the only supported suppressions 'file_suppressions' => [ @@ -57,7 +59,8 @@ 'src/features/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-launchpad.php' => ['PhanPluginDuplicateConditionalNullCoalescing'], 'src/features/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-site-migration-migrate-guru-key.php' => ['PhanUndeclaredClassMethod'], 'src/features/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-site-migration-wpcom-migration-key.php' => ['PhanUndeclaredClassMethod'], - 'tests/lib/functions-wordpress.php' => ['PhanRedefineFunction'], + 'src/features/wpcom-media/wpcom-media-url-import.php' => ['PhanParamTooMany', 'PhanUndeclaredExtendedClass', 'PhanUndeclaredMethod'], + 'tests/lib/functions-wordpress.php' => ['PhanRedefineFunction'], 'tests/php/features/block-patterns/class-wpcom-block-patterns-from-api-test.php' => ['PhanDeprecatedFunction'], 'tests/php/features/coming-soon/class-coming-soon-test.php' => ['PhanTypeMismatchArgument', 'PhanTypeMismatchArgumentProbablyReal'], 'tests/php/features/launchpad/class-launchpad-jetpack-connection-client-mock.php' => ['PhanTypeMissingReturn'], diff --git a/projects/packages/jetpack-mu-wpcom/changelog/wpcom-media-url-import b/projects/packages/jetpack-mu-wpcom/changelog/wpcom-media-url-import new file mode 100644 index 0000000000000..27db5a6914fdc --- /dev/null +++ b/projects/packages/jetpack-mu-wpcom/changelog/wpcom-media-url-import @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Implement media import via URL diff --git a/projects/packages/jetpack-mu-wpcom/src/class-jetpack-mu-wpcom.php b/projects/packages/jetpack-mu-wpcom/src/class-jetpack-mu-wpcom.php index ec3435b3fbc18..4bc2a39096518 100644 --- a/projects/packages/jetpack-mu-wpcom/src/class-jetpack-mu-wpcom.php +++ b/projects/packages/jetpack-mu-wpcom/src/class-jetpack-mu-wpcom.php @@ -152,6 +152,7 @@ public static function load_wpcom_user_features() { require_once __DIR__ . '/features/wpcom-dashboard-widgets/wpcom-dashboard-widgets.php'; require_once __DIR__ . '/features/wpcom-locale/sync-locale-from-calypso-to-atomic.php'; require_once __DIR__ . '/features/wpcom-media/wpcom-external-media-import.php'; + require_once __DIR__ . '/features/wpcom-media/wpcom-media-url-import.php'; require_once __DIR__ . '/features/wpcom-plugins/wpcom-plugins.php'; require_once __DIR__ . '/features/wpcom-profile-settings/profile-settings-link-to-wpcom.php'; require_once __DIR__ . '/features/wpcom-profile-settings/profile-settings-notices.php'; diff --git a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import-form/index.jsx b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import-form/index.jsx new file mode 100644 index 0000000000000..571144b698c67 --- /dev/null +++ b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import-form/index.jsx @@ -0,0 +1,109 @@ +import { __ } from '@wordpress/i18n'; +import clsx from 'clsx'; +import { useState } from 'react'; + +import './style.scss'; + +const WpcomMediaUrlImportForm = ( { ajaxUrl, action, nonce } ) => { + const [ url, setUrl ] = useState( '' ); + + const [ show, setShow ] = useState( false ); + const [ isUploading, setIsUploading ] = useState( false ); + const [ isUploaded, setIsUploaded ] = 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( 'image_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 ) { + window.wp.media.frame.content.get().collection.add( attachment ); + + setIsUploading( false ); + + setIsUploaded( true ); + setTimeout( () => { + setIsUploaded( false ); + setUrl( '' ); + }, 2000 ); + }, + } ); + } else { + setIsUploading( false ); + // window.alert( data[ 0 ].message ); + } + + return false; + }; + + const renderLink = () => { + return ( + setShow( true ) }> + { __( 'Upload from URL', 'jetpack-mu-wpcom' ) } + + ); + }; + + const renderForm = () => { + let buttonText = __( 'Upload', 'jetpack-mu-wpcom' ); + if ( isUploaded ) { + buttonText = __( 'Uploaded!', 'jetpack-mu-wpcom' ); + } else if ( isUploading ) { + buttonText = __( 'Uploading…', 'jetpack-mu-wpcom' ); + } + + return ( +
+ ); + }; + + return