Skip to content

Commit

Permalink
fix: filter plugins_url to fix multisite issue
Browse files Browse the repository at this point in the history
  • Loading branch information
carlalexander committed Aug 10, 2020
1 parent 3e5eb23 commit 787303f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Configuration/EventManagementConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function modify(Container $container)
new Subscriber\ImageEditorSubscriber($container['console_client'], $container['file_manager']),
new Subscriber\PluploadSubscriber($container['plugin_relative_path'], $container['rest_namespace'], $container['assets_url'], $container['plupload_error_messages']),
new Subscriber\RestApiSubscriber($container['rest_namespace'], $container['rest_endpoints']),
new Subscriber\WordPressSubscriber($container['server_software']),
new Subscriber\WordPressSubscriber($container['server_software'], $container['site_url']),
];
});
}
Expand Down
30 changes: 29 additions & 1 deletion src/Subscriber/WordPressSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@ class WordPressSubscriber implements SubscriberInterface
*/
private $serverSoftware;

/**
* WordPress site URL.
*
* @var string
*/
private $siteUrl;

/**
* Constructor.
*/
public function __construct(string $serverSoftware)
public function __construct(string $serverSoftware, string $siteUrl)
{
$this->serverSoftware = strtolower($serverSoftware);
$this->siteUrl = rtrim($siteUrl, '/');
}

/**
Expand All @@ -42,6 +50,7 @@ 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 @@ -71,6 +80,25 @@ 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
17 changes: 12 additions & 5 deletions tests/Unit/Subscriber/WordPressSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,36 @@ class WordPressSubscriberTest extends TestCase
{
public function testEnableUrlRewriteWithOtherServerSoftware()
{
$this->assertFalse((new WordPressSubscriber('PHP'))->enableUrlRewrite(false));
$this->assertFalse((new WordPressSubscriber('PHP', $this->faker->url))->enableUrlRewrite(false));
}

public function testEnableUrlRewriteWithYmirServerSoftware()
{
$this->assertTrue((new WordPressSubscriber('YMIR'))->enableUrlRewrite(false));
$this->assertTrue((new WordPressSubscriber('YMIR', $this->faker->url))->enableUrlRewrite(false));
}

public function testEnableVisualEditorWithOtherServerSoftware()
{
$this->assertFalse((new WordPressSubscriber('PHP'))->enableVisualEditor(false));
$this->assertFalse((new WordPressSubscriber('PHP', $this->faker->url))->enableVisualEditor(false));
}

public function testEnableVisualEditorWithYmirServerSoftware()
{
$this->assertTrue((new WordPressSubscriber('YMIR'))->enableVisualEditor(false));
$this->assertTrue((new WordPressSubscriber('YMIR', $this->faker->url))->enableVisualEditor(false));
}

public function testRewritePluginUrlOnlyKeepsDirectoryBelowPlugins()
{
$siteUrl = $this->faker->url;

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

public function testSanitizeFileNameCharacters()
{
$this->assertSame(
['?', '[', ']', '/', '\\', '=', '<', '>', ':', ';', ',', "'", '"', '&', '$', '#', '*', '(', ')', '|', '~', '`', '!', '{', '}', '+', chr(0)],
(new WordPressSubscriber('ymir'))->sanitizeFileNameCharacters()
(new WordPressSubscriber('YMIR', $this->faker->url))->sanitizeFileNameCharacters()
);
}
}

0 comments on commit 787303f

Please sign in to comment.