3
3
* Time Zone plugin admin page
4
4
*/
5
5
6
- // Display time zone configuration page
6
+ /**
7
+ * Display time zone configuration page
8
+ */
7
9
function yourls_tzp_admin_page () {
8
10
9
11
// Check if a form was submitted
@@ -25,17 +27,23 @@ function yourls_tzp_admin_page() {
25
27
// Draw page
26
28
yourls_tzp_js_css ();
27
29
print '<h2>Time Zone Configuration</h2>
28
- <p>This plugin enables the configuration of which time zone to use when displaying dates and time.</p>
29
- <form method="post"> ' ;
30
- print '<input type="hidden" name="nonce" value=" ' . yourls_create_nonce ( 'time_zone_config ' ) . '" /> ' ;
30
+ <p>This plugin allows to specify a time zone and to format time/date display</p> ' ;
31
+
32
+ if ( defined ('YOURLS_HOURS_OFFSET ' ) ) {
33
+ print '<p><strong>Note:</strong> you have <code>YOURLS_HOURS_OFFSET</code> defined in your config.php. This plugin will override this setting.</p> ' ;
34
+ }
31
35
36
+ print '<form method="post">
37
+ <input type="hidden" name="nonce" value=" ' . yourls_create_nonce ( 'time_zone_config ' ) . '" /> ' ;
38
+
39
+ // Time zones drop down
32
40
print '<h3>Time zone: </h3>
33
41
<div class="settings">
34
42
<p>Choose a city near your location, in the same timezone as you, or a UTC time offset.</p> ' ;
35
43
yourls_tzp_tz_dropdown ( $ user_time_zone );
36
- print '<p>Universal time (<code>UTC</code>) time is: <tt> ' . yourls_tzp_timezoned_time ( time (), ' UTC ' , ' Y-m-d H:i:s ' ) . '</tt></p> ' ;
44
+ print '<p>Universal time (<code>UTC</code>) time is: <tt id="utc_time" > ' . date ( ' Y-m-d H:i:s ', yourls_tzp_timezoned_timestamp ( time (), ' UTC ' ) ) . '</tt></p> ' ;
37
45
if ($ user_time_zone ) {
38
- print "<p>Time in $ user_time_zone is: <tt> " . yourls_tzp_timezoned_time ( time (), $ user_time_zone , 'Y-m-d H:i:s ' ) . '</tt></p> ' ;
46
+ print "<p>Time in $ user_time_zone is: <tt id='local_time' > " . date ( 'Y-m-d H:i:s ' , yourls_tzp_timezoned_timestamp ( time (), $ user_time_zone ) ) . '</tt></p> ' ;
39
47
}
40
48
print '</div> ' ;
41
49
@@ -57,16 +65,21 @@ function yourls_tzp_admin_page() {
57
65
);
58
66
yourls_tzp_format_radio ( 'Time Format ' , 'time_format ' , $ choices , $ user_time_zone , $ user_time_format , $ user_time_format_custom );
59
67
60
- print '<p><input type="submit" class="button" value="Update configuration" /></p> ' ;
61
- print '</form> ' ;
68
+ print '<p><input type="submit" class="button primary" value="Update configuration" /></p>
69
+ </form>
70
+ <p><strong>Note:</strong> custom formats need a PHP <code><a href="https://php.net/date">date()</a></code> string. ' ;
62
71
63
72
}
64
73
65
-
74
+ /**
75
+ * Output CSS & JS
76
+ */
66
77
function yourls_tzp_js_css () {
78
+ $ plugin_url = yourls_plugin_url ( __DIR__ );
67
79
print <<<JSCSS
68
- <link href="https://cdn.jsdelivr.net/npm/[email protected] /dist/css/select2.min.css" rel="stylesheet" />
69
- <script src="https://cdn.jsdelivr.net/npm/[email protected] /dist/js/select2.min.js"></script>
80
+ <link href=' $ plugin_url/assets/select2.min.css' rel='stylesheet' />
81
+ <script src=' $ plugin_url/assets/select2.min.js'></script>
82
+ <script src=' $ plugin_url/assets/php-date-formatter.min.js'></script>
70
83
<script>
71
84
jQuery( document ).ready(function() {
72
85
// auto select radio when custom input field is focused
@@ -77,11 +90,41 @@ function yourls_tzp_js_css() {
77
90
// easy selector on timezones
78
91
$('#time_zone').select2({
79
92
templateResult: format_region,
80
- placeholder:'Choose a time zone '
81
- }
93
+ placeholder:'Choose a time zone'
94
+ });
95
+
96
+ // Real time date format preview
97
+ $(".custom_format").on("keyup", function() {
98
+ format_preview($(this).attr('id'), $(this).val());
99
+ });
100
+
101
+ // Click a radio to change custom format accordingly
102
+ $('.radio_format').change(
103
+ function(){
104
+ if (this.checked) {
105
+ name = $(this).attr('name');
106
+ value = $(this).attr('value');
107
+ $('#'+name+'_custom_value').val(value).trigger($.Event("keyup"));
108
+ }
109
+ }
82
110
);
83
111
})
84
112
113
+ // Real time preview of date/time format
114
+ function format_preview(id, value) {
115
+ if($('#local_time')) {
116
+ time = $('#local_time').text();
117
+ } else {
118
+ time = $('#utc_time').text();
119
+ }
120
+ id = id.replace('_custom_value', '');
121
+ fmt = new DateFormatter();
122
+ parse = fmt.parseDate(time, 'Y-m-d H:i:s');
123
+ format = fmt.formatDate(parse, value );
124
+ $('#tz_test_' + id).text(format);
125
+ }
126
+
127
+ // modify dropdown list
85
128
function format_region(item) {
86
129
if (!item.id) {
87
130
return item.text;
@@ -100,7 +143,7 @@ function format_region(item) {
100
143
border-bottom:1px solid #ccc;
101
144
}
102
145
div.settings {
103
- padding-bottom:2em ;
146
+ padding-bottom:1em ;
104
147
}
105
148
.region{
106
149
color:#aaa;
@@ -111,7 +154,11 @@ function format_region(item) {
111
154
112
155
}
113
156
114
-
157
+ /**
158
+ * Draw the time zone drop down
159
+ *
160
+ * @param string $user_time_zone Timezone to be marked as "selected" in the dropdown
161
+ */
115
162
function yourls_tzp_tz_dropdown ( $ user_time_zone ) {
116
163
// Continent list
117
164
$ continent = array (
@@ -130,9 +177,9 @@ function yourls_tzp_tz_dropdown( $user_time_zone ) {
130
177
foreach ($ continent as $ name => $ mask ) {
131
178
$ zones = DateTimeZone::listIdentifiers ($ mask );
132
179
foreach ($ zones as $ timezone ) {
133
- // Remove region name and add a sample time
134
- $ timezones [$ name ][$ timezone ] = substr ($ timezone , strlen ($ name ) + 1 );
135
- }
180
+ // Remove region name
181
+ $ timezones [$ name ][$ timezone ] = substr ($ timezone , strlen ($ name ) +1 );
182
+ }
136
183
}
137
184
138
185
// Manual UTC offset
@@ -190,16 +237,16 @@ function yourls_tzp_format_radio( $title, $input_name, $formats, $tz, $selected,
190
237
191
238
foreach ($ formats as $ format ) {
192
239
$ checked = ( $ format === $ selected ) ? 'checked="checked" ' : '' ;
193
- print "<p><label><input type='radio' name=' $ input_name' value=' $ format' $ checked > " ;
194
- print yourls_date_i18n ( $ format , yourls_tzp_timezoned_time ( time (), $ tz ), true );
195
- print "<br> " ;
196
- print yourls_tzp_timezoned_time ( time (), $ tz , $ format );
240
+ print "<p><label><input type='radio' class='radio_format radio_ $ input_name' name=' $ input_name' value=' $ format' $ checked > " ;
241
+ print yourls_date_i18n ( $ format , yourls_tzp_timezoned_timestamp ( time (), $ tz ), true );
197
242
print "</label></p> \n" ;
198
243
}
199
244
200
245
$ checked = ( 'custom ' === $ selected ) ? 'checked="checked" ' : '' ;
246
+ $ preview = date ( $ custom , yourls_tzp_timezoned_timestamp ( time (), $ tz ) );
201
247
print "<label class='custom'><input type='radio' id=' $ {input_name}_custom' name=' $ input_name' value='custom' $ checked >
202
- Custom: <input type='text' class='text' id=' $ {input_name}_custom_value' name=' $ {input_name}_custom_value' value=' $ custom' />
248
+ Custom: <input type='text' class='text custom_format' id=' $ {input_name}_custom_value' name=' $ {input_name}_custom_value' value=' $ custom' />
249
+ <span class='tz_test' id='tz_test_ $ input_name'> $ preview</span>
203
250
</label> \n" ;
204
251
205
252
print '</div> ' ;
@@ -208,10 +255,6 @@ function yourls_tzp_format_radio( $title, $input_name, $formats, $tz, $selected,
208
255
209
256
/**
210
257
* Update time zone in database
211
- *
212
- * The array isn't sanitized here, it should be done in the caller
213
- *
214
- * @since
215
258
*/
216
259
function yourls_tzp_config_update_settings () {
217
260
0 commit comments