Skip to content

Commit 775ef54

Browse files
authored
Merge pull request #1314 from helsingborg-stad/feat/adding-get-date-timestamp-to-post-object
feat: adding get date timestamp to post object
2 parents 4c9a56e + 0f1a6e3 commit 775ef54

20 files changed

+644
-0
lines changed

library/Helper/Post.php

+13
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
use Municipio\Integrations\Component\ImageResolver;
99
use ComponentLibrary\Integrations\Image\Image as ImageComponentContract;
1010
use Municipio\Helper\Term\Term;
11+
use Municipio\PostObject\Date\ArchiveDateSettingResolver;
1112
use Municipio\PostObject\Decorators\BackwardsCompatiblePostObject;
1213
use Municipio\PostObject\Decorators\IconResolvingPostObject;
1314
use Municipio\PostObject\Decorators\PostObjectFromOtherBlog;
1415
use Municipio\PostObject\Decorators\PostObjectFromWpPost;
16+
use Municipio\PostObject\Decorators\PostDateTimestamp;
17+
use Municipio\PostObject\Decorators\PostObjectDateTimestamp;
1518
use Municipio\PostObject\Decorators\PostObjectWithOtherBlogIdFromSwitchedState;
1619
use Municipio\PostObject\Decorators\PostObjectWithSeoRedirect;
1720
use Municipio\PostObject\Icon\Resolvers\CachedIconResolver;
@@ -20,6 +23,10 @@
2023
use Municipio\PostObject\Icon\Resolvers\TermIconResolver;
2124
use Municipio\PostObject\PostObject;
2225
use Municipio\PostObject\PostObjectInterface;
26+
use Municipio\PostObject\Date\CachedArchiveDateSettingResolver;
27+
use Municipio\PostObject\Date\CachedTimestampResolver;
28+
use Municipio\PostObject\Date\TimestampResolver;
29+
use Municipio\PostObject\Decorators\PostObjectArchiveDateTimestamp;
2330

2431
/**
2532
* Class Post
@@ -159,6 +166,12 @@ private static function convertWpPostToPostObject(WP_Post $post, string $cacheGr
159166
$postObject = new PostObjectFromWpPost(new PostObject($wpService), $post, $wpService);
160167
$postObject = new PostObjectWithSeoRedirect($postObject, $wpService);
161168

169+
$archiveDateSettingResolver = new ArchiveDateSettingResolver($postObject, $wpService);
170+
$archiveDateSettingResolver = new CachedArchiveDateSettingResolver($postObject, $wpService, $archiveDateSettingResolver);
171+
$timestampResolver = new TimestampResolver($postObject, $wpService, $archiveDateSettingResolver);
172+
$timestampResolver = new CachedTimestampResolver($postObject, $wpService, $timestampResolver);
173+
$postObject = new PostObjectArchiveDateTimestamp($postObject, $wpService, $timestampResolver);
174+
162175
$iconResolver = new TermIconResolver($postObject, $wpService, new Term($wpService, AcfService::get()), new NullIconResolver());
163176
$iconResolver = new PostIconResolver($postObject, $acfService, $iconResolver);
164177
$iconResolver = new CachedIconResolver($postObject, $iconResolver);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Municipio\PostObject\Date;
4+
5+
use Municipio\PostObject\PostObjectInterface;
6+
use WpService\Contracts\GetThemeMod;
7+
8+
/**
9+
* ArchiveDateSettingResolver class.
10+
*/
11+
class ArchiveDateSettingResolver implements ArchiveDateSettingResolverInterface
12+
{
13+
/**
14+
* Constructor.
15+
*/
16+
public function __construct(
17+
private PostObjectInterface $postObject,
18+
private GetThemeMod $wpService
19+
) {
20+
}
21+
22+
/**
23+
* Resolve the archive date setting.
24+
*/
25+
public function resolve(): string
26+
{
27+
return $this->wpService->getThemeMod('archive_' . $this->postObject->getPostType() . '_date_field', 'post_date');
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Municipio\PostObject\Date;
4+
5+
use Municipio\PostObject\PostObjectInterface;
6+
use PHPUnit\Framework\TestCase;
7+
use WpService\Implementations\FakeWpService;
8+
9+
class ArchiveDateSettingResolverTest extends TestCase
10+
{
11+
/**
12+
* @testdox class can be instantiated
13+
* @runInSeparateProcess
14+
*/
15+
public function testClassCanBeInstantiated()
16+
{
17+
$this->assertInstanceOf(
18+
ArchiveDateSettingResolver::class,
19+
new ArchiveDateSettingResolver($this->createMock(PostObjectInterface::class), new FakeWpService())
20+
);
21+
}
22+
23+
/**
24+
* @testdox resolve() returns archive post date setting
25+
* @runInSeparateProcess
26+
*/
27+
public function testResolveReturnsFoundArchiveSetting()
28+
{
29+
$postObject = $this->createMock(PostObjectInterface::class);
30+
$wpService = new FakeWpService(['getThemeMod' => 'metaKey']);
31+
32+
$resolver = new ArchiveDateSettingResolver($postObject, $wpService);
33+
34+
$result = $resolver->resolve();
35+
36+
$this->assertEquals('metaKey', $result);
37+
}
38+
39+
// TODO: How do i check default value?
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Municipio\PostObject\Date;
4+
5+
/**
6+
* ArchiveDateSettingResolverInterface interface.
7+
*/
8+
interface ArchiveDateSettingResolverInterface
9+
{
10+
/**
11+
* Resolve the archive date setting.
12+
*/
13+
public function resolve(): string;
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Municipio\PostObject\Date;
4+
5+
use Municipio\PostObject\PostObjectInterface;
6+
use WpService\Contracts\GetThemeMod;
7+
8+
/**
9+
* CachedArchiveDateSettingResolver class.
10+
*/
11+
class CachedArchiveDateSettingResolver implements ArchiveDateSettingResolverInterface
12+
{
13+
private static array $cache = [];
14+
15+
/**
16+
* Constructor.
17+
*/
18+
public function __construct(
19+
private PostObjectInterface $postObject,
20+
private GetThemeMod $wpService,
21+
private ArchiveDateSettingResolverInterface $innerResolver
22+
) {
23+
}
24+
25+
/**
26+
* Resolve the archive date setting.
27+
*/
28+
public function resolve(): string
29+
{
30+
$cacheKey = "{$this->postObject->getBlogId()}_{$this->postObject->getPostType()}";
31+
32+
if (array_key_exists($cacheKey, self::$cache)) {
33+
return self::$cache[$cacheKey];
34+
}
35+
return self::$cache[$cacheKey] = (string) $this->innerResolver->resolve();
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Municipio\PostObject\Date;
4+
5+
use Municipio\PostObject\PostObjectInterface;
6+
use PHPUnit\Framework\TestCase;
7+
use WpService\Implementations\FakeWpService;
8+
9+
class CachedArchiveDateSettingResolverTest extends TestCase
10+
{
11+
/**
12+
* @testdox class can be instantiated
13+
* @runInSeparateProcess
14+
*/
15+
public function testClassCanBeInstantiated()
16+
{
17+
$this->assertInstanceOf(
18+
CachedArchiveDateSettingResolver::class,
19+
new CachedArchiveDateSettingResolver(
20+
$this->createMock(PostObjectInterface::class),
21+
new FakeWpService(),
22+
$this->createMock(ArchiveDateSettingResolverInterface::class)
23+
)
24+
);
25+
}
26+
27+
/**
28+
* @testdox resolve() caches result from inner resolver
29+
* @runInSeparateProcess
30+
*/
31+
public function testResolveCachesResultFromInnerResolver()
32+
{
33+
$postObject = $this->createMock(PostObjectInterface::class);
34+
$postObject->method('getPostType')->willReturn('post_type');
35+
$postObject->method('getBlogId')->willReturn(1);
36+
$innerResolver = $this->createMock(ArchiveDateSettingResolverInterface::class);
37+
$innerResolver->expects($this->exactly(1))->method('resolve')->willReturn('metaKey');
38+
39+
$resolver = new CachedArchiveDateSettingResolver($postObject, new FakeWpService(), $innerResolver);
40+
41+
$result = $resolver->resolve();
42+
$result = $resolver->resolve();
43+
44+
$this->assertEquals('metaKey', $result);
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Municipio\PostObject\Date;
4+
5+
use Municipio\PostObject\PostObjectInterface;
6+
use WpService\Contracts\GetPostMeta;
7+
use WpService\Contracts\GetThemeMod;
8+
9+
/**
10+
* CachedTimestampResolver class.
11+
*/
12+
class CachedTimestampResolver implements TimestampResolverInterface
13+
{
14+
private static array $idCache = [];
15+
16+
/**
17+
* Constructor.
18+
*/
19+
public function __construct(
20+
private PostObjectInterface $postObject,
21+
private GetThemeMod&GetPostMeta $wpService,
22+
private TimestampResolverInterface $innerResolver
23+
) {
24+
}
25+
26+
/**
27+
* Resolve the timestamp.
28+
*
29+
* @return int
30+
*/
31+
public function resolve(): int
32+
{
33+
$cacheKey = "{$this->postObject->getBlogId()}_{$this->postObject->getId()}";
34+
35+
if (array_key_exists($cacheKey, self::$idCache)) {
36+
return self::$idCache[$cacheKey];
37+
}
38+
39+
return self::$idCache[$cacheKey] = $this->innerResolver->resolve();
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Municipio\PostObject\Date;
4+
5+
use Municipio\PostObject\PostObjectInterface;
6+
use PHPUnit\Framework\TestCase;
7+
use WpService\Implementations\FakeWpService;
8+
9+
class CachedTimestampResolverTest extends TestCase
10+
{
11+
/**
12+
* @testdox class can be instantiated
13+
* @runInSeparateProcess
14+
*/
15+
public function testClassCanBeInstantiated()
16+
{
17+
$this->assertInstanceOf(
18+
CachedTimestampResolver::class,
19+
new CachedTimestampResolver(
20+
$this->createMock(PostObjectInterface::class),
21+
new FakeWpService(),
22+
$this->createMock(TimestampResolverInterface::class)
23+
)
24+
);
25+
}
26+
27+
/**
28+
* @testdox resolve() caches result from inner resolver
29+
* @runInSeparateProcess
30+
*/
31+
public function testResolveCachesResultFromInnerResolver()
32+
{
33+
$postObject = $this->createMock(PostObjectInterface::class);
34+
$postObject->method('getId')->willReturn(1);
35+
$postObject->method('getBlogId')->willReturn(1);
36+
$innerResolver = $this->createMock(TimestampResolverInterface::class);
37+
$innerResolver->expects($this->exactly(1))->method('resolve')->willReturn(123);
38+
39+
$resolver = new CachedTimestampResolver($postObject, new FakeWpService(), $innerResolver);
40+
41+
$result = $resolver->resolve();
42+
$result = $resolver->resolve();
43+
44+
$this->assertEquals(123, $result);
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Municipio\PostObject\Date;
4+
5+
use Municipio\PostObject\PostObjectInterface;
6+
use WpService\Contracts\GetPostMeta;
7+
use Municipio\PostObject\Date\ArchiveDateSettingResolverInterface;
8+
9+
/**
10+
* TimestampResolver class.
11+
*/
12+
class TimestampResolver implements TimestampResolverInterface
13+
{
14+
private $defaultTimestamps = [
15+
'post_date',
16+
'post_modified'
17+
];
18+
19+
/**
20+
* Constructor.
21+
*/
22+
public function __construct(
23+
private PostObjectInterface $postObject,
24+
private GetPostMeta $wpService,
25+
private ArchiveDateSettingResolverInterface $archiveDateSetting
26+
) {
27+
}
28+
29+
/**
30+
* Resolve the timestamp.
31+
*
32+
* @return int
33+
*/
34+
public function resolve(): int
35+
{
36+
$archiveDateSetting = $this->archiveDateSetting->resolve();
37+
38+
if (in_array($archiveDateSetting, $this->defaultTimestamps)) {
39+
return $this->getDefaultTimestamp($archiveDateSetting);
40+
}
41+
42+
return $this->getDateMetaValue($archiveDateSetting);
43+
}
44+
45+
/**
46+
* Get the date meta value.
47+
*/
48+
private function getDateMetaValue(string $archiveDateSetting): int
49+
{
50+
$metaValue = $this->wpService->getPostMeta($this->postObject->getId(), $archiveDateSetting, true);
51+
52+
if ($metaValue) {
53+
return strtotime($metaValue);
54+
}
55+
56+
return 0;
57+
}
58+
59+
/**
60+
* Get the archive date setting.
61+
*/
62+
private function getDefaultTimestamp(string $archiveDateSetting): int
63+
{
64+
return $archiveDateSetting === 'post_modified' ?
65+
$this->postObject->getModifiedTime() :
66+
$this->postObject->getPublishedTime();
67+
}
68+
}

0 commit comments

Comments
 (0)