WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 3 years ago

Last modified 3 years ago

#12028 closed defect (bug) (fixed)

wpmu_create_blog() breaks with multiple use

Reported by: wpmuguru Owned by:
Milestone: 3.3 Priority: high
Severity: normal Version:
Component: Multisite Keywords: has-patch
Focuses: Cc:

Description

MU Trac Ticket: http://trac.mu.wordpress.org/ticket/394

When using wpmu_create_blog() in wpmu-functions.php it will only work once. If called additional times the blog will be added to the wp_blogs table but its tables will not be created, resulting in an invalid state. This is caused by $wp_queries being set in-line in upgrade-schema.php while it must be updated before the second call to wpmu_create_blog(). This is solved by moving the initialization of $wp_queries into a function and calling it just before the tables are created (diff file attached).

Attachments (5)

wpmu-create-blog.patch (1.1 KB) - added by jpotkanski 4 years ago.
Patch for wpmu-functions.php
wpmu-create-blog.2.patch (1.1 KB) - added by jpotkanski 4 years ago.
Patch for wpmu-functions.php
12028.diff (2.6 KB) - added by ryan 3 years ago.
12028.2.diff (11.2 KB) - added by ryan 3 years ago.
12028.3.diff (13.4 KB) - added by ryan 3 years ago.

Download all attachments as: .zip

Change History (21)

comment:1 wpmuguru4 years ago

  • Milestone changed from Unassigned to Future Release

jpotkanski4 years ago

Patch for wpmu-functions.php

jpotkanski4 years ago

Patch for wpmu-functions.php

comment:2 jpotkanski4 years ago

I reviewed the original patch, which was a significant architecture change. Our patch can be use in the interim as a workable hack.

comment:3 hakre3 years ago

Duplicate: #16913

Last edited 3 years ago by hakre (previous) (diff)

comment:4 westi3 years ago

  • Cc westi added
  • Keywords needs-patch added
  • Priority changed from normal to high

Neither of the attached patches is a good idea.

Better to wrap the query generation in a function so it can be called more than once and preserve the setting of the global.

comment:5 zaneselvans3 years ago

  • Cc zane@… added

I think I ran into this same issue recently when attempting to create new blogs in a network installation. I don't know why $wp_queries wasn't getting updated to reflect the new blog_id and table prefixes... the values inside $wpdb were correct. I ended up temporarily creating a wrapper function around a copy of the table creation statements from schema.php so I could get the new blogs set up, but it wasn't something I wanted to leave that way. Notes from the experience are visible in the multi-site support forum: https://wordpress.org/support/topic/db-tables-for-new-subdomain-site-fail-to-get-created?replies=29

ryan3 years ago

comment:6 ryan3 years ago

  • Milestone changed from Future Release to 3.3

ryan3 years ago

comment:7 ryan3 years ago

Renamed the function to wp_get_db_schema() per IRC discussion. Added a scope arg which is very similar to the one for wpdb::tables(). Grouped table SQL by scope. Changed install_blog() to pass a scope of 'blog' so that it no longer uselessly and dangerously sends user and usermeta tables through dbDelta() for every blog created.

comment:8 wpmuguru3 years ago

I didn't test, but looks good.

comment:9 wpmuguru3 years ago

  • Keywords has-patch added; needs-patch removed

ryan3 years ago

comment:10 ryan3 years ago

Renames WP_NETWORK_ADMIN_PAGE to WP_INSTALLING_NETWORK and uses that to reliably determine if multisite in wp_get_db_schema(). This allows more exactly copying the wpdb:tables() API. Switched to a switch as seen in tables() for readability.

comment:11 nacin3 years ago

A quick grep of the plugins directory reveals 1588 references to $charset_collate. While many of these are surely going to be plugin-defined local variables, I found it specifically globalized in 3 different plugins. I imagine a number may also be using it in procedural, globally scoped code.

comment:12 nacin3 years ago

dbDelta()'s repetitive statements could probably be simplified to:

if ( in_array( $queries, array( '', 'all', 'blog', 'global', 'ms' ), true ) )
    $queries = wp_get_db_schema( $queries );

comment:13 ryan3 years ago

In [18899]:

Introduce wp_get_db_schema() for rerieving the various flavors of the WP db schema. Eliminates need to use global. Allows multiple calls to wpmu_create_blog(). see #12028

comment:14 ryan3 years ago

The commit incorporates nacin's last two comments.

comment:15 ryan3 years ago

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

comment:16 dd323 years ago

In [18986]:

Fix Undefined Variable Notices when no charset/collate is set for $wpdb. See #12028

Note: See TracTickets for help on using tickets.