strlen( $packed_ip ) * 8 ) { return false; } if ( 0 === $bits ) { return true; } $whole_bytes = intdiv( $bits, 8 ); $rest_bits = $bits % 8; if ( $whole_bytes > 0 && substr( $packed_ip, 0, $whole_bytes ) !== substr( $packed_subnet, 0, $whole_bytes ) ) { return false; } if ( 0 === $rest_bits ) { return true; } $mask = ~( ( 1 << ( 8 - $rest_bits ) ) - 1 ) & 0xFF; return ( ord( $packed_ip[ $whole_bytes ] ) & $mask ) === ( ord( $packed_subnet[ $whole_bytes ] ) & $mask ); } /** * Canonical binary form of an address, for equality comparison. * * @param string $ip Address. * @return string Packed form, or '' when the address is malformed. */ private static function normalise( $ip ) { $packed = @inet_pton( trim( (string) $ip ) ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- a malformed address is a '', not a warning. return false === $packed ? '' : $packed; } }