PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.9.0
Defender Security – Malware Scanner, Login Security & Firewall v5.9.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / traits / formats.php

formats.php in Defender Security – Malware Scanner, Login Security & Firewall 5.9.0, at src/traits/formats.php

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