Make WordPress Core

Opened 8 years ago

Last modified 4 weeks ago

#45761 new defect (bug)

consistency between $wpdb->blogid and get_current_blog_id()

Reported by: arena Owned by:
Priority: normal Milestone:
Component: Networks and Sites Version: 3.0
Severity: normal Keywords: dev-feedback has-patch close
Cc: Focuses: multisite

Description

On a single install (not multisite),

you have

$wpdb->blogid => 0

get_current_blog_id() => 1

Must be a very old one !!!

Change History (5)

#1 @johnjamesjacoby
8 years ago

  • Component GeneralNetworks and Sites
  • Focuses multisite added
  • Keywords needs-patch added
  • Version trunk3.0

#3 @keraweb
7 years ago

  • Keywords dev-feedback added

A check in the wpdb::__construct() for is_multisite() could fix this. If it's not a multisite then set blogid to 1.

This ticket was mentioned in PR #7316 on WordPress/wordpress-develop by @debarghyabanerjee.


2 years ago
#4

  • Keywords has-patch added; needs-patch removed

Trac Ticket: Core-45761

## Problem Statement:

  • In single-site WordPress installations, there is an inconsistency between the blogid property of the global $wpdb object and the value returned by get_current_blog_id(). Specifically:
$wpdb->blogid returns 0;
get_current_blog_id() returns 1;

This inconsistency is present in older installations and can lead to discrepancies in code that relies on the blogid value, especially in non-multisite environments.

## Fix:

  • To resolve this issue, we have modified the wpdb::__construct constructor method. The fix includes:
  • Adding an is_multisite() check within the constructor.

Setting $wpdb->blogid to 1 if is_multisite() returns false, ensuring consistency with the value returned by get_current_blog_id().

#5 @realloc
4 weeks ago

  • Keywords close added
  • Milestone Awaiting Review

The patch is correct, but on a single site the value it sets is never used.

wpdb::get_blog_prefix() returns the base prefix immediately when is_multisite() is false, without reading $blogid, and wpdb::tables() only forwards the value into it. The one path that does change is the comparison in wp_get_db_schema(), where the switch is skipped rather than performed, with the same outcome (the prefix is unchanged, and the restore is already guarded by isset( $old_blog_id )).

So the patch changes nothing in Core. It only changes a public property that has defaulted to 0 since 3.0, and code outside Core may read empty( $wpdb->blogid ) as "not multisite" or as "site context not set up yet". That is a real risk for no gain.

It also points away from where these properties are heading: $wpdb->siteid is written once in set_blog_id() and read nowhere else in Core or in the test suite (see #41507), #25293 raised deprecating it, and $wpdb->blogid is barely better off.

Suggestion: close as wontfix and open a follow-up to deprecate wpdb::$blogid and wpdb::$siteid, with get_current_blog_id() and get_current_network_id() documented as the replacements.

Code references:

  • src/wp-includes/class-wpdb.php:272, 1017, 1043-1062, 1073-1089, 1152-1156
  • src/wp-admin/includes/schema.php:41, 340
Note: See TracTickets for help on using tickets.