3 * Media Library administration panel.
6 * @subpackage Administration
9 /** WordPress Administration Bootstrap */
10 require_once( dirname( __FILE__ ) . '/admin.php' );
12 if ( !current_user_can('upload_files') )
13 wp_die( __( 'Sorry, you are not allowed to upload files.' ) );
15 $mode = get_user_option( 'media_library_mode', get_current_user_id() ) ? get_user_option( 'media_library_mode', get_current_user_id() ) : 'grid';
16 $modes = array( 'grid', 'list' );
18 if ( isset( $_GET['mode'] ) && in_array( $_GET['mode'], $modes ) ) {
19 $mode = $_GET['mode'];
20 update_user_option( get_current_user_id(), 'media_library_mode', $mode );
23 if ( 'grid' === $mode ) {
25 wp_enqueue_script( 'media-grid' );
26 wp_enqueue_script( 'media' );
28 remove_action( 'admin_head', 'wp_admin_canonical_url' );
33 $vars = wp_edit_attachments_query_vars( $q );
34 $ignore = array( 'mode', 'post_type', 'post_status', 'posts_per_page' );
35 foreach ( $vars as $key => $value ) {
36 if ( ! $value || in_array( $key, $ignore ) ) {
37 unset( $vars[ $key ] );
41 wp_localize_script( 'media-grid', '_wpMediaGridSettings', array(
42 'adminUrl' => parse_url( self_admin_url(), PHP_URL_PATH ),
43 'queryVars' => (object) $vars
46 get_current_screen()->add_help_tab( array(
48 'title' => __( 'Overview' ),
50 '<p>' . __( 'All the files you’ve uploaded are listed in the Media Library, with the most recent uploads listed first.' ) . '</p>' .
51 '<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>' .
52 '<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>'
55 get_current_screen()->add_help_tab( array(
56 'id' => 'attachment-details',
57 'title' => __( 'Attachment Details' ),
59 '<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>' .
60 '<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>' .
61 '<p>' . __( 'You can also delete individual items and access the extended edit screen from the details dialog.' ) . '</p>'
64 get_current_screen()->set_help_sidebar(
65 '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
66 '<p>' . __( '<a href="https://codex.wordpress.org/Media_Library_Screen">Documentation on Media Library</a>' ) . '</p>' .
67 '<p>' . __( '<a href="https://wordpress.org/support/">Support Forums</a>' ) . '</p>'
70 $title = __('Media Library');
71 $parent_file = 'upload.php';
73 require_once( ABSPATH . 'wp-admin/admin-header.php' );
75 <div class="wrap" id="wp-media-grid" data-search="<?php _admin_search_query() ?>">
78 echo esc_html( $title );
79 if ( current_user_can( 'upload_files' ) ) { ?>
80 <a href="<?php echo admin_url( 'media-new.php' ); ?>" class="page-title-action"><?php echo esc_html_x( 'Add New', 'file' ); ?></a><?php
84 <div class="error hide-if-js">
86 /* translators: %s: list view URL */
87 __( 'The grid view for the Media Library requires JavaScript. <a href="%s">Switch to the list view</a>.' ),
88 'upload.php?mode=list'
93 include( ABSPATH . 'wp-admin/admin-footer.php' );
97 $wp_list_table = _get_list_table('WP_Media_List_Table');
98 $pagenum = $wp_list_table->get_pagenum();
100 // Handle bulk actions
101 $doaction = $wp_list_table->current_action();
104 check_admin_referer('bulk-media');
106 if ( 'delete_all' == $doaction ) {
107 $post_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type='attachment' AND post_status = 'trash'" );
108 $doaction = 'delete';
109 } elseif ( isset( $_REQUEST['media'] ) ) {
110 $post_ids = $_REQUEST['media'];
111 } elseif ( isset( $_REQUEST['ids'] ) ) {
112 $post_ids = explode( ',', $_REQUEST['ids'] );
115 $location = 'upload.php';
116 if ( $referer = wp_get_referer() ) {
117 if ( false !== strpos( $referer, 'upload.php' ) )
118 $location = remove_query_arg( array( 'trashed', 'untrashed', 'deleted', 'message', 'ids', 'posted' ), $referer );
121 switch ( $doaction ) {
123 wp_media_attach_action( $_REQUEST['parent_post_id'], 'detach' );
127 wp_media_attach_action( $_REQUEST['found_post_id'] );
131 if ( !isset( $post_ids ) )
133 foreach ( (array) $post_ids as $post_id ) {
134 if ( !current_user_can( 'delete_post', $post_id ) )
135 wp_die( __( 'Sorry, you are not allowed to move this item to the Trash.' ) );
137 if ( !wp_trash_post( $post_id ) )
138 wp_die( __( 'Error in moving to Trash.' ) );
140 $location = add_query_arg( array( 'trashed' => count( $post_ids ), 'ids' => join( ',', $post_ids ) ), $location );
143 if ( !isset( $post_ids ) )
145 foreach ( (array) $post_ids as $post_id ) {
146 if ( !current_user_can( 'delete_post', $post_id ) )
147 wp_die( __( 'Sorry, you are not allowed to restore this item from the Trash.' ) );
149 if ( !wp_untrash_post( $post_id ) )
150 wp_die( __( 'Error in restoring from Trash.' ) );
152 $location = add_query_arg( 'untrashed', count( $post_ids ), $location );
155 if ( !isset( $post_ids ) )
157 foreach ( (array) $post_ids as $post_id_del ) {
158 if ( !current_user_can( 'delete_post', $post_id_del ) )
159 wp_die( __( 'Sorry, you are not allowed to delete this item.' ) );
161 if ( !wp_delete_attachment( $post_id_del ) )
162 wp_die( __( 'Error in deleting.' ) );
164 $location = add_query_arg( 'deleted', count( $post_ids ), $location );
167 /** This action is documented in wp-admin/edit-comments.php */
168 $location = apply_filters( 'handle_bulk_actions-' . get_current_screen()->id, $location, $doaction, $post_ids );
171 wp_redirect( $location );
173 } elseif ( ! empty( $_GET['_wp_http_referer'] ) ) {
174 wp_redirect( remove_query_arg( array( '_wp_http_referer', '_wpnonce' ), wp_unslash( $_SERVER['REQUEST_URI'] ) ) );
178 $wp_list_table->prepare_items();
180 $title = __('Media Library');
181 $parent_file = 'upload.php';
183 wp_enqueue_script( 'media' );
185 add_screen_option( 'per_page' );
187 get_current_screen()->add_help_tab( array(
189 'title' => __('Overview'),
191 '<p>' . __( 'All the files you’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>' .
192 '<p>' . __( 'You can narrow the list by file type/status or by date using the dropdown menus above the media table.' ) . '</p>' .
193 '<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>'
195 get_current_screen()->add_help_tab( array(
196 'id' => 'actions-links',
197 'title' => __('Available Actions'),
199 '<p>' . __( 'Hovering over a row reveals action links: Edit, Delete Permanently, and View. Clicking Edit or on the media file’s name displays a simple screen to edit that individual file’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>'
201 get_current_screen()->add_help_tab( array(
202 'id' => 'attaching-files',
203 'title' => __('Attaching Files'),
205 '<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>'
208 get_current_screen()->set_help_sidebar(
209 '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
210 '<p>' . __( '<a href="https://codex.wordpress.org/Media_Library_Screen">Documentation on Media Library</a>' ) . '</p>' .
211 '<p>' . __( '<a href="https://wordpress.org/support/">Support Forums</a>' ) . '</p>'
214 get_current_screen()->set_screen_reader_content( array(
215 'heading_views' => __( 'Filter media items list' ),
216 'heading_pagination' => __( 'Media items list navigation' ),
217 'heading_list' => __( 'Media items list' ),
220 require_once( ABSPATH . 'wp-admin/admin-header.php' );
226 echo esc_html( $title );
227 if ( current_user_can( 'upload_files' ) ) { ?>
228 <a href="<?php echo admin_url( 'media-new.php' ); ?>" class="page-title-action"><?php echo esc_html_x('Add New', 'file'); ?></a><?php
230 if ( isset( $_REQUEST['s'] ) && strlen( $_REQUEST['s'] ) ) {
231 /* translators: %s: search keywords */
232 printf( '<span class="subtitle">' . __( 'Search results for “%s”' ) . '</span>', get_search_query() );
239 if ( ! empty( $_GET['posted'] ) ) {
240 $message = __( 'Media file updated.' );
241 $_SERVER['REQUEST_URI'] = remove_query_arg(array('posted'), $_SERVER['REQUEST_URI']);
244 if ( ! empty( $_GET['attached'] ) && $attached = absint( $_GET['attached'] ) ) {
245 if ( 1 == $attached ) {
246 $message = __( 'Media file attached.' );
248 /* translators: %s: number of media files */
249 $message = _n( '%s media file attached.', '%s media files attached.', $attached );
251 $message = sprintf( $message, number_format_i18n( $attached ) );
252 $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'detach', 'attached' ), $_SERVER['REQUEST_URI'] );
255 if ( ! empty( $_GET['detach'] ) && $detached = absint( $_GET['detach'] ) ) {
256 if ( 1 == $detached ) {
257 $message = __( 'Media file detached.' );
259 /* translators: %s: number of media files */
260 $message = _n( '%s media file detached.', '%s media files detached.', $detached );
262 $message = sprintf( $message, number_format_i18n( $detached ) );
263 $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'detach', 'attached' ), $_SERVER['REQUEST_URI'] );
266 if ( ! empty( $_GET['deleted'] ) && $deleted = absint( $_GET['deleted'] ) ) {
267 if ( 1 == $deleted ) {
268 $message = __( 'Media file permanently deleted.' );
270 /* translators: %s: number of media files */
271 $message = _n( '%s media file permanently deleted.', '%s media files permanently deleted.', $deleted );
273 $message = sprintf( $message, number_format_i18n( $deleted ) );
274 $_SERVER['REQUEST_URI'] = remove_query_arg(array('deleted'), $_SERVER['REQUEST_URI']);
277 if ( ! empty( $_GET['trashed'] ) && $trashed = absint( $_GET['trashed'] ) ) {
278 if ( 1 == $trashed ) {
279 $message = __( 'Media file moved to the trash.' );
281 /* translators: %s: number of media files */
282 $message = _n( '%s media file moved to the trash.', '%s media files moved to the trash.', $trashed );
284 $message = sprintf( $message, number_format_i18n( $trashed ) );
285 $message .= ' <a href="' . esc_url( wp_nonce_url( 'upload.php?doaction=undo&action=untrash&ids='.(isset($_GET['ids']) ? $_GET['ids'] : ''), "bulk-media" ) ) . '">' . __('Undo') . '</a>';
286 $_SERVER['REQUEST_URI'] = remove_query_arg(array('trashed'), $_SERVER['REQUEST_URI']);
289 if ( ! empty( $_GET['untrashed'] ) && $untrashed = absint( $_GET['untrashed'] ) ) {
290 if ( 1 == $untrashed ) {
291 $message = __( 'Media file restored from the trash.' );
293 /* translators: %s: number of media files */
294 $message = _n( '%s media file restored from the trash.', '%s media files restored from the trash.', $untrashed );
296 $message = sprintf( $message, number_format_i18n( $untrashed ) );
297 $_SERVER['REQUEST_URI'] = remove_query_arg(array('untrashed'), $_SERVER['REQUEST_URI']);
300 $messages[1] = __( 'Media file updated.' );
301 $messages[2] = __( 'Media file permanently deleted.' );
302 $messages[3] = __( 'Error saving media file.' );
303 $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>';
304 $messages[5] = __( 'Media file restored from the trash.' );
306 if ( ! empty( $_GET['message'] ) && isset( $messages[ $_GET['message'] ] ) ) {
307 $message = $messages[ $_GET['message'] ];
308 $_SERVER['REQUEST_URI'] = remove_query_arg(array('message'), $_SERVER['REQUEST_URI']);
311 if ( !empty($message) ) { ?>
312 <div id="message" class="updated notice is-dismissible"><p><?php echo $message; ?></p></div>
315 <form id="posts-filter" method="get">
317 <?php $wp_list_table->views(); ?>
319 <?php $wp_list_table->display(); ?>
321 <div id="ajax-response"></div>
322 <?php find_posts_div(); ?>
327 include( ABSPATH . 'wp-admin/admin-footer.php' );