Skip to content

Commit

Permalink
feat: allow PostObject getIcon() to get icons from other blog if orig…
Browse files Browse the repository at this point in the history
…in of post is other blog (#1243)
  • Loading branch information
thorbrink authored Jan 14, 2025
1 parent 1d77a13 commit 235539e
Show file tree
Hide file tree
Showing 14 changed files with 392 additions and 26 deletions.
10 changes: 8 additions & 2 deletions library/Helper/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
use Municipio\Helper\Term\Term;
use Municipio\PostObject\Decorators\BackwardsCompatiblePostObject;
use Municipio\PostObject\Decorators\IconResolvingPostObject;
use Municipio\PostObject\Decorators\PostObjectFromOtherBlog;
use Municipio\PostObject\Decorators\PostObjectFromWpPost;
use Municipio\PostObject\Decorators\PostObjectWithOtherBlogIdFromSwitchedState;
use Municipio\PostObject\Decorators\PostObjectWithOtherBlogIdTest;
use Municipio\PostObject\Icon\Resolvers\CachedIconResolver;
use Municipio\PostObject\Icon\Resolvers\NullIconResolver;
use Municipio\PostObject\Icon\Resolvers\PostIconResolver;
Expand Down Expand Up @@ -154,12 +157,15 @@ private static function convertWpPostToPostObject(WP_Post $post, string $cacheGr
$wpService = \Municipio\Helper\WpService::get();
$acfService = \Municipio\Helper\AcfService::get();

$postObject = new PostObjectFromWpPost(new PostObject(), $post, $wpService);
$postObject = new PostObjectFromWpPost(new PostObject($wpService), $post, $wpService);
$postObject = new PostObjectWithOtherBlogIdFromSwitchedState($postObject, $wpService);

$iconResolver = new TermIconResolver($postObject, $wpService, new Term($wpService, AcfService::get()), new NullIconResolver());
$iconResolver = new PostIconResolver($postObject, $acfService, $iconResolver);
$iconResolver = new CachedIconResolver($postObject, $iconResolver);
$postObject = new IconResolvingPostObject($postObject, $iconResolver);

$postObject = new IconResolvingPostObject($postObject, $iconResolver);
$postObject = new PostObjectFromOtherBlog($postObject, $wpService);

$postObject = new BackwardsCompatiblePostObject($postObject, $camelCasedPost);

Expand Down
63 changes: 59 additions & 4 deletions library/PostObject/Decorators/BackwardsCompatiblePostObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Municipio\PostObject\Decorators;

use AllowDynamicProperties;
use Municipio\PostObject\Icon\IconInterface;
use Municipio\PostObject\PostObjectInterface;

/**
Expand All @@ -11,19 +12,73 @@
* This class is used to make sure that the PostObjectInterface is backwards compatible with the old PostObject class.
*/
#[AllowDynamicProperties]
class BackwardsCompatiblePostObject extends AbstractPostObjectDecorator implements PostObjectInterface
class BackwardsCompatiblePostObject implements PostObjectInterface
{
/**
* Constructor.
*/
public function __construct(PostObjectInterface $postObject, object $legacyPost)
public function __construct(private PostObjectInterface $postObject, object $legacyPost)
{
$this->postObject = $postObject;

foreach ($legacyPost as $key => $value) {
if (!isset($this->{$key})) {
$this->{$key} = $value;
}
}
}

/**
* @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
{
return $this->postObject->getIcon();
}

/**
* @inheritDoc
*/
public function getBlogId(): int
{
return $this->postObject->getBlogId();
}
}
53 changes: 50 additions & 3 deletions library/PostObject/Decorators/IconResolvingPostObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,61 @@
/**
* IconResolvingPostObject
*/
class IconResolvingPostObject extends AbstractPostObjectDecorator implements PostObjectInterface
class IconResolvingPostObject implements PostObjectInterface
{
/**
* Constructor.
*/
public function __construct(PostObjectInterface $inner, private IconResolverInterface $iconResolver)
public function __construct(private PostObjectInterface $postObject, private IconResolverInterface $iconResolver)
{
$this->postObject = $inner;
}

/**
* @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 getBlogId(): int
{
return $this->postObject->getBlogId();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use Municipio\PostObject\Icon\Resolvers\IconResolverInterface;
use Municipio\PostObject\Icon\Resolvers\NullIconResolver;
use Municipio\PostObject\PostObject;
use Municipio\PostObject\PostObjectInterface;
use PHPUnit\Framework\TestCase;
use WpService\Implementations\FakeWpService;

class IconResolvingPostObjectTest extends TestCase
{
Expand All @@ -16,7 +16,7 @@ class IconResolvingPostObjectTest extends TestCase
*/
public function testClassCanBeInstantiated()
{
$decorator = new IconResolvingPostObject(new PostObject(), new NullIconResolver());
$decorator = new IconResolvingPostObject(new PostObject(new FakeWpService()), new NullIconResolver());
$this->assertInstanceOf(IconResolvingPostObject::class, $decorator);
}

Expand All @@ -31,7 +31,7 @@ public function testGetIconCallsProvidedIconResolver()
$icon->method('getIcon')->willReturn('test-icon');
$iconResolver->method('resolve')->willReturn($icon);

$postObject = new IconResolvingPostObject(new PostObject(), $iconResolver);
$postObject = new IconResolvingPostObject(new PostObject(new FakeWpService()), $iconResolver);

$this->assertEquals('test-icon', $postObject->getIcon()->getIcon());
}
Expand Down
93 changes: 93 additions & 0 deletions library/PostObject/Decorators/PostObjectFromOtherBlog.php
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 library/PostObject/Decorators/PostObjectFromOtherBlog.test.php
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]);
}
}
22 changes: 19 additions & 3 deletions library/PostObject/Decorators/PostObjectFromWpPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Municipio\PostObject\Decorators;

use Municipio\PostObject\Icon\IconInterface;
use WP_Post;
use Municipio\PostObject\PostObjectInterface;
use WpService\Contracts\GetCommentCount;
Expand All @@ -10,17 +11,16 @@
/**
* PostObject from WP_Post.
*/
class PostObjectFromWpPost extends AbstractPostObjectDecorator implements PostObjectInterface
class PostObjectFromWpPost implements PostObjectInterface
{
/**
* Constructor.
*/
public function __construct(
PostObjectInterface $inner,
private PostObjectInterface $postObject,
private WP_Post $wpPost,
private GetPermalink&GetCommentCount $wpService
) {
$this->postObject = $inner;
}

/**
Expand Down Expand Up @@ -62,4 +62,20 @@ public function getPostType(): string
{
return $this->wpPost->post_type;
}

/**
* @inheritDoc
*/
public function getIcon(): ?IconInterface
{
return $this->postObject->getIcon();
}

/**
* @inheritDoc
*/
public function getBlogId(): int
{
return $this->postObject->getBlogId();
}
}
Loading

0 comments on commit 235539e

Please sign in to comment.