| @@ -1,16 +1,26 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Helper functions for formatting data. | |
| 4 | + * | |
| 5 | + * @package WP_Defender\Traits | |
| 6 | + */ | |
| 2 | 7 | |
| 3 | 8 | namespace WP_Defender\Traits; |
| 4 | 9 | |
| 10 | +use DateTime; | |
| 11 | +use Exception; | |
| 12 | +use DateTimeZone; | |
| 13 | + | |
| 5 | 14 | trait Formats { |
| 15 | + | |
| 6 | 16 | /** |
| 7 | - * Convert a unixtimestamp into the blog datetime | |
| 17 | + * Convert a unix timestamp into the blog datetime. | |
| 8 | 18 | * |
| 9 | - * @param $timestamp | |
| 10 | - * @param bool $i18n | |
| 19 | + * @param mixed $timestamp Timestamp to convert. | |
| 20 | + * @param bool $i18n Should return date in localized format. | |
| 11 | 21 | * |
| 12 | - * @return false|string | |
| 22 | + * @return bool|string | |
| 13 | 23 | */ |
| 14 | 24 | public function format_date_time( $timestamp, $i18n = true ) { |
| 15 | 25 | if ( ! filter_var( $timestamp, FILTER_VALIDATE_INT ) ) { |
| 16 | 26 | $timestamp = strtotime( $timestamp ); |
| @@ -17,23 +27,22 @@ | ||
| 17 | 27 | } |
| 18 | 28 | if ( false === $timestamp ) { |
| 19 | 29 | return 'n/a'; |
| 20 | 30 | } |
| 31 | + | |
| 32 | + // Convert the given timestamp to a local time Unix timestamp. | |
| 33 | + $time = strtotime( get_date_from_gmt( gmdate( 'Y-m-d H:i:s', $timestamp ) ) ); | |
| 21 | 34 | $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 | 35 | |
| 27 | - return date_i18n( $format, strtotime( $time ) ); | |
| 36 | + return $i18n ? wp_date( $format, $timestamp ) : gmdate( $format, $time ); | |
| 28 | 37 | } |
| 29 | 38 | |
| 30 | 39 | /** |
| 31 | - * Persistent date & time formats for Hub data | |
| 40 | + * Persistent date & time formats for Hub data. | |
| 32 | 41 | * |
| 33 | - * @param $timestamp | |
| 42 | + * @param int $timestamp Unix timestamp that defaults to the current local time. | |
| 34 | 43 | * |
| 35 | - * @return string | |
| 44 | + * @return string|bool | |
| 36 | 45 | */ |
| 37 | 46 | public function persistent_hub_datetime_format( $timestamp ) { |
| 38 | 47 | return gmdate( 'Y-m-d g:i a', $timestamp ); |
| 39 | 48 | } |
| @@ -38,13 +47,15 @@ | ||
| 38 | 47 | return gmdate( 'Y-m-d g:i a', $timestamp ); |
| 39 | 48 | } |
| 40 | 49 | |
| 41 | 50 | /** |
| 42 | - * @param $bytes | |
| 51 | + * Formats a given number of bytes into a human-readable string representation. | |
| 43 | 52 | * |
| 44 | - * @return string | |
| 53 | + * @param int $bytes The number of bytes to format. | |
| 54 | + * | |
| 55 | + * @return string The formatted string representation of the number of bytes. | |
| 45 | 56 | */ |
| 46 | - public function format_bytes_into_readable( $bytes ) { | |
| 57 | + public function format_bytes_into_readable( $bytes ): string { | |
| 47 | 58 | if ( $bytes >= 1073741824 ) { |
| 48 | 59 | $bytes = number_format( $bytes / 1073741824, 2 ) . ' GB'; |
| 49 | 60 | } elseif ( $bytes >= 1048576 ) { |
| 50 | 61 | $bytes = number_format( $bytes / 1048576, 2 ) . ' MB'; |
| @@ -50,11 +61,11 @@ | ||
| 50 | 61 | $bytes = number_format( $bytes / 1048576, 2 ) . ' MB'; |
| 51 | 62 | } elseif ( $bytes >= 1024 ) { |
| 52 | 63 | $bytes = number_format( $bytes / 1024, 2 ) . ' KB'; |
| 53 | 64 | } elseif ( $bytes > 1 ) { |
| 54 | - $bytes = $bytes . ' bytes'; | |
| 65 | + $bytes .= ' bytes'; | |
| 55 | 66 | } elseif ( 1 === $bytes ) { |
| 56 | - $bytes = $bytes . ' byte'; | |
| 67 | + $bytes .= ' byte'; | |
| 57 | 68 | } else { |
| 58 | 69 | $bytes = '0 bytes'; |
| 59 | 70 | } |
| 60 | 71 | |
| @@ -61,31 +72,36 @@ | ||
| 61 | 72 | return $bytes; |
| 62 | 73 | } |
| 63 | 74 | |
| 64 | 75 | /** |
| 65 | - * @param $since | |
| 76 | + * Calculates the time elapsed since a given timestamp and returns a human-readable string representation. | |
| 66 | 77 | * |
| 67 | - * @return string | |
| 78 | + * @param int $since The timestamp to calculate the time elapsed from. | |
| 79 | + * | |
| 80 | + * @return string The human-readable string representation of the time elapsed. | |
| 68 | 81 | */ |
| 69 | - public function time_since( $since ) { | |
| 82 | + public function time_since( $since ): string { | |
| 70 | 83 | $since = time() - $since; |
| 71 | 84 | if ( $since < 0 ) { |
| 72 | 85 | $since = 0; |
| 73 | 86 | } |
| 74 | 87 | $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' ) ), | |
| 88 | + array( 60 * 60 * 24 * 365, esc_html__( 'year', 'defender-security' ) ), | |
| 89 | + array( 60 * 60 * 24 * 30, esc_html__( 'month', 'defender-security' ) ), | |
| 90 | + array( 60 * 60 * 24 * 7, esc_html__( 'week', 'defender-security' ) ), | |
| 91 | + array( 60 * 60 * 24, esc_html__( 'day', 'defender-security' ) ), | |
| 92 | + array( 60 * 60, esc_html__( 'hour', 'defender-security' ) ), | |
| 93 | + array( 60, esc_html__( 'minute', 'defender-security' ) ), | |
| 94 | + array( 1, esc_html__( 'second', 'defender-security' ) ), | |
| 82 | 95 | ); |
| 83 | 96 | |
| 84 | - for ( $i = 0, $j = count( $chunks ); $i < $j; $i ++ ) { | |
| 97 | + $seconds = 0; | |
| 98 | + $name = ''; | |
| 99 | + $count = 0; | |
| 100 | + for ( $i = 0, $j = count( $chunks ); $i < $j; $i++ ) { | |
| 85 | 101 | $seconds = $chunks[ $i ][0]; |
| 86 | 102 | $name = $chunks[ $i ][1]; |
| 87 | - $count = floor( $since / $seconds ); | |
| 103 | + $count = (int) floor( $since / $seconds ); | |
| 88 | 104 | if ( 0 !== $count ) { |
| 89 | 105 | break; |
| 90 | 106 | } |
| 91 | 107 | } |
| @@ -95,31 +111,38 @@ | ||
| 95 | 111 | return $print; |
| 96 | 112 | } |
| 97 | 113 | |
| 98 | 114 | /** |
| 99 | - * Get formatted date | |
| 115 | + * Get formatted date. | |
| 100 | 116 | * |
| 101 | - * @param $date | |
| 117 | + * @param int $date Date timestamp. | |
| 102 | 118 | * |
| 103 | - * @return string | |
| 119 | + * @return false|string | |
| 104 | 120 | */ |
| 105 | 121 | public function get_date( $date ) { |
| 122 | + if ( ! filter_var( $date, FILTER_VALIDATE_INT ) ) { | |
| 123 | + $date = strtotime( $date ); | |
| 124 | + } | |
| 125 | + if ( false === $date ) { | |
| 126 | + return 'n/a'; | |
| 127 | + } | |
| 106 | 128 | if ( strtotime( '-24 hours' ) > $date ) { |
| 107 | - return $this->format_date_time( gmdate( 'Y-m-d H:i:s', $date ) ); | |
| 129 | + return $this->format_date_time( $date ); | |
| 108 | 130 | } else { |
| 109 | - return human_time_diff( $date, time() ) . ' ' . __( 'ago', 'wpdef' ); // phpcs:ignore | |
| 131 | + return human_time_diff( $date, time() ) . ' ' . esc_html__( 'ago', 'defender-security' ); | |
| 110 | 132 | } |
| 111 | 133 | } |
| 112 | 134 | |
| 113 | 135 | /** |
| 114 | - * Return times frame for selectbox | |
| 136 | + * Return times frame for selectbox. | |
| 115 | 137 | * |
| 116 | 138 | * @return array |
| 117 | 139 | */ |
| 118 | 140 | public function get_times() { |
| 119 | 141 | $times_interval = apply_filters( 'defender_get_times_interval', array( '00', '30' ) ); |
| 142 | + $times_interval = is_array( $times_interval ) ? $times_interval : (array) $times_interval; | |
| 120 | 143 | $data = array(); |
| 121 | - for ( $i = 0; $i < 24; $i ++ ) { | |
| 144 | + for ( $i = 0; $i < 24; $i++ ) { | |
| 122 | 145 | foreach ( $times_interval as $min ) { |
| 123 | 146 | $time = $i . ':' . $min; |
| 124 | 147 | $data[ $time ] = date_i18n( 'h:i A', strtotime( $time ) ); |
| 125 | 148 | } |
| @@ -128,16 +151,32 @@ | ||
| 128 | 151 | return $data; |
| 129 | 152 | } |
| 130 | 153 | |
| 131 | 154 | /** |
| 132 | - * @param string $timestring | |
| 155 | + * Converts a date string to a local timestamp. | |
| 133 | 156 | * |
| 134 | - * @return false|int | |
| 135 | - * @throws \Exception | |
| 157 | + * @param string $date The date string to convert. | |
| 158 | + * @param bool $end_of_day Whether to use the end of the day. | |
| 159 | + * | |
| 160 | + * @return int The local timestamp. | |
| 136 | 161 | */ |
| 162 | + public function date_string_to_timestamp( string $date, bool $end_of_day = false ): int { | |
| 163 | + $dt = new DateTime( $date, wp_timezone() ); | |
| 164 | + $end_of_day ? $dt->setTime( 23, 59, 59 ) : $dt->setTime( 0, 0, 0 ); | |
| 165 | + return $dt->getTimestamp(); | |
| 166 | + } | |
| 167 | + | |
| 168 | + /** | |
| 169 | + * Converts a local timestamp to UTC. | |
| 170 | + * | |
| 171 | + * @param string $timestring The local timestamp to convert. | |
| 172 | + * | |
| 173 | + * @return false|int The UTC timestamp, or false on failure. | |
| 174 | + * @throws Exception If an error occurs during the conversion. | |
| 175 | + */ | |
| 137 | 176 | public function local_to_utc( $timestring ) { |
| 138 | 177 | $tz = get_option( 'timezone_string' ); |
| 139 | - if ( ! $tz ) { | |
| 178 | + if ( '' === $tz || false === $tz ) { | |
| 140 | 179 | $gmt_offset = get_option( 'gmt_offset' ); |
| 141 | 180 | if ( 0 === $gmt_offset ) { |
| 142 | 181 | return strtotime( $timestring ); |
| 143 | 182 | } |
| @@ -142,21 +181,23 @@ | ||
| 142 | 181 | return strtotime( $timestring ); |
| 143 | 182 | } |
| 144 | 183 | $tz = $this->get_timezone_string( $gmt_offset ); |
| 145 | 184 | } |
| 146 | - if ( ! $tz ) { | |
| 185 | + if ( '' === $tz || false === $tz ) { | |
| 147 | 186 | $tz = 'UTC'; |
| 148 | 187 | } |
| 149 | - $timezone = new \DateTimeZone( $tz ); | |
| 150 | - $time = new \DateTime( $timestring, $timezone ); | |
| 188 | + $timezone = new DateTimeZone( $tz ); | |
| 189 | + $time = new DateTime( $timestring, $timezone ); | |
| 151 | 190 | |
| 152 | 191 | return $time->getTimestamp(); |
| 153 | 192 | } |
| 154 | 193 | |
| 155 | 194 | /** |
| 156 | - * @param string $timezone | |
| 195 | + * Returns the timezone string based on the given offset. | |
| 157 | 196 | * |
| 158 | - * @return false|string | |
| 197 | + * @param string $timezone The timezone offset in the format '+/-HH.MM'. | |
| 198 | + * | |
| 199 | + * @return false|string The timezone string or false if it cannot be determined. | |
| 159 | 200 | */ |
| 160 | 201 | public function get_timezone_string( $timezone ) { |
| 161 | 202 | $timezone = explode( '.', $timezone ); |
| 162 | 203 | if ( isset( $timezone[1] ) ) { |
| @@ -165,11 +206,11 @@ | ||
| 165 | 206 | $timezone[1] = '00'; |
| 166 | 207 | } |
| 167 | 208 | $offset = implode( ':', $timezone ); |
| 168 | 209 | |
| 169 | - [$hours, $minutes] = explode( ':', $offset ); | |
| 170 | - $seconds = $hours * 60 * 60 + $minutes * 60; | |
| 171 | - $lc = localtime( time(), true ); | |
| 210 | + [ $hours, $minutes ] = array_map( 'intval', explode( ':', $offset ) ); | |
| 211 | + $seconds = $hours * 60 * 60 + $minutes * 60; | |
| 212 | + $lc = localtime( time(), true ); | |
| 172 | 213 | if ( isset( $lc['tm_isdst'] ) ) { |
| 173 | 214 | $isdst = $lc['tm_isdst']; |
| 174 | 215 | } else { |
| 175 | 216 | $isdst = 0; |
| @@ -182,18 +223,18 @@ | ||
| 182 | 223 | return $tz; |
| 183 | 224 | } |
| 184 | 225 | |
| 185 | 226 | /** |
| 186 | - * Get days of week | |
| 227 | + * Get days of week. | |
| 187 | 228 | * |
| 188 | - * @return mixed|void | |
| 229 | + * @return array | |
| 189 | 230 | */ |
| 190 | 231 | public function get_days_of_week() { |
| 191 | 232 | $timestamp = strtotime( 'next Sunday' ); |
| 192 | 233 | $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 ); | |
| 234 | + for ( $i = 0; $i < 7; $i++ ) { | |
| 235 | + $days[ strtolower( gmdate( 'l', $timestamp ) ) ] = date_i18n( 'l', $timestamp ); | |
| 236 | + $timestamp = strtotime( '+1 day', $timestamp ); | |
| 196 | 237 | } |
| 197 | 238 | |
| 198 | 239 | return $days; |
| 199 | 240 | } |
| @@ -198,16 +239,16 @@ | ||
| 198 | 239 | return $days; |
| 199 | 240 | } |
| 200 | 241 | |
| 201 | 242 | /** |
| 202 | - * We translate the datetime format from php to something that momentjs can understand | |
| 243 | + * Translates the datetime format from PHP to something that moment.js can understand. | |
| 203 | 244 | * |
| 204 | - * @param $format | |
| 245 | + * @param string $format The PHP datetime format to be translated. | |
| 205 | 246 | * |
| 206 | - * @return string | |
| 247 | + * @return string The translated datetime format for moment.js. | |
| 207 | 248 | */ |
| 208 | 249 | public function moment_datetime_format_from( $format ) { |
| 209 | - $replacements = [ | |
| 250 | + $replacements = array( | |
| 210 | 251 | 'd' => 'DD', |
| 211 | 252 | 'D' => 'ddd', |
| 212 | 253 | 'j' => 'D', |
| 213 | 254 | 'l' => 'dddd', |
| @@ -219,16 +260,16 @@ | ||
| 219 | 260 | 'F' => 'MMMM', |
| 220 | 261 | 'm' => 'MM', |
| 221 | 262 | 'M' => 'MMM', |
| 222 | 263 | 'n' => 'M', |
| 223 | - 't' => '', // no equivalent | |
| 224 | - 'L' => '', // no equivalent | |
| 264 | + 't' => '', // no equivalent. | |
| 265 | + 'L' => '', // no equivalent. | |
| 225 | 266 | 'o' => 'YYYY', |
| 226 | 267 | 'Y' => 'YYYY', |
| 227 | 268 | 'y' => 'YY', |
| 228 | 269 | 'a' => 'a', |
| 229 | 270 | 'A' => 'A', |
| 230 | - 'B' => '', // no equivalent | |
| 271 | + 'B' => '', // no equivalent. | |
| 231 | 272 | 'g' => 'h', |
| 232 | 273 | 'G' => 'H', |
| 233 | 274 | 'h' => 'hh', |
| 234 | 275 | 'H' => 'HH', |
| @@ -234,43 +275,86 @@ | ||
| 234 | 275 | 'H' => 'HH', |
| 235 | 276 | 'i' => 'mm', |
| 236 | 277 | 's' => 'ss', |
| 237 | 278 | '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 | |
| 279 | + 'e' => 'zz', // deprecated since version 1.6.0 of moment.js . | |
| 280 | + 'I' => '', // no equivalent. | |
| 281 | + 'O' => '', // no equivalent. | |
| 282 | + 'P' => '', // no equivalent. | |
| 283 | + 'T' => '', // no equivalent. | |
| 284 | + 'Z' => '', // no equivalent. | |
| 285 | + 'c' => '', // no equivalent. | |
| 286 | + 'r' => '', // no equivalent. | |
| 246 | 287 | 'U' => 'X', |
| 247 | - ]; | |
| 288 | + ); | |
| 248 | 289 | |
| 249 | 290 | return strtr( $format, $replacements ); |
| 250 | 291 | } |
| 251 | 292 | |
| 252 | 293 | /** |
| 253 | - * This will calculate the date interval time | |
| 254 | - * @param string $date | |
| 294 | + * Calculates the date interval based on the given date. | |
| 255 | 295 | * |
| 256 | - * @return string $time | |
| 296 | + * @param string $date The date to calculate the interval for. Can be '24 hours', '7 days', '30 days', | |
| 297 | + * '3 months', '6 months' or '12 months'. | |
| 298 | + * | |
| 299 | + * @return string The date interval in ISO 8601 format. Returns an empty string if the given date is not | |
| 300 | + * recognized. | |
| 257 | 301 | */ |
| 258 | - public function calculate_date_interval( $date ) { | |
| 302 | + public function calculate_date_interval( $date ): string { | |
| 259 | 303 | $interval = ''; |
| 260 | - if ( '24 hours' == $date ) { | |
| 304 | + if ( '24 hours' === $date ) { | |
| 261 | 305 | $interval = 'P1D'; |
| 262 | - } else if ( '7 days' == $date ) { | |
| 306 | + } elseif ( '7 days' === $date ) { | |
| 263 | 307 | $interval = 'P7D'; |
| 264 | - } else if ( '30 days' == $date ) { | |
| 308 | + } elseif ( '30 days' === $date ) { | |
| 265 | 309 | $interval = 'P30D'; |
| 266 | - } else if ( '3 months' == $date ) { | |
| 310 | + } elseif ( '3 months' === $date ) { | |
| 267 | 311 | $interval = 'P3M'; |
| 268 | - } else if ( '6 months' == $date ) { | |
| 312 | + } elseif ( '6 months' === $date ) { | |
| 269 | 313 | $interval = 'P6M'; |
| 270 | - } else if ( '12 months' == $date ) { | |
| 314 | + } elseif ( '12 months' === $date ) { | |
| 271 | 315 | $interval = 'P12M'; |
| 272 | 316 | } |
| 273 | 317 | |
| 274 | 318 | return $interval; |
| 319 | + } | |
| 320 | + | |
| 321 | + /** | |
| 322 | + * Returns a human-readable date string in the local timezone, based on the given timestamp. | |
| 323 | + * | |
| 324 | + * @param int $timestamp The timestamp to convert to a local human-readable date string. | |
| 325 | + * | |
| 326 | + * @return string The local human-readable date string in the format 'Day, Date of Month Year at Hour:Minute:Second | |
| 327 | + * AM/PM'. | |
| 328 | + */ | |
| 329 | + public function get_local_human_date( int $timestamp ): string { | |
| 330 | + return wp_date( 'l, jS \of F, Y \a\t h:i:s A', $timestamp + ( 3600 * get_option( 'gmt_offset' ) ) ); | |
| 331 | + } | |
| 332 | + | |
| 333 | + /** | |
| 334 | + * Format the time difference between now and the given time. | |
| 335 | + * | |
| 336 | + * This takes a given time in the past and calculates the time difference | |
| 337 | + * between now and then. The result is a human-readable string, such as | |
| 338 | + * "3 days ago" or "2 months ago". | |
| 339 | + * | |
| 340 | + * @param string $last_time The time in the past. This should be a string in | |
| 341 | + * a format that can be parsed by strtotime(). | |
| 342 | + * | |
| 343 | + * @return string Formatted time difference. | |
| 344 | + */ | |
| 345 | + public function get_time_diff( string $last_time ): string { | |
| 346 | + // If the given time is empty, return a string indicating that the | |
| 347 | + // feature has never been used. | |
| 348 | + if ( '' === $last_time ) { | |
| 349 | + return esc_html__( 'Never', 'defender-security' ); | |
| 350 | + } | |
| 351 | + | |
| 352 | + // Get the time difference between now and the given time. This is | |
| 353 | + // returned as a human-readable string, such as "3 days" or "2 months". | |
| 354 | + $diff = human_time_diff( strtotime( $last_time ) ); | |
| 355 | + | |
| 356 | + // Append the word "ago" to the time difference, so that the final | |
| 357 | + // result is a string like "3 days ago" or "2 months ago". | |
| 358 | + return $diff . ' ' . esc_html__( 'ago', 'defender-security' ); | |
| 275 | 359 | } |
| 276 | 360 | } |