Skip to content

Commit

Permalink
fix: confused CONTENT_DIR with `WP_CONTENT_DIR
Browse files Browse the repository at this point in the history
  • Loading branch information
carlalexander committed Dec 14, 2021
1 parent bc88745 commit eb81de2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Configuration/EventManagementConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function modify(Container $container)

$container['subscribers'] = $container->service(function (Container $container) {
$subscribers = [
new Subscriber\AssetsSubscriber($container['content_directory'], $container['site_url'], $container['assets_url'], $container['ymir_project_type'], $container['uploads_baseurl']),
new Subscriber\AssetsSubscriber($container['content_directory_name'], $container['site_url'], $container['assets_url'], $container['ymir_project_type'], $container['uploads_baseurl']),
new Subscriber\ContentDeliveryNetworkPageCachingSubscriber($container['cloudfront_client'], $container['rest_url'], $container['is_page_caching_disabled']),
new Subscriber\DisallowIndexingSubscriber($container['ymir_using_vanity_domain']),
new Subscriber\ImageEditorSubscriber($container['console_client'], $container['file_manager']),
Expand Down
3 changes: 2 additions & 1 deletion src/Configuration/WordPressConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public function modify(Container $container)
$container['blog_charset'] = $container->service(function () {
return get_bloginfo('charset');
});
$container['content_directory'] = defined('WP_CONTENT_DIR') ? WP_CONTENT_DIR : ABSPATH.'wp-content';
$container['content_directory'] = defined('WP_CONTENT_DIR') ? WP_CONTENT_DIR : '';
$container['content_directory_name'] = defined('CONTENT_DIR') ? CONTENT_DIR : 'wp-content';
$container['content_url'] = defined('WP_CONTENT_URL') ? WP_CONTENT_URL : '';
$container['current_user'] = $container->service(function () {
return wp_get_current_user();
Expand Down
16 changes: 8 additions & 8 deletions src/Subscriber/AssetsSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ class AssetsSubscriber implements SubscriberInterface
private $assetsUrl;

/**
* The WordPress content directory.
* The WordPress content directory name.
*
* @var string
*/
private $contentDirectory;
private $contentDirectoryName;

/**
* The Ymir project type.
Expand All @@ -59,10 +59,10 @@ class AssetsSubscriber implements SubscriberInterface
/**
* Constructor.
*/
public function __construct(string $contentDirectory, string $siteUrl, string $assetsUrl = '', string $projectType = '', string $uploadsUrl = '')
public function __construct(string $contentDirectoryName, string $siteUrl, string $assetsUrl = '', string $projectType = '', string $uploadsUrl = '')
{
$this->assetsUrl = rtrim($assetsUrl, '/');
$this->contentDirectory = '/'.ltrim($contentDirectory, '/');
$this->contentDirectoryName = '/'.ltrim($contentDirectoryName, '/');
$this->projectType = $projectType;
$this->siteUrl = rtrim($siteUrl, '/');
$this->uploadsUrl = rtrim($uploadsUrl, '/');
Expand Down Expand Up @@ -147,7 +147,7 @@ public function replaceUrlsInContent(string $content): string

$assetsHost = parse_url($this->assetsUrl, PHP_URL_HOST);
$siteHost = parse_url($this->siteUrl, PHP_URL_HOST);
$uploadsDirectory = $this->contentDirectory.'/uploads';
$uploadsDirectory = $this->contentDirectoryName.'/uploads';
$urls = $urls->unique();

// If we have a hardcoded URL to an asset, we want to dynamically update it to the
Expand All @@ -162,14 +162,14 @@ public function replaceUrlsInContent(string $content): string
$contentUrls = $urls->filter(function (string $url) use ($siteHost) {
return parse_url($url, PHP_URL_HOST) === $siteHost;
})->filter(function (string $url) {
return 0 === stripos(parse_url($url, PHP_URL_PATH), $this->contentDirectory);
return 0 === stripos(parse_url($url, PHP_URL_PATH), $this->contentDirectoryName);
});

// Point all non-uploads "/wp-content" URLs to the assets URL.
$nonUploadsUrls = $contentUrls->filter(function (string $url) use ($uploadsDirectory) {
return false === stripos(parse_url($url, PHP_URL_PATH), $uploadsDirectory);
})->mapWithKeys(function (string $url) {
return [$url => $this->rewriteAssetsUrl(sprintf('%%https?://[^/]*(%s.*)%%', $this->contentDirectory), $url)];
return [$url => $this->rewriteAssetsUrl(sprintf('%%https?://[^/]*(%s.*)%%', $this->contentDirectoryName), $url)];
})->all();

// Point all URLs to "/wp-content/uploads" to the uploads URL.
Expand All @@ -191,7 +191,7 @@ public function replaceUrlsInContent(string $content): string
*/
public function rewriteContentUrl(string $url): string
{
return $this->rewriteAssetsUrl(sprintf('%%https?://.*(%s.*)%%', $this->contentDirectory), $url);
return $this->rewriteAssetsUrl(sprintf('%%https?://.*(%s.*)%%', $this->contentDirectoryName), $url);
}

/**
Expand Down

0 comments on commit eb81de2

Please sign in to comment.