kylephillips / favorites

Simple and flexible favorite buttons for any WordPress post type.
https://favoriteposts.com
223 stars 86 forks source link

Favorited posts wrongly filtered out on multisite #132

Open waclawjacek opened 4 years ago

waclawjacek commented 4 years ago

Hello!

While working on a client's website running Favorites version 2.3.1, I noticed that there is a bug in displaying favorites in a multisite environment.

Namely, when the [user_favorites] shortcode is used with a site_id attribute to get favorites from a different site and the code execution enters UserFavorites::removeInvalidFavorites() (the $favorites array passed to this function seems to be OK) and later, UserFavorites::postExists(), current site context is not changed to the site ID passed in the shortcode and instead is that of the site where the shortcode was placed.

This results in UserFavorites::postExists() calling the get_post_status() function to check the post status of a valid post ID on an invalid site, therefore rejecting a valid post ID as non-existent (it does exist but we are checking on an incorrect site ID).

Please see below for an example of the issue.

This issue can be fixed by switching the blog context with switch_to_blog() for the duration of the get_post_status() (and other) checks, possibly in UserFavorites::getFavoritesArray(), but it might also be required for other reasons in UserFavorites::setPostTypes() and maybe in other methods.


This example illustrates the bug:

Let's assume there are two sites: site A and site B.

Site A has post IDs #1 and #2. Site B has post IDs #1 and #3.

Let's say a user has favorited all posts from site A (#1 and #2).

On site B, we place a shortcode displaying favorites from site A.

The plugin then does the following:

  1. Get the user's favorites from site A.
    • At this stage everything is OK: the plugin receives IDs #1 and #2.
  2. Check if the favorites are published posts:
    1. Is #1 a published post?
      • The plugin answers: Yes it is. This is true, but only by sheer luck - post ID #1 exists on both site A and B so when the plugin checks "do I have a post ID #1 on site B?" the answer is "yes."
    2. Is #2 a published post?
      • The plugin answers: No, it is not. This is invalid because the plugin is checking if there is a post #2 on site B instead of site A. If it checked on site A, the answer would be "yes" and the correct favorites would display.

Do you maybe have an expected date where this should be fixed that I could communicate to my client?

Thank you and all the best!