: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
/* global List, wpforms_form_templates, wpforms_addons, wpf */
* Form Templates function.
// eslint-disable-next-line no-var
var WPFormsFormTemplates = window.WPFormsFormTemplates || ( function( document, window, $ ) {
* Public functions and properties.
$( window ).on( 'load', function() {
// in case of jQuery 3.+ we need to wait for an `ready` event first.
if ( typeof $.ready.then === 'function' ) {
$.ready.then( app.load );
* Setup. Prepare some variables.
vars.templateList = new List( 'wpforms-setup-templates-list', {
attr: 'data-subcategories',
.on( 'click', '#wpforms-setup-templates-list .wpforms-template-favorite i', app.selectFavorite )
.on( 'click', '#wpforms-setup-templates-list .wpforms-template-remove i', app.removeTemplate );
* Select Favorite Templates.
// eslint-disable-next-line max-lines-per-function
const $heartIcon = $( this ),
favorite = $heartIcon.hasClass( 'fa-heart-o' ),
$favorite = $heartIcon.closest( '.wpforms-template-favorite' ),
$template = $heartIcon.closest( '.wpforms-template' ),
$templateName = $template.find( '.wpforms-template-name' ),
templateSlug = $template.find( '.wpforms-template-select' ).data( 'slug' ),
$favoritesCategory = $( '.wpforms-setup-templates-categories' ).find( '[data-category=\'favorites\']' ),
$favoritesCount = $favoritesCategory.find( 'span' ),
action: 'wpforms_templates_favorite',
nonce: wpforms_form_templates.nonce,
let favoritesCount = parseInt( $favoritesCount.html(), 10 );
const item = vars.templateList.get( 'slug', templateSlug )[ 0 ],
const toggleHeartIcon = function() {
$favorite.find( '.fa-heart-o' ).toggleClass( 'wpforms-hidden', values.favorite );
$favorite.find( '.fa-heart' ).toggleClass( 'wpforms-hidden', ! values.favorite );
const unMarkFavorite = function() {
favoritesCount = favoritesCount - 1;
$templateName.data( 'data-favorite', 0 );
$favoritesCount.html( favoritesCount );
app.maybeHideFavoritesCategory();
const markFavorite = function() {
favoritesCount = favoritesCount + 1;
$templateName.data( 'data-favorite', 1 );
$favoritesCount.html( favoritesCount );
app.maybeHideFavoritesCategory();
$.post( wpforms_form_templates.ajaxurl, data, function( res ) {
const $trashIcon = $( this ),
$template = $trashIcon.closest( '.wpforms-template-remove' ),
$templateId = $template.data( 'template' );
title: wpforms_form_templates.delete_template_title,
content: wpforms_form_templates.delete_template_content,
icon: 'fa fa-exclamation-circle',
text: wpforms_form_templates.delete_template,
app.removeUserTemplate( $templateId );
text: wpforms_form_templates.cancel,
* @param {number} templateId Template ID.
removeUserTemplate( templateId ) {
vars.templateList.remove( 'slug', 'wpforms-user-template-' + templateId );
$.post( wpforms_form_templates.ajaxurl, {
action: 'wpforms_user_template_remove',
nonce: wpforms_form_templates.nonce,
$( '#wpforms-template-wpforms-user-template-' + templateId ).remove();
app.updateCategoryCount( 'all' );
app.updateCategoryCount( 'user' );
* @param {string} category Category name.
updateCategoryCount( category ) {
const categoriesList = $( '.wpforms-setup-templates-categories' ),
$category = categoriesList.find( `[data-category='${ category }']` ),
$count = $category.find( 'span' ),
count = parseInt( $count.html(), 10 );
$count.html( count - 1 );
$category.data( 'count', count - 1 );
if ( count - 1 === 0 && category === 'user' && $category.hasClass( 'active' ) ) {
$( '.wpforms-user-templates-empty-state' ).removeClass( 'wpforms-hidden' );
* Maybe hide favorites category if there are no templates.
maybeHideFavoritesCategory() {
const $categoriesList = $( '.wpforms-setup-templates-categories' ),
$favoritesCategory = $categoriesList.find( '[data-category=\'favorites\']' ),
favoritesCount = parseInt( $favoritesCategory.find( 'span' ).html(), 10 );
$favoritesCategory.toggleClass( 'wpforms-hidden', ! favoritesCount );
if ( $favoritesCategory.hasClass( 'active' ) ) {
if ( ! favoritesCount ) {
$categoriesList.find( '[data-category=\'all\']' ).trigger( 'click' );
$favoritesCategory.trigger( 'click' );
* Search template callback.
app.performSearch( $( this ).val() );
* @param {string} query Value to search.
const searchResult = vars.templateList.search( query, [ 'name' ], function( searchString ) {
for ( let index = 0, length = vars.templateList.items.length; index < length; index++ ) {
const values = vars.templateList.items[ index ].values();
const templateName = values[ 'wpforms-template-name' ].toLowerCase();
const templateDesc = values[ 'wpforms-template-desc' ].toLowerCase();
const fields = values.fields.toLowerCase();
const searchRegex = new RegExp( searchString );
vars.templateList.items[ index ].found = searchRegex.test( templateName ) || searchRegex.test( templateDesc ) || searchRegex.test( fields );
$( '.wpforms-templates-no-results' ).toggle( ! searchResult.length );
* @param {Object} e Event object.
const $activeCategory = $item.parent( 'ul' ).parent( 'li' ).parent( 'ul' ).children( 'li.active' );
const $activeSubcategory = $( '.wpforms-setup-templates-subcategories li.active' );
const subcategory = $item.data( 'subcategory' );
const category = $item.parents( 'li' ).data( 'category' );
const searchQuery = $( '#wpforms-setup-template-search' ).val();
// Clear active class from the parent category and current subcategory.
$activeSubcategory.removeClass( 'active' );
$activeCategory.removeClass( 'active' );
// Add active class to the parent category and current subcategory.
$item.parents( 'li' ).addClass( 'active' );
$item.addClass( 'active' );
vars.templateList.filter( function( item ) {
return category === 'all' || ( item.values().categories.split( ',' ).indexOf( category ) > -1 && item.values().subcategories.split( ',' ).indexOf( subcategory ) > -1 );
if ( searchQuery !== '' ) {
app.performSearch( searchQuery );
* @param {Object} e Event object.
const $item = $( this ).parent(),
$active = $item.closest( 'ul' ).find( '.active' ),
category = $item.data( 'category' ),
count = $item.data( 'count' ),
searchQuery = $( '#wpforms-setup-template-search' ).val();
$active.removeClass( 'active' );
$item.addClass( 'active opened' );
vars.templateList.filter( function( item ) {
if ( category === 'available' ) {
return item.values()[ 'has-access' ];
if ( category === 'favorites' ) {
return item.values().favorite;
return category === 'all' || item.values().categories.split( ',' ).indexOf( category ) > -1;
// Display/hide User Templates empty state message.
$( '.wpforms-user-templates-empty-state' ).toggleClass( 'wpforms-hidden', category !== 'user' || count !== 0 );
if ( searchQuery !== '' ) {
app.performSearch( searchQuery );
* Show/hide the subcategories list by clicking on chevron icon.
* @param {Object} e Event object.
toggleSubcategoriesList( e ) {
const $item = $( this ).parent().parent();
$item.toggleClass( 'opened' );
* Cancel button click routine.
selectTemplateCancel( ) {
const $template = $( '#wpforms-setup-templates-list' ).find( '.wpforms-template.active' ),
$button = $template.find( '.wpforms-template-select' );
$template.removeClass( 'active' );
$button.html( $button.data( 'labelOriginal' ) );
* Show upgrade banner if licence type is less than Pro.
if ( ! $( '#tmpl-wpforms-templates-upgrade-banner' ).length ) {
const template = wp.template( 'wpforms-templates-upgrade-banner' );
const $templates = $( '#wpforms-setup-templates-list .wpforms-template' );
if ( $templates.length > 5 ) {
$templates.eq( 5 ).after( template() );
$templates.last().after( template() );
* @param {string} formName Name of the form.
* @param {string} template Template slug.
* @param {jQuery} $button Use template button object.
* @param {Function} callback The function to set the template.
selectTemplateProcess( formName, template, $button, callback ) {
if ( $button.data( 'addons' ) ) {
app.addonsModal( formName, template, $button, callback );
callback( formName, template );
* Open required addons alert.
* @param {string} formName Name of the form.
* @param {string} template Template slug.
* @param {jQuery} $button Use template button object.
* @param {Function} callback The function to set the template.
addonsModal( formName, template, $button, callback ) {
const templateName = $button.data( 'template-name-raw' );
const addonsNames = $button.data( 'addons-names' );
const addonsSlugs = $button.data( 'addons' );
const addons = addonsSlugs.split( ',' );
let prompt = addons.length > 1 ? wpforms_form_templates.template_addons_prompt : wpforms_form_templates.template_addon_prompt;
prompt = prompt.replace( /%template%/g, templateName ).replace( /%addons%/g, addonsNames );