Skip to content

Commit e19c703

Browse files
committed
refactor: extract autosave/revision check into shared WordPress helper
1 parent 1d0af6a commit e19c703

4 files changed

Lines changed: 142 additions & 18 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: 1 addition & 8 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.
@@ -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 testIsAutosaveOrRevisionReturnsFalseWhenPostIsNotAutosaveOrRevision()
25+
{
26+
$function_exists = $this->getFunctionMock($this->getNamespace(WordPress::class), 'function_exists');
27+
$function_exists->expects($this->exactly(2))
28+
->withConsecutive(
29+
[$this->identicalTo('wp_is_post_autosave')],
30+
[$this->identicalTo('wp_is_post_revision')]
31+
)
32+
->willReturn(true);
33+
34+
$wp_is_post_autosave = $this->getFunctionMock($this->getNamespace(WordPress::class), 'wp_is_post_autosave');
35+
$wp_is_post_autosave->expects($this->once())
36+
->with($this->identicalTo(42))
37+
->willReturn(false);
38+
39+
$wp_is_post_revision = $this->getFunctionMock($this->getNamespace(WordPress::class), 'wp_is_post_revision');
40+
$wp_is_post_revision->expects($this->once())
41+
->with($this->identicalTo(42))
42+
->willReturn(false);
43+
44+
$this->assertFalse(WordPress::isAutosaveOrRevision(42));
45+
}
46+
47+
public function testIsAutosaveOrRevisionReturnsFalseWhenWordPressFunctionsAreUnavailable()
48+
{
49+
$function_exists = $this->getFunctionMock($this->getNamespace(WordPress::class), 'function_exists');
50+
$function_exists->expects($this->once())
51+
->with($this->identicalTo('wp_is_post_autosave'))
52+
->willReturn(false);
53+
54+
$wp_is_post_autosave = $this->getFunctionMock($this->getNamespace(WordPress::class), 'wp_is_post_autosave');
55+
$wp_is_post_autosave->expects($this->never());
56+
57+
$wp_is_post_revision = $this->getFunctionMock($this->getNamespace(WordPress::class), 'wp_is_post_revision');
58+
$wp_is_post_revision->expects($this->never());
59+
60+
$this->assertFalse(WordPress::isAutosaveOrRevision(42));
61+
}
62+
63+
public function testIsAutosaveOrRevisionReturnsTrueWhenPostIsAutosave()
64+
{
65+
$function_exists = $this->getFunctionMock($this->getNamespace(WordPress::class), 'function_exists');
66+
$function_exists->expects($this->exactly(2))
67+
->withConsecutive(
68+
[$this->identicalTo('wp_is_post_autosave')],
69+
[$this->identicalTo('wp_is_post_revision')]
70+
)
71+
->willReturn(true);
72+
73+
$wp_is_post_autosave = $this->getFunctionMock($this->getNamespace(WordPress::class), 'wp_is_post_autosave');
74+
$wp_is_post_autosave->expects($this->once())
75+
->with($this->identicalTo(42))
76+
->willReturn(true);
77+
78+
$wp_is_post_revision = $this->getFunctionMock($this->getNamespace(WordPress::class), 'wp_is_post_revision');
79+
$wp_is_post_revision->expects($this->never());
80+
81+
$this->assertTrue(WordPress::isAutosaveOrRevision(42));
82+
}
83+
84+
public function testIsAutosaveOrRevisionReturnsTrueWhenPostIsRevision()
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(true);
103+
104+
$this->assertTrue(WordPress::isAutosaveOrRevision(42));
105+
}
106+
}

0 commit comments

Comments
 (0)