PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 2.13.0
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v2.13.0
4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.11.1 1.11.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.9.0 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.13.0 2.14.0 2.14.1 2.15.0 2.15.1 2.16.0 2.16.1 2.16.2 2.16.3 2.17.0 2.17.1 2.17.2 2.18.0 2.19.0 2.19.2 2.19.3 2.19.4 2.2.0 2.2.1 2.20.0 2.20.1 2.20.2 2.20.3 2.20.4 2.20.5 2.20.6 2.21.0 2.22.0 2.22.1 2.23.0 2.24.0 2.25.0 2.25.1 2.25.2 2.26.0 2.27.0 2.27.1 2.28.0 2.29.0 2.29.1 2.29.2 2.29.3 2.29.4 2.3.0 2.3.1 2.30.0 2.31.0 2.31.1 2.31.2 2.31.3 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.40.0 2.40.1 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.9.0 3.0.0 3.0.0-RC1 3.0.0-RC2 3.0.0-beta1 3.0.0-beta2 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.10.0 3.10.1 3.11.0 3.12.0 3.13.0 3.13.1 3.13.2 3.13.3 3.13.4 3.14.0 3.15.0 3.15.1 3.15.2 3.15.3 3.15.4 3.15.5 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.16.6 3.16.7 3.16.8 3.17.0 3.17.1 3.17.2 3.17.3 3.17.4 3.17.5 3.17.6 3.18.0 3.19.0 3.19.1 3.19.2 3.2.0 3.2.1 3.2.2 3.20.0 3.20.1 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 trunk 4.1.0 0.2.19.1 4.1.1 1.0.0 4.2.0 1.0.1 1.0.2
surecart / core / app-core / src / Assets / Assets.php
surecart / core / app-core / src / Assets Last commit date
Assets.php 3 years ago AssetsServiceProvider.php 3 years ago Manifest.php 3 years ago
Assets.php
226 lines
1 <?php
2 /**
3 * @package SureCartAppCore
4 * @author SureCart <support@surecart.com>
5 * @copyright SureCart
6 * @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7 * @link https://surecart.com
8 */
9
10 namespace SureCartAppCore\Assets;
11
12 use SureCartCore\Helpers\MixedType;
13 use SureCartCore\Helpers\Url;
14 use SureCartAppCore\Config\Config;
15
16 class Assets {
17 /**
18 * App root path.
19 *
20 * @var string
21 */
22 protected $path = '';
23
24 /**
25 * App root URL.
26 *
27 * @var string
28 */
29 protected $url = '';
30
31 /**
32 * Config.
33 *
34 * @var Config
35 */
36 protected $config = null;
37
38 /**
39 * Manifest.
40 *
41 * @var Manifest
42 */
43 protected $manifest = null;
44
45 /**
46 * Constructor.
47 *
48 * @param string $path
49 * @param string $url
50 * @param Config $config
51 * @param Manifest $manifest
52 */
53 public function __construct( $path, $url, Config $config, Manifest $manifest ) {
54 $this->path = MixedType::removeTrailingSlash( $path );
55 $this->url = Url::removeTrailingSlash( $url );
56 $this->config = $config;
57 $this->manifest = $manifest;
58 }
59
60 /**
61 * Remove the protocol from an http/https url.
62 *
63 * @param string $url
64 * @return string
65 */
66 protected function removeProtocol( $url ) {
67 return preg_replace( '~^https?:~i', '', $url );
68 }
69
70 /**
71 * Get if a url is external or not.
72 *
73 * @param string $url
74 * @param string $home_url
75 * @return boolean
76 */
77 protected function isExternalUrl( $url, $home_url ) {
78 $delimiter = '~';
79 $pattern_home_url = preg_quote( $home_url, $delimiter );
80 $pattern = $delimiter . '^' . $pattern_home_url . $delimiter . 'i';
81 return ! preg_match( $pattern, $url );
82 }
83
84 /**
85 * Generate a version for a given asset src.
86 *
87 * @param string $src
88 * @return integer|boolean
89 */
90 protected function generateFileVersion( $src ) {
91 // Normalize both URLs in order to avoid problems with http, https
92 // and protocol-less cases.
93 $src = $this->removeProtocol( $src );
94 $home_url = $this->removeProtocol( WP_CONTENT_URL );
95 $version = false;
96
97 if ( ! $this->isExternalUrl( $src, $home_url ) ) {
98 // Generate the absolute path to the file.
99 $file_path = MixedType::normalizePath(
100 str_replace(
101 [ $home_url, '/' ],
102 [ WP_CONTENT_DIR, DIRECTORY_SEPARATOR ],
103 $src
104 )
105 );
106
107 if ( file_exists( $file_path ) ) {
108 // Use the last modified time of the file as a version.
109 $version = filemtime( $file_path );
110 }
111 }
112
113 return $version;
114 }
115
116 /**
117 * Get the public URL to the app root.
118 *
119 * @return string
120 */
121 public function getUrl() {
122 return $this->url;
123 }
124
125 /**
126 * Get the public URL to a generated asset based on manifest.json.
127 *
128 * @param string $asset
129 *
130 * @return string
131 */
132 public function getAssetUrl( $asset ) {
133 // Path with unix-style slashes.
134 $path = $this->manifest->get( $asset, '' );
135
136 if ( ! $path ) {
137 return '';
138 }
139
140 $url = wp_parse_url( $path );
141
142 if ( isset( $url['scheme'] ) ) {
143 // Path is an absolute URL.
144 return $path;
145 }
146
147 // Path is relative.
148 return $this->getUrl() . '/dist/' . $path;
149 }
150
151 /**
152 * Get the public URL to a generated JS or CSS bundle.
153 * Handles SCRIPT_DEBUG and hot reloading.
154 *
155 * @param string $name Source basename (no extension).
156 * @param string $extension Source extension - '.js' or '.css'.
157 * @return string
158 */
159 public function getBundleUrl( $name, $extension ) {
160 $url_path = '.css' === $extension ? "styles/{$name}" : $name;
161 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
162 $file_path = implode(
163 DIRECTORY_SEPARATOR,
164 array_filter(
165 [
166 $this->path,
167 'dist',
168 '.css' === $extension ? 'styles' : '',
169 $name . $suffix . $extension,
170 ]
171 )
172 );
173
174 if ( file_exists( $file_path ) ) {
175 return "{$this->getUrl()}/dist/{$url_path}{$suffix}{$extension}";
176 }
177
178 $hot_url = wp_parse_url( $this->config->get( 'development.hotUrl', 'http://localhost/' ) );
179 $hot_port = $this->config->get( 'development.port', 3000 );
180
181 return "${hot_url['scheme']}://{$hot_url['host']}:{$hot_port}/{$url_path}{$extension}";
182 }
183
184 /**
185 * Enqueue a style, dynamically generating a version for it.
186 *
187 * @param string $handle
188 * @param string $src
189 * @param array<string> $dependencies
190 * @param string $media
191 * @return void
192 */
193 public function enqueueStyle( $handle, $src, $dependencies = [], $media = 'all' ) {
194 wp_enqueue_style( $handle, $src, $dependencies, $this->generateFileVersion( $src ), $media );
195 }
196
197 /**
198 * Enqueue a script, dynamically generating a version for it.
199 *
200 * @param string $handle
201 * @param string $src
202 * @param array<string> $dependencies
203 * @param boolean $in_footer
204 * @return void
205 */
206 public function enqueueScript( $handle, $src, $dependencies = [], $in_footer = false ) {
207 wp_enqueue_script( $handle, $src, $dependencies, $this->generateFileVersion( $src ), $in_footer );
208 }
209
210 /**
211 * Add favicon meta.
212 *
213 * @return void
214 */
215 public function addFavicon() {
216 if ( function_exists( 'has_site_icon' ) && has_site_icon() ) {
217 // allow users to override the favicon using the WordPress Customizer
218 return;
219 }
220
221 $favicon_url = apply_filters( 'surecart_app_core_favicon_url', $this->getAssetUrl( 'images/favicon.ico' ) );
222
223 echo '<link rel="shortcut icon" href="' . $favicon_url . '" />' . "\n";
224 }
225 }
226