Automattic\WooCommerce\Internal\Admin
SiteHealth::is_uploads_test_file_served
Write a temporary file to the uploads directory, request it from the web, and delete it.
Method of the class: SiteHealth{}
No Hooks.
Returns
bool|null. Whether the file's contents were served, or null if the check could not run.
Usage
// private - for code of main (parent) class only $result = $this->is_uploads_test_file_served( $subdir ): ?bool;
- $subdir(string) (required)
- Directory relative to the uploads base directory, with a trailing slash, or
''.
SiteHealth::is_uploads_test_file_served() SiteHealth::is uploads test file served code WC 11.3.0
private function is_uploads_test_file_served( string $subdir ): ?bool {
try {
$file_name = 'wc-site-health-' . bin2hex( random_bytes( 6 ) ) . '.txt';
$contents = bin2hex( random_bytes( 16 ) );
$filesystem = FilesystemUtil::get_wp_filesystem_direct();
} catch ( Exception $e ) {
return null;
}
$uploads = wp_get_upload_dir();
$file_path = $uploads['basedir'] . '/' . $subdir . $file_name;
if ( ! $filesystem->put_contents( $file_path, $contents ) ) {
return null;
}
$response = wp_safe_remote_get( esc_url_raw( $uploads['baseurl'] . '/' . $subdir . $file_name ) );
$filesystem->delete( $file_path );
if ( is_wp_error( $response ) ) {
return null;
}
$response_code = intval( wp_remote_retrieve_response_code( $response ) );
if ( 0 === $response_code ) {
return null;
}
// Comparing contents means a login page or soft 404 reached through a redirect doesn't count as served.
return 200 === $response_code && trim( wp_remote_retrieve_body( $response ) ) === $contents;
}