Make WordPress Core


Ignore:
Timestamp:
06/13/2015 03:56:13 PM (11 years ago)
Author:
wonderboymusic
Message:

In WP_Media_List_Table::display_rows():

  • Move the giant switch statement into methods
  • Call -single_row_columns(), which it inherits from WP_List_Table

See #29881.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-media-list-table.php

    r32737 r32754  
    312312
    313313        /**
     314         * @since 4.3.0
     315         *
     316         * @param WP_Post $post
     317         */
     318        public function column_cb( $post ) {
     319                if ( current_user_can( 'edit_post', $post->ID ) ) { ?>
     320                        <label class="screen-reader-text" for="cb-select-<?php echo $post->ID; ?>"><?php
     321                                echo sprintf( __( 'Select %s' ), _draft_or_post_title() );
     322                        ?></label>
     323                        <input type="checkbox" name="media[]" id="cb-select-<?php echo $post->ID; ?>" value="<?php echo $post->ID; ?>" />
     324                <?php }
     325        }
     326
     327        /**
     328         * @since 4.3.0
     329         *
     330         * @param WP_Post $post
     331         */
     332        public function column_title( $post ) {
     333                list( $mime ) = explode( '/', $post->post_mime_type );
     334
     335                $user_can_edit = current_user_can( 'edit_post', $post->ID );
     336                $att_title = _draft_or_post_title();
     337                ?>
     338                <div class="media-icon <?php echo $mime ?>-icon">
     339                <?php
     340
     341                $thumb = wp_get_attachment_image( $post->ID, array( 60, 60 ), true );
     342                if ( $thumb ) {
     343                        if ( $this->is_trash || ! $user_can_edit ) {
     344                                echo $thumb;
     345                        } else { ?>
     346                        <a href="<?php echo get_edit_post_link( $post->ID ); ?>" title="<?php
     347                                echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) );
     348                        ?>"><?php echo $thumb; ?></a><?php
     349                        }
     350                }
     351
     352                ?>
     353                </div>
     354                <div class="media-info">
     355                        <strong>
     356                        <?php
     357                                if ( $this->is_trash || ! $user_can_edit ) {
     358                                        echo $att_title;
     359                                } else { ?>
     360                                        <a href="<?php echo get_edit_post_link( $post->ID ); ?>" title="<?php
     361                                                echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) );
     362                                        ?>"><?php echo $att_title; ?></a><?php
     363                                }
     364                                _media_states( $post );
     365                        ?>
     366                        </strong>
     367                        <p class="filename"><?php echo wp_basename( $post->guid ); ?></p>
     368                </div>
     369                <?php
     370        }
     371
     372        /**
     373         * @since 4.3.0
     374         *
     375         * @param WP_Post $post
     376         */
     377        public function column_author( $post ) {
     378                printf( '<a href="%s">%s</a>',
     379                        esc_url( add_query_arg( array( 'author' => get_the_author_meta('ID') ), 'upload.php' ) ),
     380                        get_the_author()
     381                );
     382        }
     383
     384        /**
     385         * @since 4.3.0
     386         *
     387         * @param WP_Post $post
     388         */
     389        public function column_desc( $post ) {
     390                echo has_excerpt() ? $post->post_excerpt : '';
     391        }
     392
     393        /**
     394         * @since 4.3.0
     395         *
     396         * @param WP_Post $post
     397         */
     398        public function column_date( $post ) {
     399                if ( '0000-00-00 00:00:00' == $post->post_date ) {
     400                        $h_time = __( 'Unpublished' );
     401                } else {
     402                        $m_time = $post->post_date;
     403                        $time = get_post_time( 'G', true, $post, false );
     404                        if ( ( abs( $t_diff = time() - $time ) ) < DAY_IN_SECONDS ) {
     405                                if ( $t_diff < 0 ) {
     406                                        $h_time = sprintf( __( '%s from now' ), human_time_diff( $time ) );
     407                                } else {
     408                                        $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
     409                                }
     410                        } else {
     411                                $h_time = mysql2date( __( 'Y/m/d' ), $m_time );
     412                        }
     413                }
     414
     415                echo $h_time;
     416        }
     417
     418        /**
     419         * @since 4.3.0
     420         *
     421         * @param WP_Post $post
     422         */
     423        public function column_parent( $post ) {
     424                $user_can_edit = current_user_can( 'edit_post', $post->ID );
     425
     426                if ( $post->post_parent > 0 ) {
     427                        $parent = get_post( $post->post_parent );
     428                } else {
     429                        $parent = false;
     430                }
     431
     432                if ( $parent ) {
     433                        $title = _draft_or_post_title( $post->post_parent );
     434                        $parent_type = get_post_type_object( $parent->post_type );
     435?>
     436                        <strong>
     437                        <?php if ( $parent_type && $parent_type->show_ui && current_user_can( 'edit_post', $post->post_parent ) ) { ?>
     438                                <a href="<?php echo get_edit_post_link( $post->post_parent ); ?>">
     439                                        <?php echo $title ?></a><?php
     440                        } else {
     441                                echo $title;
     442                        } ?></strong>,
     443                        <?php echo get_the_time( __( 'Y/m/d' ) ); ?><br />
     444                        <?php
     445                        if ( $user_can_edit ):
     446                                $detach_url = add_query_arg( array(
     447                                        'parent_post_id' => $post->post_parent,
     448                                        'media[]' => $post->ID,
     449                                        '_wpnonce' => wp_create_nonce( 'bulk-' . $this->_args['plural'] )
     450                                ), 'upload.php' ); ?>
     451                        <a class="hide-if-no-js detach-from-parent" href="<?php echo $detach_url ?>"><?php _e( 'Detach' ); ?></a>
     452                        <?php endif;
     453                } else {
     454                        _e( '(Unattached)' ); ?><br />
     455                        <?php if ( $user_can_edit ) { ?>
     456                                <a class="hide-if-no-js"
     457                                        onclick="findPosts.open( 'media[]','<?php echo $post->ID ?>' ); return false;"
     458                                        href="#the-list">
     459                                        <?php _e( 'Attach' ); ?></a>
     460                        <?php }
     461                }
     462        }
     463
     464        /**
     465         * @since 4.3.0
     466         *
     467         * @param WP_Post $post
     468         */
     469        public function column_comments( $post ) {
     470                echo '<div class="post-com-count-wrapper">';
     471
     472                $pending_comments = get_pending_comments_num( $post->ID );
     473                $this->comments_bubble( $post->ID, $pending_comments );
     474
     475                echo '</div>';
     476        }
     477
     478        /**
     479         * @since 4.3.0
     480         *
     481         * @param WP_Post $post
     482         * @param string  $column_name
     483         */
     484        public function column_default( $post, $column_name ) {
     485                if ( 'categories' == $column_name ) {
     486                        $taxonomy = 'category';
     487                } elseif ( 'tags' == $column_name ) {
     488                        $taxonomy = 'post_tag';
     489                } elseif ( 0 === strpos( $column_name, 'taxonomy-' ) ) {
     490                        $taxonomy = substr( $column_name, 9 );
     491                } else {
     492                        $taxonomy = false;
     493                }
     494
     495                if ( $taxonomy ) {
     496                        $terms = get_the_terms( $post->ID, $taxonomy );
     497                        if ( is_array( $terms ) ) {
     498                                $out = array();
     499                                foreach ( $terms as $t ) {
     500                                        $posts_in_term_qv = array();
     501                                        $posts_in_term_qv['taxonomy'] = $taxonomy;
     502                                        $posts_in_term_qv['term'] = $t->slug;
     503
     504                                        $out[] = sprintf( '<a href="%s">%s</a>',
     505                                                esc_url( add_query_arg( $posts_in_term_qv, 'upload.php' ) ),
     506                                                esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) )
     507                                        );
     508                                }
     509                                /* translators: used between list items, there is a space after the comma */
     510                                echo join( __( ', ' ), $out );
     511                        } else {
     512                                echo '<span aria-hidden="true">&#8212;</span><span class="screen-reader-text">' . get_taxonomy( $taxonomy )->labels->not_found . '</span>';
     513                        }
     514
     515                        return;
     516                }
     517
     518                /**
     519                 * Fires for each custom column in the Media list table.
     520                 *
     521                 * Custom columns are registered using the {@see 'manage_media_columns'} filter.
     522                 *
     523                 * @since 2.5.0
     524                 *
     525                 * @param string $column_name Name of the custom column.
     526                 * @param int    $post_id     Attachment ID.
     527                 */
     528                do_action( 'manage_media_custom_column', $column_name, $post->ID );
     529        }
     530
     531        /**
    314532         *
    315533         * @global WP_Post $post
     
    321539
    322540                while ( have_posts() ) : the_post();
    323                         $user_can_edit = current_user_can( 'edit_post', $post->ID );
    324 
    325                         if ( $this->is_trash && $post->post_status != 'trash'
    326                         ||  !$this->is_trash && $post->post_status == 'trash' )
     541                        if (
     542                                ( $this->is_trash && $post->post_status != 'trash' )
     543                                || ( ! $this->is_trash && $post->post_status == 'trash' )
     544                        ) {
    327545                                continue;
    328 
     546                        }
    329547                        $post_owner = ( get_current_user_id() == $post->post_author ) ? 'self' : 'other';
    330                         $att_title = _draft_or_post_title();
    331 ?>
    332         <tr id="post-<?php echo $post->ID; ?>" class="<?php echo trim( ' author-' . $post_owner . ' status-' . $post->post_status ); ?>">
    333 <?php
    334 
    335 list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
    336 
    337 foreach ( $columns as $column_name => $column_display_name ) {
    338         $classes = "$column_name column-$column_name";
    339         if ( $primary === $column_name ) {
    340                 $classes .= ' has-row-actions column-primary';
    341         }
    342 
    343         if ( in_array( $column_name, $hidden ) ) {
    344                 $classes .= ' hidden';
    345         }
    346 
    347         $attributes = "class='$classes'";
    348 
    349         if ( 'cb' === $column_name ) {
    350 ?>
    351                 <th scope="row" class="check-column">
    352                         <?php if ( $user_can_edit ) { ?>
    353                                 <label class="screen-reader-text" for="cb-select-<?php the_ID(); ?>"><?php echo sprintf( __( 'Select %s' ), $att_title );?></label>
    354                                 <input type="checkbox" name="media[]" id="cb-select-<?php the_ID(); ?>" value="<?php the_ID(); ?>" />
    355                         <?php } ?>
    356                 </th>
    357 <?php
    358         } else {
    359                 echo "<td $attributes>";
    360 
    361                 switch ( $column_name ) {
    362                         case 'title':
    363                                 list( $mime ) = explode( '/', $post->post_mime_type );
    364 
    365                                 echo "<div class='media-icon {$mime}-icon'>";
    366 
    367                                 if ( $thumb = wp_get_attachment_image( $post->ID, array( 60, 60 ), true ) ) {
    368                                         if ( $this->is_trash || ! $user_can_edit ) {
    369                                                 echo $thumb;
    370                                         } else { ?>
    371                                         <a href="<?php echo get_edit_post_link( $post->ID ); ?>" title="<?php echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ); ?>">
    372                                                 <?php echo $thumb; ?>
    373                                         </a><?php
    374                                         }
    375                                 }
    376 
    377                                 echo '</div><div class="media-info">';
    378 ?>
    379                                         <strong>
    380                                         <?php if ( $this->is_trash || ! $user_can_edit ) {
    381                                                 echo $att_title;
    382                                         } else { ?>
    383                                         <a href="<?php echo get_edit_post_link( $post->ID ); ?>"
    384                                                 title="<?php echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ); ?>">
    385                                                 <?php echo $att_title; ?></a>
    386                                         <?php }
    387                                         _media_states( $post ); ?></strong>
    388                                         <p class="filename"><?php echo wp_basename( $post->guid ); ?></p>
    389                                 </div>
    390 <?php
    391                                 break;
    392 
    393                         case 'author':
    394                                 printf( '<a href="%s">%s</a>',
    395                                         esc_url( add_query_arg( array( 'author' => get_the_author_meta('ID') ), 'upload.php' ) ),
    396                                         get_the_author()
    397                                 );
    398 
    399                                 break;
    400 
    401                         case 'desc':
    402                                 echo has_excerpt() ? $post->post_excerpt : '';
    403                                 break;
    404 
    405                         case 'date':
    406                                 if ( '0000-00-00 00:00:00' == $post->post_date ) {
    407                                         $h_time = __( 'Unpublished' );
    408                                 } else {
    409                                         $m_time = $post->post_date;
    410                                         $time = get_post_time( 'G', true, $post, false );
    411                                         if ( ( abs( $t_diff = time() - $time ) ) < DAY_IN_SECONDS ) {
    412                                                 if ( $t_diff < 0 )
    413                                                         $h_time = sprintf( __( '%s from now' ), human_time_diff( $time ) );
    414                                                 else
    415                                                         $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
    416                                         } else {
    417                                                 $h_time = mysql2date( __( 'Y/m/d' ), $m_time );
    418                                         }
    419                                 }
    420 
    421                                 echo $h_time;
    422                                 break;
    423 
    424                         case 'parent':
    425                                 if ( $post->post_parent > 0 )
    426                                         $parent = get_post( $post->post_parent );
    427                                 else
    428                                         $parent = false;
    429 
    430                                 if ( $parent ) {
    431                                         $title = _draft_or_post_title( $post->post_parent );
    432                                         $parent_type = get_post_type_object( $parent->post_type );
    433548                ?>
    434                                         <strong>
    435                                         <?php if ( $parent_type && $parent_type->show_ui && current_user_can( 'edit_post', $post->post_parent ) ) { ?>
    436                                                 <a href="<?php echo get_edit_post_link( $post->post_parent ); ?>">
    437                                                         <?php echo $title ?></a><?php
    438                                         } else {
    439                                                 echo $title;
    440                                         } ?></strong>,
    441                                         <?php echo get_the_time( __( 'Y/m/d' ) ); ?><br />
    442                                         <?php
    443                                         if ( $user_can_edit ):
    444                                                 $detach_url = add_query_arg( array(
    445                                                         'parent_post_id' => $post->post_parent,
    446                                                         'media[]' => $post->ID,
    447                                                         '_wpnonce' => wp_create_nonce( 'bulk-' . $this->_args['plural'] )
    448                                                 ), 'upload.php' ); ?>
    449                                         <a class="hide-if-no-js detach-from-parent" href="<?php echo $detach_url ?>"><?php _e( 'Detach' ); ?></a>
    450                                         <?php endif;
    451                                 } else {
    452                                         _e( '(Unattached)' ); ?><br />
    453                                         <?php if ( $user_can_edit ) { ?>
    454                                                 <a class="hide-if-no-js"
    455                                                         onclick="findPosts.open( 'media[]','<?php echo $post->ID ?>' ); return false;"
    456                                                         href="#the-list">
    457                                                         <?php _e( 'Attach' ); ?></a>
    458                                         <?php }
    459                                 }
    460 
    461                                 break;
    462 
    463                         case 'comments':
    464                                 echo '<div class="post-com-count-wrapper">';
    465 
    466                                 $pending_comments = get_pending_comments_num( $post->ID );
    467                                 $this->comments_bubble( $post->ID, $pending_comments );
    468 
    469                                 echo '</div>';
    470 
    471                                 break;
    472 
    473                         default:
    474                                 if ( 'categories' == $column_name )
    475                                         $taxonomy = 'category';
    476                                 elseif ( 'tags' == $column_name )
    477                                         $taxonomy = 'post_tag';
    478                                 elseif ( 0 === strpos( $column_name, 'taxonomy-' ) )
    479                                         $taxonomy = substr( $column_name, 9 );
    480                                 else
    481                                         $taxonomy = false;
    482 
    483                                 if ( $taxonomy ) {
    484                                         $terms = get_the_terms( $post->ID, $taxonomy );
    485                                         if ( is_array( $terms ) ) {
    486                                                 $out = array();
    487                                                 foreach ( $terms as $t ) {
    488                                                         $posts_in_term_qv = array();
    489                                                         $posts_in_term_qv['taxonomy'] = $taxonomy;
    490                                                         $posts_in_term_qv['term'] = $t->slug;
    491 
    492                                                         $out[] = sprintf( '<a href="%s">%s</a>',
    493                                                                 esc_url( add_query_arg( $posts_in_term_qv, 'upload.php' ) ),
    494                                                                 esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) )
    495                                                         );
    496                                                 }
    497                                                 /* translators: used between list items, there is a space after the comma */
    498                                                 echo join( __( ', ' ), $out );
    499                                         } else {
    500                                                 echo '<span aria-hidden="true">&#8212;</span><span class="screen-reader-text">' . get_taxonomy( $taxonomy )->labels->not_found . '</span>';
    501                                         }
    502 
    503                                         break;
    504                                 }
    505 
    506                                 /**
    507                                  * Fires for each custom column in the Media list table.
    508                                  *
    509                                  * Custom columns are registered using the {@see 'manage_media_columns'} filter.
    510                                  *
    511                                  * @since 2.5.0
    512                                  *
    513                                  * @param string $column_name Name of the custom column.
    514                                  * @param int    $post_id     Attachment ID.
    515                                  */
    516                                 do_action( 'manage_media_custom_column', $column_name, $post->ID );
    517                                 break;
    518                 }
    519 
    520                 if( $primary === $column_name ) {
    521                         echo $this->row_actions( $this->_get_row_actions( $post, $att_title ) );
    522                 }
    523 
    524                 echo '</td>';
    525         }
    526 }
    527 ?>
    528         </tr>
    529 <?php endwhile;
     549                        <tr id="post-<?php echo $post->ID; ?>" class="<?php echo trim( ' author-' . $post_owner . ' status-' . $post->post_status ); ?>">
     550                                <?php $this->single_row_columns( $post ); ?>
     551                        </tr>
     552                <?php
     553                endwhile;
    530554        }
    531555
Note: See TracChangeset for help on using the changeset viewer.