Skip to content

Commit

Permalink
fix: remove filter and add back whenever needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thulin committed Oct 22, 2024
1 parent 89f0a4a commit 8c188e8
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,23 @@
use WpService\Contracts\IsMultisite;
use WpService\Contracts\RestoreCurrentBlog;
use WpService\Contracts\SwitchToBlog;
use WpService\Contracts\RemoveFilter;

/**
* Allow posts from other sites to keep their permalinks.
*/
class AllowPostsFromOtherSitesToKeepTheirPermalinks implements Hookable
{
const FILTER_PRIORITY = 10;
const FILTER_NAME = 'post_link';
const FILTER_ARGUMENTS = 2;
const FILTER_FUNCTION = 'getPermalinkFromOtherSite';

/**
* Constructor.
*/
public function __construct(
private AddFilter&IsMultisite&GetCurrentBlogId&GetBlogIdFromUrl&SwitchToBlog&GetPermalink&RestoreCurrentBlog $wpService
private AddFilter&IsMultisite&GetCurrentBlogId&GetBlogIdFromUrl&SwitchToBlog&GetPermalink&RestoreCurrentBlog&RemoveFilter $wpService
) {
}

Expand All @@ -30,7 +36,7 @@ public function __construct(
*/
public function addHooks(): void
{
$this->wpService->addFilter('post_link', [$this, 'getPermalinkFromOtherSite'], 10, 2);
$this->wpService->addFilter(self::FILTER_NAME, [$this, self::FILTER_FUNCTION], self::FILTER_PRIORITY, self::FILTER_ARGUMENTS);
}

/**
Expand All @@ -43,6 +49,8 @@ public function addHooks(): void
*/
public function getPermalinkFromOtherSite(string $permalink, WP_Post $post): string
{
$this->wpService->removeFilter(self::FILTER_NAME, [$this, self::FILTER_FUNCTION], self::FILTER_PRIORITY);

if (!$this->wpService->isMultisite()) {
return $permalink;
}
Expand All @@ -54,7 +62,11 @@ public function getPermalinkFromOtherSite(string $permalink, WP_Post $post): str
return $permalink;
}

return $this->getOtherSitesPostLink($otherSitesId, $post);
$filteredPermalink = $this->getOtherSitesPostLink($otherSitesId, $post);

$this->wpService->addFilter(self::FILTER_NAME, [$this, self::FILTER_FUNCTION], self::FILTER_PRIORITY, self::FILTER_ARGUMENTS);

return $filteredPermalink;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testGetPermalinkFromOtherSiteAttachesFilterToPostLink()
*/
public function testGetPermalinkFromOtherSiteReturnsPermalinkIfSiteIsNotMultisite()
{
$wpService = new FakeWpService(['isMultisite' => false]);
$wpService = new FakeWpService(['isMultisite' => false, 'removeFilter' => true]);
$filter = new AllowPostsFromOtherSitesToKeepTheirPermalinks($wpService);

$this->assertEquals('permalink', $filter->getPermalinkFromOtherSite('permalink', WpMockFactory::createWpPost()));
Expand All @@ -41,6 +41,8 @@ public function testGetPermalinkFromOtherSiteReturnsPermalinkIfSiteIsSameAsCurre
'isMultisite' => true,
'getCurrentBlogId' => 1,
'getBlogIdFromUrl' => 1,
'removeFilter' => true,
'addFilter' => true,
]);
$filter = new AllowPostsFromOtherSitesToKeepTheirPermalinks($wpService);

Expand All @@ -61,6 +63,8 @@ public function testGetPermalinkFromOtherSiteReturnsPermalinkFromOtherSite()
'getPermalink' => $url,
'switchToBlog' => true,
'restoreCurrentBlog' => true,
'removeFilter' => true,
'addFilter' => true,
]);
$filter = new AllowPostsFromOtherSitesToKeepTheirPermalinks($wpService);

Expand All @@ -81,6 +85,8 @@ public function testGetPermalinkFromOtherSiteAllowsSwitchingToDifferentTypesOfUr
'getPermalink' => $url,
'switchToBlog' => true,
'restoreCurrentBlog' => true,
'removeFilter' => true,
'addFilter' => true,
]);
$filter = new AllowPostsFromOtherSitesToKeepTheirPermalinks($wpService);

Expand Down
Loading

0 comments on commit 8c188e8

Please sign in to comment.