Make WordPress Core


Ignore:
Timestamp:
05/29/2015 02:40:52 AM (10 years ago)
Author:
helen
Message:

List tables: introduce the concept of a "primary" column.

This becomes the column that contains the row actions, and allows for a more flexibility, particularly with custom post types and list tables. To (re)define the primary column, use the list_table_primary_column filter, which receives the column name and the screen ID as arguments.

props stephdau, DaveAl, jesin.
see #25408.

File:
1 edited

Legend:

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

    r32642 r32644  
    309309<?php
    310310
    311 list( $columns, $hidden ) = $this->get_column_info();
     311list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
     312
    312313foreach ( $columns as $column_name => $column_display_name ) {
    313     $class = "class='$column_name column-$column_name'";
     314    $classes = "$column_name column-$column_name";
     315    if ( $primary === $column_name ) {
     316        $classes .= ' has-row-actions column-primary';
     317    }
    314318
    315319    $style = '';
    316     if ( in_array( $column_name, $hidden ) )
     320    if ( in_array( $column_name, $hidden ) ) {
    317321        $style = ' style="display:none;"';
    318 
    319     $attributes = $class . $style;
    320 
    321     switch ( $column_name ) {
    322 
    323     case 'cb':
     322    }
     323
     324    $attributes = "class='$classes'$style";
     325
     326    if ( 'cb' === $column_name ) {
    324327?>
    325328        <th scope="row" class="check-column">
     
    330333        </th>
    331334<?php
    332         break;
    333 
    334     case 'icon':
    335         list( $mime ) = explode( '/', $post->post_mime_type );
    336         $attributes = 'class="column-icon media-icon ' . $mime . '-icon"' . $style;
    337 ?>
    338         <td <?php echo $attributes ?>><?php
    339             if ( $thumb = wp_get_attachment_image( $post->ID, array( 80, 60 ), true ) ) {
    340                 if ( $this->is_trash || ! $user_can_edit ) {
    341                     echo $thumb;
     335    } else {
     336        echo "<td $attributes>";
     337
     338        switch ( $column_name ) {
     339            case 'icon':
     340                list( $mime ) = explode( '/', $post->post_mime_type );
     341                $attributes = 'class="column-icon media-icon ' . $mime . '-icon"' . $style;
     342
     343                if ( $thumb = wp_get_attachment_image( $post->ID, array( 80, 60 ), true ) ) {
     344                    if ( $this->is_trash || ! $user_can_edit ) {
     345                        echo $thumb;
     346                    } else { ?>
     347                    <a href="<?php echo get_edit_post_link( $post->ID ); ?>" title="<?php echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ); ?>">
     348                        <?php echo $thumb; ?>
     349                    </a><?php
     350                    }
     351                }
     352                break;
     353
     354            case 'title':
     355?>
     356                    <strong>
     357                    <?php if ( $this->is_trash || ! $user_can_edit ) {
     358                        echo $att_title;
     359                    } else { ?>
     360                    <a href="<?php echo get_edit_post_link( $post->ID ); ?>"
     361                        title="<?php echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ); ?>">
     362                        <?php echo $att_title; ?></a>
     363                    <?php }
     364                    _media_states( $post ); ?></strong>
     365                    <p class="filename"><?php echo wp_basename( $post->guid ); ?></p>
     366<?php
     367                break;
     368
     369            case 'author':
     370                printf( '<a href="%s">%s</a>',
     371                    esc_url( add_query_arg( array( 'author' => get_the_author_meta('ID') ), 'upload.php' ) ),
     372                    get_the_author()
     373                );
     374
     375                break;
     376
     377            case 'desc':
     378                echo has_excerpt() ? $post->post_excerpt : '';
     379                break;
     380
     381            case 'date':
     382                if ( '0000-00-00 00:00:00' == $post->post_date ) {
     383                    $h_time = __( 'Unpublished' );
    342384                } else {
    343 ?>
    344                 <a href="<?php echo get_edit_post_link( $post->ID ); ?>" title="<?php echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ); ?>">
    345                     <?php echo $thumb; ?>
    346                 </a>
    347 
    348 <?php           }
    349             }
    350 ?>
    351         </td>
    352 <?php
    353         break;
    354 
    355     case 'title':
    356 ?>
    357         <td <?php echo $attributes ?>><strong>
    358             <?php if ( $this->is_trash || ! $user_can_edit ) {
    359                 echo $att_title;
    360             } else { ?>
    361             <a href="<?php echo get_edit_post_link( $post->ID ); ?>"
    362                 title="<?php echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ); ?>">
    363                 <?php echo $att_title; ?></a>
    364             <?php };
    365             _media_states( $post ); ?></strong>
    366             <p class="filename"><?php echo wp_basename( $post->guid ); ?></p>
    367 <?php
    368         echo $this->row_actions( $this->_get_row_actions( $post, $att_title ) );
    369 ?>
    370         </td>
    371 <?php
    372         break;
    373 
    374     case 'author':
    375 ?>
    376         <td <?php echo $attributes ?>><?php
    377             printf( '<a href="%s">%s</a>',
    378                 esc_url( add_query_arg( array( 'author' => get_the_author_meta('ID') ), 'upload.php' ) ),
    379                 get_the_author()
    380             );
    381         ?></td>
    382 <?php
    383         break;
    384 
    385     case 'desc':
    386 ?>
    387         <td <?php echo $attributes ?>><?php echo has_excerpt() ? $post->post_excerpt : ''; ?></td>
    388 <?php
    389         break;
    390 
    391     case 'date':
    392         if ( '0000-00-00 00:00:00' == $post->post_date ) {
    393             $h_time = __( 'Unpublished' );
    394         } else {
    395             $m_time = $post->post_date;
    396             $time = get_post_time( 'G', true, $post, false );
    397             if ( ( abs( $t_diff = time() - $time ) ) < DAY_IN_SECONDS ) {
    398                 if ( $t_diff < 0 )
    399                     $h_time = sprintf( __( '%s from now' ), human_time_diff( $time ) );
     385                    $m_time = $post->post_date;
     386                    $time = get_post_time( 'G', true, $post, false );
     387                    if ( ( abs( $t_diff = time() - $time ) ) < DAY_IN_SECONDS ) {
     388                        if ( $t_diff < 0 )
     389                            $h_time = sprintf( __( '%s from now' ), human_time_diff( $time ) );
     390                        else
     391                            $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
     392                    } else {
     393                        $h_time = mysql2date( __( 'Y/m/d' ), $m_time );
     394                    }
     395                }
     396
     397                echo $h_time;
     398                break;
     399
     400            case 'parent':
     401                if ( $post->post_parent > 0 )
     402                    $parent = get_post( $post->post_parent );
    400403                else
    401                     $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
    402             } else {
    403                 $h_time = mysql2date( __( 'Y/m/d' ), $m_time );
    404             }
    405         }
    406 ?>
    407         <td <?php echo $attributes ?>><?php echo $h_time ?></td>
    408 <?php
    409         break;
    410 
    411     case 'parent':
    412         if ( $post->post_parent > 0 )
    413             $parent = get_post( $post->post_parent );
    414         else
    415             $parent = false;
    416 
    417         if ( $parent ) {
    418             $title = _draft_or_post_title( $post->post_parent );
    419             $parent_type = get_post_type_object( $parent->post_type );
    420 ?>
    421             <td <?php echo $attributes ?>><strong>
    422                 <?php if ( $parent_type && $parent_type->show_ui && current_user_can( 'edit_post', $post->post_parent ) ) { ?>
    423                     <a href="<?php echo get_edit_post_link( $post->post_parent ); ?>">
    424                         <?php echo $title ?></a><?php
     404                    $parent = false;
     405
     406                if ( $parent ) {
     407                    $title = _draft_or_post_title( $post->post_parent );
     408                    $parent_type = get_post_type_object( $parent->post_type );
     409        ?>
     410                    <strong>
     411                    <?php if ( $parent_type && $parent_type->show_ui && current_user_can( 'edit_post', $post->post_parent ) ) { ?>
     412                        <a href="<?php echo get_edit_post_link( $post->post_parent ); ?>">
     413                            <?php echo $title ?></a><?php
     414                    } else {
     415                        echo $title;
     416                    } ?></strong>,
     417                    <?php echo get_the_time( __( 'Y/m/d' ) ); ?><br />
     418                    <?php
     419                    if ( $user_can_edit ):
     420                        $detach_url = add_query_arg( array(
     421                            'parent_post_id' => $post->post_parent,
     422                            'media[]' => $post->ID,
     423                            '_wpnonce' => wp_create_nonce( 'bulk-' . $this->_args['plural'] )
     424                        ), 'upload.php' ); ?>
     425                    <a class="hide-if-no-js detach-from-parent" href="<?php echo $detach_url ?>"><?php _e( 'Detach' ); ?></a>
     426                    <?php endif;
    425427                } else {
    426                     echo $title;
    427                 } ?></strong>,
    428                 <?php echo get_the_time( __( 'Y/m/d' ) ); ?><br />
    429                 <?php
    430                 if ( $user_can_edit ):
    431                     $detach_url = add_query_arg( array(
    432                         'parent_post_id' => $post->post_parent,
    433                         'media[]' => $post->ID,
    434                         '_wpnonce' => wp_create_nonce( 'bulk-' . $this->_args['plural'] )
    435                     ), 'upload.php' ); ?>
    436                 <a class="hide-if-no-js detach-from-parent" href="<?php echo $detach_url ?>"><?php _e( 'Detach' ); ?></a>
    437                 <?php endif; ?>
    438             </td>
    439 <?php
    440         } else {
    441 ?>
    442             <td <?php echo $attributes ?>><?php _e( '(Unattached)' ); ?><br />
    443             <?php if ( $user_can_edit ) { ?>
    444                 <a class="hide-if-no-js"
    445                     onclick="findPosts.open( 'media[]','<?php echo $post->ID ?>' ); return false;"
    446                     href="#the-list">
    447                     <?php _e( 'Attach' ); ?></a>
    448             <?php } ?></td>
    449 <?php
    450         }
    451         break;
    452 
    453     case 'comments':
    454         $attributes = 'class="comments column-comments num"' . $style;
    455 ?>
    456         <td <?php echo $attributes ?>>
    457             <div class="post-com-count-wrapper">
    458 <?php
    459         $pending_comments = get_pending_comments_num( $post->ID );
    460 
    461         $this->comments_bubble( $post->ID, $pending_comments );
    462 ?>
    463             </div>
    464         </td>
    465 <?php
    466         break;
    467 
    468     default:
    469         if ( 'categories' == $column_name )
    470             $taxonomy = 'category';
    471         elseif ( 'tags' == $column_name )
    472             $taxonomy = 'post_tag';
    473         elseif ( 0 === strpos( $column_name, 'taxonomy-' ) )
    474             $taxonomy = substr( $column_name, 9 );
    475         else
    476             $taxonomy = false;
    477 
    478         if ( $taxonomy ) {
    479             echo '<td ' . $attributes . '>';
    480             if ( $terms = get_the_terms( $post->ID, $taxonomy ) ) {
    481                 $out = array();
    482                 foreach ( $terms as $t ) {
    483                     $posts_in_term_qv = array();
    484                     $posts_in_term_qv['taxonomy'] = $taxonomy;
    485                     $posts_in_term_qv['term'] = $t->slug;
    486 
    487                     $out[] = sprintf( '<a href="%s">%s</a>',
    488                         esc_url( add_query_arg( $posts_in_term_qv, 'upload.php' ) ),
    489                         esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) )
    490                     );
    491                 }
    492                 /* translators: used between list items, there is a space after the comma */
    493                 echo join( __( ', ' ), $out );
    494             } else {
    495                 echo '&#8212;';
    496             }
    497             echo '</td>';
    498             break;
    499         }
    500 ?>
    501         <td <?php echo $attributes ?>><?php
    502             /**
    503              * Fires for each custom column in the Media list table.
    504              *
    505              * Custom columns are registered using the 'manage_media_columns' filter.
    506              *
    507              * @since 2.5.0
    508              *
    509              * @param string $column_name Name of the custom column.
    510              * @param int    $post_id     Attachment ID.
    511              */
    512             do_action( 'manage_media_custom_column', $column_name, $post->ID );
    513         ?></td>
    514 <?php
    515         break;
     428                    _e( '(Unattached)' ); ?><br />
     429                    <?php if ( $user_can_edit ) { ?>
     430                        <a class="hide-if-no-js"
     431                            onclick="findPosts.open( 'media[]','<?php echo $post->ID ?>' ); return false;"
     432                            href="#the-list">
     433                            <?php _e( 'Attach' ); ?></a>
     434                    <?php }
     435                }
     436
     437                break;
     438
     439            case 'comments':
     440                echo '<div class="post-com-count-wrapper">';
     441
     442                $pending_comments = get_pending_comments_num( $post->ID );
     443                $this->comments_bubble( $post->ID, $pending_comments );
     444
     445                echo '</div>';
     446
     447                break;
     448
     449            default:
     450                if ( 'categories' == $column_name )
     451                    $taxonomy = 'category';
     452                elseif ( 'tags' == $column_name )
     453                    $taxonomy = 'post_tag';
     454                elseif ( 0 === strpos( $column_name, 'taxonomy-' ) )
     455                    $taxonomy = substr( $column_name, 9 );
     456                else
     457                    $taxonomy = false;
     458
     459                if ( $taxonomy ) {
     460                    if ( $terms = get_the_terms( $post->ID, $taxonomy ) ) {
     461                        $out = array();
     462                        foreach ( $terms as $t ) {
     463                            $posts_in_term_qv = array();
     464                            $posts_in_term_qv['taxonomy'] = $taxonomy;
     465                            $posts_in_term_qv['term'] = $t->slug;
     466
     467                            $out[] = sprintf( '<a href="%s">%s</a>',
     468                                esc_url( add_query_arg( $posts_in_term_qv, 'upload.php' ) ),
     469                                esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) )
     470                            );
     471                        }
     472                        /* translators: used between list items, there is a space after the comma */
     473                        echo join( __( ', ' ), $out );
     474                    } else {
     475                        echo '&#8212;';
     476                    }
     477
     478                    break;
     479                }
     480
     481                /**
     482                 * Fires for each custom column in the Media list table.
     483                 *
     484                 * Custom columns are registered using the 'manage_media_columns' filter.
     485                 *
     486                 * @since 2.5.0
     487                 *
     488                 * @param string $column_name Name of the custom column.
     489                 * @param int    $post_id     Attachment ID.
     490                 */
     491                do_action( 'manage_media_custom_column', $column_name, $post->ID );
     492                break;
     493        }
     494
     495        if( $primary === $column_name ) {
     496            echo $this->row_actions( $this->_get_row_actions( $post, $att_title ) );
     497        }
     498       
     499        echo '</td>';
    516500    }
    517501}
     
    519503    </tr>
    520504<?php endwhile;
     505    }
     506
     507    /**
     508     * Get name of default primary column
     509     *
     510     * @since 4.3.0
     511     * @access protected
     512     *
     513     * @return string
     514     */
     515    protected function get_default_primary_column_name() {
     516        return 'title';
    521517    }
    522518
Note: See TracChangeset for help on using the changeset viewer.