Make WordPress Core

Changeset 54215

Timestamp:
09/19/2022 09:06:08 PM (23 months ago)
Author:
davidbaumwald
Message:

Administration: Add new get_views_links method to WP_List_Table.

Many WP_List_Table child classes in core use mostly the same code to create their "view" links markup. To DRY-up the code, a new WP_List_Table->get_view_links method is being introduced to consolidate the HTML link generation when provided an array of links.

This change also implements this new method in the relevant WP_List_Table_xxx child classes get_views methods. Finally, unit tests are being added to validate view links markup and test for some "unhappy paths".

Props afercia, costdev, garrett-eclipse, Dharm1025, juhise, peterwilsoncc.
Fixes #42066.

Location:
trunk
Files:
7 added
16 edited

Legend:

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

    r54071 r54215  
    294294
    295295        foreach ( $stati as $status => $label ) {
    296             $current_link_attributes = '';
    297 
    298             if ( $status === $comment_status ) {
    299                 $current_link_attributes = ' class="current" aria-current="page"';
    300             }
    301 
    302296            if ( 'mine' === $status ) {
    303297                $current_user_id    = get_current_user_id();
     
    330324            */
    331325
    332             $status_links[ $status ] = "<a href='$link'$current_link_attributes>" . sprintf(
    333                 translate_nooped_plural( $label, $num_comments->$status ),
    334                 sprintf(
    335                     '<span class="%s-count">%s</span>',
    336                     ( 'moderated' === $status ) ? 'pending' : $status,
    337                     number_format_i18n( $num_comments->$status )
    338                 )
    339             ) . '</a>';
     326            $status_links[ $status ] = array(
     327                'url'     => esc_url( $link ),
     328                'label'   => sprintf(
     329                    translate_nooped_plural( $label, $num_comments->$status ),
     330                    sprintf(
     331                        '<span class="%s-count">%s</span>',
     332                        ( 'moderated' === $status ) ? 'pending' : $status,
     333                        number_format_i18n( $num_comments->$status )
     334                    )
     335                ),
     336                'current' => $status === $comment_status,
     337            );
    340338        }
    341339
     
    349347         *                              'Pending', 'Approved', 'Spam', and 'Trash'.
    350348         */
    351         return apply_filters( 'comment_status_links', $status_links );
     349        return apply_filters( 'comment_status_links', $ );
    352350    }
    353351
  • trunk/src/wp-admin/includes/class-wp-list-table.php

    r54133 r54215  
    15151515
    15161516    /**
     1517
     1518
     1519
     1520
     1521
     1522
     1523
     1524
     1525
     1526
     1527
     1528
     1529
     1530
     1531
     1532
     1533
     1534
     1535
     1536
     1537
     1538
     1539
     1540
     1541
     1542
     1543
     1544
     1545
     1546
     1547
     1548
     1549
     1550
     1551
     1552
     1553
     1554
     1555
     1556
     1557
     1558
     1559
     1560
     1561
     1562
     1563
     1564
     1565
     1566
     1567
     1568
     1569
     1570
     1571
     1572
     1573
     1574
     1575
     1576
     1577
     1578
     1579
     1580
     1581
     1582
     1583
     1584
     1585
     1586
     1587
     1588
    15171589     * Sends required variables to JavaScript land.
    15181590     *
  • trunk/src/wp-admin/includes/class-wp-ms-sites-list-table.php

    r54071 r54215  
    263263
    264264        foreach ( $statuses as $status => $label_count ) {
    265             $current_link_attributes = $requested_status === $status || ( '' === $requested_status && 'all' === $status )
    266                 ? ' class="current" aria-current="page"'
    267                 : '';
    268265            if ( (int) $counts[ $status ] > 0 ) {
    269266                $label    = sprintf( translate_nooped_plural( $label_count, $counts[ $status ] ), number_format_i18n( $counts[ $status ] ) );
    270267                $full_url = 'all' === $status ? $url : add_query_arg( 'status', $status, $url );
    271268
    272                 $view_links[ $status ] = sprintf(
    273                     '<a href="%1$s"%2$s>%3$s</a>',
    274                     esc_url( $full_url ),
    275                     $current_link_attributes,
    276                     $label
     269                $view_links[ $status ] = array(
     270                    'url'     => esc_url( $full_url ),
     271                    'label'   => $label,
     272                    'current' => $requested_status === $status || ( '' === $requested_status && 'all' === $status ),
    277273                );
    278274            }
    279275        }
    280276
    281         return $view_links;
     277        return $;
    282278    }
    283279
  • trunk/src/wp-admin/includes/class-wp-ms-themes-list-table.php

    r51795 r54215  
    445445
    446446            if ( 'search' !== $type ) {
    447                 $status_links[ $type ] = sprintf(
    448                     "<a href='%s'%s>%s</a>",
    449                     esc_url( add_query_arg( 'theme_status', $type, $url ) ),
    450                     ( $type === $status ) ? ' class="current" aria-current="page"' : '',
    451                     sprintf( $text, number_format_i18n( $count ) )
     447                $status_links[ $type ] = array(
     448                    'url'     => esc_url( add_query_arg( 'theme_status', $type, $url ) ),
     449                    'label'   => sprintf( $text, number_format_i18n( $count ) ),
     450                    'current' => $type === $status,
    452451                );
    453452            }
    454453        }
    455454
    456         return $status_links;
     455        return $;
    457456    }
    458457
  • trunk/src/wp-admin/includes/class-wp-ms-users-list-table.php

    r54071 r54215  
    138138        $total_admins = count( $super_admins );
    139139
    140         $current_link_attributes = 'super' !== $role ? ' class="current" aria-current="page"' : '';
    141         $role_links              = array();
    142         $role_links['all']       = sprintf(
    143             '<a href="%s"%s>%s</a>',
    144             network_admin_url( 'users.php' ),
    145             $current_link_attributes,
    146             sprintf(
     140        $role_links        = array();
     141        $role_links['all'] = array(
     142            'url'     => network_admin_url( 'users.php' ),
     143            'label'   => sprintf(
    147144                /* translators: Number of users. */
    148145                _nx(
     
    153150                ),
    154151                number_format_i18n( $total_users )
    155             )
    156         );
    157         $current_link_attributes = 'super' === $role ? ' class="current" aria-current="page"' : '';
    158         $role_links['super']     = sprintf(
    159             '<a href="%s"%s>%s</a>',
    160             network_admin_url( 'users.php?role=super' ),
    161             $current_link_attributes,
    162             sprintf(
     152            ),
     153            'current' => 'super' !== $role,
     154        );
     155
     156        $role_links['super'] = array(
     157            'url'     => network_admin_url( 'users.php?role=super' ),
     158            'label'   => sprintf(
    163159                /* translators: Number of users. */
    164160                _n(
     
    168164                ),
    169165                number_format_i18n( $total_admins )
    170             )
    171         );
    172 
    173         return $role_links;
     166            ),
     167            'current' => 'super' === $role,
     168        );
     169
     170        return $this->get_views_links( $role_links );
    174171    }
    175172
  • trunk/src/wp-admin/includes/class-wp-plugin-install-list-table.php

    r54063 r54215  
    311311        $display_tabs = array();
    312312        foreach ( (array) $tabs as $action => $text ) {
    313             $current_link_attributes                     = ( $action === $tab ) ? ' class="current" aria-current="page"' : '';
    314             $href                                        = self_admin_url( 'plugin-install.php?tab=' . $action );
    315             $display_tabs[ 'plugin-install-' . $action ] = "<a href='$href'$current_link_attributes>$text</a>";
     313            $display_tabs[ 'plugin-install-' . $action ] = array(
     314                'url'     => self_admin_url( 'plugin-install.php?tab=' . $action ),
     315                'label'   => $text,
     316                'current' => $action === $tab,
     317            );
    316318        }
    317319        // No longer a real tab.
    318320        unset( $display_tabs['plugin-install-upload'] );
    319321
    320         return $display_tabs;
     322        return $;
    321323    }
    322324
  • trunk/src/wp-admin/includes/class-wp-plugins-list-table.php

    r54062 r54215  
    577577
    578578            if ( 'search' !== $type ) {
    579                 $status_links[ $type ] = sprintf(
    580                     "<a href='%s'%s>%s</a>",
    581                     add_query_arg( 'plugin_status', $type, 'plugins.php' ),
    582                     ( $type === $status ) ? ' class="current" aria-current="page"' : '',
    583                     sprintf( $text, number_format_i18n( $count ) )
     579                $status_links[ $type ] = array(
     580                    'url'     => add_query_arg( 'plugin_status', $type, 'plugins.php' ),
     581                    'label'   => sprintf( $text, number_format_i18n( $count ) ),
     582                    'current' => $type === $status,
    584583                );
    585584            }
    586585        }
    587586
    588         return $status_links;
     587        return $;
    589588    }
    590589
  • trunk/src/wp-admin/includes/class-wp-posts-list-table.php

    r54099 r54215  
    332332            );
    333333
    334             $mine = $this->get_edit_link( $mine_args, $mine_inner_html, $class );
     334            $mine = array(
     335                'url'     => esc_url( add_query_arg( $mine_args, 'edit.php' ) ),
     336                'label'   => $mine_inner_html,
     337                'current' => isset( $_GET['author'] ) && ( $current_user_id === (int) $_GET['author'] ),
     338            );
    335339
    336340            $all_args['all_posts'] = 1;
    337341            $class                 = '';
    338         }
    339 
    340         if ( empty( $class ) && ( $this->is_base_request() || isset( $_REQUEST['all_posts'] ) ) ) {
    341             $class = 'current';
    342342        }
    343343
     
    353353        );
    354354
    355         $status_links['all'] = $this->get_edit_link( $all_args, $all_inner_html, $class );
     355        $status_links['all'] = array(
     356            'url'     => esc_url( add_query_arg( $all_args, 'edit.php' ) ),
     357            'label'   => $all_inner_html,
     358            'current' => empty( $class ) && ( $this->is_base_request() || isset( $_REQUEST['all_posts'] ) ),
     359        );
    356360
    357361        if ( $mine ) {
     
    382386            );
    383387
    384             $status_links[ $status_name ] = $this->get_edit_link( $status_args, $status_label, $class );
     388            $status_links[ $status_name ] = array(
     389                'url'     => esc_url( add_query_arg( $status_args, 'edit.php' ) ),
     390                'label'   => $status_label,
     391                'current' => isset( $_REQUEST['post_status'] ) && $status_name === $_REQUEST['post_status'],
     392            );
    385393        }
    386394
     
    405413
    406414            $sticky_link = array(
    407                 'sticky' => $this->get_edit_link( $sticky_args, $sticky_inner_html, $class ),
     415                'sticky' => array(
     416                    'url'     => esc_url( add_query_arg( $sticky_args, 'edit.php' ) ),
     417                    'label'   => $sticky_inner_html,
     418                    'current' => ! empty( $_REQUEST['show_sticky'] ),
     419                ),
    408420            );
    409421
     
    413425        }
    414426
    415         return $status_links;
     427        return $;
    416428    }
    417429
  • trunk/src/wp-admin/includes/class-wp-privacy-requests-table.php

    r52239 r54215  
    154154        $admin_url = $this->get_admin_url();
    155155
    156         $current_link_attributes = empty( $current_status ) ? ' class="current" aria-current="page"' : '';
    157         $status_label            = sprintf(
     156        $status_label = sprintf(
    158157            /* translators: %s: Number of requests. */
    159158            _nx(
     
    166165        );
    167166
    168         $views['all'] = sprintf(
    169             '<a href="%s"%s>%s</a>',
    170             esc_url( $admin_url ),
    171             $current_link_attributes,
    172             $status_label
     167        $views['all'] = array(
     168            'url'     => esc_url( $admin_url ),
     169            'label'   => $status_label,
     170            'current' => empty( $current_status ),
    173171        );
    174172
     
    179177            }
    180178
    181             $current_link_attributes = $status === $current_status ? ' class="current" aria-current="page"' : '';
    182             $total_status_requests   = absint( $counts->{$status} );
     179            $total_status_requests = absint( $counts->{$status} );
    183180
    184181            if ( ! $total_status_requests ) {
     
    193190            $status_link = add_query_arg( 'filter-status', $status, $admin_url );
    194191
    195             $views[ $status ] = sprintf(
    196                 '<a href="%s"%s>%s</a>',
    197                 esc_url( $status_link ),
    198                 $current_link_attributes,
    199                 $status_label
     192            $views[ $status ] = array(
     193                'url'     => esc_url( $status_link ),
     194                'label'   => $status_label,
     195                'current' => $status === $current_status,
    200196            );
    201197        }
    202198
    203         return $views;
     199        return $;
    204200    }
    205201
  • trunk/src/wp-admin/includes/class-wp-theme-install-list-table.php

    r52947 r54215  
    187187        $display_tabs = array();
    188188        foreach ( (array) $tabs as $action => $text ) {
    189             $current_link_attributes                    = ( $action === $tab ) ? ' class="current" aria-current="page"' : '';
    190             $href                                       = self_admin_url( 'theme-install.php?tab=' . $action );
    191             $display_tabs[ 'theme-install-' . $action ] = "<a href='$href'$current_link_attributes>$text</a>";
    192         }
    193 
    194         return $display_tabs;
     189            $display_tabs[ 'theme-install-' . $action ] = array(
     190                'url'     => self_admin_url( 'theme-install.php?tab=' . $action ),
     191                'label'   => $text,
     192                'current' => $action === $tab,
     193            );
     194        }
     195
     196        return $this->get_views_links( $display_tabs );
    195197    }
    196198
  • trunk/src/wp-admin/includes/class-wp-users-list-table.php

    r54070 r54215  
    186186        }
    187187
    188         $role_links              = array();
    189         $avail_roles             = array();
    190         $all_text                = __( 'All' );
    191         $current_link_attributes = empty( $role ) ? ' class="current" aria-current="page"' : '';
     188        $role_links  = array();
     189        $avail_roles = array();
     190        $all_text    = __( 'All' );
    192191
    193192        if ( $count_users ) {
     
    216215        }
    217216
    218         $role_links['all'] = sprintf( '<a href="%s"%s>%s</a>', $url, $current_link_attributes, $all_text );
     217        $role_links['all'] = array(
     218            'url'     => $url,
     219            'label'   => $all_text,
     220            'current' => empty( $role ),
     221        );
    219222
    220223        foreach ( $wp_roles->get_names() as $this_role => $name ) {
    221224            if ( $count_users && ! isset( $avail_roles[ $this_role ] ) ) {
    222225                continue;
    223             }
    224 
    225             $current_link_attributes = '';
    226 
    227             if ( $this_role === $role ) {
    228                 $current_link_attributes = ' class="current" aria-current="page"';
    229226            }
    230227
     
    239236            }
    240237
    241             $role_links[ $this_role ] = "<a href='" . esc_url( add_query_arg( 'role', $this_role, $url ) ) . "'$current_link_attributes>$name</a>";
     238            $role_links[ $this_role ] = array(
     239                'url'     => esc_url( add_query_arg( 'role', $this_role, $url ) ),
     240                'label'   => $name,
     241                'current' => $this_role === $role,
     242            );
    242243        }
    243244
    244245        if ( ! empty( $avail_roles['none'] ) ) {
    245 
    246             $current_link_attributes = '';
    247 
    248             if ( 'none' === $role ) {
    249                 $current_link_attributes = ' class="current" aria-current="page"';
    250             }
    251246
    252247            $name = __( 'No role' );
     
    258253            );
    259254
    260             $role_links['none'] = "<a href='" . esc_url( add_query_arg( 'role', 'none', $url ) ) . "'$current_link_attributes>$name</a>";
    261         }
    262 
    263         return $role_links;
     255            $role_links['none'] = array(
     256                'url'     => esc_url( add_query_arg( 'role', 'none', $url ) ),
     257                'label'   => $name,
     258                'current' => 'none' === $role,
     259            );
     260        }
     261
     262        return $this->get_views_links( $role_links );
    264263    }
    265264
  • trunk/tests/phpunit/tests/admin/wpCommentsListTable.php

    r51998 r54215  
    196196    }
    197197
     198
     199
     200
     201
     202
     203
     204
     205
     206
     207
     208
     209
     210
     211
     212
     213
     214
     215
     216
    198217}
  • trunk/tests/phpunit/tests/admin/wpListTable.php

    r53868 r54215  
    77 */
    88class Tests_Admin_WpListTable extends WP_UnitTestCase {
     9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
    927
    1028    /**
     
    105123        return $datasets;
    106124    }
     125
     126
     127
     128
     129
     130
     131
     132
     133
     134
     135
     136
     137
     138
     139
     140
     141
     142
     143
     144
     145
     146
     147
     148
     149
     150
     151
     152
     153
     154
     155
     156
     157
     158
     159
     160
     161
     162
     163
     164
     165
     166
     167
     168
     169
     170
     171
     172
     173
     174
     175
     176
     177
     178
     179
     180
     181
     182
     183
     184
     185
     186
     187
     188
     189
     190
     191
     192
     193
     194
     195
     196
     197
     198
     199
     200
     201
     202
     203
     204
     205
     206
     207
     208
     209
     210
     211
     212
     213
     214
     215
     216
     217
     218
     219
     220
     221
     222
     223
     224
     225
     226
     227
     228
     229
     230
     231
     232
     233
     234
     235
     236
     237
     238
     239
     240
     241
     242
     243
     244
     245
     246
     247
     248
     249
     250
     251
     252
     253
     254
     255
     256
     257
     258
     259
     260
     261
     262
     263
     264
     265
     266
     267
     268
     269
     270
     271
     272
     273
     274
     275
     276
     277
     278
     279
     280
     281
     282
     283
     284
     285
     286
     287
     288
     289
     290
     291
     292
     293
     294
     295
     296
     297
     298
     299
     300
     301
     302
     303
     304
     305
     306
     307
     308
     309
     310
     311
     312
     313
     314
     315
     316
     317
     318
     319
     320
     321
     322
     323
     324
     325
     326
     327
     328
     329
     330
     331
     332
     333
     334
     335
     336
     337
     338
     339
     340
     341
     342
     343
     344
     345
     346
    107347}
  • trunk/tests/phpunit/tests/admin/wpPostsListTable.php

    r51997 r54215  
    320320    }
    321321
     322
     323
     324
     325
     326
     327
     328
     329
     330
     331
     332
     333
     334
     335
     336
     337
     338
     339
     340
     341
     342
     343
    322344}
  • trunk/tests/phpunit/tests/admin/wpPrivacyRequestsTable.php

    r53942 r54215  
    199199        );
    200200    }
     201
     202
     203
     204
     205
     206
     207
     208
     209
     210
     211
     212
     213
    201214}
  • trunk/tests/phpunit/tests/multisite/wpMsSitesListTable.php

    r52010 r54215  
    231231            $this->assertSameSets( $expected, $items );
    232232        }
     233
     234
     235
     236
     237
     238
     239
     240
     241
     242
     243
     244
    233245    }
    234246endif;
Note: See TracChangeset for help on using the changeset viewer.