From b17064a082b82fc737b312d9fb2849f2cccf792e Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 20 Jun 2023 15:45:23 +0000 Subject: [PATCH] =?UTF-8?q?Media:=20Display=20the=20=E2=80=9CCopy=20URL?= =?UTF-8?q?=E2=80=9D=20and=20=E2=80=9CDownload=20file=E2=80=9D=20row=20act?= =?UTF-8?q?ions=20when=20the=20=E2=80=9CUnattached=E2=80=9D=20filter=20is?= =?UTF-8?q?=20applied.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. git-svn-id: https://develop.svn.wordpress.org/trunk@55949 602fd350-edb4-49c9-b593-d223f7449a82 --- .../includes/class-wp-media-list-table.php | 180 +++++++----------- src/wp-admin/includes/meta-boxes.php | 16 +- 2 files changed, 84 insertions(+), 112 deletions(-) diff --git a/src/wp-admin/includes/class-wp-media-list-table.php b/src/wp-admin/includes/class-wp-media-list-table.php index 746abf3662..870f36f3ae 100644 --- a/src/wp-admin/includes/class-wp-media-list-table.php +++ b/src/wp-admin/includes/class-wp-media-list-table.php @@ -748,129 +748,90 @@ class WP_Media_List_Table extends WP_List_Table { private function _get_row_actions( $post, $att_title ) { $actions = array(); - if ( $this->detached ) { - if ( current_user_can( 'edit_post', $post->ID ) ) { - $actions['edit'] = sprintf( - '%s', - get_edit_post_link( $post->ID ), + if ( ! $this->is_trash && current_user_can( 'edit_post', $post->ID ) ) { + $actions['edit'] = sprintf( + '%s', + esc_url( get_edit_post_link( $post->ID ) ), + /* translators: %s: Attachment title. */ + esc_attr( sprintf( __( 'Edit “%s”' ), $att_title ) ), + __( 'Edit' ) + ); + } + + if ( current_user_can( 'delete_post', $post->ID ) ) { + if ( $this->is_trash ) { + $actions['untrash'] = sprintf( + '%s', + esc_url( wp_nonce_url( "post.php?action=untrash&post=$post->ID", 'untrash-post_' . $post->ID ) ), /* translators: %s: Attachment title. */ - esc_attr( sprintf( __( 'Edit “%s”' ), $att_title ) ), - __( 'Edit' ) + esc_attr( sprintf( __( 'Restore “%s” from the Trash' ), $att_title ) ), + __( 'Restore' ) + ); + } elseif ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) { + $actions['trash'] = sprintf( + '%s', + esc_url( wp_nonce_url( "post.php?action=trash&post=$post->ID", 'trash-post_' . $post->ID ) ), + /* translators: %s: Attachment title. */ + esc_attr( sprintf( __( 'Move “%s” to the Trash' ), $att_title ) ), + _x( 'Trash', 'verb' ) ); } - if ( current_user_can( 'delete_post', $post->ID ) ) { - if ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) { - $actions['trash'] = sprintf( - '%s', - wp_nonce_url( "post.php?action=trash&post=$post->ID", 'trash-post_' . $post->ID ), - /* translators: %s: Attachment title. */ - esc_attr( sprintf( __( 'Move “%s” to the Trash' ), $att_title ) ), - _x( 'Trash', 'verb' ) - ); - } else { - $delete_ays = ! MEDIA_TRASH ? " onclick='return showNotice.warn();'" : ''; - $actions['delete'] = sprintf( - '%s', - wp_nonce_url( "post.php?action=delete&post=$post->ID", 'delete-post_' . $post->ID ), - $delete_ays, - /* translators: %s: Attachment title. */ - esc_attr( sprintf( __( 'Delete “%s” permanently' ), $att_title ) ), - __( 'Delete Permanently' ) - ); - } - } + if ( $this->is_trash || ! EMPTY_TRASH_DAYS || ! MEDIA_TRASH ) { + $show_confirmation = ( ! $this->is_trash && ! MEDIA_TRASH ) ? " onclick='return showNotice.warn();'" : ''; - if ( get_permalink( $post->ID ) ) { + $actions['delete'] = sprintf( + '%s', + esc_url( wp_nonce_url( "post.php?action=delete&post=$post->ID", 'delete-post_' . $post->ID ) ), + $show_confirmation, + /* translators: %s: Attachment title. */ + esc_attr( sprintf( __( 'Delete “%s” permanently' ), $att_title ) ), + __( 'Delete Permanently' ) + ); + } + } + + $attachment_url = wp_get_attachment_url( $post->ID ); + + if ( ! $this->is_trash ) { + $permalink = get_permalink( $post->ID ); + + if ( $permalink ) { $actions['view'] = sprintf( '%s', - get_permalink( $post->ID ), + esc_url( $permalink ), /* translators: %s: Attachment title. */ esc_attr( sprintf( __( 'View “%s”' ), $att_title ) ), __( 'View' ) ); } - if ( current_user_can( 'edit_post', $post->ID ) ) { - $actions['attach'] = sprintf( - '%s', - $post->ID, - /* translators: %s: Attachment title. */ - esc_attr( sprintf( __( 'Attach “%s” to existing content' ), $att_title ) ), - __( 'Attach' ) - ); - } - } else { - if ( current_user_can( 'edit_post', $post->ID ) && ! $this->is_trash ) { - $actions['edit'] = sprintf( - '%s', - get_edit_post_link( $post->ID ), - /* translators: %s: Attachment title. */ - esc_attr( sprintf( __( 'Edit “%s”' ), $att_title ) ), - __( 'Edit' ) - ); - } + $actions['copy'] = sprintf( + '', + esc_url( $attachment_url ), + /* translators: %s: Attachment title. */ + esc_attr( sprintf( __( 'Copy “%s” URL to clipboard' ), $att_title ) ), + __( 'Copy URL' ), + __( 'Copied!' ) + ); + } - if ( current_user_can( 'delete_post', $post->ID ) ) { - if ( $this->is_trash ) { - $actions['untrash'] = sprintf( - '%s', - wp_nonce_url( "post.php?action=untrash&post=$post->ID", 'untrash-post_' . $post->ID ), - /* translators: %s: Attachment title. */ - esc_attr( sprintf( __( 'Restore “%s” from the Trash' ), $att_title ) ), - __( 'Restore' ) - ); - } elseif ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) { - $actions['trash'] = sprintf( - '%s', - wp_nonce_url( "post.php?action=trash&post=$post->ID", 'trash-post_' . $post->ID ), - /* translators: %s: Attachment title. */ - esc_attr( sprintf( __( 'Move “%s” to the Trash' ), $att_title ) ), - _x( 'Trash', 'verb' ) - ); - } + $actions['download'] = sprintf( + '%s', + esc_url( $attachment_url ), + /* translators: %s: Attachment title. */ + esc_attr( sprintf( __( 'Download “%s”' ), $att_title ) ), + __( 'Download file' ) + ); - if ( $this->is_trash || ! EMPTY_TRASH_DAYS || ! MEDIA_TRASH ) { - $delete_ays = ( ! $this->is_trash && ! MEDIA_TRASH ) ? " onclick='return showNotice.warn();'" : ''; - $actions['delete'] = sprintf( - '%s', - wp_nonce_url( "post.php?action=delete&post=$post->ID", 'delete-post_' . $post->ID ), - $delete_ays, - /* translators: %s: Attachment title. */ - esc_attr( sprintf( __( 'Delete “%s” permanently' ), $att_title ) ), - __( 'Delete Permanently' ) - ); - } - } - - if ( ! $this->is_trash ) { - if ( get_permalink( $post->ID ) ) { - $actions['view'] = sprintf( - '%s', - get_permalink( $post->ID ), - /* translators: %s: Attachment title. */ - esc_attr( sprintf( __( 'View “%s”' ), $att_title ) ), - __( 'View' ) - ); - } - - $actions['copy'] = sprintf( - '', - esc_url( wp_get_attachment_url( $post->ID ) ), - /* translators: %s: Attachment title. */ - esc_attr( sprintf( __( 'Copy “%s” URL to clipboard' ), $att_title ) ), - __( 'Copy URL' ), - __( 'Copied!' ) - ); - - $actions['download'] = sprintf( - '%s', - esc_url( wp_get_attachment_url( $post->ID ) ), - /* translators: %s: Attachment title. */ - esc_attr( sprintf( __( 'Download “%s”' ), $att_title ) ), - __( 'Download file' ) - ); - } + if ( $this->detached && current_user_can( 'edit_post', $post->ID ) ) { + $actions['attach'] = sprintf( + '%s', + $post->ID, + /* translators: %s: Attachment title. */ + esc_attr( sprintf( __( 'Attach “%s” to existing content' ), $att_title ) ), + __( 'Attach' ) + ); } /** @@ -879,7 +840,8 @@ class WP_Media_List_Table extends WP_List_Table { * @since 2.8.0 * * @param string[] $actions An array of action links for each attachment. - * Default 'Edit', 'Delete Permanently', 'View'. + * Includes 'Edit', 'Delete Permanently', 'View', + * 'Copy URL' and 'Download file'. * @param WP_Post $post WP_Post object for the current attachment. * @param bool $detached Whether the list table contains media not attached * to any posts. Default true. diff --git a/src/wp-admin/includes/meta-boxes.php b/src/wp-admin/includes/meta-boxes.php index 7cd8496aff..1ac43f8221 100644 --- a/src/wp-admin/includes/meta-boxes.php +++ b/src/wp-admin/includes/meta-boxes.php @@ -469,10 +469,20 @@ function attachment_submit_meta_box( $post ) { ID ) ) { if ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) { - echo "" . __( 'Move to Trash' ) . ''; + printf( + '%2$s', + get_delete_post_link( $post->ID ), + __( 'Move to Trash' ) + ); } else { - $delete_ays = ! MEDIA_TRASH ? " onclick='return showNotice.warn();'" : ''; - echo "" . __( 'Delete permanently' ) . ''; + $show_confirmation = ! MEDIA_TRASH ? " onclick='return showNotice.warn();'" : ''; + + printf( + '%3$s', + $show_confirmation, + get_delete_post_link( $post->ID, '', true ), + __( 'Delete permanently' ) + ); } } ?>