-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow PostObject getIcon() to get icons from other blog if orig…
…in of post is other blog (#1243)
- Loading branch information
Showing
14 changed files
with
392 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace Municipio\PostObject\Decorators; | ||
|
||
use Municipio\PostObject\Icon\IconInterface; | ||
use Municipio\PostObject\PostObjectInterface; | ||
use WpService\Contracts\GetCurrentBlogId; | ||
use WpService\Contracts\IsMultisite; | ||
use WpService\Contracts\RestoreCurrentBlog; | ||
use WpService\Contracts\SwitchToBlog; | ||
|
||
/** | ||
* Post object decorator that can fetch post data from another blog. | ||
* If the post is from another blog, it will switch to that blog to fetch the data. | ||
*/ | ||
class PostObjectFromOtherBlog implements PostObjectInterface | ||
{ | ||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct( | ||
private PostObjectInterface $postObject, | ||
private IsMultisite&GetCurrentBlogId&SwitchToBlog&RestoreCurrentBlog $wpService | ||
) { | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getId(): int | ||
{ | ||
return $this->postObject->getId(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getTitle(): string | ||
{ | ||
return $this->postObject->getTitle(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getPermalink(): string | ||
{ | ||
return $this->postObject->getPermalink(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getCommentCount(): int | ||
{ | ||
return $this->postObject->getCommentCount(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getPostType(): string | ||
{ | ||
return $this->postObject->getPostType(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getIcon(): ?IconInterface | ||
{ | ||
if (!$this->wpService->isMultisite()) { | ||
return $this->postObject->getIcon(); | ||
} | ||
|
||
if ($this->getBlogId() === $this->wpService->getCurrentBlogId()) { | ||
return $this->postObject->getIcon(); | ||
} | ||
|
||
$this->wpService->switchToBlog($this->getBlogId()); | ||
$icon = $this->postObject->getIcon(); | ||
$this->wpService->restoreCurrentBlog(); | ||
return $icon; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getBlogId(): int | ||
{ | ||
return $this->blogId ?? $this->postObject->getBlogId(); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
library/PostObject/Decorators/PostObjectFromOtherBlog.test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Municipio\PostObject\Decorators; | ||
|
||
use Municipio\PostObject\PostObjectInterface; | ||
use PHPUnit\Framework\TestCase; | ||
use WpService\Implementations\FakeWpService; | ||
|
||
class PostObjectFromOtherBlogTest extends TestCase | ||
{ | ||
/** | ||
* @testdox class can be instantiated | ||
*/ | ||
public function testCanBeInstantiated() | ||
{ | ||
$wpService = new FakeWpService(); | ||
$postObject = $this->createStub(PostObjectInterface::class); | ||
|
||
$this->assertInstanceOf( | ||
PostObjectFromOtherBlog::class, | ||
new PostObjectFromOtherBlog($postObject, $wpService) | ||
); | ||
} | ||
|
||
/** | ||
* @testdox getIcon() performs a switch to the correct blog if the post is from another blog | ||
*/ | ||
public function testGetIconSwitchesToCorrectBlog() | ||
{ | ||
$wpService = new FakeWpService(['isMultisite' => true, 'getCurrentBlogId' => 1, 'switchToBlog' => true, 'restoreCurrentBlog' => true]); | ||
$postObject = $this->createStub(PostObjectInterface::class); | ||
$postObject->method('getBlogId')->willReturn(2); | ||
$decoratedPostObject = new PostObjectFromOtherBlog($postObject, $wpService); | ||
|
||
$decoratedPostObject->getIcon(); | ||
|
||
$this->assertEquals(2, $wpService->methodCalls['switchToBlog'][0][0]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.