Make WordPress Core

Ticket #36259: 36259.2.patch

File 36259.2.patch, 3.6 KB (added by sachinrajcp123, 7 months ago)
Line 
1diff --git a/src/wp-admin/options.php b/src/wp-admin/options.php
2index 8348b24c8e941..3a8ef92ae8850 100644
3--- a/src/wp-admin/options.php
4+++ b/src/wp-admin/options.php
5@@ -264,18 +264,24 @@
6 $options = $allowed_options[ $option_page ];
7 }
8
9+ $using_locales_default_date_format = false;
10+ $using_locales_default_time_format = false;
11+
12 if ( 'general' === $option_page ) {
13 // Handle custom date/time formats.
14 if ( ! empty( $_POST['date_format'] ) && isset( $_POST['date_format_custom'] )
15 && '\c\u\s\t\o\m' === wp_unslash( $_POST['date_format'] )
16 ) {
17 $_POST['date_format'] = $_POST['date_format_custom'];
18+ } elseif ( __( 'F j, Y' ) === $_POST['date_format'] ) {
19+ $using_locales_default_date_format = true;
20 }
21
22 if ( ! empty( $_POST['time_format'] ) && isset( $_POST['time_format_custom'] )
23 && '\c\u\s\t\o\m' === wp_unslash( $_POST['time_format'] )
24 ) {
25 $_POST['time_format'] = $_POST['time_format_custom'];
26+ } elseif ( __( 'g:i a' ) === $_POST['time_format'] ) {
27+ $using_locales_default_time_format = true;
28 }
29
30 // Map UTC+- timezones to gmt_offsets and set timezone_string to empty.
31@@ -353,6 +359,16 @@
32 $user_language_new = get_user_locale();
33 if ( $user_language_old !== $user_language_new ) {
34 load_default_textdomain( $user_language_new );
35+
36+ // Update formats only if locale defaults were previously used.
37+ if ( $using_locales_default_date_format ) {
38+ update_option( 'date_format', __( 'F j, Y' ) );
39+ }
40+
41+ if ( $using_locales_default_time_format ) {
42+ update_option( 'time_format', __( 'g:i a' ) );
43+ }
44 }
45 } else {
46 add_settings_error( 'general', 'settings_updated', __( 'Settings save failed.' ), 'error' );
47diff --git a/tests/phpunit/tests/i18n/test-switching-locale-updates-date-time-formats.php b/tests/phpunit/tests/i18n/test-switching-locale-updates-date-time-formats.php
48new file mode 100644
49index 0000000000000..92f54494f9451
50--- /dev/null
51+++ b/tests/phpunit/tests/i18n/test-switching-locale-updates-date-time-formats.php
52@@ -0,0 +1,120 @@
53+<?php
54+/**
55+ * @ticket 36259
56+ * @group i18n
57+ */
58+
59+class Tests_I18n_Switching_Locale_Updates_Date_Time_Formats extends WP_UnitTestCase {
60+
61+ private $orig_locale;
62+ private $orig_date_format;
63+ private $orig_time_format;
64+
65+ public function set_up() {
66+ parent::set_up();
67+
68+ $this->orig_locale = get_locale();
69+ $this->orig_date_format = get_option( 'date_format' );
70+ $this->orig_time_format = get_option( 'time_format' );
71+
72+ switch_to_locale( 'en_US' );
73+ update_option( 'date_format', 'F j, Y' );
74+ update_option( 'time_format', 'g:i a' );
75+ }
76+
77+ public function tear_down() {
78+ switch_to_locale( $this->orig_locale );
79+ update_option( 'date_format', $this->orig_date_format );
80+ update_option( 'time_format', $this->orig_time_format );
81+
82+ parent::tear_down();
83+ }
84+
85+ public function test_switching_locale_updates_formats_when_defaults_used() {
86+ global $l10n;
87+
88+ $translations = new MO();
89+ $translations->add_entry(
90+ new Translation_Entry(
91+ array(
92+ 'singular' => 'F j, Y',
93+ 'translations' => array( 'j F Y' ),
94+ )
95+ )
96+ );
97+ $translations->add_entry(
98+ new Translation_Entry(
99+ array(
100+ 'singular' => 'g:i a',
101+ 'translations' => array( 'H:i' ),
102+ )
103+ )
104+ );
105+
106+ $l10n['default'] = &$translations;
107+
108+ switch_to_locale( 'en_GB' );
109+
110+ update_option( 'date_format', __( 'F j, Y' ) );
111+ update_option( 'time_format', __( 'g:i a' ) );
112+
113+ $this->assertSame( 'j F Y', get_option( 'date_format' ) );
114+ $this->assertSame( 'H:i', get_option( 'time_format' ) );
115+ }
116+}