Skip to content

Commit 2da2d28

Browse files
committed
Create VendorAsset class
1 parent 9e78471 commit 2da2d28

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

src/Assets/VendorAsset.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
declare( strict_types=1 );
4+
5+
namespace StellarWP\Assets;
6+
7+
use InvalidArgumentException;
8+
use LogicException;
9+
10+
class VendorAsset extends Asset {
11+
12+
/**
13+
* Whether this is a vendor asset.
14+
*
15+
* @var bool
16+
*/
17+
protected bool $is_vendor = true;
18+
19+
/**
20+
* VendorAsset constructor.
21+
*
22+
* @param string $slug The asset slug.
23+
* @param string $url The asset URL.
24+
* @param string $type The asset type.
25+
*
26+
* @throws InvalidArgumentException If the URL is not valid.
27+
*/
28+
public function __construct( string $slug, string $url, string $type = 'js' ) {
29+
$filtered = filter_var( $url, FILTER_VALIDATE_URL );
30+
if ( false === $filtered ) {
31+
throw new InvalidArgumentException( 'The URL must be a valid URL.' );
32+
}
33+
34+
$this->url = $filtered;
35+
$this->slug = sanitize_key( $slug );
36+
$this->type = strtolower( $type );
37+
}
38+
39+
/**
40+
* Set the asset version.
41+
*
42+
* @param string $version The asset version.
43+
*
44+
* @return $this
45+
*/
46+
public function set_version( string $version ): self {
47+
$this->version = $version;
48+
return $this;
49+
}
50+
51+
/**
52+
* Get the asset version.
53+
*
54+
* @since 1.0.0
55+
*
56+
* @return string The asset version.
57+
*/
58+
public function get_version(): string {
59+
return $this->version ?? '';
60+
}
61+
62+
/**
63+
* Get the asset url.
64+
*
65+
* If the version has been provided, then it will be used to format the URL.
66+
*
67+
* @since 1.0.0
68+
*
69+
* @param bool $use_min_if_available (Unused) Use the minified version of the asset if available.
70+
*
71+
* @return string
72+
* @throws LogicException If the URL has a placeholder but no version is provided.
73+
*/
74+
public function get_url( bool $use_min_if_available = true ): string {
75+
$has_version = null !== $this->version;
76+
if ( ! $has_version && $this->url_has_placeholder( $this->url ) ) {
77+
throw new LogicException( 'A URL with a placeholder must have a version provided.' );
78+
}
79+
80+
$url = $has_version
81+
? $this->get_formatted_url()
82+
: $this->url;
83+
84+
$hook_prefix = Config::get_hook_prefix();
85+
86+
/**
87+
* Filters the asset URL.
88+
*
89+
* @param string $url Asset URL.
90+
* @param string $slug Asset slug.
91+
* @param Asset $asset The Asset object.
92+
*/
93+
return (string) apply_filters( "stellarwp/assets/{$hook_prefix}/resource_url", $url, $this->slug, $this );
94+
}
95+
96+
/**
97+
* Get the minified version of the URL.
98+
*
99+
* @return string
100+
*/
101+
public function get_min_url(): string {
102+
return $this->get_url();
103+
}
104+
105+
/**
106+
* Get the formatted version of the URL.
107+
*
108+
* This will replace the version placeholder in the URL with the actual version. If there
109+
* is no placeholder, it will append the version as a query string.
110+
*
111+
* @return string
112+
*/
113+
protected function get_formatted_url() {
114+
return $this->url_has_placeholder( $this->url )
115+
? sprintf( $this->url, $this->version )
116+
: add_query_arg( 'ver', $this->version, $this->url );
117+
}
118+
119+
/**
120+
* Determine if the URL has a placeholder for the version.
121+
*
122+
* @param string $url The URL to check.
123+
*
124+
* @return bool True if the URL has a placeholder, false otherwise.
125+
*/
126+
protected function url_has_placeholder( string $url ): bool {
127+
return false !== strpos( $url, '%s' );
128+
}
129+
}

0 commit comments

Comments
 (0)