[wp-trac] [WordPress Trac] #63483: Compatibility float and whitelist timezone string

WordPress Trac noreply at wordpress.org
Mon May 26 11:35:15 UTC 2025


#63483: Compatibility float and whitelist timezone string
--------------------------------+-----------------------------
 Reporter:  autotutorial        |      Owner:  (none)
     Type:  enhancement         |     Status:  new
 Priority:  normal              |  Milestone:  Awaiting Review
Component:  Options, Meta APIs  |    Version:
 Severity:  trivial             |   Keywords:
  Focuses:                      |
--------------------------------+-----------------------------
 This is a follow-up to #58814.
 This is an "enhancement" of existing code. Compatibility for versions
 before php 8.
 Float point numbers add_filter(
 'pre_option_gmt_offset','wp_timezone_override_offset' ); from get_option(
 'gmt_offset' );
 https://wiki.php.net/rfc/locale_independent_float_to_string
 As of PHP 8.0.0, the decimal point character is always a period (".").
 Prior to PHP 8.0.0, the decimal point character is defined in the script's
 locale (category LC_NUMERIC).
 Example get_option( 'gmt_offset' ):
 {{{#!php
 <?php

 setlocale( LC_ALL, 'de_DE');
 $numeric = 5.5;
 var_dump( $numeric ); // Before PHP 8 is 5,5
 ?>
 }}}

 Before PHP 8 x UTC timezone is valid x timezone.
 Only set if whitelisted and resets $_POST variables instead of saving to
 the already existing timezone database (redundant code).
 https://bugs.php.net/bug.php?id=78139&edit=1

 {{{#!php
 <?php
         unset( $_POST['gmt_offset'] );
                 // Map UTC+- timezones to gmt_offsets and set
 timezone_string to empty.
         if ( isset( $_POST['timezone_string'] ) ) {
                         $first = 'UTC';
                         $two = 'UTC+';
                         $offset_range = array(
                             '',
                                 $first . '-12',
                                 $first . '-11.5',
                                 $first . '-11',
                                 $first . '-10.5',
                                 $first . '-10',
                                 $first . '-9.5',
                                 $first . '-9',
                                 $first . '-8.5',
                                 $first . '-8',
                                 $first . '-7.5',
                                 $first . '-7',
                                 $first . '-6.5',
                                 $first . '-6',
                                 $first . '-5.5',
                                 $first . '-5',
                                 $first . '-4.5',
                                 $first . '-4',
                                 $first . '-3.5',
                                 $first . '-3',
                                 $first . '-2.5',
                                 $first . '-2',
                                 $first . '-1.5',
                                 $first . '-1',
                                 $first . '-0.5',
                                 $two . '0',
                                 $two . '0.5',
                                 $two . '1',
                                 $two . '1.5',
                                 $two . '2',
                                 $two . '2.5',
                                 $two . '3',
                                 $two . '3.5',
                                 $two . '4',
                                 $two . '4.5',
                                 $two . '5',
                                 $two . '5.5',
                                 $two . '5.75',
                                 $two . '6',
                                 $two . '6.5',
                                 $two . '7',
                                 $two . '7.5',
                                 $two . '8',
                                 $two . '8.5',
                                 $two . '8.75',
                                 $two . '9',
                                 $two . '9.5',
                                 $two . '10',
                                 $two . '10.5',
                                 $two . '11',
                                 $two . '11.5',
                                 $two . '12',
                                 $two . '12.75',
                                 $two . '13',
                                 $two . '13.75',
                                 $two . '14',
                 );

             $key_valid = array_search( $_POST['timezone_string'],
 $offset_range );
             if ( $key_valid ) {
                 $_POST['gmt_offset'] = strtr( $offset_range[$key_valid],
 array( 'UTC+' => '', 'UTC' => '' ) );
                 $_POST['timezone_string'] = '';
             } else {
                 $tz_identifiers = timezone_identifiers_list(
 DateTimeZone::ALL_WITH_BC );
                 $key_valid = array_search( $_POST['timezone_string'],
 $tz_identifiers );
                 if( is_int( $key_valid ) ) {
                     $_POST['timezone_string'] =
 $tz_identifiers[$key_valid];
                 } else {
                     unset( $_POST['timezone_string'] );

                     add_settings_error(
                                         'general',
                                         'settings_updated',
                                         __( 'The timezone you have entered
 is not valid. Please select a valid timezone.' ),
                                         'error'
                                 );
                 }
             }
             unset( $offset_range, $tz_identifiers );
         }
 ?>
 }}}

 Good job

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/63483>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list