Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Implement media import via URL #41089

Draft
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Implement media import via URL
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
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
@@ -0,0 +1,21 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import WpcomMediaUrlImportForm from './wpcom-media-url-import-form';

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
@@ -0,0 +1,38 @@
<?php
/**
* WordPress.com media URL import functionality.
*
*
* @package automattic/jetpack-mu-wpcom

Check failure on line 6 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)

There must be exactly one blank line before the tags in a doc comment (Generic.Commenting.DocComment.SpacingBeforeTags)
*/

use Automattic\Jetpack\Connection\Manager as Jetpack_Connection;

Check warning on line 9 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)

Use statements are not alphabetically sorted (MediaWiki.Classes.UnsortedUseStatements.UnsortedUse)

Check warning on line 9 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 "Jetpack_Connection" (MediaWiki.Classes.UnusedUseStatement.UnusedUse)

Check failure on line 9 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 Jetpack_Connection (\Automattic\Jetpack\Connection\Manager)
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)
?>

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 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)
}
add_action('post-plupload-upload-ui', 'wpcom_media_url_import', 20 );

Check failure on line 18 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)

/**
* Enqueue the assets of the wpcom media URL import.
*/
function enqueue_wpcom_media_url_import() {
$handle = jetpack_mu_wpcom_enqueue_assets( 'wpcom-media-url-import', array( 'js' ) );

$blog_id = Jetpack_Options::get_option( 'id' );
$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(
'siteId' => $blog_id,
)
);
wp_add_inline_script(
$handle,
"window.JETPACK_MU_WPCOM_MEDIA_URL_IMPORT = $data;",
'before'
);
}
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)
1 change: 1 addition & 0 deletions projects/packages/jetpack-mu-wpcom/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module.exports = [
'./src/features/wpcom-documentation-links/wpcom-documentation-links.ts',
'wpcom-external-media-import-page':
'./src/features/wpcom-media/wpcom-external-media-import.js',
'wpcom-media-url-import': './src/features/wpcom-media/wpcom-media-url-import.js',
'wpcom-plugins-banner': './src/features/wpcom-plugins/js/banner.js',
'wpcom-plugins-banner-style': './src/features/wpcom-plugins/css/banner.css',
'wpcom-profile-settings-link-to-wpcom':
Expand Down
Loading