-
Notifications
You must be signed in to change notification settings - Fork 0
fix: use a new function for adding notice for SSL issue and already o… #61
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
Open
Arukuen
wants to merge
2
commits into
develop
Choose a base branch
from
feat/notice
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
|
|
||
| /** | ||
| * This script is in charge of keeping track of temporary upload notices for | ||
| * media conversion results, then matching those notices back to the Media | ||
| * Library attachment after WordPress finishes uploading and renaming the file. | ||
| * The cache is kept in memory only, so refreshing the page clears all notices. | ||
| */ | ||
|
|
||
| /** | ||
| * Create the cache key used to match an upload notice to a Media Library item. | ||
| * | ||
| * @param {string} filename - baseFile or WordPress-generated filename. | ||
| * @return {string|undefined} Sanitized cache key. | ||
| */ | ||
| const getCacheKey = filename => filename | ||
| // Match WordPress filename sanitization to ensure consistent cache keys. | ||
| ?.replace( /[^a-zA-Z0-9._-]+/g, '-' ) | ||
| // Collapse repeated dashes created by the previous replacement. | ||
| .replace( /-+/g, '-' ) | ||
| // Remove leading/trailing dashes around the filename. | ||
| .replace( /^-|-$/g, '' ) | ||
| .toLowerCase() | ||
|
|
||
| /** | ||
| * Remove the numeric suffix WordPress adds when a filename already exists. | ||
| * | ||
| * @param {string} filename - WordPress-generated filename. | ||
| * @return {string|undefined} Filename without the final WordPress suffix. | ||
| */ | ||
| const stripFilenameSuffix = filename => { | ||
| // Convert `video-3.mov` back to `video.mov` for one-time self-correction. | ||
| return filename?.replace( /-\d+(\.[^.]+)$/, '$1' ) | ||
| } | ||
|
|
||
| /** | ||
| * Find a temporary upload notice for a Media Library attachment. | ||
| * | ||
| * @param {string} filename - Filename from the media model. | ||
| * @return {Object|null} Matching upload notice, if any. | ||
| */ | ||
| export const getCachedUploadNotice = filename => { | ||
| if ( ! filename ) { | ||
| return null | ||
| } | ||
|
|
||
| const cache = window.cimoUploadNoticeCache || {} | ||
|
|
||
| // Get the filename which may have a suffix, and try to find a notice for it first. | ||
| const cacheKey = getCacheKey( filename ) | ||
| if ( cache[ cacheKey ] ) { | ||
| return cache[ cacheKey ] | ||
| } | ||
|
|
||
| // Extract the base filename without the suffix and check if there's a notice for it. | ||
| // If no cache with the base filename exists, then there's no notice for this upload at all. | ||
| const baseFileKey = getCacheKey( stripFilenameSuffix( filename ) ) | ||
| const baseFileNotice = cache[ baseFileKey ] | ||
| if ( ! baseFileNotice ) { | ||
| return null | ||
| } | ||
|
|
||
| // Copy the notice to WordPress's final filename key. | ||
| // Also remove remainingMatches from the cache since it's only needed for the base filename to track when to remove itself. | ||
| cache[ cacheKey ] = { | ||
| ...baseFileNotice, | ||
| } | ||
| delete cache[ cacheKey ].remainingMatches | ||
|
|
||
| // Keep the baseFile key only while more suffixed filenames still need it. | ||
| if ( baseFileNotice.remainingMatches > 1 ) { | ||
| baseFileNotice.remainingMatches-- | ||
| } else { | ||
| // Remove the base fallback so older similar media cannot keep matching it. | ||
| delete cache[ baseFileKey ] | ||
| } | ||
|
|
||
| return cache[ cacheKey ] | ||
| } | ||
|
|
||
| /** | ||
| * Cache a temporary upload notice message under a sanitized filename key. | ||
| * | ||
| * @param {string} filename - Browser File.name from the failed upload. | ||
| * @param {string} message - Notice message to display in the media manager. | ||
| */ | ||
| export const setCachedUploadNotice = ( filename, message ) => { | ||
| if ( ! filename ) { | ||
| return | ||
| } | ||
|
|
||
| // Keep notices in memory only; refreshing the page clears them. | ||
| if ( ! window.cimoUploadNoticeCache ) { | ||
| window.cimoUploadNoticeCache = {} | ||
| } | ||
|
|
||
| // Generate the cache key based on the filename, matching WordPress's sanitization. | ||
| // This will always generate the basename, and is not guaranteed to match the final WordPress filename, | ||
| // which may have a suffix added (`-1` or `-2`.). | ||
| const baseFileKey = getCacheKey( filename ) | ||
| const cachedNotice = window.cimoUploadNoticeCache[ baseFileKey ] | ||
|
|
||
| window.cimoUploadNoticeCache[ baseFileKey ] = { | ||
| message, | ||
| // Count duplicate uploads with the same browser filename so | ||
| // each suffixed item can self-correct once. | ||
| remainingMatches: ( cachedNotice?.remainingMatches || 0 ) + 1, | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Create and cache an upload notice from a converter result. | ||
| * | ||
| * @param {Object} result - Converter result. | ||
| * @return {string|null} Cached notice message when the result includes one. | ||
| */ | ||
| export const cacheConverterNotice = result => { | ||
| const file = result?.file | ||
| if ( ! file?.name ) { | ||
| return null | ||
| } | ||
|
|
||
| // The converter result decides whether a sidebar notice should be shown. | ||
| if ( ! result?.notice ) { | ||
| return null | ||
| } | ||
|
|
||
| // Store only the display message; matching metadata is added by the cache layer. | ||
| setCachedUploadNotice( file.name, result.notice ) | ||
| return result.notice | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.