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.'条!
如果觉得此插件对你有所帮助,不妨到 GitHub 上点个Star
,Watch
关注更新;欢迎加入云存储插件交流群,QQ群号:887595381;
注意:如果是首次同步,执行时间将会十分十分长(根据你的历史附件数量),有可能会因执行时间过长,页面显示超时或者报错。
所以,建议那些几千上万附件的大神们,考虑官方的 同步工具
注意:如果是首次替换,请注意备份!此功能会替换文章以及设置的特色图片(题图)等使用的资源链接