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

340 lines 9.4 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 = (array) apply_filters( 'defender_get_times_interval', array( '00', '30' ) );
136 $data = array();
137 for ( $i = 0; $i < 24; $i++ ) {
138 foreach ( $times_interval as $min ) {
139 $time = $i . ':' . $min;
140 $data[ $time ] = date_i18n( 'h:i A', strtotime( $time ) );
141 }
142 }
143
144 return $data;
145 }
146
147 /**
148 * Converts a local timestamp to UTC.
149 *
150 * @param string $timestring The local timestamp to convert.
151 *
152 * @return false|int The UTC timestamp, or false on failure.
153 * @throws Exception If an error occurs during the conversion.
154 */
155 public function local_to_utc( $timestring ) {
156 $tz = get_option( 'timezone_string' );
157 if ( '' === $tz || false === $tz ) {
158 $gmt_offset = get_option( 'gmt_offset' );
159 if ( 0 === $gmt_offset ) {
160 return strtotime( $timestring );
161 }
162 $tz = $this->get_timezone_string( $gmt_offset );
163 }
164 if ( '' === $tz || false === $tz ) {
165 $tz = 'UTC';
166 }
167 $timezone = new DateTimeZone( $tz );
168 $time = new DateTime( $timestring, $timezone );
169
170 return $time->getTimestamp();
171 }
172
173 /**
174 * Returns the timezone string based on the given offset.
175 *
176 * @param string $timezone The timezone offset in the format '+/-HH.MM'.
177 *
178 * @return false|string The timezone string or false if it cannot be determined.
179 */
180 public function get_timezone_string( $timezone ) {
181 $timezone = explode( '.', $timezone );
182 if ( isset( $timezone[1] ) ) {
183 $timezone[1] = 30;
184 } else {
185 $timezone[1] = '00';
186 }
187 $offset = implode( ':', $timezone );
188
189 [ $hours, $minutes ] = array_map( 'intval', explode( ':', $offset ) );
190 $seconds = $hours * 60 * 60 + $minutes * 60;
191 $lc = localtime( time(), true );
192 if ( isset( $lc['tm_isdst'] ) ) {
193 $isdst = $lc['tm_isdst'];
194 } else {
195 $isdst = 0;
196 }
197 $tz = timezone_name_from_abbr( '', $seconds, $isdst );
198 if ( false === $tz ) {
199 $tz = timezone_name_from_abbr( '', $seconds, 0 );
200 }
201
202 return $tz;
203 }
204
205 /**
206 * Get days of week.
207 *
208 * @return array
209 */
210 public function get_days_of_week() {
211 $timestamp = strtotime( 'next Sunday' );
212 $days = array();
213 for ( $i = 0; $i < 7; $i++ ) {
214 $days[ strtolower( gmdate( 'l', $timestamp ) ) ] = date_i18n( 'l', $timestamp );
215 $timestamp = strtotime( '+1 day', $timestamp );
216 }
217
218 return $days;
219 }
220
221 /**
222 * Translates the datetime format from PHP to something that moment.js can understand.
223 *
224 * @param string $format The PHP datetime format to be translated.
225 *
226 * @return string The translated datetime format for moment.js.
227 */
228 public function moment_datetime_format_from( $format ) {
229 $replacements = array(
230 'd' => 'DD',
231 'D' => 'ddd',
232 'j' => 'D',
233 'l' => 'dddd',
234 'N' => 'E',
235 'S' => 'o',
236 'w' => 'e',
237 'z' => 'DDD',
238 'W' => 'W',
239 'F' => 'MMMM',
240 'm' => 'MM',
241 'M' => 'MMM',
242 'n' => 'M',
243 't' => '', // no equivalent.
244 'L' => '', // no equivalent.
245 'o' => 'YYYY',
246 'Y' => 'YYYY',
247 'y' => 'YY',
248 'a' => 'a',
249 'A' => 'A',
250 'B' => '', // no equivalent.
251 'g' => 'h',
252 'G' => 'H',
253 'h' => 'hh',
254 'H' => 'HH',
255 'i' => 'mm',
256 's' => 'ss',
257 'u' => 'SSS',
258 'e' => 'zz', // deprecated since version 1.6.0 of moment.js .
259 'I' => '', // no equivalent.
260 'O' => '', // no equivalent.
261 'P' => '', // no equivalent.
262 'T' => '', // no equivalent.
263 'Z' => '', // no equivalent.
264 'c' => '', // no equivalent.
265 'r' => '', // no equivalent.
266 'U' => 'X',
267 );
268
269 return strtr( $format, $replacements );
270 }
271
272 /**
273 * Calculates the date interval based on the given date.
274 *
275 * @param string $date The date to calculate the interval for. Can be '24 hours', '7 days', '30 days',
276 * '3 months', '6 months' or '12 months'.
277 *
278 * @return string The date interval in ISO 8601 format. Returns an empty string if the given date is not
279 * recognized.
280 */
281 public function calculate_date_interval( $date ): string {
282 $interval = '';
283 if ( '24 hours' === $date ) {
284 $interval = 'P1D';
285 } elseif ( '7 days' === $date ) {
286 $interval = 'P7D';
287 } elseif ( '30 days' === $date ) {
288 $interval = 'P30D';
289 } elseif ( '3 months' === $date ) {
290 $interval = 'P3M';
291 } elseif ( '6 months' === $date ) {
292 $interval = 'P6M';
293 } elseif ( '12 months' === $date ) {
294 $interval = 'P12M';
295 }
296
297 return $interval;
298 }
299
300 /**
301 * Returns a human-readable date string in the local timezone, based on the given timestamp.
302 *
303 * @param int $timestamp The timestamp to convert to a local human-readable date string.
304 *
305 * @return string The local human-readable date string in the format 'Day, Date of Month Year at Hour:Minute:Second
306 * AM/PM'.
307 */
308 public function get_local_human_date( int $timestamp ): string {
309 return wp_date( 'l, jS \of F, Y \a\t h:i:s A', $timestamp + ( 3600 * get_option( 'gmt_offset' ) ) );
310 }
311
312 /**
313 * Format the time difference between now and the given time.
314 *
315 * This takes a given time in the past and calculates the time difference
316 * between now and then. The result is a human-readable string, such as
317 * "3 days ago" or "2 months ago".
318 *
319 * @param string $last_time The time in the past. This should be a string in
320 * a format that can be parsed by strtotime().
321 *
322 * @return string Formatted time difference.
323 */
324 public function get_time_diff( string $last_time ): string {
325 // If the given time is empty, return a string indicating that the
326 // feature has never been used.
327 if ( '' === $last_time ) {
328 return esc_html__( 'Never', 'defender-security' );
329 }
330
331 // Get the time difference between now and the given time. This is
332 // returned as a human-readable string, such as "3 days" or "2 months".
333 $diff = human_time_diff( strtotime( $last_time ) );
334
335 // Append the word "ago" to the time difference, so that the final
336 // result is a string like "3 days ago" or "2 months ago".
337 return $diff . ' ' . esc_html__( 'ago', 'defender-security' );
338 }
339 }
340