Make WordPress Core

Changeset 55178


Ignore:
Timestamp:
02/01/2023 07:45:53 PM (16 months ago)
Author:
joedolson
Message:

Media: Fix upload not visible if added after deleting media.

Unset GET parameters in the media library so they don't suppress error notifications and messages. Fix an issue where uploading an image after being redirected from deleting media did not show the new upload in the gallery.

Props rnitinb, Mista-Flo, desrosj, zebaafiashama, joedolson, antpb.
Fixes #49178.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/upload.php

    r53587 r55178  
    1212if ( ! current_user_can( 'upload_files' ) ) {
    1313    wp_die( __( 'Sorry, you are not allowed to upload files.' ) );
     14}
     15
     16$message = '';
     17if ( ! empty( $_GET['posted'] ) ) {
     18    $message                = __( 'Media file updated.' );
     19    $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'posted' ), $_SERVER['REQUEST_URI'] );
     20    unset( $_GET['posted'] );
     21}
     22
     23if ( ! empty( $_GET['attached'] ) && absint( $_GET['attached'] ) ) {
     24    $attached = absint( $_GET['attached'] );
     25    if ( 1 === $attached ) {
     26        $message = __( 'Media file attached.' );
     27    } else {
     28        /* translators: %s: Number of media files. */
     29        $message = _n( '%s media file attached.', '%s media files attached.', $attached );
     30    }
     31    $message                = sprintf( $message, $attached );
     32    $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'detach', 'attached' ), $_SERVER['REQUEST_URI'] );
     33    unset( $_GET['detach'], $_GET['attached'] );
     34}
     35
     36if ( ! empty( $_GET['detach'] ) && absint( $_GET['detach'] ) ) {
     37    $detached = absint( $_GET['detach'] );
     38    if ( 1 === $detached ) {
     39        $message = __( 'Media file detached.' );
     40    } else {
     41        /* translators: %s: Number of media files. */
     42        $message = _n( '%s media file detached.', '%s media files detached.', $detached );
     43    }
     44    $message                = sprintf( $message, $detached );
     45    $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'detach', 'attached' ), $_SERVER['REQUEST_URI'] );
     46    unset( $_GET['detach'], $_GET['attached'] );
     47}
     48
     49if ( ! empty( $_GET['deleted'] ) && absint( $_GET['deleted'] ) ) {
     50    $deleted = absint( $_GET['deleted'] );
     51    if ( 1 === $deleted ) {
     52        $message = __( 'Media file permanently deleted.' );
     53    } else {
     54        /* translators: %s: Number of media files. */
     55        $message = _n( '%s media file permanently deleted.', '%s media files permanently deleted.', $deleted );
     56    }
     57    $message                = sprintf( $message, $deleted );
     58    $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'deleted' ), $_SERVER['REQUEST_URI'] );
     59    unset( $_GET['deleted'] );
     60}
     61
     62if ( ! empty( $_GET['trashed'] ) && absint( $_GET['trashed'] ) ) {
     63    $trashed = absint( $_GET['trashed'] );
     64    if ( 1 === $trashed ) {
     65        $message = __( 'Media file moved to the Trash.' );
     66    } else {
     67        /* translators: %s: Number of media files. */
     68        $message = _n( '%s media file moved to the Trash.', '%s media files moved to the Trash.', $trashed );
     69    }
     70    $message                = sprintf( $message, $trashed );
     71    $message               .= ' <a href="' . esc_url( wp_nonce_url( 'upload.php?doaction=undo&action=untrash&ids=' . ( isset( $_GET['ids'] ) ? $_GET['ids'] : '' ), 'bulk-media' ) ) . '">' . __( 'Undo' ) . '</a>';
     72    $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'trashed' ), $_SERVER['REQUEST_URI'] );
     73    unset( $_GET['trashed'] );
     74}
     75
     76if ( ! empty( $_GET['untrashed'] ) && absint( $_GET['untrashed'] ) ) {
     77    $untrashed = absint( $_GET['untrashed'] );
     78    if ( 1 === $untrashed ) {
     79        $message = __( 'Media file restored from the Trash.' );
     80    } else {
     81        /* translators: %s: Number of media files. */
     82        $message = _n( '%s media file restored from the Trash.', '%s media files restored from the Trash.', $untrashed );
     83    }
     84    $message                = sprintf( $message, $untrashed );
     85    $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'untrashed' ), $_SERVER['REQUEST_URI'] );
     86    unset( $_GET['untrashed'] );
     87}
     88
     89$messages[1] = __( 'Media file updated.' );
     90$messages[2] = __( 'Media file permanently deleted.' );
     91$messages[3] = __( 'Error saving media file.' );
     92$messages[4] = __( 'Media file moved to the Trash.' ) . ' <a href="' . esc_url( wp_nonce_url( 'upload.php?doaction=undo&action=untrash&ids=' . ( isset( $_GET['ids'] ) ? $_GET['ids'] : '' ), 'bulk-media' ) ) . '">' . __( 'Undo' ) . '</a>';
     93$messages[5] = __( 'Media file restored from the Trash.' );
     94
     95if ( ! empty( $_GET['message'] ) && isset( $messages[ $_GET['message'] ] ) ) {
     96    $message                = $messages[ $_GET['message'] ];
     97    $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'message' ), $_SERVER['REQUEST_URI'] );
    1498}
    1599
     
    95179
    96180        <hr class="wp-header-end">
     181
     182        <?php if ( ! empty( $message ) ) : ?>
     183            <div id="message" class="updated notice is-dismissible"><p><?php echo $message; ?></p></div>
     184        <?php endif; ?>
    97185
    98186        <div class="error hide-if-js">
     
    298386<hr class="wp-header-end">
    299387
    300 <?php
    301 $message = '';
    302 if ( ! empty( $_GET['posted'] ) ) {
    303     $message                = __( 'Media file updated.' );
    304     $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'posted' ), $_SERVER['REQUEST_URI'] );
    305 }
    306 
    307 if ( ! empty( $_GET['attached'] ) && absint( $_GET['attached'] ) ) {
    308     $attached = absint( $_GET['attached'] );
    309     if ( 1 === $attached ) {
    310         $message = __( 'Media file attached.' );
    311     } else {
    312         /* translators: %s: Number of media files. */
    313         $message = _n( '%s media file attached.', '%s media files attached.', $attached );
    314     }
    315     $message                = sprintf( $message, number_format_i18n( $attached ) );
    316     $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'detach', 'attached' ), $_SERVER['REQUEST_URI'] );
    317 }
    318 
    319 if ( ! empty( $_GET['detach'] ) && absint( $_GET['detach'] ) ) {
    320     $detached = absint( $_GET['detach'] );
    321     if ( 1 === $detached ) {
    322         $message = __( 'Media file detached.' );
    323     } else {
    324         /* translators: %s: Number of media files. */
    325         $message = _n( '%s media file detached.', '%s media files detached.', $detached );
    326     }
    327     $message                = sprintf( $message, number_format_i18n( $detached ) );
    328     $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'detach', 'attached' ), $_SERVER['REQUEST_URI'] );
    329 }
    330 
    331 if ( ! empty( $_GET['deleted'] ) && absint( $_GET['deleted'] ) ) {
    332     $deleted = absint( $_GET['deleted'] );
    333     if ( 1 === $deleted ) {
    334         $message = __( 'Media file permanently deleted.' );
    335     } else {
    336         /* translators: %s: Number of media files. */
    337         $message = _n( '%s media file permanently deleted.', '%s media files permanently deleted.', $deleted );
    338     }
    339     $message                = sprintf( $message, number_format_i18n( $deleted ) );
    340     $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'deleted' ), $_SERVER['REQUEST_URI'] );
    341 }
    342 
    343 if ( ! empty( $_GET['trashed'] ) && absint( $_GET['trashed'] ) ) {
    344     $trashed = absint( $_GET['trashed'] );
    345     if ( 1 === $trashed ) {
    346         $message = __( 'Media file moved to the Trash.' );
    347     } else {
    348         /* translators: %s: Number of media files. */
    349         $message = _n( '%s media file moved to the Trash.', '%s media files moved to the Trash.', $trashed );
    350     }
    351     $message                = sprintf( $message, number_format_i18n( $trashed ) );
    352     $message               .= ' <a href="' . esc_url( wp_nonce_url( 'upload.php?doaction=undo&action=untrash&ids=' . ( isset( $_GET['ids'] ) ? $_GET['ids'] : '' ), 'bulk-media' ) ) . '">' . __( 'Undo' ) . '</a>';
    353     $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'trashed' ), $_SERVER['REQUEST_URI'] );
    354 }
    355 
    356 if ( ! empty( $_GET['untrashed'] ) && absint( $_GET['untrashed'] ) ) {
    357     $untrashed = absint( $_GET['untrashed'] );
    358     if ( 1 === $untrashed ) {
    359         $message = __( 'Media file restored from the Trash.' );
    360     } else {
    361         /* translators: %s: Number of media files. */
    362         $message = _n( '%s media file restored from the Trash.', '%s media files restored from the Trash.', $untrashed );
    363     }
    364     $message                = sprintf( $message, number_format_i18n( $untrashed ) );
    365     $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'untrashed' ), $_SERVER['REQUEST_URI'] );
    366 }
    367 
    368 $messages[1] = __( 'Media file updated.' );
    369 $messages[2] = __( 'Media file permanently deleted.' );
    370 $messages[3] = __( 'Error saving media file.' );
    371 $messages[4] = __( 'Media file moved to the Trash.' ) . ' <a href="' . esc_url( wp_nonce_url( 'upload.php?doaction=undo&action=untrash&ids=' . ( isset( $_GET['ids'] ) ? $_GET['ids'] : '' ), 'bulk-media' ) ) . '">' . __( 'Undo' ) . '</a>';
    372 $messages[5] = __( 'Media file restored from the Trash.' );
    373 
    374 if ( ! empty( $_GET['message'] ) && isset( $messages[ $_GET['message'] ] ) ) {
    375     $message                = $messages[ $_GET['message'] ];
    376     $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'message' ), $_SERVER['REQUEST_URI'] );
    377 }
    378 
    379 if ( ! empty( $message ) ) {
    380     ?>
     388<?php if ( ! empty( $message ) ) : ?>
    381389<div id="message" class="updated notice is-dismissible"><p><?php echo $message; ?></p></div>
    382 <?php } ?>
     390<?php endif; ?>
    383391
    384392<form id="posts-filter" method="get">
Note: See TracChangeset for help on using the changeset viewer.