From 47adce6c74489be884b39b515f69f8d694a39889 Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Thu, 18 Feb 2021 10:41:37 +0800 Subject: [PATCH] Update delete file --- aliyun-oss-wordpress.php | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/aliyun-oss-wordpress.php b/aliyun-oss-wordpress.php index 79084b3..66fa57b 100644 --- a/aliyun-oss-wordpress.php +++ b/aliyun-oss-wordpress.php @@ -122,8 +122,8 @@ function oss_delete_local_file($file) /** * 删除oss中的单个文件 - * @param $file - * @return bool + * + * @param string $file */ function oss_delete_oss_file($file) { @@ -134,7 +134,7 @@ function oss_delete_oss_file($file) /** * 批量删除文件 - * @param $files + * @param array $files */ function oss_delete_oss_files(array $files) { @@ -252,7 +252,9 @@ if (substr_count($_SERVER['REQUEST_URI'], '/update.php') <= 0) { * 删除远端文件,删除文件时触发 * @param $post_id */ -function oss_delete_remote_attachment($post_id) { +function oss_delete_remote_attachment($post_id) +{ + // 获取图片类附件的meta信息 $meta = wp_get_attachment_metadata( $post_id ); if (isset($meta['file'])) { @@ -280,6 +282,25 @@ function oss_delete_remote_attachment($post_id) { // } oss_delete_oss_files($deleteObjects); + } else { + // 获取链接删除 + $link = wp_get_attachment_url($post_id); + if ($link) { + $upload_path = get_option('upload_path'); + if ($upload_path != '.') { + $file_info = explode($upload_path, $link); + if (isset($file_info[1])) { + oss_delete_oss_file($upload_path . $file_info[1]); + } + } else { + $oss_options = get_option('oss_options', true); + $oss_upload_url = esc_attr($oss_options['upload_url_path']); + $file_info = explode($oss_upload_url, $link); + if (isset($file_info[1])) { + oss_delete_oss_file($file_info[1]); + } + } + } } } add_action('delete_attachment', 'oss_delete_remote_attachment');