From 6e351ad7f0b2fc2c964b90856fb05cee543c1252 Mon Sep 17 00:00:00 2001 From: lgersman Date: Mon, 5 Feb 2024 15:33:30 +0100 Subject: [PATCH] feat: new option "cleanup media" added to configure fine grained what data should be cleaned up before import --- .../inc/class-impex-import.php | 32 +++++++------------ .../cm4all-wp-impex/src/components/import.mjs | 20 ++++++++---- .../cm4all-wp-impex/src/components/screen.mjs | 25 ++++++++++++--- .../cm4all-wp-impex/src/wp.impex.store.mjs | 23 ++++++------- 4 files changed, 57 insertions(+), 43 deletions(-) diff --git a/plugins/cm4all-wp-impex/inc/class-impex-import.php b/plugins/cm4all-wp-impex/inc/class-impex-import.php index 65c66d8..f00c74b 100644 --- a/plugins/cm4all-wp-impex/inc/class-impex-import.php +++ b/plugins/cm4all-wp-impex/inc/class-impex-import.php @@ -33,10 +33,14 @@ abstract class ImpexImport extends ImpexPart const WP_FILTER_PROFILES = 'impex_import_filter_profiles'; - // post, media, block pattern, nav_menu an reusable block items + // cleanup post, block pattern, nav_menu an reusable block items const OPTION_CLEANUP_CONTENTS = 'impex-import-option-cleanup_contents'; const OPTION_CLEANUP_CONTENTS_DEFAULT = false; + // cleanup media items + const OPTION_CLEANUP_MEDIA = 'impex-import-option-cleanup_media'; + const OPTION_CLEANUP_MEDIA_DEFAULT = false; + protected function _createProvider(string $name, callable $cb): ImpexImportProvider { return new class($name, $cb) extends ImpexImportProvider @@ -174,7 +178,7 @@ function consume(ImpexImportTransformationContext $transformationContext, int $l // do clean up before importing first slices if($offset===0) { - if(($options[self::OPTION_CLEANUP_CONTENTS] ?? false) == true) { + if(($options[self::OPTION_CLEANUP_CONTENTS] ?? self::OPTION_CLEANUP_CONTENTS_DEFAULT) == true) { $menus = \wp_get_nav_menus(['fields' => 'ids' ]); foreach ($menus as $menu) { \wp_delete_nav_menu( $menu); @@ -184,14 +188,18 @@ function consume(ImpexImportTransformationContext $transformationContext, int $l foreach ($postsToDelete as $postToDelete) { \wp_delete_post( $postToDelete, true ); } + } + if(($options[self::OPTION_CLEANUP_MEDIA] ?? self::OPTION_CLEANUP_MEDIA_DEFAULT) == true) { $attachmentsToDelete= \get_posts( ['post_type'=>'attachment','numberposts'=>-1,'fields' => 'ids'] ); foreach ($attachmentsToDelete as $attachmentToDelete) { \wp_delete_attachment($attachmentToDelete, true); } - } else { - $this->_delete_transient_import_metadata(); } + + /*else { + $this->_delete_transient_import_metadata(); + }*/ } foreach ($this->get_slices($transformationContext->id, $limit, $offset) as $slice) { @@ -257,22 +265,6 @@ function($accu, $row) { }, [], ); - /* - array_reduce( - \get_posts([ - 'fields' => 'ids', - 'numberposts'=>-1, - 'post_type' => 'any', - 'meta_query' => [ - ['key' => self::KEY_TRANSIENT_IMPORT_METADATA, 'compare' => 'EXISTS', ], - ], - ]), - function($accu, $post_id) { - $accu[(int)\get_post_meta($post_id, self::KEY_TRANSIENT_IMPORT_METADATA, true)] = $post_id; - return $accu; - }, - [], - );*/ $imported = [ 'terms' => &$imported_term_ids, diff --git a/plugins/cm4all-wp-impex/src/components/import.mjs b/plugins/cm4all-wp-impex/src/components/import.mjs index a2e54d6..670e97b 100644 --- a/plugins/cm4all-wp-impex/src/components/import.mjs +++ b/plugins/cm4all-wp-impex/src/components/import.mjs @@ -53,6 +53,7 @@ export default function Import() { const [importProfile, setImportProfile] = element.useState(); const [cleanupContent, setCleanupContent] = element.useState(true); + const [cleanupMedia, setCleanupMedia] = element.useState(true); element.useEffect(() => { if (importProfiles.length === 1) { @@ -75,17 +76,17 @@ export default function Import() { ), }); - await consumeImport(_import.id, { + await consumeImport(_import.id, { // @see PHP class ImpexExport::OPTION_CLEANUP_CONTENTS 'impex-import-option-cleanup_contents' : cleanupContent, - }, - null, + }, + null, null ); setProgress(); }; - + const onUpload = async () => { let importDirHandle = null; // showDirectoryPicker will throw a DOMException in case the user pressed cancel @@ -158,13 +159,20 @@ export default function Import() { className="import-options-form" > + + {imports.map((_, index) => ( { setProgress({ component: ( @@ -124,10 +124,20 @@ function SimpleTab() { const [progress, setProgress] = element.useState(null); const [cleanupContent, setCleanupContent] = element.useState(true); + const [cleanupMedia, setCleanupMedia] = element.useState(true); const _createAndUploadConsumeImport = async () => { console.log({ importProfile, screenContext }); - const gen = await createAndUploadConsumeImport(importProfile, cleanupContent, screenContext); + const gen = await createAndUploadConsumeImport( + importProfile, + { + // @see PHP class ImpexExport::OPTION_CLEANUP_CONTENTS + 'impex-import-option-cleanup_contents' : cleanupContent, + // @see PHP class ImpexExport::OPTION_CLEANUP_MEDIA + 'impex-import-option-cleanup_media' : cleanupMedia, + }, + screenContext + ); await ImportExportGeneratorConsumer(gen, setProgress, __("Import failed", "cm4all-wp-impex")); }; @@ -170,14 +180,21 @@ function SimpleTab() { onChange={setImportProfile} /> + + + createAndUploadConsumeImport : (importProfile, importOptions, screenContext) => async function* ({ dispatch, registry, resolveSelect, select }) { - debug({importProfile, cleanupContent}); + debug({importProfile, importOptions}); let importDirHandle = null; // showDirectoryPicker will throw a DOMException in case the user pressed cancel @@ -62,7 +62,7 @@ export default async function (settings) { importProfile, { })) .payload; - + try { yield { type: "progress", @@ -82,12 +82,9 @@ export default async function (settings) { await dispatch.consumeImport( - createdImport.id, - { - // @see PHP class ImpexExport::OPTION_CLEANUP_CONTENTS - 'impex-import-option-cleanup_contents' : cleanupContent, - }, - null, + createdImport.id, + importOptions, + null, null ); @@ -179,10 +176,10 @@ export default async function (settings) { ); debug({ exportDirHandle }); - + let createdExport = null; - try { + try { // const exports = select.getExports(); // debug({ exports }); @@ -356,14 +353,14 @@ export default async function (settings) { data: { options }, }); - // process returned callbacks + // process returned callbacks const postConsumeCallbacks = callbacks.map( callback => apiFetch({ path : `${settings.base_uri}/${callback.path}`, method: callback.method, data: callback.data, }).catch(error => { - // silently ignore errors from timed out metadata updates + // silently ignore errors from timed out metadata updates if(error.code==='fetch_error') { log.push({ type: 'warning',