Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.
/ sunny Public archive

Commit 5a63e92

Browse files
committed
Merge branch 'purge-adjacent-posts'
2 parents 944653e + abe6cba commit 5a63e92

File tree

8 files changed

+160
-16
lines changed

8 files changed

+160
-16
lines changed

src/Posts/RelatedUrls/RelatedUrls.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
namespace TypistTech\Sunny\Posts\RelatedUrls;
2020

21+
use TypistTech\Sunny\Posts\RelatedUrls\Strategies\AdjacentPostUrls;
2122
use TypistTech\Sunny\Posts\RelatedUrls\Strategies\AuthorUrls;
2223
use TypistTech\Sunny\Posts\RelatedUrls\Strategies\Culprit;
2324
use TypistTech\Sunny\Posts\RelatedUrls\Strategies\FeedUrls;
@@ -51,6 +52,7 @@ public function __construct(array $strategies = null)
5152
{
5253
$strategies = $strategies ?? [
5354
new Culprit,
55+
new AdjacentPostUrls,
5456
new TermsUrls('category'),
5557
new TermsUrls('post_tag'),
5658
new AuthorUrls,
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
/**
3+
* Sunny
4+
*
5+
* Automatically purge CloudFlare cache, including cache everything rules.
6+
*
7+
* @package Sunny
8+
*
9+
* @author Typist Tech <[email protected]>
10+
* @copyright 2017 Typist Tech
11+
* @license GPL-2.0+
12+
*
13+
* @see https://www.typist.tech/projects/sunny
14+
* @see https://wordpress.org/plugins/sunny/
15+
*/
16+
17+
declare(strict_types=1);
18+
19+
namespace TypistTech\Sunny\Posts\RelatedUrls\Strategies;
20+
21+
use WP_Post;
22+
23+
/**
24+
* Final class AdjacentPostUrls
25+
*
26+
* @todo This class needs refactor!
27+
* @todo Don't use global objects.
28+
* @todo Needs a better way to find next/previous posts.
29+
*/
30+
final class AdjacentPostUrls implements StrategyInterface
31+
{
32+
const MAX_TRY = 5;
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function getKey(): string
38+
{
39+
return 'adjacent-post';
40+
}
41+
42+
/**
43+
* Get adjacent post urls related to this post.
44+
*
45+
* @param WP_Post $post The WP_Post object from which relationships are determined.
46+
*
47+
* @return string[]
48+
*/
49+
public function locate(WP_Post $post): array
50+
{
51+
$related[] = $this->getPreviousPostLink($post, self::MAX_TRY);
52+
$related[] = $this->getNextPostLink($post, self::MAX_TRY);
53+
54+
return array_values(array_filter($related));
55+
}
56+
57+
/**
58+
* Find the previous publicly published post url. Quit when counter reaches zero.
59+
*
60+
* @param WP_Post $currentPost Current post.
61+
* @param int $counter Trials left before quit.
62+
*
63+
* @return string|null
64+
*/
65+
private function getPreviousPostLink(WP_Post $currentPost, int $counter)
66+
{
67+
global $post;
68+
69+
if ($counter < 1) {
70+
return null;
71+
}
72+
$counter--;
73+
74+
// @codingStandardsIgnoreStart
75+
$post = $currentPost;
76+
$prevPost = get_previous_post();
77+
// @codingStandardsIgnoreEnd
78+
79+
if (! $prevPost instanceof WP_Post) {
80+
return null;
81+
}
82+
83+
if ('publish' !== get_post_status($prevPost->ID)) {
84+
return $this->getPreviousPostLink($prevPost, $counter);
85+
}
86+
87+
$url = get_permalink($prevPost->ID);
88+
if (! is_string($url)) {
89+
return null;
90+
}
91+
92+
return $url;
93+
}
94+
95+
/**
96+
* Find the previous publicly published post url. Quit when counter reaches zero.
97+
*
98+
* @param WP_Post $currentPost Current post.
99+
* @param int $counter Trials left before quit.
100+
*
101+
* @return string|null
102+
*/
103+
private function getNextPostLink(WP_Post $currentPost, int $counter)
104+
{
105+
global $post;
106+
107+
if ($counter < 1) {
108+
return null;
109+
}
110+
$counter--;
111+
112+
// @codingStandardsIgnoreStart
113+
$post = $currentPost;
114+
$nextPost = get_next_post();
115+
// @codingStandardsIgnoreEnd
116+
117+
if (! $nextPost instanceof WP_Post) {
118+
return null;
119+
}
120+
121+
if ('publish' !== get_post_status($nextPost->ID)) {
122+
return $this->getNextPostLink($nextPost, $counter);
123+
}
124+
125+
$url = get_permalink($nextPost->ID);
126+
if (! is_string($url)) {
127+
return null;
128+
}
129+
130+
return $url;
131+
}
132+
}

tests/_data/dump.sql

Lines changed: 9 additions & 9 deletions
Large diffs are not rendered by default.

tests/functional/Ads/DonateMeNoticeShowsUpAfterOneMonthCept.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
$I->amOnAdminPage('/options-general.php');
2828
$I->seeLink('small donation', 'https://www.typist.tech/donate/sunny/');
2929

30-
$I->wantToTest('new donate me notice last enqueue timestamp is set in the last 3 seconds');
30+
$I->wantToTest('new donate me notice last enqueue timestamp is set in the last 10 seconds');
3131
$lastEnqueueTimestamp = $I->grabOptionFromDatabase($optionName);
32-
$I->assertGreaterOrEquals(time() - 3, $lastEnqueueTimestamp->getTimestamp());
32+
$I->assertGreaterOrEquals(time() - 10, $lastEnqueueTimestamp->getTimestamp());
3333
$I->assertLessOrEquals(time(), $lastEnqueueTimestamp->getTimestamp());

tests/functional/Ads/HireMeNoticeShowsUpAfterTwoWeeksCept.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
$I->see("If you're looking for help with development");
2929
$I->seeLink('contact form', 'https://www.typist.tech/contact/');
3030

31-
$I->wantToTest('new hire me notice last enqueue timestamp is set in the last 3 seconds');
31+
$I->wantToTest('new hire me notice last enqueue timestamp is set in the last 10 seconds');
3232
$lastEnqueueTimestamp = $I->grabOptionFromDatabase($optionName);
33-
$I->assertGreaterOrEquals(time() - 3, $lastEnqueueTimestamp->getTimestamp());
33+
$I->assertGreaterOrEquals(time() - 10, $lastEnqueueTimestamp->getTimestamp());
3434
$I->assertLessOrEquals(time(), $lastEnqueueTimestamp->getTimestamp());

tests/functional/Ads/ReviewMeNoticeShowsUpAfterFifteenDaysCept.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
$I->amOnAdminPage('/options-general.php');
2828
$I->seeLink('leave a review on WordPress.org', 'https://wordpress.org/support/plugin/sunny/reviews/?filter=5#new-post');
2929

30-
$I->wantToTest('new review me notice last enqueue timestamp is set in the last 3 seconds');
30+
$I->wantToTest('new review me notice last enqueue timestamp is set in the last 10 seconds');
3131
$lastEnqueueTimestamp = $I->grabOptionFromDatabase($optionName);
32-
$I->assertGreaterOrEquals(time() - 3, $lastEnqueueTimestamp->getTimestamp());
32+
$I->assertGreaterOrEquals(time() - 10, $lastEnqueueTimestamp->getTimestamp());
3333
$I->assertLessOrEquals(time(), $lastEnqueueTimestamp->getTimestamp());

tests/integration.suite.example.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
modules:
77
config:
8-
WPLoader:
8+
WPLoader:
99
wpRootFolder: /Users/me/vagrant-local/www/sunny-tester/htdocs/
1010
dbHost: vvv.dev
1111
dbName: sunny-tester

tests/restapi/Posts/RelatedUrls/RelatedUrlsIndexCest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ public function testForbidden(RestapiTester $I)
1515
$I->assertForbiddenGet('/sunny/v2/posts/' . self::POST_ID . '/related-urls');
1616
}
1717

18+
public function testGetContainsAdjacentPost(RestapiTester $I)
19+
{
20+
$this->assertGetContains(
21+
$I,
22+
'adjacent-post',
23+
'/2012/10/02/many-categories/',
24+
'/2012/12/02/post-format-video-videopress/'
25+
);
26+
}
27+
1828
public function testGetContainsAuthor(RestapiTester $I)
1929
{
2030
$this->assertGetContains(

0 commit comments

Comments
 (0)