[wp-trac] [WordPress Trac] #25293: Switch_to_blog not switching the siteid

WordPress Trac noreply at wordpress.org
Wed Aug 17 11:12:15 UTC 2016


#25293: Switch_to_blog not switching the siteid
--------------------------------+-----------------------------
 Reporter:  Rahe                |       Owner:
     Type:  defect (bug)        |      Status:  new
 Priority:  normal              |   Milestone:  Future Release
Component:  Networks and Sites  |     Version:  3.0
 Severity:  minor               |  Resolution:
 Keywords:                      |     Focuses:  multisite
--------------------------------+-----------------------------

Comment (by flixos90):

 @Rahe:
 The ticket is still relevant.
 Actually I'm just seeing this ticket right now. It would be great to get
 this implemented somehow as it would also give a ticket like #37414 more
 value.

 @jeremyfelt:
 For `switch_to_network()` (and the related restore function), we could,
 for the most part, take the function from the WP Multi Network plugin.
 What I like about that implementation is that the entire network object is
 stored in the switched stack global.

 For the site switching, I really like your idea of introducing a
 `switch_to_site()` for it. Maybe the second parameter could be a bool
 `$switch_network` instead of the network ID so that the function detects
 the network to switch to automatically. I don't think there's a use-case
 for switching the site and network simultaneously in a way that they don't
 belong together. `switch_to_network()` could have a similar second
 parameter `$switch_site`.

 To prevent database queries on every switch, maybe both functions should
 accept _either_ an ID or an object. The site object has the network ID
 already in place (so does the network object have the site ID), so no
 additional query necessary. If the user passes an ID, we call `get_site(
 $id )` (or `get_network( $id )` respectively) which is not too unlikely to
 get a cache hit under these circumstances. In the related
 `restore_current_*()` functions, we could prevent queries in a similar
 way, by storing the entire objects in the switched stack globals. Maybe we
 should make what we store there dependent on whether the user provided
 `$switch_network = true`. If switching the network too, we store the
 object, if only the site, we store the ID. Then in the
 `restore_current_*()` function we can detect whether that last switch only
 switched the site or also the network. This way we wouldn't have DB
 queries there either, and we wouldn't even need a conditional parameter
 whether to switch the network as well since that would be automatically
 detected.

 If we hold on to `switch_to_blog()` to use it internally (in
 `switch_to_site()`), we would need to adjust that to also accept an object
 and store that in stack. Or we create `switch_to_site()` from scratch with
 a global `_wp_switched_sites_stack` to not mess with `switch_to_blog()`,
 considering it deprecated at some point.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/25293#comment:8>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list