diff --git a/includes/CMB2_Field.php b/includes/CMB2_Field.php index bd524e3fe..5bca14775 100644 --- a/includes/CMB2_Field.php +++ b/includes/CMB2_Field.php @@ -900,12 +900,17 @@ public function _set_field_defaults( $args ) { $args['options']['textarea_name'] = $args['_name']; } - $option_types = apply_filters( 'cmb2_all_or_nothing_types', array( 'taxonomy_select', 'taxonomy_radio', 'taxonomy_radio_inline' ), $this ); + $option_types = apply_filters( 'cmb2_all_or_nothing_types', array( 'select', 'radio', 'radio_inline', 'taxonomy_select', 'taxonomy_radio', 'taxonomy_radio_inline' ), $this ); if ( in_array( $args['type'], $option_types, true ) ) { - $args['show_option_none'] = isset( $args['show_option_none'] ) ? $args['show_option_none'] : __( 'None', 'cmb2' ); - $args['show_option_all'] = isset( $args['show_option_all'] ) ? $args['show_option_all'] : __( 'All', 'cmb2' ); // @todo: implementation + $args['show_option_none'] = isset( $args['show_option_none'] ) ? $args['show_option_none'] : false; + $args['show_option_none'] = true === $args['show_option_none'] ? __( 'None', 'cmb2' ) : $args['show_option_none']; + + if ( ! $args['show_option_none'] ) { + $off_by_default = in_array( $args['type'], array( 'select', 'radio', 'radio_inline' ), true ); + $args['show_option_none'] = $off_by_default ? false : __( 'None', 'cmb2' ); + } } diff --git a/includes/CMB2_Types.php b/includes/CMB2_Types.php index 194679694..270b9db98 100644 --- a/includes/CMB2_Types.php +++ b/includes/CMB2_Types.php @@ -204,7 +204,13 @@ public function concat_items( $args = array() ) { : $this->field->args( 'default' ); $concatenated_items = ''; $i = 1; - foreach ( (array) $this->field->options() as $opt_value => $opt_label ) { + + $options = array(); + if ( $option_none = $this->field->args( 'show_option_none' ) ) { + $options[ '' ] = $option_none; + } + $options = $options + (array) $this->field->options(); + foreach ( $options as $opt_value => $opt_label ) { // Clone args & modify for just this item $a = $args; diff --git a/tests/cmb-tests-base.php b/tests/cmb-tests-base.php index 758caa960..8de3fe057 100644 --- a/tests/cmb-tests-base.php +++ b/tests/cmb-tests-base.php @@ -13,6 +13,43 @@ public function tearDown() { parent::tearDown(); } + public function normalize_string( $string ) { + return trim( preg_replace( array( + '/[\t\n\r]/', // Remove tabs and newlines + '/\s{2,}/', // Replace repeating spaces with one space + '/> <', + ), $string ) ); + } + + public function is_connected() { + $connected = @fsockopen( 'www.youtube.com', 80 ); + if ( $connected ){ + $is_conn = true; + fclose( $connected ); + } else { + $is_conn = false; //action in connection failure + } + + return $is_conn; + } + + protected function capture_render( $cb ) { + ob_start(); + call_user_func( $cb ); + $output = ob_get_contents(); + ob_end_clean(); + + return $output; + } + + protected function render_field( $field ) { + return $this->capture_render( array( $field, 'render_field' ) ); + } + public function assertHTMLstringsAreEqual( $expected_string, $string_to_test ) { $expected_string = $this->normalize_string( $expected_string ); $string_to_test = $this->normalize_string( $string_to_test ); @@ -42,30 +79,6 @@ public function assertIsDefined( $definition ) { return $this->assertTrue( defined( $definition ), "$definition is not defined." ); } - public function normalize_string( $string ) { - return trim( preg_replace( array( - '/[\t\n\r]/', // Remove tabs and newlines - '/\s{2,}/', // Replace repeating spaces with one space - '/> <', - ), $string ) ); - } - - public function is_connected() { - $connected = @fsockopen( 'www.youtube.com', 80 ); - if ( $connected ){ - $is_conn = true; - fclose( $connected ); - } else { - $is_conn = false; //action in connection failure - } - - return $is_conn; - } - /** * Backport assertNotFalse to PHPUnit 3.6.12 which only runs in PHP 5.2 * diff --git a/tests/test-cmb-field.php b/tests/test-cmb-field.php index 23d889fa4..f13f6b6c0 100644 --- a/tests/test-cmb-field.php +++ b/tests/test-cmb-field.php @@ -139,6 +139,56 @@ public function test_empty_field_with_empty_object_id() { $this->assertEquals( array( 'test_test' => $cleaned_val ), cmb2_options( 0 )->get_options() ); } + public function test_show_option_none() { + $args = array( + 'name' => 'Test Radio inline', + 'desc' => 'field description (optional)', + 'id' => 'radio_inline', + 'type' => 'radio_inline', + 'options' => array( + 'standard' => 'Option One', + 'custom' => 'Option Two', + 'none' => 'Option Three', + ), + ); + $field = new CMB2_Field( array( + 'field_args' => $args, + ) ); + + $this->assertFalse( $field->args( 'show_option_none' ) ); + + $this->assertHTMLstringsAreEqual( + '

field description (optional)

', + $this->render_field( $field ) + ); + + $args['show_option_none'] = true; + $field = new CMB2_Field( array( + 'field_args' => $args, + ) ); + + $this->assertEquals( __( 'None', 'cmb2' ), $field->args( 'show_option_none' ) ); + + $this->assertHTMLstringsAreEqual( + '

field description (optional)

', + $this->render_field( $field ) + ); + + $args['show_option_none'] = 'No Value'; + $field = new CMB2_Field( array( + 'field_args' => $args, + ) ); + + $this->assertEquals( 'No Value', $field->args( 'show_option_none' ) ); + + $this->assertHTMLstringsAreEqual( + '

field description (optional)

', + $this->render_field( $field ) + ); + + } + + public function before_field_cb( $args ) { echo 'before_field_cb_' . $args['id']; } diff --git a/tests/test-cmb-types.php b/tests/test-cmb-types.php index 89a61f018..021eaa776 100644 --- a/tests/test-cmb-types.php +++ b/tests/test-cmb-types.php @@ -801,19 +801,6 @@ private function get_field_type_object( $args = '' ) { return new CMB2_Types( $field ); } - private function capture_render( $cb ) { - ob_start(); - call_user_func( $cb ); - $output = ob_get_contents(); - ob_end_clean(); - - return $output; - } - - private function render_field( $field ) { - return $this->capture_render( array( $field, 'render_field' ) ); - } - /** * Test Callbacks */