From 9f7bde1ecb7a4968aa861dac866109df5c92d872 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Wed, 17 Jan 2024 18:03:33 +0000 Subject: [PATCH 01/10] Fix delete endpoint --- .../font-library/class-wp-rest-font-families-controller.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php b/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php index 89bc88020e3330..a48c9cbe1a7763 100644 --- a/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php +++ b/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php @@ -263,6 +263,8 @@ public function delete_item( $request ) { foreach ( $this->get_font_face_ids( $font_family_id ) as $font_face_id ) { wp_delete_post( $font_face_id, true ); } + + return $deleted; } /** From 1c9aa8d2d745c1d65831d0191e3ef491df66a516 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Wed, 17 Jan 2024 18:05:31 +0000 Subject: [PATCH 02/10] Update fetchUninstallFontFamily to match new format --- .../global-styles/font-library-modal/resolvers.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/edit-site/src/components/global-styles/font-library-modal/resolvers.js b/packages/edit-site/src/components/global-styles/font-library-modal/resolvers.js index df10904b75026f..baaa6985995538 100644 --- a/packages/edit-site/src/components/global-styles/font-library-modal/resolvers.js +++ b/packages/edit-site/src/components/global-styles/font-library-modal/resolvers.js @@ -57,14 +57,10 @@ export async function fetchGetFontFamilyBySlug( slug ) { } ); } -export async function fetchUninstallFonts( fonts ) { - const data = { - font_families: fonts, - }; +export async function fetchUninstallFontFamily( fontFamilyId ) { const config = { - path: '/wp/v2/font-families', + path: `/wp/v2/font-families/${ fontFamilyId }?force=true`, method: 'DELETE', - data, }; return apiFetch( config ); } From 9b90a1835c31d84f4946364ae3f74918d0d97740 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Wed, 17 Jan 2024 18:08:53 +0000 Subject: [PATCH 03/10] Update uninstallFont --- .../font-library-modal/context.js | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/packages/edit-site/src/components/global-styles/font-library-modal/context.js b/packages/edit-site/src/components/global-styles/font-library-modal/context.js index 078d11e235c043..9f44d3ea89ff36 100644 --- a/packages/edit-site/src/components/global-styles/font-library-modal/context.js +++ b/packages/edit-site/src/components/global-styles/font-library-modal/context.js @@ -17,7 +17,7 @@ import { __ } from '@wordpress/i18n'; import { fetchGetFontFamilyBySlug, fetchInstallFontFamily, - fetchUninstallFonts, + fetchUninstallFontFamily, fetchFontCollections, fetchFontCollection, } from './resolvers'; @@ -296,13 +296,21 @@ function FontLibraryProvider( { children } ) { } } - async function uninstallFont( font ) { + async function uninstallFont( fontFamilyToUninstall ) { try { - // Uninstall the font (remove the font files from the server and the post from the database). - const response = await fetchUninstallFonts( [ font ] ); + // Get the font family data by slug. + const fontFamilyToUninstallData = await fetchGetFontFamilyBySlug( + fontFamilyToUninstall.slug + ); + + // Uninstall the font (remove the font files from the server and the posts from the database). + const uninstalledFontFamily = await fetchUninstallFontFamily( + fontFamilyToUninstallData.id + ); + // Deactivate the font family (remove the font family from the global styles). - if ( 0 === response.errors.length ) { - deactivateFontFamily( font ); + if ( uninstalledFontFamily.deleted ) { + deactivateFontFamily( fontFamilyToUninstall ); // Save the global styles to the database. await saveSpecifiedEntityEdits( 'root', @@ -311,9 +319,11 @@ function FontLibraryProvider( { children } ) { [ 'settings.typography.fontFamilies' ] ); } + // Refresh the library (the library font families from database). refreshLibrary(); - return response; + + return uninstalledFontFamily; } catch ( error ) { // eslint-disable-next-line no-console console.error( error ); From bcbbd81843a5b4d3602e9a06109bb2ab7bba5cd8 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Wed, 17 Jan 2024 18:28:47 +0000 Subject: [PATCH 04/10] Add uninstall notice back in --- .../font-library-modal/installed-fonts.js | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/packages/edit-site/src/components/global-styles/font-library-modal/installed-fonts.js b/packages/edit-site/src/components/global-styles/font-library-modal/installed-fonts.js index 0c481f6e4d8ca8..467cf619272536 100644 --- a/packages/edit-site/src/components/global-styles/font-library-modal/installed-fonts.js +++ b/packages/edit-site/src/components/global-styles/font-library-modal/installed-fonts.js @@ -48,15 +48,24 @@ function InstalledFonts() { const [ notice, setNotice ] = useState( null ); const handleConfirmUninstall = async () => { - const response = await uninstallFont( libraryFontSelected ); - // TODO: Refactor uninstall notices - // const uninstallNotice = getNoticeFromUninstallResponse( response ); - // setNotice( uninstallNotice ); - // If the font was succesfully uninstalled it is unselected - if ( ! response?.errors?.length ) { + try { + await uninstallFont( libraryFontSelected ); + setNotice( { + type: 'success', + message: __( 'Fonts were uninstalled successfully.' ), + } ); + + // If the font was succesfully uninstalled it is unselected handleUnselectFont(); + setIsConfirmDeleteOpen( false ); + } catch ( error ) { + setNotice( { + type: 'error', + message: + __( 'There was an error uninstalling the fonts.' ) + + error.message, + } ); } - setIsConfirmDeleteOpen( false ); }; const handleUninstallClick = async () => { From c96bf23afbf867ecff52eaf6fae765c2fa11b507 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Wed, 17 Jan 2024 18:36:50 +0000 Subject: [PATCH 05/10] Tidy up comments --- .../components/global-styles/font-library-modal/context.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/edit-site/src/components/global-styles/font-library-modal/context.js b/packages/edit-site/src/components/global-styles/font-library-modal/context.js index 9f44d3ea89ff36..28aa21073429a6 100644 --- a/packages/edit-site/src/components/global-styles/font-library-modal/context.js +++ b/packages/edit-site/src/components/global-styles/font-library-modal/context.js @@ -303,12 +303,14 @@ function FontLibraryProvider( { children } ) { fontFamilyToUninstall.slug ); - // Uninstall the font (remove the font files from the server and the posts from the database). + // Uninstall the font family. + // (Removes the font files from the server and the posts from the database). const uninstalledFontFamily = await fetchUninstallFontFamily( fontFamilyToUninstallData.id ); - // Deactivate the font family (remove the font family from the global styles). + // Deactivate the font family if delete request is successful + // (Removes the font family from the global styles). if ( uninstalledFontFamily.deleted ) { deactivateFontFamily( fontFamilyToUninstall ); // Save the global styles to the database. From 454eb26dc1876150d72bfc14b49d718ef6c3cd77 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Wed, 17 Jan 2024 18:38:31 +0000 Subject: [PATCH 06/10] Re-word uninstall notices --- .../global-styles/font-library-modal/installed-fonts.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/edit-site/src/components/global-styles/font-library-modal/installed-fonts.js b/packages/edit-site/src/components/global-styles/font-library-modal/installed-fonts.js index 467cf619272536..64b3ce5853ee19 100644 --- a/packages/edit-site/src/components/global-styles/font-library-modal/installed-fonts.js +++ b/packages/edit-site/src/components/global-styles/font-library-modal/installed-fonts.js @@ -52,17 +52,17 @@ function InstalledFonts() { await uninstallFont( libraryFontSelected ); setNotice( { type: 'success', - message: __( 'Fonts were uninstalled successfully.' ), + message: __( 'Font family uninstalled successfully.' ), } ); - // If the font was succesfully uninstalled it is unselected + // If the font was succesfully uninstalled it is unselected. handleUnselectFont(); setIsConfirmDeleteOpen( false ); } catch ( error ) { setNotice( { type: 'error', message: - __( 'There was an error uninstalling the fonts.' ) + + __( 'There was an error uninstalling the font family.' ) + error.message, } ); } From 371a2219d9f3f0c3d34663fc9d8aea8085ed3f11 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Wed, 17 Jan 2024 18:44:04 +0000 Subject: [PATCH 07/10] Add spacing to error message --- .../global-styles/font-library-modal/installed-fonts.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/edit-site/src/components/global-styles/font-library-modal/installed-fonts.js b/packages/edit-site/src/components/global-styles/font-library-modal/installed-fonts.js index 64b3ce5853ee19..a2f30b174ef660 100644 --- a/packages/edit-site/src/components/global-styles/font-library-modal/installed-fonts.js +++ b/packages/edit-site/src/components/global-styles/font-library-modal/installed-fonts.js @@ -62,7 +62,7 @@ function InstalledFonts() { setNotice( { type: 'error', message: - __( 'There was an error uninstalling the font family.' ) + + __( 'There was an error uninstalling the font family. ' ) + error.message, } ); } From 530939fceb5fc56bc8a98c07d144abf647548dd5 Mon Sep 17 00:00:00 2001 From: Jason Crist Date: Wed, 17 Jan 2024 14:24:20 -0500 Subject: [PATCH 08/10] Refactored how font family values were processed so they would retain their id, which is now used to delete a font family without fetching data via slug --- .../font-library-modal/context.js | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/packages/edit-site/src/components/global-styles/font-library-modal/context.js b/packages/edit-site/src/components/global-styles/font-library-modal/context.js index 28aa21073429a6..eda839ce3be830 100644 --- a/packages/edit-site/src/components/global-styles/font-library-modal/context.js +++ b/packages/edit-site/src/components/global-styles/font-library-modal/context.js @@ -70,12 +70,15 @@ function FontLibraryProvider( { children } ) { } ); const libraryFonts = - ( libraryPosts || [] ).map( ( post ) => { - post.font_family_settings.fontFace = - post?._embedded?.font_faces.map( - ( face ) => face.font_face_settings - ) || []; - return post.font_family_settings; + ( libraryPosts || [] ).map( ( fontFamilyPost ) => { + return { + id: fontFamilyPost.id, + ...fontFamilyPost.font_family_settings, + fontFace: + fontFamilyPost?._embedded?.font_faces.map( + ( face ) => face.font_face_settings + ) || [], + }; } ) || []; // Global Styles (settings) font families @@ -298,15 +301,10 @@ function FontLibraryProvider( { children } ) { async function uninstallFont( fontFamilyToUninstall ) { try { - // Get the font family data by slug. - const fontFamilyToUninstallData = await fetchGetFontFamilyBySlug( - fontFamilyToUninstall.slug - ); - // Uninstall the font family. // (Removes the font files from the server and the posts from the database). const uninstalledFontFamily = await fetchUninstallFontFamily( - fontFamilyToUninstallData.id + fontFamilyToUninstall.id ); // Deactivate the font family if delete request is successful From 46124cb450f12b33fe83dff9c39b5531c9ca9da5 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Wed, 17 Jan 2024 19:23:44 +0000 Subject: [PATCH 09/10] Rename uninstallFont to uninstallFontFamily --- .../components/global-styles/font-library-modal/context.js | 4 ++-- .../global-styles/font-library-modal/installed-fonts.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/edit-site/src/components/global-styles/font-library-modal/context.js b/packages/edit-site/src/components/global-styles/font-library-modal/context.js index eda839ce3be830..480c9e1d7af607 100644 --- a/packages/edit-site/src/components/global-styles/font-library-modal/context.js +++ b/packages/edit-site/src/components/global-styles/font-library-modal/context.js @@ -299,7 +299,7 @@ function FontLibraryProvider( { children } ) { } } - async function uninstallFont( fontFamilyToUninstall ) { + async function uninstallFontFamily( fontFamilyToUninstall ) { try { // Uninstall the font family. // (Removes the font files from the server and the posts from the database). @@ -441,7 +441,7 @@ function FontLibraryProvider( { children } ) { getFontFacesActivated, loadFontFaceAsset, installFont, - uninstallFont, + uninstallFontFamily, toggleActivateFont, getAvailableFontsOutline, modalTabOpen, diff --git a/packages/edit-site/src/components/global-styles/font-library-modal/installed-fonts.js b/packages/edit-site/src/components/global-styles/font-library-modal/installed-fonts.js index a2f30b174ef660..714f91f4bc04c7 100644 --- a/packages/edit-site/src/components/global-styles/font-library-modal/installed-fonts.js +++ b/packages/edit-site/src/components/global-styles/font-library-modal/installed-fonts.js @@ -32,7 +32,7 @@ function InstalledFonts() { baseThemeFonts, handleSetLibraryFontSelected, refreshLibrary, - uninstallFont, + uninstallFontFamily, isResolvingLibrary, } = useContext( FontLibraryContext ); const [ isConfirmDeleteOpen, setIsConfirmDeleteOpen ] = useState( false ); @@ -49,7 +49,7 @@ function InstalledFonts() { const handleConfirmUninstall = async () => { try { - await uninstallFont( libraryFontSelected ); + await uninstallFontFamily( libraryFontSelected ); setNotice( { type: 'success', message: __( 'Font family uninstalled successfully.' ), From bed9b8a8d114b7296ae0f1485481327552e1c6af Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Wed, 17 Jan 2024 19:27:59 +0000 Subject: [PATCH 10/10] Throw uninstall errors rather than returning them --- .../global-styles/font-library-modal/context.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/edit-site/src/components/global-styles/font-library-modal/context.js b/packages/edit-site/src/components/global-styles/font-library-modal/context.js index 480c9e1d7af607..c653d0a8b03626 100644 --- a/packages/edit-site/src/components/global-styles/font-library-modal/context.js +++ b/packages/edit-site/src/components/global-styles/font-library-modal/context.js @@ -326,10 +326,11 @@ function FontLibraryProvider( { children } ) { return uninstalledFontFamily; } catch ( error ) { // eslint-disable-next-line no-console - console.error( error ); - return { - errors: [ error ], - }; + console.error( + `There was an error uninstalling the font family:`, + error + ); + throw error; } }