Skip to content

Commit aade37b

Browse files
committed
feat: add support for radicle
1 parent aacd145 commit aade37b

9 files changed

+268
-175
lines changed

src/Configuration/EventManagementConfiguration.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function modify(Container $container)
3636
// to "subscribers" which wires a lot of services together that use the upload directories.
3737
$container['priority_subscribers'] = $container->service(function (Container $container) {
3838
return [
39-
new Subscriber\BedrockSubscriber($container['ymir_project_type']),
39+
new Subscriber\RootsSubscriber($container['ymir_project_type']),
4040
new Subscriber\UploadsSubscriber($container['content_directory'], $container['content_url'], $container['public_cloud_storage_protocol'], $container['upload_url'], $container['upload_limit']),
4141
];
4242
});

src/Configuration/WordPressConfiguration.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function modify(Container $container)
9292
$container['home_url'] = $container->service(function (Container $container) {
9393
$homeUrl = $container['site_url'];
9494

95-
if ('bedrock' === $container['ymir_project_type']) {
95+
if (in_array($container['ymir_project_type'], ['bedrock', 'radicle'])) {
9696
$homeUrl = preg_replace('#wp/?$#i', '', $homeUrl);
9797
}
9898

src/Subscriber/AssetsSubscriber.php

+20-3
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,13 @@ public function rewriteEnqueuedUrl(string $url): string
193193
// cached) if an asset URL was enqueued directly.
194194
$uri = '/'.ltrim(preg_replace("#^{$this->siteUrl}(/assets/[^/]*)?#i", '', $url), '/');
195195

196-
// We need to ensure we always have the /wp/ prefix in the asset URLs when using Bedrock. This gets messed
197-
// up in multisite subdirectory installations because it would be handled by a rewrite rule normally. We
196+
// We need to ensure we always have the /wp/ prefix in the asset URLs when using Bedrock or Radicle. This gets
197+
// messed up in multisite subdirectory installations because it would be handled by a rewrite rule normally. We
198198
// need to handle it programmatically instead.
199-
if ('bedrock' === $this->projectType && !str_starts_with($uri, '/wp/') && !str_starts_with($uri, '/app/')) {
199+
if (
200+
('bedrock' === $this->projectType && !$this->startsWithAny($uri, ['/wp/', '/app/']))
201+
|| ('radicle' === $this->projectType && !$this->startsWithAny($uri, ['/wp/', '/content/', '/dist/']))
202+
) {
200203
$uri = '/wp'.$uri;
201204
}
202205

@@ -256,4 +259,18 @@ private function rewriteUrlWithUploadsUrl(string $pattern, string $url, int $mat
256259

257260
return empty($matches[$matchIndex]) ? $url : $this->uploadsUrl.'/'.ltrim($matches[$matchIndex], '/');
258261
}
262+
263+
/**
264+
* Check if the given string starts with any of the given needles.
265+
*/
266+
private function startsWithAny(string $haystack, array $needles): bool
267+
{
268+
foreach ($needles as $needle) {
269+
if (str_starts_with($haystack, $needle)) {
270+
return true;
271+
}
272+
}
273+
274+
return false;
275+
}
259276
}

src/Subscriber/RedirectSubscriber.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private function addSlashToWpAdmin(string $url, string $uri): string
109109

110110
$url = $this->mappedDomainNames->getPrimaryDomainNameUrl();
111111

112-
if ('bedrock' === $this->projectType) {
112+
if (in_array($this->projectType, ['bedrock', 'radicle'])) {
113113
$url .= '/wp';
114114
}
115115

src/Subscriber/BedrockSubscriber.php src/Subscriber/RootsSubscriber.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
use Ymir\Plugin\EventManagement\SubscriberInterface;
1717

1818
/**
19-
* Subscriber for Bedrock project support.
19+
* Subscriber for Roots project support.
2020
*/
21-
class BedrockSubscriber implements SubscriberInterface
21+
class RootsSubscriber implements SubscriberInterface
2222
{
2323
/**
2424
* The Ymir project type.
@@ -52,7 +52,7 @@ public static function getSubscribedEvents(): array
5252
*/
5353
public function ensureHomeUrlDoesntContainWp(string $homeUrl): string
5454
{
55-
if ($this->isBedrockProject() && str_ends_with($homeUrl, '/wp')) {
55+
if ($this->isRootsProject() && str_ends_with($homeUrl, '/wp')) {
5656
$homeUrl = substr($homeUrl, 0, -3);
5757
}
5858

@@ -64,7 +64,7 @@ public function ensureHomeUrlDoesntContainWp(string $homeUrl): string
6464
*/
6565
public function ensureNetworkSiteUrlContainsWp(string $networkSiteUrl, string $path): string
6666
{
67-
if (!$this->isBedrockProject()) {
67+
if (!$this->isRootsProject()) {
6868
return $networkSiteUrl;
6969
}
7070

@@ -78,22 +78,22 @@ public function ensureNetworkSiteUrlContainsWp(string $networkSiteUrl, string $p
7878
}
7979

8080
/**
81-
* Ensure that site URL contains the /wp subdirectory for Bedrock projects.
81+
* Ensure that site URL contains the /wp subdirectory for Roots projects.
8282
*/
8383
public function ensureSiteUrlContainsWp(string $siteUrl): string
8484
{
85-
if ($this->isBedrockProject() && !str_ends_with($siteUrl, '/wp') && (is_main_site() || is_subdomain_install())) {
85+
if ($this->isRootsProject() && !str_ends_with($siteUrl, '/wp') && (is_main_site() || is_subdomain_install())) {
8686
$siteUrl .= '/wp';
8787
}
8888

8989
return $siteUrl;
9090
}
9191

9292
/**
93-
* Checks if this is a Bedrock project.
93+
* Checks if this is a Roots project.
9494
*/
95-
private function isBedrockProject(): bool
95+
private function isRootsProject(): bool
9696
{
97-
return 'bedrock' === $this->projectType;
97+
return in_array($this->projectType, ['bedrock', 'radicle']);
9898
}
9999
}

tests/Unit/Subscriber/AssetsSubscriberTest.php

+33-6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public function provideReplaceUrlsInContent(): array
3131
];
3232
}
3333

34+
public function provideRootsProjectTypes(): array
35+
{
36+
return [
37+
['bedrock'],
38+
['radicle'],
39+
];
40+
}
41+
3442
public function testAddAssetsUrlToDnsPrefetchDoesntAddAssetsUrlWhenDomainDifferentFromSiteUrl()
3543
{
3644
$this->assertSame(['https://assets.com/assets/uuid'], (new AssetsSubscriber('content_dir', 'https://foo.com', 'https://assets.com/assets/uuid'))->addAssetsUrlToDnsPrefetch([], 'dns-prefetch'));
@@ -102,19 +110,35 @@ public function testRewriteContentUrlUsesContentDirConstant()
102110
$this->assertSame('https://assets.com/assets/uuid/app/test.php', (new AssetsSubscriber('app', 'https://foo.com', 'https://assets.com/assets/uuid'))->rewriteContentUrl('https://foo.com/foo/directory/app/test.php'));
103111
}
104112

105-
public function testRewriteEnqueuedUrlAddsWpWhenMissingWithBedrockProjectWithSourceSameAsSiteUrl()
113+
/**
114+
* @dataProvider provideRootsProjectTypes
115+
*/
116+
public function testRewriteEnqueuedUrlAddsWpWhenMissingWithRootsProjectWithSourceSameAsSiteUrl(string $projectType)
106117
{
107-
$this->assertSame('https://assets.com/assets/uuid/wp/asset.css', (new AssetsSubscriber('content_dir', 'https://foo.com', 'https://assets.com/assets/uuid', 'bedrock', 'https://assets.com/uploads'))->rewriteEnqueuedUrl('https://foo.com/asset.css'));
118+
$this->assertSame('https://assets.com/assets/uuid/wp/asset.css', (new AssetsSubscriber('content_dir', 'https://foo.com', 'https://assets.com/assets/uuid', $projectType, 'https://assets.com/uploads'))->rewriteEnqueuedUrl('https://foo.com/asset.css'));
108119
}
109120

110121
public function testRewriteEnqueuedUrlDoesntAddWpWithBedrockProjectWithAppUrl()
111122
{
112123
$this->assertSame('https://assets.com/assets/uuid/app/asset.css', (new AssetsSubscriber('content_dir', 'https://foo.com', 'https://assets.com/assets/uuid', 'bedrock', 'https://assets.com/uploads'))->rewriteEnqueuedUrl('https://foo.com/app/asset.css'));
113124
}
114125

115-
public function testRewriteEnqueuedUrlDoesntAddWpWithBedrockProjectWithSourceSameAsSiteUrl()
126+
public function testRewriteEnqueuedUrlDoesntAddWpWithRadicleProjectWithContentUrl()
116127
{
117-
$this->assertSame('https://assets.com/assets/uuid/wp/asset.css', (new AssetsSubscriber('content_dir', 'https://foo.com', 'https://assets.com/assets/uuid', 'bedrock', 'https://assets.com/uploads'))->rewriteEnqueuedUrl('https://foo.com/wp/asset.css'));
128+
$this->assertSame('https://assets.com/assets/uuid/content/asset.css', (new AssetsSubscriber('content_dir', 'https://foo.com', 'https://assets.com/assets/uuid', 'radicle', 'https://assets.com/uploads'))->rewriteEnqueuedUrl('https://foo.com/content/asset.css'));
129+
}
130+
131+
public function testRewriteEnqueuedUrlDoesntAddWpWithRadicleProjectWithDistUrl()
132+
{
133+
$this->assertSame('https://assets.com/assets/uuid/dist/asset.css', (new AssetsSubscriber('content_dir', 'https://foo.com', 'https://assets.com/assets/uuid', 'radicle', 'https://assets.com/uploads'))->rewriteEnqueuedUrl('https://foo.com/dist/asset.css'));
134+
}
135+
136+
/**
137+
* @dataProvider provideRootsProjectTypes
138+
*/
139+
public function testRewriteEnqueuedUrlDoesntAddWpWithRootsProjectWithSourceSameAsSiteUrl(string $projectType)
140+
{
141+
$this->assertSame('https://assets.com/assets/uuid/wp/asset.css', (new AssetsSubscriber('content_dir', 'https://foo.com', 'https://assets.com/assets/uuid', $projectType, 'https://assets.com/uploads'))->rewriteEnqueuedUrl('https://foo.com/wp/asset.css'));
118142
}
119143

120144
public function testRewriteEnqueuedUrlDoesntRemoveDoubleSlashesWhenUrlStartsWithDoubleSlash()
@@ -162,9 +186,12 @@ public function testRewriteEnqueuedUrlWithSourceSameAsUploadUrl()
162186
$this->assertSame('https://foo.com/uploads/asset.css', (new AssetsSubscriber('content_dir', 'https://foo.com', 'https://foo.com/assets/uuid', '', 'https://foo.com/uploads'))->rewriteEnqueuedUrl('https://foo.com/uploads/asset.css'));
163187
}
164188

165-
public function testRewriteIncludesUrlWithBedrockIncludesDirectory()
189+
/**
190+
* @dataProvider provideRootsProjectTypes
191+
*/
192+
public function testRewriteIncludesUrlWithRootsProjectIncludesDirectory(string $projectType)
166193
{
167-
$this->assertSame('https://assets.com/assets/uuid/wp/wp-includes/js/script.min.js', (new AssetsSubscriber('content_dir', 'https://foo.com', 'https://assets.com/assets/uuid', 'bedrock'))->rewriteIncludesUrl('https://foo.com/wp/wp-includes/js/script.min.js'));
194+
$this->assertSame('https://assets.com/assets/uuid/wp/wp-includes/js/script.min.js', (new AssetsSubscriber('content_dir', 'https://foo.com', 'https://assets.com/assets/uuid', $projectType))->rewriteIncludesUrl('https://foo.com/wp/wp-includes/js/script.min.js'));
168195
}
169196

170197
public function testRewriteIncludesUrlWithStandardIncludesDirectory()

tests/Unit/Subscriber/BedrockSubscriberTest.php

-150
This file was deleted.

tests/Unit/Subscriber/RedirectSubscriberTest.php

+18-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,18 @@ class RedirectSubscriberTest extends TestCase
2626
use FunctionMockTrait;
2727
use MappedDomainNamesMockTrait;
2828

29-
public function testAddSlashToBedrockWpAdminWithHttpHostDifferentThanPrimaryDomainName()
29+
public function provideRootsProjectTypes(): array
30+
{
31+
return [
32+
['bedrock'],
33+
['radicle'],
34+
];
35+
}
36+
37+
/**
38+
* @dataProvider provideRootsProjectTypes
39+
*/
40+
public function testAddSlashToRootsProjectWpAdminWithHttpHostDifferentThanPrimaryDomainName(string $projectType)
3041
{
3142
$mappedDomainNames = $this->getMappedDomainNamesMock();
3243
$mappedDomainNames->expects($this->once())
@@ -42,10 +53,13 @@ public function testAddSlashToBedrockWpAdminWithHttpHostDifferentThanPrimaryDoma
4253
->with($this->identicalTo('https://domain_name/wp/wp-admin/'), $this->identicalTo(301))
4354
->willReturn(false);
4455

45-
(new RedirectSubscriber($mappedDomainNames, 'another_domain_name', '/wp/wp-admin', false, 'bedrock'))->redirect();
56+
(new RedirectSubscriber($mappedDomainNames, 'another_domain_name', '/wp/wp-admin', false, $projectType))->redirect();
4657
}
4758

48-
public function testAddSlashToBedrockWpAdminWithHttpHostSameAsPrimaryDomainName()
59+
/**
60+
* @dataProvider provideRootsProjectTypes
61+
*/
62+
public function testAddSlashToRootsProjectWpAdminWithHttpHostSameAsPrimaryDomainName(string $projectType)
4963
{
5064
$mappedDomainNames = $this->getMappedDomainNamesMock();
5165
$mappedDomainNames->expects($this->once())
@@ -61,7 +75,7 @@ public function testAddSlashToBedrockWpAdminWithHttpHostSameAsPrimaryDomainName(
6175
->with($this->identicalTo('https://domain_name/wp/wp-admin/'), $this->identicalTo(301))
6276
->willReturn(false);
6377

64-
(new RedirectSubscriber($mappedDomainNames, 'domain_name', '/wp/wp-admin', false, 'bedrock'))->redirect();
78+
(new RedirectSubscriber($mappedDomainNames, 'domain_name', '/wp/wp-admin', false, $projectType))->redirect();
6579
}
6680

6781
public function testAddSlashToWpAdminWithHttpHostDifferentThanPrimaryDomainName()

0 commit comments

Comments
 (0)