| 1 |
<?php # -*- coding: utf-8 -*- |
| 2 |
/* |
| 3 |
* This file is part of the Assets package. |
| 4 |
* |
| 5 |
* (c) Inpsyde GmbH |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace Inpsyde\Assets; |
| 12 |
|
| 13 |
// Exit early in case multiple Composer autoloaders try to include this file. |
| 14 |
if (function_exists(__NAMESPACE__.'\\assetSuffix')) { |
| 15 |
return; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Returns ".min" if SCRIPT_DEBUG is false. |
| 20 |
* |
| 21 |
* @return string |
| 22 |
*/ |
| 23 |
function assetSuffix(): string |
| 24 |
{ |
| 25 |
return defined('SCRIPT_DEBUG') && SCRIPT_DEBUG |
| 26 |
? '' |
| 27 |
: '.min'; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Adding the assetSuffix() before file extension to the given file. |
| 32 |
* |
| 33 |
* @param string $file |
| 34 |
* |
| 35 |
* @return string |
| 36 |
* @example before: my-script.js | after: my-script.min.js |
| 37 |
* |
| 38 |
*/ |
| 39 |
function withAssetSuffix(string $file): string |
| 40 |
{ |
| 41 |
$suffix = assetSuffix(); |
| 42 |
$extension = '.'.pathinfo($file, PATHINFO_EXTENSION); |
| 43 |
|
| 44 |
return str_replace( |
| 45 |
$extension, |
| 46 |
$suffix.$extension, |
| 47 |
$file |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Symlinks a folder inside the web-root for Assets, which are outside of the web-root |
| 53 |
* and returns a link to that folder. |
| 54 |
* |
| 55 |
* @param string $originDir |
| 56 |
* @param string $name |
| 57 |
* |
| 58 |
* @return string|null |
| 59 |
*/ |
| 60 |
function symlinkedAssetFolder(string $originDir, string $name): ?string |
| 61 |
{ |
| 62 |
// we're using realpath here, otherwise the comparisment with |
| 63 |
// readlink will not work. |
| 64 |
$originDir = realpath($originDir); |
| 65 |
|
| 66 |
$folderName = '/~inpsyde-assets/'; |
| 67 |
$rootPath = WP_CONTENT_DIR.$folderName; |
| 68 |
$rootUrl = WP_CONTENT_URL.$folderName; |
| 69 |
if (! is_dir($rootPath) && ! wp_mkdir_p($rootPath)) { |
| 70 |
return null; |
| 71 |
} |
| 72 |
|
| 73 |
$targetDir = $rootPath.$name; |
| 74 |
$targetUrl = trailingslashit($rootUrl.$name); |
| 75 |
|
| 76 |
if (is_link($targetDir)) { |
| 77 |
if (readlink($targetDir) === $originDir) { |
| 78 |
return $targetUrl; |
| 79 |
} |
| 80 |
unlink($targetDir); |
| 81 |
} |
| 82 |
|
| 83 |
if (! symlink($originDir, $targetDir)) { |
| 84 |
return null; |
| 85 |
} |
| 86 |
|
| 87 |
return $targetUrl; |
| 88 |
} |
| 89 |
|