|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Plugin Name: Code block styling and copy button |
| 4 | + * Description: Styles WordPress code blocks (wp-block-code) like a coding block and adds a one-click copy button. |
| 5 | + * Version: 1.0 |
| 6 | + */ |
| 7 | + |
| 8 | +if ( ! defined( 'ABSPATH' ) ) { |
| 9 | + exit; |
| 10 | +} |
| 11 | + |
| 12 | +add_action( 'wp_enqueue_scripts', function() { |
| 13 | + $css = ' |
| 14 | + .wp-block-code, |
| 15 | + pre.wp-block-code { |
| 16 | + position: relative; |
| 17 | + background: #1e1e1e; |
| 18 | + color: #d4d4d4; |
| 19 | + font-family: Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace; |
| 20 | + font-size: 14px; |
| 21 | + line-height: 1.5; |
| 22 | + padding: 1em 1.25em; |
| 23 | + padding-top: 2.5em; |
| 24 | + border-radius: 6px; |
| 25 | + border: 1px solid #333; |
| 26 | + overflow-x: auto; |
| 27 | + margin: 1.25em 0; |
| 28 | + box-shadow: 0 2px 8px rgba(0,0,0,0.15); |
| 29 | + } |
| 30 | + .wp-block-code code, |
| 31 | + pre.wp-block-code code { |
| 32 | + display: block; |
| 33 | + background: none; |
| 34 | + color: inherit; |
| 35 | + padding: 0; |
| 36 | + font-size: inherit; |
| 37 | + white-space: pre; |
| 38 | + } |
| 39 | + .wp-block-code .code-block-copy-btn { |
| 40 | + position: absolute; |
| 41 | + top: 8px; |
| 42 | + right: 8px; |
| 43 | + padding: 6px 12px; |
| 44 | + font-size: 12px; |
| 45 | + font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; |
| 46 | + background: #0e639c; |
| 47 | + color: #fff; |
| 48 | + border: none; |
| 49 | + border-radius: 4px; |
| 50 | + cursor: pointer; |
| 51 | + opacity: 0.9; |
| 52 | + transition: opacity 0.15s, background 0.15s; |
| 53 | + } |
| 54 | + .wp-block-code .code-block-copy-btn:hover { |
| 55 | + opacity: 1; |
| 56 | + background: #1177bb; |
| 57 | + } |
| 58 | + .wp-block-code .code-block-copy-btn.copied { |
| 59 | + background: #107c10; |
| 60 | + } |
| 61 | + '; |
| 62 | + wp_register_style( 'code-block-styling', false, array(), '1.0' ); |
| 63 | + wp_enqueue_style( 'code-block-styling' ); |
| 64 | + wp_add_inline_style( 'code-block-styling', $css ); |
| 65 | +}, 20 ); |
| 66 | + |
| 67 | +add_action( 'wp_footer', function() { |
| 68 | + $copy_text = 'Αντιγραφή'; |
| 69 | + $copied_text = 'Αντιγραφήκε!'; |
| 70 | + ?> |
| 71 | + <script> |
| 72 | + (function() { |
| 73 | + function initCodeBlockCopy() { |
| 74 | + var blocks = document.querySelectorAll( '.wp-block-code, pre.wp-block-code' ); |
| 75 | + blocks.forEach(function( block ) { |
| 76 | + if ( block.querySelector( '.code-block-copy-btn' ) ) return; |
| 77 | + var code = block.querySelector( 'code' ); |
| 78 | + var target = code || block; |
| 79 | + var btn = document.createElement( 'button' ); |
| 80 | + btn.type = 'button'; |
| 81 | + btn.className = 'code-block-copy-btn'; |
| 82 | + btn.textContent = '<?php echo esc_js( $copy_text ); ?>'; |
| 83 | + btn.setAttribute( 'aria-label', '<?php echo esc_js( $copy_text ); ?>' ); |
| 84 | + block.style.position = 'relative'; |
| 85 | + block.insertBefore( btn, block.firstChild ); |
| 86 | + btn.addEventListener( 'click', function() { |
| 87 | + var text = ( code && code.textContent ) ? code.textContent : block.innerText; |
| 88 | + text = text.replace( /\n$/, '' ); |
| 89 | + if ( navigator.clipboard && navigator.clipboard.writeText ) { |
| 90 | + navigator.clipboard.writeText( text ).then(function() { |
| 91 | + btn.textContent = '<?php echo esc_js( $copied_text ); ?>'; |
| 92 | + btn.classList.add( 'copied' ); |
| 93 | + setTimeout(function() { |
| 94 | + btn.textContent = '<?php echo esc_js( $copy_text ); ?>'; |
| 95 | + btn.classList.remove( 'copied' ); |
| 96 | + }, 2000); |
| 97 | + }); |
| 98 | + } else { |
| 99 | + var ta = document.createElement( 'textarea' ); |
| 100 | + ta.value = text; |
| 101 | + ta.style.position = 'fixed'; |
| 102 | + ta.style.left = '-9999px'; |
| 103 | + document.body.appendChild( ta ); |
| 104 | + ta.select(); |
| 105 | + try { |
| 106 | + document.execCommand( 'copy' ); |
| 107 | + btn.textContent = '<?php echo esc_js( $copied_text ); ?>'; |
| 108 | + btn.classList.add( 'copied' ); |
| 109 | + setTimeout(function() { |
| 110 | + btn.textContent = '<?php echo esc_js( $copy_text ); ?>'; |
| 111 | + btn.classList.remove( 'copied' ); |
| 112 | + }, 2000); |
| 113 | + } catch ( e ) {} |
| 114 | + document.body.removeChild( ta ); |
| 115 | + } |
| 116 | + }); |
| 117 | + }); |
| 118 | + } |
| 119 | + if ( document.readyState === 'loading' ) { |
| 120 | + document.addEventListener( 'DOMContentLoaded', initCodeBlockCopy ); |
| 121 | + } else { |
| 122 | + initCodeBlockCopy(); |
| 123 | + } |
| 124 | + })(); |
| 125 | + </script> |
| 126 | + <?php |
| 127 | +}, 99 ); |
0 commit comments