window.EasySvgPanel.api
The Icons panel contract level as a number in JavaScript, equal to EASY_SVG_PANEL_API, so an add-on can check which registry features exist.
JavaScript · Easy SVG · Status: Stable
window.EasySvgPanel.api tells your script which version of the panel contract the installed Easy SVG speaks. It is a plain number, the same as the PHP constant EASY_SVG_PANEL_API. Check it before you use a feature that a newer level added, and your add-on keeps working on sites that have not updated yet.
Usage
const level = Number( window.EasySvgPanel.api || 1 );
When to use it
- Before you call
registerCollections(), which exists from level 3. - Before you rely on
areaor a customiconfor a tab, which arrived with level 2. - In an add-on that supports several Easy SVG versions and should degrade instead of breaking.
When not to use it:
- To compare plugin versions. The level changes only when the panel contract changes shape; many releases share one level.
- In PHP. There you read
EASY_SVG_PANEL_APIdirectly, for example before you add theeasy_svg_panel_enqueueaction.
Parameters
None. It is a read-only number on the registry object.
Return value
The contract level, currently 3. What each level offers:
| Level | Adds |
|---|---|
| 1 | registerTab() with id, render, label, order; menu entries through easy_svg_panel_menu_tabs as plain labels |
| 2 | area (the five rail sections), icon as keyword or own drawing, and a capability per tab through the array form of easy_svg_panel_menu_tabs |
| 3 | registerCollections() for collections inside the Icon library tab |
An add-on written for level 1 or 2 keeps working on level 3.
Default
1 when the panel's localized data carries no panelApi. On panels that are older than this property, window.EasySvgPanel.api is undefined, which is why the examples fall back to 1.
Examples
Read the level
const panel = window.EasySvgPanel;
const level = panel ? Number( panel.api || 1 ) : 0;
if ( 0 === level ) {
// Not on the Icons screen, or the panel did not load.
}
Use a level 3 feature only where it exists
( function () {
const panel = window.EasySvgPanel;
if ( ! panel ) {
return;
}
if ( 3 <= Number( panel.api || 1 ) ) {
panel.registerCollections( {
id: 'myaddon',
load: () => [],
} );
}
} )();
Check the method, not just the number
For extra safety, combine the level with a type check. If a future level ever changed a method, the type check still keeps your script from throwing.
const panel = window.EasySvgPanel;
const canCollect =
!! panel &&
3 <= Number( panel.api || 1 ) &&
'function' === typeof panel.registerCollections;
Decide in PHP whether to load the script at all
The same number exists on the server, so you can skip the enqueue on panels that are too old.
add_action( 'plugins_loaded', 'myaddon_boot' );
function myaddon_boot() {
if ( defined( 'EASY_SVG_PANEL_API' ) && 3 <= (int) EASY_SVG_PANEL_API ) {
add_action( 'easy_svg_panel_enqueue', 'myaddon_enqueue_panel' );
}
}
How it works
- In PHP,
easy_svg_panel_assets()passes'panelApi' => (int) EASY_SVG_PANEL_APItowp_localize_script()as part ofwindow.EasySvgPanelData. The constant itself is used, so PHP and JavaScript cannot disagree. wp_localize_script()turns scalar values into strings, soEasySvgPanelData.panelApiarrives as"3".- When the panel bundle installs the registry, it computes
Number( EasySvgPanelData.panelApi || 1 )once and stores the result asapi.
The value is read once at install time. It does not change while the page is open.
Pitfalls
- Prefer
apioverEasySvgPanelData.panelApi. The localized value is a string, so'3' >= 3works by coercion but'3' === 3does not.apiis already a number. undefinedon very old panels. Always fall back, as inNumber( panel.api || 1 ).- Do not write to it. Changing the number does not add features; it only makes your own checks lie.
- The level is not the plugin version. Easy SVG 5.4.0 is level 3; do not derive one from the other.