From 4ae6458814bb412fadb91a3e0a6efd7581ec16cf Mon Sep 17 00:00:00 2001 From: Ashar Fuadi Date: Thu, 16 Jan 2025 12:02:19 +0700 Subject: [PATCH] WIP not working --- .../wpcom-media-url-import-form/index.jsx | 57 +++++++++++++++++++ .../wpcom-media/wpcom-media-url-import.js | 22 ++++++- .../wpcom-media/wpcom-media-url-import.php | 43 ++++---------- 3 files changed, 87 insertions(+), 35 deletions(-) create mode 100644 projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import-form/index.jsx 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..7902241c60d9d --- /dev/null +++ b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import-form/index.jsx @@ -0,0 +1,57 @@ +import { useState } from 'react'; +import wpcomRequest from 'wpcom-proxy-request'; + +export default function WpcomMediaUrlImportForm( { siteId } ) { + const [ url, setUrl ] = useState( '' ); + + const handleUrlChange = e => { + setUrl( e.target.value ); + }; + + const handleSubmit = async e => { + try { + new URL(url); + } catch (e) { + return false; + } + e.preventDefault(); + + console.log({ url, siteId } ); + + console.log({ wpcomRequest }); + + window.wpcomRequest = wpcomRequest; + + // NOT WORKING!! + + const response = await wpcomRequest( { + method: 'POST', + apiVersion: '2', + apiNamespace: 'rest/v1.1', + path: `/sites/${ siteId }/media/new`, + body: { + media_urls: [ url ], + } + }); + + + console.log('FINISH'); + console.log({ response}); + + return false; + } + + return ( +
+

or import from URL YAY!

+
+ + +
+
+ ); +} diff --git a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import.js b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import.js index e98e1d8df1d75..af016efa56375 100644 --- a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import.js +++ b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import.js @@ -1,5 +1,21 @@ -import apiFetch from '@wordpress/api-fetch'; +import React from 'react'; +import ReactDOM from 'react-dom/client'; +import WpcomMediaUrlImportForm from './wpcom-media-url-import-form'; -document.addEventListener( 'DOMContentLoaded', () => { - console.log( 'TESTING' ); +const props = typeof window === 'object' ? window.JETPACK_MU_WPCOM_MEDIA_URL_IMPORT : {}; + +document.addEventListener( 'DOMContentLoaded', function () { + const observer = new MutationObserver( mutations => { + mutations.forEach( mutation => { + if ( mutation.addedNodes.length > 0 ) { + const container = document.getElementById( 'wpcom-media-url-import' ); + if ( container ) { + const root = ReactDOM.createRoot( container ); + root.render( ); + observer.disconnect(); + } + } + } ); + } ); + observer.observe( document.body, { childList: true, subtree: true } ); } ); diff --git a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import.php b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import.php index 09eeaa5f47071..659b86ac45c37 100644 --- a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import.php +++ b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import.php @@ -10,18 +10,8 @@ use Automattic\Jetpack\Connection\Client; function wpcom_media_url_import() { - $nonce = wp_create_nonce('import_image'); - $ajax_url = admin_url('admin-ajax.php'); - ?> -
-

Import Image from URL hah

-
- - - - -
+
'POST', - 'headers' => array( - 'Content-Type' => 'application/json', - ), - ), - wp_json_encode(array( 'media_urls' => [$image_url])), - 'rest' + 'siteId' => $blog_id, + ) + ); + wp_add_inline_script( + $handle, + "window.JETPACK_MU_WPCOM_MEDIA_URL_IMPORT = $data;", + 'before' ); - - wp_send_json_success($response); } -add_action('wp_ajax_import_image', 'wpcom_handle_media_url_import'); +add_action('post-plupload-upload-ui', 'enqueue_wpcom_media_url_import' );