PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.4.14
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.4.14
1.4.14 1.4.13 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.54 1.3.53 1.3.52 1.3.51 1.3.50 1.3.49 1.3.48 1.3.47 1.3.46 1.3.45 All 177 releases
disco / vendor / inpsyde / assets / src / Util / AssetPathResolver.php

AssetPathResolver.php in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.4.14, at vendor/inpsyde/assets/src/Util/AssetPathResolver.php

122 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Assets package.
5 *
6 * (c) Inpsyde GmbH
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 declare(strict_types=1);
13
14 namespace Inpsyde\Assets\Util;
15
16 class AssetPathResolver
17 {
18 /**
19 * Attempt to resolve an assets path based on its URL.
20 *
21 * @param string $url
22 * @return null|string
23 */
24 public static function resolve(string $url): ?string
25 {
26 $normalizedUrl = set_url_scheme($url);
27
28 return self::resolveForThemeUrl($normalizedUrl)
29 ?? self::resolveForPluginUrl($normalizedUrl)
30 ?? self::resolveForVendorUrl($normalizedUrl)
31 ?? null;
32 }
33
34 /**
35 * @param string $normalizedUrl
36 * @return string|null
37 * @psalm-suppress PossiblyFalseArgument
38 */
39 public static function resolveForVendorUrl(string $normalizedUrl): ?string
40 {
41 // Now let's see if it's inside vendor.
42 // This is problematic, this is why vendor assets should be "published".
43
44 $fullVendorPath = wp_normalize_path(realpath(__DIR__ . '/../../../'));
45 $abspath = wp_normalize_path(ABSPATH);
46 $abspathParent = dirname($abspath);
47
48 $relativeVendorPath = null;
49 if (strpos($fullVendorPath, $abspath) === 0) {
50 $relativeVendorPath = substr($normalizedUrl, strlen($abspath));
51 } elseif (strpos($fullVendorPath, $abspathParent) === 0) {
52 $relativeVendorPath = substr($normalizedUrl, strlen($abspathParent));
53 }
54
55 if (!$relativeVendorPath) {
56 // vendor is not inside ABSPATH, nor inside its parent
57 return null;
58 }
59
60 $relativeVendorPath = trim($relativeVendorPath, '/');
61
62 // problematic, as said above: we are assuming vendor URL, but this assumption isn't safe
63 $vendorUrl = network_site_url("/{$relativeVendorPath}");
64
65 if (strpos($normalizedUrl, $vendorUrl) === 0) {
66 $relative = trim((string) substr($normalizedUrl, strlen($vendorUrl)), '/');
67
68 return trailingslashit($fullVendorPath) . $relative;
69 }
70
71 return null;
72 }
73
74 /**
75 * @param string $normalizedUrl
76 * @return string|null
77 */
78 public static function resolveForThemeUrl(string $normalizedUrl): ?string
79 {
80 $themeUrl = get_template_directory_uri();
81 $childUrl = get_stylesheet_directory_uri();
82
83 $base = '';
84 $relativeThemeUrl = null;
85 if (strpos($normalizedUrl, $childUrl) === 0) {
86 $base = get_stylesheet_directory();
87 $relativeThemeUrl = substr($normalizedUrl, strlen($childUrl));
88 } elseif (strpos($normalizedUrl, $themeUrl) === 0) {
89 $base = get_template_directory();
90 $relativeThemeUrl = substr($normalizedUrl, strlen($themeUrl));
91 }
92
93 return $relativeThemeUrl
94 ? trailingslashit($base) . trim($relativeThemeUrl, '/')
95 : null;
96 }
97
98 /**
99 * @param string $normalizedUrl
100 * @return string|null
101 */
102 public static function resolveForPluginUrl(string $normalizedUrl): ?string
103 {
104 $pluginsUrl = plugins_url('');
105 $muPluginsUrl = plugins_url('', WPMU_PLUGIN_DIR . '/file.php');
106
107 $basePath = '';
108 $relativePluginUrl = null;
109 if (strpos($normalizedUrl, $pluginsUrl) === 0) {
110 $basePath = WP_PLUGIN_DIR;
111 $relativePluginUrl = substr($normalizedUrl, strlen($pluginsUrl));
112 } elseif (strpos($normalizedUrl, $muPluginsUrl) === 0) {
113 $basePath = WPMU_PLUGIN_DIR;
114 $relativePluginUrl = substr($normalizedUrl, strlen($muPluginsUrl));
115 }
116
117 return $relativePluginUrl
118 ? trailingslashit(wp_normalize_path($basePath)) . trim($relativePluginUrl, '/')
119 : null;
120 }
121 }
122