: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
// eslint-disable-next-line no-unused-vars
const WPFormsUtils = window.WPFormsUtils || ( function( document, window, $ ) {
* Public functions and properties.
* Wrapper to trigger a native or custom event and return the event object.
* @param {jQuery} $element Element to trigger event on.
* @param {string} eventName Event name to trigger (custom or native).
* @param {Array} args Trigger arguments.
* @returns {Event} Event object.
triggerEvent: function( $element, eventName, args = [] ) {
let eventObject = new $.Event( eventName );
$element.trigger( eventObject, args );
* This function comes directly from underscore.js:
* Returns a function, that, as long as it continues to be invoked, will not
* be triggered. The function will be called after it stops being called for
* N milliseconds. If `immediate` is passed, trigger the function on the
* leading edge, instead of the trailing.
* Debouncing is removing unwanted input noise from buttons, switches or other user input.
* Debouncing prevents extra activations or slow functions from triggering too often.
* @param {Function} func The function to be debounced.
* @param {int} wait The amount of time to delay calling func.
* @param {bool} immediate Whether or not to trigger the function on the leading edge.
* @returns {Function} Returns a function that, as long as it continues to be invoked, will not be triggered.
debounce: function( func, wait, immediate ) {
func.apply( context, args );
var callNow = immediate && ! timeout;
timeout = setTimeout( later, wait );
func.apply( context, args );
* Checks if the provided color has transparency.
* @param {string} color The color to check.
* @param {number} opacityThreshold The max opacity value of the color that is considered as transparent.
* @return {boolean} Returns true if the color is transparent.
isTransparentColor( color, opacityThreshold = 0.33 ) {
const rgba = app.cssColorsUtils.getColorAsRGBArray( color );
const opacity = Number( rgba?.[ 3 ] );
// Compare the opacity value with the threshold.
return opacity <= opacityThreshold;
* Get color as an array of RGB(A) values.
* @param {string} color Color.
* @return {Array|boolean} Color as an array of RGBA values. False on error.
getColorAsRGBArray( color ) {
// Check if the given color is a valid CSS color.
if ( ! app.cssColorsUtils.isValidColor( color ) ) {
// Remove # from the beginning of the string and remove whitespaces.
color = color.replace( /^#/, '' ).replaceAll( ' ', '' );
color = color === 'transparent' ? 'rgba(0,0,0,0)' : color;
// Check if color is in HEX(A) format.
const isHex = rgba.match( /[0-9a-f]{6,8}$/ig );
// Search and split HEX(A) color into an array of couples of chars.
rgbArray = rgba.match( /\w\w/g ).map( ( x ) => parseInt( x, 16 ) );
rgbArray[ 3 ] = rgbArray[ 3 ] || rgbArray[ 3 ] === 0 ? ( rgbArray[ 3 ] / 255 ).toFixed( 2 ) : 1;
rgbArray = rgba.split( '(' )[ 1 ].split( ')' )[ 0 ].split( ',' );
* Check if the given color is a valid CSS color.
* @param {string} color Color.
* @return {boolean} True if the given color is a valid CSS color.
// Create a temporary DOM element and use `style` property.
const s = new Option().style;
// Invalid color leads to the empty color property of DOM element style.
* Get contrast color relative to given color.
* @param {string} color Color.
* @return {string} True if the given color is a valid CSS color.
getContrastColor( color ) {
const rgba = app.cssColorsUtils.getColorAsRGBArray( color );
const sum = rgba.reduce( ( a, b ) => a + b, 0 );
const avg = Math.round( ( sum / 3 ) * ( rgba[ 3 ] ?? 1 ) );
return avg < 128 ? '#ffffff' : '#000000';
* Add opacity to color string.
* Supports formats: RGB, RGBA, HEX, HEXA.
* If the given color has an alpha channel, the new alpha channel will be calculated according to the given opacity.
* @param {string} color Color.
* @param {string} opacity Opacity.
* @return {string} Color in RGBA format with an added alpha channel according to given opacity.
getColorWithOpacity( color, opacity ) {
const rgbArray = app.cssColorsUtils.getColorAsRGBArray( color );
opacity = ! opacity || opacity.length === 0 ? '1' : opacity.toString();
const alpha = rgbArray.length === 4 ? parseFloat( rgbArray[ 3 ] ) : 1;
// Calculate new alpha value.
const newAlpha = parseFloat( opacity ) * alpha;
// Combine and return the RGBA color.
return `rgba(${ rgbArray[ 0 ] },${ rgbArray[ 1 ] },${ rgbArray[ 2 ] },${ newAlpha })`.replace( /\s+/g, '' );
// Provide access to public functions/properties.
}( document, window, jQuery ) );