[ Index ]

PHP Cross Reference of WordPress

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  $mode  = get_user_option( 'media_library_mode', get_current_user_id() ) ? get_user_option( 'media_library_mode', get_current_user_id() ) : 'grid';
  17  $modes = array( 'grid', 'list' );
  18  
  19  if ( isset( $_GET['mode'] ) && in_array( $_GET['mode'], $modes, true ) ) {
  20      $mode = $_GET['mode'];
  21      update_user_option( get_current_user_id(), 'media_library_mode', $mode );
  22  }
  23  
  24  if ( 'grid' === $mode ) {
  25      wp_enqueue_media();
  26      wp_enqueue_script( 'media-grid' );
  27      wp_enqueue_script( 'media' );
  28  
  29      remove_action( 'admin_head', 'wp_admin_canonical_url' );
  30  
  31      $q = $_GET;
  32      // Let JS handle this.
  33      unset( $q['s'] );
  34      $vars   = wp_edit_attachments_query_vars( $q );
  35      $ignore = array( 'mode', 'post_type', 'post_status', 'posts_per_page' );
  36      foreach ( $vars as $key => $value ) {
  37          if ( ! $value || in_array( $key, $ignore, true ) ) {
  38              unset( $vars[ $key ] );
  39          }
  40      }
  41  
  42      wp_localize_script(
  43          'media-grid',
  44          '_wpMediaGridSettings',
  45          array(
  46              'adminUrl'  => parse_url( self_admin_url(), PHP_URL_PATH ),
  47              'queryVars' => (object) $vars,
  48          )
  49      );
  50  
  51      get_current_screen()->add_help_tab(
  52          array(
  53              'id'      => 'overview',
  54              'title'   => __( 'Overview' ),
  55              'content' =>
  56                  '<p>' . __( 'All the files you&#8217;ve uploaded are listed in the Media Library, with the most recent uploads listed first.' ) . '</p>' .
  57                  '<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>' .
  58                  '<p>' . __( 'To delete media items, click the Bulk Select button at the top of the screen. Select any items you wish to delete, then click the Delete Selected button. Clicking the Cancel Selection button takes you back to viewing your media.' ) . '</p>',
  59          )
  60      );
  61  
  62      get_current_screen()->add_help_tab(
  63          array(
  64              'id'      => 'attachment-details',
  65              'title'   => __( 'Attachment Details' ),
  66              'content' =>
  67                  '<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>' .
  68                  '<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>' .
  69                  '<p>' . __( 'You can also delete individual items and access the extended edit screen from the details dialog.' ) . '</p>',
  70          )
  71      );
  72  
  73      get_current_screen()->set_help_sidebar(
  74          '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
  75          '<p>' . __( '<a href="https://wordpress.org/support/article/media-library-screen/">Documentation on Media Library</a>' ) . '</p>' .
  76          '<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>'
  77      );
  78  
  79      // Used in the HTML title tag.
  80      $title       = __( 'Media Library' );
  81      $parent_file = 'upload.php';
  82  
  83      require_once ABSPATH . 'wp-admin/admin-header.php';
  84      ?>
  85      <div class="wrap" id="wp-media-grid" data-search="<?php _admin_search_query(); ?>">
  86          <h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1>
  87  
  88          <?php
  89          if ( current_user_can( 'upload_files' ) ) {
  90              ?>
  91              <a href="<?php echo esc_url( admin_url( 'media-new.php' ) ); ?>" class="page-title-action aria-button-if-js"><?php echo esc_html_x( 'Add New', 'file' ); ?></a>
  92              <?php
  93          }
  94          ?>
  95  
  96          <hr class="wp-header-end">
  97  
  98          <div class="error hide-if-js">
  99              <p>
 100              <?php
 101              printf(
 102                  /* translators: %s: List view URL. */
 103                  __( 'The grid view for the Media Library requires JavaScript. <a href="%s">Switch to the list view</a>.' ),
 104                  'upload.php?mode=list'
 105              );
 106              ?>
 107              </p>
 108          </div>
 109      </div>
 110      <?php
 111      require_once ABSPATH . 'wp-admin/admin-footer.php';
 112      exit;
 113  }
 114  
 115  $wp_list_table = _get_list_table( 'WP_Media_List_Table' );
 116  $pagenum       = $wp_list_table->get_pagenum();
 117  
 118  // Handle bulk actions.
 119  $doaction = $wp_list_table->current_action();
 120  
 121  if ( $doaction ) {
 122      check_admin_referer( 'bulk-media' );
 123  
 124      $post_ids = array();
 125  
 126      if ( 'delete_all' === $doaction ) {
 127          $post_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type='attachment' AND post_status = 'trash'" );
 128          $doaction = 'delete';
 129      } elseif ( isset( $_REQUEST['media'] ) ) {
 130          $post_ids = $_REQUEST['media'];
 131      } elseif ( isset( $_REQUEST['ids'] ) ) {
 132          $post_ids = explode( ',', $_REQUEST['ids'] );
 133      }
 134  
 135      $location = 'upload.php';
 136      $referer  = wp_get_referer();
 137      if ( $referer ) {
 138          if ( false !== strpos( $referer, 'upload.php' ) ) {
 139              $location = remove_query_arg( array( 'trashed', 'untrashed', 'deleted', 'message', 'ids', 'posted' ), $referer );
 140          }
 141      }
 142  
 143      switch ( $doaction ) {
 144          case 'detach':
 145              wp_media_attach_action( $_REQUEST['parent_post_id'], 'detach' );
 146              break;
 147  
 148          case 'attach':
 149              wp_media_attach_action( $_REQUEST['found_post_id'] );
 150              break;
 151  
 152          case 'trash':
 153              if ( empty( $post_ids ) ) {
 154                  break;
 155              }
 156              foreach ( (array) $post_ids as $post_id ) {
 157                  if ( ! current_user_can( 'delete_post', $post_id ) ) {
 158                      wp_die( __( 'Sorry, you are not allowed to move this item to the Trash.' ) );
 159                  }
 160  
 161                  if ( ! wp_trash_post( $post_id ) ) {
 162                      wp_die( __( 'Error in moving the item to Trash.' ) );
 163                  }
 164              }
 165              $location = add_query_arg(
 166                  array(
 167                      'trashed' => count( $post_ids ),
 168                      'ids'     => implode( ',', $post_ids ),
 169                  ),
 170                  $location
 171              );
 172              break;
 173          case 'untrash':
 174              if ( empty( $post_ids ) ) {
 175                  break;
 176              }
 177              foreach ( (array) $post_ids as $post_id ) {
 178                  if ( ! current_user_can( 'delete_post', $post_id ) ) {
 179                      wp_die( __( 'Sorry, you are not allowed to restore this item from the Trash.' ) );
 180                  }
 181  
 182                  if ( ! wp_untrash_post( $post_id ) ) {
 183                      wp_die( __( 'Error in restoring the item from Trash.' ) );
 184                  }
 185              }
 186              $location = add_query_arg( 'untrashed', count( $post_ids ), $location );
 187              break;
 188          case 'delete':
 189              if ( empty( $post_ids ) ) {
 190                  break;
 191              }
 192              foreach ( (array) $post_ids as $post_id_del ) {
 193                  if ( ! current_user_can( 'delete_post', $post_id_del ) ) {
 194                      wp_die( __( 'Sorry, you are not allowed to delete this item.' ) );
 195                  }
 196  
 197                  if ( ! wp_delete_attachment( $post_id_del ) ) {
 198                      wp_die( __( 'Error in deleting the attachment.' ) );
 199                  }
 200              }
 201              $location = add_query_arg( 'deleted', count( $post_ids ), $location );
 202              break;
 203          default:
 204              $screen = get_current_screen()->id;
 205  
 206              /** This action is documented in wp-admin/edit.php */
 207              $location = apply_filters( "handle_bulk_actions-{$screen}", $location, $doaction, $post_ids ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
 208      }
 209  
 210      wp_redirect( $location );
 211      exit;
 212  } elseif ( ! empty( $_GET['_wp_http_referer'] ) ) {
 213      wp_redirect( remove_query_arg( array( '_wp_http_referer', '_wpnonce' ), wp_unslash( $_SERVER['REQUEST_URI'] ) ) );
 214      exit;
 215  }
 216  
 217  $wp_list_table->prepare_items();
 218  
 219  // Used in the HTML title tag.
 220  $title       = __( 'Media Library' );
 221  $parent_file = 'upload.php';
 222  
 223  wp_enqueue_script( 'media' );
 224  
 225  add_screen_option( 'per_page' );
 226  
 227  get_current_screen()->add_help_tab(
 228      array(
 229          'id'      => 'overview',
 230          'title'   => __( 'Overview' ),
 231          'content' =>
 232                  '<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>' .
 233                  '<p>' . __( 'You can narrow the list by file type/status or by date using the dropdown menus above the media table.' ) . '</p>' .
 234                  '<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>',
 235      )
 236  );
 237  get_current_screen()->add_help_tab(
 238      array(
 239          'id'      => 'actions-links',
 240          'title'   => __( 'Available Actions' ),
 241          'content' =>
 242                  '<p>' . __( 'Hovering over a row reveals action links: Edit, Delete Permanently, and View. Clicking Edit or on the media file&#8217;s name displays a simple screen to edit that individual file&#8217;s metadata. Clicking Delete Permanently will delete the file from the media library (as well as from any posts to which it is currently attached). View will take you to the display page for that file.' ) . '</p>',
 243      )
 244  );
 245  get_current_screen()->add_help_tab(
 246      array(
 247          'id'      => 'attaching-files',
 248          'title'   => __( 'Attaching Files' ),
 249          'content' =>
 250                  '<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>',
 251      )
 252  );
 253  
 254  get_current_screen()->set_help_sidebar(
 255      '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
 256      '<p>' . __( '<a href="https://wordpress.org/support/article/media-library-screen/">Documentation on Media Library</a>' ) . '</p>' .
 257      '<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>'
 258  );
 259  
 260  get_current_screen()->set_screen_reader_content(
 261      array(
 262          'heading_views'      => __( 'Filter media items list' ),
 263          'heading_pagination' => __( 'Media items list navigation' ),
 264          'heading_list'       => __( 'Media items list' ),
 265      )
 266  );
 267  
 268  require_once ABSPATH . 'wp-admin/admin-header.php';
 269  ?>
 270  
 271  <div class="wrap">
 272  <h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1>
 273  
 274  <?php
 275  if ( current_user_can( 'upload_files' ) ) {
 276      ?>
 277      <a href="<?php echo esc_url( admin_url( 'media-new.php' ) ); ?>" class="page-title-action"><?php echo esc_html_x( 'Add New', 'file' ); ?></a>
 278                          <?php
 279  }
 280  
 281  if ( isset( $_REQUEST['s'] ) && strlen( $_REQUEST['s'] ) ) {
 282      echo '<span class="subtitle">';
 283      printf(
 284          /* translators: %s: Search query. */
 285          __( 'Search results for: %s' ),
 286          '<strong>' . get_search_query() . '</strong>'
 287      );
 288      echo '</span>';
 289  }
 290  ?>
 291  
 292  <hr class="wp-header-end">
 293  
 294  <?php
 295  $message = '';
 296  if ( ! empty( $_GET['posted'] ) ) {
 297      $message                = __( 'Media file updated.' );
 298      $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'posted' ), $_SERVER['REQUEST_URI'] );
 299  }
 300  
 301  if ( ! empty( $_GET['attached'] ) && absint( $_GET['attached'] ) ) {
 302      $attached = absint( $_GET['attached'] );
 303      if ( 1 === $attached ) {
 304          $message = __( 'Media file attached.' );
 305      } else {
 306          /* translators: %s: Number of media files. */
 307          $message = _n( '%s media file attached.', '%s media files attached.', $attached );
 308      }
 309      $message                = sprintf( $message, number_format_i18n( $attached ) );
 310      $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'detach', 'attached' ), $_SERVER['REQUEST_URI'] );
 311  }
 312  
 313  if ( ! empty( $_GET['detach'] ) && absint( $_GET['detach'] ) ) {
 314      $detached = absint( $_GET['detach'] );
 315      if ( 1 === $detached ) {
 316          $message = __( 'Media file detached.' );
 317      } else {
 318          /* translators: %s: Number of media files. */
 319          $message = _n( '%s media file detached.', '%s media files detached.', $detached );
 320      }
 321      $message                = sprintf( $message, number_format_i18n( $detached ) );
 322      $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'detach', 'attached' ), $_SERVER['REQUEST_URI'] );
 323  }
 324  
 325  if ( ! empty( $_GET['deleted'] ) && absint( $_GET['deleted'] ) ) {
 326      $deleted = absint( $_GET['deleted'] );
 327      if ( 1 === $deleted ) {
 328          $message = __( 'Media file permanently deleted.' );
 329      } else {
 330          /* translators: %s: Number of media files. */
 331          $message = _n( '%s media file permanently deleted.', '%s media files permanently deleted.', $deleted );
 332      }
 333      $message                = sprintf( $message, number_format_i18n( $deleted ) );
 334      $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'deleted' ), $_SERVER['REQUEST_URI'] );
 335  }
 336  
 337  if ( ! empty( $_GET['trashed'] ) && absint( $_GET['trashed'] ) ) {
 338      $trashed = absint( $_GET['trashed'] );
 339      if ( 1 === $trashed ) {
 340          $message = __( 'Media file moved to the Trash.' );
 341      } else {
 342          /* translators: %s: Number of media files. */
 343          $message = _n( '%s media file moved to the Trash.', '%s media files moved to the Trash.', $trashed );
 344      }
 345      $message                = sprintf( $message, number_format_i18n( $trashed ) );
 346      $message               .= ' <a href="' . esc_url( wp_nonce_url( 'upload.php?doaction=undo&action=untrash&ids=' . ( isset( $_GET['ids'] ) ? $_GET['ids'] : '' ), 'bulk-media' ) ) . '">' . __( 'Undo' ) . '</a>';
 347      $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'trashed' ), $_SERVER['REQUEST_URI'] );
 348  }
 349  
 350  if ( ! empty( $_GET['untrashed'] ) && absint( $_GET['untrashed'] ) ) {
 351      $untrashed = absint( $_GET['untrashed'] );
 352      if ( 1 === $untrashed ) {
 353          $message = __( 'Media file restored from the Trash.' );
 354      } else {
 355          /* translators: %s: Number of media files. */
 356          $message = _n( '%s media file restored from the Trash.', '%s media files restored from the Trash.', $untrashed );
 357      }
 358      $message                = sprintf( $message, number_format_i18n( $untrashed ) );
 359      $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'untrashed' ), $_SERVER['REQUEST_URI'] );
 360  }
 361  
 362  $messages[1] = __( 'Media file updated.' );
 363  $messages[2] = __( 'Media file permanently deleted.' );
 364  $messages[3] = __( 'Error saving media file.' );
 365  $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>';
 366  $messages[5] = __( 'Media file restored from the Trash.' );
 367  
 368  if ( ! empty( $_GET['message'] ) && isset( $messages[ $_GET['message'] ] ) ) {
 369      $message                = $messages[ $_GET['message'] ];
 370      $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'message' ), $_SERVER['REQUEST_URI'] );
 371  }
 372  
 373  if ( ! empty( $message ) ) {
 374      ?>
 375  <div id="message" class="updated notice is-dismissible"><p><?php echo $message; ?></p></div>
 376  <?php } ?>
 377  
 378  <form id="posts-filter" method="get">
 379  
 380  <?php $wp_list_table->views(); ?>
 381  
 382  <?php $wp_list_table->display(); ?>
 383  
 384  <div id="ajax-response"></div>
 385  <?php find_posts_div(); ?>
 386  </form>
 387  </div>
 388  
 389  <?php
 390  require_once ABSPATH . 'wp-admin/admin-footer.php';


Generated: Tue Nov 12 01:00:02 2024 Cross-referenced by PHPXref 0.7.1