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

Import Media: Integrate external media modal #41282

Open
wants to merge 5 commits into
base: feat/move-external-media-to-package
Choose a base branch
from
Open
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

External Media: Add external media modal on the Media Import page
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
* Add hooks and filters.
*/
public static function init() {
// Load external media import page on WordPress.com sites first.
// We will continue to enable the feature on all sites.
$host = new Host();
if ( $host->is_wpcom_simple() || $host->is_woa_site() ) {
require_once __DIR__ . '/features/admin/external-media-import.php';
}

add_action( 'enqueue_block_editor_assets', array( __CLASS__, 'enqueue_block_editor_assets' ) );
}

Expand All @@ -36,7 +43,7 @@
Assets::register_script(
$asset_name,
$assets_base_path . "$asset_name/$asset_name.js",
__FILE__,
self::BASE_FILE,
array(
'enqueue' => true,
'textdomain' => 'jetpack-external-media',
Expand All @@ -45,7 +52,7 @@

wp_add_inline_script(
$asset_name,
sprintf( 'var JetpackExternalMediaData = %s;', self::get_data() ),

Check failure on line 55 in projects/packages/external-media/src/class-external-media.php

View workflow job for this annotation

GitHub Actions / Static analysis

TypeError PhanTypeMismatchArgumentInternal Argument 2 ($values) is self::get_data() of type array but \sprintf() takes \Stringable|float|int|string
'before'
);

Expand All @@ -70,7 +77,7 @@
$jetpack_ai_enabled = true;
}

return wp_json_encode(

Check failure on line 80 in projects/packages/external-media/src/class-external-media.php

View workflow job for this annotation

GitHub Actions / Static analysis

TypeError PhanTypeMismatchReturn Returning wp_json_encode(['wpcomBlogId'=>$blog_id,'pluginBasePath'=>plugins_url('', Constants::get_constant('JETPACK__PLUGIN_FILE')),'ai-assistant'=>['is-enabled'=>apply_filters('jetpack_ai_enabled', $jetpack_ai_enabled)]]) of type false|string but get_data() is declared to return array
array(
'wpcomBlogId' => $blog_id,
'pluginBasePath' => plugins_url( '', Constants::get_constant( 'JETPACK__PLUGIN_FILE' ) ),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { sprintf, _n } from '@wordpress/i18n';
import { useState, useEffect } from 'react';
import ReactDOM from 'react-dom/client';
import { getExternalLibrary } from '../../shared';

const JETPACK_EXTERNAL_MEDIA_IMPORT_PAGE_CONTAINER = 'jetpack-external-media-import';
const JETPACK_EXTERNAL_MEDIA_IMPORT_PAGE_MODAL = 'jetpack-external-media-import-modal';

const JetpackExternalMediaImport = () => {
const [ selectedSource, setSelectedSource ] = useState( null );
const ExternalLibrary = getExternalLibrary( selectedSource );

const showNotice = message => {
const notice = document.createElement( 'div' );
notice.className = 'notice notice-success';
notice.innerHTML = `<p>${ message }</p>`;

// Add the success notice after the page title
const heading = document.querySelector(
`#${ JETPACK_EXTERNAL_MEDIA_IMPORT_PAGE_CONTAINER } > h1`
);
if ( heading ) {
heading.parentNode.insertBefore( notice, heading.nextSibling );
}
};

const handleSelect = media => {
if ( ! media || media.length === 0 ) {
return;
}

showNotice(
sprintf(
/* translators: %d is the number of the media file */
_n(
'%d media file imported successfully',
'%d media files imported successfully',
media.length,
'jetpack-external-media'
),
media.length
)
);
};

const closeLibrary = event => {
if ( event ) {
event.stopPropagation();

// The DateTime picker is triggering a modal close when selected. We don't want this to close the modal
if ( event.target.closest( '.jetpack-external-media-header__dropdown' ) ) {
return;
}
}

setSelectedSource( null );
};

useEffect( () => {
const element = document.getElementById( JETPACK_EXTERNAL_MEDIA_IMPORT_PAGE_CONTAINER );
const handleClick = event => {
const slug = event.target.dataset.slug;
if ( slug ) {
setSelectedSource( slug );
}
};

if ( element ) {
element.addEventListener( 'click', handleClick );
}

return () => {
if ( element ) {
element.removeEventListener( 'click', handleClick );
}
};
}, [] );

if ( ! ExternalLibrary ) {
return null;
}

return <ExternalLibrary multiple onSelect={ handleSelect } onClose={ closeLibrary } />;
};

const container = document.getElementById( JETPACK_EXTERNAL_MEDIA_IMPORT_PAGE_MODAL );
if ( container ) {
const root = ReactDOM.createRoot( container );
root.render( <JetpackExternalMediaImport /> );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php
/**
* The Jetpack external media import page.
*
* Adds Jetpack external media page to Media > Import.
*
* @package automattic/jetpack-external-media
*/

namespace Automattic\Jetpack;

/**
* Register the Jetpack external media page to Media > Import.
*/
function add_jetpack_external_media_import_page() {
if ( empty( $_GET['jetpack_external_media_import_page'] ) ) { // phpcs:disable WordPress.Security.NonceVerification.Recommended
return;
}

$external_media_import_page_hook = add_submenu_page(
'upload.php',
__( 'Import Media', 'jetpack-external-media' ),
__( 'Import Media', 'jetpack-external-media' ),
'upload_files',
'jetpack_external_media_import_page',
__NAMESPACE__ . '\render_jetpack_external_media_import_page'
);

add_action( "load-$external_media_import_page_hook", __NAMESPACE__ . '\enqueue_jetpack_external_media_import_page' );
}
add_action( 'admin_menu', __NAMESPACE__ . '\add_jetpack_external_media_import_page' );

/**
* Enqueue the assets of the Jetpack external media page.
*/
function enqueue_jetpack_external_media_import_page() {
$assets_base_path = 'build/';
$asset_name = 'jetpack-external-media-import-page';

Assets::register_script(
$asset_name,
$assets_base_path . "$asset_name/$asset_name.js",
External_Media::BASE_FILE,
array(
'in_footer' => true,
'textdomain' => 'jetpack-external-media',
)
);

Assets::enqueue_script( $asset_name );
}

/**
* Render the container of the Jetpack external media page.
*/
function render_jetpack_external_media_import_page() {
$title = __( 'Import Media', 'jetpack-external-media' );
$description = __( 'WordPress allows you to import media from various platforms directly into the Media Library. To begin, select a platform from the options below:', 'jetpack-external-media' );
$external_media_sources = array(
array(
'slug' => 'google_photos',
'name' => __( 'Google Photos', 'jetpack-external-media' ),
'description' => __( 'Import media from your Google Photos account.', 'jetpack-external-media' ),
),
array(
'slug' => 'pexels',
'name' => __( 'Pexels free photos', 'jetpack-external-media' ),
'description' => __( 'Free stock photos, royalty free images shared by creators.', 'jetpack-external-media' ),
),
array(
'slug' => 'openverse',
'name' => __( 'Openverse', 'jetpack-external-media' ),
'description' => __( 'Explore more than 800 million creative works.', 'jetpack-external-media' ),
),
);

?>
<div id="jetpack-external-media-import" class="wrap">
<h1><?php echo esc_html( $title ); ?></h1>
<p><?php echo esc_html( $description ); ?></p>
<table class="widefat importers striped">
<?php
foreach ( $external_media_sources as $external_media_source ) {
$slug = $external_media_source['slug'];
$name = $external_media_source['name'];
$description = $external_media_source['description'];
$action = sprintf(
'<a aria-label="%1$s" style="cursor:pointer;" data-slug="%2$s">%3$s</a>',
/* translators: %s: The name of the external media source. */
esc_attr( sprintf( __( 'Import %s', 'jetpack-external-media' ), $name ) ),
esc_attr( $slug ),
__( 'Import now', 'jetpack-external-media' )
);

?>
<tr class='importer-item'>
<td class='import-system'>
<span class='importer-title'><?php echo esc_html( $name ); ?></span>
<span class='importer-action'>
<?php echo $action; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- we escape things above. ?>
</span>
</td>
<td class='desc'>
<span class='importer-desc'><?php echo esc_html( $description ); ?></span>
</td>
</tr>
<?php
}
?>
</table>
<div id="jetpack-external-media-import-modal"></div>
</div>
<?php
}
Loading
Loading