From 08a369177581d3088293d4710b6debc0b3523eff Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 10 Nov 2017 22:29:45 +0000 Subject: [PATCH] Settings: Replace `count()` call with `empty()` in `get_settings_errors()` to prevent PHP 7.2 warnings when `$wp_settings_errors` is `null`. Props pross, dd32, westonruter. See #40109. Fixes #42498 for trunk. git-svn-id: https://develop.svn.wordpress.org/trunk@42146 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/template.php | 3 +- .../phpunit/tests/option/sanitize-option.php | 42 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) 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; + } }