Skip to content

Commit

Permalink
WIP not working
Browse files Browse the repository at this point in the history
  • Loading branch information
fushar committed Jan 16, 2025
1 parent c5cc25f commit 85afb83
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { useState } from 'react';
import wpcomRequest from 'wpcom-proxy-request';

export default function WpcomMediaUrlImportForm( { siteId } ) {

Check warning on line 4 in projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import-form/index.jsx

View workflow job for this annotation

GitHub Actions / ESLint (non-excluded files only)

Missing JSDoc comment
const [ url, setUrl ] = useState( '' );

const handleUrlChange = e => {
setUrl( e.target.value );

Check failure on line 8 in projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import-form/index.jsx

View workflow job for this annotation

GitHub Actions / ESLint (non-excluded files only)

Replace `····` with `↹↹`
};

Check failure on line 9 in projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import-form/index.jsx

View workflow job for this annotation

GitHub Actions / ESLint (non-excluded files only)

Replace `··` with `↹`

const handleSubmit = async e => {
try {
new URL(url);

Check failure on line 13 in projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import-form/index.jsx

View workflow job for this annotation

GitHub Actions / ESLint (non-excluded files only)

Do not use 'new' for side effects

Check failure on line 13 in projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import-form/index.jsx

View workflow job for this annotation

GitHub Actions / ESLint (non-excluded files only)

Replace `url` with `·url·`
} catch (e) {

Check failure on line 14 in projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import-form/index.jsx

View workflow job for this annotation

GitHub Actions / ESLint (non-excluded files only)

Replace `e` with `·e·`

Check failure on line 14 in projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import-form/index.jsx

View workflow job for this annotation

GitHub Actions / ESLint (non-excluded files only)

'e' is defined but never used

Check failure on line 14 in projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import-form/index.jsx

View workflow job for this annotation

GitHub Actions / ESLint (non-excluded files only)

'e' is already declared in the upper scope on line 11 column 29
return false;
}
e.preventDefault();

console.log({ url, siteId } );

Check failure on line 19 in projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import-form/index.jsx

View workflow job for this annotation

GitHub Actions / ESLint (non-excluded files only)

Unexpected console statement

Check failure on line 19 in projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import-form/index.jsx

View workflow job for this annotation

GitHub Actions / ESLint (non-excluded files only)

Insert `·`

console.log({ wpcomRequest });

Check failure on line 21 in projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import-form/index.jsx

View workflow job for this annotation

GitHub Actions / ESLint (non-excluded files only)

Unexpected console statement

window.wpcomRequest = wpcomRequest;

// NOT WORKING!!

const response = await wpcomRequest( {
method: 'POST',
apiVersion: '1.1',
path: `/sites/${ siteId }/media/new?http_envelope=1`,
body: {
media_urls: [ url ],
}
});


console.log('FINISH');
console.log({ response});

return false;
}

return (
<div class="wrap">
<p>or import from URL YAY!</p>
<form onsubmit="return false;">
<input
type="url"
value={ url }
onChange={ handleUrlChange }
required />
<button onClick={ handleSubmit } class="button button-primary">Import</button>
</form>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -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( <WpcomMediaUrlImportForm { ...props } /> );
observer.disconnect();
}
}
} );
} );
observer.observe( document.body, { childList: true, subtree: true } );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,8 @@
use Automattic\Jetpack\Connection\Client;

Check warning on line 10 in projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import.php

View workflow job for this annotation

GitHub Actions / PHP Code Sniffer (non-excluded files only)

Unused use statement "Client" (MediaWiki.Classes.UnusedUseStatement.UnusedUse)

Check failure on line 10 in projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import.php

View workflow job for this annotation

GitHub Actions / Static analysis

NOOPError PhanUnreferencedUseNormal Possibly zero references to use statement for classlike/namespace Client (\Automattic\Jetpack\Connection\Client)

function wpcom_media_url_import() {

Check failure on line 12 in projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import.php

View workflow job for this annotation

GitHub Actions / PHP Code Sniffer (non-excluded files only)

Missing doc comment for function wpcom_media_url_import() (Squiz.Commenting.FunctionComment.Missing)
$nonce = wp_create_nonce('import_image');
$ajax_url = admin_url('admin-ajax.php');

?>

Check failure on line 13 in projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import.php

View workflow job for this annotation

GitHub Actions / PHP Code Sniffer (non-excluded files only)

Tabs must be used to indent lines; spaces are not allowed (Generic.WhiteSpace.DisallowSpaceIndent.SpacesUsed)
<div class="wrap">
<h1>Import Image from URL hah</h1>
<form id="wpcom-media-url-import" action="<?php echo esc_url($ajax_url); ?>" method="POST">
<input type="url" id="image_url" placeholder="Enter image URL" required />
<input type="hidden" name="nonce" value="<?php echo esc_attr($nonce); ?>" />
<input type="hidden" name="action" value="import_image" />
<button type="submit" class="button button-primary">Import Image</button>
</form>
<div id="wpcom-media-url-import">

Check failure on line 14 in projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import.php

View workflow job for this annotation

GitHub Actions / PHP Code Sniffer (non-excluded files only)

Tabs must be used to indent lines; spaces are not allowed (Generic.WhiteSpace.DisallowSpaceIndent.SpacesUsed)
</div>

Check failure on line 15 in projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import.php

View workflow job for this annotation

GitHub Actions / PHP Code Sniffer (non-excluded files only)

Tabs must be used to indent lines; spaces are not allowed (Generic.WhiteSpace.DisallowSpaceIndent.SpacesUsed)
<?php

Check failure on line 16 in projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import.php

View workflow job for this annotation

GitHub Actions / PHP Code Sniffer (non-excluded files only)

Tabs must be used to indent lines; spaces are not allowed (Generic.WhiteSpace.DisallowSpaceIndent.SpacesUsed)
}
Expand All @@ -31,29 +21,18 @@ function wpcom_media_url_import() {
* Enqueue the assets of the wpcom media URL import.
*/
function enqueue_wpcom_media_url_import() {
jetpack_mu_wpcom_enqueue_assets( 'wpcom-media-url-import', array( 'js' ) );
}
add_action('post-plupload-upload-ui', 'enqueue_wpcom_media_url_import' );

// IT WON'T WORK NOT SURE WHY!?!?!
function wpcom_handle_media_url_import() {
check_ajax_referer('import_image', 'nonce');
$handle = jetpack_mu_wpcom_enqueue_assets( 'wpcom-media-url-import', array( 'js' ) );

$image_url = esc_url_raw($_POST['image_url']);
$blog_id = Jetpack_Options::get_option( 'id' );
$response = Client::wpcom_json_api_request_as_blog(
'/sites/' . $blog_id . '/media/new',
'1.1',
$data = wp_json_encode(

Check warning on line 27 in projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import.php

View workflow job for this annotation

GitHub Actions / PHP Code Sniffer (non-excluded files only)

Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space (Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
array(
'method' => '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' );

Check failure on line 38 in projects/packages/jetpack-mu-wpcom/src/features/wpcom-media/wpcom-media-url-import.php

View workflow job for this annotation

GitHub Actions / PHP Code Sniffer (non-excluded files only)

Expected 1 spaces after opening parenthesis; 0 found (PEAR.Functions.FunctionCallSignature.SpaceAfterOpenBracket)

0 comments on commit 85afb83

Please sign in to comment.