(function($, undefined){ /** * acf * * description * * @date 14/12/17 * @since 5.6.5 * * @param type $var Description. Default. * @return type Description. */ // The global acf object var acf = {}; // Set as a browser global window.acf = acf; /** @var object Data sent from PHP */ acf.data = {}; /** * get * * Gets a specific data value * * @date 14/12/17 * @since 5.6.5 * * @param string name * @return mixed */ acf.get = function( name ){ return this.data[name] || null; }; /** * has * * Returns `true` if the data exists and is not null * * @date 14/12/17 * @since 5.6.5 * * @param string name * @return boolean */ acf.has = function( name ){ return this.get(name) !== null; }; /** * set * * Sets a specific data value * * @date 14/12/17 * @since 5.6.5 * * @param string name * @param mixed value * @return this */ acf.set = function( name, value ){ this.data[ name ] = value; return this; }; /** * uniqueId * * Returns a unique ID * * @date 9/11/17 * @since 5.6.3 * * @param string prefix Optional prefix. * @return string */ var idCounter = 0; acf.uniqueId = function(prefix){ var id = ++idCounter + ''; return prefix ? prefix + id : id; }; /** * acf.uniqueArray * * Returns a new array with only unique values * Credit: https://stackoverflow.com/questions/1960473/get-all-unique-values-in-an-array-remove-duplicates * * @date 23/3/18 * @since 5.6.9 * * @param type $var Description. Default. * @return type Description. */ acf.uniqueArray = function( array ){ function onlyUnique(value, index, self) { return self.indexOf(value) === index; } return array.filter( onlyUnique ); }; /** * uniqid * * Returns a unique ID (PHP version) * * @date 9/11/17 * @since 5.6.3 * @source http://locutus.io/php/misc/uniqid/ * * @param string prefix Optional prefix. * @return string */ var uniqidSeed = ''; acf.uniqid = function(prefix, moreEntropy){ // discuss at: http://locutus.io/php/uniqid/ // original by: Kevin van Zonneveld (http://kvz.io) // revised by: Kankrelune (http://www.webfaktory.info/) // note 1: Uses an internal counter (in locutus global) to avoid collision // example 1: var $id = uniqid() // example 1: var $result = $id.length === 13 // returns 1: true // example 2: var $id = uniqid('foo') // example 2: var $result = $id.length === (13 + 'foo'.length) // returns 2: true // example 3: var $id = uniqid('bar', true) // example 3: var $result = $id.length === (23 + 'bar'.length) // returns 3: true if (typeof prefix === 'undefined') { prefix = ''; } var retId; var formatSeed = function(seed, reqWidth) { seed = parseInt(seed, 10).toString(16); // to hex str if (reqWidth < seed.length) { // so long we split return seed.slice(seed.length - reqWidth); } if (reqWidth > seed.length) { // so short we pad return Array(1 + (reqWidth - seed.length)).join('0') + seed; } return seed; }; if (!uniqidSeed) { // init seed with big random int uniqidSeed = Math.floor(Math.random() * 0x75bcd15); } uniqidSeed++; retId = prefix; // start with prefix, add current milliseconds hex string retId += formatSeed(parseInt(new Date().getTime() / 1000, 10), 8); retId += formatSeed(uniqidSeed, 5); // add seed hex string if (moreEntropy) { // for more entropy we add a float lower to 10 retId += (Math.random() * 10).toFixed(8).toString(); } return retId; }; /** * strReplace * * Performs a string replace * * @date 14/12/17 * @since 5.6.5 * * @param string search * @param string replace * @param string subject * @return string */ acf.strReplace = function( search, replace, subject ){ return subject.split(search).join(replace); }; /** * strCamelCase * * Converts a string into camelCase * Thanks to https://stackoverflow.com/questions/2970525/converting-any-string-into-camel-case * * @date 14/12/17 * @since 5.6.5 * * @param string str * @return string */ acf.strCamelCase = function( str ){ var matches = str.match( /([a-zA-Z0-9]+)/g ); return matches ? matches.map(function( s, i ){ var c = s.charAt(0); return ( i === 0 ? c.toLowerCase() : c.toUpperCase() ) + s.slice(1); }).join('') : ''; }; /** * strPascalCase * * Converts a string into PascalCase * Thanks to https://stackoverflow.com/questions/1026069/how-do-i-make-the-first-letter-of-a-string-uppercase-in-javascript * * @date 14/12/17 * @since 5.6.5 * * @param string str * @return string */ acf.strPascalCase = function( str ){ var camel = acf.strCamelCase( str ); return camel.charAt(0).toUpperCase() + camel.slice(1); }; /** * acf.strSlugify * * Converts a string into a HTML class friendly slug * * @date 21/3/18 * @since 5.6.9 * * @param string str * @return string */ acf.strSlugify = function( str ){ return acf.strReplace( '_', '-', str.toLowerCase() ); }; acf.strSanitize = function( str ){ // chars (https://jsperf.com/replace-foreign-characters) var map = { "À": "A", "Á": "A", "Â": "A", "Ã": "A", "Ä": "A", "Å": "A", "Æ": "AE", "Ç": "C", "È": "E", "É": "E", "Ê": "E", "Ë": "E", "Ì": "I", "Í": "I", "Î": "I", "Ï": "I", "Ð": "D", "Ñ": "N", "Ò": "O", "Ó": "O", "Ô": "O", "Õ": "O", "Ö": "O", "Ø": "O", "Ù": "U", "Ú": "U", "Û": "U", "Ü": "U", "Ý": "Y", "ß": "s", "à": "a", "á": "a", "â": "a", "ã": "a", "ä": "a", "å": "a", "æ": "ae", "ç": "c", "è": "e", "é": "e", "ê": "e", "ë": "e", "ì": "i", "í": "i", "î": "i", "ï": "i", "ñ": "n", "ò": "o", "ó": "o", "ô": "o", "õ": "o", "ö": "o", "ø": "o", "ù": "u", "ú": "u", "û": "u", "ü": "u", "ý": "y", "ÿ": "y", "Ā": "A", "ā": "a", "Ă": "A", "ă": "a", "Ą": "A", "ą": "a", "Ć": "C", "ć": "c", "Ĉ": "C", "ĉ": "c", "Ċ": "C", "ċ": "c", "Č": "C", "č": "c", "Ď": "D", "ď": "d", "Đ": "D", "đ": "d", "Ē": "E", "ē": "e", "Ĕ": "E", "ĕ": "e", "Ė": "E", "ė": "e", "Ę": "E", "ę": "e", "Ě": "E", "ě": "e", "Ĝ": "G", "ĝ": "g", "Ğ": "G", "ğ": "g", "Ġ": "G", "ġ": "g", "Ģ": "G", "ģ": "g", "Ĥ": "H", "ĥ": "h", "Ħ": "H", "ħ": "h", "Ĩ": "I", "ĩ": "i", "Ī": "I", "ī": "i", "Ĭ": "I", "ĭ": "i", "Į": "I", "į": "i", "İ": "I", "ı": "i", "IJ": "IJ", "ij": "ij", "Ĵ": "J", "ĵ": "j", "Ķ": "K", "ķ": "k", "Ĺ": "L", "ĺ": "l", "Ļ": "L", "ļ": "l", "Ľ": "L", "ľ": "l", "Ŀ": "L", "ŀ": "l", "Ł": "l", "ł": "l", "Ń": "N", "ń": "n", "Ņ": "N", "ņ": "n", "Ň": "N", "ň": "n", "ʼn": "n", "Ō": "O", "ō": "o", "Ŏ": "O", "ŏ": "o", "Ő": "O", "ő": "o", "Œ": "OE", "œ": "oe", "Ŕ": "R", "ŕ": "r", "Ŗ": "R", "ŗ": "r", "Ř": "R", "ř": "r", "Ś": "S", "ś": "s", "Ŝ": "S", "ŝ": "s", "Ş": "S", "ş": "s", "Š": "S", "š": "s", "Ţ": "T", "ţ": "t", "Ť": "T", "ť": "t", "Ŧ": "T", "ŧ": "t", "Ũ": "U", "ũ": "u", "Ū": "U", "ū": "u", "Ŭ": "U", "ŭ": "u", "Ů": "U", "ů": "u", "Ű": "U", "ű": "u", "Ų": "U", "ų": "u", "Ŵ": "W", "ŵ": "w", "Ŷ": "Y", "ŷ": "y", "Ÿ": "Y", "Ź": "Z", "ź": "z", "Ż": "Z", "ż": "z", "Ž": "Z", "ž": "z", "ſ": "s", "ƒ": "f", "Ơ": "O", "ơ": "o", "Ư": "U", "ư": "u", "Ǎ": "A", "ǎ": "a", "Ǐ": "I", "ǐ": "i", "Ǒ": "O", "ǒ": "o", "Ǔ": "U", "ǔ": "u", "Ǖ": "U", "ǖ": "u", "Ǘ": "U", "ǘ": "u", "Ǚ": "U", "ǚ": "u", "Ǜ": "U", "ǜ": "u", "Ǻ": "A", "ǻ": "a", "Ǽ": "AE", "ǽ": "ae", "Ǿ": "O", "ǿ": "o", // extra ' ': '_', "'": '', '?': '', '/': '', '\\': '', '.': '', ',': '', '`': '', '>': '', '<': '', '"': '', '[': '', ']': '', '|': '', '{': '', '}': '', '(': '', ')': '' }; // vars var nonWord = /\W/g; var mapping = function (c) { return (map[c] !== undefined) ? map[c] : c; }; // replace str = str.replace(nonWord, mapping); // lowercase str = str.toLowerCase(); // return return str; }; /** * acf.strMatch * * Returns the number of characters that match between two strings * * @date 1/2/18 * @since 5.6.5 * * @param type $var Description. Default. * @return type Description. */ acf.strMatch = function( s1, s2 ){ // vars var val = 0; var min = Math.min( s1.length, s2.length ); // loop for( var i = 0; i < min; i++ ) { if( s1[i] !== s2[i] ) { break; } val++; } // return return val; }; /** * Escapes HTML entities from a string. * * @date 08/06/2020 * @since 5.9.0 * * @param string string The input string. * @return string */ acf.strEscape = function( string ){ var htmlEscapes = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }; return ('' + string).replace(/[&<>"']/g, function( chr ) { return htmlEscapes[ chr ]; }); }; // Tests. //console.log( acf.strEscape('Test 1') ); //console.log( acf.strEscape('Test & 1') ); //console.log( acf.strEscape('Test\'s & 1') ); //console.log( acf.strEscape('') ); /** * Unescapes HTML entities from a string. * * @date 08/06/2020 * @since 5.9.0 * * @param string string The input string. * @return string */ acf.strUnescape = function( string ){ var htmlUnescapes = { '&': '&', '<': '<', '>': '>', '"': '"', ''': "'" }; return ('' + string).replace(/&|<|>|"|'/g, function( entity ) { return htmlUnescapes[ entity ]; }); }; // Tests. //console.log( acf.strUnescape( acf.strEscape('Test 1') ) ); //console.log( acf.strUnescape( acf.strEscape('Test & 1') ) ); //console.log( acf.strUnescape( acf.strEscape('Test\'s & 1') ) ); //console.log( acf.strUnescape( acf.strEscape('') ) ); /** * Escapes HTML entities from a string. * * @date 08/06/2020 * @since 5.9.0 * * @param string string The input string. * @return string */ acf.escAttr = acf.strEscape; /** * Encodes ') ); //console.log( acf.escHtml( acf.strEscape('') ) ); //console.log( acf.escHtml( '' ) ); /** * acf.decode * * description * * @date 13/1/18 * @since 5.6.5 * * @param type $var Description. Default. * @return type Description. */ acf.decode = function( string ){ return $('