Usage of `switch_to_site()` prevents plugins/themes active only on the receiving multisite instance from running

Open
#1,419 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
php, wordpress
Domain
api, backend

Research direction

Read includes/classes/InternalConnections/NetworkSiteConnection.php and trace the switch_to_blog() usage linked in the issue; compare that path with the external HTTP connection flow. The issue outlines two possible directions, but leaves the design decision open. Done means receiving site plugins run reliably when content is pulled, with tests covering the selected approach.

Written by the indexing model from the issue text.

Description

Describe the bug

This plugin uses WordPress' switch_to_blog() extensively: https://github.com/search?q=repo%3A10up%2Fdistributor+switch_to_blog&type=code

The problem is documented in switch_to_blog()'s own docs:

PHP code loaded with the originally requested site, such as code from a plugin or theme, does not switch. See #14941.

This means that plugins active only on the receiving site, such as 10up's Distributor Edit Remote Post Meta, don't function reliably in multisite networks.

There are a couple ways forward:

  1. Deprecate switch_to_blog() functionality and the NetworkSiteConnection class: all pushes will be treated as external connections and mediated via HTTP, which ensures that the receiving site’s plugins will be loaded by the receiving site.
  2. Update Distributor documentation to require code parity between the sending site and the receiving site

I don’t recommend option #2 because it’s a logistical problem for Distributor networks that don’t share codebases. The only places where Distributor would be guaranteed to function 100% reliably would be a multisite network, or sites with 1 shared repo for multiple sites. (example: @benlk's longest project)

Steps to Reproduce

This isn't something that's currently reproducible in Distributor, but it's a bug which will arise with the implementation of https://github.com/10up/distributor/pull/1417 to enable fixes for the bug that https://github.com/10up/distributor-edit-remote-post-meta/pull/19 tries to fix.

https://github.com/10up/distributor-edit-remote-post-meta/pull/19 points out that, with that PR's changes, that plugin must be active on both the sending and receiving site in order for that plugin to function as intended.

The reproduction case would look something like this:

  1. Create a multisite network and enable Distributor on the network
  2. On one receiving site, activate a plugin which modifies the received Distributor payload, such as by replacing all images in the post with an image of a kitten, or by ignoring Distributor's pushed updates to terms and post meta
  3. Publish a post on the origin site
  4. Pull a post to the receiving site
  5. Make an update to the post on the origin site:
  6. Expect that the receiving site's plugin functions as intended -> It doesn't. The images aren't replaced with kittens; the post meta and post terms are overwritten.
Screenshots, screen recording, code snippet

No response

Environment information

n/a

WordPress information

All.

Code of Conduct
  • I agree to follow this project's Code of Conduct
Dominant language
PHP
Stars
715
Forks
160
Avg merge
6h 10m
Merged PRs (30d)
6

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from 10up/distributor

All issues in 10up/distributor

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.