Introduce `required` argument for `wp_dropdown_categories()`.

This allows the HTML5 `required` attribute to be added to the `select` element.

Props wzislam, pcarvalho.
Fixes #31909.

git-svn-id: https://develop.svn.wordpress.org/trunk@37465 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2016-05-19 02:38:33 +00:00
parent 510bbfcb9c
commit 63755028e8
2 changed files with 74 additions and 1 deletions

View File

@ -314,6 +314,7 @@ function category_description( $category = 0 ) {
*
* @since 2.1.0
* @since 4.2.0 Introduced the `value_field` argument.
* @since 4.6.0 Introduced the `required` argument.
*
* @param string|array $args {
* Optional. Array or string of arguments to generate a categories drop-down element.
@ -351,6 +352,8 @@ function category_description( $category = 0 ) {
* @type string|array $taxonomy Name of the category or categories to retrieve. Default 'category'.
* @type bool $hide_if_empty True to skip generating markup if no categories are found.
* Default false (create select element even if no categories are found).
* @type bool $required Whether the <select> element should have the HTML5 'required' attribute.
* Default false.
* }
* @return string HTML content only if 'echo' argument is 0.
*/
@ -376,6 +379,7 @@ function wp_dropdown_categories( $args = '' ) {
'hide_if_empty' => false,
'option_none_value' => -1,
'value_field' => 'term_id',
'required' => false,
);
$defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0;
@ -414,9 +418,10 @@ function wp_dropdown_categories( $args = '' ) {
$name = esc_attr( $r['name'] );
$class = esc_attr( $r['class'] );
$id = $r['id'] ? esc_attr( $r['id'] ) : $name;
$required = $r['required'] ? 'required' : '';
if ( ! $r['hide_if_empty'] || ! empty( $categories ) ) {
$output = "<select name='$name' id='$id' class='$class' $tab_index_attribute>\n";
$output = "<select $required name='$name' id='$id' class='$class' $tab_index_attribute>\n";
} else {
$output = '';
}

View File

@ -153,4 +153,72 @@ class Tests_Category_WpDropdownCategories extends WP_UnitTestCase {
$this->assertNotContains( 'value="' . $_cat->slug . '" selected="selected"', $found );
}
}
/**
* @ticket 31909
*/
public function test_required_true_should_add_required_attribute() {
// Create a test category.
$cat_id = self::factory()->category->create( array(
'name' => 'Test Category',
'slug' => 'test_category',
) );
$args = array(
'show_option_none' => __( 'Select one', 'text-domain' ),
'option_none_value' => "",
'required' => true,
'hide_empty' => 0,
'echo' => 0,
);
$dropdown_categories = wp_dropdown_categories( $args );
// Test to see if it contains the "required" attribute.
$this->assertRegExp( '/<select[^>]+required/', $dropdown_categories );
}
/**
* @ticket 31909
*/
public function test_required_false_should_omit_required_attribute() {
// Create a test category.
$cat_id = self::factory()->category->create( array(
'name' => 'Test Category',
'slug' => 'test_category',
) );
$args = array(
'show_option_none' => __( 'Select one', 'text-domain' ),
'option_none_value' => "",
'required' => false,
'hide_empty' => 0,
'echo' => 0,
);
$dropdown_categories = wp_dropdown_categories( $args );
// Test to see if it contains the "required" attribute.
$this->assertNotRegExp( '/<select[^>]+required/', $dropdown_categories );
}
/**
* @ticket 31909
*/
public function test_required_should_default_to_false() {
// Create a test category.
$cat_id = self::factory()->category->create( array(
'name' => 'Test Category',
'slug' => 'test_category',
) );
$args = array(
'show_option_none' => __( 'Select one', 'text-domain' ),
'option_none_value' => "",
'hide_empty' => 0,
'echo' => 0,
);
$dropdown_categories = wp_dropdown_categories( $args );
// Test to see if it contains the "required" attribute.
$this->assertNotRegExp( '/<select[^>]+required/', $dropdown_categories );
}
}