PluginProbe
Depicter — Popup & Slider Builder / 1.2.0
Depicter — Popup & Slider Builder v1.2.0
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / modules / wpemerge-app-core / src / Assets / Assets.php

Assets.php in Depicter — Popup & Slider Builder 1.2.0, at modules/wpemerge-app-core/src/Assets/Assets.php

233 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package WPEmergeAppCore
4 * @author Atanas Angelov <hi@atanas.dev>
5 * @copyright 2017-2020 Atanas Angelov
6 * @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7 * @link https://wpemerge.com/
8 */
9
10 namespace WPEmergeAppCore\Assets;
11
12 use WPEmerge\Helpers\MixedType;
13 use WPEmerge\Helpers\Url;
14 use WPEmergeAppCore\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 * Filesystem.
47 *
48 * @var \WP_Filesystem_Base
49 */
50 protected $filesystem = null;
51
52 /**
53 * Constructor.
54 *
55 * @param string $path
56 * @param string $url
57 * @param Config $config
58 * @param Manifest $manifest
59 * @param \WP_Filesystem_Base $filesystem
60 */
61 public function __construct( $path, $url, Config $config, Manifest $manifest, \WP_Filesystem_Base $filesystem ) {
62 $this->path = MixedType::removeTrailingSlash( $path );
63 $this->url = Url::removeTrailingSlash( $url );
64 $this->config = $config;
65 $this->manifest = $manifest;
66 $this->filesystem = $filesystem;
67 }
68
69 /**
70 * Remove the protocol from an http/https url.
71 *
72 * @param string $url
73 * @return string
74 */
75 protected function removeProtocol( $url ) {
76 return preg_replace( '~^https?:~i', '', $url );
77 }
78
79 /**
80 * Get if a url is external or not.
81 *
82 * @param string $url
83 * @param string $home_url
84 * @return boolean
85 */
86 protected function isExternalUrl( $url, $home_url ) {
87 $delimiter = '~';
88 $pattern_home_url = preg_quote( $home_url, $delimiter );
89 $pattern = $delimiter . '^' . $pattern_home_url . $delimiter . 'i';
90 return ! preg_match( $pattern, $url );
91 }
92
93 /**
94 * Generate a version for a given asset src.
95 *
96 * @param string $src
97 * @return integer|boolean
98 */
99 protected function generateFileVersion( $src ) {
100 // Normalize both URLs in order to avoid problems with http, https
101 // and protocol-less cases.
102 $src = $this->removeProtocol( $src );
103 $home_url = $this->removeProtocol( WP_CONTENT_URL );
104 $version = false;
105
106 if ( ! $this->isExternalUrl( $src, $home_url ) ) {
107 // Generate the absolute path to the file.
108 $file_path = MixedType::normalizePath( str_replace(
109 [$home_url, '/'],
110 [WP_CONTENT_DIR, DIRECTORY_SEPARATOR],
111 $src
112 ) );
113
114 if ( $this->filesystem->exists( $file_path ) ) {
115 // Use the last modified time of the file as a version.
116 $version = $this->filesystem->mtime( $file_path );
117 }
118 }
119
120 return $version;
121 }
122
123 /**
124 * Get the public URL to the app root.
125 *
126 * @return string
127 */
128 public function getUrl() {
129 return $this->url;
130 }
131
132 /**
133 * Get the public URL to a generated asset based on manifest.json.
134 *
135 * @param string $asset
136 *
137 * @return string
138 */
139 public function getAssetUrl( $asset ) {
140 // Path with unix-style slashes.
141 $path = $this->manifest->get( $asset, '' );
142
143 if ( ! $path ) {
144 return '';
145 }
146
147 $url = wp_parse_url( $path );
148
149 if ( isset( $url['scheme'] ) ) {
150 // Path is an absolute URL.
151 return $path;
152 }
153
154 // Path is relative.
155 return $this->getUrl() . '/dist/' . $path;
156 }
157
158 /**
159 * Get the public URL to a generated JS or CSS bundle.
160 * Handles SCRIPT_DEBUG and hot reloading.
161 *
162 * @param string $name Source basename (no extension).
163 * @param string $extension Source extension - '.js' or '.css'.
164 * @return string
165 */
166 public function getBundleUrl( $name, $extension ) {
167 $url_path = '.css' === $extension ? "styles/{$name}" : $name;
168 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
169 $file_path = implode(
170 DIRECTORY_SEPARATOR,
171 array_filter(
172 [
173 $this->path,
174 'dist',
175 '.css' === $extension ? 'styles' : '',
176 $name . $suffix . $extension,
177 ]
178 )
179 );
180
181 if ( $this->filesystem->exists( $file_path ) ) {
182 return "{$this->getUrl()}/dist/{$url_path}{$suffix}{$extension}";
183 }
184
185 $hot_url = wp_parse_url( $this->config->get( 'development.hotUrl', 'http://localhost/' ) );
186 $hot_port = $this->config->get( 'development.port', 3000 );
187
188 return "${hot_url['scheme']}://{$hot_url['host']}:{$hot_port}/{$url_path}{$extension}";
189 }
190
191 /**
192 * Enqueue a style, dynamically generating a version for it.
193 *
194 * @param string $handle
195 * @param string $src
196 * @param array<string> $dependencies
197 * @param string $media
198 * @return void
199 */
200 public function enqueueStyle( $handle, $src, $dependencies = [], $media = 'all' ) {
201 wp_enqueue_style( $handle, $src, $dependencies, $this->generateFileVersion( $src ), $media );
202 }
203
204 /**
205 * Enqueue a script, dynamically generating a version for it.
206 *
207 * @param string $handle
208 * @param string $src
209 * @param array<string> $dependencies
210 * @param boolean $in_footer
211 * @return void
212 */
213 public function enqueueScript( $handle, $src, $dependencies = [], $in_footer = false ) {
214 wp_enqueue_script( $handle, $src, $dependencies, $this->generateFileVersion( $src ), $in_footer );
215 }
216
217 /**
218 * Add favicon meta.
219 *
220 * @return void
221 */
222 public function addFavicon() {
223 if ( function_exists( 'has_site_icon' ) && has_site_icon() ) {
224 // allow users to override the favicon using the WordPress Customizer
225 return;
226 }
227
228 $favicon_url = apply_filters( 'wpemerge_app_core_favicon_url', $this->getAssetUrl( 'images/favicon.ico' ) );
229
230 echo '<link rel="shortcut icon" href="' . $favicon_url . '" />' . "\n";
231 }
232 }
233