Make WordPress Core


Ignore:
Timestamp:
10/25/2016 09:25:25 PM (8 years ago)
Author:
ocean90
Message:

General: Introduce a wp_list_sort() helper function, v2.

In addition to wp_list_filter() for filtering a list of objects, and wp_list_pluck() for plucking a certain field out of each object in a list, this new function can be used for sorting a list of objects by specific fields. These functions are now all contained within the new WP_List_Util() class and wp_list_sort() is used in various parts of core for sorting lists.

This was previously committed in [38859] but got reverted in [38862] and [38863]. To fix the previous issues, wp_list_sort() supports now an additional argument to preserve array keys via uasort().

Props flixos90, DrewAPicture, jorbin.
Fixes #37128.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/customize/manager.php

    r38858 r38928  
    18631863        return $this->filtered_device_list();
    18641864    }
     1865
     1866    /**
     1867     * @ticket 37128
     1868     */
     1869    function test_prepare_controls_wp_list_sort_controls() {
     1870        wp_set_current_user( self::$admin_user_id );
     1871
     1872        $controls = array( 'foo' => 2, 'bar' => 4, 'foobar' => 3, 'key' => 1 );
     1873        $controls_sorted = array( 'key', 'foo', 'foobar', 'bar' );
     1874
     1875        $this->manager->add_section( 'foosection', array() );
     1876
     1877        foreach ( $controls as $control_id => $priority ) {
     1878            $this->manager->add_setting( $control_id );
     1879            $this->manager->add_control( $control_id, array(
     1880                'priority' => $priority,
     1881                'section'  => 'foosection',
     1882            ) );
     1883        }
     1884
     1885        $this->manager->prepare_controls();
     1886
     1887        $result = $this->manager->controls();
     1888        $this->assertEquals( $controls_sorted, array_keys( $result ) );
     1889    }
     1890
     1891    /**
     1892     * @ticket 37128
     1893     */
     1894    function test_prepare_controls_wp_list_sort_sections() {
     1895        wp_set_current_user( self::$admin_user_id );
     1896
     1897        $sections = array( 'foo' => 2, 'bar' => 4, 'foobar' => 3, 'key' => 1 );
     1898        $sections_sorted = array( 'key', 'foo', 'foobar', 'bar' );
     1899
     1900        foreach ( $sections as $section_id => $priority ) {
     1901            $this->manager->add_section( $section_id, array(
     1902                'priority' => $priority,
     1903            ) );
     1904        }
     1905
     1906        $this->manager->prepare_controls();
     1907
     1908        $result = $this->manager->sections();
     1909        $this->assertEquals( $sections_sorted, array_keys( $result ) );
     1910    }
     1911
     1912    /**
     1913     * @ticket 37128
     1914     */
     1915    function test_prepare_controls_wp_list_sort_panels() {
     1916        wp_set_current_user( self::$admin_user_id );
     1917
     1918        $panels = array( 'foo' => 2, 'bar' => 4, 'foobar' => 3, 'key' => 1 );
     1919        $panels_sorted = array( 'key', 'foo', 'foobar', 'bar' );
     1920
     1921        foreach ( $panels as $panel_id => $priority ) {
     1922            $this->manager->add_panel( $panel_id, array(
     1923                'priority' => $priority,
     1924            ) );
     1925        }
     1926
     1927        $this->manager->prepare_controls();
     1928
     1929        $result = $this->manager->panels();
     1930        $this->assertEquals( $panels_sorted, array_keys( $result ) );
     1931    }
    18651932}
    18661933
Note: See TracChangeset for help on using the changeset viewer.