Skip to content

Share Gallery media library handling between GutenbergKit editors - #25855

Merged
crazytonyli merged 2 commits into
release/27.1from
fix/custom-post-gallery-media-library
Jul 30, 2026
Merged

Share Gallery media library handling between GutenbergKit editors#25855
crazytonyli merged 2 commits into
release/27.1from
fix/custom-post-gallery-media-library

Conversation

@crazytonyli

Copy link
Copy Markdown
Contributor

Description

Fixes https://linear.app/a8c/issue/CMM-2193

I targeted at the release branch because the changes are low-risk: code formatting and movements.

@crazytonyli crazytonyli added this to the 27.1 ❄️ milestone Jul 30, 2026
@crazytonyli
crazytonyli requested a review from dcalhoun July 30, 2026 05:22
@dangermattic

Copy link
Copy Markdown
Collaborator
1 Warning
⚠️ This PR is assigned to the milestone 27.1 ❄️. The due date for this milestone has already passed.
Please assign it to a milestone with a later deadline or check whether the release for this milestone has already been finished.

Generated by 🚫 Danger

@wpmobilebot

Copy link
Copy Markdown
Contributor
App Icon📲 You can test the changes from this Pull Request in WordPress by scanning the QR code below to install the corresponding build.
App NameWordPress
ConfigurationRelease-Alpha
Build Number33543
VersionPR #25855
Bundle IDorg.wordpress.alpha
Commit35fff93
Installation URL5gvfrmfa7740o
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

@wpmobilebot

Copy link
Copy Markdown
Contributor
App Icon📲 You can test the changes from this Pull Request in Jetpack by scanning the QR code below to install the corresponding build.
App NameJetpack
ConfigurationRelease-Alpha
Build Number33543
VersionPR #25855
Bundle IDcom.jetpack.alpha
Commit35fff93
Installation URL6jenff60g4ln8
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

@wpmobilebot

Copy link
Copy Markdown
Contributor

🤖 Build Failure Analysis

This build has failures. Claude has analyzed them - check the build annotations for details.

@dcalhoun dcalhoun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look sound to me. The Media Library button now consistently functions as expected. 🚀

It took me too long to reproduce the original bug. I was unable to reproduce with xmlrpc-disabled.wpmt.co or xmlrpc-disabled-by-plugin.wpmt.co. I only reproduced with cpt.wpmt.co + installing the Hostinger Tools plugin to disable XML-RPC; I also needed to pull-to-refresh on the site's My Site view after adding it. I'm still puzzled as to why I cannot reproduce on the other sites.

assert(Thread.isMainThread, "mapMediaIdsToMedia should only be called on the main thread")
let context = ContextManager.shared.mainContext
let request = NSFetchRequest<NSManagedObject>(entityName: "Media")
request.predicate = NSPredicate(format: "mediaID IN %@", mediaIds.map { NSNumber(value: $0) })

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting a pre-existing issue finding by Claude that we might address in a follow-up PR:

Should this fetch be scoped to the blog? mediaID is the remote attachment ID, so it's only unique within a site — with two sites signed in, ID 42 can exist on both. The resolved objects flow through setInitialSelectionselectedMedia, neither of which checks blog membership, so a Gallery block could pre-select (and on Done, return) the wrong site's media. There's also a uniqueKeysWithValues trap just below if two sites both have a matching ID cached.

Pre-existing, and it only bites on the multiple-selection path — but now that the helper holds a blog, "blog == %@ AND mediaID IN %@" would close it. Happy to leave for a follow-up if you'd rather keep this diff a pure refactor.

caption: item.caption,
title: item.filename,
alt: item.alt,
metadata: [:]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting a pre-existing issue finding by Claude that we might address in a follow-up PR:

Is passing metadata: [:] here intentional? The metadata dict built just above with videopressGUID never gets used, so the GUID is dropped before it reaches the web editor — MediaInfo.metadata is an encoded field. No compiler warning since it is mutated inside the if.

Moved verbatim, so pre-existing — but worth flagging since this path now serves custom post types too. Looks like a one-word fix (metadata: metadata) if you want it here rather than a follow-up.

case .any:
mediaType = mediaType | GutenbergMediaType.all.rawValue
@unknown default:
fatalError()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting a pre-existing issue finding by Claude that we might address in a follow-up PR:

Could this fatalError() be a softer fallback? Keeping the @unknown default makes sense — it's required for a non-frozen enum from GutenbergKit, and it's what stops a new library case from being a breaking change. It's just that trapping in the body turns that safety net into a crash: a dependency bump alone arms it, and the next media-library request for the new type takes the app down mid-edit with unsaved content. It'd hit both editors now that this lives in the base class.

break looks sufficient — contributing no flag leaves an empty GutenbergMediaType, and both MediaPickerMenu.MediaFilter.init? and PHPickerFilter.init? already return nil for anything that isn't exactly .image/.video, which MediaPickerMenu treats as "no filter." So it degrades to an unfiltered picker rather than an empty one. (.all returns nil there too, so it'd behave identically — break just reads more honestly.) Fine as a follow-up if you'd rather keep this diff a pure refactor.

@crazytonyli

Copy link
Copy Markdown
Contributor Author

I'll merge this PR for now. I will look into those issues later.

@crazytonyli
crazytonyli merged commit 2419535 into release/27.1 Jul 30, 2026
30 checks passed
@crazytonyli
crazytonyli deleted the fix/custom-post-gallery-media-library branch July 30, 2026 21:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants