-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathfunctions.php
More file actions
86 lines (73 loc) · 1.83 KB
/
functions.php
File metadata and controls
86 lines (73 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
declare(strict_types=1);
namespace Inpsyde\Assets;
// Exit early in case multiple Composer autoloaders try to include this file.
if (function_exists(__NAMESPACE__ . '\\assetSuffix')) {
return;
}
/**
* Returns ".min" if SCRIPT_DEBUG is false.
*
* @return string
*/
function assetSuffix(): string
{
return defined('SCRIPT_DEBUG') && SCRIPT_DEBUG
? ''
: '.min';
}
/**
* Adding the assetSuffix() before file extension to the given file.
*
* @param string $file
*
* @return string
* @example before: my-script.js | after: my-script.min.js
*
*/
function withAssetSuffix(string $file): string
{
$suffix = assetSuffix();
$extension = '.' . pathinfo($file, PATHINFO_EXTENSION);
return str_replace(
$extension,
$suffix . $extension,
$file
);
}
/**
* Symlinks a folder inside the web-root for Assets, which are outside of the web-root
* and returns a link to that folder.
*
* @param string $originDir
* @param string $name
*
* @return string|null
*/
function symlinkedAssetFolder(string $originDir, string $name): ?string
{
// we're using realpath here, otherwise the comparison with
// readlink will not work.
$originDir = realpath($originDir);
$folderName = '/~inpsyde-assets/';
$rootPath = WP_CONTENT_DIR . $folderName;
$rootUrl = WP_CONTENT_URL . $folderName;
if (!is_dir($rootPath) && !wp_mkdir_p($rootPath)) {
return null;
}
$targetDir = $rootPath . $name;
$targetUrl = trailingslashit($rootUrl . $name);
if (is_link($targetDir)) {
if (readlink($targetDir) === $originDir) {
return $targetUrl;
}
unlink($targetDir);
}
if(!function_exists('symlink')) {
return null;
}
if (!symlink($originDir, $targetDir)) {
return null;
}
return $targetUrl;
}