Skip to content

Commit

Permalink
fix: add filter for wp-content url
Browse files Browse the repository at this point in the history
  • Loading branch information
carlalexander committed Dec 28, 2020
1 parent 0582265 commit 9d8d632
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 30 deletions.
41 changes: 40 additions & 1 deletion src/Subscriber/AssetsSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ public function __construct(string $siteUrl, string $assetsUrl = '', string $pro
public static function getSubscribedEvents(): array
{
return [
'content_url' => 'rewriteContentUrl',
'plugins_url' => 'rewritePluginsUrl',
'script_loader_src' => 'replaceSiteUrlWithAssetsUrl',
'style_loader_src' => 'replaceSiteUrlWithAssetsUrl',
'theme_root_uri' => 'replaceSiteUrlWithAssetsUrl',
'wp_resource_hints' => ['addAssetsUrlToDnsPrefetch', 10, 2],
];
}
Expand Down Expand Up @@ -97,4 +98,42 @@ public function replaceSiteUrlWithAssetsUrl(string $url): string

return $this->assetsUrl.$url;
}

/**
* Rewrite the wp-content URL so it points to the assets URL.
*/
public function rewriteContentUrl(string $url): string
{
$contentDirectoryName = '/wp-content';

if (defined('CONTENT_DIR')) {
$contentDirectoryName = CONTENT_DIR;
}

$contentDirectoryName = '/'.ltrim($contentDirectoryName, '/');

$matches = [];
preg_match(sprintf('/http(s)?:\/\/.*(%s.*)/', preg_quote($contentDirectoryName, '/')), $url, $matches);

if (empty($matches[2])) {
return $url;
}

return $this->assetsUrl.$matches[2];
}

/**
* Rewrite the plugins URL so it points to the assets URL.
*/
public function rewritePluginsUrl(string $url): string
{
$matches = [];
preg_match('/http(s)?:\/\/.*(\/[^\/]*\/plugins.*)/', $url, $matches);

if (empty($matches[2])) {
return $url;
}

return $this->assetsUrl.$matches[2];
}
}
20 changes: 0 additions & 20 deletions src/Subscriber/WordPressSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public static function getSubscribedEvents(): array
{
return [
'got_url_rewrite' => 'enableUrlRewrite',
'plugins_url' => 'rewritePluginUrl',
'sanitize_file_name_chars' => 'sanitizeFileNameCharacters',
'user_can_richedit' => 'enableVisualEditor',
];
Expand Down Expand Up @@ -80,25 +79,6 @@ public function enableVisualEditor(bool $visualEditorEnabled): bool
return $visualEditorEnabled;
}

/**
* Rewrite the plugin URL so that it matches the site URL.
*
* The plugin URL doesn't use the current site URL when you're on the non-primary site
* of a multisite installation. This breaks the URL rewriting for pointing assets to the
* assets URL.
*/
public function rewritePluginUrl(string $url): string
{
$matches = [];
preg_match('/http(s)?:\/\/.*(\/[^\/]*\/plugins.*)/', $url, $matches);

if (empty($matches[2])) {
return $url;
}

return $this->siteUrl.$matches[2];
}

/**
* Replace the list of characters that WordPress uses to sanitize file names.
*
Expand Down
20 changes: 19 additions & 1 deletion tests/Unit/Subscriber/AssetsSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ public function testGetSubscribedEvents()
}

$subscribedEvents = [
'content_url' => 'rewriteContentUrl',
'plugins_url' => 'rewritePluginsUrl',
'script_loader_src' => 'replaceSiteUrlWithAssetsUrl',
'style_loader_src' => 'replaceSiteUrlWithAssetsUrl',
'theme_root_uri' => 'replaceSiteUrlWithAssetsUrl',
'wp_resource_hints' => ['addAssetsUrlToDnsPrefetch', 10, 2],
];

Expand Down Expand Up @@ -88,4 +89,21 @@ public function testreplaceSiteUrlWithAssetsUrlWithSourceSameAsSiteUrl()
{
$this->assertSame('https://assets.com/asset.css', (new AssetsSubscriber('https://foo.com', 'https://assets.com'))->replaceSiteUrlWithAssetsUrl('https://foo.com/asset.css'));
}

public function testRewriteContentUrlDoesntKeepDirectoryBelowContentDir()
{
$this->assertSame('https://assets.com/wp-content/test.php', (new AssetsSubscriber('https://foo.com', 'https://assets.com'))->rewriteContentUrl('https://foo.com/foo/directory/wp-content/test.php'));
}

public function testRewriteContentUrlUsesContentDirConstant()
{
define('CONTENT_DIR', '/app');

$this->assertSame('https://assets.com/app/test.php', (new AssetsSubscriber('https://foo.com', 'https://assets.com'))->rewriteContentUrl('https://foo.com/foo/directory/app/test.php'));
}

public function testRewritePluginUrlOnlyKeepsDirectoryBelowPlugins()
{
$this->assertSame('https://assets.com/directory/plugins/test.php', (new AssetsSubscriber('https://foo.com', 'https://assets.com'))->rewritePluginsUrl('https://foo.com/foo/directory/plugins/test.php'));
}
}
8 changes: 0 additions & 8 deletions tests/Unit/Subscriber/WordPressSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,13 @@ public function testGetSubscribedEvents()

$subscribedEvents = [
'got_url_rewrite' => 'enableUrlRewrite',
'plugins_url' => 'rewritePluginUrl',
'sanitize_file_name_chars' => 'sanitizeFileNameCharacters',
'user_can_richedit' => 'enableVisualEditor',
];

$this->assertSame($subscribedEvents, $callbacks);
}

public function testRewritePluginUrlOnlyKeepsDirectoryBelowPlugins()
{
$siteUrl = 'https://'.$this->faker->domainName;

$this->assertSame($siteUrl.'/directory/plugins/test.php', (new WordPressSubscriber('PHP', $siteUrl))->rewritePluginUrl('https://'.$this->faker->domainName.'/foo/directory/plugins/test.php'));
}

public function testSanitizeFileNameCharacters()
{
$this->assertSame(
Expand Down

0 comments on commit 9d8d632

Please sign in to comment.