PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.2.0
Defender Security – Malware Scanner, Login Security & Firewall v6.2.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 6.2.0, at src/traits/formats.php

361 lines 10.1 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 ( ! filter_var( $date, FILTER_VALIDATE_INT ) ) {
123 $date = strtotime( $date );
124 }
125 if ( false === $date ) {
126 return 'n/a';
127 }
128 if ( strtotime( '-24 hours' ) > $date ) {
129 return $this->format_date_time( $date );
130 } else {
131 return human_time_diff( $date, time() ) . ' ' . esc_html__( 'ago', 'defender-security' );
132 }
133 }
134
135 /**
136 * Return times frame for selectbox.
137 *
138 * @return array
139 */
140 public function get_times() {
141 $times_interval = apply_filters( 'defender_get_times_interval', array( '00', '30' ) );
142 $times_interval = is_array( $times_interval ) ? $times_interval : (array) $times_interval;
143 $data = array();
144 for ( $i = 0; $i < 24; $i++ ) {
145 foreach ( $times_interval as $min ) {
146 $time = $i . ':' . $min;
147 $data[ $time ] = date_i18n( 'h:i A', strtotime( $time ) );
148 }
149 }
150
151 return $data;
152 }
153
154 /**
155 * Converts a date string to a local timestamp.
156 *
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.
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 */
176 public function local_to_utc( $timestring ) {
177 $tz = get_option( 'timezone_string' );
178 if ( '' === $tz || false === $tz ) {
179 $gmt_offset = get_option( 'gmt_offset' );
180 if ( 0 === $gmt_offset ) {
181 return strtotime( $timestring );
182 }
183 $tz = $this->get_timezone_string( $gmt_offset );
184 }
185 if ( '' === $tz || false === $tz ) {
186 $tz = 'UTC';
187 }
188 $timezone = new DateTimeZone( $tz );
189 $time = new DateTime( $timestring, $timezone );
190
191 return $time->getTimestamp();
192 }
193
194 /**
195 * Returns the timezone string based on the given offset.
196 *
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.
200 */
201 public function get_timezone_string( $timezone ) {
202 $timezone = explode( '.', $timezone );
203 if ( isset( $timezone[1] ) ) {
204 $timezone[1] = 30;
205 } else {
206 $timezone[1] = '00';
207 }
208 $offset = implode( ':', $timezone );
209
210 [ $hours, $minutes ] = array_map( 'intval', explode( ':', $offset ) );
211 $seconds = $hours * 60 * 60 + $minutes * 60;
212 $lc = localtime( time(), true );
213 if ( isset( $lc['tm_isdst'] ) ) {
214 $isdst = $lc['tm_isdst'];
215 } else {
216 $isdst = 0;
217 }
218 $tz = timezone_name_from_abbr( '', $seconds, $isdst );
219 if ( false === $tz ) {
220 $tz = timezone_name_from_abbr( '', $seconds, 0 );
221 }
222
223 return $tz;
224 }
225
226 /**
227 * Get days of week.
228 *
229 * @return array
230 */
231 public function get_days_of_week() {
232 $timestamp = strtotime( 'next Sunday' );
233 $days = array();
234 for ( $i = 0; $i < 7; $i++ ) {
235 $days[ strtolower( gmdate( 'l', $timestamp ) ) ] = date_i18n( 'l', $timestamp );
236 $timestamp = strtotime( '+1 day', $timestamp );
237 }
238
239 return $days;
240 }
241
242 /**
243 * Translates the datetime format from PHP to something that moment.js can understand.
244 *
245 * @param string $format The PHP datetime format to be translated.
246 *
247 * @return string The translated datetime format for moment.js.
248 */
249 public function moment_datetime_format_from( $format ) {
250 $replacements = array(
251 'd' => 'DD',
252 'D' => 'ddd',
253 'j' => 'D',
254 'l' => 'dddd',
255 'N' => 'E',
256 'S' => 'o',
257 'w' => 'e',
258 'z' => 'DDD',
259 'W' => 'W',
260 'F' => 'MMMM',
261 'm' => 'MM',
262 'M' => 'MMM',
263 'n' => 'M',
264 't' => '', // no equivalent.
265 'L' => '', // no equivalent.
266 'o' => 'YYYY',
267 'Y' => 'YYYY',
268 'y' => 'YY',
269 'a' => 'a',
270 'A' => 'A',
271 'B' => '', // no equivalent.
272 'g' => 'h',
273 'G' => 'H',
274 'h' => 'hh',
275 'H' => 'HH',
276 'i' => 'mm',
277 's' => 'ss',
278 'u' => 'SSS',
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.
287 'U' => 'X',
288 );
289
290 return strtr( $format, $replacements );
291 }
292
293 /**
294 * Calculates the date interval based on the given date.
295 *
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.
301 */
302 public function calculate_date_interval( $date ): string {
303 $interval = '';
304 if ( '24 hours' === $date ) {
305 $interval = 'P1D';
306 } elseif ( '7 days' === $date ) {
307 $interval = 'P7D';
308 } elseif ( '30 days' === $date ) {
309 $interval = 'P30D';
310 } elseif ( '3 months' === $date ) {
311 $interval = 'P3M';
312 } elseif ( '6 months' === $date ) {
313 $interval = 'P6M';
314 } elseif ( '12 months' === $date ) {
315 $interval = 'P12M';
316 }
317
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' );
359 }
360 }
361