hostIdentity( $normalizedUrl ); /** * Filters host patterns that are exempt from activation limits. * * @since 1.0.0 * * @param array{exact: array, suffix: array, prefix: array} $patterns The default local-host patterns. */ $patterns = apply_filters( 'releasecart_local_url_patterns', array( 'exact' => array( 'localhost' ), 'suffix' => array( '.test', '.local', '.dev' ), 'prefix' => array( 'staging.', 'dev.' ), ) ); foreach ( (array) ( $patterns['exact'] ?? array() ) as $exact ) { if ( $identity === $exact ) { return true; } } foreach ( (array) ( $patterns['suffix'] ?? array() ) as $suffix ) { if ( '' !== $suffix && str_ends_with( $identity, $suffix ) ) { return true; } } foreach ( (array) ( $patterns['prefix'] ?? array() ) as $prefix ) { if ( '' !== $prefix && str_starts_with( $identity, $prefix ) ) { return true; } } // A valid non-public IP is local. if ( false !== filter_var( $identity, FILTER_VALIDATE_IP ) ) { $is_public = filter_var( $identity, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ); if ( false === $is_public ) { return true; } } return false; } /** * Extract the host without path, port or IPv6 brackets. * * @since 1.0.0 * * @param string $normalizedUrl A URL already run through {@see normalize()}. * @return string The host identity: a hostname, or a bare IP for IP hosts. */ private function hostIdentity( string $normalizedUrl ): string { $host = $normalizedUrl; // Remove the path. $slash = strpos( $host, '/' ); if ( false !== $slash ) { $host = substr( $host, 0, $slash ); } // Strip brackets from an IPv6 literal. if ( str_starts_with( $host, '[' ) ) { $end = strpos( $host, ']' ); return false !== $end ? substr( $host, 1, $end - 1 ) : $host; } // Remove a trailing port. $colon = strrpos( $host, ':' ); return false !== $colon ? substr( $host, 0, $colon ) : $host; } }