WordPress.org

Make WordPress Core

Opened 4 months ago

Closed 4 months ago

#23400 closed defect (bug) (fixed)

insert_blog() can possible return the wrong blog id when plugin/theme uses refresh_blog_details action

Reported by: feedmeastraycat Owned by: nacin
Priority: normal Milestone: 3.6
Component: Multisite Version: 3.0
Severity: normal Keywords: has-patch commit
Cc:

Description

The function insert_blog() returns $wpdb->insert_id after calling refresh_blog_details(). So if a plugin or theme is using the refresh_blog_details action and does something that changed $wpdb, the returned blog id will be wrong and WordPress creates table names with the wrong id in their name.

I understand if this isn't considered a bug. But I've attached a suggested patch anyway. I think it would be good.

Attachments (2)

ms-functions.diff (579 bytes) - added by feedmeastraycat 4 months ago.
insert-blog-bug.php (1.7 KB) - added by feedmeastraycat 4 months ago.
Plugin that duplicates the error

Download all attachments as: .zip

Change History (7)

comment:1 nacin4 months ago

  • Keywords has-patch commit added
  • Milestone changed from Awaiting Review to 3.6

Looks proper to me.

comment:2 SergeyBiryukov4 months ago

  • Component changed from General to Multisite
  • Version changed from trunk to 3.0

feedmeastraycat4 months ago

Plugin that duplicates the error

comment:3 feedmeastraycat4 months ago

I uploaded insert-blog-bug.php that is a plugin that duplicates the error.

Warning
Only run this on a clean install. To duplicate the error I truncate wp_posts and then changes the auto increment value to 100. This way, when a post is inserted it gets id 100 and the error is shown with insert_blog() returning 100 instead of the newly created actual blog id 2.

Test it by creating a multi site install and then visiting http://your-test-site.com/?action=testbug - It will output "Created blog id: 100" if the bug was visible. If there is an error the WP_Error object us output using var_dump().

This is a screenshot of my tables after running the plugin.

This is a screenshot of the Sites panel in network admin.

Running it will my patch creates everything ok.

comment:4 dd324 months ago

  • Type changed from enhancement to defect (bug)

Looks proper from here too, and for what it's worth, definitely a bug.

comment:5 nacin4 months ago

  • Owner set to nacin
  • Resolution set to fixed
  • Status changed from new to closed

In 23511:

In insert_blog(), make sure we retrieve the insertion ID from wpdb before calling a function that could invoke queries. props feedmeastraycat. fixes #23400.

Note: See TracTickets for help on using tickets.