-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathplugin.php
124 lines (110 loc) · 3.79 KB
/
plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/*
Plugin Name: Time Zones ⏰
Plugin URI: https://github.com/YOURLS/timezones
Description: Tell YOURLS what timezone you are in
Version: 1.3
Author: YOURLS contributors
Author URI: https://yourls.org/
*/
// No direct call
if( !defined( 'YOURLS_ABSPATH' ) ) die();
/**
* Register plugin admin page
*/
yourls_add_action( 'plugins_loaded', 'yourls_tzp_config' );
function yourls_tzp_config() {
if( yourls_is_admin() ) {
require_once __DIR__ . '/admin.php';
}
yourls_register_plugin_page( 'time_zone_config', 'Time Zone Configuration', 'yourls_tzp_admin_page' );
}
/**
* Filter for time offset
*/
yourls_add_filter( 'get_time_offset', 'yourls_tzp_get_time_offset' );
function yourls_tzp_get_time_offset() {
return yourls_tzp_timezoned_offset( yourls_tzp_read_options( 'time_zone', 'UTC' ) );
}
/**
* Filter for timestamps
*/
yourls_add_filter( 'get_timestamp', 'yourls_tzp_get_timestamp' );
function yourls_tzp_get_timestamp($timestamp_offset, $timestamp, $offset) {
return yourls_tzp_timezoned_timestamp( $timestamp, yourls_tzp_read_options( 'time_zone', 'UTC' ) );
}
/**
* Filter for date + time format
*/
yourls_add_filter( 'get_datetime_format', 'yourls_tzp_get_datetime_format' );
function yourls_tzp_get_datetime_format() {
return yourls_tzp_get_date_format() . ' ' . yourls_tzp_get_time_format();
}
/**
* Filter for date (no time) format
*/
yourls_add_filter( 'get_date_format', 'yourls_tzp_get_date_format' );
function yourls_tzp_get_date_format() {
$date_format = yourls_tzp_read_options('date_format', 'Y/m/d');
if( $date_format == 'custom' ) {
$date_format = yourls_tzp_read_options('date_format_custom', 'Y/m/d');
}
return $date_format;
}
/**
* Filter for time (no date) format
*/
yourls_add_filter( 'get_time_format', 'yourls_tzp_get_time_format' );
function yourls_tzp_get_time_format() {
$time_format = yourls_tzp_read_options('time_format', 'H:i');
if( $time_format == 'custom' ) {
$time_format = yourls_tzp_read_options('time_format_custom', 'H:i');
}
return $time_format;
}
/**
* Return time offset of a timezone from UTC
*
* @param string $timezone Optional timezone (eg "Europe/Paris"). Default is UTC
* @return int Timezoned time offset
*/
function yourls_tzp_timezoned_offset($timezone = 'UTC') {
$tz = new DateTime('now', new DateTimeZone($timezone));
return $tz->getOffset()/3600;
}
/**
* Return timezoned and formatted time
*
* @param int $timestamp Optional timestamp. If omitted, function will use time()
* @param string $timezone Optional timezone (eg "Europe/Paris"). Default is UTC
* @param string $format Optional format as what PHP's date() needs. Default it 'U' (epoch)
* @return string Timezoned and formatted time
*/
function yourls_tzp_timezoned_timestamp($timestamp = false, $timezone = 'UTC') {
$timestamp = $timestamp ? $timestamp : time();
$offset = yourls_tzp_timezoned_offset($timezone);
return $timestamp + $offset * 3600;
}
/**
* Get (string)key from array, or return false if not defined
*
* @param array $array Array
* @param string $key Key
* @return string Value of (string)$array[$key], or false
*/
function yourls_tzp_get_value( $array, $key, $default ) {
return isset ( $array[$key] ) ? (string)($array[$key]) : $default ;
}
/**
* Read timezone options from the DB, and return all keys or specified key
*
* @param string $key Key of timezone option array
* @return array|mixed Array of options, or value for specified key if exists (false otherwise)
*/
function yourls_tzp_read_options( $key = false, $default = false ) {
$options = (array)yourls_get_option( 'timezone' );
if( $key !== false ) {
$options = array_key_exists($key, $options) ? $options[$key] : $default ;
}
return $options;
}