| | 245 | // Add JavaScript for Copy Permalink functionality |
| | 246 | $copy_permalink_script = "( function( $ ) { |
| | 247 | $( document ).on( 'click', '.copy-permalink-link', function( e ) { |
| | 248 | e.preventDefault(); |
| | 249 | var button = $( this ); |
| | 250 | var permalink = button.data( 'permalink' ); |
| | 251 | |
| | 252 | if ( ! permalink ) { |
| | 253 | return; |
| | 254 | } |
| | 255 | |
| | 256 | // Use the Clipboard API if available |
| | 257 | if ( navigator.clipboard && window.isSecureContext ) { |
| | 258 | navigator.clipboard.writeText( permalink ).then( function() { |
| | 259 | // Show success feedback |
| | 260 | var originalText = button.text(); |
| | 261 | button.text( '" . esc_js( __( 'Copied!' ) ) . "' ); |
| | 262 | setTimeout( function() { |
| | 263 | button.text( originalText ); |
| | 264 | }, 2000 ); |
| | 265 | }, function() { |
| | 266 | // Fallback if clipboard API fails |
| | 267 | fallbackCopyTextToClipboard( permalink, button ); |
| | 268 | } ); |
| | 269 | } else { |
| | 270 | // Fallback for older browsers |
| | 271 | fallbackCopyTextToClipboard( permalink, button ); |
| | 272 | } |
| | 273 | } ); |
| | 274 | |
| | 275 | function fallbackCopyTextToClipboard( text, button ) { |
| | 276 | var textArea = document.createElement( 'textarea' ); |
| | 277 | textArea.value = text; |
| | 278 | textArea.style.position = 'fixed'; |
| | 279 | textArea.style.left = '-999999px'; |
| | 280 | textArea.style.top = '-999999px'; |
| | 281 | document.body.appendChild( textArea ); |
| | 282 | textArea.focus(); |
| | 283 | textArea.select(); |
| | 284 | |
| | 285 | try { |
| | 286 | var successful = document.execCommand( 'copy' ); |
| | 287 | if ( successful ) { |
| | 288 | var originalText = button.text(); |
| | 289 | button.text( '" . esc_js( __( 'Copied!' ) ) . "' ); |
| | 290 | setTimeout( function() { |
| | 291 | button.text( originalText ); |
| | 292 | }, 2000 ); |
| | 293 | } |
| | 294 | } catch ( err ) { |
| | 295 | // Copy failed |
| | 296 | } |
| | 297 | |
| | 298 | document.body.removeChild( textArea ); |
| | 299 | } |
| | 300 | } )( jQuery );"; |
| | 301 | wp_add_inline_script( 'jquery', $copy_permalink_script ); |
| | 302 | |