Administration: Adjust [45814] to address a backward compatibility issue for plugins passing multiple CSS classes to add_settings_error().

Only add the `notice-` prefix for `error`, `success`, `warning`, `info` CSS classes, keep other classes as is.

Add unit tests for `settings_errors()`.

Props afercia, SergeyBiryukov.
Fixes #44941.

git-svn-id: https://develop.svn.wordpress.org/trunk@45873 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2019-08-22 00:21:49 +00:00
parent 6ea6591114
commit 7fbabd505d
3 changed files with 84 additions and 44 deletions

View File

@ -1814,13 +1814,17 @@ function settings_errors( $setting = '', $sanitize = false, $hide_on_update = fa
$details['type'] = 'success'; $details['type'] = 'success';
} }
if ( in_array( $details['type'], array( 'error', 'success', 'warning', 'info' ) ) ) {
$details['type'] = 'notice-' . $details['type'];
}
$css_id = sprintf( $css_id = sprintf(
'setting-error-%s', 'setting-error-%s',
sanitize_html_class( $details['code'] ) esc_attr( $details['code'] )
); );
$css_class = sprintf( $css_class = sprintf(
'notice notice-%s settings-error is-dismissible', 'notice %s settings-error is-dismissible',
sanitize_html_class( $details['type'] ) esc_attr( $details['type'] )
); );
$output .= "<div id='$css_id' class='$css_class'> \n"; $output .= "<div id='$css_id' class='$css_class'> \n";

View File

@ -108,4 +108,81 @@ class Tests_Admin_includesTemplate extends WP_UnitTestCase {
$this->assertFalse( $wp_meta_boxes['attachment']['advanced']['default']['testbox1'] ); $this->assertFalse( $wp_meta_boxes['attachment']['advanced']['default']['testbox1'] );
} }
/**
* Test calling get_settings_errors() with variations on where it gets errors from.
*
* @ticket 42498
* @covers ::get_settings_errors()
* @global array $wp_settings_errors
*/
public function test_get_settings_errors_sources() {
global $wp_settings_errors;
$blogname_error = array(
'setting' => 'blogname',
'code' => 'blogname',
'message' => 'Capital P dangit!',
'type' => 'error',
);
$blogdescription_error = array(
'setting' => 'blogdescription',
'code' => 'blogdescription',
'message' => 'Too short',
'type' => 'error',
);
$wp_settings_errors = null;
$this->assertSame( array(), get_settings_errors( 'blogname' ) );
// Test getting errors from transient.
$_GET['settings-updated'] = '1';
set_transient( 'settings_errors', array( $blogname_error ) );
$wp_settings_errors = null;
$this->assertSame( array( $blogname_error ), get_settings_errors( 'blogname' ) );
// Test getting errors from transient and from global.
$_GET['settings-updated'] = '1';
set_transient( 'settings_errors', array( $blogname_error ) );
$wp_settings_errors = null;
add_settings_error( $blogdescription_error['setting'], $blogdescription_error['code'], $blogdescription_error['message'], $blogdescription_error['type'] );
$this->assertEqualSets( array( $blogname_error, $blogdescription_error ), get_settings_errors() );
$wp_settings_errors = null;
}
/**
* @ticket 44941
* @covers ::settings_errors()
* @global array $wp_settings_errors
* @dataProvider settings_errors_css_classes_provider
*/
public function test_settings_errors_css_classes( $type, $expected ) {
global $wp_settings_errors;
add_settings_error( 'foo', 'bar', 'Capital P dangit!', $type );
ob_start();
settings_errors();
$output = ob_get_clean();
$wp_settings_errors = null;
$expected = sprintf( 'notice %s settings-error is-dismissible', $expected );
$this->assertContains( $expected, $output );
$this->assertNotContains( 'notice-notice-', $output );
}
public function settings_errors_css_classes_provider() {
return array(
array( 'error', 'notice-error' ),
array( 'success', 'notice-success' ),
array( 'warning', 'notice-warning' ),
array( 'info', 'notice-info' ),
array( 'updated', 'notice-success' ),
array( 'notice-error', 'notice-error' ),
array( 'error my-own-css-class hello world', 'error my-own-css-class hello world' ),
);
}
} }

View File

@ -158,45 +158,4 @@ class Tests_Sanitize_Option extends WP_UnitTestCase {
); );
} }
/**
* Test calling get_settings_errors() with variations on where it gets errors from.
*
* @ticket 42498
* @covers ::get_settings_errors()
* @global array $wp_settings_errors
*/
public function test_get_settings_errors_sources() {
global $wp_settings_errors;
$blogname_error = array(
'setting' => 'blogname',
'code' => 'blogname',
'message' => 'Capital P dangit!',
'type' => 'error',
);
$blogdescription_error = array(
'setting' => 'blogdescription',
'code' => 'blogdescription',
'message' => 'Too short',
'type' => 'error',
);
$wp_settings_errors = null;
$this->assertSame( array(), get_settings_errors( 'blogname' ) );
// Test getting errors from transient.
$_GET['settings-updated'] = '1';
set_transient( 'settings_errors', array( $blogname_error ) );
$wp_settings_errors = null;
$this->assertSame( array( $blogname_error ), get_settings_errors( 'blogname' ) );
// Test getting errors from transient and from global.
$_GET['settings-updated'] = '1';
set_transient( 'settings_errors', array( $blogname_error ) );
$wp_settings_errors = null;
add_settings_error( $blogdescription_error['setting'], $blogdescription_error['code'], $blogdescription_error['message'], $blogdescription_error['type'] );
$this->assertEqualSets( array( $blogname_error, $blogdescription_error ), get_settings_errors() );
$wp_settings_errors = null;
}
} }