Opened 6 years ago
Closed 6 years ago
#49265 closed enhancement (fixed)
switch_to_blog() and restore_current_blog() reuse switch_blog action
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 5.4 | Priority: | normal |
| Severity: | normal | Version: | |
| Component: | Networks and Sites | Keywords: | has-patch commit |
| Focuses: | Cc: |
Description
Because the use the same action, it's difficult to do different things when switching to a site and restoring from a site.
For example:
function wcorg_switch_to_blog_locale() {
$backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 5 );
$caller = $backtrace[4]['function'];
switch ( $caller ) {
case 'switch_to_blog':
switch_to_locale( get_option( 'WPLANG', 'en_US' ) );
break;
case 'restore_current_blog':
restore_previous_locale();
break;
}
}
add_action( 'switch_blog', 'wcorg_switch_to_blog_locale' );
See #49263 for the context of that example. It's not a fully working solution, just a minimal example to demonstrate the problem here.
One potential solution would be to introduce a new restore_blog action inside restore_current_blog().
That still leaves a problem where it may not be obvious to people that switch_blog will still also run when restoring a blog, but I'm not sure there's a back-compat way to solve that. We could at least highlight it in the switch_blog filter documentation, though.
Attachments (1)
Change History (8)
#1
in reply to:
↑ description
@
6 years ago
#3
@
6 years ago
I like the context parameter approach, as it solves the disambiguation problem for both switching to and switching from.
#4
@
6 years ago
I also like the context parameter. 49265.diff looks good to me.
Replying to iandunn:
We could add a context as the third parameter, see 49265.diff for example.