Make WordPress Core

Opened 8 years ago

Last modified 3 months ago

#44461 new defect (bug)

inconsistent type for global $blog_id

Reported by: anonymized_10765487 Owned by:
Priority: normal Milestone:
Component: Networks and Sites Version: 4.9.6
Severity: normal Keywords: 2nd-opinion close
Cc: Focuses:

Description

Hi,

in a single installation global $blog_id is 1, its type is integer.
in a multisite context global $blog_id is a string, this might be a problem considering the fact that operator == && != have been deprecated.

There is a function called get_current_blog_id() that basically applies an absint() on this global but I guess this should be an integer whatever happens.

Change History (5)

#1 @subrataemfluence
8 years ago

  • Focuses multisite added
  • Keywords reporter-feedback added

I tested by echo-ing blog_id in both single and multisite. Both cases it returned me integer.

On single site get_current_blog_id() returned 1, and the same is 5 when I did it on a multisite environment.

echo get_current_blog_id();

If this is not correct please let me know the exact steps for reproducing the issue.

Thank you!

#2 @anonymized_10765487
8 years ago

That's normal, as I said get_current_blog_id() is basically applying an absint() on the global but the global itself is inconsistent.

#3 @desrosj
8 years ago

  • Component General → Networks and Sites
  • Focuses multisite removed
  • Keywords needs-testing added; reporter-feedback removed

#4 @realloc
3 months ago

  • Keywords 2nd-opinion close added; needs-testing removed

The observation still holds in trunk.

Single site: wp_initial_constants() sets $blog_id = 1, an integer (src/wp-includes/default-constants.php L74-76).

Multisite: the bootstrap overwrites it with the numeric string property WP_Site::$blog_id (src/wp-includes/ms-settings.php L89, and src/wp-includes/class-wp-site.php L30-41, where the string type is documented as intentional).

I would suggest closing this ticket.

These are all occurrences of the global in src:

wp-includes/default-constants.php:19   write
wp-includes/ms-blogs.php:536           write
wp-includes/ms-blogs.php:584           write
wp-includes/load.php:1482              read
wp-settings.php:63                     declaration

Core reads the global in exactly one place, get_current_blog_id(), which applies absint() (src/wp-includes/load.php L1481-1485). No core function consumes the raw global, so no core behaviour depends on its type. Both places that annotate it already say @global int (src/wp-settings.php L61, src/wp-includes/load.php L1477), and the accessor has existed since 3.1.

The type is inconsistent, but it backs an accessor rather than forming an interface of its own. Casting it would change a value on every request with no demonstrated caller that needs the change.

#5 @realloc
3 months ago

  • Milestone Awaiting Review
Note: See TracTickets for help on using tickets.