$ASSETS_BUMP
$ASSETS_BUMP :
For future implementations and updates of this class you can differ from BUMP version.
Increment, if needed.
Assets handling.
enqueue_scripts_and_styles(string $type,string $hook_suffix = null)
Enqueue scripts and styles depending on the type. This function is called from both admin_enqueue_scripts and wp_enqueue_scripts. You can check the type through the $type parameter. In this function you can include your external libraries from public/lib, too.
Note: The scripts are loaded only on backend (admin_enqueue_scripts
). If your plugin
is also loaded on frontend you need to make sure to enqueue via wp_enqueue_scripts
, too.
See also https://app.clickup.com/t/4rknyh for more information about this (commits).
string | $type | The type (see Assets constants) |
string | $hook_suffix | The current admin page |
enableAdvancedEnqueue(string|array<mixed,string> $handles,array<mixed,string> $features = null,string $type = 'script')
Allows to enable advanced enqueue features for a given handle.
string|array<mixed,string> | $handles | |
array<mixed,string> | $features | See $ADVANCED_ENQUEUEFEATURE* constants or |
string | $type | Can be |
enqueue(\MatthiasWeb\Utils\Assets $assets = null)
Enqueue scripts and styles for this library.
\MatthiasWeb\Utils\Assets | $assets |
enqueueScript(string $handle,mixed $src,array<mixed,string> $deps = array(),boolean $in_footer = true,boolean $isLib = false): string|boolean
Registers the script if $src provided (does NOT overwrite), and enqueues it. Use this wrapper method instead of wp_enqueue_script if you want to use the cachebuster for the given src. If the src is not found in the cachebuster (inc/base/others/cachebuster.php) it falls back to _VERSION.
You can also use something like this to determine SCRIPT_DEBUG files:
$this->enqueueLibraryScript(
Constants::ASSETS_HANDLE_REACT_DOM,
[[$useNonMinifiedSources, 'react-dom/umd/react-dom.development.js'], 'react-dom/umd/react-dom.production.min.js'],
Constants::ASSETS_HANDLE_REACT
);
string | $handle | Name of the script. Should be unique. |
mixed | $src | The src relative to public/dist or public/dev folder (when $isLib is false) |
array<mixed,string> | $deps | An array of registered script handles this script depends on. |
boolean | $in_footer | Whether to enqueue the script before |
instead of in the
.
If true the public/lib/ folder is used.
string|boolean —
The used handle
enqueueLibraryScript(string $handle,mixed $src,array<mixed,string> $deps = array(),boolean $in_footer = false): string|boolean
Wrapper for Assets#enqueueScript() method with $isLib = true.
string | $handle | |
mixed | $src | |
array<mixed,string> | $deps | |
boolean | $in_footer |
The used handle
enqueueStyle(string $handle,mixed $src,array<mixed,string> $deps = array(),string $media = 'all',boolean $isLib = false): string|boolean
Enqueue a CSS stylesheet. Use this wrapper method instead of wp_enqueue_style if you want to use the cachebuster for the given src. If the src is not found in the cachebuster (inc/base/others/cachebuster.php) it falls back to _VERSION.
It also allows $src to be like in enqueueScript()
string | $handle | Name of the style. Should be unique. |
mixed | $src | The src relative to public/dist or public/dev folder (when $isLib is false) |
array<mixed,string> | $deps | An array of registered style handles this style depends on. |
string | $media | The media for which this stylesheet has been defined. Accepts media types like 'all', 'print' and 'screen', or media queries like '(orientation: portrait)' and '(max-width: 640px)'. |
boolean | $isLib | If true the public/lib/ folder is used. |
The used handle
enqueueLibraryStyle(string $handle,mixed $src,array<mixed,string> $deps = array(),string $media = 'all'): string|boolean
Wrapper for Assets#enqueueStyle() method with $isLib = true.
string | $handle | |
mixed | $src | |
array<mixed,string> | $deps | |
string | $media |
The used handle
enqueueComposerScript(string $handle,array<mixed,string> $deps = array(),string $src = 'index.js',boolean $in_footer = true): string
Enqueue a composer package script from our multi-package repository.
string | $handle | Name of the package. |
array<mixed,string> | $deps | An array of registered scripts handles this script depends on. |
string | $src | The file to use in dist or dev folder. |
boolean | $in_footer | Whether to enqueue the script before instead of in the . |
The used handle
enqueueComposerStyle(string $handle,array<mixed,string> $deps = array(),string $src = 'index.css',string $media = 'all'): string
Enqueue a composer package style from our multi-package repository.
string | $handle | Name of the package. |
array<mixed,string> | $deps | An array of registered scripts handles this script depends on. |
string | $src | The file to use in dist or dev folder. |
string | $media | The media for which this stylesheet has been defined. Accepts media types like 'all', 'print' and 'screen', or media queries like '(orientation: portrait)' and '(max-width: 640px)'. |
The used handle
setLazyScriptTranslations(string $handle,string $domain,string $path)
The function and mechanism of wp_set_script_translations() is great of course. Unfortunately popular plugins like WP Rocket and Divi are not compatible with it (especially page builders and caching plugins). Why? Shortly explained, the injected inline scripts relies on `wp.i18n` which can be deferred or async loaded (the script itself) -> wp is not defined.
In factory i18n.tsx the window.wpi18nLazy
is automatically detected and the plugin gets localized.
string | $handle | |
string | $domain | |
string | $path |
getCachebusterVersion(string $src,boolean $isLib = false,string $default = null): string
Get the cachebuster entry for a given file. If the $src begins with public/lib/ it will use the inc/base/others/cachebuster-lib.php cachebuster instead of inc/base/others/cachebuster.php.
string | $src | The src relative to public/ folder |
boolean | $isLib | If true the cachebuster-lib.php cachebuster is used |
string | $default |
_VERSION or cachebuster timestamp
anonymous_localize_script(string $handle,string $object_name,array $l10n,array<mixed,string> $makeBase64Encoded = array(),boolean $use_core = false)
Make a localized array anonymous. Some plugins like WP Rocket tries to lazy load also localized scripts and this should be avoided in some scenarios like Real Cookie Banners' banner script.
Use this instead of wp_localize_script
.
string | $handle | Name of the script to attach data to. |
string | $object_name | Name of the variable that will contain the data. |
array | $l10n | Array of data to localize. |
array<mixed,string> | $makeBase64Encoded | List of keys of the array object which should be converted to base64 at output time (e.g. to avoid ModSecurity issues) |
boolean | $use_core | Use |
getPluginsUrl(string $asset,boolean $isLib = false): string
Wrapper for plugins_url. It respects the public folder depending on the SCRIPTS_DEBUG constant.
string | $asset | The file name relative to the public folder path (dist or dev) |
boolean | $isLib | If true the public/lib/ folder is used. |
probablyDeregisterWhenNewerVersion(string $handle,string $newVersion,string $type = 'script')
Probably deregister a given handle when our requested version is higher than the current enqueued version script / style.
string | $handle | |
string | $newVersion | |
string | $type |
probablyEnqueueChunk(string $handle,boolean $isLib,string $src,array<mixed,string> $deps,boolean $in_footer,string $media)
Checks if a `vendor-` file is created for a given script and enqueue it.
string | $handle | |
boolean | $isLib | |
string | $src | |
array<mixed,string> | $deps | |
boolean | $in_footer | |
string | $media |
probablyEnqueueComposerChunk(string $handle,string $src,array<mixed,string> $deps,boolean $in_footer,string $media)
Checks if a `vendor-` file is created for a given script in a composer package and enqueue it.
string | $handle | |
string | $src | |
array<mixed,string> | $deps | |
boolean | $in_footer | |
string | $media |
enqueueComposer(string $handle,string $src = 'index.js',array<mixed,string> $deps = array(),string $type = 'script',boolean $in_footer = true,string $media = 'all',string $vendorHandle = null): string|boolean
Enqueue helper for monorepo packages. See dependents for more documentation.
string | $handle | |
string | $src | |
array<mixed,string> | $deps | |
string | $type | Can be 'script' or 'style' |
boolean | $in_footer | |
string | $media | |
string | $vendorHandle |
The used handle