2.5
New Features
New API on Asset: Attributes
The public API of Inpsyde\Assets\Asset
introduces 2 new methods Asset::attributes(): array
and Asset::withAttributes(array $attributes): Asset
and a new OutputFilter Inpsyde\Assets\OutputFilter\AttributesOutputFilter
.
This allows us more flexible to update the attributes of script
- and link
-tags. You can now do following:
<?php
use Inpsyde\Assets\Style;
use Inpsyde\Assets\Script;
$script = new Script('my-handle', 'script.js');
$script->withAttributes(
[
'async' => true,
'data-value' => 'key',
'nonce' => wp_create_nonce()
]
);
// <script src="script.js" id="my-handle-js" async data-value="key" nonce="{generated nonce}"></script>
$style = new Style('my-handle', 'style.css');
$style->withAttributes(
[
'data-value' => 'key',
'nonce' => wp_create_nonce()
]
);
// <link rel="stylesheet" href="style.css" id="my-handle-css" data-value="key" nonce="{generated nonce}" />
[!] Note: Existing attributes like "src" or "id" are not overwriteable. The Inpsyde\Assets\OutputFilter\AttributesOutputFilter
only sets attributes which are not already existent on the html-tag.
Following will not work:
<?php
use Inpsyde\Assets\Script;
$script = new Script('my-handle', 'script.js');
$script->withAttributes(['src' => 'another-script.js']); // Will not overwrite "script.js"
Deprecations
AsyncScriptOutputFilter
and DeferScriptOutputFilter
and the corrosponding methods Script::useAsyncFilter()
and Script:.useDeferFilter()
are set to deprecated. Everything will still work as normal, but the Script
-methods will now map internally to the new AttributsOutputFilter
.
In future, you should replace them as following:
Async:
<?php
// before
$script = new Script('my-handle', 'script.js');
$script->useAsyncFilter();
// after
$script = new Script('my-handle', 'script.js');
$script->withAttributes(['async' => true']);
Defer:
<?php
// before
$script = new Script('my-handle', 'script.js');
$script->useDeferFilter();
// after
$script = new Script('my-handle', 'script.js');
$script->withAttributes(['defer' => true']);
Internal refactor BaseAsset
The Inpsyde\Assets\BaseAsset
will now not use anymore internally $config
and does not support injecting $config
as fourth param in constructor. This decision was made while refactoring the usage of internal variables and to have a more consitent API. The Inpsyde\Assets\AssetFactory
was updated to now use the public API methods instead of just injecting the third parameter.
You only need to adapt your code if you're using following:
<?php
use Inpsyde\Assets\Asset;
use Inpsyde\Assets\Script;
// before - does not work anymore
$script = new Script('my-handle', 'script.js', Asset::FRONTEND, ['enqueue' => false]);
// after
$script = new Script('my-handle', 'script.js', Asset::FRONTEND);
$script->canEnqueue(false);
Improvements
Inpsyde\Assets\symlinkedAssetFolder()
- make use ofwp_mkdir_p()
insteadmkdir()
.- Util // move
AssetHookResolver
andAssetPathResolver
into new domain.
Quality
- testing.yml // test setup php with coverage:pcov
- Docs // Assets - update conditional comments.
- OutputFilter // update docs to have correct order.
- Upgrade to PHPUnit 8.
- Update documentation for Customizer Preview hook.