[ 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( dirname( __FILE__ ) . '/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 ) ) {
  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 ) ) {
  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      $title       = __( 'Media Library' );
  80      $parent_file = 'upload.php';
  81  
  82      require_once( ABSPATH . 'wp-admin/admin-header.php' );
  83      ?>
  84      <div class="wrap" id="wp-media-grid" data-search="<?php _admin_search_query(); ?>">
  85          <h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1>
  86  
  87          <?php
  88          if ( current_user_can( 'upload_files' ) ) {
  89              ?>
  90              <a href="<?php echo admin_url( 'media-new.php' ); ?>" class="page-title-action aria-button-if-js"><?php echo esc_html_x( 'Add New', 'file' ); ?></a>
  91                                  <?php
  92          }
  93          ?>
  94  
  95          <hr class="wp-header-end">
  96  
  97          <div class="error hide-if-js">
  98              <p>
  99              <?php
 100              printf(
 101                  /* translators: %s: List view URL. */
 102                  __( 'The grid view for the Media Library requires JavaScript. <a href="%s">Switch to the list view</a>.' ),
 103                  'upload.php?mode=list'
 104              );
 105              ?>
 106              </p>
 107          </div>
 108      </div>
 109      <?php
 110      include( ABSPATH . 'wp-admin/admin-footer.php' );
 111      exit;
 112  }
 113  
 114  $wp_list_table = _get_list_table( 'WP_Media_List_Table' );
 115  $pagenum       = $wp_list_table->get_pagenum();
 116  
 117  // Handle bulk actions
 118  $doaction = $wp_list_table->current_action();
 119  
 120  if ( $doaction ) {
 121      check_admin_referer( 'bulk-media' );
 122  
 123      if ( 'delete_all' == $doaction ) {
 124          $post_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type='attachment' AND post_status = 'trash'" );
 125          $doaction = 'delete';
 126      } elseif ( isset( $_REQUEST['media'] ) ) {
 127          $post_ids = $_REQUEST['media'];
 128      } elseif ( isset( $_REQUEST['ids'] ) ) {
 129          $post_ids = explode( ',', $_REQUEST['ids'] );
 130      }
 131  
 132      $location = 'upload.php';
 133      $referer  = wp_get_referer();
 134      if ( $referer ) {
 135          if ( false !== strpos( $referer, 'upload.php' ) ) {
 136              $location = remove_query_arg( array( 'trashed', 'untrashed', 'deleted', 'message', 'ids', 'posted' ), $referer );
 137          }
 138      }
 139  
 140      switch ( $doaction ) {
 141          case 'detach':
 142              wp_media_attach_action( $_REQUEST['parent_post_id'], 'detach' );
 143              break;
 144  
 145          case 'attach':
 146              wp_media_attach_action( $_REQUEST['found_post_id'] );
 147              break;
 148  
 149          case 'trash':
 150              if ( ! isset( $post_ids ) ) {
 151                  break;
 152              }
 153              foreach ( (array) $post_ids as $post_id ) {
 154                  if ( ! current_user_can( 'delete_post', $post_id ) ) {
 155                      wp_die( __( 'Sorry, you are not allowed to move this item to the Trash.' ) );
 156                  }
 157  
 158                  if ( ! wp_trash_post( $post_id ) ) {
 159                      wp_die( __( 'Error in moving to Trash.' ) );
 160                  }
 161              }
 162              $location = add_query_arg(
 163                  array(
 164                      'trashed' => count( $post_ids ),
 165                      'ids'     => join( ',', $post_ids ),
 166                  ),
 167                  $location
 168              );
 169              break;
 170          case 'untrash':
 171              if ( ! isset( $post_ids ) ) {
 172                  break;
 173              }
 174              foreach ( (array) $post_ids as $post_id ) {
 175                  if ( ! current_user_can( 'delete_post', $post_id ) ) {
 176                      wp_die( __( 'Sorry, you are not allowed to restore this item from the Trash.' ) );
 177                  }
 178  
 179                  if ( ! wp_untrash_post( $post_id ) ) {
 180                      wp_die( __( 'Error in restoring from Trash.' ) );
 181                  }
 182              }
 183              $location = add_query_arg( 'untrashed', count( $post_ids ), $location );
 184              break;
 185          case 'delete':
 186              if ( ! isset( $post_ids ) ) {
 187                  break;
 188              }
 189              foreach ( (array) $post_ids as $post_id_del ) {
 190                  if ( ! current_user_can( 'delete_post', $post_id_del ) ) {
 191                      wp_die( __( 'Sorry, you are not allowed to delete this item.' ) );
 192                  }
 193  
 194                  if ( ! wp_delete_attachment( $post_id_del ) ) {
 195                      wp_die( __( 'Error in deleting.' ) );
 196                  }
 197              }
 198              $location = add_query_arg( 'deleted', count( $post_ids ), $location );
 199              break;
 200          default:
 201              $screen = get_current_screen()->id;
 202  
 203              /** This action is documented in wp-admin/edit.php */
 204              $location = apply_filters( "handle_bulk_actions-{$screen}", $location, $doaction, $post_ids ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
 205      }
 206  
 207      wp_redirect( $location );
 208      exit;
 209  } elseif ( ! empty( $_GET['_wp_http_referer'] ) ) {
 210      wp_redirect( remove_query_arg( array( '_wp_http_referer', '_wpnonce' ), wp_unslash( $_SERVER['REQUEST_URI'] ) ) );
 211      exit;
 212  }
 213  
 214  $wp_list_table->prepare_items();
 215  
 216  $title       = __( 'Media Library' );
 217  $parent_file = 'upload.php';
 218  
 219  wp_enqueue_script( 'media' );
 220  
 221  add_screen_option( 'per_page' );
 222  
 223  get_current_screen()->add_help_tab(
 224      array(
 225          'id'      => 'overview',
 226          'title'   => __( 'Overview' ),
 227          'content' =>
 228                  '<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>' .
 229                  '<p>' . __( 'You can narrow the list by file type/status or by date using the dropdown menus above the media table.' ) . '</p>' .
 230                  '<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>',
 231      )
 232  );
 233  get_current_screen()->add_help_tab(
 234      array(
 235          'id'      => 'actions-links',
 236          'title'   => __( 'Available Actions' ),
 237          'content' =>
 238                  '<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>',
 239      )
 240  );
 241  get_current_screen()->add_help_tab(
 242      array(
 243          'id'      => 'attaching-files',
 244          'title'   => __( 'Attaching Files' ),
 245          'content' =>
 246                  '<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>',
 247      )
 248  );
 249  
 250  get_current_screen()->set_help_sidebar(
 251      '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
 252      '<p>' . __( '<a href="https://wordpress.org/support/article/media-library-screen/">Documentation on Media Library</a>' ) . '</p>' .
 253      '<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>'
 254  );
 255  
 256  get_current_screen()->set_screen_reader_content(
 257      array(
 258          'heading_views'      => __( 'Filter media items list' ),
 259          'heading_pagination' => __( 'Media items list navigation' ),
 260          'heading_list'       => __( 'Media items list' ),
 261      )
 262  );
 263  
 264  require_once( ABSPATH . 'wp-admin/admin-header.php' );
 265  ?>
 266  
 267  <div class="wrap">
 268  <h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1>
 269  
 270  <?php
 271  if ( current_user_can( 'upload_files' ) ) {
 272      ?>
 273      <a href="<?php echo admin_url( 'media-new.php' ); ?>" class="page-title-action"><?php echo esc_html_x( 'Add New', 'file' ); ?></a>
 274                          <?php
 275  }
 276  
 277  if ( isset( $_REQUEST['s'] ) && strlen( $_REQUEST['s'] ) ) {
 278      /* translators: %s: Search query. */
 279      printf( '<span class="subtitle">' . __( 'Search results for &#8220;%s&#8221;' ) . '</span>', get_search_query() );
 280  }
 281  ?>
 282  
 283  <hr class="wp-header-end">
 284  
 285  <?php
 286  $message = '';
 287  if ( ! empty( $_GET['posted'] ) ) {
 288      $message                = __( 'Media file updated.' );
 289      $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'posted' ), $_SERVER['REQUEST_URI'] );
 290  }
 291  
 292  if ( ! empty( $_GET['attached'] ) && absint( $_GET['attached'] ) ) {
 293      $attached = absint( $_GET['attached'] );
 294      if ( 1 == $attached ) {
 295          $message = __( 'Media file attached.' );
 296      } else {
 297          /* translators: %s: Number of media files. */
 298          $message = _n( '%s media file attached.', '%s media files attached.', $attached );
 299      }
 300      $message                = sprintf( $message, number_format_i18n( $attached ) );
 301      $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'detach', 'attached' ), $_SERVER['REQUEST_URI'] );
 302  }
 303  
 304  if ( ! empty( $_GET['detach'] ) && absint( $_GET['detach'] ) ) {
 305      $detached = absint( $_GET['detach'] );
 306      if ( 1 == $detached ) {
 307          $message = __( 'Media file detached.' );
 308      } else {
 309          /* translators: %s: Number of media files. */
 310          $message = _n( '%s media file detached.', '%s media files detached.', $detached );
 311      }
 312      $message                = sprintf( $message, number_format_i18n( $detached ) );
 313      $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'detach', 'attached' ), $_SERVER['REQUEST_URI'] );
 314  }
 315  
 316  if ( ! empty( $_GET['deleted'] ) && absint( $_GET['deleted'] ) ) {
 317      $deleted = absint( $_GET['deleted'] );
 318      if ( 1 == $deleted ) {
 319          $message = __( 'Media file permanently deleted.' );
 320      } else {
 321          /* translators: %s: Number of media files. */
 322          $message = _n( '%s media file permanently deleted.', '%s media files permanently deleted.', $deleted );
 323      }
 324      $message                = sprintf( $message, number_format_i18n( $deleted ) );
 325      $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'deleted' ), $_SERVER['REQUEST_URI'] );
 326  }
 327  
 328  if ( ! empty( $_GET['trashed'] ) && absint( $_GET['trashed'] ) ) {
 329      $trashed = absint( $_GET['trashed'] );
 330      if ( 1 == $trashed ) {
 331          $message = __( 'Media file moved to the trash.' );
 332      } else {
 333          /* translators: %s: Number of media files. */
 334          $message = _n( '%s media file moved to the trash.', '%s media files moved to the trash.', $trashed );
 335      }
 336      $message                = sprintf( $message, number_format_i18n( $trashed ) );
 337      $message               .= ' <a href="' . esc_url( wp_nonce_url( 'upload.php?doaction=undo&action=untrash&ids=' . ( isset( $_GET['ids'] ) ? $_GET['ids'] : '' ), 'bulk-media' ) ) . '">' . __( 'Undo' ) . '</a>';
 338      $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'trashed' ), $_SERVER['REQUEST_URI'] );
 339  }
 340  
 341  if ( ! empty( $_GET['untrashed'] ) && absint( $_GET['untrashed'] ) ) {
 342      $untrashed = absint( $_GET['untrashed'] );
 343      if ( 1 == $untrashed ) {
 344          $message = __( 'Media file restored from the trash.' );
 345      } else {
 346          /* translators: %s: Number of media files. */
 347          $message = _n( '%s media file restored from the trash.', '%s media files restored from the trash.', $untrashed );
 348      }
 349      $message                = sprintf( $message, number_format_i18n( $untrashed ) );
 350      $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'untrashed' ), $_SERVER['REQUEST_URI'] );
 351  }
 352  
 353  $messages[1] = __( 'Media file updated.' );
 354  $messages[2] = __( 'Media file permanently deleted.' );
 355  $messages[3] = __( 'Error saving media file.' );
 356  $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>';
 357  $messages[5] = __( 'Media file restored from the trash.' );
 358  
 359  if ( ! empty( $_GET['message'] ) && isset( $messages[ $_GET['message'] ] ) ) {
 360      $message                = $messages[ $_GET['message'] ];
 361      $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'message' ), $_SERVER['REQUEST_URI'] );
 362  }
 363  
 364  if ( ! empty( $message ) ) {
 365      ?>
 366  <div id="message" class="updated notice is-dismissible"><p><?php echo $message; ?></p></div>
 367  <?php } ?>
 368  
 369  <form id="posts-filter" method="get">
 370  
 371  <?php $wp_list_table->views(); ?>
 372  
 373  <?php $wp_list_table->display(); ?>
 374  
 375  <div id="ajax-response"></div>
 376  <?php find_posts_div(); ?>
 377  </form>
 378  </div>
 379  
 380  <?php
 381  include( ABSPATH . 'wp-admin/admin-footer.php' );


Generated: Mon Dec 9 01:00:03 2019 Cross-referenced by PHPXref 0.7.1