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 |