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

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