Make WordPress Core


Ignore:
Timestamp:
06/20/2023 03:45:23 PM (3 years ago)
Author:
SergeyBiryukov
Message:

Media: Display the “Copy URL” and “Download file” row actions when the “Unattached” filter is applied.

Due to partially duplicated logic for displaying row actions in the Media Library with and without the “Unattached” filter, the “Copy URL” and “Download file” row actions were unintentionally missing with the filter applied.

This commit aims to simplify the logic and bring more consistency to the code.

Includes displaying the “Download file” row action even when the “Trash” filter is applied, giving the user one more chance to download the media file before they delete it.

Follow-up to [8901], [13100], [16227], [16229], [52842], [55198], [55221].

Props kebbet, costdev, pbiron, oglekler, SergeyBiryukov.
Fixes #57890, #57893.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-media-list-table.php

    r55911 r55949  
    749749                $actions = array();
    750750
    751                 if ( $this->detached ) {
    752                         if ( current_user_can( 'edit_post', $post->ID ) ) {
    753                                 $actions['edit'] = sprintf(
    754                                         '<a href="%s" aria-label="%s">%s</a>',
    755                                         get_edit_post_link( $post->ID ),
     751                if ( ! $this->is_trash && current_user_can( 'edit_post', $post->ID ) ) {
     752                        $actions['edit'] = sprintf(
     753                                '<a href="%s" aria-label="%s">%s</a>',
     754                                esc_url( get_edit_post_link( $post->ID ) ),
     755                                /* translators: %s: Attachment title. */
     756                                esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ),
     757                                __( 'Edit' )
     758                        );
     759                }
     760
     761                if ( current_user_can( 'delete_post', $post->ID ) ) {
     762                        if ( $this->is_trash ) {
     763                                $actions['untrash'] = sprintf(
     764                                        '<a href="%s" class="submitdelete aria-button-if-js" aria-label="%s">%s</a>',
     765                                        esc_url( wp_nonce_url( "post.php?action=untrash&amp;post=$post->ID", 'untrash-post_' . $post->ID ) ),
    756766                                        /* translators: %s: Attachment title. */
    757                                         esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ),
    758                                         __( 'Edit' )
     767                                        esc_attr( sprintf( __( 'Restore &#8220;%s&#8221; from the Trash' ), $att_title ) ),
     768                                        __( 'Restore' )
    759769                                );
    760                         }
    761 
    762                         if ( current_user_can( 'delete_post', $post->ID ) ) {
    763                                 if ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) {
    764                                         $actions['trash'] = sprintf(
    765                                                 '<a href="%s" class="submitdelete aria-button-if-js" aria-label="%s">%s</a>',
    766                                                 wp_nonce_url( "post.php?action=trash&amp;post=$post->ID", 'trash-post_' . $post->ID ),
    767                                                 /* translators: %s: Attachment title. */
    768                                                 esc_attr( sprintf( __( 'Move &#8220;%s&#8221; to the Trash' ), $att_title ) ),
    769                                                 _x( 'Trash', 'verb' )
    770                                         );
    771                                 } else {
    772                                         $delete_ays        = ! MEDIA_TRASH ? " onclick='return showNotice.warn();'" : '';
    773                                         $actions['delete'] = sprintf(
    774                                                 '<a href="%s" class="submitdelete aria-button-if-js"%s aria-label="%s">%s</a>',
    775                                                 wp_nonce_url( "post.php?action=delete&amp;post=$post->ID", 'delete-post_' . $post->ID ),
    776                                                 $delete_ays,
    777                                                 /* translators: %s: Attachment title. */
    778                                                 esc_attr( sprintf( __( 'Delete &#8220;%s&#8221; permanently' ), $att_title ) ),
    779                                                 __( 'Delete Permanently' )
    780                                         );
    781                                 }
    782                         }
    783 
    784                         if ( get_permalink( $post->ID ) ) {
     770                        } elseif ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) {
     771                                $actions['trash'] = sprintf(
     772                                        '<a href="%s" class="submitdelete aria-button-if-js" aria-label="%s">%s</a>',
     773                                        esc_url( wp_nonce_url( "post.php?action=trash&amp;post=$post->ID", 'trash-post_' . $post->ID ) ),
     774                                        /* translators: %s: Attachment title. */
     775                                        esc_attr( sprintf( __( 'Move &#8220;%s&#8221; to the Trash' ), $att_title ) ),
     776                                        _x( 'Trash', 'verb' )
     777                                );
     778                        }
     779
     780                        if ( $this->is_trash || ! EMPTY_TRASH_DAYS || ! MEDIA_TRASH ) {
     781                                $show_confirmation = ( ! $this->is_trash && ! MEDIA_TRASH ) ? " onclick='return showNotice.warn();'" : '';
     782
     783                                $actions['delete'] = sprintf(
     784                                        '<a href="%s" class="submitdelete aria-button-if-js"%s aria-label="%s">%s</a>',
     785                                        esc_url( wp_nonce_url( "post.php?action=delete&amp;post=$post->ID", 'delete-post_' . $post->ID ) ),
     786                                        $show_confirmation,
     787                                        /* translators: %s: Attachment title. */
     788                                        esc_attr( sprintf( __( 'Delete &#8220;%s&#8221; permanently' ), $att_title ) ),
     789                                        __( 'Delete Permanently' )
     790                                );
     791                        }
     792                }
     793
     794                $attachment_url = wp_get_attachment_url( $post->ID );
     795
     796                if ( ! $this->is_trash ) {
     797                        $permalink = get_permalink( $post->ID );
     798
     799                        if ( $permalink ) {
    785800                                $actions['view'] = sprintf(
    786801                                        '<a href="%s" aria-label="%s" rel="bookmark">%s</a>',
    787                                         get_permalink( $post->ID ),
     802                                        esc_url( $permalink ),
    788803                                        /* translators: %s: Attachment title. */
    789804                                        esc_attr( sprintf( __( 'View &#8220;%s&#8221;' ), $att_title ) ),
    … …  
    792807                        }
    793808
    794                         if ( current_user_can( 'edit_post', $post->ID ) ) {
    795                                 $actions['attach'] = sprintf(
    796                                         '<a href="#the-list" onclick="findPosts.open( \'media[]\', \'%s\' ); return false;" class="hide-if-no-js aria-button-if-js" aria-label="%s">%s</a>',
    797                                         $post->ID,
    798                                         /* translators: %s: Attachment title. */
    799                                         esc_attr( sprintf( __( 'Attach &#8220;%s&#8221; to existing content' ), $att_title ) ),
    800                                         __( 'Attach' )
    801                                 );
    802                         }
    803                 } else {
    804                         if ( current_user_can( 'edit_post', $post->ID ) && ! $this->is_trash ) {
    805                                 $actions['edit'] = sprintf(
    806                                         '<a href="%s" aria-label="%s">%s</a>',
    807                                         get_edit_post_link( $post->ID ),
    808                                         /* translators: %s: Attachment title. */
    809                                         esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ),
    810                                         __( 'Edit' )
    811                                 );
    812                         }
    813 
    814                         if ( current_user_can( 'delete_post', $post->ID ) ) {
    815                                 if ( $this->is_trash ) {
    816                                         $actions['untrash'] = sprintf(
    817                                                 '<a href="%s" class="submitdelete aria-button-if-js" aria-label="%s">%s</a>',
    818                                                 wp_nonce_url( "post.php?action=untrash&amp;post=$post->ID", 'untrash-post_' . $post->ID ),
    819                                                 /* translators: %s: Attachment title. */
    820                                                 esc_attr( sprintf( __( 'Restore &#8220;%s&#8221; from the Trash' ), $att_title ) ),
    821                                                 __( 'Restore' )
    822                                         );
    823                                 } elseif ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) {
    824                                         $actions['trash'] = sprintf(
    825                                                 '<a href="%s" class="submitdelete aria-button-if-js" aria-label="%s">%s</a>',
    826                                                 wp_nonce_url( "post.php?action=trash&amp;post=$post->ID", 'trash-post_' . $post->ID ),
    827                                                 /* translators: %s: Attachment title. */
    828                                                 esc_attr( sprintf( __( 'Move &#8220;%s&#8221; to the Trash' ), $att_title ) ),
    829                                                 _x( 'Trash', 'verb' )
    830                                         );
    831                                 }
    832 
    833                                 if ( $this->is_trash || ! EMPTY_TRASH_DAYS || ! MEDIA_TRASH ) {
    834                                         $delete_ays        = ( ! $this->is_trash && ! MEDIA_TRASH ) ? " onclick='return showNotice.warn();'" : '';
    835                                         $actions['delete'] = sprintf(
    836                                                 '<a href="%s" class="submitdelete aria-button-if-js"%s aria-label="%s">%s</a>',
    837                                                 wp_nonce_url( "post.php?action=delete&amp;post=$post->ID", 'delete-post_' . $post->ID ),
    838                                                 $delete_ays,
    839                                                 /* translators: %s: Attachment title. */
    840                                                 esc_attr( sprintf( __( 'Delete &#8220;%s&#8221; permanently' ), $att_title ) ),
    841                                                 __( 'Delete Permanently' )
    842                                         );
    843                                 }
    844                         }
    845 
    846                         if ( ! $this->is_trash ) {
    847                                 if ( get_permalink( $post->ID ) ) {
    848                                         $actions['view'] = sprintf(
    849                                                 '<a href="%s" aria-label="%s" rel="bookmark">%s</a>',
    850                                                 get_permalink( $post->ID ),
    851                                                 /* translators: %s: Attachment title. */
    852                                                 esc_attr( sprintf( __( 'View &#8220;%s&#8221;' ), $att_title ) ),
    853                                                 __( 'View' )
    854                                         );
    855                                 }
    856 
    857                                 $actions['copy'] = sprintf(
    858                                         '<span class="copy-to-clipboard-container"><button type="button" class="button-link copy-attachment-url media-library" data-clipboard-text="%s" aria-label="%s">%s</button><span class="success hidden" aria-hidden="true">%s</span></span>',
    859                                         esc_url( wp_get_attachment_url( $post->ID ) ),
    860                                         /* translators: %s: Attachment title. */
    861                                         esc_attr( sprintf( __( 'Copy &#8220;%s&#8221; URL to clipboard' ), $att_title ) ),
    862                                         __( 'Copy URL' ),
    863                                         __( 'Copied!' )
    864                                 );
    865 
    866                                 $actions['download'] = sprintf(
    867                                         '<a href="%s" aria-label="%s" download>%s</a>',
    868                                         esc_url( wp_get_attachment_url( $post->ID ) ),
    869                                         /* translators: %s: Attachment title. */
    870                                         esc_attr( sprintf( __( 'Download &#8220;%s&#8221;' ), $att_title ) ),
    871                                         __( 'Download file' )
    872                                 );
    873                         }
     809                        $actions['copy'] = sprintf(
     810                                '<span class="copy-to-clipboard-container"><button type="button" class="button-link copy-attachment-url media-library" data-clipboard-text="%s" aria-label="%s">%s</button><span class="success hidden" aria-hidden="true">%s</span></span>',
     811                                esc_url( $attachment_url ),
     812                                /* translators: %s: Attachment title. */
     813                                esc_attr( sprintf( __( 'Copy &#8220;%s&#8221; URL to clipboard' ), $att_title ) ),
     814                                __( 'Copy URL' ),
     815                                __( 'Copied!' )
     816                        );
     817                }
     818
     819                $actions['download'] = sprintf(
     820                        '<a href="%s" aria-label="%s" download>%s</a>',
     821                        esc_url( $attachment_url ),
     822                        /* translators: %s: Attachment title. */
     823                        esc_attr( sprintf( __( 'Download &#8220;%s&#8221;' ), $att_title ) ),
     824                        __( 'Download file' )
     825                );
     826
     827                if ( $this->detached && current_user_can( 'edit_post', $post->ID ) ) {
     828                        $actions['attach'] = sprintf(
     829                                '<a href="#the-list" onclick="findPosts.open( \'media[]\', \'%s\' ); return false;" class="hide-if-no-js aria-button-if-js" aria-label="%s">%s</a>',
     830                                $post->ID,
     831                                /* translators: %s: Attachment title. */
     832                                esc_attr( sprintf( __( 'Attach &#8220;%s&#8221; to existing content' ), $att_title ) ),
     833                                __( 'Attach' )
     834                        );
    874835                }
    875836
    … …  
    880841                 *
    881842                 * @param string[] $actions  An array of action links for each attachment.
    882                  *                           Default 'Edit', 'Delete Permanently', 'View'.
     843                 *                           Includes 'Edit', 'Delete Permanently', 'View',
     844                 *                           'Copy URL' and 'Download file'.
    883845                 * @param WP_Post  $post     WP_Post object for the current attachment.
    884846                 * @param bool     $detached Whether the list table contains media not attached
Note: See TracChangeset for help on using the changeset viewer.