WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 8 months ago

#14941 closed feature request (wontfix)

dynamic unloading and loading of plugins

Reported by: madtownlems Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.0
Component: Multisite Keywords:
Focuses: Cc:

Description

The short:
If I'm blog 1 and I switch_to_blog(2), I'm still running in the environment of blog 1. All blog 1's activated plugins are active, and not all of blog 2's plugins necessarily are.

The long:
I first ran into the problem trying to get Subscribe2 (S2) to work well with Site Wide Tags (SWT).

SWT basically works like this:
When someone makes a post on their blog: switch_to_blog(aggregrate_blog_id);
wp_insert_post(...);
restore_current_blog();

S2 says that whenever someone inserts a post, to send an email alert out.

With both of these plugins enabled, a post always generates TWO emails, regardless of whether S2 is active on the aggregate blog. The same is true for any behavior that triggers on a new post being created (such as automated tweeting, etc).

I feel that, if I'm really switching to that blog, I should be running more comprehensively as it. That is, with its plugins active, and none of its un-activated plugins active.

The examples of conflicts I gave are blog behavior, and are more or less just annoying, but I could easily see this causing unforeseen security flaws as well.

Change History (14)

comment:1 scribu4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

switch_to_blog() is only meant to change the data (DB tables) you're working with, not the code.

That's because you can't un-require a php file in the same pageload.

Closing as 'invalid' although the correct resolution would be 'cantfix'.

comment:2 hakre4 years ago

  • Keywords reporter-feedback added
  • Type changed from defect (bug) to feature request
  • Version set to 3.0

The reporter might ask for a new feature here. switch_to_blog() looks undifferentiated named which might have been the cause of misguidance.

If this is a feature request, the reporter might please re-open the ticket.

comment:3 madtownlems4 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

If this is a feature request, the reporter might please re-open the ticket.

Then I guess this is a feature request. Perhaps switch_to_blog() could take a flag as to whether or not it should run with the switched-to-blog's plugins instead of the original?

comment:4 nacin4 years ago

As scribu said, we can't uninclude plugins of the initial blog. Subsequent switches will then cause additional issues. If you need this kind of awareness, then have a network plugin with blog checks throughout.

Switch_to_blog serves relatively limited purpose and cannot swap everything.

comment:5 scribu4 years ago

  • Component changed from General to Multisite
  • Keywords needs-patch added; reporter-feedback removed
  • Milestone set to 3.1
  • Type changed from feature request to enhancement

We are not going to change the name or the purpose of the function.

We should document it, though, along with all the other functions in wp-includes/ms-blogs.php

comment:6 scribu4 years ago

  • Summary changed from switch_to_blog() runs with active plugins of original blog to switch_to_blog() needs documentation

comment:7 hakre4 years ago

  • Summary changed from switch_to_blog() needs documentation to switch_to_blog() needs documentation; dynamic unloading and loading of plugins

switch_to_blog() is to switch the database only. That is by intention/design.

In case you need a new feature, that must be some function like: switch_blog_context($blogid) then which does not exists right now. Do not expect something like that in the near future as Nacin pointed out wordpress core can not unload plugins as of now.

As most plugins register hooks on inclusion you might be able to unregister all those plugin hooks and then re-include the plugin files of the new blog. The plugins must be coded in a way that they do not re-define their functions and classes then.

The current hook implementation does not support identifying of plugins as of now. I might release some code that shows how to do that the sooner or later.

Worth to consider might be runkit.

comment:8 scribu4 years ago

  • Keywords needs-patch removed
  • Milestone changed from 3.1 to Future Release
  • Summary changed from switch_to_blog() needs documentation; dynamic unloading and loading of plugins to dynamic unloading and loading of plugins

I've openend a separate ticket for inline docs: #14953

comment:9 scribu4 years ago

  • Type changed from enhancement to feature request

comment:10 follow-up: johnjamesjacoby4 years ago

Sounds like the Subscribe2 plugin needs a check for the current_site and/or current_blog to prevent the double email.

comment:11 kgraeme4 years ago

I'm also getting double emails/tweets/etc from Subscribe2, Twitter Tools, or any plugin that sends a notification when a post is published. Also using donncha's sitewide tags which appears to be what's actually calling the switch_to_blog() function that trips this behavior. Sitewide tags has been one of the core essential plugins for wordpress-mu for years and it would be crippling to lose the functionality.

So add my voice to asking for a fix/feature to resolve this.

(We're even getting complaints from congressional representatives because of double email!)

comment:12 in reply to: ↑ 10 kgraeme4 years ago

Replying to johnjamesjacoby:

Sounds like the Subscribe2 plugin needs a check for the current_site and/or current_blog to prevent the double email.

It's not just Subscribe2 though. I see the same behavior with Twitter Tools and the like.

comment:13 MattyRob4 years ago

The issue here is that any plugin that acts when a post is inserted into the database on a multiuser site with Site Wide Tags is going to fire twice because SWT uses switch_to_blog() to change to a 'central' blog and adds the post there also (duplicating the post essentially but on another blog).

Because switch_to_blog() only changes the database the plugins from the 'switched' site remain active and they get fired off a second time.

Maybe we need to look at checking $switched if SWT is installed.

comment:14 jeremyfelt8 months ago

  • Milestone Future Release deleted
  • Resolution set to wontfix
  • Status changed from reopened to closed

All comments seem to agree that switch_to_blog() is doing the job it has been given. Plugins should check for changing blogs rather than activating/deactivating plugins on switch_to_blog().

Note: See TracTickets for help on using tickets.