| @@ -93,12 +93,15 @@ | ||
| 93 | 93 | array( 60, esc_html__( 'minute', 'defender-security' ) ), |
| 94 | 94 | array( 1, esc_html__( 'second', 'defender-security' ) ), |
| 95 | 95 | ); |
| 96 | 96 | |
| 97 | + $seconds = 0; | |
| 98 | + $name = ''; | |
| 99 | + $count = 0; | |
| 97 | 100 | for ( $i = 0, $j = count( $chunks ); $i < $j; $i++ ) { |
| 98 | 101 | $seconds = $chunks[ $i ][0]; |
| 99 | 102 | $name = $chunks[ $i ][1]; |
| 100 | - $count = floor( $since / $seconds ); | |
| 103 | + $count = (int) floor( $since / $seconds ); | |
| 101 | 104 | if ( 0 !== $count ) { |
| 102 | 105 | break; |
| 103 | 106 | } |
| 104 | 107 | } |
| @@ -115,8 +118,14 @@ | ||
| 115 | 118 | * |
| 116 | 119 | * @return false|string |
| 117 | 120 | */ |
| 118 | 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 | + } | |
| 119 | 128 | if ( strtotime( '-24 hours' ) > $date ) { |
| 120 | 129 | return $this->format_date_time( $date ); |
| 121 | 130 | } else { |
| 122 | 131 | return human_time_diff( $date, time() ) . ' ' . esc_html__( 'ago', 'defender-security' ); |
| @@ -128,9 +137,10 @@ | ||
| 128 | 137 | * |
| 129 | 138 | * @return array |
| 130 | 139 | */ |
| 131 | 140 | public function get_times() { |
| 132 | - $times_interval = (array) apply_filters( 'defender_get_times_interval', array( '00', '30' ) ); | |
| 141 | + $times_interval = apply_filters( 'defender_get_times_interval', array( '00', '30' ) ); | |
| 142 | + $times_interval = is_array( $times_interval ) ? $times_interval : (array) $times_interval; | |
| 133 | 143 | $data = array(); |
| 134 | 144 | for ( $i = 0; $i < 24; $i++ ) { |
| 135 | 145 | foreach ( $times_interval as $min ) { |
| 136 | 146 | $time = $i . ':' . $min; |
| @@ -141,8 +151,22 @@ | ||
| 141 | 151 | return $data; |
| 142 | 152 | } |
| 143 | 153 | |
| 144 | 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 | + /** | |
| 145 | 169 | * Converts a local timestamp to UTC. |
| 146 | 170 | * |
| 147 | 171 | * @param string $timestring The local timestamp to convert. |
| 148 | 172 | * |
| @@ -150,9 +174,9 @@ | ||
| 150 | 174 | * @throws Exception If an error occurs during the conversion. |
| 151 | 175 | */ |
| 152 | 176 | public function local_to_utc( $timestring ) { |
| 153 | 177 | $tz = get_option( 'timezone_string' ); |
| 154 | - if ( ! $tz ) { | |
| 178 | + if ( '' === $tz || false === $tz ) { | |
| 155 | 179 | $gmt_offset = get_option( 'gmt_offset' ); |
| 156 | 180 | if ( 0 === $gmt_offset ) { |
| 157 | 181 | return strtotime( $timestring ); |
| 158 | 182 | } |
| @@ -157,9 +181,9 @@ | ||
| 157 | 181 | return strtotime( $timestring ); |
| 158 | 182 | } |
| 159 | 183 | $tz = $this->get_timezone_string( $gmt_offset ); |
| 160 | 184 | } |
| 161 | - if ( ! $tz ) { | |
| 185 | + if ( '' === $tz || false === $tz ) { | |
| 162 | 186 | $tz = 'UTC'; |
| 163 | 187 | } |
| 164 | 188 | $timezone = new DateTimeZone( $tz ); |
| 165 | 189 | $time = new DateTime( $timestring, $timezone ); |
| @@ -182,9 +206,9 @@ | ||
| 182 | 206 | $timezone[1] = '00'; |
| 183 | 207 | } |
| 184 | 208 | $offset = implode( ':', $timezone ); |
| 185 | 209 | |
| 186 | - [ $hours, $minutes ] = explode( ':', $offset ); | |
| 210 | + [ $hours, $minutes ] = array_map( 'intval', explode( ':', $offset ) ); | |
| 187 | 211 | $seconds = $hours * 60 * 60 + $minutes * 60; |
| 188 | 212 | $lc = localtime( time(), true ); |
| 189 | 213 | if ( isset( $lc['tm_isdst'] ) ) { |
| 190 | 214 | $isdst = $lc['tm_isdst']; |
| @@ -320,9 +344,9 @@ | ||
| 320 | 344 | */ |
| 321 | 345 | public function get_time_diff( string $last_time ): string { |
| 322 | 346 | // If the given time is empty, return a string indicating that the |
| 323 | 347 | // feature has never been used. |
| 324 | - if ( empty( $last_time ) ) { | |
| 348 | + if ( '' === $last_time ) { | |
| 325 | 349 | return esc_html__( 'Never', 'defender-security' ); |
| 326 | 350 | } |
| 327 | 351 | |
| 328 | 352 | // Get the time difference between now and the given time. This is |