diff --git a/src/wp-admin/includes/template.php b/src/wp-admin/includes/template.php index ec0fb52591..0c8c863ec7 100644 --- a/src/wp-admin/includes/template.php +++ b/src/wp-admin/includes/template.php @@ -1457,8 +1457,9 @@ function get_settings_errors( $setting = '', $sanitize = false ) { } // Check global in case errors have been added on this pageload. - if ( ! count( $wp_settings_errors ) ) + if ( empty( $wp_settings_errors ) ) { return array(); + } // Filter the results to those of a specific setting if one was set. if ( $setting ) { diff --git a/tests/phpunit/tests/option/sanitize-option.php b/tests/phpunit/tests/option/sanitize-option.php index bb4a814c54..b551d98481 100644 --- a/tests/phpunit/tests/option/sanitize-option.php +++ b/tests/phpunit/tests/option/sanitize-option.php @@ -157,4 +157,46 @@ class Tests_Sanitize_Option extends WP_UnitTestCase { array( '/%year/%postname%/', '/%year/%postname%/', true ), ); } + + /** + * 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; + } }