diff --git a/README.md b/README.md index b982c45..5815411 100644 --- a/README.md +++ b/README.md @@ -12,15 +12,15 @@ ## 插件特色 -* [x] 可配置是否上传缩略图和是否保留本地备份 -* [x] 本地删除可同步删除阿里云对象存储OSS中的文件 -* [x] 支持阿里云对象存储OSS绑定的用户域名 -* [x] 支持替换数据库中旧的资源链接地址 -* [x] 支持阿里云对象存储OSS完整地域使用 -* [x] 支持同步历史附件到阿里云对象存储OSS -* [x] 支持阿里云OSS图片处理 -* [x] 支持上传文件自动重命名 -* [x] 支持使用RAM操作 +- [x] 可配置是否上传缩略图和是否保留本地备份 +- [x] 本地删除可同步删除阿里云对象存储 OSS 中的文件 +- [x] 支持阿里云对象存储 OSS 绑定的用户域名 +- [x] 支持替换数据库中旧的资源链接地址 +- [x] 支持阿里云对象存储 OSS 完整地域使用 +- [x] 支持同步历史附件到阿里云对象存储 OSS +- [x] 支持阿里云 OSS 图片处理 +- [x] 支持上传文件自动重命名 +- [x] 支持使用 RAM 操作 ## 安装 @@ -36,21 +36,21 @@ GitHub 下载节点:[https://github.com/sy-records/aliyun-oss-wordpress/releas ## 修改配置 -* 方法一:在 WordPress 插件管理页面有设置按钮,进行设置 -* 方法二:在 WordPress 后台管理左侧导航栏`设置`下`阿里云OSS设置`,点击进入设置页面 +- 方法一:在 WordPress 插件管理页面有设置按钮,进行设置 +- 方法二:在 WordPress 后台管理左侧导航栏`设置`下`阿里云OSS设置`,点击进入设置页面 ## 常见问题 -* 通过文件URL访问图片无法预览而是以附件形式下载? +- 通过文件 URL 访问图片无法预览而是以附件形式下载? -因为Bucket是2019年9月23日后创建的,使用默认域名时会自动下载,需要绑定自有域名访问。具体参考[阿里云文档](https://help.aliyun.com/document_detail/142631.html)。 +因为 Bucket 是 2019 年 9 月 23 日后创建的,使用默认域名时会自动下载,需要绑定自有域名访问。具体参考[阿里云文档](https://help.aliyun.com/document_detail/142631.html)。 -* 如果存在第三方插件或者主题自带上传功能,内容上传到`本地文件夹`(即默认`wp-content/uploads`)中,怎么上传到oss中? +- 如果存在第三方插件或者主题自带上传功能,内容上传到`本地文件夹`(即默认`wp-content/uploads`)中,怎么上传到 oss 中? 解决方案有两种,推荐使用第二种。 一是修改第三方插件或者主题的上传功能,调用插件的`oss_file_upload`方法(不推荐,一般人不会修改) -二是使用对象存储OSS提供的回源功能,配置为镜像方式。如果配置了镜像回源,当用户对该存储空间内一个不存在的文件进行GET操作时,OSS会向回源地址请求这个文件,返回给用户,同时会将该文件存入OSS。这样就达到了上传到OSS的需求。具体配置参考阿里云文档[设置回源规则](https://help.aliyun.com/document_detail/31906.html) +二是使用对象存储 OSS 提供的回源功能,配置为镜像方式。如果配置了镜像回源,当用户对该存储空间内一个不存在的文件进行 GET 操作时,OSS 会向回源地址请求这个文件,返回给用户,同时会将该文件存入 OSS。这样就达到了上传到 OSS 的需求。具体配置参考阿里云文档[设置回源规则](https://help.aliyun.com/document_detail/31906.html) ## 插件截图 diff --git a/aliyun-oss-wordpress.php b/aliyun-oss-wordpress.php index 03ed281..d0c1506 100644 --- a/aliyun-oss-wordpress.php +++ b/aliyun-oss-wordpress.php @@ -3,10 +3,10 @@ Plugin Name: OSS Aliyun Plugin URI: https://github.com/sy-records/aliyun-oss-wordpress Description: 使用阿里云对象存储 OSS 作为附件存储空间。(This is a plugin that uses Aliyun Object Storage Service for attachments remote saving.) -Version: 1.4.3 +Version: 1.4.4 Author: 沈唁 Author URI: https://qq52o.me -License: Apache 2.0 +License: Apache2.0 */ if (!defined('ABSPATH')) { exit; @@ -20,7 +20,7 @@ use OSS\Credentials\CredentialsProvider; use AlibabaCloud\Credentials\Credential; use OSS\Credentials\StaticCredentialsProvider; -define('OSS_VERSION', '1.4.3'); +define('OSS_VERSION', '1.4.4'); define('OSS_BASEFOLDER', plugin_basename(dirname(__FILE__))); if (!function_exists('get_home_path')) { @@ -111,9 +111,9 @@ function oss_get_bucket_name() } /** - * @param $object - * @param $file - * @param false $no_local_file + * @param string $object + * @param string $file + * @param bool $no_local_file */ function oss_file_upload($object, $file, $no_local_file = false) { @@ -124,7 +124,7 @@ function oss_file_upload($object, $file, $no_local_file = false) $bucket = oss_get_bucket_name(); $ossClient = oss_get_client(); try{ - $ossClient->uploadFile($bucket, ltrim($object, "/"), $file); + $ossClient->uploadFile($bucket, ltrim($object, '/'), $file); } catch (OssException $e) { if (WP_DEBUG) { echo 'Error Message: ', $e->getMessage(), PHP_EOL, 'Error Code: ', $e->getCode(); @@ -254,8 +254,12 @@ function oss_upload_thumbs($metadata) //获取上传路径 $wp_uploads = wp_upload_dir(); $basedir = $wp_uploads['basedir']; + //获取oss插件的配置信息 $oss_options = get_option('oss_options', true); + $no_local_file = esc_attr($oss_options['nolocalsaving']) == 'true'; + $no_thumb = esc_attr($oss_options['nothumb']) == 'true'; + if (!empty($metadata['file'])) { // Maybe there is a problem with the old version $file = $basedir . '/' . $metadata['file']; @@ -270,26 +274,28 @@ function oss_upload_thumbs($metadata) $file = str_replace('./', '', $file); } - oss_file_upload($object, $file, esc_attr($oss_options['nolocalsaving']) == 'true'); + oss_file_upload($object, $file, $no_local_file); } + + //得到本地文件夹和远端文件夹 + $dirname = dirname($metadata['file']); + $file_path = $dirname != '.' ? "{$basedir}/{$dirname}/" : "{$basedir}/"; + $file_path = str_replace("\\", '/', $file_path); + if ($upload_path == '.') { + $file_path = str_replace('./', '', $file_path); + } + $object_path = str_replace(get_home_path(), '', $file_path); + + if (!empty($metadata['original_image'])) { + oss_file_upload("/{$object_path}{$metadata['original_image']}", "{$file_path}{$metadata['original_image']}", $no_local_file); + } + + if ($no_thumb) { + return $metadata; + } + //上传所有缩略图 if (!empty($metadata['sizes'])) { - //是否需要上传缩略图 - $nothumb = esc_attr($oss_options['nothumb']) == 'true'; - //如果禁止上传缩略图,就不用继续执行了 - if ($nothumb) { - return $metadata; - } - //得到本地文件夹和远端文件夹 - $dirname = dirname($metadata['file']); - $file_path = $dirname != '.' ? "{$basedir}/{$dirname}/" : "{$basedir}/"; - $file_path = str_replace("\\", '/', $file_path); - if ($upload_path == '.') { - $file_path = str_replace('./', '', $file_path); - } - - $object_path = str_replace(get_home_path(), '', $file_path); - //there may be duplicated filenames,so .... foreach ($metadata['sizes'] as $val) { //生成object在oss中的存储路径 @@ -298,7 +304,7 @@ function oss_upload_thumbs($metadata) $file = $file_path . $val['file']; //执行上传操作 - oss_file_upload($object, $file, esc_attr($oss_options['nolocalsaving']) == 'true'); + oss_file_upload($object, $file, $no_local_file); } } return $metadata; @@ -351,11 +357,17 @@ function oss_delete_remote_attachment($post_id) $deleteObjects[] = str_replace("\\", '/', $file_path); + $dirname = dirname($file_path) . '/'; + + // 超大图原图 + if (!empty($meta['original_image'])) { + $deleteObjects[] = ['Key' => str_replace("\\", '/', $dirname . $meta['original_image'])]; + } + // 删除缩略图 if (!empty($meta['sizes'])) { foreach ($meta['sizes'] as $val) { - $size_file = dirname($file_path) . '/' . $val['file']; - $deleteObjects[] = str_replace("\\", '/', $size_file); + $deleteObjects[] = str_replace("\\", '/', $dirname . $val['file']); } } @@ -595,12 +607,12 @@ function oss_setting_page() $new_url = esc_url_raw($_POST['new_url']); global $wpdb; - $posts_name = $wpdb->prefix .'posts'; + $posts_name = $wpdb->prefix . 'posts'; // 文章内容 $posts_result = $wpdb->query("UPDATE $posts_name SET post_content = REPLACE(post_content, '$old_url', '$new_url')"); // 修改题图之类的 - $postmeta_name = $wpdb->prefix .'postmeta'; + $postmeta_name = $wpdb->prefix . 'postmeta'; $postmeta_result = $wpdb->query("UPDATE $postmeta_name SET meta_value = REPLACE(meta_value, '$old_url', '$new_url')"); echo '

替换成功!共替换文章内链'.$posts_result.'条、题图链接'.$postmeta_result.'条!

'; @@ -634,7 +646,7 @@ function oss_setting_page() $oss_update_file_name = esc_attr($oss_options['update_file_name']); $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://'; - ?> +?>

阿里云 OSS 当前版本:

如果觉得此插件对你有所帮助,不妨到 GitHub 上点个StarWatch关注更新;欢迎加入云存储插件交流群,QQ群号:887595381

@@ -772,7 +784,7 @@ function oss_setting_page() 保存/更新选项 - + @@ -785,7 +797,7 @@ function oss_setting_page() - +

注意:如果是首次同步,执行时间将会十分十分长(根据你的历史附件数量),有可能会因执行时间过长,页面显示超时或者报错。
所以,建议那些几千上万附件的大神们,考虑官方的 同步工具

@@ -816,7 +828,7 @@ function oss_setting_page() - +

注意:如果是首次替换,请注意备份!此功能会替换文章以及设置的特色图片(题图)等使用的资源链接

diff --git a/readme.txt b/readme.txt index 1bb5b5e..34d4e7a 100644 --- a/readme.txt +++ b/readme.txt @@ -5,8 +5,8 @@ Tags: oss, 阿里云, 对象存储, aliyun Requires at least: 4.2 Tested up to: 6.3 Requires PHP: 7.0.0 -Stable tag: 1.4.3 -License: Apache 2.0 +Stable tag: 1.4.4 +License: Apache2.0 License URI: http://www.apache.org/licenses/LICENSE-2.0.html 使用阿里云对象存储 OSS 作为附件存储空间。(This is a plugin that uses Aliyun Object Storage Service for attachments remote saving.) @@ -15,32 +15,33 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html 使用阿里云对象存储 OSS 作为附件存储空间。(This is a plugin that uses Aliyun Object Storage Service for attachments remote saving.) -* 依赖阿里云OSS服务:https://www.aliyun.com/product/oss +- 依赖阿里云 OSS 服务:https://www.aliyun.com/product/oss ## 插件特点 1. 可配置是否上传缩略图和是否保留本地备份 -2. 本地删除可同步删除阿里云对象存储OSS中的文件 -3. 支持阿里云对象存储OSS绑定的用户域名 +2. 本地删除可同步删除阿里云对象存储 OSS 中的文件 +3. 支持阿里云对象存储 OSS 绑定的用户域名 4. 支持替换数据库中旧的资源链接地址 -5. 支持阿里云对象存储OSS完整地域使用 -6. 支持同步历史附件到阿里云对象存储OSS -7. 支持阿里云OSS图片处理 +5. 支持阿里云对象存储 OSS 完整地域使用 +6. 支持同步历史附件到阿里云对象存储 OSS +7. 支持阿里云 OSS 图片处理 8. 支持上传文件自动重命名 -9. 插件更多详细介绍和安装:[https://github.com/sy-records/aliyun-oss-wordpress](https://github.com/sy-records/aliyun-oss-wordpress) +9. 支持使用 RAM 操作 +10. 插件更多详细介绍和安装:[https://github.com/sy-records/aliyun-oss-wordpress](https://github.com/sy-records/aliyun-oss-wordpress) ## 其他插件 -腾讯云COS:[GitHub](https://github.com/sy-records/wordpress-qcloud-cos),[WordPress Plugins](https://wordpress.org/plugins/sync-qcloud-cos) -华为云OBS:[GitHub](https://github.com/sy-records/huaweicloud-obs-wordpress),[WordPress Plugins](https://wordpress.org/plugins/obs-huaweicloud) -七牛云KODO:[GitHub](https://github.com/sy-records/qiniu-kodo-wordpress),[WordPress Plugins](https://wordpress.org/plugins/kodo-qiniu) -又拍云USS:[GitHub](https://github.com/sy-records/upyun-uss-wordpress),[WordPress Plugins](https://wordpress.org/plugins/uss-upyun) +腾讯云 COS:[GitHub](https://github.com/sy-records/wordpress-qcloud-cos),[WordPress Plugins](https://wordpress.org/plugins/sync-qcloud-cos) +华为云 OBS:[GitHub](https://github.com/sy-records/huaweicloud-obs-wordpress),[WordPress Plugins](https://wordpress.org/plugins/obs-huaweicloud) +七牛云 KODO:[GitHub](https://github.com/sy-records/qiniu-kodo-wordpress),[WordPress Plugins](https://wordpress.org/plugins/kodo-qiniu) +又拍云 USS:[GitHub](https://github.com/sy-records/upyun-uss-wordpress),[WordPress Plugins](https://wordpress.org/plugins/uss-upyun) ## 作者博客 [沈唁志](https://qq52o.me "沈唁志") -欢迎加入沈唁的WordPress云存储全家桶QQ交流群:887595381 +欢迎加入沈唁的 WordPress 云存储全家桶 QQ 交流群:887595381 == Installation == @@ -50,8 +51,8 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html == Screenshots == -1. screenshot-1.png -2. screenshot-2.png +1. 设置页面 +2. 数据库同步 == Frequently Asked Questions == @@ -59,94 +60,118 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html 这个插件已经加上了替换数据库中之前的旧资源地址链接功能,只需要填好对应的链接即可 -= 通过文件URL访问图片无法预览而是以附件形式下载? = += 通过文件 URL 访问图片无法预览而是以附件形式下载? = -因为Bucket是2019年9月23日后创建的,使用默认域名时会自动下载,需要绑定自有域名访问。具体参考[阿里云文档](https://help.aliyun.com/document_detail/142631.html)。 +因为 Bucket 是 2019 年 9 月 23 日后创建的,使用默认域名时会自动下载,需要绑定自有域名访问。具体参考[阿里云文档](https://help.aliyun.com/document_detail/142631.html)。 -= 如果存在第三方插件或者主题自带上传功能,内容上传到本地文件夹(即默认wp-content/uploads)中,怎么上传到OSS中? = += 如果存在第三方插件或者主题自带上传功能,内容上传到本地文件夹(即默认 wp-content/uploads)中,怎么上传到 OSS 中? = 解决方案有两种,推荐使用第二种。 一是修改第三方插件或者主题的上传功能,调用插件的`oss_file_upload`方法(不推荐,一般人不会修改) -二是使用对象存储OSS提供的回源功能,配置为镜像方式。如果配置了镜像回源,当用户对该存储空间内一个不存在的文件进行GET操作时,OSS会向回源地址请求这个文件,返回给用户,同时会将该文件存入OSS。这样就达到了上传到OSS的需求。具体配置参考阿里云文档[设置回源规则](https://help.aliyun.com/document_detail/31906.html) +二是使用对象存储 OSS 提供的回源功能,配置为镜像方式。如果配置了镜像回源,当用户对该存储空间内一个不存在的文件进行 GET 操作时,OSS 会向回源地址请求这个文件,返回给用户,同时会将该文件存入 OSS。这样就达到了上传到 OSS 的需求。具体配置参考阿里云文档[设置回源规则](https://help.aliyun.com/document_detail/31906.html) == Changelog == += 1.4.4 = + +- 修复超大文件原图上传和删除 + = 1.4.3 = -* 修复同步错误 -* 更新地域 + +- 修复同步错误 +- 更新地域 = 1.4.2 = -* 优化同步代码逻辑 -* 修复 webp 和 heic 格式图片上传缩略图失败问题 + +- 优化同步代码逻辑 +- 修复 webp 和 heic 格式图片上传缩略图失败问题 = 1.4.1 = -* 支持媒体库编辑图片上传 + +- 支持媒体库编辑图片上传 = 1.4.0 = -* 支持 WordPress 6.3 版本 -* 支持 RAM 操作 OSS + +- 支持 WordPress 6.3 版本 +- 支持 RAM 操作 OSS = 1.3.2 = -* 添加地域 + +- 添加地域 = 1.3.1 = -* 优化代码 + +- 优化代码 = 1.3.0 = -* 增加地域 -* 优化 isset 判断 -* 优化访问权限 -* 修复存在同名 path 时截取错误 -* 修改 accessKeySecret 类型为 password + +- 增加地域 +- 优化 isset 判断 +- 优化访问权限 +- 修复存在同名 path 时截取错误 +- 修改 accessKeySecret 类型为 password = 1.2.8 = -* 支持上传文件自动重命名 -* 优化图片处理 + +- 支持上传文件自动重命名 +- 优化图片处理 = 1.2.7 = -* 增加地域 + +- 增加地域 = 1.2.6 = -* 升级 oss sdk -* 修复删除文件的request id异常 -* 支持 WordPress 5.8 版本 + +- 升级 oss sdk +- 修复删除文件的 request id 异常 +- 支持 WordPress 5.8 版本 = 1.2.5 = -* 修复当文章图片重复时导致添加多个样式 + +- 修复当文章图片重复时导致添加多个样式 = 1.2.4 = -* 添加 get_home_path 方法判断 -* 支持 WordPress 5.7 版本 + +- 添加 get_home_path 方法判断 +- 支持 WordPress 5.7 版本 = 1.2.3 = -* 支持删除非图片类型文件 + +- 支持删除非图片类型文件 = 1.2.2 = -* 支持 WordPress 5.6 版本 -* 升级 OSS SDK -* 修复勾选不上传缩略图删除时不会删除已存在的缩略图 + +- 支持 WordPress 5.6 版本 +- 升级 OSS SDK +- 修复勾选不上传缩略图删除时不会删除已存在的缩略图 = 1.2.1 = -* 支持阿里云OSS图片处理 + +- 支持阿里云 OSS 图片处理 = 1.2.0 = -* 优化同步上传路径获取 -* 修复多站点上传原图失败,缩略图正常问题 -* 优化上传路径获取 -* 增加数据库题图链接替换 + +- 优化同步上传路径获取 +- 修复多站点上传原图失败,缩略图正常问题 +- 优化上传路径获取 +- 增加数据库题图链接替换 = 1.1.1 = -* 修复本地文件夹为根目录时路径错误 -* 减少一次获取配置代码... -* 增加回源说明 + +- 修复本地文件夹为根目录时路径错误 +- 减少一次获取配置代码... +- 增加回源说明 = 1.1.0 = -* 优化删除文件使用删除多个接口 -* 修复勾选不在本地保存图片后媒体库显示默认图片问题 + +- 优化删除文件使用删除多个接口 +- 修复勾选不在本地保存图片后媒体库显示默认图片问题 = 1.0.1 = -* 修复勾选不在本地保存图片后媒体库显示默认图片问题 + +- 修复勾选不在本地保存图片后媒体库显示默认图片问题 = 1.0 = -* First version + +- First version