| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Traits; |
| 4 |
|
| 5 |
trait Formats { |
| 6 |
/** |
| 7 |
* Convert a unixtimestamp into the blog datetime |
| 8 |
* |
| 9 |
* @param $timestamp |
| 10 |
* @param bool $i18n |
| 11 |
* |
| 12 |
* @return false|string |
| 13 |
*/ |
| 14 |
public function format_date_time( $timestamp, $i18n = true ) { |
| 15 |
if ( ! filter_var( $timestamp, FILTER_VALIDATE_INT ) ) { |
| 16 |
$timestamp = strtotime( $timestamp ); |
| 17 |
} |
| 18 |
if ( false === $timestamp ) { |
| 19 |
return 'n/a'; |
| 20 |
} |
| 21 |
$format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); |
| 22 |
if ( false === $i18n ) { |
| 23 |
return gmdate( $format, $timestamp ); |
| 24 |
} |
| 25 |
$time = get_date_from_gmt( gmdate( 'Y-m-d H:i:s', $timestamp ), 'Y-m-d H:i:s' ); |
| 26 |
|
| 27 |
return date_i18n( $format, strtotime( $time ) ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Persistent date & time formats for Hub data |
| 32 |
* |
| 33 |
* @param $timestamp |
| 34 |
* |
| 35 |
* @return string |
| 36 |
*/ |
| 37 |
public function persistent_hub_datetime_format( $timestamp ) { |
| 38 |
return gmdate( 'Y-m-d g:i a', $timestamp ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* @param $bytes |
| 43 |
* |
| 44 |
* @return string |
| 45 |
*/ |
| 46 |
public function format_bytes_into_readable( $bytes ) { |
| 47 |
if ( $bytes >= 1073741824 ) { |
| 48 |
$bytes = number_format( $bytes / 1073741824, 2 ) . ' GB'; |
| 49 |
} elseif ( $bytes >= 1048576 ) { |
| 50 |
$bytes = number_format( $bytes / 1048576, 2 ) . ' MB'; |
| 51 |
} elseif ( $bytes >= 1024 ) { |
| 52 |
$bytes = number_format( $bytes / 1024, 2 ) . ' KB'; |
| 53 |
} elseif ( $bytes > 1 ) { |
| 54 |
$bytes = $bytes . ' bytes'; |
| 55 |
} elseif ( 1 === $bytes ) { |
| 56 |
$bytes = $bytes . ' byte'; |
| 57 |
} else { |
| 58 |
$bytes = '0 bytes'; |
| 59 |
} |
| 60 |
|
| 61 |
return $bytes; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* @param $since |
| 66 |
* |
| 67 |
* @return string |
| 68 |
*/ |
| 69 |
public function time_since( $since ) { |
| 70 |
$since = time() - $since; |
| 71 |
if ( $since < 0 ) { |
| 72 |
$since = 0; |
| 73 |
} |
| 74 |
$chunks = array( |
| 75 |
array( 60 * 60 * 24 * 365, esc_html__( 'year', 'wpdef' ) ), |
| 76 |
array( 60 * 60 * 24 * 30, esc_html__( 'month', 'wpdef' ) ), |
| 77 |
array( 60 * 60 * 24 * 7, esc_html__( 'week', 'wpdef' ) ), |
| 78 |
array( 60 * 60 * 24, esc_html__( 'day' ) ), |
| 79 |
array( 60 * 60, esc_html__( 'hour', 'wpdef' ) ), |
| 80 |
array( 60, esc_html__( 'minute', 'wpdef' ) ), |
| 81 |
array( 1, esc_html__( 'second', 'wpdef' ) ), |
| 82 |
); |
| 83 |
|
| 84 |
for ( $i = 0, $j = count( $chunks ); $i < $j; $i ++ ) { |
| 85 |
$seconds = $chunks[ $i ][0]; |
| 86 |
$name = $chunks[ $i ][1]; |
| 87 |
$count = floor( $since / $seconds ); |
| 88 |
if ( 0 !== $count ) { |
| 89 |
break; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
$print = ( 1 === $count ) ? '1 ' . $name : "$count {$name}s"; |
| 94 |
|
| 95 |
return $print; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Get formatted date |
| 100 |
* |
| 101 |
* @param $date |
| 102 |
* |
| 103 |
* @return string |
| 104 |
*/ |
| 105 |
public function get_date( $date ) { |
| 106 |
if ( strtotime( '-24 hours' ) > $date ) { |
| 107 |
return $this->format_date_time( gmdate( 'Y-m-d H:i:s', $date ) ); |
| 108 |
} else { |
| 109 |
return human_time_diff( $date, time() ) . ' ' . __( 'ago', 'wpdef' ); // phpcs:ignore |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Return times frame for selectbox |
| 115 |
* |
| 116 |
* @return array |
| 117 |
*/ |
| 118 |
public function get_times() { |
| 119 |
$times_interval = apply_filters( 'defender_get_times_interval', array( '00', '30' ) ); |
| 120 |
$data = array(); |
| 121 |
for ( $i = 0; $i < 24; $i ++ ) { |
| 122 |
foreach ( $times_interval as $min ) { |
| 123 |
$time = $i . ':' . $min; |
| 124 |
$data[ $time ] = date_i18n( 'h:i A', strtotime( $time ) ); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
return $data; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* @param string $timestring |
| 133 |
* |
| 134 |
* @return false|int |
| 135 |
* @throws \Exception |
| 136 |
*/ |
| 137 |
public function local_to_utc( $timestring ) { |
| 138 |
$tz = get_option( 'timezone_string' ); |
| 139 |
if ( ! $tz ) { |
| 140 |
$gmt_offset = get_option( 'gmt_offset' ); |
| 141 |
if ( 0 === $gmt_offset ) { |
| 142 |
return strtotime( $timestring ); |
| 143 |
} |
| 144 |
$tz = $this->get_timezone_string( $gmt_offset ); |
| 145 |
} |
| 146 |
if ( ! $tz ) { |
| 147 |
$tz = 'UTC'; |
| 148 |
} |
| 149 |
$timezone = new \DateTimeZone( $tz ); |
| 150 |
$time = new \DateTime( $timestring, $timezone ); |
| 151 |
|
| 152 |
return $time->getTimestamp(); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* @param string $timezone |
| 157 |
* |
| 158 |
* @return false|string |
| 159 |
*/ |
| 160 |
public function get_timezone_string( $timezone ) { |
| 161 |
$timezone = explode( '.', $timezone ); |
| 162 |
if ( isset( $timezone[1] ) ) { |
| 163 |
$timezone[1] = 30; |
| 164 |
} else { |
| 165 |
$timezone[1] = '00'; |
| 166 |
} |
| 167 |
$offset = implode( ':', $timezone ); |
| 168 |
|
| 169 |
[$hours, $minutes] = explode( ':', $offset ); |
| 170 |
$seconds = $hours * 60 * 60 + $minutes * 60; |
| 171 |
$lc = localtime( time(), true ); |
| 172 |
if ( isset( $lc['tm_isdst'] ) ) { |
| 173 |
$isdst = $lc['tm_isdst']; |
| 174 |
} else { |
| 175 |
$isdst = 0; |
| 176 |
} |
| 177 |
$tz = timezone_name_from_abbr( '', $seconds, $isdst ); |
| 178 |
if ( false === $tz ) { |
| 179 |
$tz = timezone_name_from_abbr( '', $seconds, 0 ); |
| 180 |
} |
| 181 |
|
| 182 |
return $tz; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Get days of week |
| 187 |
* |
| 188 |
* @return mixed|void |
| 189 |
*/ |
| 190 |
public function get_days_of_week() { |
| 191 |
$timestamp = strtotime( 'next Sunday' ); |
| 192 |
$days = array(); |
| 193 |
for ( $i = 0; $i < 7; $i ++ ) { |
| 194 |
$days[ strtolower( date( 'l', $timestamp ) ) ] = date_i18n( 'l', $timestamp ); |
| 195 |
$timestamp = strtotime( '+1 day', $timestamp ); |
| 196 |
} |
| 197 |
|
| 198 |
return $days; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* We translate the datetime format from php to something that momentjs can understand |
| 203 |
* |
| 204 |
* @param $format |
| 205 |
* |
| 206 |
* @return string |
| 207 |
*/ |
| 208 |
public function moment_datetime_format_from( $format ) { |
| 209 |
$replacements = [ |
| 210 |
'd' => 'DD', |
| 211 |
'D' => 'ddd', |
| 212 |
'j' => 'D', |
| 213 |
'l' => 'dddd', |
| 214 |
'N' => 'E', |
| 215 |
'S' => 'o', |
| 216 |
'w' => 'e', |
| 217 |
'z' => 'DDD', |
| 218 |
'W' => 'W', |
| 219 |
'F' => 'MMMM', |
| 220 |
'm' => 'MM', |
| 221 |
'M' => 'MMM', |
| 222 |
'n' => 'M', |
| 223 |
't' => '', // no equivalent |
| 224 |
'L' => '', // no equivalent |
| 225 |
'o' => 'YYYY', |
| 226 |
'Y' => 'YYYY', |
| 227 |
'y' => 'YY', |
| 228 |
'a' => 'a', |
| 229 |
'A' => 'A', |
| 230 |
'B' => '', // no equivalent |
| 231 |
'g' => 'h', |
| 232 |
'G' => 'H', |
| 233 |
'h' => 'hh', |
| 234 |
'H' => 'HH', |
| 235 |
'i' => 'mm', |
| 236 |
's' => 'ss', |
| 237 |
'u' => 'SSS', |
| 238 |
'e' => 'zz', // deprecated since version 1.6.0 of moment.js |
| 239 |
'I' => '', // no equivalent |
| 240 |
'O' => '', // no equivalent |
| 241 |
'P' => '', // no equivalent |
| 242 |
'T' => '', // no equivalent |
| 243 |
'Z' => '', // no equivalent |
| 244 |
'c' => '', // no equivalent |
| 245 |
'r' => '', // no equivalent |
| 246 |
'U' => 'X', |
| 247 |
]; |
| 248 |
|
| 249 |
return strtr( $format, $replacements ); |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* This will calculate the date interval time |
| 254 |
* @param string $date |
| 255 |
* |
| 256 |
* @return string $time |
| 257 |
*/ |
| 258 |
public function calculate_date_interval( $date ) { |
| 259 |
$interval = ''; |
| 260 |
if ( '24 hours' == $date ) { |
| 261 |
$interval = 'P1D'; |
| 262 |
} else if ( '7 days' == $date ) { |
| 263 |
$interval = 'P7D'; |
| 264 |
} else if ( '30 days' == $date ) { |
| 265 |
$interval = 'P30D'; |
| 266 |
} else if ( '3 months' == $date ) { |
| 267 |
$interval = 'P3M'; |
| 268 |
} else if ( '6 months' == $date ) { |
| 269 |
$interval = 'P6M'; |
| 270 |
} else if ( '12 months' == $date ) { |
| 271 |
$interval = 'P12M'; |
| 272 |
} |
| 273 |
|
| 274 |
return $interval; |
| 275 |
} |
| 276 |
} |
| 277 |
|