Documentation

Assets
in package
Uses UtilsProvider, Assets, Assets, Assets

Asset management for frontend scripts and styles.

Table of Contents

Constants

TCF_STUB_PATH  = '@iabtechlabtcf/stub/lib/stub.js'

Properties

$ASSETS_BUMP  : mixed
For future implementations and updates of this class you can differ from BUMP version.
$handleBanner  : mixed
The registered handle name for the enqueued banner.
$handleBlocker  : mixed
The registered handle name for the enqueued blocker.
$currentPostId  : array<string|int, int>
The current post ID captured by the body_class filter.

Methods

admin_enqueue_scripts()  : mixed
Enqueue scripts and styles for admin pages.
anonymous_localize_script()  : mixed
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.
createHashedAssets()  : mixed
See `DeliverAnonymousAsset`.
customize_controls_print_scripts()  : mixed
Enqueue scripts in customize (not preview!)
enableAdvancedEnqueue()  : mixed
Allows to enable advanced enqueue features for a given handle.
enableAsyncEnqueue()  : mixed
Enable `async` attribute for given handle(s) (only scripts are supported).
enableDeferredEnqueue()  : mixed
Enable `defer` attribute for given handle(s) (only scripts are supported, see https://stackoverflow.com/a/25890780).
enablePreloadEnqueue()  : mixed
Enable `<link rel="preload" />` HTML tag for given handle(s).
enablePriorityQueue()  : mixed
Enable scripts and styles to be appear at the top of `<head`.
enqueue_scripts_and_styles()  : mixed
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 src/public/lib, too.
enqueueAdminPage()  : mixed
Enqueue admin page (currently only the config).
enqueueBanner()  : mixed
Enqueue the banner.
enqueueBlocker()  : mixed
Enqueue the blocker.
enqueueComposerScript()  : string
Enqueue a composer package script from our multi-package repository.
enqueueComposerStyle()  : string
Enqueue a composer package style from our multi-package repository.
enqueueLibraryScript()  : string|bool
Wrapper for Assets#enqueueScript() method with $isLib = true.
enqueueLibraryStyle()  : string|bool
Wrapper for Assets#enqueueStyle() method with $isLib = true.
enqueueMobx()  : mixed
Enqueue mobx state management library.
enqueueReactJsxRuntime()  : mixed
When using WordPress < 6.6 we need to enqueue the react/jsx-runtime UMD bundle to make the `ReactJSXRuntime` external work.
enqueueScript()  : string|bool
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.
enqueueStyle()  : string|bool
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.
enqueueUtils()  : mixed
Enqueue utils and return an array of dependencies.
fluent_community_portal_head()  : mixed
Enqueue assets for Fluent Community. With this hook, we are in the `<head` section.
getAsciiUrl()  : string
Convert a complete URL to IDN url. This is necessery if you use a URIBuilder like lil-url in your frontend.
getCachebusterVersion()  : 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.
getChunkTranslationMap()  : mixed
Get a map of available translations for all available chunks.
getPluginConstantPrefix()  : string
Get the prefix of this plugin so composer packages can dynamically build other constant values on it.
getPluginsUrl()  : string
Wrapper for plugins_url. It respects the public folder depending on the SCRIPTS_DEBUG constant.
getPublicFolder()  : string
Gets a public folder depending on the debug mode relative to the plugins folder with trailing slash.
hints_dashboard_tile_predefined_links()  : mixed
Provide predefined links for `RCB/Hints` `dashboardTile`'s configuration.
isScreenBase()  : bool
Checks if a specific screen is active.
load_script_textdomain_relative_path()  : mixed
Force the basename for the md5 of a loaded JSON translation file.
load_script_translation_file()  : mixed
Fix correct filename for enqueued composer packages.
localizeFreemiumScript()  : mixed
C'tor.
localizeScript()  : mixed
Localize the WordPress backend and frontend.
login_enqueue_scripts()  : mixed
overrideLocalizeScript()  : array<string|int, mixed>
Localize the WordPress backend and frontend. If you want to provide URLs to the frontend you have to consider that some JS libraries do not support umlauts in their URI builder. For this you can use utils Assets#getAsciiUrl.
probablyDeregisterWhenNewerVersion()  : mixed
Probably deregister a given handle when our requested version is higher than the current enqueued version script / style.
probablyEnqueueCustomizeHelpers()  : mixed
Probably enqueue customize helpers in your frontend. There are two types of customize helpers:
real_queue_enqueue_scripts()  : mixed
setLazyScriptTranslations()  : mixed
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.
useNonMinifiedSources()  : bool
Check if SCRIPT_DEBUG is set to true.
wp()  : mixed
Get the current post ID through the wp filter. At this time, the wp filter is the only way to reliably detect the current post ID. Plugins like Visual Composer run into issues in `wp_enqueue_scripts` returning the wrong ID (in Visual Composer, the ID of Theme Builder > Headers is returned).
wp_enqueue_scripts()  : mixed
Enqueue scripts and styles for frontend pages.
enqueue()  : string|bool
Enqueue helper for entry points and libraries. See dependents for more documentation.
enqueueComposer()  : string|bool
Enqueue helper for monorepo packages. See dependents for more documentation.
getTranslationSuffixByBasename()  : mixed
Get the suffix for `chunks` localized variable including dependencies.
isAntiAdBlockActive()  : mixed
Check if the current banner is configured to provide an anti ad block system.
probablyEnqueueChunk()  : mixed
Checks if a `vendor-` file is created for a given script and enqueue it.
probablyEnqueueComposerChunk()  : mixed
Checks if a `vendor-` file is created for a given script in a composer package and enqueue it.

Constants

TCF_STUB_PATH

public mixed TCF_STUB_PATH = '@iabtechlabtcf/stub/lib/stub.js'

Properties

$ASSETS_BUMP

For future implementations and updates of this class you can differ from BUMP version.

public static mixed $ASSETS_BUMP = 4

Increment, if needed.

$handleBanner

The registered handle name for the enqueued banner.

public mixed $handleBanner = null

$handleBlocker

The registered handle name for the enqueued blocker.

public mixed $handleBlocker = null

$currentPostId

The current post ID captured by the body_class filter.

protected array<string|int, int> $currentPostId = []

Methods

admin_enqueue_scripts()

Enqueue scripts and styles for admin pages.

public admin_enqueue_scripts(string $hook_suffix) : mixed
Parameters
$hook_suffix : string

The current admin page

anonymous_localize_script()

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.

public anonymous_localize_script(string $handle, string $object_name, array<string|int, mixed> $l10n[, array<string|int, mixed> $settings = [] ]) : mixed

Use this instead of wp_localize_script.

Settings:

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      useCore                 Use `wp_localize_script` internally instead of custom localize script
string[]     lazyParse               A list of pathes of the array which should be lazy parsed. This could be useful to improve performance and parse as needed (e.g. huge arrays).
Parameters
$handle : string

Name of the script to attach data to.

$object_name : string

Name of the variable that will contain the data.

$l10n : array<string|int, mixed>

Array of data to localize.

$settings : array<string|int, mixed> = []
Tags
see
https://docs.wp-rocket.me/article/1349-delay-javascript-execution#technical
see
https://developer.wordpress.org/reference/functions/wp_localize_script/

createHashedAssets()

See `DeliverAnonymousAsset`.

public createHashedAssets() : mixed

customize_controls_print_scripts()

Enqueue scripts in customize (not preview!)

public customize_controls_print_scripts() : mixed

enableAdvancedEnqueue()

Allows to enable advanced enqueue features for a given handle.

public enableAdvancedEnqueue(string|array<string|int, string> $handles[, array<string|int, string> $features = null ][, string $type = 'script' ]) : mixed
Parameters
$handles : string|array<string|int, string>
$features : array<string|int, string> = null

See $ADVANCED_ENQUEUE_FEATURE_* constants or null for all features

$type : string = 'script'

Can be script or style

enableAsyncEnqueue()

Enable `async` attribute for given handle(s) (only scripts are supported).

public enableAsyncEnqueue(string|array<string|int, string> $handles) : mixed
Parameters
$handles : string|array<string|int, string>

enableDeferredEnqueue()

Enable `defer` attribute for given handle(s) (only scripts are supported, see https://stackoverflow.com/a/25890780).

public enableDeferredEnqueue(string|array<string|int, string> $handles) : mixed
Parameters
$handles : string|array<string|int, string>
Tags
see
https://stackoverflow.com/a/56128726/5506547

enablePreloadEnqueue()

Enable `<link rel="preload" />` HTML tag for given handle(s).

public enablePreloadEnqueue(string|array<string|int, string> $handles[, string $type = 'script' ]) : mixed
Parameters
$handles : string|array<string|int, string>
$type : string = 'script'

Can be script or style

Tags
see
https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content

enablePriorityQueue()

Enable scripts and styles to be appear at the top of `<head`.

public enablePriorityQueue(string|array<string|int, string> $handles[, string $type = 'script' ]) : mixed
Parameters
$handles : string|array<string|int, string>
$type : string = 'script'

Can be script or style

enqueue_scripts_and_styles()

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 src/public/lib, too.

public enqueue_scripts_and_styles(string $type[, string $hook_suffix = null ]) : mixed
Parameters
$type : string

The type (see utils Assets constants)

$hook_suffix : string = null

The current admin page

enqueueAdminPage()

Enqueue admin page (currently only the config).

public enqueueAdminPage(array<string|int, string> $scriptDeps) : mixed
Parameters
$scriptDeps : array<string|int, string>

enqueueBanner()

Enqueue the banner.

public enqueueBanner(array<string|int, string> $scriptDeps) : mixed
Parameters
$scriptDeps : array<string|int, string>

enqueueBlocker()

Enqueue the blocker.

public enqueueBlocker(array<string|int, string> $scriptDeps) : mixed
Parameters
$scriptDeps : array<string|int, string>

enqueueComposerScript()

Enqueue a composer package script from our multi-package repository.

public enqueueComposerScript(string $handle[, array<string|int, string> $deps = [] ][, string $src = 'index.js' ][, bool $in_footer = true ]) : string
Parameters
$handle : string

Name of the package.

$deps : array<string|int, string> = []

An array of registered scripts handles this script depends on.

$src : string = 'index.js'

The file to use in dist or dev folder.

$in_footer : bool = true

Whether to enqueue the script before instead of in the .

Return values
string

The used handle

enqueueComposerStyle()

Enqueue a composer package style from our multi-package repository.

public enqueueComposerStyle(string $handle[, array<string|int, string> $deps = [] ][, string $src = 'index.css' ][, string $media = 'all' ]) : string
Parameters
$handle : string

Name of the package.

$deps : array<string|int, string> = []

An array of registered scripts handles this script depends on.

$src : string = 'index.css'

The file to use in dist or dev folder.

$media : string = 'all'

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)'.

Return values
string

The used handle

enqueueLibraryScript()

Wrapper for Assets#enqueueScript() method with $isLib = true.

public enqueueLibraryScript(string $handle, mixed $src[, array<string|int, string> $deps = [] ][, bool $in_footer = false ]) : string|bool
Parameters
$handle : string
$src : mixed
$deps : array<string|int, string> = []
$in_footer : bool = false
Tags
see
self::enqueueScript()
Return values
string|bool

The used handle

enqueueLibraryStyle()

Wrapper for Assets#enqueueStyle() method with $isLib = true.

public enqueueLibraryStyle(string $handle, mixed $src[, array<string|int, string> $deps = [] ][, string $media = 'all' ]) : string|bool
Parameters
$handle : string
$src : mixed
$deps : array<string|int, string> = []
$media : string = 'all'
Tags
see
enqueueStyle()
Return values
string|bool

The used handle

enqueueMobx()

Enqueue mobx state management library.

public enqueueMobx() : mixed

enqueueScript()

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.

public enqueueScript(string $handle, mixed $src[, array<string|int, string> $deps = [] ][, bool $in_footer = true ][, bool $isLib = false ]) : string|bool

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
);
Parameters
$handle : string

Name of the script. Should be unique.

$src : mixed

The src relative to public/dist or public/dev folder (when $isLib is false)

$deps : array<string|int, string> = []

An array of registered script handles this script depends on.

$in_footer : bool = true

Whether to enqueue the script before instead of in the .

$isLib : bool = false

If true the public/lib/ folder is used.

Tags
see
https://developer.wordpress.org/reference/functions/wp_enqueue_script/

For parameters

Return values
string|bool

The used handle

enqueueStyle()

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.

public enqueueStyle(string $handle, mixed $src[, array<string|int, string> $deps = [] ][, string $media = 'all' ][, bool $isLib = false ]) : string|bool

It also allows $src to be like in enqueueScript()

Parameters
$handle : string

Name of the style. Should be unique.

$src : mixed

The src relative to public/dist or public/dev folder (when $isLib is false)

$deps : array<string|int, string> = []

An array of registered style handles this style depends on.

$media : string = 'all'

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)'.

$isLib : bool = false

If true the public/lib/ folder is used.

Tags
see
https://developer.wordpress.org/reference/functions/wp_enqueue_style/

For parameters

Return values
string|bool

The used handle

enqueueUtils()

Enqueue utils and return an array of dependencies.

public enqueueUtils() : mixed

fluent_community_portal_head()

Enqueue assets for Fluent Community. With this hook, we are in the `<head` section.

public fluent_community_portal_head() : mixed
Tags
see
https://fluentcommunity.co/

getAsciiUrl()

Convert a complete URL to IDN url. This is necessery if you use a URIBuilder like lil-url in your frontend.

public getAsciiUrl(string $url) : string
Parameters
$url : string

The url

Tags
see
https://www.php.net/manual/en/function.idn-to-ascii.php
codeCoverageIgnore

Completely relies on external library!

Return values
string

getCachebusterVersion()

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.

public getCachebusterVersion(string $src[, bool $isLib = false ][, string $default = null ]) : string
Parameters
$src : string

The src relative to public/ folder

$isLib : bool = false

If true the cachebuster-lib.php cachebuster is used

$default : string = null
Return values
string

_VERSION or cachebuster timestamp

getChunkTranslationMap()

Get a map of available translations for all available chunks.

public getChunkTranslationMap() : mixed

getPluginConstantPrefix()

Get the prefix of this plugin so composer packages can dynamically build other constant values on it.

public getPluginConstantPrefix() : string
Tags
codeCoverageIgnore

It only returns a string with the constant prefix

Return values
string

getPluginsUrl()

Wrapper for plugins_url. It respects the public folder depending on the SCRIPTS_DEBUG constant.

public getPluginsUrl(string $asset[, bool $isLib = false ]) : string
Parameters
$asset : string

The file name relative to the public folder path (dist or dev)

$isLib : bool = false

If true the public/lib/ folder is used.

Tags
see
getPublicFolder()
Return values
string

getPublicFolder()

Gets a public folder depending on the debug mode relative to the plugins folder with trailing slash.

public getPublicFolder([bool $isLib = false ]) : string
Parameters
$isLib : bool = false

If true the public/lib/ folder is returned.

Return values
string

Provide predefined links for `RCB/Hints` `dashboardTile`'s configuration.

public hints_dashboard_tile_predefined_links(array<string|int, mixed> $hints) : mixed
Parameters
$hints : array<string|int, mixed>

isScreenBase()

Checks if a specific screen is active.

public isScreenBase(string $base) : bool
Parameters
$base : string

The base

Return values
bool

load_script_translation_file()

Fix correct filename for enqueued composer packages.

public load_script_translation_file(string $file) : mixed
Parameters
$file : string
Tags
see
https://regex101.com/r/vAM0hO/1

localizeFreemiumScript()

C'tor.

public localizeFreemiumScript() : mixed

localizeScript()

Localize the WordPress backend and frontend.

public localizeScript(string $context) : mixed
Parameters
$context : string

login_enqueue_scripts()

public login_enqueue_scripts() : mixed

overrideLocalizeScript()

Localize the WordPress backend and frontend. If you want to provide URLs to the frontend you have to consider that some JS libraries do not support umlauts in their URI builder. For this you can use utils Assets#getAsciiUrl.

public overrideLocalizeScript(string $context) : array<string|int, mixed>

Also, if you want to use the options typed in your frontend you should adjust the following file too: src/public/ts/store/option.tsx

Parameters
$context : string
Return values
array<string|int, mixed>

probablyDeregisterWhenNewerVersion()

Probably deregister a given handle when our requested version is higher than the current enqueued version script / style.

public probablyDeregisterWhenNewerVersion(string $handle, string $newVersion[, string $type = 'script' ]) : mixed
Parameters
$handle : string
$newVersion : string
$type : string = 'script'

probablyEnqueueCustomizeHelpers()

Probably enqueue customize helpers in your frontend. There are two types of customize helpers:

public probablyEnqueueCustomizeHelpers([array<string|int, string> &$scriptDeps = null ][, bool $force = false ]) : mixed
  • Customize preview: Your website on right frame
  • Customize manager: Configure customize settings on the left sidebar
Parameters
$scriptDeps : array<string|int, string> = null

Append the customize handle to this array

$force : bool = false

real_queue_enqueue_scripts()

public real_queue_enqueue_scripts(mixed $handle) : mixed
Parameters
$handle : mixed

setLazyScriptTranslations()

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.

public setLazyScriptTranslations(string $handle, string $domain, string $path) : mixed

In factory i18n.tsx the window.wpi18nLazy is automatically detected and the plugin gets localized.

Parameters
$handle : string
$domain : string
$path : string
Tags
see
https://developer.wordpress.org/reference/classes/wp_scripts/print_translations/
see
https://developer.wordpress.org/reference/functions/wp_set_script_translations/
see
https://app.clickup.com/t/3mjh0e

useNonMinifiedSources()

Check if SCRIPT_DEBUG is set to true.

public useNonMinifiedSources() : bool
Return values
bool

wp()

Get the current post ID through the wp filter. At this time, the wp filter is the only way to reliably detect the current post ID. Plugins like Visual Composer run into issues in `wp_enqueue_scripts` returning the wrong ID (in Visual Composer, the ID of Theme Builder > Headers is returned).

public wp() : mixed

wp_enqueue_scripts()

Enqueue scripts and styles for frontend pages.

public wp_enqueue_scripts() : mixed

enqueue()

Enqueue helper for entry points and libraries. See dependents for more documentation.

protected enqueue(string $handle, mixed $src[, array<string|int, string> $deps = [] ][, bool $isLib = false ][, string $type = 'script' ][, bool $in_footer = true ][, string $media = 'all' ]) : string|bool
Parameters
$handle : string
$src : mixed
$deps : array<string|int, string> = []
$isLib : bool = false
$type : string = 'script'

Can be 'script' or 'style'

$in_footer : bool = true
$media : string = 'all'
Return values
string|bool

The used handle

enqueueComposer()

Enqueue helper for monorepo packages. See dependents for more documentation.

protected enqueueComposer(string $handle[, string $src = 'index.js' ][, array<string|int, string> $deps = [] ][, string $type = 'script' ][, bool $in_footer = true ][, string $media = 'all' ][, string $vendorHandle = null ]) : string|bool
Parameters
$handle : string
$src : string = 'index.js'
$deps : array<string|int, string> = []
$type : string = 'script'

Can be 'script' or 'style'

$in_footer : bool = true
$media : string = 'all'
$vendorHandle : string = null
Return values
string|bool

The used handle

getTranslationSuffixByBasename()

Get the suffix for `chunks` localized variable including dependencies.

protected getTranslationSuffixByBasename(string $basename) : mixed
Parameters
$basename : string

isAntiAdBlockActive()

Check if the current banner is configured to provide an anti ad block system.

protected isAntiAdBlockActive() : mixed

probablyEnqueueChunk()

Checks if a `vendor-` file is created for a given script and enqueue it.

protected probablyEnqueueChunk(string $handle, bool $isLib, string $src, array<string|int, string> &$deps, bool $in_footer, string $media) : mixed
Parameters
$handle : string
$isLib : bool
$src : string
$deps : array<string|int, string>
$in_footer : bool
$media : string

probablyEnqueueComposerChunk()

Checks if a `vendor-` file is created for a given script in a composer package and enqueue it.

protected probablyEnqueueComposerChunk(string $handle, string $src, array<string|int, string> &$deps, bool $in_footer, string $media) : mixed
Parameters
$handle : string
$src : string
$deps : array<string|int, string>
$in_footer : bool
$media : string

        
On this page

Search results