strlen( $suffix ) && substr( $host, -strlen( $suffix ) ) === $suffix ) { return true; } } return false; } /** * Why this address may not be used, or null when it is fine. * * Plain HTTP is allowed only for a development host, or while WordPress is in a local or * development environment with WP_DEBUG on. Anything else must be HTTPS: the secret-signed * requests carry a whole site, and the chunk bodies would be readable on the way. * * @param bool|null $debug_env Whether this is a local/development environment with WP_DEBUG; null reads WordPress. */ public static function insecureReason( string $url, ?bool $debug_env = null ): ?string { $parts = wp_parse_url( $url ); $scheme = is_array( $parts ) ? strtolower( (string) ( $parts['scheme'] ?? '' ) ) : ''; $host = is_array( $parts ) ? (string) ( $parts['host'] ?? '' ) : ''; if ( '' === $host || ! in_array( $scheme, array( 'http', 'https' ), true ) ) { return __( 'That is not a valid site address.', 'migratex' ); } if ( 'https' === $scheme ) { return null; } if ( self::isDevHost( $host ) || ( $debug_env ?? self::debugEnvironment() ) ) { return null; } return __( 'Site-to-site migration needs HTTPS on both sites. Enable HTTPS and try again.', 'migratex' ); } /** * Whether the request now being served arrived over HTTPS. A development site (judged by the address * this site is configured with, never by the Host header, which the client chooses) may use plain http. */ public static function requestIsSecure(): bool { $secure = is_ssl() || self::isDevSite(); return (bool) apply_filters( 'migratex_remote_request_is_secure', $secure ); } /** * Whether this WordPress is a development install: configured with a development address, or running * in a local/development environment with WP_DEBUG. */ public static function isDevSite(): bool { return self::isDevHost( (string) wp_parse_url( home_url(), PHP_URL_HOST ) ) || self::debugEnvironment(); } /** * Whether a request to $site_url may use the plain WordPress HTTP functions, which, unlike the "safe" * ones, will connect to loopback and private addresses. Only a development site talking to a * development host qualifies: a production server must never be steerable at its own network by * a pasted key. */ public static function mayReachLocal( string $site_url ): bool { return self::isDevHost( (string) wp_parse_url( $site_url, PHP_URL_HOST ) ) && self::isDevSite(); } private static function debugEnvironment(): bool { $env = function_exists( 'wp_get_environment_type' ) ? wp_get_environment_type() : 'production'; return defined( 'WP_DEBUG' ) && WP_DEBUG && in_array( $env, array( 'local', 'development' ), true ); } }