{ wrapper.classList.remove('is-copied'); el.textContent = ''; }, 1200); } } async function copyText(text) { if (navigator.clipboard && window.isSecureContext) { await navigator.clipboard.writeText(text); return true; } // Fallback for non-secure contexts const ta = document.createElement('textarea'); ta.value = text; ta.setAttribute('readonly', ''); ta.style.position = 'fixed'; ta.style.top = '-1000px'; ta.style.left = '-1000px'; document.body.appendChild(ta); ta.select(); const ok = document.execCommand('copy'); document.body.removeChild(ta); return ok; } function getValue(wrapper) { const valEl = wrapper.querySelector('[data-bl-copythis-value]'); if (!valEl) return ''; return (valEl.textContent || '').trim(); } async function handleCopyFromTarget(target) { const wrapper = target.closest('[data-bl-copythis]'); if (!wrapper) return; const text = getValue(wrapper); if (!text) return; try { await copyText(text); setStatus(wrapper, 'Copied'); } catch (e) { setStatus(wrapper, 'Copy failed'); } } document.addEventListener('click', (e) => { const t = e.target; if (t.closest('[data-bl-copythis-btn]') || t.closest('[data-bl-copythis-value]')) { e.preventDefault(); handleCopyFromTarget(t); } }); document.addEventListener('keydown', (e) => { const t = e.target; const isValue = t && t.matches && t.matches('[data-bl-copythis-value]'); if (!isValue) return; if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); handleCopyFromTarget(t); } }); })(); JS , 'after' ); }, 20 ); add_shortcode( 'bl_copy', function ( $atts, $content = null ) { $atts = shortcode_atts( [ 'label' => '', 'tag' => 'div', ], $atts, 'bl_copy' ); $raw = do_shortcode( $content ?? '' ); $raw = wp_strip_all_tags( $raw, false ); $raw = trim( preg_replace( "/\r\n|\r|\n/", "\n", $raw ) ); if ( '' === $raw ) { return ''; } $id = 'bl-copythis-' . wp_generate_uuid4(); $label = sanitize_text_field( $atts['label'] ); $tag = strtolower( preg_replace( '/[^a-z0-9]/i', '', (string) $atts['tag'] ) ); $tag = in_array( $tag, [ 'div', 'span', 'code' ], true ) ? $tag : 'div'; ob_start(); ?>
< class="bl-copythis__value" data-bl-copythis-value tabindex="0" role="button" aria-label="Copy value" >>