Skip to content

Commit e834fed

Browse files
Options, Meta APIs: Check if the gmt_offset value is numeric in sanitize_option().
When saving the settings via the admin UI, the default value for any options not passed in the current `$_POST` request is set to `null` in `wp-admin/options.php`. Some options, e.g. `blog_public`, then rely on `null` being passed to `update_option()` to determine whether the value was changed or not. This commit resolves a PHP 8.1 deprecation notice when saving the `gmt_offset` option without any changes: {{{ Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated }}} Includes a similar fix for the `blog_charset` option. Follow-up to [4112], [4329], [5541], [21849]. Props adi3890, dhrupo, hrdelwar, hasanmisbah, oglekler, mukesh27, SergeyBiryukov. Fixes #57728. git-svn-id: https://develop.svn.wordpress.org/trunk@56132 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 6ba1ca6 commit e834fed

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/wp-includes/formatting.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4883,7 +4883,11 @@ function sanitize_option( $option, $value ) {
48834883
break;
48844884

48854885
case 'blog_charset':
4886-
$value = preg_replace( '/[^a-zA-Z0-9_-]/', '', $value ); // Strips slashes.
4886+
if ( is_string( $value ) ) {
4887+
$value = preg_replace( '/[^a-zA-Z0-9_-]/', '', $value ); // Strips slashes.
4888+
} else {
4889+
$value = '';
4890+
}
48874891
break;
48884892

48894893
case 'blog_public':
@@ -4918,7 +4922,11 @@ function sanitize_option( $option, $value ) {
49184922
break;
49194923

49204924
case 'gmt_offset':
4921-
$value = preg_replace( '/[^0-9:.-]/', '', $value ); // Strips slashes.
4925+
if ( is_numeric( $value ) ) {
4926+
$value = preg_replace( '/[^0-9:.-]/', '', $value ); // Strips slashes.
4927+
} else {
4928+
$value = '';
4929+
}
49224930
break;
49234931

49244932
case 'siteurl':

tests/phpunit/tests/option/sanitizeOption.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function data_sanitize_option() {
3636
array( 'blogname', '&lt;i&gt;My Site&lt;/i&gt;', '<i>My Site</i>' ),
3737
array( 'blog_charset', 'UTF-8', 'UTF-8' ),
3838
array( 'blog_charset', 'charset', '">charset<"' ),
39+
array( 'blog_charset', '', null ),
3940
array( 'blog_public', 1, null ),
4041
array( 'blog_public', 1, '1' ),
4142
array( 'blog_public', -2, '-2' ),
@@ -45,6 +46,7 @@ public function data_sanitize_option() {
4546
array( 'ping_sites', "http://www.example.com\nhttp://example.org", "www.example.com \n\texample.org\n\n" ),
4647
array( 'gmt_offset', '0', 0 ),
4748
array( 'gmt_offset', '1.5', '1.5' ),
49+
array( 'gmt_offset', '', null ),
4850
array( 'siteurl', 'http://example.org', 'http://example.org' ),
4951
array( 'siteurl', 'http://example.org/subdir', 'http://example.org/subdir' ),
5052
array( 'siteurl', get_option( 'siteurl' ), '' ),

0 commit comments

Comments
 (0)