Make WordPress Core

source: trunk/src/wp-admin/upload.php @ 56590

Last change on this file since 56590 was 56590, checked in by joedolson, 11 months ago

Administration: additional_classes is not a function.

Correct usage of additional_classes array parameter as if it were a function. Clearly, I'm getting tired and missing things. So many little changes. Follow up to [56573].

Props joedolson.
See #57791.

  • Property svn:eol-style set to native
File size: 15.1 KB
Line 
1<?php
2/**
3 * Media Library administration panel.
4 *
5 * @package WordPress
6 * @subpackage Administration
7 */
8
9/** WordPress Administration Bootstrap */
10require_once __DIR__ . '/admin.php';
11
12if ( ! current_user_can( 'upload_files' ) ) {
13        wp_die( __( 'Sorry, you are not allowed to upload files.' ) );
14}
15
16$message = '';
17if ( ! 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
24if ( ! 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
41if ( ! 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
58if ( ! 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
75if ( ! 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
98if ( ! 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
125if ( ! 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$mode  = get_user_option( 'media_library_mode', get_current_user_id() ) ? get_user_option( 'media_library_mode', get_current_user_id() ) : 'grid';
132$modes = array( 'grid', 'list' );
133
134if ( isset( $_GET['mode'] ) && in_array( $_GET['mode'], $modes, true ) ) {
135        $mode = $_GET['mode'];
136        update_user_option( get_current_user_id(), 'media_library_mode', $mode );
137}
138
139if ( 'grid' === $mode ) {
140        wp_enqueue_media();
141        wp_enqueue_script( 'media-grid' );
142        wp_enqueue_script( 'media' );
143
144        // Remove the error parameter added by deprecation of wp-admin/media.php.
145        add_filter(
146                'removable_query_args',
147                function () {
148                        return array( 'error' );
149                },
150                10,
151                0
152        );
153
154        $q = $_GET;
155        // Let JS handle this.
156        unset( $q['s'] );
157        $vars   = wp_edit_attachments_query_vars( $q );
158        $ignore = array( 'mode', 'post_type', 'post_status', 'posts_per_page' );
159        foreach ( $vars as $key => $value ) {
160                if ( ! $value || in_array( $key, $ignore, true ) ) {
161                        unset( $vars[ $key ] );
162                }
163        }
164
165        wp_localize_script(
166                'media-grid',
167                '_wpMediaGridSettings',
168                array(
169                        'adminUrl'  => parse_url( self_admin_url(), PHP_URL_PATH ),
170                        'queryVars' => (object) $vars,
171                )
172        );
173
174        get_current_screen()->add_help_tab(
175                array(
176                        'id'      => 'overview',
177                        'title'   => __( 'Overview' ),
178                        'content' =>
179                                '<p>' . __( 'All the files you&#8217;ve uploaded are listed in the Media Library, with the most recent uploads listed first.' ) . '</p>' .
180                                '<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>' .
181                                '<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>',
182                )
183        );
184
185        get_current_screen()->add_help_tab(
186                array(
187                        'id'      => 'attachment-details',
188                        'title'   => __( 'Attachment Details' ),
189                        'content' =>
190                                '<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>' .
191                                '<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>' .
192                                '<p>' . __( 'You can also delete individual items and access the extended edit screen from the details dialog.' ) . '</p>',
193                )
194        );
195
196        get_current_screen()->set_help_sidebar(
197                '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
198                '<p>' . __( '<a href="https://wordpress.org/documentation/article/media-library-screen/">Documentation on Media Library</a>' ) . '</p>' .
199                '<p>' . __( '<a href="https://wordpress.org/support/forums/">Support forums</a>' ) . '</p>'
200        );
201
202        // Used in the HTML title tag.
203        $title       = __( 'Media Library' );
204        $parent_file = 'upload.php';
205
206        require_once ABSPATH . 'wp-admin/admin-header.php';
207        ?>
208        <div class="wrap" id="wp-media-grid" data-search="<?php _admin_search_query(); ?>">
209                <h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1>
210
211                <?php
212                if ( current_user_can( 'upload_files' ) ) {
213                        ?>
214                        <a href="<?php echo esc_url( admin_url( 'media-new.php' ) ); ?>" class="page-title-action aria-button-if-js"><?php echo esc_html__( 'Add New Media File' ); ?></a>
215                        <?php
216                }
217                ?>
218
219                <hr class="wp-header-end">
220
221                <?php
222                if ( ! empty( $message ) ) {
223                        wp_admin_notice(
224                                $message,
225                                array(
226                                        'id'                 => 'message',
227                                        'additional_classes' => array( 'updated' ),
228                                        'dismissible'        => true,
229                                )
230                        );
231                }
232
233                if ( ! empty( $message ) ) {
234                        wp_admin_notice(
235                                $message,
236                                array(
237                                        'id'                 => 'message',
238                                        'additional_classes' => array( 'updated' ),
239                                        'dismissible'        => true,
240                                )
241                        );
242                }
243
244                $js_required_message = sprintf(
245                        /* translators: %s: List view URL. */
246                        __( 'The grid view for the Media Library requires JavaScript. <a href="%s">Switch to the list view</a>.' ),
247                        'upload.php?mode=list'
248                );
249                wp_admin_notice(
250                        $js_required_message,
251                        array(
252                                'additional_classes' => array( 'error', 'hide-if-js' ),
253                        )
254                );
255                ?>
256        </div>
257        <?php
258        require_once ABSPATH . 'wp-admin/admin-footer.php';
259        exit;
260}
261
262$wp_list_table = _get_list_table( 'WP_Media_List_Table' );
263$pagenum       = $wp_list_table->get_pagenum();
264
265// Handle bulk actions.
266$doaction = $wp_list_table->current_action();
267
268if ( $doaction ) {
269        check_admin_referer( 'bulk-media' );
270
271        $post_ids = array();
272
273        if ( 'delete_all' === $doaction ) {
274                $post_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type='attachment' AND post_status = 'trash'" );
275                $doaction = 'delete';
276        } elseif ( isset( $_REQUEST['media'] ) ) {
277                $post_ids = $_REQUEST['media'];
278        } elseif ( isset( $_REQUEST['ids'] ) ) {
279                $post_ids = explode( ',', $_REQUEST['ids'] );
280        }
281        $post_ids = array_map( 'intval', (array) $post_ids );
282
283        $location = 'upload.php';
284        $referer  = wp_get_referer();
285        if ( $referer ) {
286                if ( str_contains( $referer, 'upload.php' ) ) {
287                        $location = remove_query_arg( array( 'trashed', 'untrashed', 'deleted', 'message', 'ids', 'posted' ), $referer );
288                }
289        }
290
291        switch ( $doaction ) {
292                case 'detach':
293                        wp_media_attach_action( $_REQUEST['parent_post_id'], 'detach' );
294                        break;
295
296                case 'attach':
297                        wp_media_attach_action( $_REQUEST['found_post_id'] );
298                        break;
299
300                case 'trash':
301                        if ( empty( $post_ids ) ) {
302                                break;
303                        }
304                        foreach ( $post_ids as $post_id ) {
305                                if ( ! current_user_can( 'delete_post', $post_id ) ) {
306                                        wp_die( __( 'Sorry, you are not allowed to move this item to the Trash.' ) );
307                                }
308
309                                if ( ! wp_trash_post( $post_id ) ) {
310                                        wp_die( __( 'Error in moving the item to Trash.' ) );
311                                }
312                        }
313                        $location = add_query_arg(
314                                array(
315                                        'trashed' => count( $post_ids ),
316                                        'ids'     => implode( ',', $post_ids ),
317                                ),
318                                $location
319                        );
320                        break;
321                case 'untrash':
322                        if ( empty( $post_ids ) ) {
323                                break;
324                        }
325                        foreach ( $post_ids as $post_id ) {
326                                if ( ! current_user_can( 'delete_post', $post_id ) ) {
327                                        wp_die( __( 'Sorry, you are not allowed to restore this item from the Trash.' ) );
328                                }
329
330                                if ( ! wp_untrash_post( $post_id ) ) {
331                                        wp_die( __( 'Error in restoring the item from Trash.' ) );
332                                }
333                        }
334                        $location = add_query_arg( 'untrashed', count( $post_ids ), $location );
335                        break;
336                case 'delete':
337                        if ( empty( $post_ids ) ) {
338                                break;
339                        }
340                        foreach ( $post_ids as $post_id_del ) {
341                                if ( ! current_user_can( 'delete_post', $post_id_del ) ) {
342                                        wp_die( __( 'Sorry, you are not allowed to delete this item.' ) );
343                                }
344
345                                if ( ! wp_delete_attachment( $post_id_del ) ) {
346                                        wp_die( __( 'Error in deleting the attachment.' ) );
347                                }
348                        }
349                        $location = add_query_arg( 'deleted', count( $post_ids ), $location );
350                        break;
351                default:
352                        $screen = get_current_screen()->id;
353
354                        /** This action is documented in wp-admin/edit.php */
355                        $location = apply_filters( "handle_bulk_actions-{$screen}", $location, $doaction, $post_ids ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
356        }
357
358        wp_redirect( $location );
359        exit;
360} elseif ( ! empty( $_GET['_wp_http_referer'] ) ) {
361        wp_redirect( remove_query_arg( array( '_wp_http_referer', '_wpnonce' ), wp_unslash( $_SERVER['REQUEST_URI'] ) ) );
362        exit;
363}
364
365$wp_list_table->prepare_items();
366
367// Used in the HTML title tag.
368$title       = __( 'Media Library' );
369$parent_file = 'upload.php';
370
371wp_enqueue_script( 'media' );
372
373add_screen_option( 'per_page' );
374
375get_current_screen()->add_help_tab(
376        array(
377                'id'      => 'overview',
378                'title'   => __( 'Overview' ),
379                'content' =>
380                                '<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>' .
381                                '<p>' . __( 'You can narrow the list by file type/status or by date using the dropdown menus above the media table.' ) . '</p>' .
382                                '<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>',
383        )
384);
385get_current_screen()->add_help_tab(
386        array(
387                'id'      => 'actions-links',
388                'title'   => __( 'Available Actions' ),
389                'content' =>
390                                '<p>' . __( 'Hovering over a row reveals action links that allow you to manage media items. You can perform the following actions:' ) . '</p>' .
391                                '<ul>' .
392                                        '<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>' .
393                                        '<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>' .
394                                        '<li>' . __( '<strong>View</strong> will take you to a public display page for that file.' ) . '</li>' .
395                                        '<li>' . __( '<strong>Copy URL</strong> copies the URL for the media file to your clipboard.' ) . '</li>' .
396                                        '<li>' . __( '<strong>Download file</strong> downloads the original media file to your device.' ) . '</li>' .
397                                '</ul>',
398        )
399);
400get_current_screen()->add_help_tab(
401        array(
402                'id'      => 'attaching-files',
403                'title'   => __( 'Attaching Files' ),
404                'content' =>
405                                '<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>',
406        )
407);
408
409get_current_screen()->set_help_sidebar(
410        '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
411        '<p>' . __( '<a href="https://wordpress.org/documentation/article/media-library-screen/">Documentation on Media Library</a>' ) . '</p>' .
412        '<p>' . __( '<a href="https://wordpress.org/support/forums/">Support forums</a>' ) . '</p>'
413);
414
415get_current_screen()->set_screen_reader_content(
416        array(
417                'heading_views'      => __( 'Filter media items list' ),
418                'heading_pagination' => __( 'Media items list navigation' ),
419                'heading_list'       => __( 'Media items list' ),
420        )
421);
422
423require_once ABSPATH . 'wp-admin/admin-header.php';
424?>
425
426<div class="wrap">
427<h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1>
428
429<?php
430if ( current_user_can( 'upload_files' ) ) {
431        ?>
432        <a href="<?php echo esc_url( admin_url( 'media-new.php' ) ); ?>" class="page-title-action"><?php echo esc_html__( 'Add New Media File' ); ?></a>
433                                                <?php
434}
435
436if ( isset( $_REQUEST['s'] ) && strlen( $_REQUEST['s'] ) ) {
437        echo '<span class="subtitle">';
438        printf(
439                /* translators: %s: Search query. */
440                __( 'Search results for: %s' ),
441                '<strong>' . get_search_query() . '</strong>'
442        );
443        echo '</span>';
444}
445?>
446
447<hr class="wp-header-end">
448
449<?php
450if ( ! empty( $message ) ) {
451        wp_admin_notice(
452                $message,
453                array(
454                        'id'                 => 'message',
455                        'additional_classes' => array( 'updated' ),
456                        'dismissible'        => true,
457                )
458        );
459}
460?>
461
462<form id="posts-filter" method="get">
463
464<?php $wp_list_table->views(); ?>
465
466<?php $wp_list_table->display(); ?>
467
468<div id="ajax-response"></div>
469<?php find_posts_div(); ?>
470</form>
471</div>
472
473<?php
474require_once ABSPATH . 'wp-admin/admin-footer.php';
Note: See TracBrowser for help on using the repository browser.