| 1 |
<?php |
| 2 |
/** |
| 3 |
* Themify Compatibility Code |
| 4 |
* |
| 5 |
* @package Themify |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Formidable Forms |
| 10 |
* @link https://formidableforms.com/ |
| 11 |
*/ |
| 12 |
class Themify_Compat_formidable { |
| 13 |
|
| 14 |
static function init() { |
| 15 |
if ( ! is_admin() ) { |
| 16 |
add_action( 'wp_print_footer_scripts', [ __CLASS__, 'wp_print_footer_scripts' ], 1 ); |
| 17 |
add_action( 'frm_pro_before_footer_js', [ __CLASS__, 'frm_pro_before_footer_js' ], 1 ); |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
public static function wp_print_footer_scripts() { |
| 22 |
if ( wp_script_is( 'slimselect', 'enqueued' ) || wp_script_is( 'slimselect', 'done' ) ) { |
| 23 |
wp_add_inline_script( 'slimselect', self::get_script(), 'after' ); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
public static function frm_pro_before_footer_js() { |
| 28 |
echo '<script>' . self::get_script() . '</script>'; |
| 29 |
} |
| 30 |
|
| 31 |
private static function get_script() { |
| 32 |
return <<<'JS' |
| 33 |
(function(){ |
| 34 |
if(!window.SlimSelect || window.SlimSelect.__themifyFrmCompat){ |
| 35 |
return; |
| 36 |
} |
| 37 |
var OriginalSlimSelect = window.SlimSelect, |
| 38 |
skipClickUntil = 0, |
| 39 |
allowProgrammaticClick = false; |
| 40 |
|
| 41 |
function ThemifySlimSelectCompat(args){ |
| 42 |
var select = args && args.select; |
| 43 |
if('string' === typeof select){ |
| 44 |
select = document.querySelector(select); |
| 45 |
} |
| 46 |
if(select && 'SELECT' === select.tagName && select.classList.contains('frm_slimselect') && select.slim){ |
| 47 |
select.style.display = 'none'; |
| 48 |
select.setAttribute('aria-hidden','true'); |
| 49 |
return select.slim; |
| 50 |
} |
| 51 |
return new OriginalSlimSelect(args); |
| 52 |
} |
| 53 |
|
| 54 |
function getFormidableSlimSelect(option){ |
| 55 |
var content = option.closest('.ss-content.frm_slimselect'), |
| 56 |
id = content ? content.getAttribute('data-id') : null, |
| 57 |
select; |
| 58 |
if(!id){ |
| 59 |
return null; |
| 60 |
} |
| 61 |
try{ |
| 62 |
select = document.querySelector('select.frm_slimselect[data-id="' + CSS.escape(id) + '"]'); |
| 63 |
} |
| 64 |
catch(e){ |
| 65 |
select = document.querySelector('select.frm_slimselect[data-id="' + id.replace(/"/g,'\\"') + '"]'); |
| 66 |
} |
| 67 |
return select && select.slim ? select.slim : null; |
| 68 |
} |
| 69 |
|
| 70 |
function selectOptionOnPointerDown(e){ |
| 71 |
var option = e.target && e.target.closest ? e.target.closest('.ss-content.frm_slimselect .ss-option') : null, |
| 72 |
slim; |
| 73 |
if(!option || option.classList.contains('ss-disabled') || option.classList.contains('ss-hide')){ |
| 74 |
return; |
| 75 |
} |
| 76 |
if(e.type === 'pointerdown' && e.pointerType && e.pointerType !== 'mouse'){ |
| 77 |
return; |
| 78 |
} |
| 79 |
if(e.button !== undefined && e.button !== 0){ |
| 80 |
return; |
| 81 |
} |
| 82 |
slim = getFormidableSlimSelect(option); |
| 83 |
if(!slim || !slim.settings || !slim.settings.isOpen){ |
| 84 |
return; |
| 85 |
} |
| 86 |
allowProgrammaticClick = true; |
| 87 |
option.click(); |
| 88 |
allowProgrammaticClick = false; |
| 89 |
skipClickUntil = Date.now() + 500; |
| 90 |
e.preventDefault(); |
| 91 |
e.stopImmediatePropagation(); |
| 92 |
} |
| 93 |
|
| 94 |
function suppressDuplicateClick(e){ |
| 95 |
if(!allowProgrammaticClick && skipClickUntil && Date.now() < skipClickUntil){ |
| 96 |
skipClickUntil = 0; |
| 97 |
e.preventDefault(); |
| 98 |
e.stopImmediatePropagation(); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
ThemifySlimSelectCompat.prototype = OriginalSlimSelect.prototype; |
| 103 |
for(var key in OriginalSlimSelect){ |
| 104 |
if(Object.prototype.hasOwnProperty.call(OriginalSlimSelect,key)){ |
| 105 |
ThemifySlimSelectCompat[key] = OriginalSlimSelect[key]; |
| 106 |
} |
| 107 |
} |
| 108 |
ThemifySlimSelectCompat.__themifyFrmCompat = true; |
| 109 |
window.SlimSelect = ThemifySlimSelectCompat; |
| 110 |
|
| 111 |
document.addEventListener('pointerdown', selectOptionOnPointerDown, true); |
| 112 |
document.addEventListener('click', suppressDuplicateClick, true); |
| 113 |
})(); |
| 114 |
JS; |
| 115 |
} |
| 116 |
} |
| 117 |
|