From 17c90eaadfa13709267df9f2209a3f98c486757d Mon Sep 17 00:00:00 2001 From: Webnist Date: Thu, 6 May 2021 14:01:38 +0900 Subject: [PATCH] Bug in multi-site user_favorites not being displayed. Bug in the favorite button of child blogs displayed in the main site list. --- app/Entities/Favorite/FavoriteFilter.php | 18 +++++++++++++++--- app/Entities/User/UserFavorites.php | 16 +++++++++------- app/Listeners/FavoriteButton.php | 2 ++ 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/app/Entities/Favorite/FavoriteFilter.php b/app/Entities/Favorite/FavoriteFilter.php index 501f70b..c580352 100644 --- a/app/Entities/Favorite/FavoriteFilter.php +++ b/app/Entities/Favorite/FavoriteFilter.php @@ -33,10 +33,18 @@ class FavoriteFilter */ private $filters; - public function __construct($favorites, $filters) + /** + * Site ID + * + * @var int + */ + private $site_id; + + public function __construct($favorites, $filters, $site_id) { $this->favorites = $favorites; $this->filters = $filters; + $this->site_id = $site_id; } public function filter() @@ -54,10 +62,12 @@ public function filter() */ private function filterByPostType() { + if ( is_multisite() ) switch_to_blog($this->site_id); foreach($this->favorites as $key => $favorite){ $post_type = get_post_type($favorite); if ( !in_array($post_type, $this->filters['post_type']) ) unset($this->favorites[$key]); } + if ( is_multisite() ) restore_current_blog(); } /** @@ -66,10 +76,12 @@ private function filterByPostType() */ private function filterByStatus() { + if ( is_multisite() ) switch_to_blog($this->site_id); foreach($this->favorites as $key => $favorite){ $status = get_post_status($favorite); if ( !in_array($status, $this->filters['status']) ) unset($this->favorites[$key]); } + if ( is_multisite() ) restore_current_blog(); } /** @@ -81,7 +93,7 @@ private function filterByTerm() { $taxonomies = $this->filters['terms']; $favorites = $this->favorites; - + if ( is_multisite() ) switch_to_blog($this->site_id); foreach ( $favorites as $key => $favorite ) : $all_terms = []; @@ -98,7 +110,7 @@ private function filterByTerm() if ( !empty($dif) ) unset($favorites[$key]); endforeach; - + if ( is_multisite() ) restore_current_blog(); $this->favorites = $favorites; } } \ No newline at end of file diff --git a/app/Entities/User/UserFavorites.php b/app/Entities/User/UserFavorites.php index 75d0504..5ace4bb 100644 --- a/app/Entities/User/UserFavorites.php +++ b/app/Entities/User/UserFavorites.php @@ -54,18 +54,18 @@ public function getFavoritesArray($user_id = null, $site_id = null, $filters = n $site_id = ( isset($site_id) ) ? $site_id : $this->site_id; $favorites = $this->user_repo->getFavorites($user_id, $site_id); if ( isset($filters) ) $this->filters = $filters; - if ( isset($this->filters) && is_array($this->filters) ) $favorites = $this->filterFavorites($favorites); - return $this->removeInvalidFavorites($favorites); + if ( isset($this->filters) && is_array($this->filters) ) $favorites = $this->filterFavorites($favorites, $site_id); + return $this->removeInvalidFavorites($favorites, $site_id); } /** * Remove non-existent or non-published favorites * @param array $favorites */ - private function removeInvalidFavorites($favorites) + private function removeInvalidFavorites($favorites, $site_id) { foreach($favorites as $key => $favorite){ - if ( !$this->postExists($favorite) ) unset($favorites[$key]); + if ( !$this->postExists($favorite, $site_id) ) unset($favorites[$key]); } return $favorites; } @@ -75,9 +75,9 @@ private function removeInvalidFavorites($favorites) * @since 1.1.1 * @param array $favorites */ - private function filterFavorites($favorites) + private function filterFavorites($favorites, $site_id) { - $favorites = new FavoriteFilter($favorites, $this->filters); + $favorites = new FavoriteFilter($favorites, $this->filters, $site_id); return $favorites->filter(); } @@ -108,10 +108,12 @@ public function getFavoritesList($include_button = false, $include_thumbnails = /** * Check if post exists and is published */ - private function postExists($id) + private function postExists($id, $site_id) { + if ( is_multisite() ) switch_to_blog($site_id); $allowed_statuses = ( isset($this->filters['status']) && is_array($this->filters['status']) ) ? $this->filters['status'] : array('publish'); $status = get_post_status($id); + if ( is_multisite() ) restore_current_blog(); if ( !$status ) return false; if ( !in_array($status, $allowed_statuses) ) return false; return true; diff --git a/app/Listeners/FavoriteButton.php b/app/Listeners/FavoriteButton.php index 2c50b5f..0ef2b10 100644 --- a/app/Listeners/FavoriteButton.php +++ b/app/Listeners/FavoriteButton.php @@ -75,9 +75,11 @@ private function afterUpdateAction() */ private function validates() { + if ( is_multisite() ) switch_to_blog($this->data['siteid']); $post_type = get_post_type($this->data['postid']); $enabled = $this->settings_repo->displayInPostType($post_type); $post_type_object = get_post_type_object(get_post_type($this->data['postid'])); + if ( is_multisite() ) restore_current_blog(); if ( !$post_type_object ) return false; if ( !$post_type_object->public || !$enabled ) return false; return true;