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

fix: remove filter and add back whenever needed. #1088

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading