WordPress.org

Make WordPress Core

Changeset 31338


Ignore:
Timestamp:
02/05/2015 07:03:52 PM (6 years ago)
Author:
boonebgorges
Message:

Introduce 'value_field' parameter to wp_dropdown_pages().

This parameter allows developers to choose the post field that will be used to
fill in the 'option' attribute of the generated dropdown markup.

See [31006] #30306 for a parallel enhancement in wp_dropdown_categories().

Props jfarthing84.
Fixes #12494.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post-template.php

    r31337 r31338  
    972972        'name' => 'page_id', 'id' => '',
    973973        'show_option_none' => '', 'show_option_no_change' => '',
    974         'option_none_value' => ''
     974        'option_none_value' => '',
     975        'value_field' => 'ID',
    975976    );
    976977
     
    14261427     *
    14271428     * @param string $output Passed by reference. Used to append additional content.
    1428      * @param object $page Page data object.
    1429      * @param int $depth Depth of page in reference to parent pages. Used for padding.
    1430      * @param array $args Uses 'selected' argument for selected page to set selected HTML attribute for option element.
     1429     * @param object $page   Page data object.
     1430     * @param int    $depth  Depth of page in reference to parent pages. Used for padding.
     1431     * @param array  $args   Uses 'selected' argument for selected page to set selected HTML attribute for option
     1432     *              element. Uses 'value_field' argument to fill "value" attribute. See {@see wp_dropdown_pages()}.
    14311433     * @param int $id
    14321434     */
     
    14341436        $pad = str_repeat(' ', $depth * 3);
    14351437
    1436         $output .= "\t<option class=\"level-$depth\" value=\"$page->ID\"";
     1438        if ( ! isset( $args['value_field'] ) || ! isset( $page->{$args['value_field']} ) ) {
     1439            $args['value_field'] = 'ID';
     1440        }
     1441
     1442        $output .= "\t<option class=\"level-$depth\" value=\"" . esc_attr( $page->{$args['value_field']} ) . "\"";
    14371443        if ( $page->ID == $args['selected'] )
    14381444            $output .= ' selected="selected"';
  • trunk/tests/phpunit/tests/post/template.php

    r30035 r31338  
    132132        $this->assertEquals( $option_no_change, $output );
    133133    }
     134
     135    /**
     136     * @ticket 12494
     137     */
     138    public function test_wp_dropdown_pages_value_field_should_default_to_ID() {
     139        $p = $this->factory->post->create( array(
     140            'post_type' => 'page',
     141        ) );
     142
     143        $found = wp_dropdown_pages( array(
     144            'echo' => 0,
     145        ) );
     146
     147        // Should contain page ID by default.
     148        $this->assertContains( 'value="' . $p . '"', $found );
     149    }
     150
     151    /**
     152     * @ticket 12494
     153     */
     154    public function test_wp_dropdown_pages_value_field_ID() {
     155        $p = $this->factory->post->create( array(
     156            'post_type' => 'page',
     157        ) );
     158
     159        $found = wp_dropdown_pages( array(
     160            'echo' => 0,
     161            'value_field' => 'ID',
     162        ) );
     163
     164        $this->assertContains( 'value="' . $p . '"', $found );
     165    }
     166
     167    /**
     168     * @ticket 12494
     169     */
     170    public function test_wp_dropdown_pages_value_field_post_name() {
     171        $p = $this->factory->post->create( array(
     172            'post_type' => 'page',
     173            'post_name' => 'foo',
     174        ) );
     175
     176        $found = wp_dropdown_pages( array(
     177            'echo' => 0,
     178            'value_field' => 'post_name',
     179        ) );
     180
     181        $this->assertContains( 'value="foo"', $found );
     182    }
     183
     184    /**
     185     * @ticket 12494
     186     */
     187    public function test_wp_dropdown_pages_value_field_should_fall_back_on_ID_when_an_invalid_value_is_provided() {
     188        $p = $this->factory->post->create( array(
     189            'post_type' => 'page',
     190            'post_name' => 'foo',
     191        ) );
     192
     193        $found = wp_dropdown_pages( array(
     194            'echo' => 0,
     195            'value_field' => 'foo',
     196        ) );
     197
     198        $this->assertContains( 'value="' . $p . '"', $found );
     199    }
    134200}
Note: See TracChangeset for help on using the changeset viewer.