From 24221b822cfda8130fe3ce21a335bd6c9415d312 Mon Sep 17 00:00:00 2001 From: Carl Alexander Date: Tue, 4 Aug 2020 13:38:16 -0400 Subject: [PATCH] fix: handle url rewrite for bedrock assets --- .../EventManagementConfiguration.php | 2 +- src/Configuration/YmirConfiguration.php | 32 +++++++++++++++++++ src/Plugin.php | 1 + src/Subscriber/AssetsSubscriber.php | 23 +++++++++++-- .../Unit/Subscriber/AssetsSubscriberTest.php | 14 ++++++++ 5 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 src/Configuration/YmirConfiguration.php diff --git a/src/Configuration/EventManagementConfiguration.php b/src/Configuration/EventManagementConfiguration.php index b5fec43..a3da11c 100644 --- a/src/Configuration/EventManagementConfiguration.php +++ b/src/Configuration/EventManagementConfiguration.php @@ -42,7 +42,7 @@ public function modify(Container $container) $container['subscribers'] = $container->service(function (Container $container) { return [ - new Subscriber\AssetsSubscriber($container['site_url'], $container['assets_url']), + new Subscriber\AssetsSubscriber($container['site_url'], $container['assets_url'], $container['ymir_project_type']), new Subscriber\HttpApiSubscriber(), 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']), diff --git a/src/Configuration/YmirConfiguration.php b/src/Configuration/YmirConfiguration.php new file mode 100644 index 0000000..b506340 --- /dev/null +++ b/src/Configuration/YmirConfiguration.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Ymir\Plugin\Configuration; + +use Ymir\Plugin\DependencyInjection\Container; +use Ymir\Plugin\DependencyInjection\ContainerConfigurationInterface; + +/** + * Configures the dependency injection container with Ymir parameters and services. + */ +class YmirConfiguration implements ContainerConfigurationInterface +{ + /** + * {@inheritdoc} + */ + public function modify(Container $container) + { + $container['ymir_environment'] = getenv('YMIR_ENVIRONMENT') ?: ''; + $container['ymir_project_type'] = getenv('YMIR_PROJECT_TYPE') ?: 'wordpress'; + } +} diff --git a/src/Plugin.php b/src/Plugin.php index aa77571..f7d215a 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -99,6 +99,7 @@ public function load() Configuration\RestApiConfiguration::class, Configuration\UploadsConfiguration::class, Configuration\WordPressConfiguration::class, + Configuration\YmirConfiguration::class, ]); CloudStorageStreamWrapper::register($this->container['cloud_storage_client']); diff --git a/src/Subscriber/AssetsSubscriber.php b/src/Subscriber/AssetsSubscriber.php index db85172..17cddb4 100644 --- a/src/Subscriber/AssetsSubscriber.php +++ b/src/Subscriber/AssetsSubscriber.php @@ -27,6 +27,13 @@ class AssetsSubscriber implements SubscriberInterface */ private $assetsUrl; + /** + * The Ymir project type. + * + * @var string + */ + private $projectType; + /** * URL to the plugin's assets folder. * @@ -37,9 +44,10 @@ class AssetsSubscriber implements SubscriberInterface /** * Constructor. */ - public function __construct(string $siteUrl, string $assetsUrl = '') + public function __construct(string $siteUrl, string $assetsUrl = '', string $projectType = '') { $this->assetsUrl = rtrim($assetsUrl, '/'); + $this->projectType = $projectType; $this->siteUrl = rtrim($siteUrl, '/'); } @@ -59,10 +67,19 @@ public static function getSubscribedEvents(): array */ public function replaceLoaderSource(string $src): string { - if (empty($this->assetsUrl) || false !== stripos($src, $this->assetsUrl)) { + if (empty($this->assetsUrl) || false !== stripos($src, $this->assetsUrl) || false === stripos($src, $this->siteUrl)) { return $src; } - return str_ireplace($this->siteUrl, $this->assetsUrl, $src); + $src = str_ireplace($this->siteUrl, '', $src); + + // We need to ensure we always have the /wp/ prefix in the asset URLs when using Bedrock. This gets messed + // up in multisite subdirectory installations because it would be handled by a rewrite rule normally. We + // need to handle it programmatically instead. + if ('bedrock' === $this->projectType && '/wp/' !== substr($src, 0, 4)) { + $src = '/wp'.$src; + } + + return $this->assetsUrl.$src; } } diff --git a/tests/Unit/Subscriber/AssetsSubscriberTest.php b/tests/Unit/Subscriber/AssetsSubscriberTest.php index 7d6fa7d..ab0ab8e 100644 --- a/tests/Unit/Subscriber/AssetsSubscriberTest.php +++ b/tests/Unit/Subscriber/AssetsSubscriberTest.php @@ -37,6 +37,20 @@ public function testGetSubscribedEvents() $this->assertSame($subscribedEvents, $callbacks); } + public function testReplaceLoaderSourceAddsWpWhenMissingWithBedrockProjectWithSourceSameAsSiteUrl() + { + $subscriber = new AssetsSubscriber('https://foo.com', 'https://assets.com', 'bedrock'); + + $this->assertSame('https://assets.com/wp/asset.css', $subscriber->replaceLoaderSource('https://foo.com/asset.css')); + } + + public function testReplaceLoaderSourceDoesntAddWpWithBedrockProjectWithSourceSameAsSiteUrl() + { + $subscriber = new AssetsSubscriber('https://foo.com', 'https://assets.com', 'bedrock'); + + $this->assertSame('https://assets.com/wp/asset.css', $subscriber->replaceLoaderSource('https://foo.com/wp/asset.css')); + } + public function testReplaceLoaderSourceWithEmptyAssetsUrl() { $subscriber = new AssetsSubscriber('https://foo.com');