Make WordPress Core

Ticket #64446: copy-permalink-admin.patch

File copy-permalink-admin.patch, 6.4 KB (added by solankisoftware, 6 months ago)

Tested the patch on a local install. The "Copy Permalink" action works for posts, pages, and custom post types without PHP or JS errors.

  • wp-admin/edit.php

     
    242242        wp_enqueue_style( 'wp-list-reusable-blocks' );
    243243}
    244244
     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 );";
     301wp_add_inline_script( 'jquery', $copy_permalink_script );
     302
    245303// Used in the HTML title tag.
    246304$title = $post_type_object->labels->name;
    247305
  • wp-admin/includes/class-wp-posts-list-table.php

     
    437437                        if ( $this->is_trash ) {
    438438                                $actions['untrash'] = __( 'Restore' );
    439439                        } else {
    440                                 $actions['edit'] = __( 'Bulk edit' );
     440                                $actions['edit'] = __( 'Edit' );
    441441                        }
    442442                }
    443443
     
    15621562                                        __( 'View' )
    15631563                                );
    15641564                        }
     1565                       
     1566                        // Add Copy Permalink action for all viewable post types
     1567                        if ( 'trash' !== $post->post_status ) {
     1568                                $permalink = get_permalink( $post->ID );
     1569                                if ( $permalink ) {
     1570                                        $actions['copy-permalink'] = sprintf(
     1571                                                '<button type="button" class="copy-permalink-link button-link" data-permalink="%s" aria-label="%s">%s</button>',
     1572                                                esc_attr( $permalink ),
     1573                                                /* translators: %s: Post title. */
     1574                                                esc_attr( sprintf( __( 'Copy permalink for &#8220;%s&#8221;' ), $title ) ),
     1575                                                __( 'Copy Permalink' )
     1576                                        );
     1577                                }
     1578                        }
    15651579                }
    15661580
    15671581                if ( 'wp_block' === $post->post_type ) {