Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure post abstraction methods use correct site ID. #1010

Merged
merged 14 commits into from
Feb 13, 2023

Conversation

peterwilsoncc
Copy link
Collaborator

@peterwilsoncc peterwilsoncc commented Jan 30, 2023

Description of the Change

This resolves an issue in which the post abstraction can return a mix of data on Multisite networks when the site is switched.

It introduces the DistributorPost::__call() magic method to ensure the current site (get_current_blog_id()) matches the site ID used to instantiate the object.

To match the PHP behavior without the magic method, a fatal error is thrown if the method name does not exist.

To discourage developers from calling the helper methods while using a switched site, a notice is thrown if the magic method needs to switch sites.

To avoid site switching when possible, an in memory cache is used to bypass the site switch the second or subsequent time a method is called. The cached data is only used if the object is called from the wrong site to reduce the risk of errors caused by the addition/removal of hooks during runtime. Basically it's a performance compromise.

Closes #1007.
Supersedes #1004.

TODO

  • Test interaction of magic methods: ensure $dt_post->source_site doesn't result in multiple switches

How to test the Change

Changelog Entry

Fixed - Inconsistent data returned from post abstraction after site switch on multisite.

Credits

Props @peterwilsoncc, @dkotter

Checklist:

  • I agree to follow this project's Code of Conduct.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests pass.

@peterwilsoncc peterwilsoncc self-assigned this Jan 30, 2023
@peterwilsoncc peterwilsoncc added this to the 2.0.0 milestone Jan 30, 2023
return $result;
}

/**
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

::__get() and ::__isset() methods are unchanged but have been relocated to group all the magic methods together.

@jeffpaul jeffpaul added the v2 label Jan 31, 2023
@peterwilsoncc
Copy link
Collaborator Author

In the revised PR #1010 (in draft), I went with a __call magic method to avoid statics. I'll see if I can use a cache to avoid switching sites when the method is used a second time.

@peterwilsoncc peterwilsoncc marked this pull request as ready for review February 2, 2023 03:22
@peterwilsoncc peterwilsoncc requested a review from a team as a code owner February 2, 2023 03:22
@peterwilsoncc peterwilsoncc requested review from Sidsector9, ravinderk and dkotter and removed request for a team and Sidsector9 February 2, 2023 03:22
@peterwilsoncc
Copy link
Collaborator Author

@peterwilsoncc peterwilsoncc merged commit 13fb240 into develop Feb 13, 2023
@peterwilsoncc peterwilsoncc deleted the fix/1007-ms-switched-sites branch February 13, 2023 02:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Consider multisite for post abstraction.
3 participants