|
<?php |
|
/** |
|
* WBCom — Dokan Downloadable File Picker (Clean, Race-proof) |
|
* |
|
* Fixes the vendor front-end “Choose file” button not populating |
|
* `_wc_file_urls[]` / `_wc_file_names[]` on first attempt. |
|
* |
|
* - Creates a fresh wp.media frame on every click (avoids stale state) |
|
* - Stops propagation to beat conflicting handlers |
|
* - Double-sets value (immediate + slight delay) to win race conditions |
|
* - No console logs (production clean) |
|
* |
|
* Usage: Add via Code Snippets (front-end) or a site plugin. |
|
*/ |
|
if (!defined('ABSPATH')) exit; |
|
|
|
function wbcom_dokan_download_picker_clean() { |
|
if ( is_admin() ) return; |
|
|
|
if ( function_exists('wp_enqueue_media') ) { |
|
wp_enqueue_media(); |
|
} |
|
|
|
$lines = array( |
|
'(function(){', |
|
' function fillInput(el, val){', |
|
' if(!el) return;', |
|
' el.value = val || "";', |
|
' el.dispatchEvent(new Event("input",{bubbles:true}));', |
|
' el.dispatchEvent(new Event("change",{bubbles:true}));', |
|
' if (window.jQuery) { jQuery(el).trigger("input").trigger("change"); }', |
|
' }', |
|
'', |
|
' function handleClick(e){', |
|
' var a = e.target;', |
|
' while(a && a.nodeType===1 && a.tagName!=="A"){ a = a.parentNode; }', |
|
' if(!a || a.tagName!=="A" || !a.classList.contains("upload_file_button")) return;', |
|
' e.preventDefault();', |
|
' e.stopPropagation();', |
|
' if (e.stopImmediatePropagation) e.stopImmediatePropagation();', |
|
'', |
|
' if (typeof wp==="undefined" || !wp.media){', |
|
' alert("Media Library not available (wp.media missing).");', |
|
' return;', |
|
' }', |
|
'', |
|
' var row = a.closest("tr") || document;', |
|
' var url = row.querySelector(\'input.wc_file_url[name="_wc_file_urls[]"]\');', |
|
' var name = row.querySelector(\'input[name="_wc_file_names[]"]\');', |
|
'', |
|
' var frame = wp.media({', |
|
' title: a.getAttribute("data-choose") || "Choose file",', |
|
' button: { text: a.getAttribute("data-update") || "Insert file URL" },', |
|
' multiple: false', |
|
' });', |
|
'', |
|
' frame.on("open", function(){', |
|
' try {', |
|
' var st = frame.state && frame.state();', |
|
' if(st && st.get && st.get("selection")) st.get("selection").reset();', |
|
' } catch(_){}', |
|
' });', |
|
'', |
|
' frame.on("select", function(){', |
|
' var sel = frame.state().get("selection");', |
|
' var file = sel && sel.first ? sel.first().toJSON() : null;', |
|
' if(!file) return;', |
|
' fillInput(url, file.url || "");', |
|
' if (name && !name.value) fillInput(name, file.title || file.filename || "");', |
|
' setTimeout(function(){', |
|
' fillInput(url, file.url || "");', |
|
' if (name && !name.value) fillInput(name, file.title || file.filename || "");', |
|
' }, 150);', |
|
' });', |
|
'', |
|
' frame.open();', |
|
' }', |
|
'', |
|
' document.addEventListener("click", handleClick, true);', |
|
'})();' |
|
); |
|
$js = implode("\n", $lines); |
|
|
|
wp_enqueue_script('jquery'); |
|
wp_add_inline_script('jquery', $js, 'after'); |
|
} |
|
add_action('wp_enqueue_scripts', 'wbcom_dokan_download_picker_clean', 5); |