Skip to content
Permalink
Browse files

Administration: Adjust [45814] to address a backward compatibility is…

…sue 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
  • Loading branch information...
SergeyBiryukov committed Aug 22, 2019
1 parent 6ea6591 commit 7fbabd505d47b90d5343e0b0a5dbec72a6f0b49b
@@ -1814,13 +1814,17 @@ function settings_errors( $setting = '', $sanitize = false, $hide_on_update = fa
$details['type'] = 'success';
}
if ( in_array( $details['type'], array( 'error', 'success', 'warning', 'info' ) ) ) {
$details['type'] = 'notice-' . $details['type'];
}
$css_id = sprintf(
'setting-error-%s',
sanitize_html_class( $details['code'] )
esc_attr( $details['code'] )
);
$css_class = sprintf(
'notice notice-%s settings-error is-dismissible',
sanitize_html_class( $details['type'] )
'notice %s settings-error is-dismissible',
esc_attr( $details['type'] )
);
$output .= "<div id='$css_id' class='$css_class'> \n";
@@ -108,4 +108,81 @@ public function test_remove_meta_box_from_multiple_screens() {
$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' ),
);
}
}
@@ -158,45 +158,4 @@ public function permalink_structure_provider() {
);
}
/**
* 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;
}
}

0 comments on commit 7fbabd5

Please sign in to comment.
You can’t perform that action at this time.