Skip to content

Commit 7a6d96b

Browse files
committed
refactor: extract autosave/revision check into shared WordPress helper
1 parent 1f30920 commit 7a6d96b

4 files changed

Lines changed: 144 additions & 20 deletions

File tree

src/Subscriber/Compatibility/ElementorSubscriber.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Ymir\Plugin\EventManagement\SubscriberInterface;
1717
use Ymir\Plugin\PageCache\ContentDeliveryNetworkPageCacheClientInterface;
1818
use Ymir\Plugin\Support\Collection;
19+
use Ymir\Plugin\Support\WordPress;
1920

2021
/**
2122
* Subscriber that handles Elementor compatibility.
@@ -73,7 +74,7 @@ public static function getSubscribedEvents(): array
7374
*/
7475
public function clearElementorLoopPageCache(int $postId)
7576
{
76-
if ($this->isAutosaveOrRevision($postId)) {
77+
if (WordPress::isAutosaveOrRevision($postId)) {
7778
return;
7879
}
7980

@@ -125,7 +126,7 @@ public function clearElementorLoopPages()
125126
*/
126127
public function clearElementorLoopPagesOnSave(int $postId)
127128
{
128-
if ($this->isAutosaveOrRevision($postId)) {
129+
if (WordPress::isAutosaveOrRevision($postId)) {
129130
return;
130131
}
131132

@@ -215,12 +216,4 @@ private function getElementorLoopPageIds(): Collection
215216

216217
return new Collection($loopPagesIds);
217218
}
218-
219-
/**
220-
* Check if a post save is an autosave or revision.
221-
*/
222-
private function isAutosaveOrRevision(int $postId): bool
223-
{
224-
return (bool) wp_is_post_autosave($postId) || (bool) wp_is_post_revision($postId);
225-
}
226219
}

src/Subscriber/Compatibility/WooCommerceSubscriber.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Ymir\Plugin\EventManagement\SubscriberInterface;
1919
use Ymir\Plugin\PageCache\ContentDeliveryNetworkPageCacheClientInterface;
2020
use Ymir\Plugin\Support\Collection;
21+
use Ymir\Plugin\Support\WordPress;
2122

2223
/**
2324
* Subscriber that handles WooCommerce compatibility.
@@ -135,7 +136,7 @@ public function clearCacheOnProductVariationUpdate($variationId, $variation)
135136
*/
136137
public function clearCacheOnProductSave(int $postId, \WP_Post $post, bool $update): void
137138
{
138-
if (!$update || $this->isAutosaveOrRevision($postId)) {
139+
if (!$update || WordPress::isAutosaveOrRevision($postId)) {
139140
return;
140141
}
141142

@@ -147,7 +148,7 @@ public function clearCacheOnProductSave(int $postId, \WP_Post $post, bool $updat
147148
*/
148149
public function clearCacheOnProductVariationSave(int $postId, \WP_Post $post, bool $update): void
149150
{
150-
if (!$update || $this->isAutosaveOrRevision($postId)) {
151+
if (!$update || WordPress::isAutosaveOrRevision($postId)) {
151152
return;
152153
}
153154

@@ -247,12 +248,4 @@ private function clearProductUrls($productId)
247248

248249
$this->pageCacheClient->clearUrls($urlsToClear);
249250
}
250-
251-
/**
252-
* Check if a post save is an autosave or revision.
253-
*/
254-
private function isAutosaveOrRevision(int $postId): bool
255-
{
256-
return (bool) wp_is_post_autosave($postId) || (bool) wp_is_post_revision($postId);
257-
}
258251
}

src/Support/WordPress.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Ymir WordPress plugin.
7+
*
8+
* (c) Carl Alexander <support@ymirapp.com>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Ymir\Plugin\Support;
15+
16+
/**
17+
* Helper class for interacting with WordPress core functions.
18+
*/
19+
class WordPress
20+
{
21+
/**
22+
* Check if a post save is an autosave or revision.
23+
*/
24+
public static function isAutosaveOrRevision(int $postId): bool
25+
{
26+
if (!function_exists('wp_is_post_autosave') || !function_exists('wp_is_post_revision')) {
27+
return false;
28+
}
29+
30+
return (bool) wp_is_post_autosave($postId) || (bool) wp_is_post_revision($postId);
31+
}
32+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Ymir WordPress plugin.
7+
*
8+
* (c) Carl Alexander <support@ymirapp.com>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Ymir\Plugin\Tests\Unit\Support;
15+
16+
use Ymir\Plugin\Support\WordPress;
17+
use Ymir\Plugin\Tests\Mock\FunctionMockTrait;
18+
use Ymir\Plugin\Tests\Unit\TestCase;
19+
20+
class WordPressTest extends TestCase
21+
{
22+
use FunctionMockTrait;
23+
24+
public function testIsAutosaveOrRevisionReturnsFalseWhenWordPressFunctionsAreUnavailable()
25+
{
26+
$function_exists = $this->getFunctionMock($this->getNamespace(WordPress::class), 'function_exists');
27+
$function_exists->expects($this->once())
28+
->with($this->identicalTo('wp_is_post_autosave'))
29+
->willReturn(false);
30+
31+
$wp_is_post_autosave = $this->getFunctionMock($this->getNamespace(WordPress::class), 'wp_is_post_autosave');
32+
$wp_is_post_autosave->expects($this->never());
33+
34+
$wp_is_post_revision = $this->getFunctionMock($this->getNamespace(WordPress::class), 'wp_is_post_revision');
35+
$wp_is_post_revision->expects($this->never());
36+
37+
$this->assertFalse(WordPress::isAutosaveOrRevision(42));
38+
}
39+
40+
public function testIsAutosaveOrRevisionReturnsTrueWhenPostIsAutosave()
41+
{
42+
$function_exists = $this->getFunctionMock($this->getNamespace(WordPress::class), 'function_exists');
43+
$function_exists->expects($this->exactly(2))
44+
->withConsecutive(
45+
[$this->identicalTo('wp_is_post_autosave')],
46+
[$this->identicalTo('wp_is_post_revision')]
47+
)
48+
->willReturn(true);
49+
50+
$wp_is_post_autosave = $this->getFunctionMock($this->getNamespace(WordPress::class), 'wp_is_post_autosave');
51+
$wp_is_post_autosave->expects($this->once())
52+
->with($this->identicalTo(42))
53+
->willReturn(true);
54+
55+
$wp_is_post_revision = $this->getFunctionMock($this->getNamespace(WordPress::class), 'wp_is_post_revision');
56+
$wp_is_post_revision->expects($this->never());
57+
58+
$this->assertTrue(WordPress::isAutosaveOrRevision(42));
59+
}
60+
61+
public function testIsAutosaveOrRevisionReturnsTrueWhenPostIsRevision()
62+
{
63+
$function_exists = $this->getFunctionMock($this->getNamespace(WordPress::class), 'function_exists');
64+
$function_exists->expects($this->exactly(2))
65+
->withConsecutive(
66+
[$this->identicalTo('wp_is_post_autosave')],
67+
[$this->identicalTo('wp_is_post_revision')]
68+
)
69+
->willReturn(true);
70+
71+
$wp_is_post_autosave = $this->getFunctionMock($this->getNamespace(WordPress::class), 'wp_is_post_autosave');
72+
$wp_is_post_autosave->expects($this->once())
73+
->with($this->identicalTo(42))
74+
->willReturn(false);
75+
76+
$wp_is_post_revision = $this->getFunctionMock($this->getNamespace(WordPress::class), 'wp_is_post_revision');
77+
$wp_is_post_revision->expects($this->once())
78+
->with($this->identicalTo(42))
79+
->willReturn(true);
80+
81+
$this->assertTrue(WordPress::isAutosaveOrRevision(42));
82+
}
83+
84+
public function testIsAutosaveOrRevisionReturnsFalseWhenPostIsNotAutosaveOrRevision()
85+
{
86+
$function_exists = $this->getFunctionMock($this->getNamespace(WordPress::class), 'function_exists');
87+
$function_exists->expects($this->exactly(2))
88+
->withConsecutive(
89+
[$this->identicalTo('wp_is_post_autosave')],
90+
[$this->identicalTo('wp_is_post_revision')]
91+
)
92+
->willReturn(true);
93+
94+
$wp_is_post_autosave = $this->getFunctionMock($this->getNamespace(WordPress::class), 'wp_is_post_autosave');
95+
$wp_is_post_autosave->expects($this->once())
96+
->with($this->identicalTo(42))
97+
->willReturn(false);
98+
99+
$wp_is_post_revision = $this->getFunctionMock($this->getNamespace(WordPress::class), 'wp_is_post_revision');
100+
$wp_is_post_revision->expects($this->once())
101+
->with($this->identicalTo(42))
102+
->willReturn(false);
103+
104+
$this->assertFalse(WordPress::isAutosaveOrRevision(42));
105+
}
106+
}

0 commit comments

Comments
 (0)