Fix deletion of wrong assets in multisite setup#1162
Merged
gabrielcld2 merged 3 commits intodevelopfrom Apr 24, 2026
Merged
Conversation
PatelUtkarsh
approved these changes
Apr 24, 2026
| */ | ||
| protected function add_asset_parent( $post ) { | ||
| if ( is_multisite() ) { | ||
| $post->blog_id = get_current_blog_id(); |
Contributor
There was a problem hiding this comment.
💡 thought: Stamping blog_id as a dynamic property on WP_Post only works because WP core annotated WP_Post with #[AllowDynamicProperties] (WP 6.4+). On sites running PHP 8.2+ with older WordPress, this triggers deprecation warnings. Which is odd combo anyway.
Collaborator
Author
There was a problem hiding this comment.
Good point! It's probably fine if it's just a deprecation warning given the odd combination of WP & PHP version like you said, it's quite unlikely to happen.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #1138
Context
This bug is related to non-media assets such as Theme files, plugin files, WP core files, and upload-dir content.
The issue
In a WordPress Multisite, PHP can switch blog contexts within the same process via switch_to_blog() — this happens during cron, AJAX handlers, WooCommerce, WPML, and other plugins.
If the switch happen during the assets purging process, current Cloudinary plugin might delete assets from the wrong site.
The scenario
What the Fix Does
create_asset_parent().
QA Notes
Prerequisites
Setup Phase
On Site B:
Record the baseline (DB check)
(Note: Replace
wp_3_with your Site B's table prefix if different):Note the IDs and count of Site B's asset parents.
Reproducing the issue
Reproducing the Cross-Blog Scenario (Reliable Method via WP-CLI)
Run the following the command via WP CLI (make sure to replace the URL param to point to your site A, and site B's ID in the switch_to_blog call within the script if needed):
This script basically emulates what a cron or plugin might do which is to trigger the assets parents purge across both sites while switching to another blog.
Verify
After running, re-check the DB:
SELECT COUNT(*) FROM wp_3_posts WHERE post_type = 'cloudinary_asset' AND post_parent = 0;Expected with the fix: Count is unchanged. Site B's parents survive and didn't get deleted.
Testing Normal Purge Still Works (Regression)
To ensure the fix doesn't break the legitimate purge behavior: