[ 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=' . ( isset( $_GET['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=' . ( isset( $_GET['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 arrow buttons at the top of the dialog, or the left and 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          $post_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type='attachment' AND post_status = 'trash'" );
 266          $doaction = 'delete';
 267      } elseif ( isset( $_REQUEST['media'] ) ) {
 268          $post_ids = $_REQUEST['media'];
 269      } elseif ( isset( $_REQUEST['ids'] ) ) {
 270          $post_ids = explode( ',', $_REQUEST['ids'] );
 271      }
 272      $post_ids = array_map( 'intval', (array) $post_ids );
 273  
 274      $location = 'upload.php';
 275      $referer  = wp_get_referer();
 276      if ( $referer ) {
 277          if ( str_contains( $referer, 'upload.php' ) ) {
 278              $location = remove_query_arg( array( 'trashed', 'untrashed', 'deleted', 'message', 'ids', 'posted' ), $referer );
 279          }
 280      }
 281  
 282      switch ( $doaction ) {
 283          case 'detach':
 284              wp_media_attach_action( $_REQUEST['parent_post_id'], 'detach' );
 285              break;
 286  
 287          case 'attach':
 288              wp_media_attach_action( $_REQUEST['found_post_id'] );
 289              break;
 290  
 291          case 'trash':
 292              if ( empty( $post_ids ) ) {
 293                  break;
 294              }
 295              foreach ( $post_ids as $post_id ) {
 296                  if ( ! current_user_can( 'delete_post', $post_id ) ) {
 297                      wp_die( __( 'Sorry, you are not allowed to move this item to the Trash.' ) );
 298                  }
 299  
 300                  if ( ! wp_trash_post( $post_id ) ) {
 301                      wp_die( __( 'Error in moving the item to Trash.' ) );
 302                  }
 303              }
 304              $location = add_query_arg(
 305                  array(
 306                      'trashed' => count( $post_ids ),
 307                      'ids'     => implode( ',', $post_ids ),
 308                  ),
 309                  $location
 310              );
 311              break;
 312          case 'untrash':
 313              if ( empty( $post_ids ) ) {
 314                  break;
 315              }
 316              foreach ( $post_ids as $post_id ) {
 317                  if ( ! current_user_can( 'delete_post', $post_id ) ) {
 318                      wp_die( __( 'Sorry, you are not allowed to restore this item from the Trash.' ) );
 319                  }
 320  
 321                  if ( ! wp_untrash_post( $post_id ) ) {
 322                      wp_die( __( 'Error in restoring the item from Trash.' ) );
 323                  }
 324              }
 325              $location = add_query_arg( 'untrashed', count( $post_ids ), $location );
 326              break;
 327          case 'delete':
 328              if ( empty( $post_ids ) ) {
 329                  break;
 330              }
 331              foreach ( $post_ids as $post_id_del ) {
 332                  if ( ! current_user_can( 'delete_post', $post_id_del ) ) {
 333                      wp_die( __( 'Sorry, you are not allowed to delete this item.' ) );
 334                  }
 335  
 336                  if ( ! wp_delete_attachment( $post_id_del ) ) {
 337                      wp_die( __( 'Error in deleting the attachment.' ) );
 338                  }
 339              }
 340              $location = add_query_arg( 'deleted', count( $post_ids ), $location );
 341              break;
 342          default:
 343              $screen = get_current_screen()->id;
 344  
 345              /** This action is documented in wp-admin/edit.php */
 346              $location = apply_filters( "handle_bulk_actions-{$screen}", $location, $doaction, $post_ids ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
 347      }
 348  
 349      wp_redirect( $location );
 350      exit;
 351  } elseif ( ! empty( $_GET['_wp_http_referer'] ) ) {
 352      wp_redirect( remove_query_arg( array( '_wp_http_referer', '_wpnonce' ), wp_unslash( $_SERVER['REQUEST_URI'] ) ) );
 353      exit;
 354  }
 355  
 356  $wp_list_table->prepare_items();
 357  
 358  // Used in the HTML title tag.
 359  $title       = __( 'Media Library' );
 360  $parent_file = 'upload.php';
 361  
 362  wp_enqueue_script( 'media' );
 363  
 364  add_screen_option( 'per_page' );
 365  
 366  get_current_screen()->add_help_tab(
 367      array(
 368          'id'      => 'overview',
 369          'title'   => __( 'Overview' ),
 370          'content' =>
 371                  '<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>' .
 372                  '<p>' . __( 'You can narrow the list by file type/status or by date using the dropdown menus above the media table.' ) . '</p>' .
 373                  '<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>',
 374      )
 375  );
 376  get_current_screen()->add_help_tab(
 377      array(
 378          'id'      => 'actions-links',
 379          'title'   => __( 'Available Actions' ),
 380          'content' =>
 381                  '<p>' . __( 'Hovering over a row reveals action links that allow you to manage media items. You can perform the following actions:' ) . '</p>' .
 382                  '<ul>' .
 383                      '<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>' .
 384                      '<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>' .
 385                      '<li>' . __( '<strong>View</strong> will take you to a public display page for that file.' ) . '</li>' .
 386                      '<li>' . __( '<strong>Copy URL</strong> copies the URL for the media file to your clipboard.' ) . '</li>' .
 387                      '<li>' . __( '<strong>Download file</strong> downloads the original media file to your device.' ) . '</li>' .
 388                  '</ul>',
 389      )
 390  );
 391  get_current_screen()->add_help_tab(
 392      array(
 393          'id'      => 'attaching-files',
 394          'title'   => __( 'Attaching Files' ),
 395          'content' =>
 396                  '<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>',
 397      )
 398  );
 399  
 400  get_current_screen()->set_help_sidebar(
 401      '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
 402      '<p>' . __( '<a href="https://wordpress.org/documentation/article/media-library-screen/">Documentation on Media Library</a>' ) . '</p>' .
 403      '<p>' . __( '<a href="https://wordpress.org/support/forums/">Support forums</a>' ) . '</p>'
 404  );
 405  
 406  get_current_screen()->set_screen_reader_content(
 407      array(
 408          'heading_views'      => __( 'Filter media items list' ),
 409          'heading_pagination' => __( 'Media items list navigation' ),
 410          'heading_list'       => __( 'Media items list' ),
 411      )
 412  );
 413  
 414  require_once  ABSPATH . 'wp-admin/admin-header.php';
 415  ?>
 416  
 417  <div class="wrap">
 418  <h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1>
 419  
 420  <?php
 421  if ( current_user_can( 'upload_files' ) ) {
 422      ?>
 423      <a href="<?php echo esc_url( admin_url( 'media-new.php' ) ); ?>" class="page-title-action"><?php echo esc_html__( 'Add Media File' ); ?></a>
 424                          <?php
 425  }
 426  
 427  if ( isset( $_REQUEST['s'] ) && strlen( $_REQUEST['s'] ) ) {
 428      echo '<span class="subtitle">';
 429      printf(
 430          /* translators: %s: Search query. */
 431          __( 'Search results for: %s' ),
 432          '<strong>' . get_search_query() . '</strong>'
 433      );
 434      echo '</span>';
 435  }
 436  ?>
 437  
 438  <hr class="wp-header-end">
 439  
 440  <?php
 441  if ( ! empty( $message ) ) {
 442      wp_admin_notice(
 443          $message,
 444          array(
 445              'id'                 => 'message',
 446              'additional_classes' => array( 'updated' ),
 447              'dismissible'        => true,
 448          )
 449      );
 450  }
 451  ?>
 452  
 453  <form id="posts-filter" method="get">
 454  
 455  <?php $wp_list_table->views(); ?>
 456  
 457  <?php $wp_list_table->display(); ?>
 458  
 459  <div id="ajax-response"></div>
 460  <?php find_posts_div(); ?>
 461  </form>
 462  </div>
 463  
 464  <?php
 465  require_once  ABSPATH . 'wp-admin/admin-footer.php';


Generated : Mon Dec 8 08:20:05 2025 Cross-referenced by PHPXref