Make WordPress Core

Opened 22 months ago

Last modified 5 months ago

#60366 new enhancement

add call_for_blog for easy to switch blog and restore in multisite

Reported by: denishua's profile denishua Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: General Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

add call_for_blog for easy to switch blog and restore in multisite

function call_for_blog($blog_id, $callback, ...$args){
	try{
		$switched	= (is_multisite() && $blog_id && $blog_id != get_current_blog_id()) ? switch_to_blog($blog_id) : false;

		return call_user_func_array($callback, $args);
	}finally{
		if($switched){
			restore_current_blog();
		}
	}
}

usage:

call_for_blog(123, 'wp_update_post', ['ID'=>$id, 'post_title'=>$title]);

closure callback:

for_blog($shop_blog_id, function($id, $title){
	$result	= some_function();

	if(is_wp_error($result)){
		return $result;
	}

	$result	= some_other_function();

	if(is_wp_error($result)){
		return $result;
	}

	return wp_update_post(['ID'=>$id, 'post_title'=>$title]);	
}, $id, $title);

Change History (3)

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


9 months ago
#1

  • Keywords has-patch has-unit-tests added

#2 follow-up: @karmatosed
5 months ago

In 60367:

Twenty Twenty-One: Revert 63549 which brings in margin fix.

The tests were failing on the theme as adresult of bringing in this once committed. For now, reverting until explore.

See #60366.

#3 in reply to: ↑ 2 @SirLouen
5 months ago

Replying to karmatosed:

See #60366.

Information for anyone who could be taking this ticket in the future:
This is a little typo, this revision [60367], is referring to ticket #60196.

Last edited 5 months ago by SirLouen (previous) (diff)
Note: See TracTickets for help on using tickets.