~canonical-sysadmins/wordpress/5.1

« back to all changes in this revision

Viewing changes to wp-admin/includes/class-wp-media-list-table.php

  • Committer: Barry Price
  • Date: 2019-02-22 03:51:26 UTC
  • mfrom: (1.2.12 upstream)
  • Revision ID: barry.price@canonical.com-20190222035126-o28k38qs8jfyjsxt
Merge WP5.1 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
 
43
43
                $this->modes = array(
44
44
                        'list' => __( 'List View' ),
45
 
                        'grid' => __( 'Grid View' )
 
45
                        'grid' => __( 'Grid View' ),
46
46
                );
47
47
 
48
 
                parent::__construct( array(
49
 
                        'plural' => 'media',
50
 
                        'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
51
 
                ) );
 
48
                parent::__construct(
 
49
                        array(
 
50
                                'plural' => 'media',
 
51
                                'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
 
52
                        )
 
53
                );
52
54
        }
53
55
 
54
56
        /**
55
 
         *
56
57
         * @return bool
57
58
         */
58
59
        public function ajax_user_can() {
59
 
                return current_user_can('upload_files');
 
60
                return current_user_can( 'upload_files' );
60
61
        }
61
62
 
62
63
        /**
63
 
         *
64
64
         * @global WP_Query $wp_query
65
65
         * @global array    $post_mime_types
66
66
         * @global array    $avail_post_mime_types
71
71
 
72
72
                list( $post_mime_types, $avail_post_mime_types ) = wp_edit_attachments_query( $_REQUEST );
73
73
 
74
 
                $this->is_trash = isset( $_REQUEST['attachment-filter'] ) && 'trash' === $_REQUEST['attachment-filter'];
75
 
 
76
 
                $mode = empty( $_REQUEST['mode'] ) ? 'list' : $_REQUEST['mode'];
77
 
 
78
 
                $this->set_pagination_args( array(
79
 
                        'total_items' => $wp_query->found_posts,
80
 
                        'total_pages' => $wp_query->max_num_pages,
81
 
                        'per_page' => $wp_query->query_vars['posts_per_page'],
82
 
                ) );
 
74
                $this->is_trash = isset( $_REQUEST['attachment-filter'] ) && 'trash' === $_REQUEST['attachment-filter'];
 
75
 
 
76
                $mode = empty( $_REQUEST['mode'] ) ? 'list' : $_REQUEST['mode'];
 
77
 
 
78
                $this->set_pagination_args(
 
79
                        array(
 
80
                                'total_items' => $wp_query->found_posts,
 
81
                                'total_pages' => $wp_query->max_num_pages,
 
82
                                'per_page'    => $wp_query->query_vars['posts_per_page'],
 
83
                        )
 
84
                );
83
85
        }
84
86
 
85
87
        /**
112
114
                                false
113
115
                        );
114
116
 
115
 
                        $type_links[$mime_type] = sprintf(
 
117
                        $type_links[ $mime_type ] = sprintf(
116
118
                                '<option value="post_mime_type:%s"%s>%s</option>',
117
119
                                esc_attr( $mime_type ),
118
120
                                $selected,
128
130
                        _x( 'Mine', 'media items' )
129
131
                );
130
132
 
131
 
                if ( $this->is_trash || ( defined( 'MEDIA_TRASH') && MEDIA_TRASH ) ) {
 
133
                if ( $this->is_trash || ( defined( 'MEDIA_TRASH' ) && MEDIA_TRASH ) ) {
132
134
                        $type_links['trash'] = sprintf(
133
135
                                '<option value="trash"%s>%s</option>',
134
136
                                selected( 'trash' === $filter, true, false ),
140
142
        }
141
143
 
142
144
        /**
143
 
         *
144
145
         * @return array
145
146
         */
146
147
        protected function get_bulk_actions() {
148
149
                if ( MEDIA_TRASH ) {
149
150
                        if ( $this->is_trash ) {
150
151
                                $actions['untrash'] = __( 'Restore' );
151
 
                                $actions['delete'] = __( 'Delete Permanently' );
 
152
                                $actions['delete']  = __( 'Delete Permanently' );
152
153
                        } else {
153
154
                                $actions['trash'] = _x( 'Trash', 'verb' );
154
155
                        }
156
157
                        $actions['delete'] = __( 'Delete Permanently' );
157
158
                }
158
159
 
159
 
                if ( $this->detached )
 
160
                if ( $this->detached ) {
160
161
                        $actions['attach'] = __( 'Attach' );
 
162
                }
161
163
 
162
164
                return $actions;
163
165
        }
169
171
                if ( 'bar' !== $which ) {
170
172
                        return;
171
173
                }
172
 
?>
 
174
                ?>
173
175
                <div class="actions">
174
 
<?php
 
176
                <?php
175
177
                if ( ! is_singular() ) {
176
178
                        if ( ! $this->is_trash ) {
177
179
                                $this->months_dropdown( 'attachment' );
185
187
 
186
188
                if ( $this->is_trash && current_user_can( 'edit_others_posts' ) && $this->has_items() ) {
187
189
                        submit_button( __( 'Empty Trash' ), 'apply', 'delete_all', false );
188
 
                } ?>
 
190
                }
 
191
                ?>
189
192
                </div>
190
 
<?php
 
193
                <?php
191
194
        }
192
195
 
193
196
        /**
194
 
         *
195
197
         * @return string
196
198
         */
197
199
        public function current_action() {
198
 
                if ( isset( $_REQUEST['found_post_id'] ) && isset( $_REQUEST['media'] ) )
 
200
                if ( isset( $_REQUEST['found_post_id'] ) && isset( $_REQUEST['media'] ) ) {
199
201
                        return 'attach';
 
202
                }
200
203
 
201
 
                if ( isset( $_REQUEST['parent_post_id'] ) && isset( $_REQUEST['media'] ) )
 
204
                if ( isset( $_REQUEST['parent_post_id'] ) && isset( $_REQUEST['media'] ) ) {
202
205
                        return 'detach';
 
206
                }
203
207
 
204
 
                if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) )
 
208
                if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) ) {
205
209
                        return 'delete_all';
 
210
                }
206
211
 
207
212
                return parent::current_action();
208
213
        }
209
214
 
210
215
        /**
211
 
         *
212
216
         * @return bool
213
217
         */
214
218
        public function has_items() {
232
236
                $views = $this->get_views();
233
237
 
234
238
                $this->screen->render_screen_reader_content( 'heading_views' );
235
 
?>
 
239
                ?>
236
240
<div class="wp-filter">
237
241
        <div class="filter-items">
238
242
                <?php $this->view_switcher( $mode ); ?>
248
252
                        ?>
249
253
                </select>
250
254
 
251
 
<?php
 
255
                <?php
252
256
                $this->extra_tablenav( 'bar' );
253
257
 
254
258
                /** This filter is documented in wp-admin/inclues/class-wp-list-table.php */
262
266
                        }
263
267
                        echo '</ul>';
264
268
                }
265
 
?>
 
269
                ?>
266
270
        </div>
267
271
 
268
272
        <div class="search-form">
269
273
                <label for="media-search-input" class="screen-reader-text"><?php esc_html_e( 'Search Media' ); ?></label>
270
 
                <input type="search" placeholder="<?php esc_attr_e( 'Search media items...' ) ?>" id="media-search-input" class="search" name="s" value="<?php _admin_search_query(); ?>"></div>
 
274
                <input type="search" placeholder="<?php esc_attr_e( 'Search media items...' ); ?>" id="media-search-input" class="search" name="s" value="<?php _admin_search_query(); ?>"></div>
271
275
        </div>
272
 
        <?php
 
276
                <?php
273
277
        }
274
278
 
275
279
        /**
276
 
         *
277
280
         * @return array
278
281
         */
279
282
        public function get_columns() {
280
 
                $posts_columns = array();
 
283
                $posts_columns       = array();
281
284
                $posts_columns['cb'] = '<input type="checkbox" />';
282
285
                /* translators: column name */
283
 
                $posts_columns['title'] = _x( 'File', 'column name' );
 
286
                $posts_columns['title']  = _x( 'File', 'column name' );
284
287
                $posts_columns['author'] = __( 'Author' );
285
288
 
286
289
                $taxonomies = get_taxonomies_for_attachments( 'objects' );
291
294
                 *
292
295
                 * @since 3.5.0
293
296
                 *
294
 
                 * @param array  $taxonomies An array of registered taxonomies to show for attachments.
295
 
                 * @param string $post_type  The post type. Default 'attachment'.
 
297
                 * @param string[] $taxonomies An array of registered taxonomy names to show for attachments.
 
298
                 * @param string   $post_type  The post type. Default 'attachment'.
296
299
                 */
297
300
                $taxonomies = apply_filters( 'manage_taxonomies_for_attachment_columns', $taxonomies, 'attachment' );
298
301
                $taxonomies = array_filter( $taxonomies, 'taxonomy_exists' );
309
312
                }
310
313
 
311
314
                /* translators: column name */
312
 
                if ( !$this->detached ) {
 
315
                if ( ! $this->detached ) {
313
316
                        $posts_columns['parent'] = _x( 'Uploaded to', 'column name' );
314
 
                        if ( post_type_supports( 'attachment', 'comments' ) )
 
317
                        if ( post_type_supports( 'attachment', 'comments' ) ) {
315
318
                                $posts_columns['comments'] = '<span class="vers comment-grey-bubble" title="' . esc_attr__( 'Comments' ) . '"><span class="screen-reader-text">' . __( 'Comments' ) . '</span></span>';
 
319
                        }
316
320
                }
317
321
                /* translators: column name */
318
322
                $posts_columns['date'] = _x( 'Date', 'column name' );
321
325
                 *
322
326
                 * @since 2.5.0
323
327
                 *
324
 
                 * @param array $posts_columns An array of columns displayed in the Media list table.
325
 
                 * @param bool  $detached      Whether the list table contains media not attached
326
 
                 *                             to any posts. Default true.
 
328
                 * @param string[] $posts_columns An array of columns displayed in the Media list table.
 
329
                 * @param bool     $detached      Whether the list table contains media not attached
 
330
                 *                                to any posts. Default true.
327
331
                 */
328
332
                return apply_filters( 'manage_media_columns', $posts_columns, $this->detached );
329
333
        }
330
334
 
331
335
        /**
332
 
         *
333
336
         * @return array
334
337
         */
335
338
        protected function get_sortable_columns() {
350
353
         * @param WP_Post $post The current WP_Post object.
351
354
         */
352
355
        public function column_cb( $post ) {
353
 
                if ( current_user_can( 'edit_post', $post->ID ) ) { ?>
354
 
                        <label class="screen-reader-text" for="cb-select-<?php echo $post->ID; ?>"><?php
355
 
                                echo sprintf( __( 'Select %s' ), _draft_or_post_title() );
356
 
                        ?></label>
 
356
                if ( current_user_can( 'edit_post', $post->ID ) ) {
 
357
                        ?>
 
358
                        <label class="screen-reader-text" for="cb-select-<?php echo $post->ID; ?>">
 
359
                                                                                                                                                <?php
 
360
                                                                                                                                                echo sprintf( __( 'Select %s' ), _draft_or_post_title() );
 
361
                                                                                                                                                ?>
 
362
                        </label>
357
363
                        <input type="checkbox" name="media[]" id="cb-select-<?php echo $post->ID; ?>" value="<?php echo $post->ID; ?>" />
358
 
                <?php }
 
364
                        <?php
 
365
                }
359
366
        }
360
367
 
361
368
        /**
368
375
        public function column_title( $post ) {
369
376
                list( $mime ) = explode( '/', $post->post_mime_type );
370
377
 
371
 
                $title = _draft_or_post_title();
372
 
                $thumb = wp_get_attachment_image( $post->ID, array( 60, 60 ), true, array( 'alt' => '' ) );
 
378
                $title      = _draft_or_post_title();
 
379
                $thumb      = wp_get_attachment_image( $post->ID, array( 60, 60 ), true, array( 'alt' => '' ) );
373
380
                $link_start = $link_end = '';
374
381
 
375
382
                if ( current_user_can( 'edit_post', $post->ID ) && ! $this->is_trash ) {
387
394
                <strong<?php echo $class; ?>>
388
395
                        <?php
389
396
                        echo $link_start;
390
 
                        if ( $thumb ) : ?>
 
397
                        if ( $thumb ) :
 
398
                                ?>
391
399
                                <span class="media-icon <?php echo sanitize_html_class( $mime . '-icon' ); ?>"><?php echo $thumb; ?></span>
392
 
                        <?php endif;
 
400
                                <?php
 
401
                        endif;
393
402
                        echo $title . $link_end;
394
403
                        _media_states( $post );
395
404
                        ?>
412
421
         * @param WP_Post $post The current WP_Post object.
413
422
         */
414
423
        public function column_author( $post ) {
415
 
                printf( '<a href="%s">%s</a>',
416
 
                        esc_url( add_query_arg( array( 'author' => get_the_author_meta('ID') ), 'upload.php' ) ),
 
424
                printf(
 
425
                        '<a href="%s">%s</a>',
 
426
                        esc_url( add_query_arg( array( 'author' => get_the_author_meta( 'ID' ) ), 'upload.php' ) ),
417
427
                        get_the_author()
418
428
                );
419
429
        }
441
451
                        $h_time = __( 'Unpublished' );
442
452
                } else {
443
453
                        $m_time = $post->post_date;
444
 
                        $time = get_post_time( 'G', true, $post, false );
 
454
                        $time   = get_post_time( 'G', true, $post, false );
445
455
                        if ( ( abs( $t_diff = time() - $time ) ) < DAY_IN_SECONDS ) {
446
456
                                if ( $t_diff < 0 ) {
447
457
                                        $h_time = sprintf( __( '%s from now' ), human_time_diff( $time ) );
473
483
                }
474
484
 
475
485
                if ( $parent ) {
476
 
                        $title = _draft_or_post_title( $post->post_parent );
 
486
                        $title       = _draft_or_post_title( $post->post_parent );
477
487
                        $parent_type = get_post_type_object( $parent->post_type );
478
488
 
479
489
                        if ( $parent_type && $parent_type->show_ui && current_user_can( 'edit_post', $post->post_parent ) ) {
480
 
?>
 
490
                                ?>
481
491
                                <strong><a href="<?php echo get_edit_post_link( $post->post_parent ); ?>">
482
 
                                        <?php echo $title ?></a></strong><?php
 
492
                                        <?php echo $title; ?></a></strong>
 
493
                                                                <?php
483
494
                        } elseif ( $parent_type && current_user_can( 'read_post', $post->post_parent ) ) {
484
 
?>
485
 
                                <strong><?php echo $title ?></strong><?php
 
495
                                ?>
 
496
                                <strong><?php echo $title; ?></strong>
 
497
                                                                        <?php
486
498
                        } else {
487
499
                                _e( '(Private post)' );
488
500
                        }
489
501
 
490
 
                        if ( $user_can_edit ):
491
 
                                $detach_url = add_query_arg( array(
492
 
                                        'parent_post_id' => $post->post_parent,
493
 
                                        'media[]' => $post->ID,
494
 
                                        '_wpnonce' => wp_create_nonce( 'bulk-' . $this->_args['plural'] )
495
 
                                ), 'upload.php' );
 
502
                        if ( $user_can_edit ) :
 
503
                                $detach_url = add_query_arg(
 
504
                                        array(
 
505
                                                'parent_post_id' => $post->post_parent,
 
506
                                                'media[]'        => $post->ID,
 
507
                                                '_wpnonce'       => wp_create_nonce( 'bulk-' . $this->_args['plural'] ),
 
508
                                        ),
 
509
                                        'upload.php'
 
510
                                );
496
511
                                printf(
497
512
                                        '<br /><a href="%s" class="hide-if-no-js detach-from-parent" aria-label="%s">%s</a>',
498
513
                                        $detach_url,
502
517
                                );
503
518
                        endif;
504
519
                } else {
505
 
                        _e( '(Unattached)' ); ?>
506
 
                        <?php if ( $user_can_edit ) {
 
520
                        _e( '(Unattached)' );
 
521
                        ?>
 
522
                        <?php
 
523
                        if ( $user_can_edit ) {
507
524
                                $title = _draft_or_post_title( $post->post_parent );
508
525
                                printf(
509
526
                                        '<br /><a href="#the-list" onclick="findPosts.open( \'media[]\', \'%s\' ); return false;" class="hide-if-no-js aria-button-if-js" aria-label="%s">%s</a>',
561
578
                        if ( is_array( $terms ) ) {
562
579
                                $out = array();
563
580
                                foreach ( $terms as $t ) {
564
 
                                        $posts_in_term_qv = array();
 
581
                                        $posts_in_term_qv             = array();
565
582
                                        $posts_in_term_qv['taxonomy'] = $taxonomy;
566
 
                                        $posts_in_term_qv['term'] = $t->slug;
 
583
                                        $posts_in_term_qv['term']     = $t->slug;
567
584
 
568
 
                                        $out[] = sprintf( '<a href="%s">%s</a>',
 
585
                                        $out[] = sprintf(
 
586
                                                '<a href="%s">%s</a>',
569
587
                                                esc_url( add_query_arg( $posts_in_term_qv, 'upload.php' ) ),
570
588
                                                esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) )
571
589
                                        );
593
611
        }
594
612
 
595
613
        /**
596
 
         *
597
614
         * @global WP_Post $post
598
615
         */
599
616
        public function display_rows() {
604
621
 
605
622
                $this->comment_pending_count = get_pending_comments_num( $post_ids );
606
623
 
607
 
                add_filter( 'the_title','esc_html' );
 
624
                add_filter( 'the_title', 'esc_html' );
608
625
 
609
 
                while ( have_posts() ) : the_post();
 
626
                while ( have_posts() ) :
 
627
                        the_post();
610
628
                        if (
611
629
                                ( $this->is_trash && $post->post_status != 'trash' )
612
630
                                || ( ! $this->is_trash && $post->post_status === 'trash' )
614
632
                                continue;
615
633
                        }
616
634
                        $post_owner = ( get_current_user_id() == $post->post_author ) ? 'self' : 'other';
617
 
                ?>
 
635
                        ?>
618
636
                        <tr id="post-<?php echo $post->ID; ?>" class="<?php echo trim( ' author-' . $post_owner . ' status-' . $post->post_status ); ?>">
619
637
                                <?php $this->single_row_columns( $post ); ?>
620
638
                        </tr>
621
 
                <?php
 
639
                        <?php
622
640
                endwhile;
623
641
        }
624
642
 
662
680
                                                _x( 'Trash', 'verb' )
663
681
                                        );
664
682
                                } else {
665
 
                                        $delete_ays = ! MEDIA_TRASH ? " onclick='return showNotice.warn();'" : '';
 
683
                                        $delete_ays        = ! MEDIA_TRASH ? " onclick='return showNotice.warn();'" : '';
666
684
                                        $actions['delete'] = sprintf(
667
685
                                                '<a href="%s" class="submitdelete aria-button-if-js"%s aria-label="%s">%s</a>',
668
686
                                                wp_nonce_url( "post.php?action=delete&amp;post=$post->ID", 'delete-post_' . $post->ID ),
690
708
                                        __( 'Attach' )
691
709
                                );
692
710
                        }
693
 
                }
694
 
                else {
695
 
                        if ( current_user_can( 'edit_post', $post->ID ) && !$this->is_trash ) {
 
711
                } else {
 
712
                        if ( current_user_can( 'edit_post', $post->ID ) && ! $this->is_trash ) {
696
713
                                $actions['edit'] = sprintf(
697
714
                                        '<a href="%s" aria-label="%s">%s</a>',
698
715
                                        get_edit_post_link( $post->ID ),
720
737
                                        );
721
738
                                }
722
739
                                if ( $this->is_trash || ! EMPTY_TRASH_DAYS || ! MEDIA_TRASH ) {
723
 
                                        $delete_ays = ( !$this->is_trash && !MEDIA_TRASH ) ? " onclick='return showNotice.warn();'" : '';
 
740
                                        $delete_ays        = ( ! $this->is_trash && ! MEDIA_TRASH ) ? " onclick='return showNotice.warn();'" : '';
724
741
                                        $actions['delete'] = sprintf(
725
742
                                                '<a href="%s" class="submitdelete aria-button-if-js"%s aria-label="%s">%s</a>',
726
743
                                                wp_nonce_url( "post.php?action=delete&amp;post=$post->ID", 'delete-post_' . $post->ID ),
747
764
                 *
748
765
                 * @since 2.8.0
749
766
                 *
750
 
                 * @param array   $actions  An array of action links for each attachment.
751
 
                 *                          Default 'Edit', 'Delete Permanently', 'View'.
752
 
                 * @param WP_Post $post     WP_Post object for the current attachment.
753
 
                 * @param bool    $detached Whether the list table contains media not attached
754
 
                 *                          to any posts. Default true.
 
767
                 * @param string[] $actions  An array of action links for each attachment.
 
768
                 *                           Default 'Edit', 'Delete Permanently', 'View'.
 
769
                 * @param WP_Post  $post     WP_Post object for the current attachment.
 
770
                 * @param bool     $detached Whether the list table contains media not attached
 
771
                 *                           to any posts. Default true.
755
772
                 */
756
773
                return apply_filters( 'media_row_actions', $actions, $post, $this->detached );
757
774
        }