Skip to content

Commit

Permalink
feat: move post permalink seo redirect value to PostObject (#1269)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorbrink authored Jan 21, 2025
1 parent 2d558ca commit dcbc20e
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 45 deletions.
3 changes: 0 additions & 3 deletions library/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,6 @@ public function __construct(
$decorator = new \Municipio\PostDecorators\ApplyProjectTerms($decorator);
$decorator = new \Municipio\PostDecorators\ApplyProjectProgress($this->wpService, $decorator);

//Seo Redirect
$decorator = new \Municipio\PostDecorators\ApplySeoRedirect($this->wpService, $decorator);

return $decorator->apply($post);
}, 10, 1);

Expand Down
2 changes: 2 additions & 0 deletions library/Helper/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Municipio\PostObject\Decorators\PostObjectFromOtherBlog;
use Municipio\PostObject\Decorators\PostObjectFromWpPost;
use Municipio\PostObject\Decorators\PostObjectWithOtherBlogIdFromSwitchedState;
use Municipio\PostObject\Decorators\PostObjectWithSeoRedirect;
use Municipio\PostObject\Icon\Resolvers\CachedIconResolver;
use Municipio\PostObject\Icon\Resolvers\NullIconResolver;
use Municipio\PostObject\Icon\Resolvers\PostIconResolver;
Expand Down Expand Up @@ -156,6 +157,7 @@ private static function convertWpPostToPostObject(WP_Post $post, string $cacheGr
$acfService = \Municipio\Helper\AcfService::get();

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

$iconResolver = new TermIconResolver($postObject, $wpService, new Term($wpService, AcfService::get()), new NullIconResolver());
$iconResolver = new PostIconResolver($postObject, $acfService, $iconResolver);
Expand Down
40 changes: 0 additions & 40 deletions library/PostDecorators/ApplySeoRedirect.php

This file was deleted.

19 changes: 19 additions & 0 deletions library/PostObject/Decorators/BackwardsCompatiblePostObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,37 @@
#[AllowDynamicProperties]
class BackwardsCompatiblePostObject implements PostObjectInterface
{
private const PROPERTY_TO_METHOD_MAP = ['permalink' => 'getPermalink'];

/**
* Constructor.
*/
public function __construct(private PostObjectInterface $postObject, object $legacyPost)
{
foreach ($legacyPost as $key => $value) {
if (array_key_exists($key, self::PROPERTY_TO_METHOD_MAP)) {
continue;
}

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

/**
* Magic getter.
*
* @param string $name
* @return mixed
*/
public function __get($name)
{
if (array_key_exists($name, self::PROPERTY_TO_METHOD_MAP)) {
return $this->{self::PROPERTY_TO_METHOD_MAP[$name]}();
}
}

/**
* @inheritDoc
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ class BackwardsCompatiblePostObjectTest extends TestCase
public function testInheritsPublicPropertiesFromObject()
{
$legacyPost = (object) [
'permalink' => 'http://example.com',
'title' => 'TestTitle',
];

$result = new BackwardsCompatiblePostObject($this->getPostObject(), $legacyPost);

$this->assertEquals('http://example.com', $result->permalink);
$this->assertEquals('TestTitle', $result->title);
}

/**
Expand All @@ -35,6 +35,33 @@ public function testForwardsGetIdToPostObject()
$this->assertEquals(123, $result->getId());
}

/**
* @testdox does not set ->permalink from legacy post
*/
public function testDoesNotSetPermalinkFromLegacyPost()
{
$legacyPost = (object) [
'permalink' => 'http://example.com',
];

$result = new BackwardsCompatiblePostObject($this->getPostObject(), $legacyPost);

$this->assertNotEquals('http://example.com', @$result->permalink);
}

/**
* @testdox ->permalink returns the result from PostObject::getPermalink()
*/
public function testPermalinkReturnsTheResultFromPostObjectGetPermalink()
{
$postObject = $this->getPostObject();
$postObject->expects($this->once())->method('getPermalink')->willReturn('http://example.com');

$result = new BackwardsCompatiblePostObject($postObject, (object) []);

$this->assertEquals('http://example.com', $result->permalink);
}

private function getPostObject(): PostObjectInterface|MockObject
{
return $this->createMock(PostObjectInterface::class);
Expand Down
90 changes: 90 additions & 0 deletions library/PostObject/Decorators/PostObjectWithSeoRedirect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Municipio\PostObject\Decorators;

use Municipio\PostObject\Icon\IconInterface;
use Municipio\PostObject\PostObjectInterface;
use WpService\Contracts\GetPostMeta;

/**
* PostObjectWithSeoRedirect class.
*
* Applies the SEO redirect to the post object permalink if a redirect is set.
*/
class PostObjectWithSeoRedirect implements PostObjectInterface
{
/**
* Constructor.
*/
public function __construct(private PostObjectInterface $postObject, private GetPostMeta $wpService)
{
}

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

/**
* @inheritDoc
* @codeCoverageIgnore
*/
public function getTitle(): string
{
return $this->postObject->getTitle();
}

/**
* @inheritDoc
*/
public function getPermalink(): string
{
$seoRedirectMetaUrl = $this->wpService->getPostMeta($this->postObject->getId(), 'redirect', true);

if (filter_var($seoRedirectMetaUrl, FILTER_VALIDATE_URL)) {
return $seoRedirectMetaUrl;
}

return $this->postObject->getPermalink();
}

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

/**
* @inheritDoc
* @codeCoverageIgnore
*/
public function getPostType(): string
{
return $this->postObject->getPostType();
}

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

/**
* @inheritDoc
* @codeCoverageIgnore
*/
public function getIcon(): ?IconInterface
{
return $this->postObject->getIcon();
}
}
58 changes: 58 additions & 0 deletions library/PostObject/Decorators/PostObjectWithSeoRedirect.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Municipio\PostObject\Decorators;

use Municipio\PostObject\Icon\IconInterface;
use Municipio\PostObject\PostObjectInterface;
use PHPUnit\Framework\TestCase;
use WpService\Contracts\GetPostMeta;

class PostObjectWithSeoRedirectTest extends TestCase
{
/**
* @testdox class can be instantiated
*/
public function testClassCanBeInstantiated()
{
$postObject = $this->createMock(PostObjectInterface::class);
$wpService = $this->createMock(GetPostMeta::class);

$this->assertInstanceOf(PostObjectWithSeoRedirect::class, new PostObjectWithSeoRedirect($postObject, $wpService));
}

/**
* @testdox getPermalink returns redirect url if set
*/
public function testGetPermalinkReturnsRedirectUrlIfSet()
{
$postObject = $this->createMock(PostObjectInterface::class);
$wpService = $this->createMock(GetPostMeta::class);

$postObject->method('getId')->willReturn(1);
$postObject->method('getPermalink')->willReturn('http://example.com/post');

$wpService->method('getPostMeta')->willReturn('http://example.com/redirect');

$postObjectWithSeoRedirect = new PostObjectWithSeoRedirect($postObject, $wpService);

$this->assertEquals('http://example.com/redirect', $postObjectWithSeoRedirect->getPermalink());
}

/**
* @testdox getPermalink returns original permalink if redirect url is not set
*/
public function testGetPermalinkReturnsOriginalPermalinkIfRedirectUrlIsNotSet()
{
$postObject = $this->createMock(PostObjectInterface::class);
$wpService = $this->createMock(GetPostMeta::class);

$postObject->method('getId')->willReturn(1);
$postObject->method('getPermalink')->willReturn('http://example.com/post');

$wpService->method('getPostMeta')->willReturn('');

$postObjectWithSeoRedirect = new PostObjectWithSeoRedirect($postObject, $wpService);

$this->assertEquals('http://example.com/post', $postObjectWithSeoRedirect->getPermalink());
}
}

0 comments on commit dcbc20e

Please sign in to comment.