Skip to content

Commit

Permalink
fix: handle url rewrite for bedrock assets
Browse files Browse the repository at this point in the history
  • Loading branch information
carlalexander committed Aug 4, 2020
1 parent bad1372 commit 24221b8
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Configuration/EventManagementConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']),
Expand Down
32 changes: 32 additions & 0 deletions src/Configuration/YmirConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

/*
* This file is part of Ymir WordPress plugin.
*
* (c) Carl Alexander <[email protected]>
*
* 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';
}
}
1 change: 1 addition & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
23 changes: 20 additions & 3 deletions src/Subscriber/AssetsSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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, '/');
}

Expand All @@ -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;
}
}
14 changes: 14 additions & 0 deletions tests/Unit/Subscriber/AssetsSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 24221b8

Please sign in to comment.