WordPress.org

Make WordPress Core

Opened 7 months ago

Last modified 5 months ago

#40513 new enhancement

switch_to_blog should accept a WP_site as param

Reported by: spacedmonkey Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.0
Component: Networks and Sites Keywords: has-patch
Focuses: multisite Cc:

Description

The switch_to_blog function currently only accepts a blog id. By passing a WP_Site object where available, it means switch has more context. The WP_site object has all the site information, including the network id.

Attachments (3)

40513.diff (2.2 KB) - added by spacedmonkey 7 months ago.
40513.2.diff (6.2 KB) - added by spacedmonkey 7 months ago.
40513.3.diff (6.5 KB) - added by spacedmonkey 7 months ago.

Download all attachments as: .zip

Change History (10)

@spacedmonkey
7 months ago

#1 @MaximeCulea
7 months ago

  • Keywords has-patch 2nd-opinion added

Hi, this seems nice.

#2 @spacedmonkey
7 months ago

40513.2.diff has some examples of where we can use this change in core. #40511 will help.

#3 @flixos90
7 months ago

+1

We should have a close look at the parameter names and documentation in order to clarify that $site_object may not actually contain a site object. I have to admit that appears a bit confusing.

Maybe this is a good time to introduce a new switch_site action and only run it if an object is passed. If a plugin developer then wants to do something with the site object, they'd need to use the switch_site action. If we ever introduce a function switch_to_site() or so, we could then have it require an object - this is of course future stuff, but such an action would bring something like forward compatibility. :)

#4 @spacedmonkey
7 months ago

I agree. Naming of variables and docs do need work here. @DrewAPicture is the best man to look at this.

I don't agree about a new action. The switch_blog action is fired in switch_to_blog and in the restore. In 40513.3.diff I now pass $current_blog in the restore function. If doing this is going to be useful, we have to pass it to the switch_blog action.

This ticket was mentioned in Slack in #core-multisite by spacedmonkey. View the logs.


7 months ago

This ticket was mentioned in Slack in #core-multisite by spacedmonkey. View the logs.


7 months ago

#7 @DrewAPicture
5 months ago

  • Keywords 2nd-opinion removed

Hi, sorry missed the ping. Yeah, I'd probably just simplify it down to $site or $new_site. Separately, I think I'd probably recommend doing the get_site() call inside of switch_to_blog() as it can already accept an object or int – and it would follow with core style on similar usage, such as with how get_post() is used inside of functions all the time.

Note: See TracTickets for help on using tickets.