getTimestamp() ); } /** * Whether a stored UTC datetime is in the past. * * A null or empty value means "no expiry". * * @param string|null $gmt UTC datetime. * @return bool */ public static function is_expired( ?string $gmt ): bool { if ( null === $gmt || '' === $gmt ) { return false; } try { $date = new \DateTimeImmutable( $gmt, new \DateTimeZone( 'UTC' ) ); } catch ( \Exception $e ) { return true; } return $date->getTimestamp() <= time(); } /** * Returns the requesting client IP. * * Only REMOTE_ADDR is trusted; proxy headers are deliberately ignored. * * @return string Empty string when unavailable. */ public static function client_ip(): string { if ( empty( $_SERVER['REMOTE_ADDR'] ) || ! is_string( $_SERVER['REMOTE_ADDR'] ) ) { return ''; } return sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ); } /** * Returns an irreversible hash of an IP address. * * @param string $ip IP address, normally from client_ip(). * @return string */ public static function hash_ip( string $ip = '' ): string { return hash( 'sha256', (string) wp_salt( 'proofkit_ip' ) . $ip ); } /** * Formats a byte count for display. * * @param int $bytes Byte count. * @return string */ public static function human_size( int $bytes ): string { $bytes = max( 0, $bytes ); if ( $bytes >= GB_IN_BYTES ) { /* translators: %s: file size in gigabytes, e.g. "1.5". */ return sprintf( __( '%s GB', 'proofkit' ), number_format_i18n( $bytes / GB_IN_BYTES, 1 ) ); } if ( $bytes >= MB_IN_BYTES ) { /* translators: %s: file size in megabytes, e.g. "1.5". */ return sprintf( __( '%s MB', 'proofkit' ), number_format_i18n( $bytes / MB_IN_BYTES, 1 ) ); } if ( $bytes >= KB_IN_BYTES ) { /* translators: %s: file size in kilobytes, e.g. "1.5". */ return sprintf( __( '%s KB', 'proofkit' ), number_format_i18n( $bytes / KB_IN_BYTES, 1 ) ); } /* translators: %s: file size in bytes, e.g. "512". */ return sprintf( __( '%s B', 'proofkit' ), number_format_i18n( $bytes ) ); } /** * Converts a WP_Error into a REST-friendly array. * * @param \WP_Error $error Error object. * @return array{error: int|string, message: string} */ public static function error_response( \WP_Error $error ): array { return array( 'error' => $error->get_error_code(), 'message' => $error->get_error_message(), ); } /** * Writes a debug log line when WP_DEBUG is enabled. * * @param string $message Log message. * @param array $context Optional structured context. * @return void */ public static function log( string $message, array $context = array() ): void { if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) { return; } $line = 'ProofKit: ' . $message; if ( array() !== $context ) { $line .= ' ' . (string) wp_json_encode( $context ); } error_log( $line ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log } }