[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/ -> upload.php (source)

   1  <?php
   2  /**
   3   * Media Library administration panel.
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   */
   8  
   9  /** WordPress Administration Bootstrap */
  10  require_once  __DIR__ . '/admin.php';
  11  
  12  if ( ! current_user_can( 'upload_files' ) ) {
  13      wp_die( __( 'Sorry, you are not allowed to upload files.' ) );
  14  }
  15  
  16  $message = '';
  17  if ( ! empty( $_GET['posted'] ) ) {
  18      $message = __( 'Media file updated.' );
  19  
  20      $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'posted' ), $_SERVER['REQUEST_URI'] );
  21      unset( $_GET['posted'] );
  22  }
  23  
  24  if ( ! empty( $_GET['attached'] ) && absint( $_GET['attached'] ) ) {
  25      $attached = absint( $_GET['attached'] );
  26  
  27      if ( 1 === $attached ) {
  28          $message = __( 'Media file attached.' );
  29      } else {
  30          $message = sprintf(
  31              /* translators: %s: Number of media files. */
  32              _n( '%s media file attached.', '%s media files attached.', $attached ),
  33              number_format_i18n( $attached )
  34          );
  35      }
  36  
  37      $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'detach', 'attached' ), $_SERVER['REQUEST_URI'] );
  38      unset( $_GET['detach'], $_GET['attached'] );
  39  }
  40  
  41  if ( ! empty( $_GET['detach'] ) && absint( $_GET['detach'] ) ) {
  42      $detached = absint( $_GET['detach'] );
  43  
  44      if ( 1 === $detached ) {
  45          $message = __( 'Media file detached.' );
  46      } else {
  47          $message = sprintf(
  48              /* translators: %s: Number of media files. */
  49              _n( '%s media file detached.', '%s media files detached.', $detached ),
  50              number_format_i18n( $detached )
  51          );
  52      }
  53  
  54      $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'detach', 'attached' ), $_SERVER['REQUEST_URI'] );
  55      unset( $_GET['detach'], $_GET['attached'] );
  56  }
  57  
  58  if ( ! empty( $_GET['deleted'] ) && absint( $_GET['deleted'] ) ) {
  59      $deleted = absint( $_GET['deleted'] );
  60  
  61      if ( 1 === $deleted ) {
  62          $message = __( 'Media file permanently deleted.' );
  63      } else {
  64          $message = sprintf(
  65              /* translators: %s: Number of media files. */
  66              _n( '%s media file permanently deleted.', '%s media files permanently deleted.', $deleted ),
  67              number_format_i18n( $deleted )
  68          );
  69      }
  70  
  71      $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'deleted' ), $_SERVER['REQUEST_URI'] );
  72      unset( $_GET['deleted'] );
  73  }
  74  
  75  if ( ! empty( $_GET['trashed'] ) && absint( $_GET['trashed'] ) ) {
  76      $trashed = absint( $_GET['trashed'] );
  77  
  78      if ( 1 === $trashed ) {
  79          $message = __( 'Media file moved to the Trash.' );
  80      } else {
  81          $message = sprintf(
  82              /* translators: %s: Number of media files. */
  83              _n( '%s media file moved to the Trash.', '%s media files moved to the Trash.', $trashed ),
  84              number_format_i18n( $trashed )
  85          );
  86      }
  87  
  88      $message .= sprintf(
  89          ' <a href="%1$s">%2$s</a>',
  90          esc_url( wp_nonce_url( 'upload.php?doaction=undo&action=untrash&ids=' . ( $_GET['ids'] ?? '' ), 'bulk-media' ) ),
  91          __( 'Undo' )
  92      );
  93  
  94      $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'trashed' ), $_SERVER['REQUEST_URI'] );
  95      unset( $_GET['trashed'] );
  96  }
  97  
  98  if ( ! empty( $_GET['untrashed'] ) && absint( $_GET['untrashed'] ) ) {
  99      $untrashed = absint( $_GET['untrashed'] );
 100  
 101      if ( 1 === $untrashed ) {
 102          $message = __( 'Media file restored from the Trash.' );
 103      } else {
 104          $message = sprintf(
 105              /* translators: %s: Number of media files. */
 106              _n( '%s media file restored from the Trash.', '%s media files restored from the Trash.', $untrashed ),
 107              number_format_i18n( $untrashed )
 108          );
 109      }
 110  
 111      $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'untrashed' ), $_SERVER['REQUEST_URI'] );
 112      unset( $_GET['untrashed'] );
 113  }
 114  
 115  $messages[1] = __( 'Media file updated.' );
 116  $messages[2] = __( 'Media file permanently deleted.' );
 117  $messages[3] = __( 'Error saving media file.' );
 118  $messages[4] = __( 'Media file moved to the Trash.' ) . sprintf(
 119      ' <a href="%1$s">%2$s</a>',
 120      esc_url( wp_nonce_url( 'upload.php?doaction=undo&action=untrash&ids=' . ( $_GET['ids'] ?? '' ), 'bulk-media' ) ),
 121      __( 'Undo' )
 122  );
 123  $messages[5] = __( 'Media file restored from the Trash.' );
 124  
 125  if ( ! empty( $_GET['message'] ) && isset( $messages[ $_GET['message'] ] ) ) {
 126      $message = $messages[ $_GET['message'] ];
 127  
 128      $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'message' ), $_SERVER['REQUEST_URI'] );
 129  }
 130  
 131  $modes = array( 'grid', 'list' );
 132  
 133  if ( isset( $_GET['mode'] ) && in_array( $_GET['mode'], $modes, true ) ) {
 134      $mode = $_GET['mode'];
 135      update_user_option( get_current_user_id(), 'media_library_mode', $mode );
 136  } else {
 137      $mode = get_user_option( 'media_library_mode', get_current_user_id() ) ? get_user_option( 'media_library_mode', get_current_user_id() ) : 'grid';
 138  }
 139  
 140  if ( 'grid' === $mode ) {
 141      wp_enqueue_media();
 142      wp_enqueue_script( 'media-grid' );
 143      wp_enqueue_script( 'media' );
 144  
 145      // Remove the error parameter added by deprecation of wp-admin/media.php.
 146      add_filter(
 147          'removable_query_args',
 148          function () {
 149              return array( 'error' );
 150          },
 151          10,
 152          0
 153      );
 154  
 155      $query_string = $_GET;
 156      // Let JS handle this.
 157      unset( $query_string['s'] );
 158      $query_vars = wp_edit_attachments_query_vars( $query_string );
 159      $ignore     = array( 'mode', 'post_type', 'post_status', 'posts_per_page' );
 160  
 161      foreach ( $query_vars as $key => $value ) {
 162          if ( ! $value || in_array( $key, $ignore, true ) ) {
 163              unset( $query_vars[ $key ] );
 164          }
 165      }
 166  
 167      wp_localize_script(
 168          'media-grid',
 169          '_wpMediaGridSettings',
 170          array(
 171              'adminUrl'  => parse_url( self_admin_url(), PHP_URL_PATH ),
 172              'queryVars' => (object) $query_vars,
 173          )
 174      );
 175  
 176      get_current_screen()->add_help_tab(
 177          array(
 178              'id'      => 'overview',
 179              'title'   => __( 'Overview' ),
 180              'content' =>
 181                  '<p>' . __( 'All the files you&#8217;ve uploaded are listed in the Media Library, with the most recent uploads listed first.' ) . '</p>' .
 182                  '<p>' . __( 'You can view your media in a simple visual grid or a list with columns. Switch between these views using the icons to the left above the media.' ) . '</p>' .
 183                  '<p>' . __( 'To delete media items, click the <strong>Bulk select</strong> button at the top of the screen. Select any items you wish to delete, then click the <strong>Delete permanently</strong> button. Clicking the <strong>Cancel</strong> button takes you back to viewing your media.' ) . '</p>',
 184          )
 185      );
 186  
 187      get_current_screen()->add_help_tab(
 188          array(
 189              'id'      => 'attachment-details',
 190              'title'   => __( 'Attachment Details' ),
 191              'content' =>
 192                  '<p>' . __( 'Clicking an item will display an Attachment Details dialog, which allows you to preview media and make quick edits. Any changes you make to the attachment details will be automatically saved.' ) . '</p>' .
 193                  '<p>' . __( 'Use the buttons at the top of the dialog, or <code>alt</code>/<code>option</code> plus the left or right arrow keys on your keyboard, to navigate between media items quickly.' ) . '</p>' .
 194                  '<p>' . __( 'You can also delete individual items and access the extended edit screen from the details dialog.' ) . '</p>',
 195          )
 196      );
 197  
 198      get_current_screen()->set_help_sidebar(
 199          '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
 200          '<p>' . __( '<a href="https://wordpress.org/documentation/article/media-library-screen/">Documentation on Media Library</a>' ) . '</p>' .
 201          '<p>' . __( '<a href="https://wordpress.org/support/forums/">Support forums</a>' ) . '</p>'
 202      );
 203  
 204      // Used in the HTML title tag.
 205      $title       = __( 'Media Library' );
 206      $parent_file = 'upload.php';
 207  
 208      require_once  ABSPATH . 'wp-admin/admin-header.php';
 209      ?>
 210      <div class="wrap" id="wp-media-grid" data-search="<?php _admin_search_query(); ?>">
 211          <h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1>
 212  
 213          <?php
 214          if ( current_user_can( 'upload_files' ) ) {
 215              ?>
 216              <a href="<?php echo esc_url( admin_url( 'media-new.php' ) ); ?>" class="page-title-action aria-button-if-js"><?php echo esc_html__( 'Add Media File' ); ?></a>
 217              <?php
 218          }
 219          ?>
 220  
 221          <hr class="wp-header-end">
 222  
 223          <?php
 224          if ( ! empty( $message ) ) {
 225              wp_admin_notice(
 226                  $message,
 227                  array(
 228                      'id'                 => 'message',
 229                      'additional_classes' => array( 'updated' ),
 230                      'dismissible'        => true,
 231                  )
 232              );
 233          }
 234  
 235          $js_required_message = sprintf(
 236              /* translators: %s: List view URL. */
 237              __( 'The grid view for the Media Library requires JavaScript. <a href="%s">Switch to the list view</a>.' ),
 238              'upload.php?mode=list'
 239          );
 240          wp_admin_notice(
 241              $js_required_message,
 242              array(
 243                  'additional_classes' => array( 'error', 'hide-if-js' ),
 244              )
 245          );
 246          ?>
 247      </div>
 248      <?php
 249      require_once  ABSPATH . 'wp-admin/admin-footer.php';
 250      exit;
 251  }
 252  
 253  $wp_list_table = _get_list_table( 'WP_Media_List_Table' );
 254  $pagenum       = $wp_list_table->get_pagenum();
 255  
 256  // Handle bulk actions.
 257  $doaction = $wp_list_table->current_action();
 258  
 259  if ( $doaction ) {
 260      check_admin_referer( 'bulk-media' );
 261  
 262      $post_ids = array();
 263  
 264      if ( 'delete_all' === $doaction ) {
 265          /**
 266           * @global wpdb $wpdb WordPress database abstraction object.
 267           */
 268          global $wpdb;
 269  
 270          $post_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type='attachment' AND post_status = 'trash'" );
 271          $doaction = 'delete';
 272      } elseif ( isset( $_REQUEST['media'] ) ) {
 273          $post_ids = $_REQUEST['media'];
 274      } elseif ( isset( $_REQUEST['ids'] ) ) {
 275          $post_ids = explode( ',', $_REQUEST['ids'] );
 276      }
 277      $post_ids = array_map( 'intval', (array) $post_ids );
 278  
 279      $location = 'upload.php';
 280      $referer  = wp_get_referer();
 281      if ( $referer ) {
 282          if ( str_contains( $referer, 'upload.php' ) ) {
 283              $location = remove_query_arg( array( 'trashed', 'untrashed', 'deleted', 'message', 'ids', 'posted' ), $referer );
 284          }
 285      }
 286  
 287      switch ( $doaction ) {
 288          case 'detach':
 289              wp_media_attach_action( $_REQUEST['parent_post_id'], 'detach' );
 290              break;
 291  
 292          case 'attach':
 293              wp_media_attach_action( $_REQUEST['found_post_id'] );
 294              break;
 295  
 296          case 'trash':
 297              if ( empty( $post_ids ) ) {
 298                  break;
 299              }
 300              foreach ( $post_ids as $post_id ) {
 301                  if ( ! current_user_can( 'delete_post', $post_id ) ) {
 302                      wp_die( __( 'Sorry, you are not allowed to move this item to the Trash.' ) );
 303                  }
 304  
 305                  if ( ! wp_trash_post( $post_id ) ) {
 306                      wp_die( __( 'Error in moving the item to Trash.' ) );
 307                  }
 308              }
 309              $location = add_query_arg(
 310                  array(
 311                      'trashed' => count( $post_ids ),
 312                      'ids'     => implode( ',', $post_ids ),
 313                  ),
 314                  $location
 315              );
 316              break;
 317          case 'untrash':
 318              if ( empty( $post_ids ) ) {
 319                  break;
 320              }
 321              foreach ( $post_ids as $post_id ) {
 322                  if ( ! current_user_can( 'delete_post', $post_id ) ) {
 323                      wp_die( __( 'Sorry, you are not allowed to restore this item from the Trash.' ) );
 324                  }
 325  
 326                  if ( ! wp_untrash_post( $post_id ) ) {
 327                      wp_die( __( 'Error in restoring the item from Trash.' ) );
 328                  }
 329              }
 330              $location = add_query_arg( 'untrashed', count( $post_ids ), $location );
 331              break;
 332          case 'delete':
 333              if ( empty( $post_ids ) ) {
 334                  break;
 335              }
 336              foreach ( $post_ids as $post_id_del ) {
 337                  if ( ! current_user_can( 'delete_post', $post_id_del ) ) {
 338                      wp_die( __( 'Sorry, you are not allowed to delete this item.' ) );
 339                  }
 340  
 341                  if ( ! wp_delete_attachment( $post_id_del ) ) {
 342                      wp_die( __( 'Error in deleting the attachment.' ) );
 343                  }
 344              }
 345              $location = add_query_arg( 'deleted', count( $post_ids ), $location );
 346              break;
 347          default:
 348              $screen = get_current_screen()->id;
 349  
 350              /** This action is documented in wp-admin/edit.php */
 351              $location = apply_filters( "handle_bulk_actions-{$screen}", $location, $doaction, $post_ids ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
 352      }
 353  
 354      wp_redirect( $location );
 355      exit;
 356  } elseif ( ! empty( $_GET['_wp_http_referer'] ) ) {
 357      wp_redirect( remove_query_arg( array( '_wp_http_referer', '_wpnonce' ), wp_unslash( $_SERVER['REQUEST_URI'] ) ) );
 358      exit;
 359  }
 360  
 361  $wp_list_table->prepare_items();
 362  
 363  // Used in the HTML title tag.
 364  $title       = __( 'Media Library' );
 365  $parent_file = 'upload.php';
 366  
 367  wp_enqueue_script( 'media' );
 368  
 369  add_screen_option( 'per_page' );
 370  
 371  get_current_screen()->add_help_tab(
 372      array(
 373          'id'      => 'overview',
 374          'title'   => __( 'Overview' ),
 375          'content' =>
 376                  '<p>' . __( 'All the files you&#8217;ve uploaded are listed in the Media Library, with the most recent uploads listed first. You can use the Screen Options tab to customize the display of this screen.' ) . '</p>' .
 377                  '<p>' . __( 'You can narrow the list by file type/status or by date using the dropdown menus above the media table.' ) . '</p>' .
 378                  '<p>' . __( 'You can view your media in a simple visual grid or a list with columns. Switch between these views using the icons to the left above the media.' ) . '</p>',
 379      )
 380  );
 381  get_current_screen()->add_help_tab(
 382      array(
 383          'id'      => 'actions-links',
 384          'title'   => __( 'Available Actions' ),
 385          'content' =>
 386                  '<p>' . __( 'Hovering over a row reveals action links that allow you to manage media items. You can perform the following actions:' ) . '</p>' .
 387                  '<ul>' .
 388                      '<li>' . __( '<strong>Edit</strong> takes you to a simple screen to edit that individual file&#8217;s metadata. You can also reach that screen by clicking on the media file name or thumbnail.' ) . '</li>' .
 389                      '<li>' . __( '<strong>Delete Permanently</strong> will delete the file from the media library (as well as from any posts to which it is currently attached).' ) . '</li>' .
 390                      '<li>' . __( '<strong>View</strong> will take you to a public display page for that file.' ) . '</li>' .
 391                      '<li>' . __( '<strong>Copy URL</strong> copies the URL for the media file to your clipboard.' ) . '</li>' .
 392                      '<li>' . __( '<strong>Download file</strong> downloads the original media file to your device.' ) . '</li>' .
 393                  '</ul>',
 394      )
 395  );
 396  get_current_screen()->add_help_tab(
 397      array(
 398          'id'      => 'attaching-files',
 399          'title'   => __( 'Attaching Files' ),
 400          'content' =>
 401                  '<p>' . __( 'If a media file has not been attached to any content, you will see that in the Uploaded To column, and can click on Attach to launch a small popup that will allow you to search for existing content and attach the file.' ) . '</p>',
 402      )
 403  );
 404  
 405  get_current_screen()->set_help_sidebar(
 406      '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
 407      '<p>' . __( '<a href="https://wordpress.org/documentation/article/media-library-screen/">Documentation on Media Library</a>' ) . '</p>' .
 408      '<p>' . __( '<a href="https://wordpress.org/support/forums/">Support forums</a>' ) . '</p>'
 409  );
 410  
 411  get_current_screen()->set_screen_reader_content(
 412      array(
 413          'heading_views'      => __( 'Filter media items list' ),
 414          'heading_pagination' => __( 'Media items list navigation' ),
 415          'heading_list'       => __( 'Media items list' ),
 416      )
 417  );
 418  
 419  require_once  ABSPATH . 'wp-admin/admin-header.php';
 420  ?>
 421  
 422  <div class="wrap">
 423  <h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1>
 424  
 425  <?php
 426  if ( current_user_can( 'upload_files' ) ) {
 427      ?>
 428      <a href="<?php echo esc_url( admin_url( 'media-new.php' ) ); ?>" class="page-title-action"><?php echo esc_html__( 'Add Media File' ); ?></a>
 429                          <?php
 430  }
 431  
 432  if ( isset( $_REQUEST['s'] ) && strlen( $_REQUEST['s'] ) ) {
 433      echo '<span class="subtitle">';
 434      printf(
 435          /* translators: %s: Search query. */
 436          __( 'Search results for: %s' ),
 437          '<strong>' . get_search_query() . '</strong>'
 438      );
 439      echo '</span>';
 440  }
 441  ?>
 442  
 443  <hr class="wp-header-end">
 444  
 445  <?php
 446  if ( ! empty( $message ) ) {
 447      wp_admin_notice(
 448          $message,
 449          array(
 450              'id'                 => 'message',
 451              'additional_classes' => array( 'updated' ),
 452              'dismissible'        => true,
 453          )
 454      );
 455  }
 456  ?>
 457  
 458  <form id="posts-filter" method="get">
 459  
 460  <?php $wp_list_table->views(); ?>
 461  
 462  <?php $wp_list_table->display(); ?>
 463  
 464  <div id="ajax-response"></div>
 465  <?php find_posts_div(); ?>
 466  </form>
 467  </div>
 468  
 469  <?php
 470  require_once  ABSPATH . 'wp-admin/admin-footer.php';


Generated : Sat Oct 3 08:20:34 2026 Cross-referenced by PHPXref