Make WordPress Core


Ignore:
Timestamp:
05/29/2015 02:40:52 AM (8 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-links-list-table.php

    r32642 r32644  
    122122
    123123    /**
     124     * Get name of default primary column
     125     *
     126     * @since 4.3.0
     127     * @access protected
     128     *
     129     * @return string
     130     */
     131    protected function get_default_primary_column_name() {
     132        return 'name';
     133    }
     134
     135    /**
    124136     *
    125137     * @global int $cat_id
     
    143155<?php
    144156
    145             list( $columns, $hidden ) = $this->get_column_info();
     157            list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
    146158
    147159            foreach ( $columns as $column_name => $column_display_name ) {
    148                 $class = "class='column-$column_name'";
     160                $classes = "$column_name column-$column_name";
     161                if ( $primary === $column_name ) {
     162                    $classes .= ' has-row-actions column-primary';
     163                }
    149164
    150165                $style = '';
    151                 if ( in_array( $column_name, $hidden ) )
     166                if ( in_array( $column_name, $hidden ) ) {
    152167                    $style = ' style="display:none;"';
    153 
    154                 $attributes = $class . $style;
    155 
    156                 switch ( $column_name ) {
    157                     case 'cb': ?>
    158                         <th scope="row" class="check-column">
    159                             <label class="screen-reader-text" for="cb-select-<?php echo $link->link_id; ?>"><?php echo sprintf( __( 'Select %s' ), $link->link_name ); ?></label>
    160                             <input type="checkbox" name="linkcheck[]" id="cb-select-<?php echo $link->link_id; ?>" value="<?php echo esc_attr( $link->link_id ); ?>" />
    161                         </th>
    162                         <?php
    163                         break;
    164 
    165                     case 'name':
    166                         echo "<td $attributes><strong><a class='row-title' href='$edit_link' title='" . esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $link->link_name ) ) . "'>$link->link_name</a></strong><br />";
    167 
    168                         $actions = array();
    169                         $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
    170                         $actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url( "link.php?action=delete&amp;link_id=$link->link_id", 'delete-bookmark_' . $link->link_id ) . "' onclick=\"if ( confirm( '" . esc_js( sprintf( __( "You are about to delete this link '%s'\n  'Cancel' to stop, 'OK' to delete." ), $link->link_name ) ) . "' ) ) { return true;}return false;\">" . __( 'Delete' ) . "</a>";
    171                         echo $this->row_actions( $actions );
    172 
    173                         echo '</td>';
    174                         break;
    175                     case 'url':
    176                         echo "<td $attributes><a href='$link->link_url' title='". esc_attr( sprintf( __( 'Visit %s' ), $link->link_name ) )."'>$short_url</a></td>";
    177                         break;
    178                     case 'categories':
    179                         ?><td <?php echo $attributes ?>><?php
    180                         $cat_names = array();
    181                         foreach ( $link->link_category as $category ) {
    182                             $cat = get_term( $category, 'link_category', OBJECT, 'display' );
    183                             if ( is_wp_error( $cat ) )
    184                                 echo $cat->get_error_message();
    185                             $cat_name = $cat->name;
    186                             if ( $cat_id != $category )
    187                                 $cat_name = "<a href='link-manager.php?cat_id=$category'>$cat_name</a>";
    188                             $cat_names[] = $cat_name;
    189                         }
    190                         echo implode( ', ', $cat_names );
    191                         ?></td><?php
    192                         break;
    193                     case 'rel':
    194                         ?><td <?php echo $attributes ?>><?php echo empty( $link->link_rel ) ? '<br />' : $link->link_rel; ?></td><?php
    195                         break;
    196                     case 'visible':
    197                         ?><td <?php echo $attributes ?>><?php echo $visible; ?></td><?php
    198                         break;
    199                     case 'rating':
    200                         ?><td <?php echo $attributes ?>><?php echo $rating; ?></td><?php
    201                         break;
    202                     default:
    203                         ?>
    204                         <td <?php echo $attributes ?>><?php
     168                }
     169
     170                $attributes = "class='$classes'$style";
     171
     172                if ( 'cb' === $column_name ) {
     173                    ?>
     174                    <th scope="row" class="check-column">
     175                        <label class="screen-reader-text" for="cb-select-<?php echo $link->link_id; ?>"><?php echo sprintf( __( 'Select %s' ), $link->link_name ); ?></label>
     176                        <input type="checkbox" name="linkcheck[]" id="cb-select-<?php echo $link->link_id; ?>" value="<?php echo esc_attr( $link->link_id ); ?>" />
     177                    </th>
     178                    <?php
     179                } else {
     180                    echo "<td $attributes>";
     181
     182                    switch ( $column_name ) {
     183                        case 'name':
     184                            echo "<strong><a class='row-title' href='$edit_link' title='" . esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $link->link_name ) ) . "'>$link->link_name</a></strong><br />";
     185                            break;
     186                        case 'url':
     187                            echo "<a href='$link->link_url' title='". esc_attr( sprintf( __( 'Visit %s' ), $link->link_name ) )."'>$short_url</a>";
     188                            break;
     189                        case 'categories':
     190                            $cat_names = array();
     191                            foreach ( $link->link_category as $category ) {
     192                                $cat = get_term( $category, 'link_category', OBJECT, 'display' );
     193                                if ( is_wp_error( $cat ) )
     194                                    echo $cat->get_error_message();
     195                                $cat_name = $cat->name;
     196                                if ( $cat_id != $category )
     197                                    $cat_name = "<a href='link-manager.php?cat_id=$category'>$cat_name</a>";
     198                                $cat_names[] = $cat_name;
     199                            }
     200                            echo implode( ', ', $cat_names );
     201                            break;
     202                        case 'rel':
     203                            echo empty( $link->link_rel ) ? '<br />' : $link->link_rel;
     204                            break;
     205                        case 'visible':
     206                            echo $visible;
     207                            break;
     208                        case 'rating':
     209                            echo $rating;
     210                            break;
     211                        default:
    205212                            /**
    206213                             * Fires for each registered custom link column.
     
    212219                             */
    213220                            do_action( 'manage_link_custom_column', $column_name, $link->link_id );
    214                         ?></td>
    215                         <?php
    216                         break;
     221                            break;
     222                    }
     223
     224                    echo $this->handle_row_actions( $link, $column_name, $primary );
     225                    echo '</td>';
    217226                }
    218227            }
     
    222231        }
    223232    }
     233
     234    /**
     235     * Generate and display row actions links
     236     *
     237     * @since 4.3
     238     * @access protected
     239     *
     240     * @param object $link Link being acted upon
     241     * @param string $column_name Current column name
     242     * @param string $primary Primary column name
     243     *
     244     * @return string
     245     */
     246    protected function handle_row_actions( $link, $column_name, $primary ) {
     247        if( $primary === $column_name ) {
     248            $edit_link = get_edit_bookmark_link( $link );
     249
     250            $actions = array();
     251            $actions['edit'] = '<a href="' . $edit_link . '">' . __('Edit') . '</a>';
     252            $actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url("link.php?action=delete&amp;link_id=$link->link_id", 'delete-bookmark_' . $link->link_id) . "' onclick=\"if ( confirm( '" . esc_js(sprintf(__("You are about to delete this link '%s'\n  'Cancel' to stop, 'OK' to delete."), $link->link_name)) . "' ) ) { return true;}return false;\">" . __('Delete') . "</a>";
     253            return $this->row_actions($actions);
     254        }
     255    }
    224256}
Note: See TracChangeset for help on using the changeset viewer.