diff --git a/src/Admin/RemoveDashboardWidgets.php b/src/Admin/RemoveDashboardWidgets.php index af6d18a..ec6e10e 100644 --- a/src/Admin/RemoveDashboardWidgets.php +++ b/src/Admin/RemoveDashboardWidgets.php @@ -4,9 +4,10 @@ namespace Fire\Admin; -class RemoveDashboardWidgets +final class RemoveDashboardWidgets { - protected array $ids = []; + /** @var string[] */ + protected readonly array $ids; public function __construct(string ...$ids) { @@ -16,6 +17,7 @@ public function __construct(string ...$ids) public function register(): self { add_action('wp_dashboard_setup', [$this, 'remove']); + return $this; } diff --git a/src/Core/CacheBustScripts.php b/src/Core/CacheBustScripts.php index 8ef75ed..6f6101e 100644 --- a/src/Core/CacheBustScripts.php +++ b/src/Core/CacheBustScripts.php @@ -6,12 +6,12 @@ use InvalidArgumentException; -class CacheBustScripts +final class CacheBustScripts { protected int $hashLength = 10; /** @var string[] */ - protected array $validHosts; + protected readonly array $validHosts; public function __construct(string ...$validHosts) { @@ -30,6 +30,7 @@ public function register(): self add_filter('script_loader_src', [$this, 'src']); add_filter('style_loader_src', [$this, 'src']); + return $this; } @@ -57,12 +58,14 @@ public function src(string $src): string // Remove version and append to filename $src = remove_query_arg('ver', $src); $ver = substr(sha1($ver), 0, $this->hashLength); + return preg_replace('/\.(js|css)(\?.*)?$/', ".$ver.\$1\$2", $src); } public function setHashLength(int $length): self { $this->hashLength = $length; + return $this; } diff --git a/tests/Admin/RemoveDashboardWidgetsTest.php b/tests/Admin/RemoveDashboardWidgetsTest.php index 84eee33..c1b6947 100644 --- a/tests/Admin/RemoveDashboardWidgetsTest.php +++ b/tests/Admin/RemoveDashboardWidgetsTest.php @@ -14,6 +14,7 @@ final class RemoveDashboardWidgetsTest extends TestCase public function testActionsAdded(): void { $instance = (new RemoveDashboardWidgets())->register(); + $this->assertIsInt(has_action('wp_dashboard_setup', [$instance, 'remove'])); } diff --git a/tests/Core/CacheBustScriptsTest.php b/tests/Core/CacheBustScriptsTest.php index b13296e..27943e5 100644 --- a/tests/Core/CacheBustScriptsTest.php +++ b/tests/Core/CacheBustScriptsTest.php @@ -18,7 +18,9 @@ final class CacheBustScriptsTest extends TestCase public function testFiltersAdded(): void { when('is_admin')->justReturn(false); + $instance = (new CacheBustScripts($this->current))->register(); + $this->assertIsInt(has_filter('script_loader_src', [$instance, 'src'])); $this->assertIsInt(has_filter('style_loader_src', [$instance, 'src'])); } @@ -26,7 +28,9 @@ public function testFiltersAdded(): void public function testFiltersNotAddedForAdmin(): void { when('is_admin')->justReturn(true); + $instance = (new CacheBustScripts($this->current))->register(); + $this->assertFalse(has_filter('script_loader_src', [$instance, 'src'])); $this->assertFalse(has_filter('style_loader_src', [$instance, 'src'])); } @@ -34,18 +38,21 @@ public function testFiltersNotAddedForAdmin(): void public function testNoValidHosts(): void { $this->expectException(InvalidArgumentException::class); + new CacheBustScripts('//'); } public function testFallbackHosts(): void { when('home_url')->justReturn($this->current); + $this->assertSame(parse_hosts($this->current), (new CacheBustScripts())->validHosts()); } public function testLocal(): void { $this->functions(); + $bust = new CacheBustScripts($this->current); $this->assertSame( @@ -75,20 +82,25 @@ public function testLocal(): void public function testLocalWithoutVersion(): void { $this->functions(); + $bust = new CacheBustScripts($this->current); + $this->assertSame($src = 'https://domain.com/file.js?a=b&c=d', $bust->src($src)); } public function testExternal(): void { $this->functions(); + $bust = new CacheBustScripts($this->current); + $this->assertSame($src = 'https://google.com/fonts.css', $bust->src($src)); } public function testLength(): void { $this->functions(); + $bust = (new CacheBustScripts($this->current))->setHashLength(5); $this->assertSame( @@ -100,7 +112,9 @@ public function testLength(): void public function testBadSrc(): void { $this->functions(); + $bust = new CacheBustScripts($this->current); + $this->assertSame($src = '//', $bust->src($src)); }