Skip to content

Commit

Permalink
fix: remove filter and add back whenever needed. (#1088)
Browse files Browse the repository at this point in the history
prevent infinite loop by removing filter while running
  • Loading branch information
sebastianthulin authored Oct 22, 2024
1 parent aebfad4 commit 82a9634
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 160 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
{
public const FILTER_PRIORITY = 10;
public const FILTER_NAME = 'post_link';
public const FILTER_ARGUMENTS = 2;
public 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 @@ -5,6 +5,7 @@
use Municipio\TestUtils\WpMockFactory;
use PHPUnit\Framework\TestCase;
use WpService\Implementations\FakeWpService;
use Municipio\PostFilters\AllowPostsFromOtherSitesToKeepTheirPermalinks as SUT;

class AllowPostsFromOtherSitesToKeepTheirPermalinksTest extends TestCase
{
Expand All @@ -14,20 +15,20 @@ class AllowPostsFromOtherSitesToKeepTheirPermalinksTest extends TestCase
public function testGetPermalinkFromOtherSiteAttachesFilterToPostLink()
{
$wpService = new FakeWpService(['addFilter' => true]);
$filter = new AllowPostsFromOtherSitesToKeepTheirPermalinks($wpService);
$filter = new SUT($wpService);

$filter->addHooks();

$this->assertEquals('post_link', $wpService->methodCalls['addFilter'][0][0]);
$this->assertEquals(SUT::FILTER_NAME, $wpService->methodCalls['addFilter'][0][0]);
}

/**
* @testdox getPermalinkFromOtherSite() returns the permalink if the site is not a multisite.
*/
public function testGetPermalinkFromOtherSiteReturnsPermalinkIfSiteIsNotMultisite()
{
$wpService = new FakeWpService(['isMultisite' => false]);
$filter = new AllowPostsFromOtherSitesToKeepTheirPermalinks($wpService);
$wpService = new FakeWpService(['isMultisite' => false, 'removeFilter' => true]);
$filter = new SUT($wpService);

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

$post = WpMockFactory::createWpPost(['guid' => 'http://example.com/path']);
$this->assertEquals('permalink', $filter->getPermalinkFromOtherSite('permalink', $post));
Expand All @@ -61,8 +64,10 @@ public function testGetPermalinkFromOtherSiteReturnsPermalinkFromOtherSite()
'getPermalink' => $url,
'switchToBlog' => true,
'restoreCurrentBlog' => true,
'removeFilter' => true,
'addFilter' => true,
]);
$filter = new AllowPostsFromOtherSitesToKeepTheirPermalinks($wpService);
$filter = new SUT($wpService);

$post = WpMockFactory::createWpPost(['ID' => 1, 'guid' => $url]);
$this->assertEquals($url, $filter->getPermalinkFromOtherSite('permalink', $post));
Expand All @@ -81,8 +86,10 @@ public function testGetPermalinkFromOtherSiteAllowsSwitchingToDifferentTypesOfUr
'getPermalink' => $url,
'switchToBlog' => true,
'restoreCurrentBlog' => true,
'removeFilter' => true,
'addFilter' => true,
]);
$filter = new AllowPostsFromOtherSitesToKeepTheirPermalinks($wpService);
$filter = new SUT($wpService);

$post = WpMockFactory::createWpPost(['ID' => 1, 'guid' => $url]);
$filter->getPermalinkFromOtherSite('permalink', $post);
Expand Down
Loading

0 comments on commit 82a9634

Please sign in to comment.