Make WordPress Core

Changeset 49190

Timestamp:
10/18/2020 04:20:07 PM (4 years ago)
Author:
johnbillion
Message:

Administration: Allow WP_List_Table::get_bulk_items() to receive a nested array in order to output optgroups.

The allowed format for bulk actions is now an associative array where each element represents either a top level option value and label, or an array representing an optgroup and its options.

For a standard option, the array element key is the field value and the array element value is the field label.

For an optgroup, the array element key is the label and the array element value is an associative array of options as above.

Props goldenapples, mattkeys, valentinbora, davidbaumwald

Fixes #19278

Location:
trunk
Files:
4 edited

Legend:

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

    r49119 r49190  
    420420
    421421    /**
    422      * Gets the list of bulk actions available on this table.
    423      *
    424      * The format is an associative array:
    425      * - `'option_name' => 'option_title'`
    426      *
    427      * @since 3.1.0
     422     * Retrieves the list of bulk actions available for this table.
     423     *
     424     * The format is an associative array where each element represents either a top level option value and label, or
     425     * an array representing an optgroup and its options.
     426     *
     427     * For a standard option, the array element key is the field value and the array element value is the field label.
     428     *
     429     * For an optgroup, the array element key is the label and the array element value is an associative array of
     430     * options as above.
     431     *
     432     * Example:
     433     *
     434     *     [
     435     *         'edit'         => 'Edit',
     436     *         'delete'       => 'Delete',
     437     *         'Change State' => [
     438     *             'feature' => 'Featured',
     439     *             'sale'    => 'On Sale',
     440     *         ]
     441     *     ]
     442     *
     443     * @since 3.1.0
     444     * @since 5.6.0 A bulk action can now contain an array of options in order to create an optgroup.
    428445     *
    429446     * @return array
     
    446463
    447464            /**
    448              * Filters the list table bulk actions drop-down.
     465             * Filters the .
    449466             *
    450467             * The dynamic portion of the hook name, `$this->screen->id`, refers
    451              * to the ID of the current screen, usually a string.
     468             * to the ID of the current screen.
    452469             *
    453470             * @since 3.1.0
     471
    454472             *
    455              * @param string[] $actions An array of the available bulk actions.
     473             * @param $actions An array of the available bulk actions.
    456474             */
    457475            $this->_actions = apply_filters( "bulk_actions-{$this->screen->id}", $this->_actions ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
     
    470488        echo '<option value="-1">' . __( 'Bulk actions' ) . "</option>\n";
    471489
    472         foreach ( $this->_actions as $name => $title ) {
    473             $class = 'edit' === $name ? ' class="hide-if-no-js"' : '';
    474 
    475             echo "\t" . '<option value="' . $name . '"' . $class . '>' . $title . "</option>\n";
     490        foreach ( $this->_actions as $key => $value ) {
     491            if ( is_array( $value ) ) {
     492                echo "\t" . '<optgroup label="' . esc_attr( $key ) . '">' . "\n";
     493
     494                foreach ( $value as $name => $title ) {
     495                    $class = ( 'edit' === $name ) ? ' class="hide-if-no-js"' : '';
     496
     497                    echo "\t\t" . '<option value="' . esc_attr( $name ) . '"' . $class . '>' . $title . "</option>\n";
     498                }
     499                echo "\t" . "</optgroup>\n";
     500            } else {
     501                $class = ( 'edit' === $key ) ? ' class="hide-if-no-js"' : '';
     502
     503                echo "\t" . '<option value="' . esc_attr( $key ) . '"' . $class . '>' . $value . "</option>\n";
     504            }
    476505        }
    477506
  • trunk/src/wp-admin/includes/class-wp-privacy-requests-table.php

    r49035 r49190  
    207207     * @since 4.9.6
    208208     *
    209      * @return string[] Array of bulk action labels keyed by their action.
     209     * @return Array of bulk action labels keyed by their action.
    210210     */
    211211    protected function get_bulk_actions() {
  • trunk/src/wp-admin/includes/class-wp-users-list-table.php

    r49108 r49190  
    260260     * @since 3.1.0
    261261     *
    262      * @return string[] Array of bulk action labels keyed by their action.
     262     * @return Array of bulk action labels keyed by their action.
    263263     */
    264264    protected function get_bulk_actions() {
  • trunk/tests/phpunit/tests/admin/includesListTable.php

    r48943 r49190  
    354354
    355355    /**
     356
     357
     358
     359
     360
     361
     362
     363
     364
     365
     366
     367
     368
     369
     370
     371
     372
     373
     374
     375
     376
     377
     378
     379
     380
     381
     382
     383
     384
     385
     386
     387
     388
     389
     390
     391
    356392     * @ticket 45089
    357393     */
Note: See TracChangeset for help on using the changeset viewer.