brain-wp / assets Goto Github PK
View Code? Open in Web Editor NEWWordPress assets helpers
License: MIT License
WordPress assets helpers
License: MIT License
Nice code - not joking when I say it reduced my asset enqeueuing code by >50% and made it far more readable :D
For completeness though, it'd be nice to have a simple method for dequeuing an arbitrary asset. As an example, my theme spits out its own styles for a particular plugin, making the plugin styles useless. It'd be great to be able to dequeue the plugin styles through the class (for literally no other reason than pretty code)
$assets = Assets::forTheme();
$manifest = $assets
->enqueueScript('manifest', ['jquery'])
->useDefer()
$vendor = $assets
->enqueueScript('vendor', [$manifest])
->useDefer();
$app = $assets
->enqueueScript('vendor', [$vendor])
->useDefer();
// ew...
wp_dequeue_script('some-plugin-script');
// wow! :D
$assets->dequeue('some-plugin-script')
Hello @gmazzap !
Very nice code. Psalm helps you keep code quality high.
Some things to consider
$baseUrl = (string)$context->baseUrl();
All the best to you!
diff --git a/composer.json b/composer.json
index e8db817..a5f08e2 100644
--- a/composer.json
+++ b/composer.json
@@ -29,7 +29,8 @@
"brain/monkey": "^2",
"squizlabs/php_codesniffer": "3.2.*",
"inpsyde/php-coding-standards": "0.*",
- "vimeo/psalm": "^3"
+ "phpstan/phpstan-shim": "^0.11.8",
+ "szepeviktor/phpstan-wordpress": "^0.1.2"
},
"scripts": {
"qa": [
diff --git a/src/Assets.php b/src/Assets.php
index d216df9..f8c8d88 100644
--- a/src/Assets.php
+++ b/src/Assets.php
@@ -810,7 +810,9 @@ class Assets
*/
private function tryRelativeUrl(string $url, string $folder, ?string $baseUrl = null): string
{
+ /** @var string[] */
$urlData = parse_url($url);
+ /** @var string[] */
$baseData = parse_url($baseUrl ?? $this->context->baseUrl());
$urlHost = (string)($urlData['host'] ?? '');
diff --git a/src/UrlResolver/DirectUrlResolver.php b/src/UrlResolver/DirectUrlResolver.php
index 9275d2f..883d217 100644
--- a/src/UrlResolver/DirectUrlResolver.php
+++ b/src/UrlResolver/DirectUrlResolver.php
@@ -29,6 +29,7 @@ final class DirectUrlResolver implements UrlResolver
$baseUrl = $this->context->baseUrl();
$basePath = $this->context->basePath();
+ /** @var string[] */
$urlData = parse_url($relative);
$path = trim((string)($urlData['path'] ?? ''), '/');
$query = (string)($urlData['query'] ?? '');
diff --git a/src/Version/LastModifiedVersion.php b/src/Version/LastModifiedVersion.php
index 564fe9b..b2e1e43 100644
--- a/src/Version/LastModifiedVersion.php
+++ b/src/Version/LastModifiedVersion.php
@@ -7,7 +7,7 @@ use Brain\Assets\Context\Context;
final class LastModifiedVersion implements Version
{
/**
- * @var array<string, array{0:string, 1:int}>
+ * @var array<string, array>
*/
private $bases = [];
phpstan.neon.dist
# Start command: vendor/bin/phpstan analyze
includes:
- phar://phpstan.phar/conf/bleedingEdge.neon
- vendor/szepeviktor/phpstan-wordpress/extension.neon
parameters:
level: max
paths:
- %currentWorkingDirectory%/src/
ignoreErrors:
# Uses func_get_args()
- '#^Function add_query_arg invoked with [123] parameters?, 0 required\.$#'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.