From 7755d061206829076759244d8a2ea4906226ebf9 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Mon, 30 Aug 2021 19:53:13 +0400 Subject: [PATCH 1/2] Gallery Block: Get media data in a single request --- .../block-library/src/gallery/use-get-media.js | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/packages/block-library/src/gallery/use-get-media.js b/packages/block-library/src/gallery/use-get-media.js index 2d70119e4aa229..fd496bcc52bd9d 100644 --- a/packages/block-library/src/gallery/use-get-media.js +++ b/packages/block-library/src/gallery/use-get-media.js @@ -29,23 +29,18 @@ export default function useGetMedia( innerBlockImages ) { if ( imageIds.length === 0 ) { return currentImageMedia; } - const getMedia = select( coreStore ).getMedia; - const newImageMedia = imageIds.map( ( img ) => { - return getMedia( img ); - } ); - if ( newImageMedia.some( ( img ) => ! img ) ) { - return currentImageMedia; - } - - return newImageMedia; + return select( coreStore ).getMediaItems( { + include: imageIds.join( ',' ), + per_page: imageIds.length, + } ); }, [ innerBlockImages ] ); if ( - imageMedia?.length !== currentImageMedia.length || - imageMedia.some( + imageMedia?.length !== currentImageMedia?.length || + imageMedia?.some( ( newImage ) => ! currentImageMedia.find( ( currentImage ) => currentImage.id === newImage.id From 9da683a8bb5305f9c563c052e2eec4b09e7f2724 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 30 Sep 2021 16:53:11 +0400 Subject: [PATCH 2/2] Set per_page to -1 --- packages/block-library/src/gallery/use-get-media.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/gallery/use-get-media.js b/packages/block-library/src/gallery/use-get-media.js index fd496bcc52bd9d..0114db392bc345 100644 --- a/packages/block-library/src/gallery/use-get-media.js +++ b/packages/block-library/src/gallery/use-get-media.js @@ -32,7 +32,7 @@ export default function useGetMedia( innerBlockImages ) { return select( coreStore ).getMediaItems( { include: imageIds.join( ',' ), - per_page: imageIds.length, + per_page: -1, } ); }, [ innerBlockImages ]