-
Notifications
You must be signed in to change notification settings - Fork 4k
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
useShortCodeTransform: Fetch media data in single request #40945
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,21 +23,13 @@ export default function useShortCodeTransform( shortCodeTransforms ) { | |
if ( ! shortCodeTransforms || shortCodeTransforms.length === 0 ) { | ||
return; | ||
} | ||
const getMedia = select( coreStore ).getMedia; | ||
return shortCodeTransforms.map( ( image ) => { | ||
const imageData = getMedia( image.id ); | ||
if ( imageData ) { | ||
return { | ||
id: imageData.id, | ||
type: 'image', | ||
url: imageData.source_url, | ||
mime: imageData.mime_type, | ||
alt: imageData.alt_text, | ||
link: imageData.link, | ||
caption: imageData?.caption?.raw, | ||
}; | ||
} | ||
return undefined; | ||
|
||
const imageIds = shortCodeTransforms.map( ( image ) => image.id ); | ||
|
||
return select( coreStore ).getMediaItems( { | ||
include: imageIds.join( ',' ), | ||
per_page: -1, | ||
orderby: 'include', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need this to preserve the original image order from the shortcode. |
||
} ); | ||
}, | ||
[ shortCodeTransforms ] | ||
|
@@ -47,7 +39,17 @@ export default function useShortCodeTransform( shortCodeTransforms ) { | |
return; | ||
} | ||
|
||
if ( every( newImageData, ( img ) => img && img.url ) ) { | ||
return newImageData; | ||
if ( every( newImageData, ( img ) => img && img.source_url ) ) { | ||
return newImageData.map( ( imageData ) => { | ||
return { | ||
id: imageData.id, | ||
type: 'image', | ||
url: imageData.source_url, | ||
mime: imageData.mime_type, | ||
alt: imageData.alt_text, | ||
link: imageData.link, | ||
caption: imageData?.caption?.raw, | ||
}; | ||
} ); | ||
Comment on lines
+43
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved this outside of the |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This way, data cache keys are the same, so we re-use data from the store instead of re-fetching it.