feat: create function `get_file_name_from_url`
This commit is contained in:
parent
4f7042f422
commit
77ef02650b
|
@ -52,6 +52,28 @@ class Media {
|
|||
// flush();
|
||||
}
|
||||
|
||||
private function get_file_name_from_url($url) {
|
||||
$response = wp_remote_head($url);
|
||||
if (is_wp_error($response)) {
|
||||
return basename($url);
|
||||
}
|
||||
|
||||
$headers = wp_remote_retrieve_headers($response);
|
||||
$fileName = null;
|
||||
|
||||
if (isset($headers['content-disposition'])) {
|
||||
if (preg_match('/filename="([^"]+)"/', $headers['content-disposition'], $matches)) {
|
||||
$fileName = $matches[1];
|
||||
}
|
||||
}
|
||||
|
||||
if (!$fileName) {
|
||||
$fileName = basename($path);
|
||||
}
|
||||
|
||||
return $fileName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert an attachment from an URL address.
|
||||
*
|
||||
|
@ -73,7 +95,7 @@ class Media {
|
|||
return false;
|
||||
}
|
||||
|
||||
return $this->insert_attachment_from_blob($file, basename($url), $post_id);
|
||||
return $this->insert_attachment_from_blob($file, $this->get_file_name_from_url($url), $post_id);
|
||||
} catch (\Exception $e) {
|
||||
error_log($e);
|
||||
return false;
|
||||
|
|
Loading…
Reference in New Issue